Skip to content

Commit c0146ac

Browse files
committed
add replacement wrapper for numpy's .data access
1 parent f6f5588 commit c0146ac

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

paramz/core/parameter_core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import re
3939
import logging
4040

41+
from paramz.util import _set_mem_addr
42+
4143
from ..transformations import __fixed__, FIXED
4244
from .constrainable import Constrainable
4345
from .nameable import adjust_name_for_printing
@@ -287,8 +289,8 @@ def _propagate_param_grad(self, parray, garray):
287289
self.param_array[pislice] = pi.param_array.flat # , requirements=['C', 'W']).flat
288290
self.gradient_full[pislice] = pi.gradient_full.flat # , requirements=['C', 'W']).flat
289291

290-
pi.param_array.data = parray[pislice].data
291-
pi.gradient_full.data = garray[pislice].data
292+
_set_mem_addr(pi.param_array, parray[pislice])
293+
_set_mem_addr(pi.gradient_full, garray[pislice])
292294

293295
pi._propagate_param_grad(parray[pislice], garray[pislice])
294296
pi_old_size += pi.size

paramz/parameterized.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
#===============================================================================
3030

31-
import numpy; np = numpy
31+
import ctypes
32+
import numpy
33+
34+
from paramz.util import _set_mem_addr; np = numpy
3235
from re import compile
3336
try:
3437
from re import _pattern_type
@@ -268,8 +271,8 @@ def _connect_parameters(self, ignore_added_names=False):
268271
self.param_array[pslice] = p.param_array.flat # , requirements=['C', 'W']).ravel(order='C')
269272
self.gradient_full[pslice] = p.gradient_full.flat # , requirements=['C', 'W']).ravel(order='C')
270273

271-
p.param_array.data = self.param_array[pslice].data
272-
p.gradient_full.data = self.gradient_full[pslice].data
274+
_set_mem_addr(p.param_array, self.param_array[pslice])
275+
_set_mem_addr(p.gradient_full, self.gradient_full[pslice])
273276

274277
self._param_slices_.append(pslice)
275278

paramz/util.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
#===============================================================================
30+
import warnings
3031

3132
def _inherit_doc(fromclass, done_classes = None):
3233
inherited = ''
@@ -44,4 +45,23 @@ def _inherit_doc(fromclass, done_classes = None):
4445
pass
4546
inherited += _inherit_doc(c, done_classes=done_classes)
4647

47-
return inherited
48+
return inherited
49+
50+
51+
def _set_mem_addr(dest, src) -> None:
52+
"""
53+
This function serves to replace the `.data` getter/setter that existed in
54+
`numpy<2` and got removed in `numpy>=2`.
55+
The original behavior was setting the memory address of dest to that of src.
56+
However, directly setting the memory address of a numpy array to the data of
57+
another one seems to be unwanted in `numpy>=2`, which is causing some major
58+
problems here.
59+
"""
60+
with warnings.catch_warnings():
61+
warnings.simplefilter("ignore", DeprecationWarning)
62+
63+
# original
64+
# dest.data = src.data
65+
66+
# take 1
67+
dest.data = memoryview(src)

0 commit comments

Comments
 (0)