CUDA: Difference between revisions
→Enabling CUDA In Packages: Improved tips on installing large CUDA packages without a cache |
|||
| (16 intermediate revisions by 5 users not shown) | |||
| Line 1: | Line 1: | ||
NixOS supports using NVIDIA GPUs for pure computing purposes, not just for graphics. For example, many users rely on NixOS for machine learning both locally and on cloud instances. These use cases are supported by the [https://github.com/orgs/NixOS/teams/cuda-maintainers @NixOS/cuda-maintainers team] on GitHub ([https://github.com/orgs/NixOS/projects/27 project board]). If you have an issue using your NVIDIA GPU for computing purposes [https://github.com/NixOS/nixpkgs/issues/new/choose open an issue] on GitHub and tag <code>@NixOS/cuda-maintainers</code>. | NixOS supports using NVIDIA GPUs for pure computing purposes, not just for graphics. For example, many users rely on NixOS for machine learning both locally and on cloud instances. These use cases are supported by the [https://github.com/orgs/NixOS/teams/cuda-maintainers @NixOS/cuda-maintainers team] on GitHub ([https://github.com/orgs/NixOS/projects/27 project board]). If you have an issue using your NVIDIA GPU for computing purposes [https://github.com/NixOS/nixpkgs/issues/new/choose open an issue] on GitHub and tag <code>@NixOS/cuda-maintainers</code>. | ||
{{tip|1='''Cache''': Using the | {{tip|1='''Cache''': Using the binary cache is recommended! It will save you valuable time and electrons. Click [[#Setting up CUDA Binary Cache|here]] for more details.}} | ||
{{tip|1='''Data center GPUs''': Note that you may need to adjust your driver version to use "data center" GPUs like V100/A100s. See [https://discourse.nixos.org/t/how-to-use-nvidia-v100-a100-gpus/17754 this thread] for more info.}} | {{tip|1='''Data center GPUs''': Note that you may need to adjust your driver version to use "data center" GPUs like V100/A100s. See [https://discourse.nixos.org/t/how-to-use-nvidia-v100-a100-gpus/17754 this thread] for more info.}} | ||
== Driver Installation == | |||
Assuming you've followed the [[NVIDIA]] page correctly, and have a CUDA compatible GPU, you shouldn't need to do any further configuration. You can confirm your CUDA version by running the following command in your terminal. | |||
{{code|lang=console|line=no|<nowiki>nvidia-smi | grep CUDA</nowiki>}} | |||
== <code>cudatoolkit</code>, <code>cudnn</code>, and related packages == | == <code>cudatoolkit</code>, <code>cudnn</code>, and related packages == | ||
| Line 17: | Line 23: | ||
* By making a FHS user env | * By making a FHS user env | ||
<syntaxhighlight lang="nix" line="1" start="1"># flake.nix, run with `nix develop` | |||
# Run with `nix-shell cuda-fhs.nix` | # Run with `nix-shell cuda-fhs.nix` | ||
{ pkgs ? import | { pkgs ? import <nixpkgs> {} }: | ||
let | |||
# Change according to the driver used: stable, beta | |||
nvidiaPackage = pkgs.linuxPackages.nvidiaPackages.stable; | |||
in | |||
(pkgs.buildFHSEnv { | (pkgs.buildFHSEnv { | ||
name = "cuda-env"; | name = "cuda-env"; | ||
| Line 35: | Line 45: | ||
unzip | unzip | ||
cudatoolkit | cudatoolkit | ||
nvidiaPackage | |||
libGLU libGL | libGLU libGL | ||
xorg.libXi xorg.libXmu freeglut | xorg.libXi xorg.libXmu freeglut | ||
| Line 47: | Line 57: | ||
profile = '' | profile = '' | ||
export CUDA_PATH=${pkgs.cudatoolkit} | export CUDA_PATH=${pkgs.cudatoolkit} | ||
# export LD_LIBRARY_PATH=${ | # export LD_LIBRARY_PATH=${nvidiaPackage}/lib | ||
export EXTRA_LDFLAGS="-L/lib -L${ | export EXTRA_LDFLAGS="-L/lib -L${nvidiaPackage}/lib" | ||
export EXTRA_CCFLAGS="-I/usr/include" | export EXTRA_CCFLAGS="-I/usr/include" | ||
''; | ''; | ||
}).env | }).env | ||
</ | </syntaxhighlight> | ||
* By making a nix-shell | * By making a nix-shell | ||
<syntaxhighlight lang="nix" line="1" start="1"> | |||
# Run with `nix-shell cuda-shell.nix` | # flake.nix, run with `nix develop`# Run with `nix-shell cuda-shell.nix` | ||
{ pkgs ? import | { pkgs ? import <nixpkgs> {} }: | ||
let | |||
nvidiaPackage = pkgs.linuxPackages.nvidiaPackages.stable; | |||
in | |||
pkgs.mkShell { | pkgs.mkShell { | ||
name = "cuda-env-shell"; | name = "cuda-env-shell"; | ||
| Line 64: | Line 77: | ||
git gitRepo gnupg autoconf curl | git gitRepo gnupg autoconf curl | ||
procps gnumake util-linux m4 gperf unzip | procps gnumake util-linux m4 gperf unzip | ||
cudatoolkit | cudatoolkit nvidiaPackage | ||
libGLU libGL | libGLU libGL | ||
xorg.libXi xorg.libXmu freeglut | xorg.libXi xorg.libXmu freeglut | ||
| Line 72: | Line 85: | ||
shellHook = '' | shellHook = '' | ||
export CUDA_PATH=${pkgs.cudatoolkit} | export CUDA_PATH=${pkgs.cudatoolkit} | ||
# export LD_LIBRARY_PATH=${ | # export LD_LIBRARY_PATH=${nvidiaPackage}/lib:${pkgs.ncurses}/lib | ||
export EXTRA_LDFLAGS="-L/lib -L${ | export EXTRA_LDFLAGS="-L/lib -L${nvidiaPackage}/lib" | ||
export EXTRA_CCFLAGS="-I/usr/include" | export EXTRA_CCFLAGS="-I/usr/include" | ||
''; | ''; | ||
} | } | ||
</ | </syntaxhighlight> | ||
* By making a flake.nix<syntaxhighlight lang="nix" line="1" start="1"> | * By making a flake.nix | ||
# flake.nix, run with `nix develop` | <syntaxhighlight lang="nix" line="1" start="1"># flake.nix, run with `nix develop` | ||
{ | { | ||
description = "CUDA development environment"; | description = "CUDA development environment"; | ||
| Line 94: | Line 107: | ||
config.cudaVersion = "12"; | config.cudaVersion = "12"; | ||
}; | }; | ||
# Change according to the driver used: stable, beta | |||
nvidiaPackage = pkgs.linuxPackages.nvidiaPackages.stable; | |||
in { | in { | ||
# alejandra is a nix formatter with a beautiful output | # alejandra is a nix formatter with a beautiful output | ||
| Line 103: | Line 118: | ||
cudaPackages.cuda_cudart | cudaPackages.cuda_cudart | ||
cudatoolkit | cudatoolkit | ||
nvidiaPackage | |||
cudaPackages.cudnn | cudaPackages.cudnn | ||
libGLU | libGLU | ||
| Line 115: | Line 130: | ||
xorg.libXrandr | xorg.libXrandr | ||
zlib | zlib | ||
ncurses | |||
stdenv.cc | stdenv.cc | ||
binutils | binutils | ||
| Line 122: | Line 137: | ||
shellHook = '' | shellHook = '' | ||
export LD_LIBRARY_PATH="${ | export LD_LIBRARY_PATH="${nvidiaPackage}/lib:$LD_LIBRARY_PATH" | ||
export CUDA_PATH=${pkgs.cudatoolkit} | export CUDA_PATH=${pkgs.cudatoolkit} | ||
export EXTRA_LDFLAGS="-L/lib -L${ | export EXTRA_LDFLAGS="-L/lib -L${nvidiaPackage}/lib" | ||
export EXTRA_CCFLAGS="-I/usr/include" | export EXTRA_CCFLAGS="-I/usr/include" | ||
export CMAKE_PREFIX_PATH="${pkgs.fmt.dev}:$CMAKE_PREFIX_PATH" | export CMAKE_PREFIX_PATH="${pkgs.fmt.dev}:$CMAKE_PREFIX_PATH" | ||
| Line 131: | Line 146: | ||
}; | }; | ||
}; | }; | ||
} | }</syntaxhighlight> | ||
</syntaxhighlight> | |||
== Setting up CUDA Binary Cache == | == Setting up CUDA Binary Cache == | ||
The | The binary cache contains pre-built CUDA packages. By adding it to your system, Nix will fetch these packages instead of building them, saving valuable time and processing power. | ||
For more information, refer to the [[Binary Cache#Using a binary cache Using a binary cache|Using a binary cache]] page. | For more information, refer to the [[Binary Cache#Using a binary cache Using a binary cache|Using a binary cache]] page. | ||
{{ | {{info|1=You need to rebuild your system at least once after adding the cache, before it can be used.}} | ||
=== NixOS === | === NixOS === | ||
Add the cache to <code>substituters</code> and <code>trusted-public-keys</code> inside your system configuration: | Add the cache to <code>substituters</code> and <code>trusted-public-keys</code> inside your system configuration: | ||
{{file| | {{file|3=<nowiki> | ||
nix.settings = { | nix.settings = { | ||
substituters = [ | substituters = [ | ||
"https:// | "https://cache.nixos-cuda.org" | ||
]; | ]; | ||
trusted-public-keys = [ | trusted-public-keys = [ | ||
" | "cache.nixos-cuda.org:74DUi4Ye579gUqzH4ziL9IyiJBlDpMRn9MBN8oNan9M=" | ||
]; | ]; | ||
}; | }; | ||
</nowiki>}} | </nowiki>|name=/etc/nixos/configuration.nix|lang=nix}} | ||
=== Non-NixOS === | === Non-NixOS === | ||
If you | You have to add <code>substituters</code> and <code>trusted-public-keys</code> to <code>/etc/nix/nix.conf</code>: | ||
{{file|3=<nowiki> | |||
trusted-public-keys = cache.nixos-cuda.org:74DUi4Ye579gUqzH4ziL9IyiJBlDpMRn9MBN8oNan9M= | |||
trusted-substituters = https://cache.nixos-cuda.org | |||
trusted-users = root @wheel | |||
</nowiki>|name=/etc/nix/nix.conf|lang=nix}} | |||
If your user is in <code>trusted-users</code>, you can also add the cache in your home directory: | |||
{{file|3=<nowiki> | |||
trusted-public-keys = cache.nixos-cuda.org:74DUi4Ye579gUqzH4ziL9IyiJBlDpMRn9MBN8oNan9M= | |||
trusted-substituters = https://cache.nixos-cuda.org | |||
</nowiki>|name=~/.config/nix/nix.conf|lang=nix}} | |||
== Enabling CUDA In Packages == | |||
By default, software packaged in source code form has CUDA support disabled, because of the unfree license. There are multiple options to solve this. | |||
You can enable builds with CUDA support with a nixpkgs wide configuration. | |||
<syntaxhighlight lang="nix"> | |||
nixpkgs.config.cudaSupport = true; | |||
</syntaxhighlight> | |||
Or you can override individual packages. | |||
<syntaxhighlight lang="nix"> | |||
environment.systemPackages = with pkgs; [ | |||
(mlt.override {config.cudaSupport=true;}) | |||
]; | |||
</syntaxhighlight> | |||
Or you can use binary-packaged versions of CUDA compatible software, such as [https://github.com/edolstra/nix-warez/tree/master/blender blender-bin] for Blender. | |||
{{info|If you will be using <code>cudaSupport</code> in packages, it is recommended you utilize a [[#Setting up CUDA Binary Cache|CUDA binary cache]].}} | |||
Without a [[#Setting up CUDA Binary Cache|CUDA cache]], any CUDA compatible package installed with <code>cudaSupport</code> will be compiled from source. This is because NixOS Foundation does not build (and therefore [https://cache.nixos.org/ cache.nixos.org] does not cache) CUDA packages. | |||
For larger programs like Blender, that process can be very resource-intensive. If you are installing large CUDA-enabled package(s) that either are not cached or you are not using a cache, then (especially on older or weaker hardware) it is recommended to reduce the number of cores and/or jobs that the process will take, to prevent a system freeze from resource limits. This can be done with the <code>--max-jobs</code> / <code>-j</code> and <code>--cores</code> flags, for more details see the [https://github.com/NixOS/nix/blob/master/doc/manual/source/advanced-topics/cores-vs-jobs.md Tuning Cores & Jobs] manual page. | |||
If | If you don't want to deal with the increased time that compilation will take when <code>--max-jobs</code> / <code>-j</code> and <code>--cores</code> are set below maximum, you can also try simply closing other running processes to see if that frees up enough resources for compilation to be successful. | ||
→ For specifics on setting up Blender with CUDA (and OptiX) see: [[Blender#CUDA & OptiX]]. | |||
== Some things to keep in mind when setting up CUDA in NixOS == | == Some things to keep in mind when setting up CUDA in NixOS == | ||
* Some GPUs, like Tesla K80, don't work with the latest drivers, so you must specify them in the option <code>hardware.nvidia.package</code> getting the value from your selected kernel, for example, <code>config.boot.kernelPackages.nvidia_x11_legacy470</code>. You can check which driver version your GPU supports by visiting the [https://www.nvidia.com/Download/index.aspx nvidia site] and checking the driver version. | * Some GPUs, like Tesla K80, don't work with the latest drivers, so you must specify them in the option <code>hardware.nvidia.package</code> getting the value from your selected kernel, for example, <code>config.boot.kernelPackages.nvidia_x11_legacy470</code>. You can check which driver version your GPU supports by visiting the [https://www.nvidia.com/Download/index.aspx nvidia site] and checking the driver version. | ||
* Even with the drivers correctly installed, some software, like Blender, may not see the CUDA GPU. Make sure your system configuration has the option <code>hardware. | * Even with the drivers correctly installed, some software, like Blender, may not see the CUDA GPU. Make sure your system configuration has the option <code>hardware.graphics.enable</code> enabled. | ||
== CUDA under WSL == | == CUDA under WSL == | ||