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

Make unit tests compatible with NumPy 2.x #1826

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Marc-Jindra
Copy link
Collaborator

Due Diligence

  • General:
  • Implementation:
    • unit tests: all split configurations tested
    • unit tests: multiple dtypes tested
    • benchmarks: created for new functionality
    • benchmarks: performance improved or maintained
    • documentation updated where needed

Description

Fixed DeprecationWarnings from numpy 2.x

  • row_stack to vstack
  • passing axes to np.fft.fftn when s is not None
  • __array__ implementation doesn't accept a copy keyword (probably related to Tensor.__array__)
  • __array_wrap__ must accept context and return_scalar arguments
  • Arrays of 2-dimensional vectors are deprecated (np.cross())

Issue/s resolved: #1552

Changes proposed:

  • unittests use vstack instead of row_stack if the numpy version is >= 2.0.0
  • unittests now pass axes to np.fft.fftn when s is not None
  • changed setup.py to allow numpy versions >= 2.0.0

Type of change

Memory requirements

Performance

Does this change modify the behaviour of other functions? If so, which?

yes / no

@Marc-Jindra
Copy link
Collaborator Author

Numpy 2 unittest Warnings

row_stack (solved)

DeprecationWarning: `row_stack` alias is deprecated. Use `np.vstack` directly.

self explanatory.

__array__ (I think it’s related to torch)

DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and 'copy' keyword arguments. To learn more, see the migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword

Here is the link

Triggered in: test_save (heat.core.tests.test_io.TestIO.test_save)

Or: /heat/heat/core/io.py:441/439 by the following code: var[merged_slices] = data.larray.cpu()

Apparently, it's related to PyTorch, as the Tensor.__array__ method does not accept a copy keyword: def __array__(self, dtype=None).

__array_wrap__

DeprecationWarning: __array_wrap__ must accept context and return_scalar arguments (positionally) in the future. (Deprecated in NumPy 2.0)

Triggered in:

  • test_fit_iris_unsplit (heat.cluster.tests.test_kmedians.TestKMedians.test_fit_iris_unsplit)
  • test_exp2 (heat.core.tests.test_exponential.TestExponential.test_exp2)
  • test_sort (heat.core.tests.test_manipulations.TestManipulations.test_sort)
  • test_randn (heat.core.tests.test_random.TestRandom_Batchparallel.test_randn)
  • test_RobustScaler (heat.preprocessing.tests.test_preprocessing.TestRobustScaler.test_RobustScaler)

Or:

  • numpy/_core/fromnumeric.py:48
  • heat/heat/core/tests/test_exponential.py:99

test_exponential Line 99: tmp = np.exp2(torch.arange(elements, dtype=torch.float64))

I couldn't find anything in the NumPy documentation, but there should be a __array_wrap__ method in an ndarray subclass to which context=None and return_scalar=None must be added. I couldn’t find such a method in heat.

np.cross()

DeprecationWarning: Arrays of 2-dimensional vectors are deprecated. Use arrays of 3-dimensional vectors instead. (deprecated in NumPy 2.0)

Calling np.cross with two-dimensional arrays is no longer supported. As a solution, NumPy suggests writing your own function:

def cross2d(x, y):
    return x[..., 0] * y[..., 1] - x[..., 1] * y[..., 0]

However, this is not the desired solution.

axes (solved)

DeprecationWarning: `axes` should not be `None` if `s` is not `None` (Deprecated in NumPy 2.0). In a future version of NumPy, this will raise an error and `s[i]` will correspond to the size along the transformed axis specified by `axes[i]`. To retain current behaviour, pass a sequence [0, ..., k-1] to `axes` for an array of dimension k.

Now, when s is passed to np.fft.fftn, axes must also be provided. To maintain the previous default behavior, axes must be passed for k elements in s, which correspond to the last k dimensions of the array (starting from 0).

Example for a 3-dimensional array: np.fft.fftn(x.numpy(), s=(6, 6), axes=(1, 2))

@ClaudiaComito ClaudiaComito added this to the 1.6 milestone Mar 17, 2025
@ClaudiaComito
Copy link
Contributor

Sample stack trace from Marc:

...........................s...................s.s.s..................................../home/marc/Heat/heat/heat/core/tests/test_exponential.py:99: DeprecationWarning: __array_wrap__ must accept context and return_scalar arguments (positionally) in the future. (Deprecated NumPy 2.0)
  tmp = np.exp2(torch.arange(elements, dtype=torch.float64))
......................../home/marc/Heat/envs/numpy2env/lib/python3.12/site-packages/torch/functional.py:539: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at /pytorch/aten/src/ATen/native/TensorShape.cpp:3637.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
............ss....../home/marc/Heat/heat/heat/core/io.py:441: DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and 'copy' keyword arguments. To learn more, see the migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword
  var[file_slices] = data.larray.cpu()
/home/marc/Heat/heat/heat/core/io.py:439: DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and 'copy' keyword arguments. To learn more, see the migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword
  var[merged_slices] = data.larray.cpu()
..........................................E./home/marc/Heat/heat/heat/core/tests/test_manipulations.py:2690: DeprecationWarning: `row_stack` alias is deprecated. Use `np.vstack` directly.
  np_rstack = np.row_stack((a, b))
/home/marc/Heat/heat/heat/core/tests/test_manipulations.py:2698: DeprecationWarning: `row_stack` alias is deprecated. Use `np.vstack` directly.
  np_rstack = np.row_stack((a, b, c))
/home/marc/Heat/heat/heat/core/tests/test_manipulations.py:2705: DeprecationWarning: `row_stack` alias is deprecated. Use `np.vstack` directly.
  np_rstack = np.row_stack((a, b, c))
/home/marc/Heat/heat/heat/core/tests/test_manipulations.py:2716: DeprecationWarning: `row_stack` alias is deprecated. Use `np.vstack` directly.
  np_rstack = np.row_stack((d, e))
../home/marc/Heat/envs/numpy2env/lib/python3.12/site-packages/numpy/_core/fromnumeric.py:48: DeprecationWarning: __array_wrap__ must accept context and return_scalar arguments (positionally) in the future. (Deprecated NumPy 2.0)
  return conv.wrap(result, to_scalar=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

Make unit tests compatible with NumPy 2.x
2 participants