Skip to content

[Feature] Add Local Deep InfoMax (DIM) - #462

Open
ryan3473356866 wants to merge 5 commits into
galilai-group:mainfrom
ryan3473356866:add-dim
Open

ryan3473356866 wants to merge 5 commits into
galilai-group:mainfrom
ryan3473356866:add-dim

Conversation

@ryan3473356866

@ryan3473356866 ryan3473356866 commented Oct 5, 2026 •

Copy link
Copy Markdown

Description

Adds the local objective of Deep InfoMax (Hjelm et al., ICLR 2019, arXiv:1808.06670) with the Jensen-Shannon estimator and the optional prior matching term. Global DIM and the other MI estimators are not included.

  • methods.LocalDIM (spt.LocalDIM): method class wrapping forward.local_dim
  • losses.LocalDIMLoss, losses.DIMPriorLoss
  • backbone.LocalGlobalCritic, backbone.DIMEmbedding (encode-and-dot-product critic)
  • forward.local_dim
  • benchmarks/cifar10/local-dim-dcgan.py: CIFAR10 pretraining + frozen-encoder evaluation (Table 1 protocol)

Settings not stated in the paper follow the reference code (rdevon/DIM). One difference: the reference uses separate Adam optimizers for the encoder and the prior discriminator, while this PR uses one Adam with gradient isolation. These are equivalent here because both use the same hyperparameters, both step once per batch, and the gradients do not cross (tested).

Results (CIFAR10, single seed)

conv fc Y
Paper, DIM(L) (JSD), γ = 0.1 73.25 73.62 66.96
This PR, γ = 0.1 71.91 66.18 62.51
This PR, γ = 0 (local only) 73.00 69.34 66.91

Both runs: per-GPU batch 64 on 1 GPU (RTX 4060 Laptop), so the global batch is 64, matching the reference basic32x32 config; 1000 epochs. Local DIM draws negatives only from the other images in the per-device batch (no cross-device gathering), so with N GPUs the global batch would be 64 × N but each image still sees 63 negatives (checked in tests/distributed/test_dim_dist.py).

The local-only run matches the paper on conv and Y. The fc layer is lower in both runs, likely due to the evaluation schedule (the reference README notes it differs from the paper's). The prior did not help here, unlike in the paper; with a single seed this may be variance.

Tests

  • 45 unit tests in tests/unit/test_dim.py (100% coverage of new code), including a numerical match with the reference loss, training through spt.Manager with checkpoint resume, and finite gradients on extreme inputs.
  • LocalDIM added to tests/unit/test_methods_smoke.py.
  • 2 two-process (gloo) DDP tests in tests/distributed/test_dim_dist.py.

The full unit suite and pre-commit pass locally.

Checklist

  • I have read the Contributing document.
  • The documentation is up-to-date with the changes I made (check build artifacts).
  • All tests passed, and additional code has been covered with new tests.
  • I have added the PR to the RELEASES.rst file.

- Add LocalDIMLoss and DIMPriorLoss (stable_pretraining.losses)
- Add LocalGlobalCritic and DIMEmbedding encode-and-dot-product critic (stable_pretraining.backbone)
- Add local_dim forward function
- Add CIFAR10 pretraining and frozen-encoder evaluation script (benchmarks/cifar10/local-dim-dcgan.py)
- Add unit tests, METHODS.md and RELEASES.rst entries, API docs and citation
@ryan3473356866 ryan3473356866 changed the title [WIP] Add Local Deep InfoMax (DIM) Add Local Deep InfoMax (DIM) Oct 23, 2026
@ryan3473356866 ryan3473356866 changed the title Add Local Deep InfoMax (DIM) [Feature] Add Local Deep InfoMax (DIM) Oct 23, 2026

@Leon-Leyang Leon-Leyang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ryan3473356866, I've gone through the PR. Please check the issues below:

  • stable_pretraining/methods/: there's no DIM method class (an spt.Module subclass). Please add one that calls the existing local_dim forward function (stable_pretraining/forward.py:1267), exported from stable_pretraining/methods/__init__.py and the top-level package, and listed in its METHODS.md row (line 21) and docs/source/api/methods.rst.
  • stable_pretraining/tests/unit/test_dim.py: no test trains local DIM through spt.Manager (a small model and a few training steps are enough) and resumes from a checkpoint.
  • There's no real two-process DDP test, although the negatives come from the other images in the batch (stable_pretraining/losses/dim.py:58-62).
  • PR description: the global batch size (per-GPU batch × number of GPUs) isn't stated.
  • stable_pretraining/tests/unit/test_dim.py:85: the extreme-input tests (here and at line 75) only check the loss value, and DIMPriorLoss gets no extreme inputs. No test checks that the gradients stay finite.

Yanbo Hua added 2 commits December 2, 2026 16:32
- Add LocalDIM method class (spt.LocalDIM) wrapping the local_dim forward function; register in methods, top-level exports, METHODS.md, methods.rst, README and RELEASES.rst
- Add LocalDIM to test_methods_smoke.py
- Test training through spt.Manager and resuming from a checkpoint
- Add two-process gloo DDP tests (tests/distributed/test_dim_dist.py)
- Check gradients stay finite on extreme inputs for LocalDIMLoss and DIMPriorLoss
…enchmark

- List LocalDIM in stable_pretraining/__init__.pyi
- Benchmark builds spt.LocalDIM
- Document in local_dim that negatives are per GPU under DDP
- Add the Local DIM benchmark to benchmarks/README.md
@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@           Coverage Diff           @@
##             main     #462   +/-   ##
=======================================
  Coverage   98.17%   98.18%           
=======================================
  Files         163      166    +3     
  Lines       16406    16490   +84     
=======================================
+ Hits        16107    16191   +84     
  Misses        299      299           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ryan3473356866

Copy link
Copy Markdown
Author

Thanks for the review! I addressed all five points in the last two commits.

Method class. I added LocalDIM in stable_pretraining/methods/dim.py. It is a subclass of spt.Module and uses the existing local_dim forward function. You can import it as spt.LocalDIM. It is listed in METHODS.md, docs/source/api/methods.rst, README and init.pyi, and it has a smoke test in test_methods_smoke.py. The CIFAR-10 benchmark script now uses spt.LocalDIM, which builds the same model as before. I called it LocalDIM instead of DIM because only the local objective is implemented.

Manager and resume. A new test, test_dim_trains_with_manager_and_resumes_from_checkpoint, trains for 3 steps with spt.Manager. Then it resumes from last.ckpt and trains to step 6. It checks that the weights and the Adam state are restored, and that only steps 4–6 run.

DDP. I added tests/distributed/test_dim_dist.py, which runs 2 processes. It checks that each GPU only uses negatives from its own batch. It also checks that the DDP gradients equal the average of the per-GPU gradients, and that the model copies stay identical during training. The docstrings now say that negatives are per GPU under DDP.

Global batch size. It is 64 (64 per GPU × 1 GPU). I added this to the PR description and to benchmarks/README.md.

Extreme inputs. The tests at lines 75 and 85 now also run backward and check that the gradients are finite, for scores up to ±1e4. I also added a test where the critic is confidently wrong. DIMPriorLoss has two new tests, one with a saturated embedding and one with very large discriminator outputs. Both check that the loss and the gradients are finite.

@Leon-Leyang Leon-Leyang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ryan3473356866, I've gone through the changes and it looks good to me.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants