cudaPackages.cudnn: migrate to redist cuda, fix missing zlib (#168748)

* cudaPackages.cudnn: migrate to redist cudaPackages

* cudaPackages.cudnn: fix missing zlib in rpath

* cudaPackages.cudnn: remove the propagated cudatoolkit

* cudaPackages.cudnn: use autoPatchelfHook

instead of custom find ... -exec ldd | grep routine
mark libcudnn_cnn_infer.so as needed for libcudnn.so on cudnn>=8.0.5
- a hint for autoPatchelf, as an alternative to manually adding $ORIGIN

* cudaPackages.cudnn: use automatic hooks for rpath

as a more common way to use addOpenGLRunpath and autoPatchelf with cudaPackages

* cudaPackages.cudnn: consume individual cuda packages

...since cudnn is part of the cuda package set

- introduces the scary useCudatoolkitRunfile function argument
to discourage usage of the runfile-based cudatoolkit
- instead of the rather hidden useRedist term in let ... in
- repeats cudatoolkit_root pattern after cuda_joined in pytorch &c
(the "toolchain view")
- redist packages are marked optional to support cuda<11.4 where the
attributes for redist packages do not exist

* cudaPackages.cudnn: update to pname+version

Co-authored-by: Sandro <sandro.jaeckel@gmail.com>

Co-authored-by: Sandro <sandro.jaeckel@gmail.com>

authored by Serge K Sandro and committed by GitHub d924de58 047473aa

+54 -33
+5 -1
pkgs/development/libraries/science/math/cudnn/extension.nix
··· 5 5 6 6 ### CuDNN 7 7 8 - buildCuDnnPackage = args: callPackage ./generic.nix {} args; 8 + buildCuDnnPackage = args: 9 + let 10 + useCudatoolkitRunfile = lib.versionOlder cudaVersion "11.3.999"; 11 + in 12 + callPackage ./generic.nix { inherit useCudatoolkitRunfile; } args; 9 13 10 14 toUnderscore = str: lib.replaceStrings ["."] ["_"] str; 11 15
+48 -32
pkgs/development/libraries/science/math/cudnn/generic.nix
··· 1 1 { stdenv 2 2 , lib 3 - , cudatoolkit 3 + , zlib 4 + , useCudatoolkitRunfile ? false 5 + , cudaVersion 6 + , cudaMajorVersion 7 + , cudatoolkit # if cuda>=11: only used for .cc 8 + , libcublas ? null # cuda <11 doesn't ship redist packages 9 + , autoPatchelfHook 10 + , autoAddOpenGLRunpathHook 4 11 , fetchurl 5 - , addOpenGLRunpath 6 12 , # The distributed version of CUDNN includes both dynamically liked .so files, 7 13 # as well as statically linked .a files. However, CUDNN is quite large 8 14 # (multiple gigabytes), so you can save some space in your nix store by ··· 17 23 , url 18 24 , hash ? null 19 25 , sha256 ? null 20 - , supportedCudaVersions ? [] 26 + , supportedCudaVersions ? [ ] 21 27 }: 22 28 23 29 assert (hash != null) || (sha256 != null); 24 30 31 + assert useCudatoolkitRunfile || (libcublas != null); 32 + 25 33 let 34 + inherit (cudatoolkit) cc; 35 + 26 36 majorMinorPatch = version: lib.concatStringsSep "." (lib.take 3 (lib.splitVersion version)); 27 37 version = majorMinorPatch fullVersion; 28 - in stdenv.mkDerivation { 29 - name = "cudatoolkit-${cudatoolkit.majorVersion}-cudnn-${version}"; 30 38 39 + cudatoolkit_root = 40 + if useCudatoolkitRunfile 41 + then cudatoolkit 42 + else libcublas; 43 + in 44 + stdenv.mkDerivation { 45 + pname = "cudatoolkit-${cudaMajorVersion}-cudnn"; 31 46 inherit version; 32 - # It's often the case that the src depends on the version of cudatoolkit it's 33 - # being linked against, so we pass in `cudatoolkit` as an argument to `mkSrc`. 47 + 34 48 src = fetchurl { 35 49 inherit url hash sha256; 36 50 }; 37 51 38 - nativeBuildInputs = [ addOpenGLRunpath ]; 52 + # Check and normalize Runpath against DT_NEEDED using autoPatchelf. 53 + # Prepend /run/opengl-driver/lib using addOpenGLRunpath for dlopen("libcudacuda.so") 54 + nativeBuildInputs = [ 55 + autoPatchelfHook 56 + autoAddOpenGLRunpathHook 57 + ]; 39 58 40 - # Some cuDNN libraries depend on things in cudatoolkit, eg. 41 - # libcudnn_ops_infer.so.8 tries to load libcublas.so.11. So we need to patch 42 - # cudatoolkit into RPATH. See also https://github.com/NixOS/nixpkgs/blob/88a2ad974692a5c3638fcdc2c772e5770f3f7b21/pkgs/development/python-modules/jaxlib/bin.nix#L78-L98. 59 + # Used by autoPatchelfHook 60 + buildInputs = [ 61 + cc.cc.lib # libstdc++ 62 + zlib 63 + cudatoolkit_root 64 + ]; 65 + 66 + # We used to patch Runpath here, but now we use autoPatchelfHook 43 67 # 44 68 # Note also that version <=8.3.0 contained a subdirectory "lib64/" but in 45 69 # version 8.3.2 it seems to have been renamed to simply "lib/". 46 70 installPhase = '' 47 71 runHook preInstall 48 72 49 - function fixRunPath { 50 - p=$(patchelf --print-rpath $1) 51 - patchelf --set-rpath "''${p:+$p:}${lib.makeLibraryPath [ stdenv.cc.cc cudatoolkit.lib ]}:${cudatoolkit}/lib:\$ORIGIN/" $1 52 - } 53 - 54 - for sofile in {lib,lib64}/lib*.so; do 55 - fixRunPath $sofile 56 - done 57 - 58 73 mkdir -p $out 59 74 cp -a include $out/include 60 75 [ -d "lib/" ] && cp -a lib $out/lib ··· 66 81 runHook postInstall 67 82 ''; 68 83 69 - # Set RUNPATH so that libcuda in /run/opengl-driver(-32)/lib can be found. 70 - # See the explanation in addOpenGLRunpath. 71 - postFixup = '' 72 - for lib in $out/lib/lib*.so; do 73 - addOpenGLRunpath $lib 74 - done 84 + # Without --add-needed autoPatchelf forgets $ORIGIN on cuda>=8.0.5. 85 + postFixup = lib.optionalString (lib.versionAtLeast fullVersion "8.0.5") '' 86 + patchelf $out/lib/libcudnn.so --add-needed libcudnn_cnn_infer.so 75 87 ''; 76 88 77 - propagatedBuildInputs = [ 78 - cudatoolkit 79 - ]; 80 - 81 89 passthru = { 82 - inherit cudatoolkit; 90 + inherit useCudatoolkitRunfile; 91 + 92 + cudatoolkit = lib.warn '' 93 + cudnn.cudatoolkit passthru attribute is deprecated; 94 + if your derivation uses cudnn directly, it should probably consume cudaPackages instead 95 + '' 96 + cudatoolkit; 97 + 83 98 majorVersion = lib.versions.major version; 84 99 }; 85 100 ··· 89 104 # official version constraints (as recorded in default.nix). In some cases 90 105 # you _may_ be able to smudge version constraints, just know that you're 91 106 # embarking into unknown and unsupported territory when doing so. 92 - broken = !(elem cudatoolkit.majorMinorVersion supportedCudaVersions); 107 + broken = !(elem cudaVersion supportedCudaVersions); 93 108 description = "NVIDIA CUDA Deep Neural Network library (cuDNN)"; 94 109 homepage = "https://developer.nvidia.com/cudnn"; 110 + # TODO: consider marking unfreRedistributable when not using runfile 95 111 license = licenses.unfree; 96 112 platforms = [ "x86_64-linux" ]; 97 113 maintainers = with maintainers; [ mdaiter samuela ];
+1
pkgs/games/katago/default.nix
··· 52 52 eigen 53 53 ] ++ lib.optionals (enableGPU && enableCuda) [ 54 54 cudaPackages.cudnn 55 + cudaPackages.cudatoolkit 55 56 mesa.drivers 56 57 ] ++ lib.optionals (enableGPU && !enableCuda) [ 57 58 opencl-headers