Repository navigation
Skip the dense copy of the sparse arguments in a sparse-plus-scalar broadcast such as A .+ 1 - #1105
Open
ViralBShah wants to merge 1 commit into
Open
ViralBShah wants to merge 1 commit into
ViralBShah wants to merge 1 commit into
Conversation
…roadcast such as `A .+ 1` Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1105 +/- ##
==========================================
- Coverage 94.83% 94.77% -0.06%
==========================================
Files 16 16
Lines 10252 10300 +48
==========================================
+ Hits 9722 9762 +40
- Misses 530 538 +8 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
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.
Since #1004, a broadcast sum of a sparse array and a scalar, such as
A .+ 1, is dense. It gets there by copying every sparse argument into a dense array, then broadcasting over the copies into a second dense array. For a sparse argument with few stored entries, that copy is half the memory and more than half the time.Before (
main; randomn×nsparseA,Bof densityd; minimum time over 20 runs)A .+ 1A .+ B .- 2After
A .+ 1A .+ B .- 2Sums with a dense array, such as
A .+ Mandv .+ M, are unchanged.The new path evaluates the broadcast once with each sparse argument replaced by its implicit zero. That fills every entry of the result that no stored entry reaches. It then evaluates the broadcast again only at the entries a stored entry is broadcast to. The function is a tree of
+and-, so evaluating it twice at an entry is unobservable. Results match dense broadcast in type andisequal, including NaN and-0.0. I checked that against dense on 36 expressions over Float64, ComplexF64, Bool and Int entries, with sparse vectors and 1-row and 1-column matrices broadcast across the result.Each re-evaluated entry looks up every sparse argument. So the new path runs only while the entries reached, times the number of sparse arguments, stay below 1/32 of the result; otherwise the dense copy is cheaper. Above that density, as in the 0.5 row and for
A .+ B .- 2at 0.01, the old path runs.Left out:
Number. Their implicit zero can have another type, which would change the result type.🤖 Generated with Claude Code