2020-04-24 23:36:52 +00:00
|
|
|
{ stdenv
|
|
|
|
, lib
|
|
|
|
, fetchurl
|
|
|
|
, buildPythonPackage
|
2021-12-06 16:07:01 +00:00
|
|
|
, isPy3k, pythonOlder, pythonAtLeast, astor
|
2020-04-24 23:36:52 +00:00
|
|
|
, gast
|
|
|
|
, google-pasta
|
|
|
|
, wrapt
|
|
|
|
, numpy
|
|
|
|
, six
|
|
|
|
, termcolor
|
2022-06-26 10:26:21 +00:00
|
|
|
, packaging
|
2020-04-24 23:36:52 +00:00
|
|
|
, protobuf
|
|
|
|
, absl-py
|
|
|
|
, grpcio
|
|
|
|
, mock
|
|
|
|
, scipy
|
|
|
|
, wheel
|
|
|
|
, opt-einsum
|
|
|
|
, backports_weakref
|
2021-12-06 16:07:01 +00:00
|
|
|
, tensorflow-estimator
|
2022-04-15 01:41:22 +00:00
|
|
|
, tensorboard
|
2020-04-24 23:36:52 +00:00
|
|
|
, cudaSupport ? false
|
2022-04-15 01:41:22 +00:00
|
|
|
, cudaPackages ? {}
|
2021-12-06 16:07:01 +00:00
|
|
|
, patchelfUnstable
|
2020-04-24 23:36:52 +00:00
|
|
|
, zlib
|
|
|
|
, python
|
|
|
|
, keras-applications
|
|
|
|
, keras-preprocessing
|
|
|
|
, addOpenGLRunpath
|
2021-05-28 09:39:13 +00:00
|
|
|
, astunparse
|
|
|
|
, flatbuffers
|
|
|
|
, h5py
|
|
|
|
, typing-extensions
|
2020-04-24 23:36:52 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# unsupported combination
|
|
|
|
assert ! (stdenv.isDarwin && cudaSupport);
|
|
|
|
|
|
|
|
let
|
|
|
|
packages = import ./binary-hashes.nix;
|
2022-04-15 01:41:22 +00:00
|
|
|
inherit (cudaPackages) cudatoolkit cudnn;
|
2020-04-24 23:36:52 +00:00
|
|
|
in buildPythonPackage {
|
2021-12-06 16:07:01 +00:00
|
|
|
pname = "tensorflow" + lib.optionalString cudaSupport "-gpu";
|
2020-04-24 23:36:52 +00:00
|
|
|
inherit (packages) version;
|
|
|
|
format = "wheel";
|
|
|
|
|
2023-01-20 10:41:00 +00:00
|
|
|
# Python 3.11 still unsupported
|
|
|
|
disabled = pythonAtLeast "3.11";
|
|
|
|
|
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 = [
|
2021-05-28 09:39:13 +00:00
|
|
|
astunparse
|
|
|
|
flatbuffers
|
|
|
|
typing-extensions
|
2022-06-26 10:26:21 +00:00
|
|
|
packaging
|
2020-04-24 23:36:52 +00:00
|
|
|
protobuf
|
|
|
|
numpy
|
|
|
|
scipy
|
|
|
|
termcolor
|
|
|
|
grpcio
|
|
|
|
six
|
|
|
|
astor
|
|
|
|
absl-py
|
|
|
|
gast
|
|
|
|
opt-einsum
|
|
|
|
google-pasta
|
|
|
|
wrapt
|
2021-12-06 16:07:01 +00:00
|
|
|
tensorflow-estimator
|
2022-04-15 01:41:22 +00:00
|
|
|
tensorboard
|
2020-04-24 23:36:52 +00:00
|
|
|
keras-applications
|
|
|
|
keras-preprocessing
|
2021-05-28 09:39:13 +00:00
|
|
|
h5py
|
2020-04-24 23:36:52 +00:00
|
|
|
] ++ lib.optional (!isPy3k) mock
|
|
|
|
++ lib.optionals (pythonOlder "3.4") [ backports_weakref ];
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
# remove patchelfUnstable once patchelf 0.14 with https://github.com/NixOS/patchelf/pull/256 becomes the default
|
2022-10-30 15:09:59 +00:00
|
|
|
nativeBuildInputs = [ wheel ] ++ lib.optionals cudaSupport [ addOpenGLRunpath patchelfUnstable ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
preConfigure = ''
|
|
|
|
unset SOURCE_DATE_EPOCH
|
|
|
|
|
|
|
|
# Make sure that dist and the wheel file are writable.
|
|
|
|
chmod u+rwx -R ./dist
|
|
|
|
|
|
|
|
pushd dist
|
|
|
|
|
2022-06-26 10:26:21 +00:00
|
|
|
orig_name="$(echo ./*.whl)"
|
2020-04-24 23:36:52 +00:00
|
|
|
wheel unpack --dest unpacked ./*.whl
|
2021-12-06 16:07:01 +00:00
|
|
|
rm ./*.whl
|
2021-05-28 09:39:13 +00:00
|
|
|
(
|
|
|
|
cd unpacked/tensorflow*
|
2021-12-06 16:07:01 +00:00
|
|
|
# Adjust dependency requirements:
|
2022-11-02 22:02:43 +00:00
|
|
|
# - Relax flatbuffers, gast, protobuf, tensorboard, and tensorflow-estimator version requirements that don't match what we have packaged
|
2021-12-06 16:07:01 +00:00
|
|
|
# - The purpose of python3Packages.libclang is not clear at the moment and we don't have it packaged yet
|
|
|
|
# - keras and tensorlow-io-gcs-filesystem will be considered as optional for now.
|
|
|
|
sed -i *.dist-info/METADATA \
|
2022-06-26 10:26:21 +00:00
|
|
|
-e "/Requires-Dist: flatbuffers/d" \
|
|
|
|
-e "/Requires-Dist: gast/d" \
|
2021-12-06 16:07:01 +00:00
|
|
|
-e "/Requires-Dist: keras/d" \
|
2022-11-02 22:02:43 +00:00
|
|
|
-e "/Requires-Dist: libclang/d" \
|
|
|
|
-e "/Requires-Dist: protobuf/d" \
|
|
|
|
-e "/Requires-Dist: tensorboard/d" \
|
2022-06-26 10:26:21 +00:00
|
|
|
-e "/Requires-Dist: tensorflow-estimator/d" \
|
2021-12-06 16:07:01 +00:00
|
|
|
-e "/Requires-Dist: tensorflow-io-gcs-filesystem/d"
|
2021-05-28 09:39:13 +00:00
|
|
|
)
|
2020-04-24 23:36:52 +00:00
|
|
|
wheel pack ./unpacked/tensorflow*
|
2022-06-26 10:26:21 +00:00
|
|
|
mv *.whl $orig_name # avoid changes to the _os_arch.whl suffix
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
];
|
|
|
|
|
|
|
|
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=(
|
2021-05-28 09:39:13 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/core/kernels"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/compiler/tf2tensorrt/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/compiler/tf2xla/ops/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/lite/experimental/microfrontend/python/ops/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/lite/python/interpreter_wrapper/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/lite/python/optimize/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/framework/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/autograph/impl/testing"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/data/experimental/service"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/framework"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/profiler/internal"
|
2020-04-24 23:36:52 +00:00
|
|
|
"${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
|
2022-04-15 01:41:22 +00:00
|
|
|
# and the propagated input tensorboard, which causes environment collisions.
|
2020-05-15 21:57:56 +00:00
|
|
|
# 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.python"
|
|
|
|
"tensorflow.python.framework"
|
|
|
|
];
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
passthru = {
|
|
|
|
inherit cudaPackages;
|
|
|
|
};
|
|
|
|
|
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";
|
2022-06-26 10:26:21 +00:00
|
|
|
sourceProvenance = with sourceTypes; [ binaryNativeCode ];
|
2020-04-24 23:36:52 +00:00
|
|
|
license = licenses.asl20;
|
|
|
|
maintainers = with maintainers; [ jyp abbradar cdepillabout ];
|
|
|
|
platforms = [ "x86_64-linux" "x86_64-darwin" ];
|
|
|
|
};
|
|
|
|
}
|