Skip to content

Add methods __bool__, __float__ and __int__ to usm_ndarray#578

Merged
oleksandr-pavlyk merged 9 commits intomasterfrom
scalar_copy
Sep 13, 2021
Merged

Add methods __bool__, __float__ and __int__ to usm_ndarray#578
oleksandr-pavlyk merged 9 commits intomasterfrom
scalar_copy

Conversation

@densmirn
Copy link
Copy Markdown
Contributor

@densmirn densmirn commented Sep 8, 2021

Closes #545

@coveralls
Copy link
Copy Markdown
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
Copy Markdown
Contributor

@densmirn can you also implement __index__?

See data-apis/array-api#231

@oleksandr-pavlyk
Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor Author

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

@oleksandr-pavlyk
Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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