Science Score: 36.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
✓Academic publication links
Links to: arxiv.org -
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (12.2%) to scientific vocabulary
Keywords from Contributors
Repository
JMP is a Mixed Precision library for JAX.
Basic Info
Statistics
- Stars: 208
- Watchers: 5
- Forks: 17
- Open Issues: 4
- Releases: 3
Metadata Files
README.md
Mixed precision training in JAX
Installation | Examples | Policies | Loss scaling | Citing JMP | References
Mixed precision training [0] is a technique that mixes the use of full and half precision floating point numbers during training to reduce the memory bandwidth requirements and improve the computational efficiency of a given model.
This library implements support for mixed precision training in JAX by providing
two key abstractions (mixed precision "policies" and loss scaling). Neural
network libraries (such as Haiku) can integrate with jmp and provide
"Automatic Mixed Precision (AMP)" support (automating or simplifying applying
policies to modules).
All code examples below assume the following:
```python import jax import jax.numpy as jnp import jmp
half = jnp.float16 # On TPU this should be jnp.bfloat16. full = jnp.float32 ```
Installation
JMP is written in pure Python, but depends on C++ code via JAX and NumPy.
Because JAX installation is different depending on your CUDA version, JMP does
not list JAX as a dependency in requirements.txt.
First, follow these instructions to install JAX with the relevant accelerator support.
Then, install JMP using pip:
bash
$ pip install git+https://github.com/deepmind/jmp
Examples
You can find a fully worked JMP example in Haiku which shows how to use mixed f32/f16 precision to halve training time on GPU and mixed f32/bf16 to reduce training time on TPU by a third.
Policies
A mixed precision policy encapsulates the configuration in a mixed precision experiment.
```python
Our policy specifies that we will store parameters in full precision but will
compute and return output in half precision.
mypolicy = jmp.Policy(computedtype=half, paramdtype=full, outputdtype=half) ```
The policy object can be used to cast pytrees:
```python def layer(params, x): params, x = mypolicy.casttocompute((params, x)) w, b = params y = x @ w + b return mypolicy.casttooutput(y)
params = {"w": jnp.ones([], dtype=mypolicy.paramdtype)} y = layer(params, x) assert y.dtype == half ```
You can replace the output type of a given policy:
python
my_policy = my_policy.with_output_dtype(full)
You can also define a policy via a string, which may be useful for specifying a policy as a command-line argument or as a hyperparameter to your experiment:
python
my_policy = jmp.get_policy("params=float32,compute=float16,output=float32")
float16 = jmp.get_policy("float16") # Everything in f16.
half = jmp.get_policy("half") # Everything in half (f16 or bf16).
Loss scaling
When training with reduced precision, consider whether gradients will need to be
shifted into the representable range of the format that you are using. This is
particularly important when training with float16 and less important for
bfloat16. See the NVIDIA mixed precision user guide [1] for more details.
The easiest way to shift gradients is with loss scaling, which scales your loss
and gradients by S and 1/S respectively.
```python def mylossfn(params, lossscale: jmp.LossScale, ...): loss = ... # You should apply regularization etc before scaling. loss = lossscale.scale(loss) return loss
def trainstep(params, lossscale: jmp.LossScale, ...): grads = jax.grad(mylossfn)(...) grads = lossscale.unscale(grads) # You should put gradient clipping etc after unscaling. params = applyoptimizer(params, grads) return params
lossscale = jmp.StaticLossScale(2 ** 15) for _ in range(numsteps): params = trainstep(params, lossscale, ...) ```
The appropriate value for S depends on your model, loss, batch size and
potentially other factors. You can determine this with trial and error. As a
rule of thumb you want the largest value of S that does not introduce overflow
during backprop. NVIDIA [1] recommend computing statistics about the gradients
of your model (in full precision) and picking S such that its product with the
maximum norm of your gradients is below 65,504.
We provide a dynamic loss scale, which adjusts the loss scale periodically
during training to find the largest value for S that produces finite
gradients. This is more convenient and robust compared with picking a static
loss scale, but has a small performance impact (between 1 and 5%).
```python def mylossfn(params, lossscale: jmp.LossScale, ...): loss = ... # You should apply regularization etc before scaling. loss = lossscale.scale(loss) return loss
def trainstep(params, lossscale: jmp.LossScale, ...): grads = jax.grad(mylossfn)(...) grads = loss_scale.unscale(grads) # You should put gradient clipping etc after unscaling.
# You definitely want to skip non-finite updates with the dynamic loss scale, # but you might also want to consider skipping them when using a static loss # scale if you experience NaN's when training. skipnonfiniteupdates = isinstance(loss_scale, jmp.DynamicLossScale)
if skipnonfiniteupdates: gradsfinite = jmp.allfinite(grads) # Adjust our loss scale depending on whether gradients were finite. The # loss scale will be periodically increased if gradients remain finite and # will be decreased if not. lossscale = lossscale.adjust(gradsfinite) # Only apply our optimizer if grads are finite, if any element of any # gradient is non-finite the whole update is discarded. params = jmp.selecttree(gradsfinite, applyoptimizer(params, grads), params) else: # With static or no loss scaling just apply our optimizer. params = apply_optimizer(params, grads)
# Since our loss scale is dynamic we need to return the new value from
# each step. All loss scales are PyTrees.
return params, loss_scale
lossscale = jmp.DynamicLossScale(jmp.halfdtype()(2 ** 15)) for _ in range(numsteps): params, lossscale = trainstep(params, lossscale, ...) ```
In general using a static loss scale should offer the best speed, but we have optimized dynamic loss scaling to make it competitive. We recommend you start with dynamic loss scaling and move to static loss scaling if performance is an issue.
We finally offer a no-op loss scale which you can use as a drop in replacement.
It does nothing (apart from implement the jmp.LossScale API):
python
loss_scale = jmp.NoOpLossScale()
assert loss is loss_scale.scale(loss)
assert grads is loss_scale.unscale(grads)
assert loss_scale is loss_scale.adjust(grads_finite)
assert loss_scale.loss_scale == 1
Citing JMP
This repository is part of the DeepMind JAX Ecosystem, to cite JMP please use the DeepMind JAX Ecosystem citation.
References
[0] Paulius Micikevicius, Sharan Narang, Jonah Alben, Gregory Diamos, Erich Elsen, David Garcia, Boris Ginsburg, Michael Houston, Oleksii Kuchaiev, Ganesh Venkatesh, Hao Wu: "Mixed Precision Training", 2017; arXiv:1710.03740 https://arxiv.org/abs/1710.03740.
[1] "Training With Mixed Precision :: NVIDIA Deep Learning Performance Documentation". Docs.Nvidia.Com, 2020, https://docs.nvidia.com/deeplearning/performance/mixed-precision-training/.
Owner
- Name: Google DeepMind
- Login: google-deepmind
- Kind: organization
- Website: https://www.deepmind.com/
- Repositories: 245
- Profile: https://github.com/google-deepmind
GitHub Events
Total
- Watch event: 16
- Delete event: 1
- Issue comment event: 1
- Push event: 7
- Pull request event: 3
- Fork event: 1
- Create event: 2
Last Year
- Watch event: 16
- Delete event: 1
- Issue comment event: 1
- Push event: 7
- Pull request event: 3
- Fork event: 1
- Create event: 2
Committers
Last synced: about 1 year ago
Top Committers
| Name | Commits | |
|---|---|---|
| Tom Hennigan | t****n@g****m | 22 |
| Nicolas Forstner | n****r@g****m | 4 |
| JMP Contributor | n****y@g****m | 3 |
| Peter Hawkins | p****s@g****m | 2 |
| Neil Girdhar | m****k@g****m | 2 |
| Loren Maggiore | l****o@g****m | 2 |
| Ron Shapiro | r****o@g****m | 1 |
| Richard Levasseur | r****r@g****m | 1 |
| Oleh Prypin | o****n@g****m | 1 |
| Mario Geiger | g****o@g****m | 1 |
| Anurag Kumar | m****8@g****m | 1 |
| Jake VanderPlas | v****s@g****m | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 11 months ago
All Time
- Total issues: 10
- Total pull requests: 42
- Average time to close issues: about 1 month
- Average time to close pull requests: 6 days
- Total issue authors: 10
- Total pull request authors: 7
- Average comments per issue: 1.5
- Average comments per pull request: 0.24
- Merged pull requests: 30
- Bot issues: 0
- Bot pull requests: 35
Past Year
- Issues: 0
- Pull requests: 3
- Average time to close issues: N/A
- Average time to close pull requests: 3 minutes
- Issue authors: 0
- Pull request authors: 1
- Average comments per issue: 0
- Average comments per pull request: 0.0
- Merged pull requests: 2
- Bot issues: 0
- Bot pull requests: 3
Top Authors
Issue Authors
- mariogeiger (1)
- psmaragdis (1)
- yardenas (1)
- danijar (1)
- ghost (1)
- JSchuurmans (1)
- llCurious (1)
- jiagaoxiang (1)
- joeryjoery (1)
- ostueker (1)
Pull Request Authors
- copybara-service[bot] (37)
- NeilGirdhar (2)
- nlsfnr (1)
- tomhennigan (1)
- mariogeiger (1)
- cyprienc (1)
- anukaal (1)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- jax >=0.2.20
- jaxlib >=0.1.71
- pytest >=6.2.1 test
- dataclasses >=0.7
- numpy >=1.19.5
- actions/checkout v2 composite
- actions/setup-python v1 composite
- actions/checkout v2 composite
- actions/setup-python v1 composite