Skip the redundant permute in matricize, returning a view for dense arrays#183
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #183 +/- ##
==========================================
+ Coverage 78.84% 79.07% +0.23%
==========================================
Files 20 20
Lines 657 669 +12
==========================================
+ Hits 518 529 +11
- Misses 139 140 +1
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:
|
4ba84bf to
ec2331e
Compare
…rrays `matricize`/`matricizeop` now skip the permuted copy when the requested row and column grouping is already in storage order, instead of always permuting first. A new `matricizekind` classifier, dispatched on `FusionStyle`, decides this per bipermutation: the generic classifier recognizes the always-safe aligned case (skipping a no-op permute is valid for any style), and `ReshapeFusion` (dense) additionally recognizes a pure codomain/domain swap, which it realizes as a lazy `transpose`. For a dense array the aligned and swapped cases return a `reshape`/`transpose` view of the input. For a graded array `matricize` still gathers blocks into a new matrix, but the redundant permute copy beforehand is skipped. The fast paths require `op === identity`, since a plain view cannot carry a fused `op` like `conj`. The result may alias the input and is read-only, which matches the `matricizeop` docstring's existing contract. This removes input-copy allocations from aligned contractions. At bond dimension 64 the dense matmul runs about 35% faster and a memory-bound rank-3 contraction about 50% faster, closing most of the gap to an optimized reference, and an aligned `AbelianGradedArray` contraction drops one operand's permute copy. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
ec2331e to
0fc13e3
Compare
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
matricize/matricizeopnow skip the permuted copy when the requested row and column grouping is already in storage order, instead of always permuting first. A newmatricizekindclassifier, dispatched onFusionStyle, decides this per bipermutation: the generic classifier recognizes the always-safe aligned case (skipping a no-op permute is valid for any style), andReshapeFusion(dense) additionally recognizes a pure codomain/domain swap, which it realizes as a lazytranspose. For a dense array the aligned and swapped cases return areshape/transposeview of the input. For a graded arraymatricizestill gathers blocks into a new matrix, but the redundant permute copy beforehand is skipped. The fast paths requireop === identity, since a plain view cannot carry a fusedoplikeconj. The result may alias the input and is read-only, which matches thematricizeopdocstring's existing contract.This removes input-copy allocations from aligned contractions. At bond dimension 64 the dense matmul runs about 35% faster and a memory-bound rank-3 contraction about 50% faster, closing most of the gap to an optimized reference, and an aligned
AbelianGradedArraycontraction drops one operand's permute copy.The contraction driver still allocates the product matrix between the matrix multiply and the destination, and the factorizations still copy their input. Removing the product temp and adding an ownership-preserving
matricize_copywill land in follow-up PRs.