Adaptive gradient clipping for PyTorch, TensorFlow, and JAX
Project description
smartclip
Adaptive gradient clipping for PyTorch, TensorFlow, and JAX.
SmartClip keeps training stable with adaptive, per-step clipping you can enable in one line of code.
See the full documentation for details of the algorithms, framework usage examples, and logging metrics.
Supported Algorithms
-
AutoClip — Seetharaman et al., 2020 (MLSP). Adaptive percentile-based clipping of gradient norms.
-
Adaptive Gradient Clipping (AGC, NFNets-style) — Brock, De, Smith, 2021. Threshold scales with parameter norm.
-
Z-Score clipping (EMA mean/std) — standard z-score thresholding using streaming mean/variance
zmaxcontrols how aggressive clipping is: threshold ismean + zmax * stdover recent norms. Higherzmaxclips less (more tolerant), lower clips more (more aggressive). Start atzmax=3.0; try2.0–2.5if you see instability from spikes, or3.5–4.0if training seems over‑clipped.
Install
pip install smartclip
Optional extras provide helpers for specific frameworks (install framework wheels first per vendor docs):
pip install "smartclip[torch]" # PyTorch + Lightning/Transformers helpers
pip install "smartclip[tf]" # TensorFlow/Keras helpers
pip install "smartclip[jax]" # JAX/Flax/Optax helpers
Quickstart
PyTorch
import torch
import smartclip as sc
model = MyModel().to("cpu")
opt = torch.optim.AdamW(model.parameters(), lr=3e-4)
with sc.clip_context(model, opt): # Defaults to AutoClip
for x, y in loader:
opt.zero_grad(set_to_none=True)
loss = model(x).loss_fn(y)
loss.backward()
opt.step() # clipped automatically
TensorFlow/Keras
import tensorflow as tf
import smartclip as sc
model = MyModel()
opt = tf.keras.optimizers.Adam(3e-4)
with sc.clip_context(model, opt, clipper=sc.ZScoreClip(zmax=3.0)): # Use the zscore algorithm
model.fit(ds, epochs=5)
JAX/Optax
import jax
import optax
from flax import linen as nn
import smartclip as sc
model = MyModel() # Flax Module
tx = optax.adam(3e-4)
with sc.clip_context(model, tx): # wraps tx.update
grads = jax.grad(loss_fn)(params, batch)
updates, opt_state = tx.update(grads, opt_state, params) # clipped automatically
params = optax.apply_updates(params, updates)
See documentation for full guides for TensorFlow, JAX, Lightning, Keras, and HF Trainer.
Contributing
We welcome issues and pull requests. See contribute.md for developer setup, testing, docs, and release workflows.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file smartclip-0.1.0.tar.gz.
File metadata
- Download URL: smartclip-0.1.0.tar.gz
- Upload date:
- Size: 32.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f4a8b89554c12c3d455a9395f0ab2f3ca2088328f97a3125c16d4b3ff2c1a9d
|
|
| MD5 |
6a8051a879d7a8700373ae7790c809aa
|
|
| BLAKE2b-256 |
ce925cc99d7c707c2638b5677861ff9acc9c03312d0a956d03c6f88381341f5f
|
Provenance
The following attestation bundles were made for smartclip-0.1.0.tar.gz:
Publisher:
release.yml on stefangordon/smartclip
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
smartclip-0.1.0.tar.gz -
Subject digest:
3f4a8b89554c12c3d455a9395f0ab2f3ca2088328f97a3125c16d4b3ff2c1a9d - Sigstore transparency entry: 512249150
- Sigstore integration time:
-
Permalink:
stefangordon/smartclip@bbfa8fea582440ce5ad62cc5d52627440704c980 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/stefangordon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@bbfa8fea582440ce5ad62cc5d52627440704c980 -
Trigger Event:
release
-
Statement type:
File details
Details for the file smartclip-0.1.0-py3-none-any.whl.
File metadata
- Download URL: smartclip-0.1.0-py3-none-any.whl
- Upload date:
- Size: 33.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cc4f46b64e167d32974bb2bacbe66709a13941254b0361271de7e47ea0521e4
|
|
| MD5 |
e2f67c9bcd2c6dc800aae088af6e47f0
|
|
| BLAKE2b-256 |
4093fbd5a6ab08d59524785bdf53f6daa8463459792f9baa9c5f87e03978cccb
|
Provenance
The following attestation bundles were made for smartclip-0.1.0-py3-none-any.whl:
Publisher:
release.yml on stefangordon/smartclip
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
smartclip-0.1.0-py3-none-any.whl -
Subject digest:
0cc4f46b64e167d32974bb2bacbe66709a13941254b0361271de7e47ea0521e4 - Sigstore transparency entry: 512249160
- Sigstore integration time:
-
Permalink:
stefangordon/smartclip@bbfa8fea582440ce5ad62cc5d52627440704c980 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/stefangordon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@bbfa8fea582440ce5ad62cc5d52627440704c980 -
Trigger Event:
release
-
Statement type: