Skip to content

Conversation

@vlad-perevezentsev
Copy link
Collaborator

This PR partially aligns usm_ndarray scalar conversion semantics with the Python Array API by allowing numeric scalar conversion only for 0D usm_ndarrays

Conversions to Python numeric scalars via int, float, complex and their corresponding methods (__int__, _float__, __complex__) now raise TypeError when applied to usm_ndarrays with ndim !=0

Note:
For full compliance with the Python Array API, the same check should be used for truth values __bool__ method and integer indexing index method
But these changes would impact dpnp compatibility with Numpy 2.4 which currently disallows only numeric scalar conversation (a minor behavioral difference I think)
In addition several dpctl tests rely on implicit truth conversion for non-0D arrays and must be updated accordingly

test_usm_ndarray_indexing.py::test_nonzero_f_contig
test_usm_ndarray_indexing.py::test_nonzero_compacting
test_usm_ndarray_manipulation.py::test_tile_size_1 
test_usm_ndarray_operators.py::test_comp_ops[dpctl.tensor]
test_usm_ndarray_operators.py::test_comp_ops[namespace1]
test_usm_ndarray_sorting.py::test_radix_sort_size_1_axis 
test_usm_ndarray_sorting.py::test_radix_argsort_size_1_axis 
  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?
  • If this PR is a work in progress, are you opening the PR as a draft?

@vlad-perevezentsev vlad-perevezentsev self-assigned this Dec 22, 2025
@github-actions
Copy link

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.22.0dev0=py310h93fe807_103 ran successfully.
Passed: 1114
Failed: 44
Skipped: 82

@coveralls
Copy link
Collaborator

coveralls commented Dec 22, 2025

Coverage Status

coverage: 86.245%. remained the same
when pulling 239e5b1 on disallow_conv_to_scalar_ndim
into 4c57ee7 on master.

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.22.0dev0=py310h93fe807_108 ran successfully.
Passed: 1112
Failed: 46
Skipped: 82

cdef inline void _check_0d_scalar_conversion(object usm_ary) except *:
"Raise TypeError if array cannot be converted to a Python scalar"
if (usm_ary.ndim != 0):
raise TypeError(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
raise TypeError(
raise ValueError(

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess NumPy raises a TypeError but this really seems like a ValueError to me

assert not X.__le__(-1)
assert not X.__eq__(-1)
assert X.__ne__(-1)
assert dpt.all(X.__gt__(-1))
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of adding these additional kernel launches, we can just either strip the dimension off of X or we can use dpt.asarray above, so that X is an array scalar

x2 = dpt.ones([1], dtype="i1")
r2 = dpt.argsort(x2, kind="radixsort")
assert r2 == 0
assert dpt.all(r2 == 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

similar to the other test, we can save a kernel launch by stripping the dimension off of r2. It's not too bad of a hack I feel.

Copy link
Collaborator

@ndgrigorian ndgrigorian left a comment

Choose a reason for hiding this comment

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

aside from minor comments this LGTM

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.

4 participants