Skip to main content

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_gns every N steps (like 100+) to avoid overhead
  • Use an EMA on the GNS values since they are very noisy
  • The param_percentage param lets you sample a subset of model parameters for faster computation
  • Enable vmap with use_vmap=True to speed up computation by parallelizing per-example gradients (unfortunately, PyTorch's vmap isn't composable with flex attention and torch.compile yet)
  • GNS directly approximates critical batch size. For example, if GNS logger shows 64 and your global batch size is 32, you should double your gradient accumulation steps

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

gns_pytorch-0.1.5.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gns_pytorch-0.1.5-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file gns_pytorch-0.1.5.tar.gz.

File metadata

  • Download URL: gns_pytorch-0.1.5.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.6

File hashes

Hashes for gns_pytorch-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2debe2e9966b1ea8b9b83dbd2d55a1a7c387e2f6ec55263a15afe869fa882b2c
MD5 1aa9c4b4eeeacffdec22d5ec84f6a52c
BLAKE2b-256 c61f77957a760e530e079922c3b907108175c1e68ce06e0cfd1737bab5b04529

See more details on using hashes here.

File details

Details for the file gns_pytorch-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for gns_pytorch-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9a6d2e054da8d75cb8e3bb97899c8991e8493008b4ed375dbd2f98f50608da79
MD5 4aaa3c7b4f1daa69f89e54278bd80b08
BLAKE2b-256 e03e2384236269a8d4183860d7d47453b71937052bc59dff0552b2437f7a3ff0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page