Use linear scans instead of hashing for contraction labels#190
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #190 +/- ##
==========================================
- Coverage 82.71% 82.62% -0.09%
==========================================
Files 22 22
Lines 619 616 -3
==========================================
- Hits 512 509 -3
Misses 107 107
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
## Summary Does the dimname/label bookkeeping in the out-of-place contraction path with linear scans instead of the `Set`- and `Dict`-based `Base.setdiff`/`intersect`/`indexin`. For the handful of labels a tensor carries the hashing those build dominates, and on a small contraction it costs more than the matrix multiply itself. `biperms` and `contract_labels` use order-preserving `Vector` comprehensions for the output and contracted labels, and `tuple_indexin` finds label positions with `findfirst` rather than `Base.indexin`. The old check that every label appears exactly twice is replaced by a check that the destination carries exactly the uncontracted labels: the contracted and output groups partition the operands by construction, so that is the only consistency left to verify. These all assume the labels within each group are unique, which holds for a contraction. On a 4x4 contraction sharing one index, `a1 * a2` drops from 4.6 μs / 124 allocations to 1.9 μs / 60.
ba983f1 to
687f8c7
Compare
mtfishman
added a commit
that referenced
this pull request
Jun 30, 2026
## Summary Derives the bipartitioned permutations for `contract` as concretely-typed tuples behind a single `Val` function-barrier on the contracted count, rather than building them by converting runtime-length `Vector`s into abstract-typed tuples. The abstract tuples defeated the type stability of the downstream `matricize` pipeline, which is built around statically-sized tuples and `Val` barriers, so the per-call bookkeeping for a small contraction boxed heavily. This is a second pass on the label-derivation layer, after #190 removed the `Set` and `Dict` hashing from the same path. For a 4x4 matrix multiply the per-call cost drops from 51 allocations to 17 and is several times faster, landing just above the floor set by the lower-level entry point that takes the permutations directly. The remaining allocations are the output array temporaries and the destination-label `Vector`. The public `biperms` and `biperm` signatures are unchanged. The type-stable core is a new `Val`-parameterized method of `biperms`, and the two label entry points cross into `Val`-specialized helpers so the contraction itself runs type-stably.
This file contains hidden or 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
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.
Summary
Does the dimname/label bookkeeping in the out-of-place contraction path with linear scans instead of the
Set- andDict-basedBase.setdiff/intersect/indexin. For the handful of labels a tensor carries the hashing those build dominates, and on a small contraction it costs more than the matrix multiply itself.bipermsandcontract_labelsuse order-preservingVectorcomprehensions for the output and contracted labels, andtuple_indexinfinds label positions withfindfirstrather thanBase.indexin. The old check that every label appears exactly twice is replaced by a check that the destination carries exactly the uncontracted labels: the contracted and output groups partition the operands by construction, so that is the only consistency left to verify. These all assume the labels within each group are unique, which holds for a contraction.On a 4x4 contraction sharing one index,
a1 * a2drops from 4.6 μs / 124 allocations to 1.9 μs / 60.