@@ -1,18 +1,101 @@
1−{ stdenv, fetchurl, buildPythonPackage, pythonOlder,
2− cudaSupport ? false, cudatoolkit ? null, cudnn ? null,
3− fetchFromGitHub, lib, numpy, pyyaml, cffi, typing, cmake, hypothesis, numactl,
1+{ stdenv, fetchurl, fetchgit, buildPythonPackage, python, pythonOlder,
2+ cudaSupport ? false, cudatoolkit ? null, cudnn ? null, nccl ? null, magma ? null,
3+ mklSupport ? false, mkl ? null,
4+ openMPISupport ? false, openmpi ? null,
5+ buildNamedTensor ? false,
6+ buildBinaries ? false,
7+ cudaArchList ? null,
8+ fetchFromGitHub, lib, numpy, pyyaml, cffi, click, typing, cmake, hypothesis, numactl,
49 linkFarm, symlinkJoin,
10+
11+ # ninja (https://ninja-build.org) must be available to run C++ extensions tests,
12+ ninja,
13+
14+ # dependencies for torch.utils.tensorboard
15+ tensorboardSupport ? true, pillow, six, future, tensorflow-tensorboard,
16+
517 utillinux, which, isPy3k }:
618
7−assert cudnn == null || cudatoolkit != null;
19+assert !openMPISupport || openmpi != null;
20+assert !tensorboardSupport || tensorflow-tensorboard != null;
21+
22+# assert that everything needed for cuda is present and that the correct cuda versions are used
823assert !cudaSupport || cudatoolkit != null;
24+assert cudnn == null || cudatoolkit != null;
25+assert !cudaSupport || (let majorIs = lib.versions.major cudatoolkit.version;
26+ in majorIs == "9" || majorIs == "10");
27+
28+let
29+ hasDependency = dep: pkg: lib.lists.any (inp: inp == dep) pkg.buildInputs;
30+ matchesCudatoolkit = hasDependency cudatoolkit;
31+ matchesMkl = hasDependency mkl;
32+in
33+# confirm that cudatoolkits are sync'd across dependencies
34+assert !(openMPISupport && cudaSupport) || matchesCudatoolkit openmpi;
35+assert !cudaSupport || matchesCudatoolkit magma;
36+
37+# confirm that mkl is sync'd across dependencies
38+assert !mklSupport || mkl != null;
39+assert !(mklSupport && cudaSupport) || matchesMkl magma;
40+assert !mklSupport || (numpy.blasImplementation == "mkl" && numpy.blas == mkl);
941
1042let
1143 cudatoolkit_joined = symlinkJoin {
1244 name = "${cudatoolkit.name}-unsplit";
13− paths = [ cudatoolkit.out cudatoolkit.lib ];
45+ # nccl is here purely for semantic grouping it could be moved to nativeBuildInputs
46+ paths = [ cudatoolkit.out cudatoolkit.lib nccl.dev nccl.out ];
1447 };
1548
49+ # Give an explicit list of supported architectures for the build, See:
50+ # - pytorch bug report: https://github.com/pytorch/pytorch/issues/23573
51+ # - pytorch-1.2.0 build on nixpks: https://github.com/NixOS/nixpkgs/pull/65041
52+ #
53+ # This list was selected by omitting the TORCH_CUDA_ARCH_LIST parameter,
54+ # observing the fallback option (which selected all architectures known
55+ # from cudatoolkit_10_0, pytorch-1.2, and python-3.6), and doing a binary
56+ # searching to find offending architectures.
57+ #
58+ # NOTE: Because of sandboxing, this derivation can't auto-detect the hardware's
59+ # cuda architecture, so there is also now a problem around new architectures
60+ # not being supported until explicitly added to this derivation.
61+ #
62+ # FIXME: CMake is throwing the following warning on python-1.2:
63+ #
64+ # ```
65+ # CMake Warning at cmake/public/utils.cmake:172 (message):
66+ # In the future we will require one to explicitly pass TORCH_CUDA_ARCH_LIST
67+ # to cmake instead of implicitly setting it as an env variable. This will
68+ # become a FATAL_ERROR in future version of pytorch.
69+ # ```
70+ # If this is causing problems for your build, this derivation may have to strip
71+ # away the standard `buildPythonPackage` and use the
72+ # [*Adjust Build Options*](https://github.com/pytorch/pytorch/tree/v1.2.0#adjust-build-options-optional)
73+ # instructions. This will also add more flexibility around configurations
74+ # (allowing FBGEMM to be built in pytorch-1.1), and may future proof this
75+ # derivation.
76+ brokenArchs = [ "3.0" ]; # this variable is only used as documentation.
77+ cuda9ArchList = [
78+ "3.5"
79+ "5.0"
80+ "5.2"
81+ "6.0"
82+ "6.1"
83+ "7.0"
84+ "7.0+PTX" # I am getting a "undefined architecture compute_75" on cuda 9
85+ # which leads me to believe this is the final cuda-9-compatible architecture.
86+ ];
87+ cuda10ArchList = cuda9ArchList ++ [
88+ "7.5"
89+ "7.5+PTX" # < most recent architecture as of cudatoolkit_10_0 and pytorch-1.2.0
90+ ];
91+ final_cudaArchList =
92+ if !cudaSupport || cudaArchList != null
93+ then cudaArchList
94+ else
95+ if lib.versions.major cudatoolkit.version == "9"
96+ then cuda9ArchList
97+ else cuda10ArchList; # the assert above removes any ambiguity here.
98+
1699 # Normally libcuda.so.1 is provided at runtime by nvidia-x11 via
17100 # LD_LIBRARY_PATH=/run/opengl-driver/lib. We only use the stub
18101 # libcuda.so from cudatoolkit for running tests, so that we don’t have
@@ -25,28 +108,27 @@ let
25108 "LD_LIBRARY_PATH=${cudaStub}\${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} ";
26109
27110in buildPythonPackage rec {
28− version = "1.0.0";
111+ version = "1.2.0";
29112 pname = "pytorch";
30113 disabled = !isPy3k;
31114
115+ outputs = [
116+ "out" # output standard python package
117+ "dev" # output libtorch only
118+ ];
119+
32120 src = fetchFromGitHub {
33121 owner = "pytorch";
34122 repo = "pytorch";
35123 rev = "v${version}";
36124 fetchSubmodules = true;
37− sha256 = "076cpbig4sywn9vv674c0xdg832sdrd5pk1d0725pjkm436kpvlm";
125+ sha256 = "1biyq2p48chakf2xw7hazzqmr5ps1nx475ql8vkmxjg5zaa071cz";
38126 };
39127
40− patches =
41− [ # Skips two tests that are only meant to run on multi GPUs
42− (fetchurl {
43− url = "https://github.com/pytorch/pytorch/commit/bfa666eb0deebac21b03486e26642fd70d66e478.patch";
44− sha256 = "1fgblcj02gjc0y62svwc5gnml879q3x2z7m69c9gax79dpr37s9i";
45− })
46− ];
47−
48128 dontUseCmakeConfigure = true;
129+
49130 preConfigure = lib.optionalString cudaSupport ''
131+ export TORCH_CUDA_ARCH_LIST="${lib.strings.concatStringsSep ";" final_cudaArchList}"
50132 export CC=${cudatoolkit.cc}/bin/gcc CXX=${cudatoolkit.cc}/bin/g++
51133 '' + lib.optionalString (cudaSupport && cudnn != null) ''
52134 export CUDNN_INCLUDE_DIR=${cudnn}/include
@@ -61,7 +143,6 @@ in buildPythonPackage rec {
61143 RP_NEW=$(join_by : ''${RP[@]:2})
62144 patchelf --set-rpath \$ORIGIN:''${RP_NEW} "$1"
63145 }
64−
65146 for f in $(find ''${out} -name 'libcaffe2*.so')
66147 do
67148 strip2 $f
@@ -74,38 +155,84 @@ in buildPythonPackage rec {
74155 PYTORCH_BUILD_VERSION = version;
75156 PYTORCH_BUILD_NUMBER = 0;
76157
158+ BUILD_NAMEDTENSOR = buildNamedTensor; # experimental feature
159+ USE_SYSTEM_NCCL=true; # don't build pytorch's third_party NCCL
160+
77161 # Suppress a weird warning in mkl-dnn, part of ideep in pytorch
78162 # (upstream seems to have fixed this in the wrong place?)
79163 # https://github.com/intel/mkl-dnn/commit/8134d346cdb7fe1695a2aa55771071d455fae0bc
80− NIX_CFLAGS_COMPILE = lib.optionals (numpy.blasImplementation == "mkl") [ "-Wno-error=array-bounds" ];
164+ # https://github.com/pytorch/pytorch/issues/22346
165+ #
166+ # Also of interest: pytorch ignores CXXFLAGS uses CFLAGS for both C and C++:
167+ # https://github.com/pytorch/pytorch/blob/v1.2.0/setup.py#L17
168+ NIX_CFLAGS_COMPILE = lib.optionals (numpy.blas == mkl) [ "-Wno-error=array-bounds" ];
81169
82170 nativeBuildInputs = [
83− cmake
84− utillinux
85− which
171+ cmake
172+ utillinux
173+ which
174+ ninja
86175 ] ++ lib.optionals cudaSupport [ cudatoolkit_joined ];
87176
88177 buildInputs = [
89− numpy.blas
90− ] ++ lib.optionals cudaSupport [ cudnn ]
178+ numpy.blas
179+ ] ++ lib.optionals cudaSupport [ cudnn magma nccl ]
91180 ++ lib.optionals stdenv.isLinux [ numactl ];
92181
93182 propagatedBuildInputs = [
94183 cffi
184+ click
95185 numpy
96186 pyyaml
97− ] ++ lib.optional (pythonOlder "3.5") typing;
187+ ] ++ lib.optionals openMPISupport [ openmpi ]
188+ ++ lib.optional (pythonOlder "3.5") typing
189+ ++ lib.optionals tensorboardSupport [pillow six future tensorflow-tensorboard];
190+
191+ checkInputs = [ hypothesis ninja ];
192+
193+ doCheck = false; # tests take a long time for channel release, so doCheck should be overridden only when developing
194+ checkPhase = "${cudaStubEnv}python test/run_test.py"
195+ + " --exclude utils" # utils requires git, which is not allowed in the check phase
196+
197+ # Other tests which have been disabled in previous nix derivations of pytorch.
198+ # --exclude dataloader sparse torch utils thd_distributed distributed cpp_extensions
199+ ;
200+ postInstall = ''
201+ mkdir $dev
202+ cp -r $out/${python.sitePackages}/torch/lib $dev/lib
203+ cp -r $out/${python.sitePackages}/torch/include $dev/include
204+ '';
205+
206+ postFixup = stdenv.lib.optionalString stdenv.isDarwin ''
207+ for f in $(ls $dev/lib/*.dylib); do
208+ install_name_tool -id $dev/lib/$(basename $f) $f || true
209+ done
210+
211+ install_name_tool -change @rpath/libshm.dylib $dev/lib/libshm.dylib $dev/lib/libtorch_python.dylib
212+ install_name_tool -change @rpath/libtorch.dylib $dev/lib/libtorch.dylib $dev/lib/libtorch_python.dylib
213+ install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libtorch_python.dylib
98214
99− checkInputs = [ hypothesis ];
100− checkPhase = ''
101− ${cudaStubEnv}python test/run_test.py --exclude dataloader sparse torch utils thd_distributed distributed cpp_extensions
215+ install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libtorch.dylib
216+
217+ install_name_tool -change @rpath/libtorch.dylib $dev/lib/libtorch.dylib $dev/lib/libcaffe2_observers.dylib
218+ install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libcaffe2_observers.dylib
219+
220+ install_name_tool -change @rpath/libtorch.dylib $dev/lib/libtorch.dylib $dev/lib/libcaffe2_module_test_dynamic.dylib
221+ install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libcaffe2_module_test_dynamic.dylib
222+
223+ install_name_tool -change @rpath/libtorch.dylib $dev/lib/libtorch.dylib $dev/lib/libcaffe2_detectron_ops.dylib
224+ install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libcaffe2_detectron_ops.dylib
225+
226+ install_name_tool -change @rpath/libtorch.dylib $dev/lib/libtorch.dylib $dev/lib/libshm.dylib
227+ install_name_tool -change @rpath/libc10.dylib $dev/lib/libc10.dylib $dev/lib/libshm.dylib
102228 '';
103229
230+
104231 meta = {
105232 description = "Open source, prototype-to-production deep learning platform";
106233 homepage = https://pytorch.org/;
107234 license = lib.licenses.bsd3;
108− platforms = lib.platforms.linux;
109− maintainers = with lib.maintainers; [ teh thoughtpolice ];
235+ platforms = with lib.platforms; linux ++ lib.optionals (!cudaSupport) darwin;
236+ maintainers = with lib.maintainers; [ teh thoughtpolice stites tscholak ]; # tscholak esp. for darwin-related builds
110237 };
111238}