Skip to main content

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.

Metadata

Release files for toploss 0.1.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 toploss 0.1.0
File Size Uploaded Provenance Actions
toploss-0.1.0.tar.gz 13.5 kB With provenance Details for toploss-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for toploss 0.1.0
File Interpreter ABI Platform Actions
toploss-0.1.0-py3-none-any.whl Python 3 none any Details for toploss-0.1.0-py3-none-any.whl

Total release size: 25.6 kB

Release files / toploss-0.1.0.tar.gz

Download URL toploss-0.1.0.tar.gz
Size 13.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e25213f2325c012d1a4372b1fdfb9e22c6790d791255f3555c933fc784f0519d
BLAKE2b-256 checksum
How to use checksums
10d4dc48336eaf61078e2968f3b940f3ecfb25bf57a1bac7b147956e115f91b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 1, 2026.

Transparency log

Release files / toploss-0.1.0-py3-none-any.whl

Download URL toploss-0.1.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8adebd76a97a118b03ec9780a0ebaed8f6934dd024b5d479dc1aa50de84adad8
BLAKE2b-256 checksum
How to use checksums
baf0b5bff469d781c6bc53d69083437e7f91cb72519f33c719ccee2c2ffd3ad2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 1, 2026.

Transparency log

Security for toploss 0.1.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for toploss-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 1, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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