Repository navigation
Add cupy.ndim - #3060
Add cupy.ndim#3060
cupy.ndim#3060Conversation
|
Thank you for the PR. As for where to implement it, I don't think it doesn't have to be written in Cython. Also, this function does not support a complicated data structure such as lists of Thank you! |
|
Okay, I will move it to pure python in I don't think it is super widely used, but I have seen it used in a few limited places within both SciPy and Scikit-Image. It also occasionally used internally within NumPy itself. |
Somewhat surprisingly, this case does seem to work: import cupy
import numpy
mixed_list = [cupy.arange(8), numpy.arange(8)]
assert cupy.ndim(mixed_list) == 2
obj_array = numpy.asarray(mixed_list)
print(f"shape = {obj_array.shape}")
print(obj_array)gives So the CuPy array got converted to an NumPy object array where each element is a 0-dimensional cupy array. |
|
Oh, thank you for testing!
But it's not trivial to check and forbid such inputs, either... |
|
I thought we don't have to support such inputs in the initial implementation, but maybe it's easier to support it rather than trying to forbid it? |
In the last commit I removed the test cases that involved lists of cupy and/or numpy arrays so we are not locked into a specific behavior in that case. I am not really keen on trying to inspect and explicitly prevent such cases, though. |
|
So you're not going to implement the real support either?
|
|
@kmaehashi @grlee77 Is it possible to revisit this PR? I think it's useful (see e.g. #3542). |
|
@kmaehashi @grlee77 For compatibility with NEP-18 I'd like to see this PR merged. Let us worry about performance and corner cases after the rc1 release, ok? |
|
Jenkins, test this please |
|
Jenkins CI test (for commit 0dc8787, target branch master) failed with status FAILURE. |
|
Jenkins, test this please |
|
Jenkins CI test (for commit 0dc8787, target branch master) succeeded! |
|
|
||
| @testing.numpy_cupy_equal() | ||
| def test_ndim_ndarray2d(self, xp): | ||
| assert xp.ndim(xp.ones((2, 4))) == 2 |
There was a problem hiding this comment.
I think with numpy_cupy_equal we should return instead of asserting. I can fix this later.
Co-authored-by: Leo Fang <[email protected]>
|
pfnCI, test this please. |
|
We revisited this PR and agreed to add this. (c.f. |
|
Jenkins CI test (for commit 63aba4a, target branch master) succeeded! |
|
Jenkins CI test (for commit 63aba4a, target branch master) failed with status FAILURE. |
|
Thank you, @kmaehashi @grlee77! I will send a follow-up PR to fix the test style later. |
This is a pretty trivial function, but was missing from the CuPy API. Please advise on whether this is wanted and if it should be in the Cython code as done here or if we should just define it in pure Python elsewhere.
Note that I left the determination for objects without an ndim attribute up to
numpy.asarray(a).ndimrather thancupy.asarray(a).ndimto avoid a host->device transfer and make sure behavior is equivalent to NumPy for objects. e.g.assert cupy.ndim(None) == 0Adding this also makes it possible for
numpy.ndimto return the dimension of a CuPy array via the__array_function__protocol. (e.g.assert np.ndim(cupy.ones((16, 16))) == 2)Let me know if this looks likely to be accepted and I will add some test cases.