Simple Gradient Noise Scale (GNS) calculation for PyTorch
Project description
GNS PyTorch
This is the easiest way to calculate GNS (Gradient Noise Scale) for your PyTorch models. No hooks, gradient accumulation, or multi-GPU setup needed. Just pass in your per-example losses and model.
What's GNS?
GNS measures gradient noise in your training. See https://arxiv.org/pdf/1812.06162 and https://openreview.net/forum?id=xINTMAvPQA
Install
pip install gns-pytorch
Usage
Simple usage:
from gns_pytorch import compute_gns
import torch
model = YourModel()
optimizer = torch.optim.Adam(model.parameters())
def training_step(batch):
x, y = batch
logits = model(x)
per_example_losses = torch.nn.functional.cross_entropy(logits, y, reduction='none')
if global_step % 100 == 0:
gns_value = compute_gns(per_example_losses, model)
gns_ema = 0.9 * gns_ema + 0.1 * gns_value
print(f"Current GNS (EMA): {gns_ema}")
loss = per_example_losses.mean()
optimizer.zero_grad()
loss.backward()
optimizer.step()
Adaptive Batch Size Scheduling
With accurate GNS you can schedule your batch size (using gradient accumulation) to always be critical / optimal throughout training, massively boosting convergence and sample efficiency. This is similar to what deepseek-v3 did.
Tips
- Call
compute_gnsevery N steps (like 100+) to avoid overhead - Use an EMA on the GNS values since they are very noisy
- The
param_percentageparam lets you sample a subset of model parameters for faster computation - Enable vmap with
use_vmap=Trueto speed up computation by parallelizing per-example gradients (unfortunately, PyTorch's vmap isn't composable with flex attention and torch.compile yet)
Project details
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 gns_pytorch-0.1.4.tar.gz.
File metadata
- Download URL: gns_pytorch-0.1.4.tar.gz
- Upload date:
- Size: 29.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdb765b957e8b5f67528820eca398bf98c8a8383647ca70cd7f234c85948d391
|
|
| MD5 |
6de07d1414bf5a73bc17f6cabe44ea89
|
|
| BLAKE2b-256 |
3d2cce56bf247a333fa83a96a6d4026f9fd4b71e636fa99b063096f6f3c67a23
|
File details
Details for the file gns_pytorch-0.1.4-py3-none-any.whl.
File metadata
- Download URL: gns_pytorch-0.1.4-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3d6cfc5de3dbe64da3641f228919269c2f568a3672bc2fac540d85737c28d38
|
|
| MD5 |
d224879527a4da1004f61743ea71145f
|
|
| BLAKE2b-256 |
a6b289e7f0c0527af0a58cf701fc92a5b9625ec239702506f2e46de5a955e559
|