Skip to content

fix: preserve exact unsigned COO bounds checks - #1029

Open
Doribelove wants to merge 1 commit into
pydata:mainfrom
Doribelove:fix/uint64-coo-bounds
Open

Doribelove wants to merge 1 commit into
pydata:mainfrom
Doribelove:fix/uint64-coo-bounds

Conversation

@Doribelove

Copy link
Copy Markdown
Contributor

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • 🪄 Feature
  • 🐞 Bug Fix
  • 🔧 Optimization
  • 📚 Documentation
  • 🧪 Test
  • 🛠️ Other

Related issues

Regression introduced by #1015, found while updating the unsigned-coordinate regression in #962.

Checklist

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

Please explain your changes below.

COO construction rejects valid uint64 coordinates near large dimension bounds. On current main (9fbb898), this single-element array raises ValueError: ... out of bounds indices:

import numpy as np
import sparse

size = 2**54
x = sparse.COO(np.array([[size - 1]], dtype=np.uint64), [3], shape=(size,))

The new validator compares unsigned coordinates with signed intp bounds. Numba promotes that combination to floating point, making size - 1 indistinguishable from size. Use uint64 bounds for unsigned coordinates, and retain intp bounds for other dtypes. The bounds are not narrowed to the coordinate dtype, so a uint8 coordinate can still belong to an axis larger than 255. Coordinates and output dtypes are unchanged.

The 29 regression/control cases cover the float64 exact-integer boundary, the intp limit, genuinely out-of-range uint64 values, narrow signed/unsigned types, negative coordinates, and existing float-coordinate rejection. Three cases fail before the fix; all 29 pass afterward. No large dense arrays are allocated.

Validation on Linux x86_64, Python 3.12.14, NumPy 2.5.3 and Numba 0.67.0:

  • Complete COO test module: 3,461 passed, 80 xfailed, 33 xpassed. Existing non-strict xfail markers are unchanged.
  • Numba backend tests and module doctests: 7,160 passed, 1 skipped, 115 xfailed, 39 xpassed.
  • Repository pre-commit hooks and git diff --check pass.

Finch/MLIR, the external Array API suite, other versions and other platforms were not tested locally.

Use uint64 bounds when validating unsigned coordinate arrays to prevent
Numba from promoting mixed uint64/intp comparisons to float64. Keep
bounds wide enough for narrow coordinate arrays and retain signed checks.

Cover large valid coordinates, exact upper-bound rejection, uint64 values
above intp.max, narrow integer coordinates, negatives and existing float
rejection. The three precision regressions fail on main before this fix.

Signed-off-by: 李永祺 <doribelove@gmail.com>
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