Skip to content

usm_ndarray object special methods, and other changes #586

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

Merged
merged 2 commits into from
Sep 23, 2021

Conversation

oleksandr-pavlyk
Copy link
Contributor

@oleksandr-pavlyk oleksandr-pavlyk commented Sep 15, 2021

Use of public keyword when declaring usm_ndarray Cython class amounted to Cython exposing private struct members to Python (such usm_ndarray.typenum_, usm_ndarray.nd_, etc)

Keeping only api (no public), these members are not longer exposed, while the python object and its type remain exposed.

Added __pyx_capi__ functions to access these private members, paving the road to using these classes in C/C++ extensions, e.g. built with help of pybind11.

Added tests to test these __pyx_capi__ functions.

This PR has also grown to add __setitem__ special method to usm_ndarray class, as well as dpctl.tensor.astype(array, new_dtype, order="K", copy=True) and dpctl.tensor.reshape(array, new_shape, order="C").

usm_ndarray can also be reshaped using shape setter if this can be done as a view.

@coveralls
Copy link
Collaborator

coveralls commented Sep 15, 2021

Coverage Status

Coverage increased (+1.3%) to 74.041% when pulling 936bd69 on usm_ndarray_private into ebc8b6d on master.

@oleksandr-pavlyk oleksandr-pavlyk changed the title usm_ndarray used to be declared public api, not just api usm_ndarray used to be declared public api, now just api Sep 16, 2021
@oleksandr-pavlyk oleksandr-pavlyk force-pushed the usm_ndarray_private branch 14 times, most recently from ed4c5b4 to a3e209f Compare September 23, 2021 11:11
public keyword amounted to Cython exposing private struct
members to Python (such usm_ndarray.typenum_, usm_ndarray.nd_, etc)

Keeping only api (no public), these members are not longer exposed.

Added __pyx_capi__ function to access these private members.

Added tests to test __pyx_capi__ functions
Implemented copy based on Python API to _Memory.memcpy (slow) as a reference implementation.
Implemented `dpctl.tensor.reshape` and `dpctl.tensor.astype`, and usm_ndarray.shape setter.

Full signature follows NumPy:

```
  dpt.astype(usm_ary, new_type, order="K", casting="unsage", copy=True)
```

  order can be "K", "C", "F" or "A"
  casting can be what is accepted by np.can_cast
  copy: True/False (if True, a copy is always made)

Added member attribute of the array storing array namespace, which can be set by
dpctl.tensor.usm_ndarray._set_namespace. This namespace is used to relay special method
calls, e.g. __abs__ translates into namespace.abs().

Presently, namespace defaults to None, which is interpreted as use `dpnp` is loaded.
If no function to relay to is available, NotImplemented object is returned.

added internal attribute `._pointer` to get array pointer corresponding to zero indexes.

Implemented `dpct.tensor.to_numpy(usm_ary)` and
`dpctl.tensor.from_numpy(np_ary, usm_type="device", queue=None)`.

Added tests to ensure coverage of > 80%.

```
In [1]: import dpnp
Running on: Intel(R) UHD Graphics [0x9bca]
DPCtrl SYCL queue used
SYCL kernels link time: 5.94e-07 (sec.)
Math backend version: Intel(R) oneAPI Math Kernel Library Version 2021.3-Product Build 20210617 for Intel(R) 64 architecture applications

In [2]: import dpctl.tensor as dpt

In [3]: import numpy as np

In [4]: X = dpt.from_numpy(np.array([1.2, 0.3, 2.4], dtype='d'))

In [5]: Y = dpt.from_numpy(np.array([-0.8, 0.6, -1.8], dtype='d'))

In [6]: X + Y  # works by falling back to dpnp.add
Out[6]: <dpctl.tensor._usmarray.usm_ndarray at 0x7f8be3b3ead0>

In [7]: dpt.to_numpy(_)
Out[7]: array([0.4, 0.9, 0.6])
```

Modularized  __pyx_capi__, also added tests for exported constants.

Closes #591
Closes #592

N.B.:
Cython version 0.* generates its extensions with quirky behavior of
special methods as documented in

https://stackoverflow.com/questions/33218006/correct-override-of-rmul-in-cython-extensions
http://docs.cython.org/en/latest/src/userguide/special_methods.html#arithmetic-methods

Essentially, even if such Cython extension implemented __rmul__ special method, it never
gets called by CPython, instead __mul__ is called with swapped arguments.

Therefore, ``def __mul__(self, other)`` can not assume that `self` is an instance of
implementor.

This behavior changes in Cython 3 (currently in alpha). There, `__rmul__` will get called,
and first argument of `__mul__` would be guaranteed to be an instance of the class implementing
the special method.
@oleksandr-pavlyk oleksandr-pavlyk changed the title usm_ndarray used to be declared public api, now just api usm_ndarray object special methods, and other changes Sep 23, 2021
@oleksandr-pavlyk oleksandr-pavlyk merged commit 002d24b into master Sep 23, 2021
@oleksandr-pavlyk oleksandr-pavlyk deleted the usm_ndarray_private branch September 23, 2021 14:09
Copy link

@Alexander-Makaryev Alexander-Makaryev left a comment

Choose a reason for hiding this comment

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

👍

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.

3 participants