@@ -1,8 +1,31 @@
1−{ buildPythonPackage, fetchFromGitHub, lib, numpy, pyyaml, cffi, cmake,
2− git, stdenv }:
1+{ buildPythonPackage,
2+ cudaSupport ? false, cudatoolkit ? null, cudnn ? null,
3+ fetchFromGitHub, fetchpatch, lib, numpy, pyyaml, cffi, cmake,
4+ git, stdenv, linkFarm, symlinkJoin,
5+ utillinux, which }:
36
4−buildPythonPackage rec {
5− version = "0.2.0";
7+assert cudnn == null || cudatoolkit != null;
8+assert !cudaSupport || cudatoolkit != null;
9+
10+let
11+ cudatoolkit_joined = symlinkJoin {
12+ name = "${cudatoolkit.name}-unsplit";
13+ paths = [ cudatoolkit.out cudatoolkit.lib ];
14+ };
15+
16+ # Normally libcuda.so.1 is provided at runtime by nvidia-x11 via
17+ # LD_LIBRARY_PATH=/run/opengl-driver/lib. We only use the stub
18+ # libcuda.so from cudatoolkit for running tests, so that we don’t have
19+ # to recompile pytorch on every update to nvidia-x11 or the kernel.
20+ cudaStub = linkFarm "cuda-stub" [{
21+ name = "libcuda.so.1";
22+ path = "${cudatoolkit}/lib/stubs/libcuda.so";
23+ }];
24+ cudaStubEnv = lib.optionalString cudaSupport
25+ "LD_LIBRARY_PATH=${cudaStub}\${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} ";
26+
27+in buildPythonPackage rec {
28+ version = "0.3.1";
629 pname = "pytorch";
730 name = "${pname}-${version}";
831
@@ -10,18 +33,41 @@ buildPythonPackage rec {
1033 owner = "pytorch";
1134 repo = "pytorch";
1235 rev = "v${version}";
13− sha256 = "1s3f46ga1f4lfrcj3lpvvhgkdr1pi8i2hjd9xj9qiz3a9vh2sj4n";
36+ fetchSubmodules = true;
37+ sha256 = "1k8fr97v5pf7rni5cr2pi21ixc3pdj3h3lkz28njbjbgkndh7mr3";
1438 };
1539
16− checkPhase = ''
17− ${stdenv.shell} test/run_test.sh
40+ patches = [
41+ (fetchpatch {
42+ # make sure stdatomic.h is included when checking for ATOMIC_INT_LOCK_FREE
43+ # Fixes this test failure:
44+ # RuntimeError: refcounted file mapping not supported on your system at /tmp/nix-build-python3.6-pytorch-0.3.0.drv-0/source/torch/lib/TH/THAllocator.c:525
45+ url = "https://github.com/pytorch/pytorch/commit/502aaf39cf4a878f9e4f849e5f409573aa598aa9.patch";
46+ stripLen = 3;
47+ extraPrefix = "torch/lib/";
48+ sha256 = "1miz4lhy3razjwcmhxqa4xmlcmhm65lqyin1czqczj8g16d3f62f";
49+ })
50+ ];
51+
52+ postPatch = ''
53+ substituteInPlace test/run_test.sh --replace \
54+ "INIT_METHOD='file://'\$TEMP_DIR'/shared_init_file' \$PYCMD ./test_distributed.py" \
55+ "echo Skipped for Nix package"
56+ '';
57+
58+ preConfigure = lib.optionalString cudaSupport ''
59+ export CC=${cudatoolkit.cc}/bin/gcc
60+ '' + lib.optionalString (cudaSupport && cudnn != null) ''
61+ export CUDNN_INCLUDE_DIR=${cudnn}/include
1862 '';
1963
2064 buildInputs = [
2165 cmake
2266 git
2367 numpy.blas
24− ];
68+ utillinux
69+ which
70+ ] ++ lib.optionals cudaSupport [cudatoolkit_joined cudnn];
2571
2672 propagatedBuildInputs = [
2773 cffi
@@ -29,8 +75,8 @@ buildPythonPackage rec {
2975 pyyaml
3076 ];
3177
32− preConfigure = ''
33− export NO_CUDA=1
78+ checkPhase = ''
79+ ${cudaStubEnv}${stdenv.shell} test/run_test.sh
3480 '';
3581
3682 meta = {