1 { stdenv, lib, fetchFromGitHub, fetchpatch, buildPythonPackage, python,
2 cudaSupport ? false, cudatoolkit, cudnn, nccl, magma,
3 mklDnnSupport ? true, useSystemNccl ? true,
4 MPISupport ? false, mpi,
9 cmake, util-linux, linkFarm, symlinkJoin, which,
14 # Propagated build inputs
15 dataclasses, numpy, pyyaml, cffi, click, typing-extensions,
20 # virtual pkg that consistently instantiates blas across nixpkgs
21 # See https://github.com/NixOS/nixpkgs/pull/83888
24 # ninja (https://ninja-build.org) must be available to run C++ extensions tests,
27 # dependencies for torch.utils.tensorboard
28 pillow, six, future, tensorflow-tensorboard, protobuf,
30 isPy3k, pythonOlder }:
32 # assert that everything needed for cuda is present and that the correct cuda versions are used
33 assert !cudaSupport || (let majorIs = lib.versions.major cudatoolkit.version;
34 in majorIs == "9" || majorIs == "10" || majorIs == "11");
36 # confirm that cudatoolkits are sync'd across dependencies
37 assert !(MPISupport && cudaSupport) || mpi.cudatoolkit == cudatoolkit;
38 assert !cudaSupport || magma.cudatoolkit == cudatoolkit;
41 setBool = v: if v then "1" else "0";
42 cudatoolkit_joined = symlinkJoin {
43 name = "${cudatoolkit.name}-unsplit";
44 # nccl is here purely for semantic grouping it could be moved to nativeBuildInputs
45 paths = [ cudatoolkit.out cudatoolkit.lib nccl.dev nccl.out ];
48 # Give an explicit list of supported architectures for the build, See:
49 # - pytorch bug report: https://github.com/pytorch/pytorch/issues/23573
50 # - pytorch-1.2.0 build on nixpks: https://github.com/NixOS/nixpkgs/pull/65041
52 # This list was selected by omitting the TORCH_CUDA_ARCH_LIST parameter,
53 # observing the fallback option (which selected all architectures known
54 # from cudatoolkit_10_0, pytorch-1.2, and python-3.6), and doing a binary
55 # searching to find offending architectures.
57 # NOTE: Because of sandboxing, this derivation can't auto-detect the hardware's
58 # cuda architecture, so there is also now a problem around new architectures
59 # not being supported until explicitly added to this derivation.
61 # FIXME: CMake is throwing the following warning on python-1.2:
64 # CMake Warning at cmake/public/utils.cmake:172 (message):
65 # In the future we will require one to explicitly pass TORCH_CUDA_ARCH_LIST
66 # to cmake instead of implicitly setting it as an env variable. This will
67 # become a FATAL_ERROR in future version of pytorch.
69 # If this is causing problems for your build, this derivation may have to strip
70 # away the standard `buildPythonPackage` and use the
71 # [*Adjust Build Options*](https://github.com/pytorch/pytorch/tree/v1.2.0#adjust-build-options-optional)
72 # instructions. This will also add more flexibility around configurations
73 # (allowing FBGEMM to be built in pytorch-1.1), and may future proof this
75 brokenArchs = [ "3.0" ]; # this variable is only used as documentation.
77 cudaCapabilities = rec {
85 "7.0+PTX" # I am getting a "undefined architecture compute_75" on cuda 9
86 # which leads me to believe this is the final cuda-9-compatible architecture.
91 "7.5+PTX" # < most recent architecture as of cudatoolkit_10_0 and pytorch-1.2.0
96 "8.0+PTX" # < CUDA toolkit 11.0
98 "8.6+PTX" # < CUDA toolkit 11.1
102 if !cudaSupport || cudaArchList != null
104 else cudaCapabilities."cuda${lib.versions.major cudatoolkit.version}";
106 # Normally libcuda.so.1 is provided at runtime by nvidia-x11 via
107 # LD_LIBRARY_PATH=/run/opengl-driver/lib. We only use the stub
108 # libcuda.so from cudatoolkit for running tests, so that we don’t have
109 # to recompile pytorch on every update to nvidia-x11 or the kernel.
110 cudaStub = linkFarm "cuda-stub" [{
111 name = "libcuda.so.1";
112 path = "${cudatoolkit}/lib/stubs/libcuda.so";
114 cudaStubEnv = lib.optionalString cudaSupport
115 "LD_LIBRARY_PATH=${cudaStub}\${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH ";
117 in buildPythonPackage rec {
119 # Don't forget to update pytorch-bin to the same version.
125 "out" # output standard python package
126 "dev" # output libtorch headers
127 "lib" # output libtorch libraries
130 src = fetchFromGitHub {
134 fetchSubmodules = true;
135 sha256 = "sha256-HERbvmrfhWwH164GFHU/M0KbhVAuhI5sBZSxCZy8mRk=";
138 patches = lib.optionals stdenv.isDarwin [
139 # pthreadpool added support for Grand Central Dispatch in April
140 # 2020. However, this relies on functionality (DISPATCH_APPLY_AUTO)
141 # that is available starting with macOS 10.13. However, our current
142 # base is 10.12. Until we upgrade, we can fall back on the older
144 ./pthreadpool-disable-gcd.diff
147 # The dataclasses module is included with Python >= 3.7. This should
148 # be fixed with the next PyTorch release.
150 substituteInPlace setup.py \
151 --replace "'dataclasses'" "'dataclasses; python_version < \"3.7\"'"
154 preConfigure = lib.optionalString cudaSupport ''
155 export TORCH_CUDA_ARCH_LIST="${lib.strings.concatStringsSep ";" final_cudaArchList}"
156 export CC=${cudatoolkit.cc}/bin/gcc CXX=${cudatoolkit.cc}/bin/g++
157 '' + lib.optionalString (cudaSupport && cudnn != null) ''
158 export CUDNN_INCLUDE_DIR=${cudnn}/include
161 # Use pytorch's custom configurations
162 dontUseCmakeConfigure = true;
164 BUILD_NAMEDTENSOR = setBool true;
165 BUILD_DOCS = setBool buildDocs;
167 # We only do an imports check, so do not build tests either.
168 BUILD_TEST = setBool false;
170 # Unlike MKL, oneDNN (née MKLDNN) is FOSS, so we enable support for
171 # it by default. PyTorch currently uses its own vendored version
172 # of oneDNN through Intel iDeep.
173 USE_MKLDNN = setBool mklDnnSupport;
174 USE_MKLDNN_CBLAS = setBool mklDnnSupport;
177 export MAX_JOBS=$NIX_BUILD_CORES
178 ${python.interpreter} setup.py build --cmake-only
179 ${cmake}/bin/cmake build
183 function join_by { local IFS="$1"; shift; echo "$*"; }
186 read -ra RP <<< $(patchelf --print-rpath $1)
188 RP_NEW=$(join_by : ''${RP[@]:2})
189 patchelf --set-rpath \$ORIGIN:''${RP_NEW} "$1"
191 for f in $(find ''${out} -name 'libcaffe2*.so')
197 # Override the (weirdly) wrong version set by default. See
198 # https://github.com/NixOS/nixpkgs/pull/52437#issuecomment-449718038
199 # https://github.com/pytorch/pytorch/blob/v1.0.0/setup.py#L267
200 PYTORCH_BUILD_VERSION = version;
201 PYTORCH_BUILD_NUMBER = 0;
203 USE_SYSTEM_NCCL=setBool useSystemNccl; # don't build pytorch's third_party NCCL
205 # Suppress a weird warning in mkl-dnn, part of ideep in pytorch
206 # (upstream seems to have fixed this in the wrong place?)
207 # https://github.com/intel/mkl-dnn/commit/8134d346cdb7fe1695a2aa55771071d455fae0bc
208 # https://github.com/pytorch/pytorch/issues/22346
210 # Also of interest: pytorch ignores CXXFLAGS uses CFLAGS for both C and C++:
211 # https://github.com/pytorch/pytorch/blob/v1.2.0/setup.py#L17
212 NIX_CFLAGS_COMPILE = lib.optionals (blas.implementation == "mkl") [ "-Wno-error=array-bounds" ];
214 nativeBuildInputs = [
219 ] ++ lib.optionals cudaSupport [ cudatoolkit_joined ];
221 buildInputs = [ blas blas.provider ]
222 ++ lib.optionals cudaSupport [ cudnn magma nccl ]
223 ++ lib.optionals stdenv.isLinux [ numactl ];
225 propagatedBuildInputs = [
231 # the following are required for tensorboard support
232 pillow six future tensorflow-tensorboard protobuf
233 ] ++ lib.optionals MPISupport [ mpi ]
234 ++ lib.optionals (pythonOlder "3.7") [ dataclasses ];
236 checkInputs = [ hypothesis ninja psutil ];
238 # Tests take a long time and may be flaky, so just sanity-check imports
240 pythonImportsCheck = [
244 checkPhase = with lib.versions; with lib.strings; concatStringsSep " " [
246 "${python.interpreter} test/run_test.py"
248 (concatStringsSep " " [
249 "utils" # utils requires git, which is not allowed in the check phase
251 # "dataloader" # psutils correctly finds and triggers multiprocessing, but is too sandboxed to run -- resulting in numerous errors
252 # ^^^^^^^^^^^^ NOTE: while test_dataloader does return errors, these are acceptable errors and do not interfere with the build
254 # tensorboard has acceptable failures for pytorch 1.3.x due to dependencies on tensorboard-plugins
255 (optionalString (majorMinor version == "1.3" ) "tensorboard")
260 cp -r $out/${python.sitePackages}/torch/include $dev/include
261 cp -r $out/${python.sitePackages}/torch/share $dev/share
263 # Fix up library paths for split outputs
265 $dev/share/cmake/Torch/TorchConfig.cmake \
266 --replace \''${TORCH_INSTALL_PREFIX}/lib "$lib/lib"
269 $dev/share/cmake/Caffe2/Caffe2Targets-release.cmake \
270 --replace \''${_IMPORT_PREFIX}/lib "$lib/lib"
273 cp -r $out/${python.sitePackages}/torch/lib $lib/lib
276 postFixup = lib.optionalString stdenv.isDarwin ''
277 for f in $(ls $lib/lib/*.dylib); do
278 install_name_tool -id $lib/lib/$(basename $f) $f || true
281 install_name_tool -change @rpath/libshm.dylib $lib/lib/libshm.dylib $lib/lib/libtorch_python.dylib
282 install_name_tool -change @rpath/libtorch.dylib $lib/lib/libtorch.dylib $lib/lib/libtorch_python.dylib
283 install_name_tool -change @rpath/libc10.dylib $lib/lib/libc10.dylib $lib/lib/libtorch_python.dylib
285 install_name_tool -change @rpath/libc10.dylib $lib/lib/libc10.dylib $lib/lib/libtorch.dylib
287 install_name_tool -change @rpath/libtorch.dylib $lib/lib/libtorch.dylib $lib/lib/libcaffe2_observers.dylib
288 install_name_tool -change @rpath/libc10.dylib $lib/lib/libc10.dylib $lib/lib/libcaffe2_observers.dylib
290 install_name_tool -change @rpath/libtorch.dylib $lib/lib/libtorch.dylib $lib/lib/libcaffe2_module_test_dynamic.dylib
291 install_name_tool -change @rpath/libc10.dylib $lib/lib/libc10.dylib $lib/lib/libcaffe2_module_test_dynamic.dylib
293 install_name_tool -change @rpath/libtorch.dylib $lib/lib/libtorch.dylib $lib/lib/libcaffe2_detectron_ops.dylib
294 install_name_tool -change @rpath/libc10.dylib $lib/lib/libc10.dylib $lib/lib/libcaffe2_detectron_ops.dylib
296 install_name_tool -change @rpath/libtorch.dylib $lib/lib/libtorch.dylib $lib/lib/libshm.dylib
297 install_name_tool -change @rpath/libc10.dylib $lib/lib/libc10.dylib $lib/lib/libshm.dylib
301 description = "Open source, prototype-to-production deep learning platform";
302 homepage = "https://pytorch.org/";
303 license = licenses.bsd3;
304 platforms = with platforms; linux ++ lib.optionals (!cudaSupport) darwin;
305 maintainers = with maintainers; [ danieldk teh thoughtpolice tscholak ]; # tscholak esp. for darwin-related builds
306 # error: use of undeclared identifier 'noU'; did you mean 'no'?
307 broken = stdenv.isDarwin;