Merge pull request #240333 from mschwaig/tflite-bazel

authored by Sandro and committed by GitHub e2c84aaf eddd37fa

+73 -166
+6
maintainers/maintainer-list.nix
··· 11318 name = "Maxim Schuwalow"; 11319 email = "maxim.schuwalow@gmail.com"; 11320 }; 11321 msfjarvis = { 11322 github = "msfjarvis"; 11323 githubId = 13348378;
··· 11318 name = "Maxim Schuwalow"; 11319 email = "maxim.schuwalow@gmail.com"; 11320 }; 11321 + mschwaig = { 11322 + name = "Martin Schwaighofer"; 11323 + github = "mschwaig"; 11324 + githubId = 3856390; 11325 + email = "mschwaig+nixpkgs@eml.cc"; 11326 + }; 11327 msfjarvis = { 11328 github = "msfjarvis"; 11329 githubId = 13348378;
+67 -166
pkgs/development/libraries/science/math/tensorflow-lite/default.nix
··· 1 { stdenv 2 , bash 3 - , abseil-cpp 4 , fetchFromGitHub 5 - , fetchFromGitLab 6 - , fetchpatch 7 - , fetchurl 8 - , flatbuffers 9 , lib 10 - , zlib 11 }: 12 let 13 - tflite-eigen = fetchFromGitLab { 14 - owner = "libeigen"; 15 - repo = "eigen"; 16 - rev = "3d9051ea84a5089b277c88dac456b3b1576bfa7f"; 17 - sha256 = "1y3f2jvimb5i904f4n37h23cv2pkdlbz8656s0kga1y7c0p50wif"; 18 }; 19 - 20 - gemmlowp-src = fetchFromGitHub { 21 - owner = "google"; 22 - repo = "gemmlowp"; 23 - rev = "fda83bdc38b118cc6b56753bd540caa49e570745"; 24 - sha256 = "1sbp8kmr2azwlvfbzryy1frxi99jhsh1nc93bdbxdf8zdgpv0kxl"; 25 - }; 26 - 27 - neon-2-sse-src = fetchFromGitHub { 28 - owner = "intel"; 29 - repo = "ARM_NEON_2_x86_SSE"; 30 - rev = "1200fe90bb174a6224a525ee60148671a786a71f"; 31 - sha256 = "0fhxch711ck809dpq1myxz63jiiwfcnxvj45ww0kg8s0pqpn5kv6"; 32 - }; 33 - 34 - farmhash-src = fetchFromGitHub { 35 - owner = "google"; 36 - repo = "farmhash"; 37 - rev = "816a4ae622e964763ca0862d9dbd19324a1eaf45"; 38 - sha256 = "1mqxsljq476n1hb8ilkrpb39yz3ip2hnc7rhzszz4sri8ma7qzp6"; 39 - }; 40 - 41 - fft2d-src = fetchurl { 42 - url = "http://www.kurims.kyoto-u.ac.jp/~ooura/fft2d.tgz"; 43 - sha256 = "ada7e99087c4ed477bfdf11413f2ba8db8a840ba9bbf8ac94f4f3972e2a7cec9"; 44 - }; 45 - 46 - fp16-src = fetchFromGitHub { 47 - owner = "Maratyszcza"; 48 - repo = "FP16"; 49 - rev = "4dfe081cf6bcd15db339cf2680b9281b8451eeb3"; 50 - sha256 = "06a8dfl3a29r93nxpp6hpywsajz5d555n3sqd3i6krybb6swnvh7"; 51 - }; 52 - 53 - ruy-src = fetchFromGitHub { 54 - owner = "google"; 55 - repo = "ruy"; 56 - rev = "23633b37099b614a2f836ef012cafc8087fdb98c"; 57 - sha256 = "14k9hz6ss8qy8nsajk6lrq25f6qxrldxky31ijw0dpqnfnnswrx4"; 58 - }; 59 - 60 - cpuinfo-src = fetchFromGitHub { 61 - owner = "pytorch"; 62 - repo = "cpuinfo"; 63 - rev = "5916273f79a21551890fd3d56fc5375a78d1598d"; 64 - sha256 = "0q6760xdxsg18acdv8vq3yrq7ksr7wsm8zbyan01zf2khnb6fw4x"; 65 - }; 66 in 67 - stdenv.mkDerivation rec { 68 - pname = "tensorflow-lite"; 69 - version = "2.5.0"; 70 71 src = fetchFromGitHub { 72 owner = "tensorflow"; 73 repo = "tensorflow"; 74 rev = "v${version}"; 75 - sha256 = "1jdw2i1rq06zqd6aabh7bbm0avsg4pygnfmd7gviv0blhih9054l"; 76 }; 77 78 - patches = [ 79 - # TODO: remove on the next version bump 80 - (fetchpatch { 81 - name = "include-schema-conversion-utils-source.patch"; 82 - url = "https://github.com/tensorflow/tensorflow/commit/f3c4f4733692150fd6174f2cd16438cfaba2e5ab.patch"; 83 - sha256 = "0zx4hbz679kn79f30159rl1mq74dg45cvaawii0cyv48z472yy4k"; 84 - }) 85 - # TODO: remove on the next version bump 86 - (fetchpatch { 87 - name = "cxxstandard-var.patch"; 88 - url = "https://github.com/tensorflow/tensorflow/commit/9b128ae4200e10b4752f903492d1e7d11957ed5c.patch"; 89 - sha256 = "1q0izdwdji5fbyqll6k4dmkzfykyvvz5cvc6hysdj285nkn2wy6h"; 90 - }) 91 - ]; 92 93 - buildInputs = [ zlib flatbuffers ]; 94 95 - dontConfigure = true; 96 97 - postPatch = '' 98 - substituteInPlace ./tensorflow/lite/tools/make/Makefile \ 99 - --replace /bin/bash ${bash}/bin/bash \ 100 - --replace /bin/sh ${bash}/bin/sh 101 - ''; 102 - 103 - makefile = "tensorflow/lite/tools/make/Makefile"; 104 - 105 - preBuild = 106 - let 107 - includes = 108 - lib.concatMapStringsSep 109 - " " 110 - (subdir: "-I $PWD/tensorflow/lite/tools/make/downloads/${subdir}") 111 - [ 112 - "neon_2_sse" 113 - "gemmlowp" 114 - "absl" 115 - "fp16/include" 116 - "farmhash/src" 117 - "ruy" 118 - "cpuinfo" 119 - "cpuinfo/src" 120 - "cpuinfo/include" 121 - "cpuinfo/deps/clog/include" 122 - "eigen" 123 - ]; 124 - in 125 - '' 126 - # enter the vendoring lair of doom 127 - 128 - prefix="$PWD/tensorflow/lite/tools/make/downloads" 129 130 - mkdir -p "$prefix" 131 132 - tar xzf ${fft2d-src} -C "$prefix" 133 134 - ln -s ${ruy-src} "$prefix/ruy" 135 - ln -s ${gemmlowp-src} "$prefix/gemmlowp" 136 - ln -s ${neon-2-sse-src} "$prefix/neon_2_sse" 137 - ln -s ${farmhash-src} "$prefix/farmhash" 138 - ln -s ${cpuinfo-src} "$prefix/cpuinfo" 139 - ln -s ${fp16-src} "$prefix/fp16" 140 - ln -s ${tflite-eigen} "$prefix/eigen" 141 142 - # tensorflow lite is using the *source* of flatbuffers 143 - ln -s ${flatbuffers.src} "$prefix/flatbuffers" 144 - 145 - # tensorflow lite expects to compile abseil into `libtensorflow-lite.a` 146 - ln -s ${abseil-cpp.src} "$prefix/absl" 147 148 - # set CXXSTANDARD=c++17 here because abseil-cpp in nixpkgs is set as 149 - # such and would be used in dependents like libedgetpu 150 - buildFlagsArray+=( 151 - INCLUDES="-I $PWD ${includes}" 152 - CXXSTANDARD="-std=c++17" 153 - TARGET_TOOLCHAIN_PREFIX="" 154 - -j$NIX_BUILD_CORES 155 - all) 156 ''; 157 158 - installPhase = '' 159 - mkdir "$out" 160 161 - # copy the static lib and binaries into the output dir 162 - cp -r ./tensorflow/lite/tools/make/gen/linux_${stdenv.hostPlatform.uname.processor}/{bin,lib} "$out" 163 164 - find ./tensorflow/lite -type f -name '*.h' | while read f; do 165 - path="$out/include/''${f/.\//}" 166 - install -D "$f" "$path" 167 168 - # remove executable bit from headers 169 - chmod -x "$path" 170 - done 171 ''; 172 173 meta = with lib; { 174 description = "An open source deep learning framework for on-device inference."; 175 homepage = "https://www.tensorflow.org/lite"; 176 license = licenses.asl20; 177 - maintainers = with maintainers; [ cpcloud ]; 178 platforms = [ "x86_64-linux" "aarch64-linux" ]; 179 - knownVulnerabilities = [ 180 - # at least some of 181 - "CVE-2023-27579" 182 - "CVE-2023-25801" 183 - "CVE-2023-25676" 184 - "CVE-2023-25675" 185 - "CVE-2023-25674" 186 - "CVE-2023-25673" 187 - "CVE-2023-25671" 188 - "CVE-2023-25670" 189 - "CVE-2023-25669" 190 - "CVE-2023-25668" 191 - "CVE-2023-25667" 192 - "CVE-2023-25665" 193 - "CVE-2023-25666" 194 - "CVE-2023-25664" 195 - "CVE-2023-25663" 196 - "CVE-2023-25662" 197 - "CVE-2023-25660" 198 - "CVE-2023-25659" 199 - "CVE-2023-25658" 200 - # and many many more 201 - ]; 202 }; 203 }
··· 1 { stdenv 2 , bash 3 + , buildPackages 4 + , buildBazelPackage 5 , fetchFromGitHub 6 , lib 7 }: 8 let 9 + buildPlatform = stdenv.buildPlatform; 10 + hostPlatform = stdenv.hostPlatform; 11 + pythonEnv = buildPackages.python3.withPackages (ps: [ ps.numpy ]); 12 + bazelDepsSha256ByBuildAndHost = { 13 + x86_64-linux = { 14 + x86_64-linux = "sha256-61qmnAB80syYhURWYJOiOnoGOtNa1pPkxfznrFScPAo="; 15 + aarch64-linux = "sha256-sOIYpp98wJRz3RGvPasyNEJ05W29913Lsm+oi/aq/Ag="; 16 + }; 17 + aarch64-linux = { 18 + aarch64-linux = "sha256-MJU4y9Dt9xJWKgw7iKW+9Ur856rMIHeFD5u05s+Q7rQ="; 19 + }; 20 }; 21 + bazelHostConfigName.aarch64-linux = "elinux_aarch64"; 22 + bazelDepsSha256ByHost = 23 + bazelDepsSha256ByBuildAndHost.${buildPlatform.system} or 24 + (throw "unsupported build system ${buildPlatform.system}"); 25 + bazelDepsSha256 = bazelDepsSha256ByHost.${hostPlatform.system} or 26 + (throw "unsupported host system ${hostPlatform.system} with build system ${buildPlatform.system}"); 27 in 28 + buildBazelPackage rec { 29 + name = "tensorflow-lite"; 30 + version = "2.13.0"; 31 32 src = fetchFromGitHub { 33 owner = "tensorflow"; 34 repo = "tensorflow"; 35 rev = "v${version}"; 36 + hash = "sha256-Rq5pAVmxlWBVnph20fkAwbfy+iuBNlfFy14poDPd5h0="; 37 }; 38 39 + bazel = buildPackages.bazel_5; 40 41 + nativeBuildInputs = [ pythonEnv buildPackages.perl ]; 42 43 + bazelTargets = [ 44 + "//tensorflow/lite:libtensorflowlite.so" 45 + "//tensorflow/lite/c:tensorflowlite_c" 46 + "//tensorflow/lite/tools/benchmark:benchmark_model" 47 + "//tensorflow/lite/tools/benchmark:benchmark_model_performance_options" 48 + ]; 49 50 + bazelFlags = [ 51 + "--config=opt" 52 + ] ++ lib.optionals (hostPlatform.system != buildPlatform.system) [ 53 + "--config=${bazelHostConfigName.${hostPlatform.system}}" 54 + ]; 55 56 + bazelBuildFlags = [ "--cxxopt=--std=c++17" ]; 57 58 + buildAttrs = { 59 + installPhase = '' 60 + mkdir -p $out/{bin,lib} 61 62 + # copy the libs and binaries into the output dir 63 + cp ./bazel-bin/tensorflow/lite/c/libtensorflowlite_c.so $out/lib 64 + cp ./bazel-bin/tensorflow/lite/libtensorflowlite.so $out/lib 65 + cp ./bazel-bin/tensorflow/lite/tools/benchmark/benchmark_model $out/bin 66 + cp ./bazel-bin/tensorflow/lite/tools/benchmark/benchmark_model_performance_options $out/bin 67 68 + find . -type f -name '*.h' | while read f; do 69 + path="$out/include/''${f/.\//}" 70 + install -D "$f" "$path" 71 72 + # remove executable bit from headers 73 + chmod -x "$path" 74 + done 75 ''; 76 + }; 77 78 + fetchAttrs.sha256 = bazelDepsSha256; 79 + 80 + PYTHON_BIN_PATH = pythonEnv.interpreter; 81 82 + dontAddBazelOpts = true; 83 + removeRulesCC = false; 84 85 + postPatch = '' 86 + rm .bazelversion 87 + ''; 88 89 + preConfigure = '' 90 + patchShebangs configure 91 ''; 92 93 + # configure script freaks out when parameters are passed 94 + dontAddPrefix = true; 95 + configurePlatforms = []; 96 + 97 meta = with lib; { 98 description = "An open source deep learning framework for on-device inference."; 99 homepage = "https://www.tensorflow.org/lite"; 100 license = licenses.asl20; 101 + maintainers = with maintainers; [ mschwaig cpcloud ]; 102 platforms = [ "x86_64-linux" "aarch64-linux" ]; 103 }; 104 }