Skip to content

Support dynamic shape and newton step#2145

Merged
unalmis merged 21 commits intoku/bouncefrom
ku/dynamic
Apr 2, 2026
Merged

Support dynamic shape and newton step#2145
unalmis merged 21 commits intoku/bouncefrom
ku/dynamic

Conversation

@unalmis
Copy link
Copy Markdown
Collaborator

@unalmis unalmis commented Mar 29, 2026

  • resolves Patch for differentiable code with dynamic shapes #1303 .
  • Activates newton step to find bounce points.
    • It can be shown there exists O(sqrt(epsilon)) error for bounce integrals with 1/v_ll where epsilon is error of bounce point. For v_|| integrals error is conveniently O(epsilon times sqrt(epsilon)). Hence, the spline method would require thousands of knots per transit for just a couple digits of accuracy, and it would stop convergence at epsilon<=1e-5 (so sqrt(epsilon) <=3 digits) error due to condition number. Of course, the spline=False method has always computed the points with spectral accuracy and has very fast convergence after New inverse stream map to accelerate convergence #1919 ; that method converges to epsilon = machine precision without the newton step. With the newton step, fast convergence is achieved with the spline method as well.
    • I suspect fast ion confinement optimization will be easier now.

I did this on a couple lunch breaks, so it would be very weird if clicking the approve button to merge into master took a year.

Benchmarks

Here is a timing benchmark on my CPU with nufft_eps=1e-6. Prior to this PR, every adjoint call to nufft1 took >= 1 second and the full computation was 34 seconds. Now every adjoint call to nufft1 is 250 milliseconds, and the full computation is 14 seconds. These improvements become larger as the sparsity grows and error tolerance parameter for the nuffts epsilon tends to 0. Likewise, the improvement grows linearly with the problem size. As this is called within an optimization loop where time and memory are tight, the improvement is significant.

Before

Screenshot From 2026-03-29 15-22-56 Screenshot From 2026-03-29 15-22-36

After

Screenshot From 2026-03-29 15-12-35 Screenshot From 2026-03-29 15-15-59

@unalmis unalmis self-assigned this Mar 29, 2026
@unalmis unalmis added performance New feature or request to make the code faster easy Short and simple to code or review waiting for other PRs labels Mar 29, 2026
@unalmis unalmis linked an issue Mar 29, 2026 that may be closed by this pull request
@unalmis unalmis added the robustness Make the code more robust label Mar 30, 2026
@unalmis unalmis added the stable Awaiting merge to master. Only updates will be merging from master. label Mar 30, 2026
@unalmis unalmis changed the title Support dynamic shape to resolve #1303 Support dynamic shape and newton step Mar 30, 2026
dB_dz,
dB_dz + jnp.copysign(_eps, dB_dz.real),
)
dz12 = jnp.where(mask, (dp[..., None, :, None] - dB_do) / dB_dz, 0.0)
Copy link
Copy Markdown
Collaborator Author

@unalmis unalmis Mar 31, 2026

Choose a reason for hiding this comment

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

Equation 19 in the supplementary information in publications/unalmis2025.

Adds files to reproduce the results in the article.
@unalmis unalmis merged commit 0af3ec7 into ku/bounce Apr 2, 2026
5 checks passed
@unalmis unalmis deleted the ku/dynamic branch April 2, 2026 08:35
@unalmis unalmis removed the easy Short and simple to code or review label Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance New feature or request to make the code faster robustness Make the code more robust stable Awaiting merge to master. Only updates will be merging from master.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Patch for differentiable code with dynamic shapes

1 participant