-
-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NVIDIA acceleration how-to documentation #586
Draft
shikanime
wants to merge
3
commits into
nix-community:main
Choose a base branch
from
shikanime:pr586
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# How to use NVIDIA GPU in Docker | ||
|
||
WSL supports NVIDIA-based GPU acceleration, making it ideal for many use cases | ||
including machine learning or general compute acceleration. Since Docker version | ||
25, the use of `virtualisation.docker.enableNvidia` has been deprecated in | ||
favour of a more standard specification called Container Device Interface. | ||
|
||
1. Enable the NVIDIA Container Toolkit and disable the mounting of executables, | ||
as this will cause an problem related to missing libraries. | ||
|
||
```nix | ||
hardware.nvidia-container-toolkit = { | ||
enable = true; | ||
mount-nvidia-executables = false; | ||
}; | ||
``` | ||
|
||
2. The Docker daemon doesn't have the CDI feature enabled by default. | ||
|
||
```nix | ||
virtualisation.docker = { | ||
enable = true; | ||
daemon.settings.features.cdi = true; | ||
}; | ||
``` | ||
|
||
3. Test the installation by running the following command. | ||
|
||
```shell | ||
docker run --rm --device nvidia.com/gpu=all ubuntu nvidia-smi | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# How to use NVIDIA acceleration with nix-ld | ||
|
||
By default, the NixOS WSL configuration provides a symlink farm from | ||
/usr/lib/wsl/lib to the OpenGL packages. However, if you want to use the GPU | ||
with a non-NixOS compiled program via nix-ld (e.g. nvidia-smi), you also need to | ||
add the symlinks to the to the nix-ld libraries. | ||
|
||
```nix | ||
let | ||
wsl-lib = pkgs.runCommand "wsl-lib" { } '' | ||
mkdir -p "$out/lib" | ||
# # We can't just symlink the lib directory, because it will break merging with other drivers that provide the same directory | ||
ln -s /usr/lib/wsl/lib/libcudadebugger.so.1 "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libcuda.so "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libcuda.so.1 "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libcuda.so.1.1 "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libd3d12core.so "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libd3d12.so "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libdxcore.so "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libnvcuvid.so "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libnvcuvid.so.1 "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libnvdxdlkernels.so "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libnvidia-encode.so "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libnvidia-encode.so.1 "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libnvidia-ml.so.1 "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libnvidia-opticalflow.so "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libnvidia-opticalflow.so.1 "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libnvoptix.so.1 "$out/lib" | ||
ln -s /usr/lib/wsl/lib/libnvwgf2umx.so "$out/lib" | ||
ln -s /usr/lib/wsl/lib/nvidia-smi "$out/lib" | ||
''; | ||
in | ||
{ | ||
programs.nix-ld = { | ||
enable = true; | ||
libraries = [ wsl-lib ]; | ||
}; | ||
} | ||
``` | ||
|
||
Test the installation by running the following command: | ||
|
||
```shell | ||
/usr/lib/wsl/lib/nvidia-smi | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is literally a copy of
NixOS-WSL/modules/wsl-distro.nix
Lines 84 to 105 in adb6bc4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's the same, we could make it more dynamic with a for loop or depending on the API design of this project, add it as an option.
Update: So far, build isolation doesn't seem to allow us to dynamically shell for each for the shared library in an asolute path, I don't know how the home-manager managed to make it work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the
ldconfig
option help non-Nix binaries find the WSL drivers?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is different, there are numerous cases where the executable we want to work with comes from outside the nix store, so nix-ld is needed. This example shows how to use the GPU with it.