Skip to content

Fix vector/matrix product (@) silently overflowing for narrow value dtypes - #44

Merged
aarmey merged 1 commit into
mainfrom
fix-43-matmul-narrow-dtype-overflow
Sep 11, 2026
Merged

Fix vector/matrix product (@) silently overflowing for narrow value dtypes#44
aarmey merged 1 commit into
mainfrom
fix-43-matmul-narrow-dtype-overflow

Conversation

@aarmey

@aarmey aarmey commented Sep 11, 2026

Copy link
Copy Markdown
Member

Summary

  • vector @ VCSRArray / VCSRArray @ vector (and the 2-D @ matrix variants) allocated their output accumulator using values.dtype — the compact stored-element dtype (e.g. uint16 for single-cell counts) — instead of a dtype wide enough for the accumulated total, so results silently wrapped modulo 2**bits while sum() (which already accumulates in float64) stayed correct.
  • Fix: promote values and the other operand to their common np.result_type before running the _ops.py kernels, matching the dtype promotion an equivalent dense-array product would get.

Test plan

  • Added test_matmul_does_not_overflow_narrow_value_dtype in tests/test_ops.py, covering both @ directions and both vector/matrix operands, for both VCSRArray and VCSCArray, reproducing the issue's uint16 repro.
  • uv run pytest -q — 1662 passed, 109 skipped.
  • uv run ruff check — clean.

Fixes #43.

🤖 Generated with Claude Code

…types

_major_matvec/_minor_matvec/_major_matmat/_minor_matmat allocated their
output accumulator as values.dtype, the compact per-element storage
dtype (e.g. uint16 for single-cell counts), instead of a dtype sized
for the accumulated total. Contributions landing in the same output
slot wrapped modulo 2**bits instead of promoting, unlike sum() which
already accumulates in float64.

Promote values and the other operand to their common numpy dtype
(np.result_type) before running the kernels, matching the dtype
promotion an equivalent dense-array product would get.

Fixes #43.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@aarmey
aarmey merged commit aa171bd into main Sep 11, 2026
6 checks passed
@aarmey
aarmey deleted the fix-43-matmul-narrow-dtype-overflow branch September 11, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vector/matrix product (@) silently overflows for narrow value dtypes (e.g. uint16), unlike sum()

1 participant