Skip to content

fix: return 0-D array for full reductions per Array API standard#932

Open
Abineshabee wants to merge 5 commits intopydata:mainfrom
Abineshabee:fix/array-api-reduction-returns-0d-array
Open

fix: return 0-D array for full reductions per Array API standard#932
Abineshabee wants to merge 5 commits intopydata:mainfrom
Abineshabee:fix/array-api-reduction-returns-0d-array

Conversation

@Abineshabee
Copy link
Copy Markdown

Fixes #921

Problem

sparse.sum() and all other full reductions (max, min, prod, mean, any, all, einsum) were returning a NumPy scalar instead of a 0-D array, violating the Array API standard.

Root Cause

SparseArray.reduce() had if out.ndim == 0: return out[()] which unwrapped the 0-D COO into a scalar. _einsum_single() also had two explicit scalar return paths.

Changes

  • _sparse_array.py — remove scalar unwrap in reduce(), unify mean() path
  • _common.py — fix both scalar paths in _einsum_single()
  • test_einsum.py — update scalar-output branches to use .todense()
  • test_array_function.py — add TestArrayAPIReductions with 16 regression tests

Testing

6079 passed, 0 failed

Abineshabee and others added 2 commits March 22, 2026 10:47
Fixes pydata#921

- Remove if out.ndim == 0: return out[()] in SparseArray.reduce()
  which was converting 0-D COO arrays into NumPy scalars
- Fix mean() to handle 0-D result uniformly without scalar branch
- Fix _einsum_single() two scalar escape hatches to return 0-D COO
- Update 	est_einsum.py scalar branches to use .todense()
- Add TestArrayAPIReductions with 16 regression tests
@github-actions github-actions bot added the fix label Mar 22, 2026
@hameerabbasi
Copy link
Copy Markdown
Collaborator

One thing that can be improved here: out[()] itself should just return out, never a NumPy scalar.

@Abineshabee
Copy link
Copy Markdown
Author

Fixed out[()] scalar unwrap — reduce() and var() now return 0-D sparse arrays directly per Array API standard. All 316 tests passing. @hameerabbasi please re-review!

@Abineshabee
Copy link
Copy Markdown
Author

Please let me know if any further changes are needed!

Copy link
Copy Markdown
Collaborator

@hameerabbasi hameerabbasi left a comment

Choose a reason for hiding this comment

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

Just one small concern.

mean along all axes.

>>> s.mean()
np.float64(0.5)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Huh? s.mean() should produce SOME output.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 3, 2026

Merging this PR will degrade performance by 54.75%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 339 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
test_index_slice[side=100-rank=2-format='gcxs'] 1.7 ms 3.8 ms -54.75%

Comparing Abineshabee:fix/array-api-reduction-returns-0d-array (9304ca7) with main (3589a7c)

Open in CodSpeed

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.

array API standard: return an array for sparse.sum of a 2-D array

2 participants