Skip to content

Add apply_over_axes API#8177

Merged
asi1024 merged 14 commits into
cupy:mainfrom
syheliel:main
Mar 31, 2025
Merged

Add apply_over_axes API#8177
asi1024 merged 14 commits into
cupy:mainfrom
syheliel:main

Conversation

@syheliel
Copy link
Copy Markdown
Contributor

Comment thread cupy/lib/_shape_base.py Outdated
Comment thread cupy/lib/_shape_base.py Outdated
Comment thread cupy/lib/_shape_base.py Outdated
Comment thread cupy/lib/_shape_base.py Outdated
Comment thread cupy/lib/_shape_base.py Outdated
Comment thread cupy/lib/_shape_base.py Outdated
xp.apply_along_axis(xp.sum, axis, a)


class TestApplyOverAxes(unittest.TestCase):
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.

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

Copy link
Copy Markdown
Contributor 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

Comment thread cupy/lib/_shape_base.py
Comment thread tests/cupy_tests/lib_tests/test_shape_base.py Outdated
Comment thread tests/cupy_tests/lib_tests/test_shape_base.py Outdated
for xp in [numpy, cupy]:
a = xp.array(42)
with pytest.raises(AttributeError):
xp.apply_over_axes(xp.sum, a, 0)
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.

This line should not raise AttributeError.

Copy link
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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
Copy link
Copy Markdown
Contributor Author

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

@asi1024
Copy link
Copy Markdown
Member

asi1024 commented Feb 24, 2024

@syheliel Could you fix for CI failures?

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Mar 1, 2024

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

@syheliel
Copy link
Copy Markdown
Contributor Author

syheliel commented Mar 1, 2024

got it, I will fix it later

@asi1024
Copy link
Copy Markdown
Member

asi1024 commented Mar 2, 2024

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

@syheliel
Copy link
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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?

Comment thread docs/source/reference/functional.rst Outdated
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Jun 7, 2024

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

@kmaehashi
Copy link
Copy Markdown
Member

Are the only blockers docs and conflicts?

Kenichi Maehashi and others added 2 commits February 17, 2025 15:03
@asi1024
Copy link
Copy Markdown
Member

asi1024 commented Feb 18, 2025

/test mini

@asi1024 asi1024 added this to the v14.0.0a1 milestone Feb 18, 2025
@asi1024 asi1024 enabled auto-merge February 18, 2025 05:23
asi1024
asi1024 previously approved these changes Feb 18, 2025
Copy link
Copy Markdown
Member

@kmaehashi kmaehashi left a comment

Choose a reason for hiding this comment

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

For NumPy v2 compatibility.

Comment thread tests/cupy_tests/lib_tests/test_shape_base.py Outdated
Comment thread tests/cupy_tests/lib_tests/test_shape_base.py Outdated
@kmaehashi kmaehashi changed the title add apply_over_axes API Add apply_over_axes API Mar 26, 2025
@kmaehashi
Copy link
Copy Markdown
Member

/test mini

Copy link
Copy Markdown
Member

@kmaehashi kmaehashi left a comment

Choose a reason for hiding this comment

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

For NumPy v1 compatibility (currently CI uses NumPy v1)

Comment thread tests/cupy_tests/lib_tests/test_shape_base.py Outdated
Comment thread tests/cupy_tests/lib_tests/test_shape_base.py Outdated
@kmaehashi
Copy link
Copy Markdown
Member

/test mini

@asi1024 asi1024 modified the milestone: v14.0.0a1 Mar 31, 2025
@asi1024 asi1024 disabled auto-merge March 31, 2025 02:48
@asi1024 asi1024 merged commit 04ec12e into cupy:main Mar 31, 2025
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.

3 participants