Skip to main content

Efficient Optimizers

Project description

heavyball

PyPI version License

High-performance, extensible, chainable optimizers for PyTorch.

Why heavyball

  • Lightning-Fast Training: Batched foreach operations deliver significant speedups on large models.
  • Adaptive & Extensible: Built-in AdamW, RMSprop, Schedule-Free algorithms, and PaLM-inspired schedules.
  • Plug-and-Play: Drop-in replacements for torch.optim with seamless integration.
  • Customizable: Chainable API lets you compose optimizers and transforms (MARS correction, cautious updates, orthogonal updates).
  • Battle-Tested: Extensive benchmarks and real-world examples included.

Key Features

  • Foreach-based optimizers: ForeachAdamW, ForeachRMSprop, ForeachSFAdamW, Muon, ADOPT, MSAM (Momentum SAM), …
  • Schedule-Free optimizers with dynamic learning rate adaptation.
  • Advanced update rules: MARS correction, cautious updates, PaLM beta2 scheduling.
  • Chainable transforms for custom optimization recipes.
  • Comprehensive benchmark suite (benchmark/).
  • Detailed documentation and example-driven tutorials.

Quickstart

Install:

pip install heavyball

Basic usage:

import torch
from torch import nn
from heavyball import ForeachAdamW

model = nn.Sequential(
    nn.Linear(128, 64), nn.ReLU(), nn.Linear(64, 10)
)
optimizer = ForeachAdamW(model.parameters(), lr=1e-3)

for data, target in dataloader:
    optimizer.zero_grad()
    output = model(data)
    loss = torch.nn.functional.cross_entropy(output, target)
    loss.backward()
    optimizer.step()

Benchmarks

Reproduce benchmarks with:

python3 -m benchmark.run_all_benchmarks --opt ForeachSOAP --opt LaProp --opt AdamW --opt Muon --opt ForeachCachedNewtonPSGD  --opt RMSprop --opt OrthoLaProp --opt ForeachSFAdamW --opt ForeachADOPT --opt LaPropOrtho --opt CachedPSGDKron --opt SignLaProp --opt ForeachSOLP --opt PSGDLRA --opt NewtonPSGDLRA --opt NewtonHybrid2PSGDKron --opt NewtonHybrid2PSGDLRA --opt mars-NewtonHybrid2PSGDLRA --opt MSAMLaProp --opt mars-adaptive-NewtonHybrid2PSGDKron  --opt mars-ortho-NewtonHybrid2PSGDKron --opt MuonLaProp --opt mars-unscaled-NewtonHybrid2PSGDKron --opt mars-NewtonHybrid2PSGDKron --opt cautious-AdamW --opt unscaled_cautious-AdamW --opt mars-AdamW  --dtype float32 --steps 1000000 --trials 1000 --parallelism 256 --seeds 1 --difficulties trivial --difficulties easy --difficulties medium --difficulties hard --difficulties extreme --difficulties nightmare --timeout 2880

Migrating from HeavyBall 1.x

  • Read the detailed 2.0.0 migration notes for an end-to-end checklist, including guidance for restoring legacy behaviour when needed.
  • Use scripts/migrate_optimizer_state.py to rewrite pre-2.0 optimizer checkpoints:
    python scripts/migrate_optimizer_state.py path/to/checkpoint.pt heavyball.ForeachAdamW --state-key optimizer
    
    The utility renames legacy state entries, fans them out per parameter view, and injects the HeavyBall metadata block expected by 2.0.0.

Contributing

We welcome contributions! Please check the issue tracker and follow these steps:

  1. Fork the repo and create a feature branch.
  2. Install dev dependencies: pip install -e .[dev].
  3. Run tests: pytest.
  4. Submit a pull request.

License

BSD 3-Clause — see the LICENSE file.


Made by the HeavyBall team.

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

heavyball-2.0.0.tar.gz (60.8 kB view details)

Uploaded Source

Built Distribution

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

heavyball-2.0.0-py3-none-any.whl (47.7 kB view details)

Uploaded Python 3

File details

Details for the file heavyball-2.0.0.tar.gz.

File metadata

  • Download URL: heavyball-2.0.0.tar.gz
  • Upload date:
  • Size: 60.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for heavyball-2.0.0.tar.gz
Algorithm Hash digest
SHA256 705c635d9be9f87bf37bf0c2a4d268d9964deae7474a14c710234292451f838f
MD5 a254f1f46b631e3c25d9fcdb8dd69340
BLAKE2b-256 40cccf047331b5918ad8d994df58648f52bd6ca64d36270581062ff5c7a31589

See more details on using hashes here.

File details

Details for the file heavyball-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: heavyball-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 47.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for heavyball-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ff752f735f44a64f46956aed7a742aeb701811f4c98df066587e80539d6f5c0
MD5 6de37f549ec3d032c8a8edce14084058
BLAKE2b-256 72c4d21d33bbcae5ebef0b3ab70b4d4275319c0e6187a68c5f45b1610305b689

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