Skip to content

fix: broadcast fill-only COO arrays without allocating a coordinate grid - #1035

Open
Doribelove wants to merge 1 commit into
pydata:mainfrom
Doribelove:perf/empty-broadcast
Open

Doribelove wants to merge 1 commit into
pydata:mainfrom
Doribelove:perf/empty-broadcast

Conversation

@Doribelove

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • 🐞 Bug Fix
  • 🔧 Optimization
  • 🧪 Test

Related issues

#1034 covers incompatible target ranks. The example below uses a compatible shape and an input with no stored entries.

Changes

Broadcasting a fill-only COO creates a Cartesian product of indices even though the result has no coordinates to store. On d0da78b, a scalar or a (1, 1) input fails on this example while constructing the same fill-only target directly succeeds.

import numpy as np
import sparse

x = sparse.full((1, 1), 7, dtype=np.int16)
y = sparse.broadcast_to(x, (1024,) * 6)
assert y.coords.shape == (6, 0) and y.fill_value == 7

Return empty coordinate and data buffers before building the grid when the input has no stored values. A scalar with one explicitly stored value still expands it across the target. The COO method documentation describes the fill-only behavior.

Validation

  • The two large-shape regressions failed on the base commit. Both pass with this change, as does the stored-scalar control.
  • For a (1, 1) fill-only input broadcast to (1024, 1024), median peak traced allocation fell from 33,556,627 to 2,235 bytes across seven warmed calls on Linux with Python 3.12.14 and NumPy 2.5.3. A separate CodSpeed benchmark covers a (512, 512) target.
  • Compared 3,200 small shape and input combinations with NumPy. The 660 compatible results matched NumPy in values and dtype, kept the input fill value, and stored exactly the elements different from it. The remaining 2,540 raised ValueError in both libraries.
  • The Numba backend suite with doctests finished with 7,458 passed, 1 skipped, 96 expected failures and 58 unexpected passes. Pre-commit passed on the changed files.

Checklist

  • Code follows style guide
  • Tests added
  • Documented the changes

Signed-off-by: 李永祺 <doribelove@gmail.com>
@github-actions github-actions Bot added the fix label Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant