2024-06-05 15:53:02 +00:00
|
|
|
{
|
|
|
|
stdenv,
|
|
|
|
lib,
|
|
|
|
fetchurl,
|
|
|
|
buildPythonPackage,
|
|
|
|
isPy3k,
|
|
|
|
pythonOlder,
|
|
|
|
pythonAtLeast,
|
|
|
|
astor,
|
|
|
|
gast,
|
|
|
|
google-pasta,
|
|
|
|
wrapt,
|
|
|
|
numpy,
|
|
|
|
six,
|
|
|
|
termcolor,
|
|
|
|
packaging,
|
|
|
|
protobuf,
|
|
|
|
absl-py,
|
|
|
|
grpcio,
|
|
|
|
mock,
|
|
|
|
scipy,
|
|
|
|
wheel,
|
|
|
|
jax,
|
|
|
|
opt-einsum,
|
|
|
|
tensorflow-estimator-bin,
|
|
|
|
tensorboard,
|
|
|
|
config,
|
|
|
|
cudaSupport ? config.cudaSupport,
|
|
|
|
cudaPackages,
|
|
|
|
zlib,
|
|
|
|
python,
|
|
|
|
keras-applications,
|
|
|
|
keras-preprocessing,
|
|
|
|
addOpenGLRunpath,
|
|
|
|
astunparse,
|
|
|
|
flatbuffers,
|
|
|
|
h5py,
|
|
|
|
llvmPackages,
|
|
|
|
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
|
2024-06-05 15:53:02 +00:00
|
|
|
assert !(stdenv.isDarwin && cudaSupport);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
packages = import ./binary-hashes.nix;
|
2024-05-15 15:35:15 +00:00
|
|
|
inherit (cudaPackages) cudatoolkit cudnn;
|
2024-06-05 15:53:02 +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";
|
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
src =
|
|
|
|
let
|
|
|
|
pyVerNoDot = lib.strings.stringAsChars (x: lib.optionalString (x != ".") x) python.pythonVersion;
|
|
|
|
platform = stdenv.system;
|
|
|
|
cuda = lib.optionalString cudaSupport "_gpu";
|
|
|
|
key = "${platform}_${pyVerNoDot}${cuda}";
|
|
|
|
in
|
|
|
|
fetchurl (packages.${key} or (throw "tensoflow-bin: unsupported system: ${stdenv.system}"));
|
|
|
|
|
|
|
|
buildInputs = [ llvmPackages.openmp ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
dependencies = [
|
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
|
2023-04-12 12:48:02 +00:00
|
|
|
jax
|
2020-04-24 23:36:52 +00:00
|
|
|
termcolor
|
|
|
|
grpcio
|
|
|
|
six
|
|
|
|
astor
|
|
|
|
absl-py
|
|
|
|
gast
|
|
|
|
opt-einsum
|
|
|
|
google-pasta
|
|
|
|
wrapt
|
2023-02-02 18:25:31 +00:00
|
|
|
tensorflow-estimator-bin
|
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
|
2023-08-04 22:07:22 +00:00
|
|
|
] ++ lib.optional (!isPy3k) mock;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-06-05 15:53:02 +00:00
|
|
|
build-system = [ wheel ] ++ lib.optionals cudaSupport [ addOpenGLRunpath ];
|
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
|
|
|
|
|
|
|
|
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.
|
2023-04-12 12:48:02 +00:00
|
|
|
# - numpy was pinned to fix some internal tests: https://github.com/tensorflow/tensorflow/issues/60216
|
2021-12-06 16:07:01 +00:00
|
|
|
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" \
|
2023-04-12 12:48:02 +00:00
|
|
|
-e "/Requires-Dist: tensorflow-io-gcs-filesystem/d" \
|
|
|
|
-e "s/Requires-Dist: numpy (.*)/Requires-Dist: numpy/"
|
2021-05-28 09:39:13 +00:00
|
|
|
)
|
2020-04-24 23:36:52 +00:00
|
|
|
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
|
|
|
|
];
|
|
|
|
|
|
|
|
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"
|
2024-01-02 11:29:13 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/compiler/mlir/stablehlo/"
|
2021-05-28 09:39:13 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/compiler/tf2tensorrt/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/compiler/tf2xla/ops/"
|
2024-01-02 11:29:13 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/include/external/ml_dtypes/"
|
2021-05-28 09:39:13 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/lite/experimental/microfrontend/python/ops/"
|
2023-10-09 19:29:22 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/lite/python/analyzer_wrapper/"
|
2021-05-28 09:39:13 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/lite/python/interpreter_wrapper/"
|
2023-10-09 19:29:22 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/lite/python/metrics/"
|
2021-05-28 09:39:13 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/lite/python/optimize/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/autograph/impl/testing"
|
2023-10-09 19:29:22 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/python/client"
|
2021-05-28 09:39:13 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/python/data/experimental/service"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/framework"
|
2023-10-09 19:29:22 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/python/grappler"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/lib/core"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/lib/io"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/platform"
|
2021-05-28 09:39:13 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/python/profiler/internal"
|
2023-10-09 19:29:22 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow/python/saved_model"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/python/util"
|
|
|
|
"$out/${python.sitePackages}/tensorflow/tsl/python/lib/core"
|
2024-06-05 15:53:02 +00:00
|
|
|
"$out/${python.sitePackages}/tensorflow.libs/"
|
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"
|
|
|
|
];
|
|
|
|
|
2024-07-27 06:49:29 +00:00
|
|
|
meta = {
|
2020-04-24 23:36:52 +00:00
|
|
|
description = "Computation using data flow graphs for scalable machine learning";
|
|
|
|
homepage = "http://tensorflow.org";
|
2024-07-27 06:49:29 +00:00
|
|
|
sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
|
|
|
|
license = lib.licenses.asl20;
|
|
|
|
maintainers = with lib.maintainers; [
|
2024-06-05 15:53:02 +00:00
|
|
|
jyp
|
|
|
|
abbradar
|
|
|
|
];
|
2024-07-27 06:49:29 +00:00
|
|
|
badPlatforms = [ "x86_64-darwin" ];
|
2024-06-05 15:53:02 +00:00
|
|
|
# Cannot import tensortfow on python 3.12 as it still dependends on distutils:
|
|
|
|
# ModuleNotFoundError: No module named 'distutils'
|
|
|
|
# https://github.com/tensorflow/tensorflow/issues/58073
|
|
|
|
broken = pythonAtLeast "3.12";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
}
|