Fix BufferizationStage IR blowup for runtime-parameterized Hamiltonians (Scalarize)#3013
Fix BufferizationStage IR blowup for runtime-parameterized Hamiltonians (Scalarize)#3013jk20342 wants to merge 10 commits into
Conversation
…tonians Add scalarize-tensor-extracts and reroll-loops passes and re-order the default pipeline so runtime-coefficient Trotterization no longer amplifies IR through bufferization (9.9x -> ~1.0x on the H2 QPE benchmark). Fixes PennyLaneAI#2759
…tonians Add scalarize-tensor-extracts and reroll-loops passes and re-order the default pipeline so runtime-coefficient Trotterization no longer amplifies IR through bufferization (9.9x -> ~1.0x on the H2 QPE benchmark). Fixes PennyLaneAI#2759
…tonians Add scalarize-tensor-extracts and reroll-loops passes and re-order the default pipeline so runtime-coefficient Trotterization no longer amplifies IR through bufferization (9.9x -> ~1.0x on the H2 QPE benchmark). Fixes PennyLaneAI#2759
|
here is some benchmark code https://gist.github.com/jk20342/d6cc77c374522db0d59e646dfc4a04ae and results through my docker container :] |
|
This could be amplified lower I think but there is upstream issue on LLVM that I bumped to try to get more traction on |
|
Thanks for this contribution @jk20342, very nice results 💯 I love the scalarization pass, which is beneficial in more ways than performance! I'd be curious to know the effect size of the scalarization vs the rerolling, as using loops is known to have massive impact on the compile time. Automatically detecting unrolled loops is definitely nice for programs that were suboptimally written, although we are continuously updating PL library code to make sure programs use loops to begin with as well. |
@dime10 when i tested it the reroll improved ~1.2x wall time ~1.35x memory and ~1.27x final IR and a 5x reduction in gate-op volume over just scalarize could remove it, if you think this will be fixed on PL directly though |
|
Thanks @jk20342, this is a very impressive work! It seems to me that the |
Yep I can split it up |
…xtracts here Co-authored-by: Cursor <cursoragent@cursor.com>
|
@mehrdad2m made this PR the scalarize #3036 is reroll now :) |
Context:
qml.TrotterProductwith runtime coefficients (viaqml.dot) causes severe IRamplification: tracing unrolls all Trotter steps, and every gate angle drags a chain of
single-element tensor ops through bufferization, each becoming an alloc plus copies.
For an H2 QPE workload this meant ~62x total IR growth and ~19 GB of compiler memory.
Description of the Change:
scalarize-tensor-extractspass: foldstensor.extractthroughextract_slice/collapse_shape/small elementwiselinalg.generic, turning gate-angledataflow into scalar arithmetic.
reroll-loopspass: detects repeated op sequences via structural hashing andrebuilds them as
scf.forloops.linalg-fuse-elementwise-opsbeforereroll-loops, and a second scalarize after detensorization (which materializes theextract chains).
Benefits:
On the H2 QPE benchmark, before/after measured on the same machine, same build, same
input IR, differing only in the pipeline: compile time 1h41m -> ~25s (~240x), peak
memory 15.4 GB -> 1.2 GB (12.8x), final IR 18.4 MB -> 5.4 MB (3.4x), post-bufferization
IR 7.45 MB -> 2.64 MB (2.8x). Output probabilities are bit-identical to the old pipeline.
these baseline numbers differ from the issue report (306 s, 21.3 GB, 241 MB final
IR) because the issue predates current main, which already reduced the
baseline's bufferization blowup (60 MB -> 7.45 MB post-bufferization; input IR drifted
3.86 -> 4.69 MB), and these measurements use an assertions-enabled dev build, ~20x
slower in wall time than the release wheel the issue used. Ratios within this PR are
apples-to-apples; absolute times are not comparable to the issue's.
Possible Drawbacks:
Both passes are conservative (size caps, semantic verification, high
min-savingsdefault) but rerolling may miss non-contiguous repeats; overhead on workloads without
the pattern is negligible.
Related GitHub Issues:
Fixes #2759. Related: #1464, #1507.