Skip to content
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

python3Packages.jax: add missing cuda libraries #375186

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pkgs/development/python-modules/jax-cuda12-pjrt/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ let
cudaLibPath = lib.makeLibraryPath (
with cudaPackages;
[
(lib.getLib libcublas) # libcublas.so
(lib.getLib cuda_cupti) # libcupti.so
(lib.getLib cuda_cudart) # libcudart.so
(lib.getLib cudnn) # libcudnn.so
(lib.getLib libcublas) # libcublas.so
addDriverRunpath.driverLink # libcuda.so
(lib.getLib libcufft) # libcufft.so
(lib.getLib libcusolver) # libcusolver.so
(lib.getLib libcusparse) # libcusparse.so
(lib.getLib nccl) # libnccl.so
(lib.getLib libnvjitlink) # libnvJitLink.so
(lib.getLib addDriverRunpath.driverLink) # libcuda.so
]
);

Expand Down Expand Up @@ -83,6 +89,8 @@ buildPythonPackage {

pythonImportsCheck = [ "jax_plugins" ];

inherit cudaLibPath;

meta = {
description = "JAX XLA PJRT Plugin for NVIDIA GPUs";
homepage = "https://github.com/jax-ml/jax/tree/main/jax_plugins/cuda";
Expand Down
29 changes: 26 additions & 3 deletions pkgs/development/python-modules/jax-cuda12-plugin/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
jax-cuda12-pjrt,
}:
let
inherit (cudaPackages) cudaVersion;
inherit (jaxlib) version;
inherit (cudaPackages) cudaVersion;
inherit (jax-cuda12-pjrt) cudaLibPath;

getSrcFromPypi =
{
Expand Down Expand Up @@ -94,12 +95,34 @@ buildPythonPackage {
wheelUnpackHook
];

# jax-cuda12-plugin looks for ptxas at runtime, e.g. with a triton kernel.
# Linking into $out is the least bad solution. See
# * https://github.com/NixOS/nixpkgs/pull/164176#discussion_r828801621
# * https://github.com/NixOS/nixpkgs/pull/288829#discussion_r1493852211
# * https://github.com/NixOS/nixpkgs/pull/375186
# for more info.
postInstall = ''
mkdir -p $out/${python.sitePackages}/jax_cuda12_plugin/cuda/bin
ln -s ${lib.getExe' cudaPackages.cuda_nvcc "ptxas"} $out/${python.sitePackages}/jax_cuda12_plugin/cuda/bin
ln -s ${lib.getExe' cudaPackages.cuda_nvcc "nvlink"} $out/${python.sitePackages}/jax_cuda12_plugin/cuda/bin
'';
Comment on lines +104 to +108
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mostly copied from jax-cuda12-pjrt, with the exception of the nvvm folder. I'm not sure where to put it, because I haven't encountered any errors with it missing.

postInstall = ''
mkdir -p $out/${python.sitePackages}/jax_plugins/nvidia/cuda_nvcc/bin
ln -s ${lib.getExe' cudaPackages.cuda_nvcc "ptxas"} $out/${python.sitePackages}/jax_plugins/nvidia/cuda_nvcc/bin/ptxas
ln -s ${lib.getExe' cudaPackages.cuda_nvcc "nvlink"} $out/${python.sitePackages}/jax_plugins/nvidia/cuda_nvcc/bin/nvlink
ln -s ${cudaPackages.cuda_nvcc}/nvvm $out/${python.sitePackages}/jax_plugins/nvidia/cuda_nvcc/nvvm
'';


# jax-cuda12-plugin contains shared libraries that open other shared libraries via dlopen
# and these implicit dependencies are not recognized by ldd or
# autoPatchelfHook. That means we need to sneak them into rpath. This step
# must be done after autoPatchelfHook and the automatic stripping of
# artifacts. autoPatchelfHook runs in postFixup and auto-stripping runs in the
# patchPhase.
preInstallCheck = ''
patchelf --add-rpath "${cudaLibPath}" $out/${python.sitePackages}/jax_cuda12_plugin/*.so
'';

dependencies = [ jax-cuda12-pjrt ];

pythonImportsCheck = [ "jax_cuda12_plugin" ];

# no tests
doCheck = false;
# FIXME: there are no tests, but we need to run preInstallCheck above
doCheck = true;

meta = {
description = "JAX Plugin for CUDA12";
Expand Down
4 changes: 0 additions & 4 deletions pkgs/development/python-modules/jax/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ buildPythonPackage rec {

meta = {
description = "Source-built JAX frontend: differentiate, compile, and transform Numpy code";
longDescription = ''
This is the JAX frontend package, it's meant to be used together with one of the jaxlib implementations,
e.g. `python3Packages.jaxlib`, `python3Packages.jaxlib-bin`, or `python3Packages.jaxlibWithCuda`.
'';
homepage = "https://github.com/google/jax";
license = lib.licenses.asl20;
maintainers = with lib.maintainers; [ samuela ];
Expand Down
11 changes: 8 additions & 3 deletions pkgs/development/python-modules/jax/test-cuda.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ pkgs.writers.writePython3Bin "jax-test-cuda"
}
''
import jax
import jax.numpy as jnp
from jax import random
from jax.experimental import sparse

assert jax.devices()[0].platform == "gpu"
assert jax.devices()[0].platform == "gpu" # libcuda.so

rng = random.PRNGKey(0)
rng = random.key(0) # libcudart.so, libcudnn.so
x = random.normal(rng, (100, 100))
x @ x
x @ x # libcublas.so
jnp.fft.fft(x) # libcufft.so
jnp.linalg.inv(x) # libcusolver.so
sparse.CSR.fromdense(x) @ x # libcusparse.so

print("success!")
''