1# For the moment we only support the CPU and GPU backends of jaxlib. The TPU 2# backend will require some additional work. Those wheels are located here: 3# https://storage.googleapis.com/jax-releases/libtpu_releases.html. 4 5# For future reference, the easiest way to test the GPU backend is to run 6# NIX_PATH=.. nix-shell -p python3 python3Packages.jax "python3Packages.jaxlib-bin.override { cudaSupport = true; }" 7# export XLA_FLAGS=--xla_gpu_force_compilation_parallelism=1 8# python -c "from jax.lib import xla_bridge; assert xla_bridge.get_backend().platform == 'gpu'" 9# python -c "from jax import random; random.PRNGKey(0)" 10# python -c "from jax import random; x = random.normal(random.PRNGKey(0), (100, 100)); x @ x" 11# There's no convenient way to test the GPU backend in the derivation since the 12# nix build environment blocks access to the GPU. See also: 13# * https://github.com/google/jax/issues/971#issuecomment-508216439 14# * https://github.com/google/jax/issues/5723#issuecomment-913038780 15 16{ absl-py 17, addOpenGLRunpath 18, autoPatchelfHook 19, buildPythonPackage 20, config 21, cudnn ? cudaPackages.cudnn 22, fetchurl 23, flatbuffers 24, isPy39 25, lib 26, python 27, scipy 28, stdenv 29 # Options: 30, cudaSupport ? config.cudaSupport or false 31, cudaPackages ? {} 32}: 33 34let 35 inherit (cudaPackages) cudatoolkit cudnn; 36in 37 38assert cudaSupport -> lib.versionAtLeast cudatoolkit.version "11.1"; 39assert cudaSupport -> lib.versionAtLeast cudnn.version "8.2"; 40 41let 42 version = "0.3.22"; 43 44 pythonVersion = python.pythonVersion; 45 46 # Find new releases at https://storage.googleapis.com/jax-releases/jax_releases.html. 47 # When upgrading, you can get these hashes from prefetch.sh. See 48 # https://github.com/google/jax/issues/12879 as to why this specific URL is 49 # the correct index. 50 cpuSrcs = { 51 "x86_64-linux" = fetchurl { 52 url = "https://storage.googleapis.com/jax-releases/nocuda/jaxlib-${version}-cp310-cp310-manylinux2014_x86_64.whl"; 53 hash = "sha256-w2wo0jk+1BdEkNwfSZRQbebdI4Ac8Kgn0MB0cIMcWU4="; 54 }; 55 "aarch64-darwin" = fetchurl { 56 url = "https://storage.googleapis.com/jax-releases/mac/jaxlib-${version}-cp310-cp310-macosx_11_0_arm64.whl"; 57 hash = "sha256-7Ir55ZhBkccqfoa56WVBF8QwFAC2ws4KFHDkfVw6zm0="; 58 }; 59 }; 60 61 gpuSrc = fetchurl { 62 url = "https://storage.googleapis.com/jax-releases/cuda11/jaxlib-${version}+cuda11.cudnn82-cp310-cp310-manylinux2014_x86_64.whl"; 63 hash = "sha256-rabU62p4fF7Tu/6t8LNYZdf6YO06jGry/JtyFZeamCs="; 64 }; 65in 66buildPythonPackage rec { 67 pname = "jaxlib"; 68 inherit version; 69 format = "wheel"; 70 71 # At the time of writing (2022-10-19), there are releases for <=3.10. 72 # Supporting all of them is a pain, so we focus on 3.10, the current nixpkgs 73 # python version. 74 disabled = !(pythonVersion == "3.10"); 75 76 src = if !cudaSupport then cpuSrcs."${stdenv.hostPlatform.system}" else gpuSrc; 77 78 # Prebuilt wheels are dynamically linked against things that nix can't find. 79 # Run `autoPatchelfHook` to automagically fix them. 80 nativeBuildInputs = lib.optionals cudaSupport [ autoPatchelfHook addOpenGLRunpath ]; 81 # Dynamic link dependencies 82 buildInputs = [ stdenv.cc.cc ]; 83 84 # jaxlib contains shared libraries that open other shared libraries via dlopen 85 # and these implicit dependencies are not recognized by ldd or 86 # autoPatchelfHook. That means we need to sneak them into rpath. This step 87 # must be done after autoPatchelfHook and the automatic stripping of 88 # artifacts. autoPatchelfHook runs in postFixup and auto-stripping runs in the 89 # patchPhase. Dependencies: 90 # * libcudart.so.11.0 -> cudatoolkit_11.lib 91 # * libcublas.so.11 -> cudatoolkit_11 92 # * libcuda.so.1 -> opengl driver in /run/opengl-driver/lib 93 preInstallCheck = lib.optional cudaSupport '' 94 shopt -s globstar 95 96 addOpenGLRunpath $out/**/*.so 97 98 for file in $out/**/*.so; do 99 rpath=$(patchelf --print-rpath $file) 100 # For some reason `makeLibraryPath` on `cudatoolkit_11` maps to 101 # <cudatoolkit_11.lib>/lib which is different from <cudatoolkit_11>/lib. 102 patchelf --set-rpath "$rpath:${cudatoolkit}/lib:${lib.makeLibraryPath [ cudatoolkit.lib cudnn ]}" $file 103 done 104 ''; 105 106 propagatedBuildInputs = [ absl-py flatbuffers scipy ]; 107 108 # Note that cudatoolkit is snecessary since jaxlib looks for "ptxas" in $PATH. 109 # See https://github.com/NixOS/nixpkgs/pull/164176#discussion_r828801621 for 110 # more info. 111 postInstall = lib.optional cudaSupport '' 112 mkdir -p $out/bin 113 ln -s ${cudatoolkit}/bin/ptxas $out/bin/ptxas 114 ''; 115 116 pythonImportsCheck = [ "jaxlib" ]; 117 118 meta = with lib; { 119 description = "XLA library for JAX"; 120 homepage = "https://github.com/google/jax"; 121 sourceProvenance = with sourceTypes; [ binaryNativeCode ]; 122 license = licenses.asl20; 123 maintainers = with maintainers; [ samuela ]; 124 platforms = [ "aarch64-darwin" "x86_64-linux" ]; 125 }; 126}