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
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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e25213f2325c012d1a4372b1fdfb9e22c6790d791255f3555c933fc784f0519d
|
|
| MD5 |
62b5f208a13611d4c61a5b654bb08c2c
|
|
| BLAKE2b-256 |
10d4dc48336eaf61078e2968f3b940f3ecfb25bf57a1bac7b147956e115f91b1
|
Provenance
The following attestation bundles were made for toploss-0.1.0.tar.gz:
Publisher:
publish.yaml on MrRobotop/toploss
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
toploss-0.1.0.tar.gz -
Subject digest:
e25213f2325c012d1a4372b1fdfb9e22c6790d791255f3555c933fc784f0519d - Sigstore transparency entry: 1697114000
- Sigstore integration time:
-
Permalink:
MrRobotop/toploss@92f7ea510d0041821ff5943d2da8317197c9224e -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/MrRobotop
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@92f7ea510d0041821ff5943d2da8317197c9224e -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8adebd76a97a118b03ec9780a0ebaed8f6934dd024b5d479dc1aa50de84adad8
|
|
| MD5 |
65c2c0ea68fcd4c063d6ce17eb0f7286
|
|
| BLAKE2b-256 |
baf0b5bff469d781c6bc53d69083437e7f91cb72519f33c719ccee2c2ffd3ad2
|
Provenance
The following attestation bundles were made for toploss-0.1.0-py3-none-any.whl:
Publisher:
publish.yaml on MrRobotop/toploss
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
toploss-0.1.0-py3-none-any.whl -
Subject digest:
8adebd76a97a118b03ec9780a0ebaed8f6934dd024b5d479dc1aa50de84adad8 - Sigstore transparency entry: 1697114077
- Sigstore integration time:
-
Permalink:
MrRobotop/toploss@92f7ea510d0041821ff5943d2da8317197c9224e -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/MrRobotop
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@92f7ea510d0041821ff5943d2da8317197c9224e -
Trigger Event:
release
-
Statement type: