-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Perf optimization for building circuits using only appends #6882
Merged
Conversation
This file contains 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
daxfohl
force-pushed
the
circuit-append-perf
branch
from
December 29, 2024 22:54
2160169
to
c03dc02
Compare
daxfohl
changed the title
Perf optimization for circuit construction using only appends
Perf optimization for building circuits using only appends
Dec 30, 2024
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #6882 +/- ##
==========================================
- Coverage 97.88% 97.88% -0.01%
==========================================
Files 1085 1085
Lines 94962 94983 +21
==========================================
+ Hits 92952 92972 +20
- Misses 2010 2011 +1 ☔ View full report in Codecov by Sentry. |
mhucka
approved these changes
Jan 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
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.
Added a helper class
_PlacementCache
that maintains all the indices needed for fast near-constant-time op placement duringappend
, speeding up a worst-case quadratic perf behavior when constructing circuits via repeatedappend
calls. Any non-append
modification of the circuit simply nulls out the placer, and subsequent modifications just fall back to the (existing, slow) placement index search from then on.Updated
transformer_primitives.apply_map_func
to also use this class, since much of the code was duplicated.There's probably room to support additional insert strategies, or to allow for manual recreation of a nulled-out circuit placer, etc., to increase the applicability of this. But for now,
append
is the most common way to create circuits, and this PR handles that case.As mentioned in the test, this change fixes the placement perf, which is most egregious for long narrow circuits. (For short, wide circuits, the cost of recreating the (immutable) Moment for each new op added to it dominates, which this PR does not address.)
Also note that this change has no effect on performance of constructing circuits by passing in a list of ops into the
Circuit
constructor, which is already fast for both long and wide circuits. The only reason this PR is needed is because many of our tutorials demonstrate using repeatedappend
calls for constructing circuits rather than passing the ops into theCircuit
constructor, and it appears that many users do the same.