Skip to main content

CASMO: Confident Adaptive Selective Momentum Optimizer

PyPI version Downloads Python 3.8+ License: MIT Paper: Theory

The Optimizer That Knows What To Learn.

Abstract

CASMO is a novel optimization algorithm designed to address the limitations of uniform gradient application in deep learning. Unlike standard optimizers (e.g., AdamW, SGD) that treat all gradients as equally valid, CASMO introduces Adaptive Gradient-Aware Regularization (AGAR). This mechanism dynamically quantifies the "signal-to-noise" ratio of gradients over time, allowing the optimizer to selectively dampen updates that correspond to noise, outliers, or conflicting information while accelerating learning on generalizable patterns.

This approach yields state-of-the-art stability in challenging regimes such as label noise, long-tail distributions, differential privacy, and continual learning.


💡 Key Innovation: The AGAR Mechanism

The core of CASMO is the Adaptive Gradient Alignment Ratio (AGAR), a metric that measures the consistency of gradient direction over a sliding window:

$$ \text{AGAR} = \frac{||\mathbb{E}[g]||^2}{||\mathbb{E}[g]||^2 + \text{Var}[g]} $$

  • High AGAR ($\approx 1$): Indicates consistent, reliable signal (e.g., generalizable features). CASMO applies full updates.
  • Low AGAR ($\approx 0$): Indicates high variance or conflict (e.g., label noise, privacy noise, catastrophic forgetting). CASMO automatically dampens the learning rate.

This allows CASMO to act as an automatic filter during training, improving robustness without manual hyperparameter tuning or data filtering.


⚡ Quick Start

Installation

Via PyPI (Recommended):

pip install casmo-optimizer

From Source (For Development):

git clone https://github.com/abderahmane-ai/CASMO.git
cd CASMO
pip install -e .

Usage

CASMO is a drop-in replacement for torch.optim.AdamW.

from casmo import CASMO

# Initialize with standard parameters
optimizer = CASMO(
    model.parameters(), 
    lr=1e-3, 
    weight_decay=0.01,
    granularity='group'  # Recommended for efficiency
)

# Standard PyTorch training loop
for batch in dataloader:
    optimizer.zero_grad()
    loss = model(batch)
    loss.backward()
    optimizer.step()

🧪 Benchmarks & Reproducibility

We provide a comprehensive suite of benchmarks demonstrating CASMO's superiority in specific failure modes of standard optimizers. Each benchmark contains full reproduction scripts and detailed analysis.

Benchmark Domain Challenge Key Result Link
B1 Generalization Grokking with 30% Label Noise 90.4% vs 24.0% val acc View
B2 Long-Tail CIFAR-100 Imbalance (100:1) Better tail-class accuracy View
B3 Privacy DP-SGD (ε ≈ 0.37) Maintains accuracy under DP View
B4 Continual Learning Sequential LLM Fine-tuning 13% less forgetting View
B5 Finance Portfolio Optimization Lower turnover, higher Sharpe View

See benchmarks/README.md for detailed results and reproduction instructions.


🛠️ Configuration

Parameter Default Description
lr 1e-3 Base learning rate.
weight_decay 0.0 Decoupled weight decay (same as AdamW).
granularity 'group' 'group' (Fast, recommended) or 'parameter' (Precise).
tau_init_steps 500 Steps for initial AGAR calibration (auto-tuned).
c_min 0.1 Minimum confidence floor (prevents dead neurons).

Citation

If you use CASMO in your research, please cite:

@software{casmo2025,
  title={CASMO: Confident Adaptive Selective Momentum Optimizer},
  author={Ainouche, Abderahmane},
  year={2025},
  url={https://github.com/abderahmane-ai/CASMO}
}

Made with ❤️ for the ML research community.

Metadata

Release files for casmo-optimizer 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for casmo-optimizer 0.3.0
File Size Uploaded Provenance
casmo_optimizer-0.3.0.tar.gz 1.6 MB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for casmo-optimizer 0.3.0
File Interpreter ABI Platform
casmo_optimizer-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.6 MB

Release files / casmo_optimizer-0.3.0.tar.gz

Download URL casmo_optimizer-0.3.0.tar.gz
Size 1.6 MB
Tags Source
SHA-256 checksum
How to use checksums
2f41c10a1e09b8eac163dd7fa988d5509aed7ba04de6a51e5f25d84f7db42b76
BLAKE2b-256 checksum
How to use checksums
235db0d8370a0a1456c7cd6e82d3e78df246ba499edff4cb7b0e3e4ffee1e252
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Release files / casmo_optimizer-0.3.0-py3-none-any.whl

Download URL casmo_optimizer-0.3.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f1bd1f56738694c2ab3890052abf2e6da4eb373d791b49d1dea1efc6e6b63c04
BLAKE2b-256 checksum
How to use checksums
1694c666a33ce63857c363692e958d17150a424adec5f3955fd6b2e0ff4931b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Security for casmo-optimizer 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page