diff options
Diffstat (limited to 'pkgs/development/python-modules/tensorflow')
-rw-r--r-- | pkgs/development/python-modules/tensorflow/bin.nix | 27 | ||||
-rw-r--r-- | pkgs/development/python-modules/tensorflow/binary-hashes.nix (renamed from pkgs/development/python-modules/tensorflow/tf1.14.0-hashes.nix) | 1 | ||||
-rw-r--r-- | pkgs/development/python-modules/tensorflow/default.nix | 340 | ||||
-rw-r--r-- | pkgs/development/python-modules/tensorflow/no-saved-proto.patch | 14 | ||||
-rwxr-xr-x[-rw-r--r--] | pkgs/development/python-modules/tensorflow/prefetcher.sh | 5 | ||||
-rw-r--r-- | pkgs/development/python-modules/tensorflow/system-jsoncpp.patch | 21 |
6 files changed, 331 insertions, 77 deletions
diff --git a/pkgs/development/python-modules/tensorflow/bin.nix b/pkgs/development/python-modules/tensorflow/bin.nix index d02a4e1b9f2..a562ba8f2fa 100644 --- a/pkgs/development/python-modules/tensorflow/bin.nix +++ b/pkgs/development/python-modules/tensorflow/bin.nix @@ -35,15 +35,19 @@ assert cudaSupport -> cudatoolkit != null && cudnn != null && nvidia_x11 != null; + +# unsupported combination +assert ! (stdenv.isDarwin && cudaSupport); + let - cudatoolkit_joined = symlinkJoin { - name = "unsplit_cudatoolkit"; - paths = [ cudatoolkit.out - cudatoolkit.lib ];}; + packages = import ./binary-hashes.nix; + + variant = if cudaSupport then "-gpu" else ""; + pname = "tensorflow${variant}"; in buildPythonPackage rec { - pname = "tensorflow"; - version = "1.14.0"; + inherit pname; + inherit (packages) version; format = "wheel"; src = let @@ -52,8 +56,7 @@ in buildPythonPackage rec { platform = if stdenv.isDarwin then "mac" else "linux"; unit = if cudaSupport then "gpu" else "cpu"; key = "${platform}_py_${pyver}_${unit}"; - dls = import (./. + "/tf${version}-hashes.nix"); - in fetchurl dls.${key}; + in fetchurl packages.${key}; propagatedBuildInputs = [ protobuf @@ -86,12 +89,12 @@ in buildPythonPackage rec { # patchelf --shrink-rpath will remove the cuda libraries. postFixup = let rpath = stdenv.lib.makeLibraryPath - ([ stdenv.cc.cc.lib zlib ] ++ lib.optionals cudaSupport [ cudatoolkit_joined cudnn nvidia_x11 ]); + ([ stdenv.cc.cc.lib zlib ] ++ lib.optionals cudaSupport [ cudatoolkit.out cudatoolkit.lib cudnn nvidia_x11 ]); in - lib.optionalString (stdenv.isLinux) '' + lib.optionalString stdenv.isLinux '' rrPath="$out/${python.sitePackages}/tensorflow/:$out/${python.sitePackages}/tensorflow/contrib/tensor_forest/:${rpath}" internalLibPath="$out/${python.sitePackages}/tensorflow/python/_pywrap_tensorflow_internal.so" - find $out -name '*${stdenv.hostPlatform.extensions.sharedLibrary}' -exec patchelf --set-rpath "$rrPath" {} \; + find $out \( -name '*.so' -or -name '*.so.*' \) -exec patchelf --set-rpath "$rrPath" {} \; ''; @@ -100,7 +103,7 @@ in buildPythonPackage rec { homepage = http://tensorflow.org; license = licenses.asl20; maintainers = with maintainers; [ jyp abbradar ]; - platforms = with platforms; linux ++ lib.optionals (!cudaSupport) darwin; + platforms = [ "x86_64-linux" "x86_64-darwin" ]; # Python 2.7 build uses different string encoding. # See https://github.com/NixOS/nixpkgs/pull/37044#issuecomment-373452253 broken = stdenv.isDarwin && !isPy3k; diff --git a/pkgs/development/python-modules/tensorflow/tf1.14.0-hashes.nix b/pkgs/development/python-modules/tensorflow/binary-hashes.nix index f6b367aea38..fa4809dc3f1 100644 --- a/pkgs/development/python-modules/tensorflow/tf1.14.0-hashes.nix +++ b/pkgs/development/python-modules/tensorflow/binary-hashes.nix @@ -1,4 +1,5 @@ { +version = "1.14.0"; linux_py_27_cpu = { url = "https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.14.0-cp27-none-linux_x86_64.whl"; sha256 = "0yywdrfk97dh1bxhibspg0raz70fx9lcczj6xlimqy4xb60clx7k"; diff --git a/pkgs/development/python-modules/tensorflow/default.nix b/pkgs/development/python-modules/tensorflow/default.nix index a8f902fdb38..43134fb746d 100644 --- a/pkgs/development/python-modules/tensorflow/default.nix +++ b/pkgs/development/python-modules/tensorflow/default.nix @@ -1,9 +1,16 @@ -{ stdenv, buildBazelPackage, lib, fetchFromGitHub, fetchpatch, symlinkJoin -, buildPythonPackage, isPy3k, pythonOlder, pythonAtLeast -, which, swig, binutils, glibcLocales -, python, jemalloc, openmpi -, numpy, six, protobuf, tensorflow-tensorboard, backports_weakref, mock, enum34, absl-py -, cudaSupport ? false, nvidia_x11 ? null, cudatoolkit ? null, cudnn ? null +{ stdenv, pkgs, buildBazelPackage, lib, fetchFromGitHub, fetchpatch, symlinkJoin +# Python deps +, buildPythonPackage, isPy3k, pythonOlder, pythonAtLeast, python +# Python libraries +, numpy, tensorflow-tensorboard, backports_weakref, mock, enum34, absl-py +, future, setuptools, wheel, keras-preprocessing, keras-applications, google-pasta +, termcolor, grpcio, six, wrapt, protobuf, tensorflow-estimator +# Common deps +, git, swig, which, binutils, glibcLocales, cython +# Common libraries +, jemalloc, openmpi, astor, gast, grpc, sqlite, openssl, jsoncpp, re2 +, curl, snappy, flatbuffers, icu, double-conversion, libpng, libjpeg, giflib +, cudaSupport ? false, nvidia_x11 ? null, cudatoolkit ? null, cudnn ? null, nccl ? null # XLA without CUDA is broken , xlaSupport ? cudaSupport # Default from ./configure script @@ -21,128 +28,333 @@ assert cudaSupport -> nvidia_x11 != null assert ! (stdenv.isDarwin && cudaSupport); let - withTensorboard = pythonOlder "3.6"; cudatoolkit_joined = symlinkJoin { - name = "${cudatoolkit.name}-unsplit"; + name = "${cudatoolkit.name}-merged"; paths = [ cudatoolkit.out cudatoolkit.lib ]; }; + cudatoolkit_cc_joined = symlinkJoin { + name = "${cudatoolkit.cc.name}-merged"; + paths = [ + cudatoolkit.cc + binutils.bintools # for ar, dwp, nm, objcopy, objdump, strip + ]; + }; + + # Needed for _some_ system libraries, grep INCLUDEDIR. + includes_joined = symlinkJoin { + name = "tensorflow-deps-merged"; + paths = [ + pkgs.protobuf + jsoncpp + ]; + }; + tfFeature = x: if x then "1" else "0"; - version = "1.5.0"; + version = "1.14.0"; + variant = if cudaSupport then "-gpu" else ""; + pname = "tensorflow${variant}"; + + # TODO: remove after there's support for setupPyDistFlags + setuppy = ../../../development/interpreters/python/run_setup.py; - pkg = buildBazelPackage rec { - name = "tensorflow-build-${version}"; + bazel-build = buildBazelPackage rec { + name = "${pname}-${version}"; src = fetchFromGitHub { owner = "tensorflow"; repo = "tensorflow"; rev = "v${version}"; - sha256 = "1c4djsaip901nasm7a6dsimr02bsv70a7b1g0kysb4n39qpdh22q"; + sha256 = "06jvwlsm14b8rqwd8q8796r0vmn0wk64s4ps2zg0sapkmp9vvcmi"; }; patches = [ - # Fix build with Bazel >= 0.10 + # Work around https://github.com/tensorflow/tensorflow/issues/24752 + ./no-saved-proto.patch + # Fixes for NixOS jsoncpp + ./system-jsoncpp.patch + + # https://github.com/tensorflow/tensorflow/pull/29673 (fetchpatch { - url = "https://github.com/tensorflow/tensorflow/commit/6fcfab770c2672e2250e0f5686b9545d99eb7b2b.patch"; - sha256 = "0p61za1mx3a7gj1s5lsps16fcw18iwnvq2b46v1kyqfgq77a12vb"; + name = "fix-compile-with-cuda-and-mpi.patch"; + url = "https://github.com/tensorflow/tensorflow/pull/29673/commits/498e35a3bfe38dd75cf1416a1a23c07c3b59e6af.patch"; + sha256 = "1m2qmwv1ysqa61z6255xggwbq6mnxbig749bdvrhnch4zydxb4di"; }) + + # https://github.com/tensorflow/tensorflow/issues/29220 (fetchpatch { - url = "https://github.com/tensorflow/tensorflow/commit/3f57956725b553d196974c9ad31badeb3eabf8bb.patch"; - sha256 = "11dja5gqy0qw27sc9b6yw9r0lfk8dznb32vrqqfcnypk2qmv26va"; + name = "bazel-0.27.patch"; + url = "https://github.com/tensorflow/tensorflow/commit/cfccbdb8c4a92dd26382419dceb4d934c2380391.patch"; + sha256 = "1l56wjia2c4685flsfkkgy471wx3c66wyv8khspv06zchj0k0liw"; }) ]; - nativeBuildInputs = [ swig which ]; + # On update, it can be useful to steal the changes from gentoo + # https://gitweb.gentoo.org/repo/gentoo.git/tree/sci-libs/tensorflow - buildInputs = [ python jemalloc openmpi glibcLocales numpy ] - ++ lib.optionals cudaSupport [ cudatoolkit cudnn nvidia_x11 ]; + nativeBuildInputs = [ + swig which cython + ]; + + buildInputs = [ + python + jemalloc + openmpi + glibcLocales + git + + # python deps needed during wheel build time + numpy + keras-preprocessing + protobuf + wrapt + gast + astor + absl-py + termcolor + keras-applications + + # libs taken from system through the TF_SYS_LIBS mechanism + grpc + sqlite + openssl + jsoncpp + pkgs.protobuf + curl + snappy + flatbuffers + icu + double-conversion + libpng + libjpeg + giflib + re2 + pkgs.lmdb + + # for building the wheel + setuptools + wheel + ] ++ lib.optionals (!isPy3k) [ + future + mock + ] ++ lib.optionals cudaSupport [ + cudatoolkit + cudnn + nvidia_x11 + ]; + + + # arbitrarily set to the current latest bazel version, overly careful + TF_IGNORE_MAX_BAZEL_VERSION = true; + + # Take as many libraries from the system as possible. Keep in sync with + # list of valid syslibs in + # https://github.com/tensorflow/tensorflow/blob/master/third_party/systemlibs/syslibs_configure.bzl + TF_SYSTEM_LIBS = lib.concatStringsSep "," [ + "absl_py" + "astor_archive" + "boringssl" + # Not packaged in nixpkgs + # "com_github_googleapis_googleapis" + # "com_github_googlecloudplatform_google_cloud_cpp" + "com_google_protobuf" + "com_google_protobuf_cc" + "com_googlesource_code_re2" + "curl" + "cython" + "double_conversion" + "flatbuffers" + "gast_archive" + "gif_archive" + "grpc" + "hwloc" + "icu" + "jpeg" + "jsoncpp_git" + "keras_applications_archive" + "lmdb" + "nasm" + # "nsync" # not packaged in nixpkgs + "org_sqlite" + "pasta" + "pcre" + "png_archive" + "protobuf_archive" + "six_archive" + "snappy" + "swig" + "termcolor_archive" + "wrapt" + "zlib_archive" + ]; - preConfigure = '' + INCLUDEDIR = "${includes_joined}/include"; + + PYTHON_BIN_PATH = python.interpreter; + + TF_NEED_GCP = true; + TF_NEED_HDFS = true; + TF_ENABLE_XLA = tfFeature xlaSupport; + + CC_OPT_FLAGS = " "; + + # https://github.com/tensorflow/tensorflow/issues/14454 + TF_NEED_MPI = tfFeature cudaSupport; + + TF_NEED_CUDA = tfFeature cudaSupport; + TF_CUDA_PATHS = lib.optionalString cudaSupport "${cudatoolkit_joined},${cudnn},${nccl}"; + GCC_HOST_COMPILER_PREFIX = lib.optionalString cudaSupport "${cudatoolkit_cc_joined}/bin"; + TF_CUDA_COMPUTE_CAPABILITIES = lib.concatStringsSep "," cudaCapabilities; + + postPatch = '' + # https://github.com/tensorflow/tensorflow/issues/20919 + sed -i '/androidndk/d' tensorflow/lite/kernels/internal/BUILD + + # Tensorboard pulls in a bunch of dependencies, some of which may + # include security vulnerabilities. So we make it optional. + # https://github.com/tensorflow/tensorflow/issues/20280#issuecomment-400230560 + sed -i '/tensorboard >=/d' tensorflow/tools/pip_package/setup.py + ''; + + preConfigure = let + opt_flags = [] + ++ lib.optionals sse42Support ["-msse4.2"] + ++ lib.optionals avx2Support ["-mavx2"] + ++ lib.optionals fmaSupport ["-mfma"]; + in '' patchShebangs configure - export PYTHON_BIN_PATH="${python.interpreter}" - export PYTHON_LIB_PATH="$NIX_BUILD_TOP/site-packages" - export TF_NEED_GCP=1 - export TF_NEED_HDFS=1 - export TF_ENABLE_XLA=${tfFeature xlaSupport} - export CC_OPT_FLAGS=" " - # https://github.com/tensorflow/tensorflow/issues/14454 - export TF_NEED_MPI=${tfFeature cudaSupport} - export TF_NEED_CUDA=${tfFeature cudaSupport} - ${lib.optionalString cudaSupport '' - export CUDA_TOOLKIT_PATH=${cudatoolkit_joined} - export TF_CUDA_VERSION=${cudatoolkit.majorVersion} - export CUDNN_INSTALL_PATH=${cudnn} - export TF_CUDNN_VERSION=${cudnn.majorVersion} - export GCC_HOST_COMPILER_PATH=${cudatoolkit.cc}/bin/gcc - export TF_CUDA_COMPUTE_CAPABILITIES=${lib.concatStringsSep "," cudaCapabilities} - ''} + # dummy ldconfig + mkdir dummy-ldconfig + echo "#!${stdenv.shell}" > dummy-ldconfig/ldconfig + chmod +x dummy-ldconfig/ldconfig + export PATH="$PWD/dummy-ldconfig:$PATH" + export PYTHON_LIB_PATH="$NIX_BUILD_TOP/site-packages" + export CC_OPT_FLAGS="${lib.concatStringsSep " " opt_flags}" mkdir -p "$PYTHON_LIB_PATH" ''; - NIX_LDFLAGS = lib.optionals cudaSupport [ "-lcublas" "-lcudnn" "-lcuda" "-lcudart" ]; + configurePhase = '' + runHook preConfigure + ./configure + runHook postConfigure + ''; - hardeningDisable = [ "all" ]; + # FIXME: Tensorflow uses dlopen() for CUDA libraries. + # No idea why gpr isn't linked properly; perhaps Tensorflow expects a static library? + NIX_LDFLAGS = [ "-lgpr" ] ++ lib.optionals cudaSupport [ "-lcudart" "-lcublas" "-lcufft" "-lcurand" "-lcusolver" "-lcusparse" "-lcudnn" ]; - bazelFlags = [ "--config=opt" ] - ++ lib.optional sse42Support "--copt=-msse4.2" - ++ lib.optional avx2Support "--copt=-mavx2" - ++ lib.optional fmaSupport "--copt=-mfma" - ++ lib.optional cudaSupport "--config=cuda"; + hardeningDisable = [ "format" ]; - bazelTarget = "//tensorflow/tools/pip_package:build_pip_package"; + bazelFlags = [ + # temporary fixes to make the build work with bazel 0.27 + "--incompatible_no_support_tools_in_action_inputs=false" + ]; + bazelBuildFlags = [ + "--config=opt" # optimize using the flags set in the configure phase + ]; + + bazelTarget = "//tensorflow/tools/pip_package:build_pip_package //tensorflow/tools/lib_package:libtensorflow"; fetchAttrs = { preInstall = '' rm -rf $bazelOut/external/{bazel_tools,\@bazel_tools.marker,local_*,\@local_*} ''; - sha256 = "1nc98aqrp14q7llypcwaa0kdn9xi7r0p1mnd3vmmn1m299py33ca"; + # cudaSupport causes fetch of ncclArchive, resulting in different hashes + sha256 = if cudaSupport then + "1rbzvir569ch33bpvm47byx954vr80rjnzrbgs4rg3bqww73hr3k" + else + "1wnhma450zh72raiq7ddd9lyq3a056cjqiy6i7y4sidiy09ncvcg"; }; buildAttrs = { + outputs = [ "out" "python" ]; + preBuild = '' patchShebangs . - find -type f -name CROSSTOOL\* -exec sed -i \ - -e 's,/usr/bin/ar,${binutils.bintools}/bin/ar,g' \ - {} \; ''; installPhase = '' - sed -i 's,.*bdist_wheel.*,cp -rL . "$out"; exit 0,' bazel-bin/tensorflow/tools/pip_package/build_pip_package - bazel-bin/tensorflow/tools/pip_package/build_pip_package $PWD/dist + mkdir -p "$out" + tar -xf bazel-bin/tensorflow/tools/lib_package/libtensorflow.tar.gz -C "$out" + # Write pkgconfig file. + mkdir "$out/lib/pkgconfig" + cat > "$out/lib/pkgconfig/tensorflow.pc" << EOF + Name: TensorFlow + Version: ${version} + Description: Library for computation using data flow graphs for scalable machine learning + Requires: + Libs: -L$out/lib -ltensorflow + Cflags: -I$out/include/tensorflow + EOF + + # build the source code, then copy it to $python (build_pip_package + # actually builds a symlink farm so we must dereference them). + bazel-bin/tensorflow/tools/pip_package/build_pip_package --src "$PWD/dist" + cp -Lr "$PWD/dist" "$python" ''; }; - - dontFixup = true; }; in buildPythonPackage rec { - pname = "tensorflow"; - inherit version; + inherit version pname; - src = pkg; + src = bazel-build.python; - installFlags = lib.optional (!withTensorboard) "--no-dependencies"; + # Upstream has a pip hack that results in bin/tensorboard being in both tensorflow + # and the propagated input tensorflow-tensorboard, which causes environment collisions. + # Another possibility would be to have tensorboard only in the buildInputs + # https://github.com/tensorflow/tensorflow/blob/v1.7.1/tensorflow/tools/pip_package/setup.py#L79 + postInstall = '' + rm $out/bin/tensorboard + ''; - postPatch = lib.optionalString (pythonAtLeast "3.4") '' - sed -i '/enum34/d' setup.py + # TODO: remove after there's support for setupPyDistFlags + buildPhase = '' + runHook preBuild + cp ${setuppy} nix_run_setup + ${python.interpreter} nix_run_setup --project_name ${pname} bdist_wheel + runHook postBuild ''; - propagatedBuildInputs = [ numpy six protobuf absl-py ] - ++ lib.optional (!isPy3k) mock - ++ lib.optionals (pythonOlder "3.4") [ backports_weakref enum34 ] - ++ lib.optional withTensorboard tensorflow-tensorboard; + # tensorflow/tools/pip_package/setup.py + propagatedBuildInputs = [ + absl-py + astor + gast + google-pasta + keras-applications + keras-preprocessing + numpy + six + protobuf + tensorflow-estimator + termcolor + wrapt + grpcio + ] ++ lib.optionals (!isPy3k) [ + mock + future # FIXME + ] ++ lib.optionals (pythonOlder "3.4") [ + backports_weakref enum34 + ] ++ lib.optionals withTensorboard [ + tensorflow-tensorboard + ]; # Actual tests are slow and impure. + # TODO try to run them anyway + # TODO better test (files in tensorflow/tools/ci_build/builds/*test) checkPhase = '' ${python.interpreter} -c "import tensorflow" ''; + passthru.libtensorflow = bazel-build.out; + meta = with stdenv.lib; { description = "Computation using data flow graphs for scalable machine learning"; homepage = http://tensorflow.org; diff --git a/pkgs/development/python-modules/tensorflow/no-saved-proto.patch b/pkgs/development/python-modules/tensorflow/no-saved-proto.patch new file mode 100644 index 00000000000..ead112f8ddd --- /dev/null +++ b/pkgs/development/python-modules/tensorflow/no-saved-proto.patch @@ -0,0 +1,14 @@ +diff --git a/tensorflow/cc/saved_model/BUILD b/tensorflow/cc/saved_model/BUILD +index 8626ed0087..27deb34387 100644 +--- a/tensorflow/cc/saved_model/BUILD ++++ b/tensorflow/cc/saved_model/BUILD +@@ -49,9 +49,6 @@ cc_library( + # tf_lib depending on the build platform. + "//tensorflow/core:lib", + "//tensorflow/core:protos_all_cc", +- ]) + if_mobile([ +- # Mobile-friendly SavedModel proto. See go/portable-proto for more info. +- "//tensorflow/core:saved_model_portable_proto", + ]) + if_android([ + "//tensorflow/core:android_tensorflow_lib", + ]) + if_ios([ diff --git a/pkgs/development/python-modules/tensorflow/prefetcher.sh b/pkgs/development/python-modules/tensorflow/prefetcher.sh index 3059340d127..d590fb0f173 100644..100755 --- a/pkgs/development/python-modules/tensorflow/prefetcher.sh +++ b/pkgs/development/python-modules/tensorflow/prefetcher.sh @@ -1,7 +1,10 @@ +#!/usr/bin/env bash + version=1.14.0 -hashfile=tf${version}-hashes.nix +hashfile=binary-hashes.nix rm -f $hashfile echo "{" >> $hashfile +echo "version = \"$version\";" >> $hashfile for sys in "linux" "mac"; do for tfpref in "cpu/tensorflow" "gpu/tensorflow_gpu"; do for pykind in "py2-none-any" "py3-none-any" "cp27-none-linux_x86_64" "cp35-cp35m-linux_x86_64" "cp36-cp36m-linux_x86_64" "cp37-cp37m-linux_x86_64"; do diff --git a/pkgs/development/python-modules/tensorflow/system-jsoncpp.patch b/pkgs/development/python-modules/tensorflow/system-jsoncpp.patch new file mode 100644 index 00000000000..ecb2d04d7ee --- /dev/null +++ b/pkgs/development/python-modules/tensorflow/system-jsoncpp.patch @@ -0,0 +1,21 @@ +diff --git a/third_party/systemlibs/jsoncpp.BUILD b/third_party/systemlibs/jsoncpp.BUILD +index 526fd0c418..646f3fdcea 100644 +--- a/third_party/systemlibs/jsoncpp.BUILD ++++ b/third_party/systemlibs/jsoncpp.BUILD +@@ -7,6 +7,7 @@ filegroup( + + HEADERS = [ + "include/json/autolink.h", ++ "include/json/allocator.h", + "include/json/config.h", + "include/json/features.h", + "include/json/forwards.h", +@@ -23,7 +24,7 @@ genrule( + cmd = """ + for i in $(OUTS); do + i=$${i##*/} +- ln -sf $(INCLUDEDIR)/jsoncpp/json/$$i $(@D)/include/json/$$i ++ ln -sf $(INCLUDEDIR)/json/$$i $(@D)/include/json/$$i + done + """, + ) |