Fix inverted sign in KDE loss - #77
Open
anishdulal wants to merge 1 commit into
Open
Conversation
The term returned +entropy, which is high when embeddings are spread out. ssl_meta_arch adds it to the minimized loss, so training was rewarding collapse instead of penalizing it. Return -entropy, matching the orientation of the KoLeo loss this replaced.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
KDELoss.forwardreturns-log(density), an entropy estimate that is high whenembeddings are spread out and low when they collapse.
ssl_meta_arch.py:332adds that value to the minimized total loss with a positive weight, so gradient
descent minimizes entropy — it rewards collapse.
KoLeoLoss, which this term replaced, has the opposite (correct) orientation.Since
koleo_loss_weight: 0in every training config, KDE is currently theonly entropy regularizer in the objective, and it points the wrong way.
Evidence
1. Value at the two extremes (n=24, D=1536, both classes instantiated):
KDELoss(current)KoLeoLoss2. Gradient descent on each term alone, from a random init (n=24, D=128):
Sign-fixed KDE (−0.0435) and the KoLeo it replaced (−0.0433) converge to the same
configuration, which is what you would expect from two implementations of the same idea.
3. It has sat at its pro-collapse optimum in every run. Observed
kde_lossfromtraining_metrics.jsonagainst the analytic collapse floor-0.05 * (ln n + kappa):Three different batch sizes, three matches to within one fp16 ULP. The term
contributes no anti-collapse pressure at any point in training.
Why this is the right direction
foundation models with orders of magnitude less data (2025) — the paper this
regularizer was adopted from — introduces it in §"Self-supervised training with
DINOv2" as "a more stable KDE regularizer [28]". Its ref [28] is Wang & Isola
(2020), Understanding contrastive representation learning through alignment and
uniformity on the hypersphere, whose uniformity loss is
log E[exp(-t||x-y||^2)], minimized. For unit vectors that reduces tologsumexp(2t·cos) - log N+ const, i.e.+log(density). This implementationreturns its negation.
diversity of tile embeddings generated by the FM." Diversity means spread.
nanopathimplements the corrected form (train.py,kde_loss).On convergence
Karasikov et al. report that "Without replacing the default KoLeo regularizer
with KDE and without the HSV filter, our training did not converge". That is
a joint ablation: both changes were absent together, so it does not isolate the
contribution of either. OpenMidnight's training converged with the term in its
present form.
Notes
kde_lossmetric changes sign, so it is not comparable withhistorical run logs.
vitg14_reg4.yamlhas carried
koleo_loss_weight: 0+kde_loss_weight: .05since its firstcommit (016786c).
do_kde/do_koleoin the YAML are neverread — the gates are
cfg.dino.kde_loss_weight > 0andcfg.dino.koleo_loss_weight > 0. Sokde_loss_weight: -.05silently disablesthe term rather than flipping it, and the sign cannot be corrected from config.
Reproduction
Refs #4.