about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/libraries/science/math/tensorflow-lite/default.nix
blob: cf2346bad271ef9f255833f9dc7cf051d74fc2cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
{ stdenv
, bash
, abseil-cpp
, fetchFromGitHub
, fetchFromGitLab
, fetchpatch
, fetchurl
, flatbuffers
, hostPlatform
, lib
, zlib
}:
let
  tflite-eigen = fetchFromGitLab {
    owner = "libeigen";
    repo = "eigen";
    rev = "3d9051ea84a5089b277c88dac456b3b1576bfa7f";
    sha256 = "1y3f2jvimb5i904f4n37h23cv2pkdlbz8656s0kga1y7c0p50wif";
  };

  gemmlowp-src = fetchFromGitHub {
    owner = "google";
    repo = "gemmlowp";
    rev = "fda83bdc38b118cc6b56753bd540caa49e570745";
    sha256 = "1sbp8kmr2azwlvfbzryy1frxi99jhsh1nc93bdbxdf8zdgpv0kxl";
  };

  neon-2-sse-src = fetchFromGitHub {
    owner = "intel";
    repo = "ARM_NEON_2_x86_SSE";
    rev = "1200fe90bb174a6224a525ee60148671a786a71f";
    sha256 = "0fhxch711ck809dpq1myxz63jiiwfcnxvj45ww0kg8s0pqpn5kv6";
  };

  farmhash-src = fetchFromGitHub {
    owner = "google";
    repo = "farmhash";
    rev = "816a4ae622e964763ca0862d9dbd19324a1eaf45";
    sha256 = "1mqxsljq476n1hb8ilkrpb39yz3ip2hnc7rhzszz4sri8ma7qzp6";
  };

  fft2d-src = fetchurl {
    url = "http://www.kurims.kyoto-u.ac.jp/~ooura/fft2d.tgz";
    sha256 = "ada7e99087c4ed477bfdf11413f2ba8db8a840ba9bbf8ac94f4f3972e2a7cec9";
  };

  fp16-src = fetchFromGitHub {
    owner = "Maratyszcza";
    repo = "FP16";
    rev = "4dfe081cf6bcd15db339cf2680b9281b8451eeb3";
    sha256 = "06a8dfl3a29r93nxpp6hpywsajz5d555n3sqd3i6krybb6swnvh7";
  };

  ruy-src = fetchFromGitHub {
    owner = "google";
    repo = "ruy";
    rev = "23633b37099b614a2f836ef012cafc8087fdb98c";
    sha256 = "14k9hz6ss8qy8nsajk6lrq25f6qxrldxky31ijw0dpqnfnnswrx4";
  };

  cpuinfo-src = fetchFromGitHub {
    owner = "pytorch";
    repo = "cpuinfo";
    rev = "5916273f79a21551890fd3d56fc5375a78d1598d";
    sha256 = "0q6760xdxsg18acdv8vq3yrq7ksr7wsm8zbyan01zf2khnb6fw4x";
  };
in
stdenv.mkDerivation rec {
  pname = "tensorflow-lite";
  version = "2.5.0";

  src = fetchFromGitHub {
    owner = "tensorflow";
    repo = "tensorflow";
    rev = "v${version}";
    sha256 = "1jdw2i1rq06zqd6aabh7bbm0avsg4pygnfmd7gviv0blhih9054l";
  };

  patches = [
    # TODO: remove on the next version bump
    (fetchpatch {
      name = "include-schema-conversion-utils-source.patch";
      url = "https://github.com/tensorflow/tensorflow/commit/f3c4f4733692150fd6174f2cd16438cfaba2e5ab.patch";
      sha256 = "0zx4hbz679kn79f30159rl1mq74dg45cvaawii0cyv48z472yy4k";
    })
    # TODO: remove on the next version bump
    (fetchpatch {
      name = "cxxstandard-var.patch";
      url = "https://github.com/tensorflow/tensorflow/commit/9b128ae4200e10b4752f903492d1e7d11957ed5c.patch";
      sha256 = "1q0izdwdji5fbyqll6k4dmkzfykyvvz5cvc6hysdj285nkn2wy6h";
    })
  ];

  buildInputs = [ zlib flatbuffers ];

  dontConfigure = true;

  postPatch = ''
    substituteInPlace ./tensorflow/lite/tools/make/Makefile \
      --replace /bin/bash ${bash}/bin/bash \
      --replace /bin/sh ${bash}/bin/sh
  '';

  makefile = "tensorflow/lite/tools/make/Makefile";

  preBuild =
    let
      includes =
        lib.concatMapStringsSep
          " "
          (subdir: "-I $PWD/tensorflow/lite/tools/make/downloads/${subdir}")
          [
            "neon_2_sse"
            "gemmlowp"
            "absl"
            "fp16/include"
            "farmhash/src"
            "ruy"
            "cpuinfo"
            "cpuinfo/src"
            "cpuinfo/include"
            "cpuinfo/deps/clog/include"
            "eigen"
          ];
    in
    ''
      # enter the vendoring lair of doom

      prefix="$PWD/tensorflow/lite/tools/make/downloads"

      mkdir -p "$prefix"

      tar xzf ${fft2d-src} -C "$prefix"

      ln -s ${ruy-src} "$prefix/ruy"
      ln -s ${gemmlowp-src} "$prefix/gemmlowp"
      ln -s ${neon-2-sse-src} "$prefix/neon_2_sse"
      ln -s ${farmhash-src} "$prefix/farmhash"
      ln -s ${cpuinfo-src} "$prefix/cpuinfo"
      ln -s ${fp16-src} "$prefix/fp16"
      ln -s ${tflite-eigen} "$prefix/eigen"

      # tensorflow lite is using the *source* of flatbuffers
      ln -s ${flatbuffers.src} "$prefix/flatbuffers"

      # tensorflow lite expects to compile abseil into `libtensorflow-lite.a`
      ln -s ${abseil-cpp.src} "$prefix/absl"

      # set CXXSTANDARD=c++17 here because abseil-cpp in nixpkgs is set as
      # such and would be used in dependents like libedgetpu
      buildFlagsArray+=(
        INCLUDES="-I $PWD ${includes}"
        CXXSTANDARD="-std=c++17"
        TARGET_TOOLCHAIN_PREFIX=""
        -j$NIX_BUILD_CORES
        all)
    '';

  installPhase = ''
    mkdir "$out"

    # copy the static lib and binaries into the output dir
    cp -r ./tensorflow/lite/tools/make/gen/linux_${hostPlatform.uname.processor}/{bin,lib} "$out"

    find ./tensorflow/lite -type f -name '*.h' | while read f; do
      path="$out/include/''${f/.\//}"
      install -D "$f" "$path"

      # remove executable bit from headers
      chmod -x "$path"
    done
  '';

  meta = with lib; {
    description = "An open source deep learning framework for on-device inference.";
    homepage = "https://www.tensorflow.org/lite";
    license = licenses.asl20;
    maintainers = with maintainers; [ cpcloud ];
    platforms = [ "x86_64-linux" "aarch64-linux" ];
  };
}