Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add apply_over_axes API #8177

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

add apply_over_axes API #8177

wants to merge 10 commits into from

Conversation

syheliel
Copy link

Hello, the community! This PR is to contribute a little step forward #6078. It's my first time to get involved in cupy :)

implement Code is from: https://github.com/numpy/numpy/blob/8f7e4c57931569760a036599ade6955ecc387c84/numpy/lib/_shape_base_impl.py#L413

Test code is from: https://github.com/numpy/numpy/blob/8f7e4c57931569760a036599ade6955ecc387c84/numpy/lib/tests/test_shape_base.py#L274

cupy/lib/_shape_base.py Outdated Show resolved Hide resolved
cupy/lib/_shape_base.py Outdated Show resolved Hide resolved
cupy/lib/_shape_base.py Outdated Show resolved Hide resolved
cupy/lib/_shape_base.py Outdated Show resolved Hide resolved
cupy/lib/_shape_base.py Outdated Show resolved Hide resolved
cupy/lib/_shape_base.py Outdated Show resolved Hide resolved
@@ -106,3 +106,11 @@ def test_apply_along_axis_invalid_axis():
for axis in [-3, 2]:
with pytest.raises(numpy.AxisError):
xp.apply_along_axis(xp.sum, axis, a)


class TestApplyOverAxes(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

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

Could you add another test for axes.ndim == 0 for coverage?

Copy link
Author

Choose a reason for hiding this comment

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

add test_apply_over_axis_invalid_0darr

syheliel and others added 2 commits February 14, 2024 08:55
Co-authored-by: Akifumi Imanishi <akifumi.imanishi@gmail.com>
for axis in axes:
axis = internal._normalize_axis_index(axis, N)
res = func(val, axis)
if res.ndim == val.ndim:
Copy link
Member

Choose a reason for hiding this comment

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

Could you add another test for res.ndim == val.ndim?

Copy link
Member

Choose a reason for hiding this comment

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

Could you add this test?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I'm working on it.

Copy link
Author

Choose a reason for hiding this comment

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

add test_apply_over_axis_shape_preserve_func

syheliel and others added 2 commits February 15, 2024 11:33
Co-authored-by: Akifumi Imanishi <akifumi.imanishi@gmail.com>
Co-authored-by: Akifumi Imanishi <akifumi.imanishi@gmail.com>
for xp in [numpy, cupy]:
a = xp.array(42)
with pytest.raises(AttributeError):
xp.apply_over_axes(xp.sum, a, 0)
Copy link
Member

Choose a reason for hiding this comment

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

This line should not raise AttributeError.

Copy link
Author

Choose a reason for hiding this comment

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

I've Changed it into numpy.AxisError

@syheliel
Copy link
Author

syheliel commented Feb 15, 2024

I found that cupy behaves different from numpy in 0-demension array.

cupy will throw an AxisError:

import cupy as cp
a = cp.array(42)
cp.apply_over_axes(cp.sum, a, 0) 
# error msg:
  File "<stdin>", line 1, in <module>
  File "/home/xxx/cupy/cupy/lib/_shape_base.py", line 136, in apply_over_axes
    axis = internal._normalize_axis_index(axis, N)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "cupy/_core/internal.pyx", line 417, in cupy._core.internal._normalize_axis_index
    cpdef Py_ssize_t _normalize_axis_index(
  File "cupy/_core/internal.pyx", line 437, in cupy._core.internal._normalize_axis_index
    raise numpy.AxisError(axis, ndim)
numpy.exceptions.AxisError: axis 0 is out of bounds for array of dimension 0

But numpy will not throw an error:

import numpy as np
a = np.array(42)
np.apply_over_axes(cp.sum, a, 0)  # returns [42]

The key difference is that cupy uses:

axis = internal._normalize_axis_index(axis, N)

but numpy uses

        if axis < 0:
            axis = N + axis

I will make the behavior the same with numpy. But also add a comment on there.

syheliel added 2 commits February 15, 2024 15:13
@syheliel
Copy link
Author

HI, I have fixed all the problems. Would you like to take a look?

@asi1024
Copy link
Member

asi1024 commented Feb 24, 2024

@syheliel Could you fix for CI failures?

Copy link
Contributor

mergify bot commented Mar 1, 2024

This pull request is now in conflicts. Could you fix it @syheliel? 🙏

@syheliel
Copy link
Author

syheliel commented Mar 1, 2024

got it, I will fix it later

@asi1024
Copy link
Member

asi1024 commented Mar 2, 2024

CIs are still failing. We can provide assistance if needed. 😃

@syheliel
Copy link
Author

syheliel commented Mar 2, 2024

@asi1024 thanks! I have located where is the problem and I'm trying to fix/test it on my local machine

@syheliel
Copy link
Author

syheliel commented Mar 2, 2024

I've fixed the flake8 problem. But I can't find a way to fix this in doc:

/home/runner/work/cupy/cupy/docs/source/reference/functional.rst:13: WARNING: autosummary: failed to import apply_over_axis.
Possible hints:
* ValueError: not enough values to unpack (expected 2, got 1)
* ModuleNotFoundError: No module named 'apply_over_axis'
* ModuleNotFoundError: No module named 'cupy.apply_over_axis'
* KeyError: 'apply_over_axis'
* AttributeError: module 'cupy' has no attribute 'apply_over_axis'
looking for now-outdated files... none found
pickling environment... done
checking consistency... done

Would you like take a look?

@@ -14,5 +14,6 @@ Functional programming
:toctree: generated/

apply_along_axis
apply_over_axis
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
apply_over_axis
apply_over_axes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants