2021-12-30 13:39:12 +00:00
|
|
|
# For the moment we only support the CPU and GPU backends of jaxlib. The TPU
|
|
|
|
# backend will require some additional work. Those wheels are located here:
|
|
|
|
# https://storage.googleapis.com/jax-releases/libtpu_releases.html.
|
|
|
|
|
|
|
|
# For future reference, the easiest way to test the GPU backend is to run
|
2022-10-30 15:09:59 +00:00
|
|
|
# NIX_PATH=.. nix-shell -p python3 python3Packages.jax "python3Packages.jaxlib-bin.override { cudaSupport = true; }"
|
2021-12-30 13:39:12 +00:00
|
|
|
# export XLA_FLAGS=--xla_gpu_force_compilation_parallelism=1
|
|
|
|
# python -c "from jax.lib import xla_bridge; assert xla_bridge.get_backend().platform == 'gpu'"
|
|
|
|
# python -c "from jax import random; random.PRNGKey(0)"
|
|
|
|
# python -c "from jax import random; x = random.normal(random.PRNGKey(0), (100, 100)); x @ x"
|
|
|
|
# There's no convenient way to test the GPU backend in the derivation since the
|
|
|
|
# nix build environment blocks access to the GPU. See also:
|
|
|
|
# * https://github.com/google/jax/issues/971#issuecomment-508216439
|
|
|
|
# * https://github.com/google/jax/issues/5723#issuecomment-913038780
|
|
|
|
|
2022-02-20 05:27:41 +00:00
|
|
|
{ absl-py
|
|
|
|
, addOpenGLRunpath
|
|
|
|
, autoPatchelfHook
|
|
|
|
, buildPythonPackage
|
|
|
|
, config
|
2023-08-10 07:59:29 +00:00
|
|
|
, fetchPypi
|
2022-02-20 05:27:41 +00:00
|
|
|
, fetchurl
|
|
|
|
, flatbuffers
|
2023-08-10 07:59:29 +00:00
|
|
|
, jaxlib-build
|
2022-02-20 05:27:41 +00:00
|
|
|
, lib
|
2023-08-10 07:59:29 +00:00
|
|
|
, ml-dtypes
|
2022-03-10 19:12:11 +00:00
|
|
|
, python
|
2022-02-20 05:27:41 +00:00
|
|
|
, scipy
|
|
|
|
, stdenv
|
|
|
|
# Options:
|
2023-08-04 22:07:22 +00:00
|
|
|
, cudaSupport ? config.cudaSupport
|
2022-04-15 01:41:22 +00:00
|
|
|
, cudaPackages ? {}
|
2021-12-30 13:39:12 +00:00
|
|
|
}:
|
|
|
|
|
2022-04-15 01:41:22 +00:00
|
|
|
let
|
|
|
|
inherit (cudaPackages) cudatoolkit cudnn;
|
|
|
|
in
|
|
|
|
|
2023-08-10 07:59:29 +00:00
|
|
|
assert cudaSupport -> lib.versionAtLeast cudatoolkit.version "11.1" && lib.versionAtLeast cudnn.version "8.2" && stdenv.isLinux;
|
2021-12-30 13:39:12 +00:00
|
|
|
|
|
|
|
let
|
2023-10-09 19:29:22 +00:00
|
|
|
version = "0.4.18";
|
2023-08-10 07:59:29 +00:00
|
|
|
|
|
|
|
inherit (python) pythonVersion;
|
|
|
|
|
|
|
|
# As of 2023-06-06, google/jax upstream is no longer publishing CPU-only wheels to their GCS bucket. Instead the
|
|
|
|
# official instructions recommend installing CPU-only versions via PyPI.
|
|
|
|
cpuSrcs =
|
|
|
|
let
|
|
|
|
getSrcFromPypi = { platform, hash }: fetchPypi {
|
|
|
|
inherit version platform hash;
|
|
|
|
pname = "jaxlib";
|
|
|
|
format = "wheel";
|
|
|
|
# See the `disabled` attr comment below.
|
|
|
|
dist = "cp310";
|
|
|
|
python = "cp310";
|
|
|
|
abi = "cp310";
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
"x86_64-linux" = getSrcFromPypi {
|
|
|
|
platform = "manylinux2014_x86_64";
|
2023-10-09 19:29:22 +00:00
|
|
|
hash = "sha256-MpNomovvSVx4N6gsowOLksTyEgTK261vSXMGxYqlVOE=";
|
2023-08-10 07:59:29 +00:00
|
|
|
};
|
|
|
|
"aarch64-darwin" = getSrcFromPypi {
|
|
|
|
platform = "macosx_11_0_arm64";
|
2023-10-09 19:29:22 +00:00
|
|
|
hash = "sha256-if/5O5DQVHFdsLw9O1creZBx5j8ftE7fsWMMX1NjHP0=";
|
2023-08-10 07:59:29 +00:00
|
|
|
};
|
|
|
|
"x86_64-darwin" = getSrcFromPypi {
|
|
|
|
platform = "macosx_10_14_x86_64";
|
2023-10-09 19:29:22 +00:00
|
|
|
hash = "sha256-4NeHA/0SGdmHXyDGxpK7oJc7dE1meR4LPjzbIwxloqU=";
|
2023-08-10 07:59:29 +00:00
|
|
|
};
|
|
|
|
};
|
2021-12-30 13:39:12 +00:00
|
|
|
|
|
|
|
|
2022-10-30 15:09:59 +00:00
|
|
|
# Find new releases at https://storage.googleapis.com/jax-releases/jax_releases.html.
|
|
|
|
# When upgrading, you can get these hashes from prefetch.sh. See
|
2023-08-10 07:59:29 +00:00
|
|
|
# https://github.com/google/jax/issues/12879 as to why this specific URL is the correct index.
|
2022-10-30 15:09:59 +00:00
|
|
|
gpuSrc = fetchurl {
|
2023-10-09 19:29:22 +00:00
|
|
|
url = "https://storage.googleapis.com/jax-releases/cuda12/jaxlib-${version}+cuda12.cudnn89-cp310-cp310-manylinux2014_x86_64.whl";
|
|
|
|
hash = "sha256-p6BNvhhRzVDQdpEoIRau5JovC+eDjlW3bXrahtsGvmI=";
|
2022-03-10 19:12:11 +00:00
|
|
|
};
|
2023-08-10 07:59:29 +00:00
|
|
|
|
2022-03-10 19:12:11 +00:00
|
|
|
in
|
2023-08-10 07:59:29 +00:00
|
|
|
buildPythonPackage {
|
2022-03-10 19:12:11 +00:00
|
|
|
pname = "jaxlib";
|
|
|
|
inherit version;
|
|
|
|
format = "wheel";
|
|
|
|
|
2022-10-30 15:09:59 +00:00
|
|
|
disabled = !(pythonVersion == "3.10");
|
2022-03-10 19:12:11 +00:00
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
# See https://discourse.nixos.org/t/ofborg-does-not-respect-meta-platforms/27019/6.
|
|
|
|
src =
|
|
|
|
if !cudaSupport then
|
|
|
|
(
|
|
|
|
cpuSrcs."${stdenv.hostPlatform.system}"
|
|
|
|
or (throw "jaxlib-bin is not supported on ${stdenv.hostPlatform.system}")
|
|
|
|
) else gpuSrc;
|
2021-12-30 13:39:12 +00:00
|
|
|
|
|
|
|
# Prebuilt wheels are dynamically linked against things that nix can't find.
|
|
|
|
# Run `autoPatchelfHook` to automagically fix them.
|
2023-08-10 07:59:29 +00:00
|
|
|
nativeBuildInputs = lib.optionals stdenv.isLinux [ autoPatchelfHook ]
|
|
|
|
++ lib.optionals cudaSupport [ addOpenGLRunpath ];
|
2021-12-30 13:39:12 +00:00
|
|
|
# Dynamic link dependencies
|
2023-08-10 07:59:29 +00:00
|
|
|
buildInputs = [ stdenv.cc.cc.lib ];
|
2021-12-30 13:39:12 +00:00
|
|
|
|
|
|
|
# jaxlib 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. Dependencies:
|
|
|
|
# * libcudart.so.11.0 -> cudatoolkit_11.lib
|
|
|
|
# * libcublas.so.11 -> cudatoolkit_11
|
|
|
|
# * libcuda.so.1 -> opengl driver in /run/opengl-driver/lib
|
|
|
|
preInstallCheck = lib.optional cudaSupport ''
|
|
|
|
shopt -s globstar
|
|
|
|
|
|
|
|
addOpenGLRunpath $out/**/*.so
|
|
|
|
|
|
|
|
for file in $out/**/*.so; do
|
|
|
|
rpath=$(patchelf --print-rpath $file)
|
|
|
|
# For some reason `makeLibraryPath` on `cudatoolkit_11` maps to
|
|
|
|
# <cudatoolkit_11.lib>/lib which is different from <cudatoolkit_11>/lib.
|
2022-04-15 01:41:22 +00:00
|
|
|
patchelf --set-rpath "$rpath:${cudatoolkit}/lib:${lib.makeLibraryPath [ cudatoolkit.lib cudnn ]}" $file
|
2021-12-30 13:39:12 +00:00
|
|
|
done
|
|
|
|
'';
|
|
|
|
|
2023-08-10 07:59:29 +00:00
|
|
|
propagatedBuildInputs = [
|
|
|
|
absl-py
|
|
|
|
flatbuffers
|
|
|
|
ml-dtypes
|
|
|
|
scipy
|
|
|
|
];
|
2022-03-30 09:31:56 +00:00
|
|
|
|
|
|
|
# Note that cudatoolkit is snecessary since jaxlib looks for "ptxas" in $PATH.
|
|
|
|
# See https://github.com/NixOS/nixpkgs/pull/164176#discussion_r828801621 for
|
|
|
|
# more info.
|
|
|
|
postInstall = lib.optional cudaSupport ''
|
|
|
|
mkdir -p $out/bin
|
2022-04-15 01:41:22 +00:00
|
|
|
ln -s ${cudatoolkit}/bin/ptxas $out/bin/ptxas
|
2022-03-30 09:31:56 +00:00
|
|
|
'';
|
2021-12-30 13:39:12 +00:00
|
|
|
|
2023-08-10 07:59:29 +00:00
|
|
|
inherit (jaxlib-build) pythonImportsCheck;
|
2021-12-30 13:39:12 +00:00
|
|
|
|
|
|
|
meta = with lib; {
|
|
|
|
description = "XLA library for JAX";
|
2022-02-20 05:27:41 +00:00
|
|
|
homepage = "https://github.com/google/jax";
|
2022-06-26 10:26:21 +00:00
|
|
|
sourceProvenance = with sourceTypes; [ binaryNativeCode ];
|
2022-02-20 05:27:41 +00:00
|
|
|
license = licenses.asl20;
|
2021-12-30 13:39:12 +00:00
|
|
|
maintainers = with maintainers; [ samuela ];
|
2022-11-27 09:42:12 +00:00
|
|
|
platforms = [ "aarch64-darwin" "x86_64-linux" "x86_64-darwin" ];
|
2021-12-30 13:39:12 +00:00
|
|
|
};
|
|
|
|
}
|