From c4482a0c2a945806495ab3d7847b3082540f558d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20de=20Kok?= Date: Mon, 15 Feb 2021 17:08:20 +0100 Subject: [PATCH 1/3] libtorch-bin: do not depend on nvidia_x11 libcuda.so should be provided through /run/opengl-driver/lib to use the system's version. This also avoids rebuilds when linuxPackages is updated. --- .../libraries/science/math/libtorch/bin.nix | 11 ++--------- pkgs/top-level/all-packages.nix | 1 - 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/pkgs/development/libraries/science/math/libtorch/bin.nix b/pkgs/development/libraries/science/math/libtorch/bin.nix index 9631f3931cab..5e82d50ed9cb 100644 --- a/pkgs/development/libraries/science/math/libtorch/bin.nix +++ b/pkgs/development/libraries/science/math/libtorch/bin.nix @@ -8,7 +8,6 @@ , fixDarwinDylibNames , cudaSupport -, nvidia_x11 }: let @@ -24,12 +23,7 @@ in stdenv.mkDerivation { nativeBuildInputs = if stdenv.isDarwin then [ fixDarwinDylibNames ] - else [ addOpenGLRunpath patchelf ] - ++ lib.optionals cudaSupport [ addOpenGLRunpath ]; - - buildInputs = [ - stdenv.cc.cc - ] ++ lib.optionals cudaSupport [ nvidia_x11 ]; + else [ patchelf ] ++ lib.optionals cudaSupport [ addOpenGLRunpath ]; dontBuild = true; dontConfigure = true; @@ -56,8 +50,7 @@ in stdenv.mkDerivation { ''; postFixup = let - libPaths = [ stdenv.cc.cc.lib ] - ++ lib.optionals cudaSupport [ nvidia_x11 ]; + libPaths = [ stdenv.cc.cc.lib ]; rpath = lib.makeLibraryPath libPaths; in lib.optionalString stdenv.isLinux '' find $out/lib -type f \( -name '*.so' -or -name '*.so.*' \) | while read lib; do diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index beeecbea57ef..2812f73bc30a 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -1671,7 +1671,6 @@ in else libtensorflow-bin; libtorch-bin = callPackage ../development/libraries/science/math/libtorch/bin.nix { - inherit (linuxPackages) nvidia_x11; cudaSupport = config.cudaSupport or false; }; From d5b676eb34358dcb7dee47d2be2981754e94bea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20de=20Kok?= Date: Mon, 15 Feb 2021 17:19:00 +0100 Subject: [PATCH 2/3] libtorch-bin: fix passthru.tests for CUDA - Provide the CUDA toolkit and CuDNN dependencies. - Expose libcuda.so stub to run the test in the sandbox. --- .../libraries/science/math/libtorch/bin.nix | 16 +++++++-- .../science/math/libtorch/test/default.nix | 35 ++++++++++++++++--- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/pkgs/development/libraries/science/math/libtorch/bin.nix b/pkgs/development/libraries/science/math/libtorch/bin.nix index 5e82d50ed9cb..a301b0f881eb 100644 --- a/pkgs/development/libraries/science/math/libtorch/bin.nix +++ b/pkgs/development/libraries/science/math/libtorch/bin.nix @@ -8,9 +8,16 @@ , fixDarwinDylibNames , cudaSupport +, cudatoolkit_10_2 +, cudnn_cudatoolkit_10_2 }: let + # The binary libtorch distribution statically links the CUDA + # toolkit. This means that we do not need to provide CUDA to + # this derivation. However, we should ensure on version bumps + # that the CUDA toolkit for `passthru.tests` is still + # up-to-date. version = "1.7.1"; device = if cudaSupport then "cuda" else "cpu"; srcs = import ./binary-hashes.nix version; @@ -50,8 +57,7 @@ in stdenv.mkDerivation { ''; postFixup = let - libPaths = [ stdenv.cc.cc.lib ]; - rpath = lib.makeLibraryPath libPaths; + rpath = lib.makeLibraryPath [ stdenv.cc.cc.lib ]; in lib.optionalString stdenv.isLinux '' find $out/lib -type f \( -name '*.so' -or -name '*.so.*' \) | while read lib; do echo "setting rpath for $lib..." @@ -101,7 +107,11 @@ in stdenv.mkDerivation { outputs = [ "out" "dev" ]; - passthru.tests.cmake = callPackage ./test { }; + passthru.tests.cmake = callPackage ./test { + inherit cudaSupport; + cudatoolkit = cudatoolkit_10_2; + cudnn = cudnn_cudatoolkit_10_2; + }; meta = with lib; { description = "C++ API of the PyTorch machine learning framework"; diff --git a/pkgs/development/libraries/science/math/libtorch/test/default.nix b/pkgs/development/libraries/science/math/libtorch/test/default.nix index e69807871f46..60f9b5ad8846 100644 --- a/pkgs/development/libraries/science/math/libtorch/test/default.nix +++ b/pkgs/development/libraries/science/math/libtorch/test/default.nix @@ -1,6 +1,28 @@ -{ stdenv, cmake, libtorch-bin, symlinkJoin }: +{ lib +, stdenv +, cmake +, libtorch-bin +, linkFarm +, symlinkJoin -stdenv.mkDerivation { +, cudaSupport +, cudatoolkit +, cudnn +}: +let + cudatoolkit_joined = symlinkJoin { + name = "${cudatoolkit.name}-unsplit"; + paths = [ cudatoolkit.out cudatoolkit.lib ]; + }; + + # We do not have access to /run/opengl-driver/lib in the sandbox, + # so use a stub instead. + cudaStub = linkFarm "cuda-stub" [{ + name = "libcuda.so.1"; + path = "${cudatoolkit}/lib/stubs/libcuda.so"; + }]; + +in stdenv.mkDerivation { pname = "libtorch-test"; version = libtorch-bin.version; @@ -8,7 +30,11 @@ stdenv.mkDerivation { nativeBuildInputs = [ cmake ]; - buildInputs = [ libtorch-bin ]; + buildInputs = [ libtorch-bin ] ++ + lib.optionals cudaSupport [ cudnn ]; + + cmakeFlags = lib.optionals cudaSupport + [ "-DCUDA_TOOLKIT_ROOT_DIR=${cudatoolkit_joined}" ]; doCheck = true; @@ -17,6 +43,7 @@ stdenv.mkDerivation { ''; checkPhase = '' - ./test + LD_LIBRARY_PATH=${cudaStub}''${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH \ + ./test ''; } From 67e6eba6ca73374ef0efcef47665e81e97a6b534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20de=20Kok?= Date: Mon, 15 Feb 2021 19:53:57 +0100 Subject: [PATCH 3/3] libtorch-bin: add danieldk as maintainer --- pkgs/development/libraries/science/math/libtorch/bin.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/development/libraries/science/math/libtorch/bin.nix b/pkgs/development/libraries/science/math/libtorch/bin.nix index a301b0f881eb..72c4e5ac1eb1 100644 --- a/pkgs/development/libraries/science/math/libtorch/bin.nix +++ b/pkgs/development/libraries/science/math/libtorch/bin.nix @@ -117,6 +117,7 @@ in stdenv.mkDerivation { description = "C++ API of the PyTorch machine learning framework"; homepage = "https://pytorch.org/"; license = licenses.unfree; # Includes CUDA and Intel MKL. + maintainers = with maintainers; [ danieldk ]; platforms = with platforms; linux ++ darwin; }; }