Repository navigation
FSE trains: exact derivatives for the Busse and PD designs - #231
Merged
Merged
Conversation
Contributor
Author
|
CI is red at install, before any test runs: This is expected. The pin is to the tangent kernels and Generated by Claude Code |
The PD shooting and both outer optimizers take their derivatives from blochsim: the Newton Jacobian from prescribed_flips tangents, the train's slope with respect to its controls from busse_flips(jacobian=True) or the implicit function theorem over the PD levels, and the ensemble echoes' slopes from forward-mode JVPs through FSESimulator. No residual or cost is differenced any more. Requires blochsim>=0.0.19. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MFB6QKxkFL4hC7MC4RCb2V
mcencini
force-pushed
the
claude/project-thread-5qxlqi
branch
from
November 12, 2026 18:28
3cc126b to
6664d41
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
mcencini
added a commit
that referenced
this pull request
Nov 13, 2026
…ve kernel #231 took the optimizer's flip derivatives with torch.func.vmap(jvp(...)). blochsim differentiates a sequence argument by reverse mode ("build a cost and call backward()"), and its native packing reads the train's structure with .tolist(), which a torch.func wrapper does not allow under the PyTorch the deployed environments pin (2.7.1): every fse3d Busse protocol failed at generation with "Cannot access data pointer of Tensor that doesn't have storage" on the 7 T (exam 5195). CI passed because it only ran the latest PyTorch. _ForwardSignal.slopes now takes one backward pass, J^T u with create_graph, and its derivative along each direction, J d: exact, plain autograd, blochsim's native backward. In the deployed environment tests/test_fse_busse.py and tests/test_fse_effective_te.py pass 40/40 (4 failed before, with the 7 T error); native reverse-mode gradients match float32-safe central differences to 4-5 digits. CI's Python 3.10 job now also installs torch 2.7.1. Co-Authored-By: Claude Opus 5.5 <[email protected]>
mcencini
added a commit
that referenced
this pull request
Nov 13, 2026
#235) * EPI sequences: blip-free phase reference train in a dummy shot epi2D/epi3D take phase_reference (default False) and navigator_lines (None: NAVIGATOR_LINES without the reference, none with it). With the reference, the last dummy shot of each slice (2D), of the volume (3D) and the FLEET calibration's dummy shot plays the shot's own train with every phase- and partition-encode prewinder, blip and rewinder scaled to zero, its lines acquired and labelled PHASECORR; every train line carries its position in the train in USER0 (ECHO_COUNTER). A scan without dummies plays one dummy cycle for it. The reference reuses the imaging shot's events, so it adds no segment. gain_calibration_readouts counts PHASECORR readouts with the dummies, as it does navigators. Scanner hot-fix, 13 Nov 7 T session (exam 5195 series 2: the navigator's odd/even slope, +52 mrad/px, is not the train's, +35 mrad/px). Co-Authored-By: Claude Opus 5.5 <[email protected]> * Busse optimizer: exact slopes by reverse mode through blochsim's native kernel #231 took the optimizer's flip derivatives with torch.func.vmap(jvp(...)). blochsim differentiates a sequence argument by reverse mode ("build a cost and call backward()"), and its native packing reads the train's structure with .tolist(), which a torch.func wrapper does not allow under the PyTorch the deployed environments pin (2.7.1): every fse3d Busse protocol failed at generation with "Cannot access data pointer of Tensor that doesn't have storage" on the 7 T (exam 5195). CI passed because it only ran the latest PyTorch. _ForwardSignal.slopes now takes one backward pass, J^T u with create_graph, and its derivative along each direction, J d: exact, plain autograd, blochsim's native backward. In the deployed environment tests/test_fse_busse.py and tests/test_fse_effective_te.py pass 40/40 (4 failed before, with the 7 T error); native reverse-mode gradients match float32-safe central differences to 4-5 digits. CI's Python 3.10 job now also installs torch 2.7.1. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Docs: the EPI phase reference train in the EPI tour; te without a navigator The EPI tour says what the shipped EPI sequences acquire for the odd/even correction: navigator lines, or with phase_reference the blip-free reference train. The te default of epi2D/epi3D no longer assumes a navigator. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
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.
Requested by Matteo · project thread
What changed
No residual or cost in the Busse and PD refocusing-train designs is differenced any more; every derivative is exact.
_pd.py): the 2×2 Newton Jacobian comes fromprescribed_flips(signal_tangents=), using analytic derivatives of the envelope with respect to level, ratio and plateau fraction (_Envelope)._pd_fraction_slopesgives the solved train's derivative with respect to the plateau fraction, by the implicit function theorem over the two levels._busse_opt.optimize_controls): dflip/du comes frombusse_flips(jacobian=True)(cached as_busse._schedule_slopes). The ensemble echoes and their slopes come from forward-mode JVPs throughFSESimulator:torch.func.jvpvmapped over the 2 directions, on blochsim's nativesimulate_jvp. The PSF radius, contrast, RF-energy and effective-TE constraints are differentiated analytically from those, the last througheffective_te(flip_tangents=). Each SLSQP evaluation is one ensemble forward plus one JVP per control, where it was 5 forwards on probes._pd_opt.optimize_pd_controls): works the same way, with 1 direction (plateau fraction).TimedFSEof_pd_optis now shared as_busse_opt._ForwardSignal.blochsim>=0.0.19.Why
Matteo found the finite differences numerically unstable. The optimizers stepped 0.025° in angle through a float32 simulator, so the gradient carried round-off noise. The design runs on the scanner host's CPU.
Depends on pulserver/blochsim#57 (tangent kernels,
busse_flips(jacobian=True)). CI here will fail on theblochsim>=0.0.19pin until that is merged and released.Validation
pytest -q -n 4against blochsim#57 installed editable: 5338 passed, 37 skipped.bash scripts/format_and_lint.sh --check: all checks passed.test_busse_schedule_slopes_are_the_derivative_of_the_scheduletest_psf_radius_slopes_are_its_derivativetest_ensemble_echo_slopes_are_the_derivative_of_the_simulated_echoes(float32 simulator, so a 1e-2 step)test_pd_fraction_slope_is_the_derivative_of_the_solved_traintest_outer_fit_keeps_family_and_improves_forward_psfnow records through_ForwardSignal, because vmapped JVP tensors have no storage to copy.OMP_NUM_THREADS=1 python benchmarks/busse_schedule.py, run on this branch and on main in the same container. The fitted angles and PSF are the same, and the fits take equal or less time:These timings are from this container and only compare the two branches.
Documentation impact
None. The changed functions are private, and the public
make_busse_schedule/make_pd_schedulereturn the same trains.benchmarks/busse_schedule.pyalso clears the new slopes cache between timed fits.Generated by Claude Code