pitrou commented on code in PR #50929:
URL: https://github.com/apache/arrow/pull/50929#discussion_r3923399917


##########
python/pyarrow/tests/test_dlpack.py:
##########
@@ -145,6 +145,68 @@ def test_tensor_dlpack(np_type):
     check_dlpack_export(t, expected)
 
 
+def multidim_arrays():
+    np_arr = np.arange(12, dtype=np.int32).reshape(3, 2, 2)
+    values = pa.array(np_arr.ravel(), type=pa.int32())
+    nested_list = pa.FixedSizeListArray.from_arrays(
+        pa.FixedSizeListArray.from_arrays(values, 2), 2)
+    return [
+        pytest.param(nested_list, np_arr, id="nested_fixed_size_list"),
+        pytest.param(
+            pa.FixedShapeTensorArray.from_numpy_ndarray(np_arr),
+            np_arr,
+            id="fixed_shape_tensor",
+        ),
+    ]
+
+
+@check_bytes_allocated
[email protected](('arr', 'expected'), multidim_arrays())
+def test_array_to_tensor_dlpack(arr, expected):
+    if Version(np.__version__) < Version("2.1.0"):
+        pytest.skip("Versioned DLPack capsules require numpy 2.1.0 or later")
+
+    tensor = arr.to_tensor()
+    # A Tensor sharing an Array buffer is immutable, so it can only be exported
+    # through the versioned DLPack protocol.
+    assert not tensor.is_mutable
+    result = np.from_dlpack(DLPackForwarder(tensor, max_version=(1, 0)))

Review Comment:
   I was thinking more about this:
   ```python
   >>> a = pa.FixedShapeTensorArray.from_numpy_ndarray(np_arr)
   >>> a.__dlpack__()
   Traceback (most recent call last):
     Cell In[10], line 1
       a.__dlpack__()
     File pyarrow/array.pxi:2331 in pyarrow.lib.Array.__dlpack__
       legacy_tensor = GetResultValue(ExportArrayToDLPack(self.sp_array))
     File pyarrow/error.pxi:155 in pyarrow.lib.pyarrow_internal_check_status
       return check_status(status)
     File pyarrow/error.pxi:92 in pyarrow.lib.check_status
       raise convert_status(status)
   ArrowTypeError: DataType is not compatible with DLPack spec: 
extension<arrow.fixed_shape_tensor[value_type=int32, shape=[2,2], 
permutation=[0,1]]>, try converting to a Tensor for multi dimensional data 
support
   /home/antoine/arrow/dev/cpp/src/arrow/c/dlpack.cc:131  GetDLDataType(type)
   
   ```
   
   It would be nice to make it work at some point (perhaps not in this PR?).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to