nv-legate / cupynumeric

An Aspiring Drop-In Replacement for NumPy at Scale
https://docs.nvidia.com/cupynumeric
Apache License 2.0
623 stars 71 forks source link

Does reshape create array copy? #1154

Closed wang-xianghao closed 3 weeks ago

wang-xianghao commented 4 weeks ago

Hello, The cuNumeric best practices say reshape returns a copy rather than a view (Reshape). However, with the sample provided, the assert successds. I just want to confirm whether the lastest version changes reshape to make it create a view. Thanks.

Environment:

Code:

import cunumeric as np

x = np.ones((3,4))
y = x.reshape((12,))

y[0] = 42

assert x[0,0] == 42 # succeeds in NumPy, fails in cuNumeric

Running command:

time legate --cpus 16 \
                --gpus 4 --sysmem 256000 \
                --fbmem 30000 \
                --eager-alloc-percentage 10 test.py

Result: The assertion does not fail. Screenshot 2024-10-19 021520

manopapad commented 4 weeks ago

Thanks for pointing this out. This part of the documentation is inaccurate, it should say something like:

Reshaping will trigger a copy in more cases than NumPy (operations that add/remove unitary dimensions, e.g. ``np.ones(3, 4).reshape(3, 1, 4)``, return a view, but those that do not, e.g. ``np.ones(3,4).reshape(12)``, return a copy).

This should be updated on next documentation push.

lightsighter commented 4 weeks ago

@manopapad I think even your example might return a view as well. It's when you do a reshape like from something like an (8,3) array to a (6,4) array where there's a non-affine mapping between two shapes that we would make a copy. At least that is the way that it used to work.

wang-xianghao commented 4 weeks ago

Thanks @manopapad and @lightsighter. I will use reshape with caution. By the way, do other shape-related operations, e.g. squeeze(), transpose(), expand_dims() also return a view like numpy?

magnatelee commented 4 weeks ago

yes