Skip to content

Share the compiled solve across VMEC Tracings - #230

Merged
rogeriojorge merged 4 commits into
mainfrom
perf/vmec-compile-cache
Nov 18, 2026
Merged

rogeriojorge merged 4 commits into
mainfrom
perf/vmec-compile-cache

Conversation

@rogeriojorge

Copy link
Copy Markdown
Member

PR #213's cache never hit for VMEC guiding centers: _axis_regular returned a new closure for every Tracing, so the static part of the key differed each time and jit(solve) was rebuilt (3 backend compiles per steady call, 0 recompiles in the coil case). Also Vmec was an opaque leaf compared by identity, so a second Vmec with the same shapes could not share the compile, and an in-place edit silently reused stale coefficients.

Changes:

  • _axis_regular returns a small callable that compares equal when the wrapped vector field is the same (dynamics.py).
  • Vmec is a pytree: coefficient tables are leaves, nfp/ns/mpol/ntor and the mode-number tables (read concretely by _radial_interp) are static (fields.py).
  • Test: repeated Tracing on one Vmec compiles nothing after the first, and a Vmec with the same shapes but 10% larger |B| reuses the one cached solve and gives different orbits.

Measured, gc_vmec, 500 ARIES-CS alphas, interleaved A/B x3 on one A4000 (steady, 3 reps each): recompiles per steady call 3 -> 0; first call 12.7-13.3 s -> about 20 s on the first run after the fix (the solve now compiles as one unit; same compile cost is paid once per process, not per call); steady 8.4 s median -> 8.3 s. So the steady wall time is kernel-bound; the win is the removed retracing/compiles, not a large speed-up. 142 tests in test_dynamics.py and test_fields.py pass.

Overlap: #207 and #209 also edit essos/dynamics.py; this touches only _axis_regular (about 15 lines), so conflicts should be small.

The axis-regular vector field was a new closure per Tracing, so the static
key never matched and every VMEC trace recompiled. Make it a comparable
callable, and make Vmec a pytree (coefficient tables as leaves, mode
numbers and counts static) so Vmec fields with the same shapes share it.

@eduardolneto eduardolneto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Makes axis_regular condition a pytree for optimization.Should be merged.

@rogeriojorge
rogeriojorge merged commit ac3168b into main Nov 18, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants