Skip to content

Compact sparse-vector broadcast output on the device - #795

Merged
maleadt merged 1 commit into
mainfrom
tb/sparse-vector-broadcast
Oct 23, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/sparse-vector-broadcast

Conversation

@maleadt

@maleadt maleadt commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Broadcasting over several sparse vectors sorted an array of Pairs with @allowscalar sort!(offsets; by=first) to move the rows without an output entry to the end. Back-ends whose device sort cannot handle Pairs fall back to Base's algorithm through scalar indexing: on Metal, x .* y for two 4096-element vectors at 10% density took 7.3 s.

The rows are already in order, so this numbers the rows that have an output entry with a prefix sum (accumulate!) and lets the broadcast kernel write each entry to its position. It also drops the scalar reads of the first and last stored index that narrowed the row range: they threw a BoundsError for vectors without stored entries, and left the dense-output kernel reading past the end of the offsets. Every row is processed instead, and each argument's entry is found by binary search rather than a linear scan.

Tests: vectors without stored entries (which failed before), and a 10⁴-element case with several sparse arguments.

This is independent of the sparse redesign in #745 (stacked above it) and can go into 11.x on its own.

@maleadt
maleadt added this pull request to stack #797 September 25, 2026 14:56
@maleadt maleadt changed the title tb/sparse vector broadcast Compact sparse-vector broadcast output on the device Sep 25, 2026
@maleadt maleadt added arrays The array abstraction: broadcasting, mapreduce, indexing, views, generic linear algebra. performance Runtime performance. labels Oct 19, 2026
@maleadt
maleadt removed this pull request from stack #797 October 21, 2026 13:00
@maleadt
maleadt added this pull request to stack #832 October 21, 2026 13:00
Broadcasting several sparse vectors sorted an array of Pairs on the host
through scalar indexing to move the rows without output entries to the
end, which takes seconds on back-ends whose sort cannot handle Pairs
(Metal). The rows are already in order, so number the rows that have an
output entry with a prefix sum instead, and let the broadcast kernel
write each entry to its position.

The first and last stored index were also read with scalar indexing to
narrow the row range, which threw a BoundsError for vectors without
stored entries and left the dense-output kernel reading past the end of
the offsets. Process every row instead, and find each argument's entry
by binary search rather than a linear scan.
@maleadt
maleadt force-pushed the tb/sparse-vector-broadcast branch 2 times, most recently from 55a9018 to 6d1f474 Compare October 22, 2026 10:54
@maleadt
maleadt removed this pull request from stack #832 October 23, 2026 08:18
@maleadt
maleadt marked this pull request as ready for review October 23, 2026 13:05
@maleadt
maleadt merged commit 37c2516 into main Oct 23, 2026
18 checks passed
@maleadt
maleadt deleted the tb/sparse-vector-broadcast branch October 23, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrays The array abstraction: broadcasting, mapreduce, indexing, views, generic linear algebra. performance Runtime performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant