Make PermutedDims a valid broadcast leaf#198
Merged
Merged
Conversation
## Summary Makes `PermutedDims` a valid `Base.Broadcasted` leaf, so a linear-combination broadcast that carries a lazily permuted operand (adding a permuted array, say) flattens through `tryflattenlinear` and materializes via `bipermutedimsopadd!` on the parent, instead of hitting the `broadcastable` fallback that would `collect` it. It implements the broadcast-leaf interface (`axes`, `eltype`, `broadcastable`, `BroadcastStyle`) and is still never indexed, since the fold absorbs it into a single leaf call. Generalizes `islinearbroadcast` so any leaf type participates, not just `AbstractArray`/`Broadcasted`. The default leaf slot is now `::Any`, with `::Number` marking the scalars, and that distinction is what keeps the predicate correct: scaling by a scalar is linear while a scalar shift (`a .+ 1`) is affine, and an elementwise array product is nonlinear while scaling is not. A `PermutedDims` wrapper (or a backend tensor) is then admitted by the `::Any` slot with no dedicated method, and `PermutedDims` becomes public. Builds on #195.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #198 +/- ##
==========================================
- Coverage 83.38% 82.95% -0.43%
==========================================
Files 22 22
Lines 656 669 +13
==========================================
+ Hits 547 555 +8
- Misses 109 114 +5
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:
|
mtfishman
added a commit
to ITensor/ITensorBase.jl
that referenced
this pull request
Jul 1, 2026
## Summary Aligns a misaligned operand in named-tensor broadcasting with `TensorAlgebra.PermutedDims` instead of `Base.PermutedDimsArray`. `PermutedDimsArray` encodes the permutation in a type parameter, so a runtime permutation forces a type-unstable, allocating construction on every permuted add. `PermutedDims` stores the permutation in a field, builds cheaply and type-stably, and is a broadcast leaf the linear-combination fold absorbs via `bipermutedimsopadd!`, so a permuted add no longer pays that type-construction floor and closes most of the gap to an aligned add. The wrapper stays confined to the broadcast alignment path (`broadcasted_unnamed`) and is never returned by the public `unnamed(a, names)`. Builds on the broadcast-friendly `PermutedDims` from ITensor/TensorAlgebra.jl#198.
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
Makes
PermutedDimsa validBase.Broadcastedleaf, so a linear-combination broadcast that carries a lazily permuted operand (adding a permuted array, say) flattens throughtryflattenlinearand materializes viabipermutedimsopadd!on the parent. The wrapper is still never indexed.Generalizes
islinearbroadcastso any leaf type participates, not justAbstractArray/Broadcasted. The default leaf slot is now::Any, with::Numbermarking the scalars, and that distinction is what keeps the predicate correct: scaling by a scalar is linear while a scalar shift (a .+ 1) is affine, and an elementwise array product is nonlinear while scaling is not. APermutedDimswrapper (or a backend tensor) is then admitted by the::Anyslot with no dedicated method, andPermutedDimsbecomes public.Builds on #195.