From 291f72c55aeffc663d7d49626e10fd6970696b84 Mon Sep 17 00:00:00 2001 From: Bruno Rodrigues Date: Fri, 9 Aug 2024 15:03:27 +0200 Subject: [PATCH 1/3] rPackages.torch: use binary --- pkgs/development/r-modules/default.nix | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pkgs/development/r-modules/default.nix b/pkgs/development/r-modules/default.nix index 678604f04af2e..8937f8ce4a52f 100644 --- a/pkgs/development/r-modules/default.nix +++ b/pkgs/development/r-modules/default.nix @@ -1803,10 +1803,23 @@ let ''; }); - torch = old.torch.overrideAttrs (attrs: { - preConfigure = '' - patchShebangs configure - ''; + torch = let + # Sets the correct string to download either the binary for + # the cpu version of the torch package, or the + # the cuda-enabled version. + # To use gpu acceleration, set `config.cudaSupport = true;` + # when importing nixpkgs in your shell + accel = if pkgs.config.cudaSupport then "cu118" else "cpu"; + + binary_sha = if pkgs.config.cudaSupport then + "sha256-a80sG89C0svZzkjNRpY0rTR2P1JdvKAbWDGIIghsv2Y=" else + "sha256-qUn8Rot6ME7iTvtNd52iw3ebqMnpLz7kwl/9GoPHD+I="; + in + old.torch.overrideAttrs (attrs: { + src = pkgs.fetchzip { + url = "https://torch-cdn.mlverse.org/packages/${accel}/0.13.0/src/contrib/torch_0.13.0_R_x86_64-pc-linux-gnu.tar.gz"; + sha256 = binary_sha; + }; }); pak = old.pak.overrideAttrs (attrs: { From 6b4943ee634127b42303e35cdf55d461f4a7e19a Mon Sep 17 00:00:00 2001 From: Bruno Rodrigues Date: Fri, 9 Aug 2024 15:05:13 +0200 Subject: [PATCH 2/3] doc: update R section to explain how to use gpu acceleration for torch --- doc/languages-frameworks/r.section.md | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/doc/languages-frameworks/r.section.md b/doc/languages-frameworks/r.section.md index ad0fb10987c98..21829586fbb0e 100644 --- a/doc/languages-frameworks/r.section.md +++ b/doc/languages-frameworks/r.section.md @@ -94,6 +94,54 @@ Executing `nix-shell` will then drop you into an environment equivalent to the one above. If you need additional packages just add them to the list and re-enter the shell. +## torch with GPU acceleration {#torch-gpu-accel} + +The `torch` package included in `nixpkgs` is based on the binary packages by the +mlverse team. By default, the CPU version of the package is installed. On Linux +distributions other than NixOS, it is possible to enable GPU acceleration by +setting `config.cudaSupport` to true when importing `nixpkgs` in a `shell.nix`. +You will also need to use `nixglhost`. Here is an example of such a shell: + +```nix +{ pkgs ? import {config.cudaSupport = true; }, lib ? pkgs.lib }: + +let + nixglhost-sources = pkgs.fetchFromGitHub { + owner = "numtide"; + repo = "nix-gl-host"; + rev = "main"; + # Replace this with the hash Nix will complain about, TOFU style. + hash = ""; + }; + + nixglhost = pkgs.callPackage "${nixglhost-sources}/default.nix" { }; + + rpkgs = builtins.attrValues { + inherit (pkgs.rPackages) + torch; + }; + + wrapped_pkgs = pkgs.rWrapper.override { + packages = [ rpkgs ]; + }; + +in pkgs.mkShell { + buildInputs = [ + nixglhost + wrapped_pkgs + ]; +} + +``` + +After executing `nix-shell` to drop in that environment, call `nixglhost R` to +start a GPU accelerated R session. Test that everything worked well by calling +`torch::cuda_memory_summary()` which should return a list of summary statistics +regarding the available GPU memory. + +We are looking for contributors that could help us package the darwin binaries +as well as fix `torch` for NixOS as well. + ## Updating the package set {#updating-the-package-set} There is a script and associated environment for regenerating the package From 3482c1135c52147baecd400b309f9bfde563e16c Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Fri, 16 Aug 2024 13:41:29 +0000 Subject: [PATCH 3/3] rPackages.torch: (torch-bin) link cuda libraries --- pkgs/development/r-modules/default.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkgs/development/r-modules/default.nix b/pkgs/development/r-modules/default.nix index 8937f8ce4a52f..bf1c3e22b6cbd 100644 --- a/pkgs/development/r-modules/default.nix +++ b/pkgs/development/r-modules/default.nix @@ -1816,6 +1816,16 @@ let "sha256-qUn8Rot6ME7iTvtNd52iw3ebqMnpLz7kwl/9GoPHD+I="; in old.torch.overrideAttrs (attrs: { + nativeBuildInputs = attrs.nativeBuildInputs or [ ] ++ [ + pkgs.autoPatchelfHook + pkgs.autoAddDriverRunpath + ]; + buildInputs = attrs.buildInputs or [ ] ++ [ + "${lib.getLib R}/lib/R" # libR + pkgs.zlib + ] ++ lib.optionals pkgs.config.cudaSupport [ + pkgs.cudaPackages.cuda_cudart + ]; src = pkgs.fetchzip { url = "https://torch-cdn.mlverse.org/packages/${accel}/0.13.0/src/contrib/torch_0.13.0_R_x86_64-pc-linux-gnu.tar.gz"; sha256 = binary_sha;