Skip to content

Add cupy.ndim - #3060

Merged
kmaehashi merged 5 commits into
cupy:masterfrom
grlee77:ndim
Aug 25, 2020
Merged

kmaehashi merged 5 commits into
cupy:masterfrom
grlee77:ndim

Conversation

@grlee77

@grlee77 grlee77 commented Feb 10, 2020

Copy link
Copy Markdown
Member

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).ndim rather than cupy.asarray(a).ndim to avoid a host->device transfer and make sure behavior is equivalent to NumPy for objects. e.g. assert cupy.ndim(None) == 0

Adding this also makes it possible for numpy.ndim to 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.

@asi1024 asi1024 added this to the v8.0.0b1 milestone Feb 13, 2020
@niboshi

niboshi commented Feb 13, 2020

Copy link
Copy Markdown
Contributor

Thank you for the PR.
It looks numpy.ndim is not documented, but I think it's ok to have it in cupy, as it's necessary for np.ndim users as you pointed out.

As for where to implement it, I don't think it doesn't have to be written in Cython.
Could you move the implementation to cupy/__init__.py, and also write some tests?

Also, this function does not support a complicated data structure such as lists of cupy.ndarrays. I don't think we have to support such inputs at least in the first version (it would be quite complicated), but could you add some statement in the docstring to clarify that?

Thank you!

@grlee77

grlee77 commented Feb 13, 2020

Copy link
Copy Markdown
Member Author

Okay, I will move it to pure python in cupy/__init__.py. Apparently this function is a relic of the old Numeric library. I did not realize it was undocumented, but as far as I can tell there has not been discussion of removing it from the public API. It was mentioned as an alternative to the recently removed np.rank in the Numpy 1.18 release notes:

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.

@grlee77

grlee77 commented Feb 13, 2020

Copy link
Copy Markdown
Member Author

Also, this function does not support a complicated data structure such as lists of cupy.ndarray

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

shape = (2, 8)
[[array(0) array(1) array(2) array(3) array(4) array(5) array(6) array(7)]
 [0 1 2 3 4 5 6 7]]

So the CuPy array got converted to an NumPy object array where each element is a 0-dimensional cupy array.

@niboshi

niboshi commented Feb 14, 2020

Copy link
Copy Markdown
Contributor

Oh, thank you for testing!
But I think it's not desirable to depend on that.

  1. As you said, this creates as many cupy.ndarrays as elements in the original arrays. This overhead is significant for a simple feature like ndim().
  2. Maybe NumPy does not intend ndim to be caculated correctly for a numpy.ndarray with cupy.ndarrays as elements. Any subtle change to NumPy's internal code could potentially break the result of cupy.ndim().

But it's not trivial to check and forbid such inputs, either...

@niboshi

niboshi commented Feb 14, 2020

Copy link
Copy Markdown
Contributor

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?

@niboshi niboshi added cat:feature New features/APIs st:awaiting-author Awaiting response from author labels Feb 14, 2020
@grlee77

grlee77 commented Feb 26, 2020

Copy link
Copy Markdown
Member Author

Oh, thank you for testing!
But I think it's not desirable to depend on that.

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.

@niboshi

niboshi commented Feb 27, 2020

Copy link
Copy Markdown
Contributor

So you're not going to implement the real support either?
Then, please

  • Remove fallback to numpy.asarray
  • Revise the docstring to make it clear that this function expects only cupy.ndarray
  • Perhaps add a TODO comment, too

@kmaehashi kmaehashi assigned kmaehashi and unassigned niboshi Mar 17, 2020
@emcastillo emcastillo modified the milestones: v8.0.0b1, v8.0.0b2 Mar 23, 2020
@asi1024 asi1024 modified the milestones: v8.0.0b2, v8.0.0b3 Apr 23, 2020
@emcastillo emcastillo modified the milestones: v8.0.0b3, v8.0.0b4 May 29, 2020
@asi1024 asi1024 modified the milestones: v8.0.0b4, v8.0.0b5 Jun 24, 2020
Comment thread cupy/__init__.py Resolved
@leofang

leofang commented Jul 11, 2020

Copy link
Copy Markdown
Member

@kmaehashi @grlee77 Is it possible to revisit this PR? I think it's useful (see e.g. #3542).

@asi1024 asi1024 removed this from the v8.0.0b5 milestone Jul 29, 2020
@leofang

leofang commented Aug 25, 2020

Copy link
Copy Markdown
Member

@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?

@leofang

leofang commented Aug 25, 2020

Copy link
Copy Markdown
Member

Jenkins, test this please

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 0dc8787, target branch master) failed with status FAILURE.

@leofang

leofang commented Aug 25, 2020

Copy link
Copy Markdown
Member

Jenkins, test this please

@chainer-ci

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think with numpy_cupy_equal we should return instead of asserting. I can fix this later.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See #3890.

@kmaehashi

Copy link
Copy Markdown
Member

pfnCI, test this please.

@kmaehashi

Copy link
Copy Markdown
Member

We revisited this PR and agreed to add this. (c.f. cupy.shape #3229)
We can later enhance these functions to support list of cupy arrays; we already have a similar routine around here: https://github.com/cupy/cupy/blob/v8.0.0b5/cupy/core/core.pyx#L2069

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 63aba4a, target branch master) succeeded!

@kmaehashi
kmaehashi merged commit 323780e into cupy:master Aug 25, 2020
@kmaehashi kmaehashi added this to the v8.0.0rc1 milestone Aug 25, 2020
@kmaehashi kmaehashi removed the st:awaiting-author Awaiting response from author label Aug 25, 2020
@kmaehashi kmaehashi changed the title add cupy.ndim Add cupy.ndim1 Aug 25, 2020
@kmaehashi kmaehashi changed the title Add cupy.ndim1 Add cupy.ndim Aug 25, 2020
@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 63aba4a, target branch master) failed with status FAILURE.

@leofang

leofang commented Aug 27, 2020

Copy link
Copy Markdown
Member

Thank you, @kmaehashi @grlee77! I will send a follow-up PR to fix the test style later.

@grlee77
grlee77 deleted the ndim branch September 9, 2020 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:feature New features/APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants