Vectorize angular (ATS) reductions to fix XLA compile blowup#102
Merged
almilder merged 1 commit intoJun 4, 2026
Merged
Conversation
The angular forward model wrapped two functions whose Python list comprehensions were fully unrolled into the jit-traced graph, once per output bin, and then re-traced through value_and_grad: - thomson_diagnostic.reduce_ATS_to_resunit: ~1024 + 1024 slice+mean+stack ops to bin the wavelength and angular axes. - irf.add_ATS_IRF: ~2048 + 1024 jnp.convolve ops for the separable 2D instrument-response smoothing. At angular shapes (npts=2048, 1024 angles) this is ~5000 unrolled iterations baked into a single graph. On CPU the angular value_and_grad failed to finish compiling in 15+ minutes. Replace both with vectorized equivalents: - _bin_average: reshape (+ NaN-pad for a ragged final window) and mean, a single op; bit-for-bit equal to the loop including the ragged case. - add_ATS_IRF: vmap each 1D convolution over the batch axis. After the change the same angular value_and_grad compiles in ~4.6s on CPU. Outputs are unchanged (1D fits recover identical parameters). Add tests/test_forward/test_ats_vectorization.py pinning both vectorized forms to the original loop semantics — these run on CPU, unlike the angular integration tests which skip without a GPU. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
almilder
approved these changes
Jun 4, 2026
almilder
left a comment
Collaborator
There was a problem hiding this comment.
Looks like a great fix, thanks for adding this!
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.
The angular forward model wrapped two functions whose Python list comprehensions were fully unrolled into the jit-traced graph, once per output bin, and then re-traced through value_and_grad:
At angular shapes (npts=2048, 1024 angles) this is ~5000 unrolled iterations baked into a single graph. On CPU the angular value_and_grad failed to finish compiling in 15+ minutes.
Replace both with vectorized equivalents:
After the change the same angular value_and_grad compiles in ~4.6s on CPU. Outputs are unchanged (1D fits recover identical parameters).
This is done with vmap not scan so both CPU and GPU should benefit equally.
Add tests/test_forward/test_ats_vectorization.py pinning both vectorized forms to the original loop semantics — these run on CPU, unlike the angular integration tests which skip without a GPU.