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

Add methods __bool__, __float__ and __int__ to usm_ndarray #578

Merged
merged 9 commits into from
Sep 13, 2021

Conversation

densmirn
Copy link
Contributor

@densmirn densmirn commented Sep 8, 2021

Closes #545

@coveralls
Copy link
Collaborator

coveralls commented Sep 8, 2021

Coverage Status

Coverage increased (+0.3%) to 72.733% when pulling 1f34ee8 on scalar_copy into b97efdf on master.

@oleksandr-pavlyk
Copy link
Collaborator

@densmirn can you also implement __index__?

See data-apis/array-api#231

@oleksandr-pavlyk
Copy link
Collaborator

import dpctl.tensor as dpt
X = dpt.usm_ndarray(10, 'd')
float(X[:1])   # raises TypeError

This works with NumPy:

import numpy as np
X = np.ndarray(10, 'd')
float(X[:1])   # produces a float 

It breaks with the current code because Xusm.usm_data gives the container underlying the view which may be bigger than the view:

X = dpt.usm_ndarray(10, 'd')
X[:1].usm_data.copy_to_host().view('d').shape  # outputs (10,)

Thus instead of self.usm_data.copy_to_host().view('d').__float__() we should do self.usm_data.copy_to_host().view(self.dtype)[:0].__float__().

Ideally, in a future, we should avoid copying the entire underlying buffer.

@densmirn
Copy link
Contributor Author

densmirn commented Sep 9, 2021

Actually slice of array can be different, e.g.:

import numpy as np
X = np.arange(10)
float(X[:1])  # 0.0
float(X[2:3])  # 2.0

So when we copy data to host we don't know the slice:
self.usm_data.copy_to_host().view(self.dtype)[???].__float__()

Looks like it should be somehow handled on __getitem__ side.

@densmirn
Copy link
Contributor Author

@oleksandr-pavlyk could you please look at last comment?

@oleksandr-pavlyk
Copy link
Collaborator

This is affected by #583

densmirn and others added 6 commits September 12, 2021 19:09
np.number comprises np.integer and np.inexact, so replace
issubdtype(dt.type, np.inexact) or issubdtype(dt.type, np.number)
with more efficient issubdtype(dt.type, np.number).

Also allowed dtype.type to be np.bool_, to accommodate dtype="|b1"

Replaced single quotation marks with double quotation marks per
linter's flavor.
@oleksandr-pavlyk
Copy link
Collaborator

This PR(tests) have uncovered another problem which I fixed.

It was inability of memory objects to understand objects with __sycl_usm_array_interface__ with 'typestr': '|b1'.

Also added tests to cover new functionality.

@oleksandr-pavlyk
Copy link
Collaborator

I also added __complex__ magic (a.k.a. dunder) method.

@oleksandr-pavlyk oleksandr-pavlyk merged commit 34ce6b7 into master Sep 13, 2021
@oleksandr-pavlyk oleksandr-pavlyk deleted the scalar_copy branch September 13, 2021 17:08
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.

0d usm_ndarray must allow conversion to Python scalars
3 participants