8000 BUG: DLPack cannot export readonly buffers and raises wrong exception · Issue #20742 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
BUG: DLPack cannot export readonly buffers and raises wrong exception  #20742
Closed
@leofang

Description

@leofang

Describe the issue:

The root issue is currently DLPack does not support exporting readonly arrays:

if ( !(PyArray_FLAGS(self) & NPY_ARRAY_WRITEABLE)) {
PyErr_SetString(PyExc_TypeError, "NumPy currently only supports "
"dlpack for writeable arrays");
return NULL;
}

which prevents its usefulness from situations where downstream consumers only need to read it or wrap it in the same readonly fashion (ex: JAX, MPI). IMHO NumPy should not make this abrupt decision for consumers, but instead just raises a warning.

But, for short-term solution before we converge, it's more important that a BufferError, instead of TypeError, is raised, in order to be aligned with the Python buffer protocol. In mpi4py, we do not anticipate TypeError and it causes this test to fail in our CI, which is a regression:
https://github.com/mpi4py/mpi4py/blob/02a6f31f6cc4a5779c6414b993a714caa2bf93c6/test/test_msgspec.py#L399-L404

cc: @dalcinl @mattip

Reproduce the code example:

import numpy as np

a = np.arange(10)
a.flags.writeable = False
b = np._from_dlpack(a)

Error message:

https://github.com/mpi4py/mpi4py-testing/runs/4676901782?check_suite_focus=true

======================================================================
1452
ERROR: testNotWriteable (test_msgspec.TestMessageSimpleNumPy)
1453
----------------------------------------------------------------------
1454
Traceback (most recent call last):
1455
  File "mpi4py/MPI/asbuffer.pxi", line 60, in mpi4py.MPI.PyMPI_GetBuffer
1456
ValueError: buffer source array is read-only
1457

1458
During handling of the above exception, another exception occurred:
1459

1460
Traceback (most recent call last):
1461
  File "/home/runner/work/mpi4py-testing/mpi4py-testing/mpi4py/test/test_msgspec.py", line 403, in testNotWriteable
1462
    self.assertRaises((BufferError, ValueError),
1463
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/unittest/case.py", line 739, in assertRaises
1464
    return context.handle('assertRaises', args, kwargs)
1465
  File "/opt/hostedtoolcache/Python/3.9.9/x64/lib/python3.9/unittest/case.py", line 201, in handle
1466
    callable_obj(*args, **kwargs)
1467
  File "/home/runner/work/mpi4py-testing/mpi4py-testing/mpi4py/test/test_msgspec.py", line 133, in Sendrecv
1468
    MPI.COMM_SELF.Sendrecv(sendbuf=smsg, dest=0,   sendtag=0,
1469
  File "mpi4py/MPI/Comm.pyx", line 328, in mpi4py.MPI.Comm.Sendrecv
1470
  File "mpi4py/MPI/msgbuffer.pxi", line 447, in mpi4py.MPI.message_p2p_recv
1471
  File "mpi4py/MPI/msgbuffer.pxi", line 433, in mpi4py.MPI._p_msg_p2p.for_recv
1472
  File "mpi4py/MPI/Comm.pyx", line 199, in mpi4py.MPI.Comm.Split
1473
  File "mpi4py/MPI/msgbuffer.pxi", line 138, in mpi4py.MPI.message_basic
1474
  File "mpi4py/MPI/asbuffer.pxi", line 264, in mpi4py.MPI.getbuffer
1475
  File "mpi4py/MPI/asbuffer.pxi", line 64, in mpi4py.MPI.PyMPI_GetBuffer
1476
  File "mpi4py/MPI/asbuffer.pxi", line 62, in mpi4py.MPI.PyMPI_GetBuffer
1477
  File "mpi4py/MPI/asdlpack.pxi", line 193, in mpi4py.MPI.Py_GetDLPackBuffer
1478
TypeError: NumPy currently only supports dlpack for writeable arrays

NumPy/Python version information:

NumPy 1.22.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0