Replace Python for loop with jax.lax.scan in step_n#7
Conversation
Currently, step_n uses a Python for loop to iterate over Langevin steps. While correct, this causes JAX to unroll the full loop at trace/compile time, which leads to longer compilation times and higher memory usage as num_steps grows. This PR replaces the loop with jax.lax.scan, which compiles the loop body once regardless of num_steps, making compilation time constant and reduces memory The behavior is identical — same random key splitting logic, same step_single calls, same outputs. The **kwargs are captured via closure inside scan_step, which works correctly as long as kwargs don't vary per step (consistent with the existing usage).
|
Dear @maximilianGord We already do ScoreMD/src/scoremd/simulation.py Line 68 in 149b1ec The problem with This also reduces memory allocations since the arrays can actually be overridden. Have you performed any benchmarks? As of now, I do not see what this change would add to the code base. If you think compilation time is actually a problem, we can talk about just setting |
Currently, step_n uses a Python for loop to iterate over Langevin steps. While correct, this causes JAX to unroll the full loop at trace/compile time, which leads to longer compilation times and higher memory usage as num_steps grows.
This PR replaces the loop with jax.lax.scan, which compiles the loop body once regardless of num_steps, making compilation time constant and reduces memory.
The behavior is identical — same random key splitting logic, same step_single calls, same outputs. The **kwargs are captured via closure inside scan_step, which works correctly as long as kwargs don't vary per step (consistent with the existing usage).