Skip to content

Conversation

@oleksandr-pavlyk
Copy link
Contributor

This PR resolves gh-1769

shape setter now accepts integer values.

  • 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?
  • If this PR is a work in progress, are you opening the PR as a draft?

Modify shape.setter to handle integers.

```

In [1]: import dpctl.tensor as dpt

In [2]: x = dpt.ones((2, 3))

In [3]: x.shape = 6

In [4]: x
Out[4]: usm_ndarray([1., 1., 1., 1., 1., 1.], dtype=float32)

In [5]: x = dpt.ones((2, 3))

In [6]: class Six:
   ...:     def __init__(self, dim=1):
   ...:         self.v = (1,) * (dim - 1) + (6,)
   ...:     def __len__(self):
   ...:         return len(self.v)
   ...:     def __iter__(self):
   ...:         return iter(self.v)
   ...:

In [7]: x.shape = Six(3)

In [8]: x.shape
Out[8]: (1, 1, 6)
```
Replaced uses of np.prod with math.prod.
@github-actions
Copy link

github-actions bot commented Aug 5, 2024

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

@github-actions
Copy link

github-actions bot commented Aug 5, 2024

Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_240 ran successfully.
Passed: 894
Failed: 1
Skipped: 119

@coveralls
Copy link
Collaborator

coveralls commented Aug 5, 2024

Coverage Status

coverage: 87.93% (+0.2%) from 87.739%
when pulling 9173fed on fix-gh-1769
into b95988d on master.

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.

Does not work for -1, where NumPy does

In [2]: x = dpt.arange(100, dtype="i4")

In [3]: x.shape = -1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 x.shape = -1

File ~/repos/dpctl/dpctl/tensor/_usmarray.pyx:592, in dpctl.tensor._usmarray.usm_ndarray.shape.__set__()
    590 size = shape_to_elem_count(self.nd_, self.shape_)
    591 if not np.prod(new_shape) == size:
--> 592     raise TypeError(
    593         f"Can not reshape array of size {self.size} into {new_shape}"
    594     )

TypeError: Can not reshape array of size 100 into (-1,)

@oleksandr-pavlyk
Copy link
Contributor Author

I added a docstring to document limitations of usm_ndarray.shape setter. The intent was never to match NumPy, although it is certainly possible.

@github-actions
Copy link

github-actions bot commented Aug 6, 2024

Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_252 ran successfully.
Passed: 895
Failed: 0
Skipped: 119

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.

LGTM!

Copy link
Collaborator

@antonwolfy antonwolfy left a comment

Choose a reason for hiding this comment

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

The dpnp scope of tests passed with the change. Thank you @oleksandr-pavlyk

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.

Shape setter does not work with integer scalar

4 participants