Repository navigation
[Feature] Add Local Deep InfoMax (DIM) - #462
ryan3473356866 wants to merge 5 commits into
Conversation
d3f9a79 to
2c511c4
Compare
- 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
2c511c4 to
ae384f0
Compare
Leon-Leyang
left a comment
There was a problem hiding this comment.
Hi @ryan3473356866, I've gone through the PR. Please check the issues below:
stable_pretraining/methods/: there's no DIM method class (anspt.Modulesubclass). Please add one that calls the existinglocal_dimforward function (stable_pretraining/forward.py:1267), exported fromstable_pretraining/methods/__init__.pyand the top-level package, and listed in itsMETHODS.mdrow (line 21) anddocs/source/api/methods.rst.stable_pretraining/tests/unit/test_dim.py: no test trains local DIM throughspt.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, andDIMPriorLossgets no extreme inputs. No test checks that the gradients stay finite.
- 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 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:
|
|
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
left a comment
There was a problem hiding this comment.
Hi @ryan3473356866, I've gone through the changes and it looks good to me.
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 wrappingforward.local_dimlosses.LocalDIMLoss,losses.DIMPriorLossbackbone.LocalGlobalCritic,backbone.DIMEmbedding(encode-and-dot-product critic)forward.local_dimbenchmarks/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)
Both runs: per-GPU batch 64 on 1 GPU (RTX 4060 Laptop), so the global batch is 64, matching the reference
basic32x32config; 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 intests/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
tests/unit/test_dim.py(100% coverage of new code), including a numerical match with the reference loss, training throughspt.Managerwith checkpoint resume, and finite gradients on extreme inputs.LocalDIMadded totests/unit/test_methods_smoke.py.tests/distributed/test_dim_dist.py.The full unit suite and pre-commit pass locally.
Checklist