test_trace sporadically fails under numpy
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
Research direction
Start with array_api_tests/test_linalg.py::test_trace and reproduce the failure using the reported Hypothesis example. Check how generated values and dtypes reach xp.linalg.trace, then adjust the test inputs or dtype strategy so the test avoids invalid overflow cases and remains stable across NumPy; done means test_trace passes reliably.
Written by the indexing model from the issue text.
Description
Caught in https://github.com/data-apis/array-api-compat/actions/runs/31951617577/job/95175816104?pr=464 : the values are very different:
FAILED array_api_tests/test_linalg.py::test_trace - AssertionError: The input arrays have different values (array(2048, dtype=uint32) != np.uint32(0)) (x_idxes = [(slice(None, None, None), slice(None, None, None))], res_idx = ())
adding @reproduce_failure('6.165.9', b'AEEIAEEEQQQoQfAAABAQAAAAAAAAAUEC'), as suggested by hypothesis shows this:
(Pdb) p x
array([[4.294968e+09, 4.294968e+09, 4.294968e+09, 4.294968e+09],
[4.294968e+09, 4.294968e+09, 4.294968e+09, 4.294968e+09],
[4.294968e+09, 4.294968e+09, 4.294968e+09, 4.294968e+09],
[4.294968e+09, 4.294968e+09, 4.294968e+09, 4.294968e+09]],
dtype=float32)
(Pdb) kw
{'dtype': <class 'numpy.uint32'>}
(Pdb) p xp.linalg.trace(x)
array(1.7179871e+10, dtype=float32) # correct value
(Pdb) p xp.linalg.trace(x, **kw)
array(2048, dtype=uint32) # incorrect
(Pdb) p xp.sum(np.diag(x), **kw) # trace matches sum
np.uint32(2048)
So what happens is that the summation is forced to occur in int32 and it overflows. The behavior comes straight from numpy.
The test effectively casts a float32 array to int32, which is not a very reasonable thing to do, and the (wrong) results depend on the order of operations in fairly unpredictable manner:
(Pdb) p xp.linalg.trace(xp.astype(x, xp.int32))
array(-8589934592)
(Pdb) p xp.linalg.trace(xp.astype(x, xp.int32), dtype=xp.int32)
array(0, dtype=int32)
What to do about it at the test suite level? Not entirely sure. We can either limit the range of values of x or only draw flowing-point dtype for floating-point x, for example.
- Dominant language
- Python
- Stars
- 74
- Forks
- 54
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 6
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from data-apis/array-api-tests
-
Difficulty 2/5 1-3 hours Newbie friendliness 87/100
data-apis/array-api-tests#465 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
data-apis/array-api-tests#460 · 1 comment ·
-
tracking
Difficulty 5/5 Over a week Newbie friendliness 25/100
data-apis/array-api-tests#381 · 9 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 38/100
data-apis/array-api-tests#379 · 10 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
data-apis/array-api-tests#368 · 3 comments ·
All issues in data-apis/array-api-tests
Similar issues
-
documentation help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
simonw/sqlite-utils#872 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100