Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow asarray to work on sequences of usm_array #1139

Merged
merged 9 commits into from
Mar 29, 2023

Conversation

oleksandr-pavlyk
Copy link
Collaborator

Closes gh-1134

In [1]: import dpctl.tensor as dpt, dpctl

In [2]: m = dpt.ones((2,4), dtype='i4')

In [3]: w = dpt.full(4, -1)

In [4]: v = dpt.zeros(4)

In [5]: dpt.asarray([m, [w, v]])
Out[5]:
usm_ndarray([[[ 1.,  1.,  1.,  1.],
              [ 1.,  1.,  1.,  1.]],

             [[-1., -1., -1., -1.],
              [ 0.,  0.,  0.,  0.]]], dtype=float32)
  • 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?
  • If this PR is a work in progress, are you filing the PR as a draft?

@github-actions
Copy link

@coveralls
Copy link
Collaborator

coveralls commented Mar 27, 2023

Coverage Status

Coverage: 82.857% (+0.05%) from 82.802% when pulling d294fb6 on fix-gh-1134-asarray into 068f65c on master.

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_24 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

@npolina4
Copy link
Collaborator

import dpctl.tensor as dpt
import dpnp

dpt.asarray([dpnp.ones(3)])

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/localdisk/work/npolina/ws/dpctl/dpctl/tensor/_ctors.py", line 543, in asarray
    seq_shape, seq_dt, devs = _array_info_sequence(obj)
  File "/localdisk/work/npolina/ws/dpctl/dpctl/tensor/_ctors.py", line 85, in _array_info_sequence
    el_dim, el_dt, el_dev = _array_info_dispatch(el)
  File "/localdisk/work/npolina/ws/dpctl/dpctl/tensor/_ctors.py", line 75, in _array_info_dispatch
    raise ValueError(type(obj))
ValueError: <class 'dpnp.dpnp_array.dpnp_array'>

@oleksandr-pavlyk
Copy link
Collaborator Author

import dpctl.tensor as dpt
import dpnp

dpt.asarray([dpnp.ones(3)])

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/localdisk/work/npolina/ws/dpctl/dpctl/tensor/_ctors.py", line 543, in asarray
    seq_shape, seq_dt, devs = _array_info_sequence(obj)
  File "/localdisk/work/npolina/ws/dpctl/dpctl/tensor/_ctors.py", line 85, in _array_info_sequence
    el_dim, el_dt, el_dev = _array_info_dispatch(el)
  File "/localdisk/work/npolina/ws/dpctl/dpctl/tensor/_ctors.py", line 75, in _array_info_dispatch
    raise ValueError(type(obj))
ValueError: <class 'dpnp.dpnp_array.dpnp_array'>

This should be fixed now:

In [1]: import dpctl.tensor as dpt, dpnp

In [2]: dpt.asarray([dpnp.ones(3)])
Out[2]: usm_ndarray([[1., 1., 1.]], dtype=float32)

In [3]: dpnp.asarray([dpnp.ones(3)])
Out[3]: array([[1., 1., 1.]], dtype=float32)

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_33 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

@npolina4
Copy link
Collaborator

>>> dpnp.array(dpnp.ones(0))
array([])
>>> dpnp.array([dpnp.ones(0)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nfs/site/home/npolina/work/ws/dpnp_bench/dpnp/dpnp/dpnp_iface_arraycreation.py", line 208, in array
    return dpnp_container.asarray(x1,
  File "/nfs/site/home/npolina/work/ws/dpnp_bench/dpnp/dpnp/dpnp_container.py", line 100, in asarray
    array_obj = dpt.asarray(x1_obj,
  File "/nfs/site/home/npolina/work/ws/dpctl/dpctl/tensor/_ctors.py", line 561, in asarray
    seq_shape, seq_dt, devs = _array_info_sequence(obj)
  File "/nfs/site/home/npolina/work/ws/dpctl/dpctl/tensor/_ctors.py", line 86, in _array_info_sequence
    el_dim, el_dt, el_dev = _array_info_dispatch(el)
  File "/nfs/site/home/npolina/work/ws/dpctl/dpctl/tensor/_ctors.py", line 74, in _array_info_dispatch
    usm_ar = _usm_ndarray_from_suai(obj)
  File "/nfs/site/home/npolina/work/ws/dpctl/dpctl/tensor/_ctors.py", line 226, in _usm_ndarray_from_suai
    membuf = dpm.as_usm_memory(obj)
  File "dpctl/memory/_memory.pyx", line 823, in dpctl.memory._memory.as_usm_memory
  File "dpctl/memory/_memory.pyx", line 208, in dpctl.memory._memory._Memory._cinit_other
  File "dpctl/memory/_sycl_usm_array_interface_utils.pxi", line 199, in dpctl.memory._memory._USMBufferData.from_sycl_usm_ary_iface
  File "dpctl/memory/_sycl_usm_array_interface_utils.pxi", line 97, in dpctl.memory._memory._pointers_from_shape_and_stride
ValueError: Array shape elements need to be positive

@oleksandr-pavlyk
Copy link
Collaborator Author

Thank for tracing this down, @npolina4 ! A simpler reproducer:

In [7]: dpt.asarray([dpt.ones(0)])
Out[7]: usm_ndarray([], dtype=float32)

In [8]: try:
   ...:    dpt.asarray([dpnp.ones(0)])
   ...: except ValueError as e:
   ...:     print(e)
   ...:
Array shape elements need to be positive

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_37 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

@npolina4
Copy link
Collaborator

In [1]: import dpnp

In [2]: x = dpnp.array([1, 2, 3], device='cpu')

In [3]: dpnp.asarray(x)
Out[3]: array([1, 2, 3])

In [4]: dpnp.asarray([x])

RuntimeError: Unrecogized typenum 17 encountered.

@oleksandr-pavlyk
Copy link
Collaborator Author

@npolina4 This must be a bug in dpnp, since it works as expected in dpctl.tensor:


In [1]: import dpctl.tensor as dpt

In [2]: x = dpt.asarray([1, 2, 3], device='cpu')

In [3]: class MockArray:
   ...:     def __init__(self, arr):
   ...:         self.arr_ = arr
   ...:     @property
   ...:     def __sycl_usm_array_interface__(self):
   ...:         return self.arr_.__sycl_usm_array_interface__
   ...:

In [4]: m = MockArray(x)

In [5]: dpt.asarray(m)
Out[5]: usm_ndarray([1, 2, 3])

In [6]: dpt.asarray([m])
Out[6]: usm_ndarray([[1, 2, 3]])

In [7]: dpt.asarray([m, m])
Out[7]:
usm_ndarray([[1, 2, 3],
             [1, 2, 3]])

dpnp array is being treated by dpctl.tensor as an object with __sycl_usm_array_interface__, and the above example works.

The type number 17 corresponds to type np.object_:

In [13]: np.dtype(np.object_).num
Out[13]: 17

Examples:

```
import dpctl.tensor as dpt

m = dpt.ones((2,4), dtype='i4')
w = dpt.zeros(4)
v = dpt.full(4, -1)

ar = dpt.asarray([m, [w, v]])
ar2 = dpt.asarray([m, [w, v]], device='cpu')
```
```
In [1]: import dpctl.tensor._tensor_impl as ti, dpctl.tensor as dpt, dpnp

In [2]: dpt.asarray([dpnp.ones(3)])
Out[2]: usm_ndarray([[1., 1., 1.]], dtype=float32)

In [3]: dpnp.asarray([dpnp.ones(3)])
Out[3]: array([[1., 1., 1.]], dtype=float32)
```
npolina4
npolina4 previously approved these changes Mar 29, 2023
f"does not support {dtype} natively."
)
dtype = _mapped_dt
if order in "KA":
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
if order in "KA":
if order in ("K", "A"):

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using str appears faster overall:

In [1]: t = "C"

In [2]: %timeit t in ("K", "A")
40.3 ns ± 0.768 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [3]: %timeit t in "KA"
26.9 ns ± 0.518 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [4]: %timeit t in ("K", "A")
40.3 ns ± 0.486 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [5]: %timeit t in "KA"
27.6 ns ± 1.17 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [6]: t = "K"

In [7]: %timeit t in ("K", "A")
25.7 ns ± 0.39 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [8]: %timeit t in "KA"
26.2 ns ± 0.488 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_39 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_39 ran successfully.
Passed: 45
Failed: 789
Skipped: 280

@npolina4
Copy link
Collaborator

import dpnp
import dpctl.tensor as dpt

x = dpnp.ones((3,3), device='cpu')
dpt.asarray([x], device='gpu')
RuntimeError: Unrecogized typenum 17 encountered.

@oleksandr-pavlyk
Copy link
Collaborator Author

Thank you @npolina4 for thorough review. I have fixed the underlying issue.

The reported error can be reproduced without involvement of dpnp as follows:

import dpctl.tensor as dpt, numpy as np
u = dpt.empty(5)
n = np.empty(5, dtype="O")
u[...] = n   # raises  RuntimeError: Unrecogized typenum 17 encountered. which trips cascade of other exceptions 

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_41 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

@oleksandr-pavlyk oleksandr-pavlyk merged commit 3352c3d into master Mar 29, 2023
@oleksandr-pavlyk oleksandr-pavlyk deleted the fix-gh-1134-asarray branch March 29, 2023 23:33
@github-actions
Copy link

Deleted rendered PR docs from intelpython.github.com/dpctl, latest should be updated shortly. 🤞

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_46 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

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.

dpctl.tensor.asarray must support sequences of usm_ndarrays
3 participants