Skip to content

Return the dense result of a broadcast whose index type cannot address every entry - #1086

Merged
ViralBShah merged 1 commit into
mainfrom
vs/dense-broadcast-index-type
Oct 13, 2026
Merged

ViralBShah merged 1 commit into
mainfrom
vs/dense-broadcast-index-type

Conversation

@ViralBShah

Copy link
Copy Markdown
Member

A broadcast whose function is not zero at the zeros returns a dense array, but it is computed by the sparse kernels, which store every entry in a sparse array with the index type of the arguments. When that index type cannot address every entry, the call throws although the dense result is representable.

S = sparse(Int8[1], Int8[1], [1.0], 12, 12)   # 144 entries, `Int8` indices
R = cos.(S)

Before

ERROR: InexactError: trunc(Int8, 133)

After

julia> typeof(R), R == cos.(Array(S))
(Matrix{Float64}, true)

_densenonzero now asks, from the promoted index type of the arguments and the shape of the result, whether that type addresses every entry, and otherwise hands the broadcast to _densebroadcast, as it already does for a Bool or non-concrete element type. The check is on a type and folds away, so cos.(A) for an Int-indexed matrix stays inferred as Matrix{Float64}; at 10^6 entries its time (2.0 ms) and allocations (16.0 MB) are the same as on main.

Left out: the in-place forms on the same matrix (S .= 1.0, map!), which fail for the same reason in a sparse destination and are a separate change.

Split from #1022. A bug fix with no change of result type, so a candidate for backporting.

Written by Claude Code.

🤖 Generated with Claude Code

…s every entry

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.79%. Comparing base (c0f75f0) to head (3a97dbb).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1086      +/-   ##
==========================================
+ Coverage   94.75%   94.79%   +0.03%     
==========================================
  Files          16       16              
  Lines        9885     9886       +1     
==========================================
+ Hits         9367     9371       +4     
+ Misses        518      515       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ViralBShah ViralBShah left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed by Codex at 3a97dbb.

No actionable findings. Checking the promoted sparse index type against the broadcast result shape before entering the fully stored sparse kernel addresses the narrow-index failure; the fallback preserves the dense result container.

@ViralBShah
ViralBShah merged commit 8fbd854 into main Oct 13, 2026
10 checks passed
@ViralBShah
ViralBShah deleted the vs/dense-broadcast-index-type branch October 13, 2026 22:46
@ViralBShah ViralBShah added the backport 1.13 Change should be backported to release-1.13 label Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 1.13 Change should be backported to release-1.13

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant