Guard β=0 in 0-dim bipermutedimsopadd! and drop empty-array workaround#173
Merged
Conversation
The 0-dim branch read `dest[]` unconditionally (`β * dest[] + α * op(src[])`),
even when `β = 0`. By BLAS convention, `β = 0` means `dest` is treated as
write-only and its contents need not be defined. With element types whose
`undef` storage is unreadable,
`bipermutedimsopadd!(Array{BigFloat, 0}(undef), identity, src, (), (), true, false)`
threw `UndefRefError`, since the unassigned slot of a 0-dim array of mutable
`BigFloat` cannot be read.
The 0-dim case is now handled by the existing multi-dim broadcast path, which
already guards on `iszero(β)`. The previous special case carried a TODO
blocking on `GradedArray` no longer being an alias for `BlockSparseArray`;
that condition is now met (`GradedArrays` ships `AbstractGradedArray <:
AbstractArray` with its own `bipermutedimsopadd!` overload).
The companion `isempty(dest) && return dest` early return worked around a
broadcasting bug in `Strided.jl` fixed by QuantumKitHub/Strided.jl#50,
released in `Strided` 2.3.5. Adds `Strided` to `[deps]` (with
`using Strided: Strided` so it isn't reported as stale) and a
`Strided = "2.3.5"` compat floor, then drops the workaround.
Adds a regression test covering `Array{T, 0}` for `T` in `{Float64, BigFloat}`
with both `β = 0` (write-only) and `β ≠ 0` (accumulating) cases.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #173 +/- ##
==========================================
+ Coverage 80.37% 80.39% +0.02%
==========================================
Files 24 24
Lines 963 964 +1
==========================================
+ Hits 774 775 +1
Misses 189 189
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Downstream array types (notably `BlockSparseArray{T, 0}`) rely on the 0-dim
case being handled by direct `dest[]`/`src[]` accesses rather than going
through the permute-broadcast path. Without the short-circuit, the broadcast
path wraps the source in a 0-dim `PermutedDimsArray`, and `getindex` on that
wrapper is undefined for these array types
(`CanonicalIndexError: getindex not defined for PermutedDimsArray{T, 0, (), (), BlockSparseArray{T, 0, ...}}`).
Restoring the special case keeps the BLAS-style `β = 0` guard from the
previous commit so that `Array{BigFloat, 0}(undef)` destinations stay safe.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
bipermutedimsopadd!readdest[]unconditionally (β * dest[] + α * op(src[])), even whenβ = 0. By BLAS convention,β = 0meansdestis treated as write-only — its contents need not be defined. With element types whoseundefstorage is unreadable, the unguarded read crashed:bipermutedimsopadd!(Array{BigFloat, 0}(undef), identity, src, (), (), true, false)threwUndefRefError, since the unassigned slot of a 0-dim array of mutableBigFloatcannot be read. This surfaced via callers that allocate a 0-dim destination viasimilarand call intobipermutedimsopadd!withβ = 0(e.g. scalar contractions producing a 0-dim result).iszero(β)guard to the 0-dim branch to skip readingdestin the write-only case. The 0-dim short-circuit itself is kept: it lets downstream array types (e.g.BlockSparseArray{T, 0}) use directdest[]/src[]accesses instead of having to supportgetindexon a 0-dimPermutedDimsArraywrapper around their type.isempty(dest) && return destearly return worked around aStrided.jlbroadcasting bug fixed by QuantumKitHub/Strided.jl#50, released inStrided2.3.5. AddsStridedto[deps](withusing Strided: Stridedso it isn't reported as stale) and aStrided = "2.3.5"compat floor, then drops the workaround.Array{T, 0}forT ∈ {Float64, BigFloat}with bothβ = 0(write-only) andβ ≠ 0(accumulating) cases.