Xilinx / PYNQ

Python Productivity for ZYNQ
http://www.pynq.io/
BSD 3-Clause "New" or "Revised" License
1.95k stars 808 forks source link

Numpy 2.0 breaks pynq.allocate on XRT Platforms #1456

Closed drichmond closed 1 month ago

drichmond commented 1 month ago

(I think I have it diagnosed)

Numpy added a device getter to ndarray in Version 2.0. (doc, src)

When allocating a buffer on an XRT platform, the default target works just fine because I don't believe it touches the device field of PynqBuffer.

However, when a non-default target for allocation is provided, it fails with the following exception:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[4], line 1
----> 1 input = pynq.allocate(1024, 'u4', target=ol.plram)
      2 input[:] = np.ones(1024)

File ~/.local/lib/python3.10/site-packages/pynq/buffer.py:158, in allocate(shape, dtype, target, **kwargs)
    156 if target is None:
    157     target = Device.active_device
--> 158 return target.allocate(shape, dtype, **kwargs)

File ~/.local/lib/python3.10/site-packages/pynq/pl_server/xrt_device.py:156, in XrtMemory.allocate(self, shape, dtype, **kwargs)
    145 def allocate(self, shape, dtype, **kwargs):
    146     """Create a new  buffer in the memory bank
    147
    148     Parameters
   (...)
    154
    155     """
--> 156     buf = _xrt_allocate(shape, dtype, self.device, self.idx, **kwargs)
    157     buf.memory = self
    158     return buf

File ~/.local/lib/python3.10/site-packages/pynq/pl_server/xrt_device.py:107, in _xrt_allocate(shape, dtype, device, memidx, cacheable, pointer, cache)
    105     buf = device.map_bo(bo)
    106     device_address = device.get_device_address(bo)
--> 107 ar = PynqBuffer(
    108     shape,
    109     dtype,
    110     bo=bo,
    111     device=device,
    112     buffer=buf,
    113     device_address=device_address,
    114     coherent=False,
    115 )
    116 if pointer is not None:
    117     weakref.finalize(buf, _free_bo, device, bo, ar.virtual_address, ar.nbytes)

File ~/.local/lib/python3.10/site-packages/pynq/buffer.py:45, in PynqBuffer.__new__(cls, device, device_address, bo, coherent, *args, **kwargs)
     43 self.coherent = coherent
     44 self.bo = bo
---> 45 self.device = device
     46 self.offset = 0
     47 self.freed = False

AttributeError: attribute 'device' of 'numpy.ndarray' objects is not writable

If I edit buffer.py and change the name of the device field to ndevice, no exception is thrown, and my code works just fine.

drichmond commented 1 month ago

Forgot to mention: Downgrading to Numpy 1.26.4 also fixes the issue.

mariodruiz commented 1 month ago

Hi Dustin,

Thank you for reporting this, We're tracking this, see https://github.com/Xilinx/PYNQ/pull/1453

drichmond commented 1 month ago

Race condition! Do you want me to close?

mariodruiz commented 1 month ago

If it is resolved, please close.