2020-04-24 23:36:52 +00:00
|
|
|
{ stdenv
|
|
|
|
, lib
|
|
|
|
, fetchurl
|
|
|
|
, buildPythonPackage
|
2020-12-03 08:41:04 +00:00
|
|
|
, isPy3k, pythonOlder, pythonAtLeast, isPy38
|
2020-04-24 23:36:52 +00:00
|
|
|
, astor
|
|
|
|
, gast
|
|
|
|
, google-pasta
|
|
|
|
, wrapt
|
|
|
|
, numpy
|
|
|
|
, six
|
|
|
|
, termcolor
|
|
|
|
, protobuf
|
|
|
|
, absl-py
|
|
|
|
, grpcio
|
|
|
|
, mock
|
|
|
|
, scipy
|
|
|
|
, wheel
|
|
|
|
, opt-einsum
|
|
|
|
, backports_weakref
|
|
|
|
, tensorflow-estimator_2
|
2020-05-15 21:57:56 +00:00
|
|
|
, tensorflow-tensorboard_2
|
2020-04-24 23:36:52 +00:00
|
|
|
, cudaSupport ? false
|
|
|
|
, cudatoolkit ? null
|
|
|
|
, cudnn ? null
|
|
|
|
, nvidia_x11 ? null
|
|
|
|
, zlib
|
|
|
|
, python
|
|
|
|
, symlinkJoin
|
|
|
|
, keras-applications
|
|
|
|
, keras-preprocessing
|
|
|
|
, addOpenGLRunpath
|
|
|
|
}:
|
|
|
|
|
|
|
|
# We keep this binary build for two reasons:
|
|
|
|
# - the source build doesn't work on Darwin.
|
|
|
|
# - the source build is currently brittle and not easy to maintain
|
|
|
|
|
|
|
|
assert cudaSupport -> cudatoolkit != null
|
|
|
|
&& cudnn != null
|
|
|
|
&& nvidia_x11 != null;
|
|
|
|
|
|
|
|
# unsupported combination
|
|
|
|
assert ! (stdenv.isDarwin && cudaSupport);
|
|
|
|
|
|
|
|
let
|
|
|
|
packages = import ./binary-hashes.nix;
|
|
|
|
|
|
|
|
variant = if cudaSupport then "-gpu" else "";
|
|
|
|
pname = "tensorflow${variant}";
|
|
|
|
|
|
|
|
in buildPythonPackage {
|
|
|
|
inherit pname;
|
|
|
|
inherit (packages) version;
|
|
|
|
format = "wheel";
|
|
|
|
|
2020-12-03 08:41:04 +00:00
|
|
|
disabled = pythonAtLeast "3.8";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
src = let
|
|
|
|
pyVerNoDot = lib.strings.stringAsChars (x: if x == "." then "" else x) python.pythonVersion;
|
|
|
|
platform = if stdenv.isDarwin then "mac" else "linux";
|
|
|
|
unit = if cudaSupport then "gpu" else "cpu";
|
|
|
|
key = "${platform}_py_${pyVerNoDot}_${unit}";
|
|
|
|
in fetchurl packages.${key};
|
|
|
|
|
|
|
|
propagatedBuildInputs = [
|
|
|
|
protobuf
|
|
|
|
numpy
|
|
|
|
scipy
|
|
|
|
termcolor
|
|
|
|
grpcio
|
|
|
|
six
|
|
|
|
astor
|
|
|
|
absl-py
|
|
|
|
gast
|
|
|
|
opt-einsum
|
|
|
|
google-pasta
|
|
|
|
wrapt
|
|
|
|
tensorflow-estimator_2
|
2020-05-15 21:57:56 +00:00
|
|
|
tensorflow-tensorboard_2
|
2020-04-24 23:36:52 +00:00
|
|
|
keras-applications
|
|
|
|
keras-preprocessing
|
|
|
|
] ++ lib.optional (!isPy3k) mock
|
|
|
|
++ lib.optionals (pythonOlder "3.4") [ backports_weakref ];
|
|
|
|
|
|
|
|
nativeBuildInputs = [ wheel ] ++ lib.optional cudaSupport addOpenGLRunpath;
|
|
|
|
|
|
|
|
preConfigure = ''
|
|
|
|
unset SOURCE_DATE_EPOCH
|
|
|
|
|
|
|
|
# Make sure that dist and the wheel file are writable.
|
|
|
|
chmod u+rwx -R ./dist
|
|
|
|
|
|
|
|
pushd dist
|
|
|
|
|
|
|
|
# Unpack the wheel file.
|
|
|
|
wheel unpack --dest unpacked ./*.whl
|
|
|
|
|
2021-04-25 03:57:28 +00:00
|
|
|
# Tensorflow wheels tightly constrain the versions of gast, tensorflow-estimator and scipy.
|
|
|
|
# This code relaxes these requirements:
|
|
|
|
substituteInPlace ./unpacked/tensorflow*/tensorflow_core/tools/pip_package/setup.py \
|
|
|
|
--replace "tensorflow_estimator >= 2.1.0rc0, < 2.2.0" "tensorflow_estimator" \
|
|
|
|
--replace "tensorboard >= 2.1.0, < 2.2.0" "tensorboard" \
|
|
|
|
--replace "gast == 0.2.2" "gast" \
|
|
|
|
--replace "scipy == 1.2.2" "scipy"
|
|
|
|
|
|
|
|
substituteInPlace ./unpacked/tensorflow*/tensorflow*.dist-info/METADATA \
|
|
|
|
--replace "gast (==0.2.2)" "gast" \
|
|
|
|
--replace "tensorflow-estimator (<2.2.0,>=2.1.0rc0)" "tensorflow_estimator" \
|
|
|
|
--replace "tensorboard (<2.2.0,>=2.1.0)" "tensorboard" \
|
|
|
|
--replace "scipy (==1.4.1)" "scipy"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# Pack the wheel file back up.
|
|
|
|
wheel pack ./unpacked/tensorflow*
|
|
|
|
|
|
|
|
popd
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Note that we need to run *after* the fixup phase because the
|
|
|
|
# libraries are loaded at runtime. If we run in preFixup then
|
|
|
|
# patchelf --shrink-rpath will remove the cuda libraries.
|
|
|
|
postFixup =
|
|
|
|
let
|
|
|
|
# rpaths we only need to add if CUDA is enabled.
|
|
|
|
cudapaths = lib.optionals cudaSupport [
|
|
|
|
cudatoolkit.out
|
|
|
|
cudatoolkit.lib
|
|
|
|
cudnn
|
|
|
|
nvidia_x11
|
|
|
|
];
|
|
|
|
|
|
|
|
libpaths = [
|
|
|
|
stdenv.cc.cc.lib
|
|
|
|
zlib
|
|
|
|
];
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
rpath = lib.makeLibraryPath (libpaths ++ cudapaths);
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
|
|
|
lib.optionalString stdenv.isLinux ''
|
|
|
|
# This is an array containing all the directories in the tensorflow2
|
|
|
|
# package that contain .so files.
|
|
|
|
#
|
|
|
|
# TODO: Create this list programmatically, and remove paths that aren't
|
|
|
|
# actually needed.
|
|
|
|
rrPathArr=(
|
|
|
|
"$out/${python.sitePackages}/tensorflow_core/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow_core/compiler/tf2tensorrt/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow_core/compiler/tf2xla/ops/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow_core/lite/experimental/microfrontend/python/ops/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow_core/lite/python/interpreter_wrapper/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow_core/lite/python/optimize/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow_core/python/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow_core/python/framework/"
|
|
|
|
"${rpath}"
|
|
|
|
)
|
|
|
|
|
|
|
|
# The the bash array into a colon-separated list of RPATHs.
|
|
|
|
rrPath=$(IFS=$':'; echo "''${rrPathArr[*]}")
|
|
|
|
echo "about to run patchelf with the following rpath: $rrPath"
|
|
|
|
|
|
|
|
find $out -type f \( -name '*.so' -or -name '*.so.*' \) | while read lib; do
|
|
|
|
echo "about to patchelf $lib..."
|
|
|
|
chmod a+rx "$lib"
|
|
|
|
patchelf --set-rpath "$rrPath" "$lib"
|
|
|
|
${lib.optionalString cudaSupport ''
|
|
|
|
addOpenGLRunpath "$lib"
|
|
|
|
''}
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
# 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
|
|
|
|
# See https://github.com/NixOS/nixpkgs/pull/44381 for more information.
|
|
|
|
postInstall = ''
|
|
|
|
rm $out/bin/tensorboard
|
|
|
|
'';
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
pythonImportsCheck = [
|
|
|
|
"tensorflow"
|
|
|
|
"tensorflow.keras"
|
|
|
|
"tensorflow.python"
|
|
|
|
"tensorflow.python.framework"
|
|
|
|
];
|
|
|
|
|
2021-01-15 22:18:51 +00:00
|
|
|
meta = with lib; {
|
2020-04-24 23:36:52 +00:00
|
|
|
description = "Computation using data flow graphs for scalable machine learning";
|
|
|
|
homepage = "http://tensorflow.org";
|
|
|
|
license = licenses.asl20;
|
|
|
|
maintainers = with maintainers; [ jyp abbradar cdepillabout ];
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|