Keep broadcasts over views of dense arrays sparse - #772
Open
ViralBShah wants to merge 1 commit into
Open
Conversation
`is_supported_sparse_broadcast` only whitelisted `Array`, so any `SubArray` of a dense array fell out of the `PromoteToSparse` path and diverted the whole broadcast to the generic dense code: `A .* B` returned a `SparseMatrixCSC` but `A .* view(B, :, :)` returned a `Matrix`. Recurse through `SubArray` to its parent, the same way `Transpose`/`Adjoint` are already handled. Views of sparse arrays are covered by this too. Fixes #508. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L8HenXTVrASo1sBZVGhpDQ
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #772 +/- ##
=======================================
Coverage 84.19% 84.19%
=======================================
Files 13 13
Lines 9403 9404 +1
=======================================
+ Hits 7917 7918 +1
Misses 1486 1486 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes #508.
Broadcasting a sparse array against a dense
Matrixstays sparse, but doingthe same against a view of that matrix silently produced a dense result:
The cause is
is_supported_sparse_broadcast, which whitelistsArraybutnot
SubArray. A view therefore answeredfalse, andcopy(::Broadcasted{PromoteToSparse})diverted the whole expression to the generic dense path — losing both the
sparse result type and the structural zeros of
A.This recurses through
SubArrayto its parent, exactly asTranspose/Adjointare already handled one line above, so a view is supported whenever the array
it views is. That covers non-strided views (
view(B, [1,3,5], :)) as well asstrided ones, and picks up views of sparse arrays at the same time. Arrays
that were unsupported before still divert to the dense fallback.
Tests cover dense views, adjoints of views, sparse views,
broadcast!, andthat a zero-preserving op no longer fills in structural zeros.
🤖 Generated with Claude Code
https://claude.ai/code/session_01L8HenXTVrASo1sBZVGhpDQ