Repository navigation
Conversation
sbi potentials defaulted to `track_gradients=True` and switched gradients back on inside `torch.no_grad()`. Every sampler that only needs potential values therefore built an autograd graph it never used: the MCMC init strategies, importance sampling (SIR, the importance posterior and its normalization), the accept/reject loop of rejection sampling, the VI proportionality check and the evaluation steps of the MAP search. `track_gradients` now defaults to `None`, which keeps the caller's grad mode. An explicit `True` or `False` now applies to the whole potential: the likelihood, ratio and ensemble potentials added the prior or the ensemble weights outside their grad block, so inside `torch.no_grad()` they returned no gradients even with `True`. `importance_sample` runs without gradients, so a learnable proposal builds no graph either. Rejection sampling no longer forces gradients; `gradient_ascent` turns them on for its optimization step, so `map()` now also works inside `torch.no_grad()`. On slcp with NLE's default NSF on a V100, the extra peak GPU memory drops from 199 MB to 26 MB for the MCMC init, for 1,000 rejection draws and for the VI check with 10,000 samples, from 61 MB to 8 MB for 100 SIR draws, and from 19 MB to 2 MB for 10 MAP iterations. The results are identical. The tests check the grad mode inside each potential type with and without `track_gradients`, in the importance and rejection samplers and in the MCMC init, and that `map()` works inside `torch.no_grad()`. 14 of their 16 cases fail on main.
…e too The vector-field potential still defaulted to `track_gradients=False`. The conditioned potential forwarded the new `None` default into it, which `torch.set_grad_enabled` rejects, so conditional sampling of vector-field posteriors raised a TypeError; a rejection posterior built on a vector-field potential lost the gradients for its max search. It now defaults to `None` like the other potentials. The conditioned, likelihood-conditioned and ensemble potentials did part of their work before entering the grad block, so an explicit `True` inside `torch.no_grad()` lost the gradient with respect to theta. They now run the whole call in the block. The tutorial potential and the test potentials follow the same pattern. The grad-mode tests now cover the vector-field and conditioned potentials and use a Gaussian prior, and the override test compares gradients instead of checking `requires_grad`.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This branch has not been deployed
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
Many samplers only need potential values, so they evaluate the potential inside
torch.no_grad(). sbi potentials ignored this. Theirtrack_gradientsargument defaulted toTrue, and each potential set that grad mode itself. So the MCMC init, importance sampling, rejection sampling, the VI quality check and the MAP search all built an autograd graph that they never used.track_gradientsnow defaults toNone: the potential keeps the grad mode of its caller. An explicitTrueorFalseapplies to the whole potential, also insidetorch.no_grad().Three callers change with it.
importance_sampleruns without gradients. Rejection sampling no longer forces gradients on its potential.gradient_ascentturns gradients on for its optimization step, somap()now also works insidetorch.no_grad(); on main it raises there.This also fixes
map()of FMPE and NPSE posteriors built withsample_with="mcmc","importance"or"rejection". Their potential turned gradients off by default, so the MAP search raised "element 0 of tensors does not require grad" onmain.This helps everyone who samples with MCMC, rejection or importance sampling, most on a GPU.
Evidence
Extra peak GPU memory on mini-sbibm slcp, NLE with the default NSF:
resampleorsirprop, 10,000 samplessir, 100 drawsThe samples, initial parameters and MAP are identical before and after.
New tests check the grad mode for every potential type, with and without
track_gradients. Other tests check the samplers above andmap()insidetorch.no_grad(). 18 of the 20 cases fail on main and pass on this branch.Merge Danger
Door: two-way
Blast Radius: small
A potential called inside
torch.no_grad()withouttrack_gradientsnow returns no gradients. sbi itself does not rely on this. User code that does must passtrack_gradients=True. Subclasses ofBasePotentialthat set their own default keep their behaviour.