1{ lib
2, absl-py
3, blas
4, buildPythonPackage
5, etils
6, fetchFromGitHub
7, jaxlib
8, jaxlib-bin
9, lapack
10, matplotlib
11, numpy
12, opt-einsum
13, pytestCheckHook
14, pytest-xdist
15, pythonOlder
16, scipy
17, stdenv
18, typing-extensions
19}:
20
21let
22 usingMKL = blas.implementation == "mkl" || lapack.implementation == "mkl";
23 # jaxlib is broken on aarch64-* as of 2023-03-05, but the binary wheels work
24 # fine. jaxlib is only used in the checkPhase, so switching backends does not
25 # impact package behavior. Get rid of this once jaxlib is fixed on aarch64-*.
26 jaxlib' = if jaxlib.meta.broken then jaxlib-bin else jaxlib;
27in
28buildPythonPackage rec {
29 pname = "jax";
30 version = "0.4.5";
31 format = "setuptools";
32
33 disabled = pythonOlder "3.7";
34
35 src = fetchFromGitHub {
36 owner = "google";
37 repo = pname;
38 # google/jax contains tags for jax and jaxlib. Only use jax tags!
39 rev = "refs/tags/${pname}-v${version}";
40 hash = "sha256-UJzX8zP3qaEUIV5hPJhiGiLJO7k8p962MHWxIHDY1ZA=";
41 };
42
43 # jaxlib is _not_ included in propagatedBuildInputs because there are
44 # different versions of jaxlib depending on the desired target hardware. The
45 # JAX project ships separate wheels for CPU, GPU, and TPU.
46 propagatedBuildInputs = [
47 absl-py
48 etils
49 numpy
50 opt-einsum
51 scipy
52 typing-extensions
53 ] ++ etils.optional-dependencies.epath;
54
55 nativeCheckInputs = [
56 jaxlib'
57 matplotlib
58 pytestCheckHook
59 pytest-xdist
60 ];
61
62 # high parallelism will result in the tests getting stuck
63 dontUsePytestXdist = true;
64
65 # NOTE: Don't run the tests in the expiremental directory as they require flax
66 # which creates a circular dependency. See https://discourse.nixos.org/t/how-to-nix-ify-python-packages-with-circular-dependencies/14648/2.
67 # Not a big deal, this is how the JAX docs suggest running the test suite
68 # anyhow.
69 pytestFlagsArray = [
70 "--numprocesses=4"
71 "-W ignore::DeprecationWarning"
72 "tests/"
73 ];
74
75 disabledTests = [
76 # Exceeds tolerance when the machine is busy
77 "test_custom_linear_solve_aux"
78 # UserWarning: Explicitly requested dtype <class 'numpy.float64'>
79 # requested in astype is not available, and will be truncated to
80 # dtype float32. (With numpy 1.24)
81 "testKde3"
82 "testKde5"
83 "testKde6"
84 ] ++ lib.optionals usingMKL [
85 # See
86 # * https://github.com/google/jax/issues/9705
87 # * https://discourse.nixos.org/t/getting-different-results-for-the-same-build-on-two-equally-configured-machines/17921
88 # * https://github.com/NixOS/nixpkgs/issues/161960
89 "test_custom_linear_solve_cholesky"
90 "test_custom_root_with_aux"
91 "testEigvalsGrad_shape"
92 ] ++ lib.optionals stdenv.isAarch64 [
93 # See https://github.com/google/jax/issues/14793.
94 "test_for_loop_fixpoint_correctly_identifies_loop_varying_residuals_unrolled_for_loop"
95 "testQdwhWithRandomMatrix3"
96 "testScanGrad_jit_scan"
97 ];
98
99 # See https://github.com/google/jax/issues/11722. This is a temporary fix in
100 # order to unblock etils, and upgrading jax/jaxlib to the latest version. See
101 # https://github.com/NixOS/nixpkgs/issues/183173#issuecomment-1204074993.
102 disabledTestPaths = [
103 "tests/api_test.py"
104 "tests/core_test.py"
105 "tests/lax_numpy_indexing_test.py"
106 "tests/lax_numpy_test.py"
107 "tests/nn_test.py"
108 "tests/random_test.py"
109 "tests/sparse_test.py"
110 ] ++ lib.optionals (stdenv.isDarwin && stdenv.isAarch64) [
111 # RuntimeWarning: invalid value encountered in cast
112 "tests/lax_test.py"
113 ];
114
115 # As of 0.3.22, `import jax` does not work without jaxlib being installed.
116 pythonImportsCheck = [ ];
117
118 meta = with lib; {
119 description = "Differentiate, compile, and transform Numpy code";
120 homepage = "https://github.com/google/jax";
121 license = licenses.asl20;
122 maintainers = with maintainers; [ samuela ];
123 };
124}