dolfin-adjoint / pyadjoint

The algorithmic differentation tool pyadjoint and add-ons.
GNU Lesser General Public License v3.0
91 stars 37 forks source link

Pyadjoint uses np.float_ which was removed in numpy 2 #155

Closed stephankramer closed 4 months ago

stephankramer commented 5 months ago

With numpy 2.0, which is now used in Firedrake CI, the following code

from firedrake import *
import numpy as np

mesh = UnitSquareMesh(1,1)
V = VectorFunctionSpace(mesh, "R", 0)
u = Function(V)
u.assign(np.array([1,2]))

from firedrake.adjoint import continue_annotation
continue_annotation()
u.assign(np.array([3,4]))

fails in the assign with the tape switched on as it's trying to create and overloaded object of the numpy array:

  File "/home/firedrake/firedrake/src/firedrake/firedrake/adjoint_utils/function.py", line 112, in wrapper
    other = create_overloaded_object(other)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/firedrake/firedrake/src/pyadjoint/pyadjoint/overloaded_type.py", line 33, in create_overloaded_object
    return overloaded_type._ad_init_object(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/firedrake/firedrake/src/pyadjoint/numpy_adjoint/array.py", line 14, in _ad_init_object
    return cls(obj.shape, numpy.float_, buffer=obj)
                          ^^^^^^^^^^^^
  File "/home/firedrake/firedrake/lib/python3.12/site-packages/numpy/__init__.py", line 397, in __getattr__
    raise AttributeError(
AttributeError: `np.float_` was removed in the NumPy 2.0 release. Use `np.float64` instead.. Did you mean: 'float16'?

This break Thetis CI e.g. here: https://github.com/thetisproject/thetis/actions/runs/9658385914/job/26639444161#step:6:1704

So should this be np.float64 indeed? Or perhaps obj.dtype?