ENH: Adding dtype parameter to the test for linalg.trace#441
Open
prady0t wants to merge 2 commits into
Open
Conversation
Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
tril, triu and adding dtype parameter to the test for linalg.tracetril, triu and adding dtype parameter to the test for linalg.trace
prady0t
commented
Jul 12, 2026
ev-br
requested changes
Jul 13, 2026
ev-br
left a comment
Member
There was a problem hiding this comment.
Nice!
Let's split the PR into two though: one adding tril/triu tests, and a separate one for trace. Both need some work, too, per array-api-compat testing below.
| # that are way larger than the array shape isn't very important. | ||
| kw=kwargs(offset=integers(-MAX_ARRAY_SIZE, MAX_ARRAY_SIZE)) | ||
| kw=kwargs(offset=integers(-MAX_ARRAY_SIZE, MAX_ARRAY_SIZE), | ||
| dtype=sampled_from(dh.numeric_dtypes)) |
Member
There was a problem hiding this comment.
Running
$ ARRAY_API_TESTS_SKIP_DTYPES=uint16,uint32,uint64 ARRAY_API_TESTS_MODULE=array_api_compat.torch pytest array_api_tests/test_linalg.py::test_trace -sv --max-examples=10_000
emits
array_api_tests/test_linalg.py::test_trace
/home/br/repos/array-api-compat/src/array_api_compat/torch/_aliases.py:360: UserWarning: Casting complex values to real discards the imaginary part (Triggered internally at /pytorch/aten/src/ATen/native/Copy.cpp:308.)
return torch.sum(x, axis, dtype=dtype, keepdims=keepdims, **kwargs)
With numpy, a similar stanza,
$ ARRAY_API_TESTS_SKIP_DTYPES=uint16,uint32,uint64 ARRAY_API_TESTS_MODULE=array_api_compat.numpy pytest array_api_tests/test_linalg.py::test_trace -sv --max-examples=10_000
generates both a ComplexWarning, and a failure:
array_api_tests/test_linalg.py::test_trace FAILED
======================================= FAILURES =======================================
______________________________________ test_trace ______________________________________
@pytest.mark.unvectorized
> @pytest.mark.xp_extension('linalg')
array_api_tests/test_linalg.py:918:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
x = array([[0]], dtype=uint8), kw = {}
@pytest.mark.unvectorized
@pytest.mark.xp_extension('linalg')
@given(
x=arrays(dtype=numeric_dtypes, shape=matrix_shapes()),
# offset may produce an overflow if it is too large. Supporting offsets
# that are way larger than the array shape isn't very important.
kw=kwargs(offset=integers(-MAX_ARRAY_SIZE, MAX_ARRAY_SIZE),
dtype=sampled_from(dh.numeric_dtypes))
)
def test_trace(x, kw):
res = linalg.trace(x, **kw)
dtype = kw.get("dtype", None)
expected_dtype = dh.accumulation_result_dtype(x.dtype, dtype)
if expected_dtype is None:
# If a default uint cannot exist (i.e. in PyTorch which doesn't support
# uint32 or uint64), we skip testing the output dtype.
# See https://github.com/data-apis/array-api-tests/issues/160
if x.dtype in dh.uint_dtypes:
> assert dh.is_int_dtype(res.dtype) # sanity check
E AssertionError: assert False
E + where False = <function is_int_dtype at 0x7f85b7900e00>(dtype('uint64'))
E + where <function is_int_dtype at 0x7f85b7900e00> = dh.is_int_dtype
E + and dtype('uint64') = array(0, dtype=uint64).dtype
E Falsifying example: test_trace(
E x=_f(
E <...>, # or any other generated value
E False, # or any other generated value
E ),
E kw={},
E )
E Explanation:
E These lines were always and only run by failing examples:
E /home/br/repos/array-api-tests/array_api_tests/dtype_helpers.py:323
array_api_tests/test_linalg.py:936: AssertionError
Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
tril, triu and adding dtype parameter to the test for linalg.tracedtype parameter to the test for linalg.trace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found in #439 (Please also refer to it for all missing coverage reports)
This PR, along with #442, adds tests for
trilandtriu. Originally, only their signature tests were available. Also, in the above findings,linalg.tracewasn't tested on non-default arguments ofdtype(only None); this PR addresses that.