lightly icon indicating copy to clipboard operation
lightly copied to clipboard

Implement VISReg (Variance-Invariance-Sketching Regularization) loss + examples

Open gabrielfruet opened this issue 5 months ago • 11 comments

Add VISReg loss + examples

Paper: https://arxiv.org/abs/2606.02572 (Wu, Balestriero, Levine, Jun 2026) Official repo: https://github.com/HaiyuWu/visreg

Sits between VICRegLoss and SIGReg/LeJEPALoss: keeps the variance term from VICReg, replaces covariance with a sliced-Wasserstein sketching term against an isotropic Gaussian. O(NDK), and unlike SIGReg the gradient doesn't vanish under collapse (their Fig. 2).

Note: the official repo is CC BY-NC 4.0, non-commercial. Fine as a reference for the math (Algorithm 1 is unambiguous), but we shouldn't port code directly given our license.

Two PRs:

PR 1: Loss

  • lightly/loss/visreg_loss.py, structured like LeJEPALoss
    • Don't forget to reference the paper and authors.
  • register in lightly/loss/__init__.py
  • update docs/source/lightly.loss.rst (autodoc entry, same pattern as VICRegLoss/LeJEPALoss)
class VISRegLoss(nn.Module):
    def __init__(
        self,
        lambda_param: float = 0.9,
        num_slices: int = 4096,
        lambda_scale: float = 1.0,
        lambda_shape: float = 1.0,
        lambda_center: float = 1.0,
        gather_distributed: bool = False,
        eps: float = 1e-4,
    ):
        ...

    def forward(self, z: Tensor) -> Tensor:
        ...

Open points:

  1. Distributed: paper generates K slices per GPU, no batch gather (Sec 3.2, Fig. 6). Use that instead of the SIGReg all_reduce pattern from #1920.
  2. Return component losses (scale/shape/center/pred) separately — same as #1422 for VICReg. Table 12 shows shape-loss weight is the main tuning lever for long-tailed/low-rank data.
  3. Reuse LeJEPAProjectionHead, no new head needed (no changes to lightly/models/modules/heads.py expected).
  4. Use the best hyperparameters combinations defined in the paper.

Tests:

  • Try to match same testing patterns for the other losses.

PR 2: Examples (depends on PR 1)

  • examples/pytorch/visreg.py
  • examples/pytorch_lightning/visreg.py
  • examples/pytorch_lightning_distributed/visreg.py
  • docs/source/examples/visreg.rst, same format as vicreg.rst, plus adding it to the examples toctree/index
  • add VISReg to the model list in the main README and docs landing page

Follow the VICReg examples structure: backbone + LeJEPAProjectionHead + VISRegLoss, multi-crop augmentation per paper's A.1 setup.

gabrielfruet avatar Jul 02 '26 12:07 gabrielfruet

@RecreationalMath @saud5150 @MohamedFarag21 If any of you find this interesting and would like to contribute, please let me know :).

We would love your contributions.

gabrielfruet avatar Jul 02 '26 12:07 gabrielfruet

This looks interesting. I'm occupied till July 10. If not needed immediately, then I'm happy to pick it up in the week of July 13th.

RecreationalMath avatar Jul 02 '26 13:07 RecreationalMath

FYI, I found BarlowTwins to have the same bug as the one pointed out for SIGReg in #1920. I will open an issue for it and fix that as well.

RecreationalMath avatar Jul 02 '26 13:07 RecreationalMath

Thanks for the ping — this is right up my alley. I'll pick up PR 1 (loss implementation): distributed K-slices per GPU (no all_reduce), component loss returns, and reusing LeJEPAProjectionHead. I'll open a draft PR with tests before July 10, so @RecreationalMath has something concrete to build the examples on top of when they're back.

saud5150 avatar Jul 02 '26 14:07 saud5150

@RecreationalMath @saud5150 Sounds like a plan to me!

gabrielfruet avatar Jul 02 '26 19:07 gabrielfruet

Hey, another thing that would be really nice is implementing the VISRegL into the benchmarks code. If any of you would like to that, would be really nice!!

@lorinczszabolcs @RecreationalMath @saud5150 .

If you also need any help, please, feel free to reach out to me.

gabrielfruet avatar Jul 22 '26 15:07 gabrielfruet

Happy to take this on. I'll add VISReg to the resnet50 suite alongside VICReg, since it's VICReg-family, unless you'd rather it go in vitb16 given it reuses LeJEPAProjectionHead. Either way I'll wire it in and prep the launch script so it's a one-command run on your end.

saud5150 avatar Jul 22 '26 15:07 saud5150

I think ViTs are more relevant. If you could add both is also nice!!

I would rather focus on vits16 instead of vitb16. The idea would be to compare it with LeJEPA instead :)

Let me know what you think!

gabrielfruet avatar Jul 22 '26 19:07 gabrielfruet

Confirmed. I'll add VISReg to the ViT suite on ViT-S/16, right next to lejepa.py, with the loss swapped.

On settings: the paper gives a full recipe (Appendix A.1), but its headline run is ViT-B/16 for 400 epochs, which doesn't match the LeJEPA benchmark here (ViT-S/16, batch 512, 100 epochs). So I'd take VISReg's loss and augmentation settings from the paper (λ≈0.9 for ImageNet, projection dim 256, K=2048, DINO multi-crop with 4 global + 6 local views) and match LeJEPA's training scale for everything else (backbone, epochs, batch, schedule). That keeps it a fair comparison and runnable on the same setup.

ResNet-50 as a follow-up.

saud5150 avatar Jul 22 '26 20:07 saud5150

Looks good to me! changing the schedule and batch size is ok, since the method may have different dynamics

You can take a look at how we do with other methods, like LeJEPA and DINOv2. Some methods benefit from longer schedules or larger batch sizes and that's fine, the ideal is to use the best setting in the paper as much as possible like what we did in LeJEPA.

We generally train smaller models because we want to add a contribution to the community, these checkpoints are generally not released in the original papers

gabrielfruet avatar Jul 23 '26 09:07 gabrielfruet

I think this is done :) Thank you everyone

gabrielfruet avatar Aug 07 '26 19:08 gabrielfruet