anchor.diff:      repo   = "pytorch";
       rev    = "v${version}";
       fetchSubmodules = true;
  -    sha256 = "1biyq2p48chakf2xw7hazzqmr5ps1nx475ql8vkmxjg5zaa071cz";
  +    sha256 = "1aa1il4f98pswfj20cv27yfb91l1jcq4515i7mvq7sh5647yzwms";
     };
  -
  -  dontUseCmakeConfigure = true;
   
     preConfigure = lib.optionalString cudaSupport ''
       export TORCH_CUDA_ARCH_LIST="${lib.strings.concatStringsSep ";" final_cudaArchList}"
       export CC=${cudatoolkit.cc}/bin/gcc CXX=${cudatoolkit.cc}/bin/g++
     '' + lib.optionalString (cudaSupport && cudnn != null) ''
       export CUDNN_INCLUDE_DIR=${cudnn}/include
  +  '';
  +
  +  patches = [
  +    # Prevents a race condition which would be introduced by pull 30333.
  +    # See https://github.com/pytorch/pytorch/issues/32277
  +    # Can be removed >1.5.0.
  +    (fetchpatch {
  +      url = "https://patch-diff.githubusercontent.com/raw/pytorch/pytorch/pull/30332.patch";
  +      sha256 = "1v9dwbhz3rdxcx6sz8y8j9n3bj6nqs78b1r8yg89yc15n6l4cqx2";
  +    })
  +
  +    # Fixes errors with gcc-9 compilation. Cherry-picked on advice from ezyang.
  +    # See https://github.com/pytorch/pytorch/issues/32277
  +    # Can be removed >1.5.0.
  +    (fetchpatch {
  +      url = "https://patch-diff.githubusercontent.com/raw/pytorch/pytorch/pull/30333.patch";
  +      sha256 = "139413fl37h2fnil0cv99a67mqqnsh02k74b92by1qyr6pcfyg3q";
  +    })
  +  ];
  +
  +  # Use pytorch's custom configurations
  +  dontUseCmakeConfigure = true;
  +
  +  BUILD_NAMEDTENSOR = true;
  +  BUILD_DOCS = buildDocs;
  +  USE_MKLDNN = mklDnnSupport;
  +  preBuild = ''
  +    export MAX_JOBS=$NIX_BUILD_CORES
  +    ${python.interpreter} setup.py build --cmake-only
  +    ${cmake}/bin/cmake build
     '';
   
     preFixup = ''
  
positive.code:     click
      numpy
      pyyaml
    ] ++ lib.optionals openMPISupport [ openmpi ]
      ++ lib.optional (pythonOlder "3.5") typing
      ++ lib.optionals tensorboardSupport [pillow six future tensorflow-tensorboard];
  
    checkInputs = [ hypothesis ninja ];
  
    doCheck = false; # tests take a long time for channel release, so doCheck should be overridden only when developing
    checkPhase = "${cudaStubEnv}python test/run_test.py"
      + " --exclude utils" # utils requires git, which is not allowed in the check phase
  
      # Other tests which have been disabled in previous nix derivations of pytorch.
      # --exclude dataloader sparse torch utils thd_distributed distributed cpp_extensions
      ;
    postInstall = ''
      mkdir $dev
      cp -r $out/${python.sitePackages}/torch/lib     $dev/lib
      cp -r $out/${python.sitePackages}/torch/include $dev/include
    '';
  
    postFixup = stdenv.lib.optionalString stdenv.isDarwin ''
  
negative.code:     homepage    = "https://pytorch.org/";
      license     = lib.licenses.bsd3;
      platforms   = with lib.platforms; linux ++ lib.optionals (!cudaSupport) darwin;
      maintainers = with lib.maintainers; [ teh thoughtpolice tscholak ]; # tscholak esp. for darwin-related builds
    };
  }
  