jax
Composable transformations of Python+NumPy programs: differentiate, vectorize, JIT to GPU/TPU, and more
Science Score: 54.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
✓Committers with academic emails
40 of 820 committers (4.9%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (13.6%) to scientific vocabulary
Keywords
Keywords from Contributors
Scientific Fields
Repository
Composable transformations of Python+NumPy programs: differentiate, vectorize, JIT to GPU/TPU, and more
Basic Info
- Host: GitHub
- Owner: jax-ml
- License: apache-2.0
- Language: Python
- Default Branch: main
- Homepage: https://docs.jax.dev
- Size: 127 MB
Statistics
- Stars: 33,339
- Watchers: 327
- Forks: 3,146
- Open Issues: 2,274
- Releases: 126
Topics
Metadata Files
README.md
Transformable numerical computing at scale
Transformations | Scaling | Install guide | Change logs | Reference docs
What is JAX?
JAX is a Python library for accelerator-oriented array computation and program transformation, designed for high-performance numerical computing and large-scale machine learning.
JAX can automatically differentiate native
Python and NumPy functions. It can differentiate through loops, branches,
recursion, and closures, and it can take derivatives of derivatives of
derivatives. It supports reverse-mode differentiation (a.k.a. backpropagation)
via jax.grad as well as forward-mode differentiation,
and the two can be composed arbitrarily to any order.
JAX uses XLA
to compile and scale your NumPy programs on TPUs, GPUs, and other hardware accelerators.
You can compile your own pure functions with jax.jit.
Compilation and automatic differentiation can be composed arbitrarily.
Dig a little deeper, and you'll see that JAX is really an extensible system for composable function transformations at scale.
This is a research project, not an official Google product. Expect sharp edges. Please help by trying it out, reporting bugs, and letting us know what you think!
```python import jax import jax.numpy as jnp
def predict(params, inputs): for W, b in params: outputs = jnp.dot(inputs, W) + b inputs = jnp.tanh(outputs) # inputs to the next layer return outputs # no activation on last layer
def loss(params, inputs, targets): preds = predict(params, inputs) return jnp.sum((preds - targets)**2)
gradloss = jax.jit(jax.grad(loss)) # compiled gradient evaluation function perexgrads = jax.jit(jax.vmap(gradloss, inaxes=(None, 0, 0))) # fast per-example grads ```
Contents
Transformations
At its core, JAX is an extensible system for transforming numerical functions.
Here are three: jax.grad, jax.jit, and jax.vmap.
Automatic differentiation with grad
Use jax.grad
to efficiently compute reverse-mode gradients:
```python import jax import jax.numpy as jnp
def tanh(x): y = jnp.exp(-2.0 * x) return (1.0 - y) / (1.0 + y)
gradtanh = jax.grad(tanh) print(gradtanh(1.0))
prints 0.4199743
```
You can differentiate to any order with grad:
```python print(jax.grad(jax.grad(jax.grad(tanh)))(1.0))
prints 0.62162673
```
You're free to use differentiation with Python control flow:
```python def abs_val(x): if x > 0: return x else: return -x
absvalgrad = jax.grad(absval) print(absvalgrad(1.0)) # prints 1.0 print(absvalgrad(-1.0)) # prints -1.0 (absval is re-evaluated) ```
See the JAX Autodiff Cookbook and the reference docs on automatic differentiation for more.
Compilation with jit
Use XLA to compile your functions end-to-end with
jit,
used either as an @jit decorator or as a higher-order function.
```python import jax import jax.numpy as jnp
def slow_f(x): # Element-wise ops see a large benefit from fusion return x * x + x * 2.0
x = jnp.ones((5000, 5000)) fastf = jax.jit(slowf) %timeit -n10 -r3 fastf(x) %timeit -n10 -r3 slowf(x) ```
Using jax.jit constrains the kind of Python control flow
the function can use; see
the tutorial on Control Flow and Logical Operators with JIT
for more.
Auto-vectorization with vmap
vmap maps
a function along array axes.
But instead of just looping over function applications, it pushes the loop down
onto the function’s primitive operations, e.g. turning matrix-vector multiplies into
matrix-matrix multiplies for better performance.
Using vmap can save you from having to carry around batch dimensions in your
code:
```python import jax import jax.numpy as jnp
def l1_distance(x, y): assert x.ndim == y.ndim == 1 # only works on 1D inputs return jnp.sum(jnp.abs(x - y))
def pairwise_distances(dist1D, xs): return jax.vmap(jax.vmap(dist1D, (0, None)), (None, 0))(xs, xs)
xs = jax.random.normal(jax.random.key(0), (100, 3)) dists = pairwisedistances(l1distance, xs) dists.shape # (100, 100) ```
By composing jax.vmap with jax.grad and jax.jit, we can get efficient
Jacobian matrices, or per-example gradients:
python
per_example_grads = jax.jit(jax.vmap(jax.grad(loss), in_axes=(None, 0, 0)))
Scaling
To scale your computations across thousands of devices, you can use any
composition of these:
* Compiler-based automatic parallelization
where you program as if using a single global machine, and the compiler chooses
how to shard data and partition computation (with some user-provided constraints);
* Explicit sharding and automatic partitioning
where you still have a global view but data shardings are
explicit in JAX types, inspectable using jax.typeof;
* Manual per-device programming
where you have a per-device view of data
and computation, and can communicate with explicit collectives.
| Mode | View? | Explicit sharding? | Explicit Collectives? | |---|---|---|---| | Auto | Global | ❌ | ❌ | | Explicit | Global | ✅ | ❌ | | Manual | Per-device | ✅ | ✅ |
```python from jax.sharding import setmesh, AxisType, PartitionSpec as P mesh = jax.makemesh((8,), ('data',), axistypes=(AxisType.Explicit,)) setmesh(mesh)
parameters are sharded for FSDP:
for W, b in params: print(f'{jax.typeof(W)}') # f32[512@data,512] print(f'{jax.typeof(b)}') # f32[512]
shard data for batch parallelism:
inputs, targets = jax.device_put((inputs, targets), P('data'))
evaluate gradients, automatically parallelized!
gradfun = jax.jit(jax.grad(loss)) param_grads = gradfun(params, (inputs, targets)) ```
See the tutorial and advanced guides for more.
Gotchas and sharp bits
See the Gotchas Notebook.
Installation
Supported platforms
| | Linux x8664 | Linux aarch64 | Mac aarch64 | Windows x8664 | Windows WSL2 x86_64 | |------------|--------------|---------------|--------------|----------------|---------------------| | CPU | yes | yes | yes | yes | yes | | NVIDIA GPU | yes | yes | n/a | no | experimental | | Google TPU | yes | n/a | n/a | n/a | n/a | | AMD GPU | yes | no | n/a | no | no | | Apple GPU | n/a | no | experimental | n/a | n/a | | Intel GPU | experimental | n/a | n/a | no | no |
Instructions
| Platform | Instructions |
|-----------------|-----------------------------------------------------------------------------------------------------------------|
| CPU | pip install -U jax |
| NVIDIA GPU | pip install -U "jax[cuda12]" |
| Google TPU | pip install -U "jax[tpu]" |
| AMD GPU (Linux) | Follow AMD's instructions. |
| Mac GPU | Follow Apple's instructions. |
| Intel GPU | Follow Intel's instructions. |
See the documentation for information on alternative installation strategies. These include compiling from source, installing with Docker, using other versions of CUDA, a community-supported conda build, and answers to some frequently-asked questions.
Citing JAX
To cite this repository:
@software{jax2018github,
author = {James Bradbury and Roy Frostig and Peter Hawkins and Matthew James Johnson and Chris Leary and Dougal Maclaurin and George Necula and Adam Paszke and Jake Vander{P}las and Skye Wanderman-{M}ilne and Qiao Zhang},
title = {{JAX}: composable transformations of {P}ython+{N}um{P}y programs},
url = {http://github.com/jax-ml/jax},
version = {0.3.13},
year = {2018},
}
In the above bibtex entry, names are in alphabetical order, the version number is intended to be that from jax/version.py, and the year corresponds to the project's open-source release.
A nascent version of JAX, supporting only automatic differentiation and compilation to XLA, was described in a paper that appeared at SysML 2018. We're currently working on covering JAX's ideas and capabilities in a more comprehensive and up-to-date paper.
Reference documentation
For details about the JAX API, see the reference documentation.
For getting started as a JAX developer, see the developer documentation.
Owner
- Name: jax-ml
- Login: jax-ml
- Kind: organization
- Website: github.com/google/jax
- Repositories: 6
- Profile: https://github.com/jax-ml
miscellaneous libraries and projects relating to JAX
Citation (CITATION.bib)
@software{jax2018github,
author = {James Bradbury and Roy Frostig and Peter Hawkins and Matthew James Johnson and Chris Leary and Dougal Maclaurin and George Necula and Adam Paszke and Jake Vander{P}las and Skye Wanderman-{M}ilne and Qiao Zhang},
title = {{JAX}: composable transformations of {P}ython+{N}um{P}y programs},
url = {http://github.com/jax-ml/jax},
version = {0.3.13},
year = {2018},
}
Committers
Last synced: 8 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Peter Hawkins | p****s@g****m | 2,674 |
| Jake VanderPlas | j****p@g****m | 2,334 |
| Matthew Johnson | m****j@g****m | 2,069 |
| Yash Katariya | y****a@g****m | 1,502 |
| George Necula | g****a@g****m | 1,113 |
| jax authors | g****n@g****m | 737 |
| jax authors | n****y@g****m | 697 |
| Roy Frostig | f****g@g****m | 614 |
| Adam Paszke | a****e@g****m | 610 |
| Skye Wanderman-Milne | s****m@g****m | 495 |
| Sergei Lebedev | s****v@g****m | 464 |
| Sharad Vikram | s****v@g****m | 351 |
| Dan Foreman-Mackey | d****m@g****m | 254 |
| Parker Schuh | p****s@g****m | 170 |
| Jake VanderPlas | v****s@g****m | 170 |
| Benjamin Chetioui | c****n@g****m | 161 |
| Stephan Hoyer | s****r@g****m | 145 |
| Tomás Longeri | t****i@g****m | 143 |
| Jevin Jiang | j****g@g****m | 128 |
| rajasekharporeddy | r****p@g****m | 112 |
| Chris Jones | c****j@g****m | 112 |
| Lena Martens | l****s@g****m | 107 |
| Justin Fu | j****u@g****m | 105 |
| Nitin Srinivasan | s****n@g****m | 98 |
| Qiao Zhang | z****c@g****m | 96 |
| James Bradbury | j****y@g****m | 95 |
| Jean-Baptiste Lespiau | j****u@g****m | 88 |
| Ayaka | a****x@g****m | 87 |
| Dougal Maclaurin | d****m@g****m | 87 |
| Jieying Luo | j****g@g****m | 84 |
| and 790 more... | ||
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 4 months ago
All Time
- Total issues: 1,147
- Total pull requests: 9,130
- Average time to close issues: 9 months
- Average time to close pull requests: 9 days
- Total issue authors: 633
- Total pull request authors: 242
- Average comments per issue: 2.55
- Average comments per pull request: 0.23
- Merged pull requests: 5,279
- Bot issues: 46
- Bot pull requests: 6,558
Past Year
- Issues: 905
- Pull requests: 9,005
- Average time to close issues: 12 days
- Average time to close pull requests: 4 days
- Issue authors: 501
- Pull request authors: 227
- Average comments per issue: 1.29
- Average comments per pull request: 0.21
- Merged pull requests: 5,249
- Bot issues: 46
- Bot pull requests: 6,496
Top Authors
Issue Authors
- carlosgmartin (42)
- PhilipVinc (36)
- copybara-service[bot] (26)
- ayaka14732 (24)
- jakevdp (20)
- github-actions[bot] (18)
- patrick-kidger (16)
- apiqwe (14)
- hawkinsp (13)
- gnecula (13)
- sbodenstein (13)
- vfdev-5 (12)
- crusaderky (11)
- emilyfertig (10)
- mattjj (10)
Pull Request Authors
- copybara-service[bot] (6,446)
- jakevdp (538)
- mattjj (197)
- hawkinsp (183)
- dfm (179)
- gnecula (159)
- dependabot[bot] (112)
- vfdev-5 (66)
- carlosgmartin (61)
- rajasekharporeddy (50)
- Ruturaj4 (47)
- dougalm (39)
- andportnoy (38)
- MichaelHudgins (32)
- froystig (29)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- actions/cache 704facf57e6136b1bc63b828d79edcd491f0ee84 composite
- actions/checkout b4ffde65f46336ab88eb53be808477a3936bae11 composite
- actions/setup-python 0a5c61591373683505ea898e09a3ea4f39ef2b9c composite
- pre-commit/action 646c83fcd040023954eafda54b4db0192ce70507 composite
- styfle/cancel-workflow-action 01ce38bf961b4e243a6342cbade0dbc8ba3f0432 composite
- actions/checkout b4ffde65f46336ab88eb53be808477a3936bae11 composite
- actions/checkout b4ffde65f46336ab88eb53be808477a3936bae11 composite
- actions/setup-python 0a5c61591373683505ea898e09a3ea4f39ef2b9c composite
- actions/checkout b4ffde65f46336ab88eb53be808477a3936bae11 composite
- actions/setup-python 0a5c61591373683505ea898e09a3ea4f39ef2b9c composite
- actions/upload-artifact a8a3f3ad30e3422c9c7b888a15615d19a852ae32 composite
- styfle/cancel-workflow-action 01ce38bf961b4e243a6342cbade0dbc8ba3f0432 composite
- actions/checkout b4ffde65f46336ab88eb53be808477a3936bae11 composite
- actions/setup-python 0a5c61591373683505ea898e09a3ea4f39ef2b9c composite
- actions/upload-artifact a8a3f3ad30e3422c9c7b888a15615d19a852ae32 composite
- styfle/cancel-workflow-action 01ce38bf961b4e243a6342cbade0dbc8ba3f0432 composite
- protobuf *
- tensorboard-plugin-profile *
- tensorflow *
- absl-py * test
- build * test
- cloudpickle * test
- colorama >=0.4.4 test
- flatbuffers * test
- hypothesis * test
- numpy >=1.22 test
- pillow >=9.1.0 test
- portpicker * test
- pytest-xdist * test
- rich * test
- setuptools * test
- wheel * test
- absl-py *
- flatbuffers *
- ipython >=8.8.0
- jupyter-sphinx >=0.3.2
- matplotlib *
- myst-nb >=1.0.0
- numpy *
- pytest *
- pytest-xdist *
- scikit-learn *
- sphinx >=6.0.0
- sphinx-autodoc-typehints *
- sphinx-book-theme >=1.0.1
- sphinx-copybutton >=0.5.0
- sphinx-design *
- sphinx-remove-toctrees *
- flax *
- tensorflow_datasets *
- tensorflow_hub *
- ml_dtypes >=0.2.0
- numpy >=1.22
- scipy >=1.11.1
- scipy >=1.9
- Python *
- Required *
- importlib_metadata >=4.6
- ml_dtypes >=0.2.0
- numpy >=1.26.0
- numpy >=1.22
- numpy >=1.23.2
- opt_einsum *
- required *
- scipy >=1.11.1
- scipy >=1.9