2019-07-13 20:42:50 +01:00
|
|
|
{ stdenv, pkgs, buildBazelPackage, lib, fetchFromGitHub, fetchpatch, symlinkJoin
|
|
|
|
# Python deps
|
|
|
|
, buildPythonPackage, isPy3k, pythonOlder, pythonAtLeast, python
|
|
|
|
# Python libraries
|
2019-06-20 19:17:13 +01:00
|
|
|
, numpy, tensorflow-tensorboard, backports_weakref, mock, enum34, absl-py
|
2019-07-13 20:42:50 +01:00
|
|
|
, 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
|
2019-06-20 19:17:13 +01:00
|
|
|
, cudaSupport ? false, nvidia_x11 ? null, cudatoolkit ? null, cudnn ? null, nccl ? null
|
2018-02-27 00:16:33 +00:00
|
|
|
# XLA without CUDA is broken
|
|
|
|
, xlaSupport ? cudaSupport
|
2017-10-15 13:23:56 +01:00
|
|
|
# Default from ./configure script
|
|
|
|
, cudaCapabilities ? [ "3.5" "5.2" ]
|
2019-04-21 18:06:50 +01:00
|
|
|
, sse42Support ? builtins.elem (stdenv.hostPlatform.platform.gcc.arch or "default") ["westmere" "sandybridge" "ivybridge" "haswell" "broadwell" "skylake" "skylake-avx512"]
|
|
|
|
, avx2Support ? builtins.elem (stdenv.hostPlatform.platform.gcc.arch or "default") [ "haswell" "broadwell" "skylake" "skylake-avx512"]
|
|
|
|
, fmaSupport ? builtins.elem (stdenv.hostPlatform.platform.gcc.arch or "default") [ "haswell" "broadwell" "skylake" "skylake-avx512"]
|
2017-02-15 11:30:30 +00:00
|
|
|
}:
|
|
|
|
|
2017-11-10 17:07:58 +00:00
|
|
|
assert cudaSupport -> nvidia_x11 != null
|
|
|
|
&& cudatoolkit != null
|
2017-10-15 13:23:56 +01:00
|
|
|
&& cudnn != null;
|
2017-02-26 10:03:27 +00:00
|
|
|
|
|
|
|
# unsupported combination
|
|
|
|
assert ! (stdenv.isDarwin && cudaSupport);
|
|
|
|
|
2017-10-15 13:23:56 +01:00
|
|
|
let
|
|
|
|
withTensorboard = pythonOlder "3.6";
|
2017-02-15 11:30:30 +00:00
|
|
|
|
2017-10-03 14:50:39 +01:00
|
|
|
cudatoolkit_joined = symlinkJoin {
|
2019-07-13 20:42:50 +01:00
|
|
|
name = "${cudatoolkit.name}-merged";
|
2017-10-15 13:23:56 +01:00
|
|
|
paths = [ cudatoolkit.out cudatoolkit.lib ];
|
|
|
|
};
|
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
cudatoolkit_cc_joined = symlinkJoin {
|
|
|
|
name = "${cudatoolkit.cc.name}-merged";
|
2019-06-20 19:17:13 +01:00
|
|
|
paths = [
|
|
|
|
cudatoolkit.cc
|
|
|
|
binutils.bintools # for ar, dwp, nm, objcopy, objdump, strip
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
# Needed for _some_ system libraries, grep INCLUDEDIR.
|
|
|
|
includes_joined = symlinkJoin {
|
|
|
|
name = "tensorflow-deps-merged";
|
|
|
|
paths = [
|
|
|
|
pkgs.protobuf
|
|
|
|
jsoncpp
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2017-10-15 13:23:56 +01:00
|
|
|
tfFeature = x: if x then "1" else "0";
|
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
version = "1.14.0";
|
|
|
|
variant = if cudaSupport then "-gpu" else "";
|
2019-06-20 19:17:13 +01:00
|
|
|
pname = "tensorflow${variant}";
|
2017-11-10 17:07:58 +00:00
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
# TODO: remove after there's support for setupPyDistFlags
|
|
|
|
setuppy = ../../../development/interpreters/python/run_setup.py;
|
|
|
|
|
|
|
|
bazel-build = buildBazelPackage rec {
|
|
|
|
name = "${pname}-${version}";
|
2017-10-15 13:23:56 +01:00
|
|
|
|
|
|
|
src = fetchFromGitHub {
|
|
|
|
owner = "tensorflow";
|
|
|
|
repo = "tensorflow";
|
2019-07-13 20:42:50 +01:00
|
|
|
rev = "v${version}";
|
|
|
|
sha256 = "06jvwlsm14b8rqwd8q8796r0vmn0wk64s4ps2zg0sapkmp9vvcmi";
|
2017-10-15 13:23:56 +01:00
|
|
|
};
|
|
|
|
|
2017-11-10 17:07:58 +00:00
|
|
|
patches = [
|
2019-06-20 19:17:13 +01:00
|
|
|
# Work around https://github.com/tensorflow/tensorflow/issues/24752
|
|
|
|
./no-saved-proto.patch
|
2019-07-13 20:42:50 +01:00
|
|
|
# Fixes for NixOS jsoncpp
|
|
|
|
./system-jsoncpp.patch
|
2019-06-20 19:17:13 +01:00
|
|
|
|
|
|
|
# https://github.com/tensorflow/tensorflow/pull/29673
|
2017-11-10 17:07:58 +00:00
|
|
|
(fetchpatch {
|
2019-06-20 19:17:13 +01:00
|
|
|
name = "fix-compile-with-cuda-and-mpi.patch";
|
|
|
|
url = "https://github.com/tensorflow/tensorflow/pull/29673/commits/498e35a3bfe38dd75cf1416a1a23c07c3b59e6af.patch";
|
|
|
|
sha256 = "1m2qmwv1ysqa61z6255xggwbq6mnxbig749bdvrhnch4zydxb4di";
|
2017-11-10 17:07:58 +00:00
|
|
|
})
|
2019-06-20 19:17:13 +01:00
|
|
|
|
|
|
|
# https://github.com/tensorflow/tensorflow/issues/29220
|
2017-11-10 17:07:58 +00:00
|
|
|
(fetchpatch {
|
2019-06-20 19:17:13 +01:00
|
|
|
name = "bazel-0.27.patch";
|
|
|
|
url = "https://github.com/tensorflow/tensorflow/commit/cfccbdb8c4a92dd26382419dceb4d934c2380391.patch";
|
|
|
|
sha256 = "1l56wjia2c4685flsfkkgy471wx3c66wyv8khspv06zchj0k0liw";
|
2017-11-10 17:07:58 +00:00
|
|
|
})
|
|
|
|
];
|
2017-10-15 13:23:56 +01:00
|
|
|
|
2019-06-20 19:17:13 +01:00
|
|
|
# On update, it can be useful to steal the changes from gentoo
|
|
|
|
# https://gitweb.gentoo.org/repo/gentoo.git/tree/sci-libs/tensorflow
|
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
nativeBuildInputs = [
|
|
|
|
swig which cython
|
|
|
|
];
|
2017-10-15 13:23:56 +01:00
|
|
|
|
2019-06-20 19:17:13 +01:00
|
|
|
buildInputs = [
|
|
|
|
python
|
|
|
|
jemalloc
|
|
|
|
openmpi
|
|
|
|
glibcLocales
|
|
|
|
git
|
|
|
|
|
|
|
|
# python deps needed during wheel build time
|
|
|
|
numpy
|
|
|
|
keras-preprocessing
|
2019-07-13 20:42:50 +01:00
|
|
|
protobuf
|
|
|
|
wrapt
|
|
|
|
gast
|
|
|
|
astor
|
|
|
|
absl-py
|
|
|
|
termcolor
|
|
|
|
keras-applications
|
2019-06-20 19:17:13 +01:00
|
|
|
|
|
|
|
# libs taken from system through the TF_SYS_LIBS mechanism
|
2019-07-13 20:42:50 +01:00
|
|
|
grpc
|
|
|
|
sqlite
|
|
|
|
openssl
|
|
|
|
jsoncpp
|
|
|
|
pkgs.protobuf
|
|
|
|
curl
|
|
|
|
snappy
|
|
|
|
flatbuffers
|
|
|
|
icu
|
|
|
|
double-conversion
|
|
|
|
libpng
|
|
|
|
libjpeg
|
|
|
|
giflib
|
|
|
|
re2
|
|
|
|
pkgs.lmdb
|
2019-06-20 19:17:13 +01:00
|
|
|
|
|
|
|
# for building the wheel
|
|
|
|
setuptools
|
|
|
|
wheel
|
|
|
|
] ++ lib.optionals (!isPy3k) [
|
|
|
|
future
|
|
|
|
mock
|
|
|
|
] ++ lib.optionals cudaSupport [
|
|
|
|
cudatoolkit
|
|
|
|
cudnn
|
|
|
|
nvidia_x11
|
|
|
|
];
|
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
|
|
|
|
# arbitrarily set to the current latest bazel version, overly careful
|
|
|
|
TF_IGNORE_MAX_BAZEL_VERSION = true;
|
|
|
|
|
2019-06-20 19:17:13 +01:00
|
|
|
# Take as many libraries from the system as possible. Keep in sync with
|
|
|
|
# list of valid syslibs in
|
2019-07-13 20:42:50 +01:00
|
|
|
# https://github.com/tensorflow/tensorflow/blob/master/third_party/systemlibs/syslibs_configure.bzl
|
|
|
|
TF_SYSTEM_LIBS = lib.concatStringsSep "," [
|
2019-06-20 19:17:13 +01:00
|
|
|
"absl_py"
|
|
|
|
"astor_archive"
|
|
|
|
"boringssl"
|
2019-07-13 20:42:50 +01:00
|
|
|
# Not packaged in nixpkgs
|
|
|
|
# "com_github_googleapis_googleapis"
|
|
|
|
# "com_github_googlecloudplatform_google_cloud_cpp"
|
2019-06-20 19:17:13 +01:00
|
|
|
"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
|
2019-07-13 20:42:50 +01:00
|
|
|
"org_sqlite"
|
2019-06-20 19:17:13 +01:00
|
|
|
"pasta"
|
|
|
|
"pcre"
|
|
|
|
"png_archive"
|
|
|
|
"protobuf_archive"
|
|
|
|
"six_archive"
|
|
|
|
"snappy"
|
|
|
|
"swig"
|
|
|
|
"termcolor_archive"
|
|
|
|
"wrapt"
|
|
|
|
"zlib_archive"
|
|
|
|
];
|
2017-10-15 13:23:56 +01:00
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
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
|
|
|
|
'';
|
|
|
|
|
2019-07-18 12:49:30 +01:00
|
|
|
preConfigure = let
|
|
|
|
opt_flags = []
|
|
|
|
++ lib.optionals sse42Support ["-msse4.2"]
|
|
|
|
++ lib.optionals avx2Support ["-mavx2"]
|
|
|
|
++ lib.optionals fmaSupport ["-mfma"];
|
|
|
|
in ''
|
2017-10-15 13:23:56 +01:00
|
|
|
patchShebangs configure
|
|
|
|
|
2019-06-20 19:17:13 +01:00
|
|
|
# dummy ldconfig
|
|
|
|
mkdir dummy-ldconfig
|
|
|
|
echo "#!${stdenv.shell}" > dummy-ldconfig/ldconfig
|
|
|
|
chmod +x dummy-ldconfig/ldconfig
|
|
|
|
export PATH="$PWD/dummy-ldconfig:$PATH"
|
|
|
|
|
2017-11-10 17:07:58 +00:00
|
|
|
export PYTHON_LIB_PATH="$NIX_BUILD_TOP/site-packages"
|
2019-07-18 12:49:30 +01:00
|
|
|
export CC_OPT_FLAGS="${lib.concatStringsSep " " opt_flags}"
|
2017-11-10 17:07:58 +00:00
|
|
|
mkdir -p "$PYTHON_LIB_PATH"
|
2017-10-15 13:23:56 +01:00
|
|
|
'';
|
|
|
|
|
2019-06-20 19:17:13 +01:00
|
|
|
configurePhase = ''
|
|
|
|
runHook preConfigure
|
|
|
|
./configure
|
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
# 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" ];
|
2017-11-10 17:07:58 +00:00
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
hardeningDisable = [ "format" ];
|
2017-10-15 13:23:56 +01:00
|
|
|
|
2019-06-20 19:17:13 +01:00
|
|
|
bazelFlags = [
|
|
|
|
# temporary fixes to make the build work with bazel 0.27
|
|
|
|
"--incompatible_no_support_tools_in_action_inputs=false"
|
2019-07-18 12:49:30 +01:00
|
|
|
];
|
|
|
|
bazelBuildFlags = [
|
|
|
|
"--config=opt" # optimize using the flags set in the configure phase
|
|
|
|
];
|
2017-10-15 13:23:56 +01:00
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
bazelTarget = "//tensorflow/tools/pip_package:build_pip_package //tensorflow/tools/lib_package:libtensorflow";
|
2017-10-15 13:23:56 +01:00
|
|
|
|
2017-11-10 17:07:58 +00:00
|
|
|
fetchAttrs = {
|
|
|
|
preInstall = ''
|
|
|
|
rm -rf $bazelOut/external/{bazel_tools,\@bazel_tools.marker,local_*,\@local_*}
|
|
|
|
'';
|
2017-10-15 13:23:56 +01:00
|
|
|
|
2019-06-20 19:17:13 +01:00
|
|
|
# cudaSupport causes fetch of ncclArchive, resulting in different hashes
|
|
|
|
sha256 = if cudaSupport then
|
2019-08-19 09:21:37 +01:00
|
|
|
"19ll3f1i5qzd7ngz3m2jbxzgcrdjx5sv6kv2j5mcb8g3xsws8j5x"
|
2019-06-20 19:17:13 +01:00
|
|
|
else
|
2019-08-19 09:21:37 +01:00
|
|
|
"0y9kw3k4yvrxwdy7zry7nip9mdiwyv35r6mx65g4w7qajiypfc7i";
|
2017-11-10 17:07:58 +00:00
|
|
|
};
|
2017-10-15 13:23:56 +01:00
|
|
|
|
2017-11-10 17:07:58 +00:00
|
|
|
buildAttrs = {
|
2019-07-13 20:42:50 +01:00
|
|
|
outputs = [ "out" "python" ];
|
|
|
|
|
2017-11-10 17:07:58 +00:00
|
|
|
preBuild = ''
|
|
|
|
patchShebangs .
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = ''
|
2019-07-13 20:42:50 +01:00
|
|
|
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"
|
2017-11-10 17:07:58 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2017-10-15 13:23:56 +01:00
|
|
|
|
2017-11-10 17:07:58 +00:00
|
|
|
in buildPythonPackage rec {
|
2019-06-20 19:17:13 +01:00
|
|
|
inherit version pname;
|
2017-10-15 13:23:56 +01:00
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
src = bazel-build.python;
|
2017-02-15 11:30:30 +00:00
|
|
|
|
2019-06-20 19:17:13 +01: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
|
|
|
|
# https://github.com/tensorflow/tensorflow/blob/v1.7.1/tensorflow/tools/pip_package/setup.py#L79
|
|
|
|
postInstall = ''
|
|
|
|
rm $out/bin/tensorboard
|
2017-10-15 13:23:56 +01:00
|
|
|
'';
|
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
# 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
|
|
|
|
'';
|
|
|
|
|
2019-06-20 19:17:13 +01:00
|
|
|
# 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
|
|
|
|
];
|
2017-11-10 17:07:58 +00:00
|
|
|
|
|
|
|
# Actual tests are slow and impure.
|
2019-06-20 19:17:13 +01:00
|
|
|
# TODO try to run them anyway
|
|
|
|
# TODO better test (files in tensorflow/tools/ci_build/builds/*test)
|
2017-11-10 17:07:58 +00:00
|
|
|
checkPhase = ''
|
2017-10-15 13:23:56 +01:00
|
|
|
${python.interpreter} -c "import tensorflow"
|
|
|
|
'';
|
2017-11-10 17:07:58 +00:00
|
|
|
|
2019-07-13 20:42:50 +01:00
|
|
|
passthru.libtensorflow = bazel-build.out;
|
|
|
|
|
2017-11-10 17:07:58 +00:00
|
|
|
meta = with stdenv.lib; {
|
|
|
|
description = "Computation using data flow graphs for scalable machine learning";
|
2018-02-27 00:16:33 +00:00
|
|
|
homepage = http://tensorflow.org;
|
2017-11-10 17:07:58 +00:00
|
|
|
license = licenses.asl20;
|
|
|
|
maintainers = with maintainers; [ jyp abbradar ];
|
2018-03-15 00:08:06 +00:00
|
|
|
platforms = platforms.linux;
|
2018-02-27 00:16:33 +00:00
|
|
|
broken = !(xlaSupport -> cudaSupport);
|
2017-11-10 17:07:58 +00:00
|
|
|
};
|
|
|
|
}
|