Skip to main content

Topological Loss Engineering: differentiable, optimizer-free regularizers that embed 2024-2026 optimizer breakthroughs (Muon/XSAM/CWD/AdEMAMix/NTKMTL/SymNoise) directly into the loss.

Project description

toploss: Topological Loss Engineering for PyTorch

Differentiable, optimizer-free regularizers that embed the 2024–2026 wave of optimizer breakthroughs (Muon/XSAM, Cautious Weight Decay, AdEMAMix, NTKMTL, SymNoise) directly into the loss function.

Modern training recipes push networks toward flat, well-conditioned minima by modifying the optimizer (SAM, Muon, CWD, …). toploss takes the opposite, much more portable route: it expresses each of those topological constraints as a plain penalty added to the loss, so a vanilla SGD/Adam reproduces the behaviour with no custom optimizer code.

Regularizer Inspiration One-line idea
SASP SAM / XSAM penalize the closed-form empirical-Fisher trace of the head, flattening curvature with no extra backward pass
CVP Cautious Weight Decay sigmoid-gated weight decay (stop-grad) → sliding-mode volume control
NGER NTKMTL + Excess Risk softmax task weights ∝ excess_risk / ntk_eigenvalue^γ
SBMP NEFTune / SymNoise KL consistency under symmetric-Bernoulli embedding noise
DMTA AdEMAMix align the current gradient with a slow-EMA descent direction

Install

pip install toploss          # from PyPI (once published)
# or, from source:
pip install -e .

Quickstart

import torch, torch.nn.functional as F
from toploss import TopologicalLoss

crit = TopologicalLoss(lambda_sasp=1e-2, lambda_cvp=1e-2)

logits, feats = model.forward_with_features(x)   # head logits + head inputs h
base = F.cross_entropy(logits, y)
loss = crit(base_loss=base, logits=logits, features=feats, targets=y,
            params=model.parameters())           # grads read from p.grad
loss.backward()
optimizer.step()                                  # any optimizer

Individual pieces

from toploss import SASPLoss, CVPRegularizer, NGERWeighter, SBMPLoss, DMTATracker

sasp = SASPLoss(rho=1e-2)
loss = F.cross_entropy(logits, y) + sasp(logits, feats, y)

cvp = CVPRegularizer(lam=1e-2, beta=50.0)
loss = loss + cvp(model.parameters())            # call after loss.backward()-free fwd

weighter = NGERWeighter(num_tasks=3, gamma=1.0)  # multi-task
mtl_loss = weighter([l1, l2, l3], [k1, k2, k3])  # k_t = ||grad_shared l_t||_F^2

Why it works (the correspondence principle)

For an optimizer update u = -η (g + c(θ)), the discrete trajectory is, to first order, gradient flow on L(θ) + Φ(θ) with ∇Φ = c. Each toploss penalty supplies exactly that c, so the loss now carries the topological bias that previously lived in the optimizer. See the accompanying paper for the full derivations, proofs, and experiments.

Tests

PYTHONPATH=src pytest tests/ -q

License

MIT.

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

toploss-0.1.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

toploss-0.1.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file toploss-0.1.0.tar.gz.

File metadata

  • Download URL: toploss-0.1.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for toploss-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e25213f2325c012d1a4372b1fdfb9e22c6790d791255f3555c933fc784f0519d
MD5 62b5f208a13611d4c61a5b654bb08c2c
BLAKE2b-256 10d4dc48336eaf61078e2968f3b940f3ecfb25bf57a1bac7b147956e115f91b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for toploss-0.1.0.tar.gz:

Publisher: publish.yaml on MrRobotop/toploss

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file toploss-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: toploss-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for toploss-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8adebd76a97a118b03ec9780a0ebaed8f6934dd024b5d479dc1aa50de84adad8
MD5 65c2c0ea68fcd4c063d6ce17eb0f7286
BLAKE2b-256 baf0b5bff469d781c6bc53d69083437e7f91cb72519f33c719ccee2c2ffd3ad2

See more details on using hashes here.

Provenance

The following attestation bundles were made for toploss-0.1.0-py3-none-any.whl:

Publisher: publish.yaml on MrRobotop/toploss

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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