Skip to content

ENH: Generation of compatable device and dtype pairs for test_from_dlpack#443

Open
prady0t wants to merge 1 commit into
data-apis:masterfrom
prady0t:new-device-strategy
Open

ENH: Generation of compatable device and dtype pairs for test_from_dlpack#443
prady0t wants to merge 1 commit into
data-apis:masterfrom
prady0t:new-device-strategy

Conversation

@prady0t

@prady0t prady0t commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Closes #440

This is a fairly minimal fix. The new hh.device_dtype_pairs strategy generates pairs of all possible (dtype, device) such that dtype is compatible with device.

Running:

ARRAY_API_TESTS_MODULE=array_api_strict pytest array_api_tests/test_dlpack.py -sv --max-examples=10_000 

succeeds, which previously failed for dtype = float64 on device = no_float64

Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
except Exception:
return False
else:
return True

@ev-br ev-br Jul 24, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually does not work, I'm afraid: with torch, we need to filter out the device="meta", which a version on main does, cf test_dlpack.py::_compatible_devices.

With this PR (note device="meta")

In [3]: hh.device_dtype_pairs
Out[3]: sampled_from([(torch.bool, device(type='cpu')), (torch.bool, device(type='meta')), (torch.uint8, device(type='cpu')), (torch.uint8, device(type='meta')), (torch.uint16, device(type='cpu')), (torch.uint16, device(type='meta')), (torch.uint32, device(type='cpu')), (torch.uint32, device(type='meta')), (torch.uint64, device(type='cpu')), (torch.uint64, device(type='meta')), (torch.int8, device(type='cpu')), (torch.int8, device(type='meta')), (torch.int16, device(type='cpu')), (torch.int16, device(type='meta')), (torch.int32, device(type='cpu')), (torch.int32, device(type='meta')), (torch.int64, device(type='cpu')), (torch.int64, device(type='meta')), (torch.float32, device(type='cpu')), (torch.float32, device(type='meta')), (torch.float64, device(type='cpu')), (torch.float64, device(type='meta')), (torch.complex64, device(type='cpu')), (torch.complex64, device(type='meta')), (torch.complex128, device(type='cpu')), (torch.complex128, device(type='meta'))])

and

In [5]: import array_api_compat.torch as xp

In [6]: xp.from_dlpack(xp.asarray([], dtype=xp.bool), device=xp.device("meta"))
---------------------------------------------------------------------------
BufferError                               Traceback (most recent call last)
Cell In[6], line 1
----> 1 xp.from_dlpack(xp.asarray([], dtype=xp.bool), device=xp.device("meta"))

File ~/miniforge3/envs/array-api/lib/python3.12/site-packages/torch/utils/dlpack.py:149, in from_dlpack(ext_tensor, device, copy)
    146     raise AssertionError(f"from_dlpack: unsupported device type: {type(device)}")
    148 # Convert target device to DLPack format
--> 149 target_dl_device = torch._C._torchDeviceToDLDevice(device)
    151 # Detect cross-device transfer by comparing source and target devices
    152 # E.g. CPU->CUDA, cuda:0->cuda:1, etc.
    153 cross_device_transfer = (ext_device != target_dl_device)

BufferError: Cannot pack tensors on meta

which leads to failing

$ ARRAY_API_TESTS_MODULE=array_api_compat.torch pytest array_api_tests/test_dlpack.py::test_from_dlpack -v --max-examples=10_000 -vs


_dtype_categories = [(xp.bool,), dh.uint_dtypes, dh.int_dtypes, dh.real_float_dtypes, dh.complex_dtypes]
_sorted_dtypes = [d for category in _dtype_categories for d in category]
_device_dtype_pairs = [(d, device) for d in _sorted_dtypes for device in xp.__array_namespace_info__().devices() if dh.is_dtype_device_compatible(d, device)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this PR needs a tweak anyway, please break the long long line


@given(
x=hh.arrays(dtype=hh.all_dtypes, shape=hh.shapes(min_dims=1, max_side=2)),
dtype_device_pair = hh.device_dtype_pairs,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a kind of sad to not being able to use x = hh.arrays(....) in the decorator, but well, "what is, is".
Would be nice to come up with a not-very-ugly general pattern for #302, but taht's not a blocker here. At this stage, let's see what it takes to fix an immediate problem, and keep thinking about a general solution for a general problem on a back-burner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_from_dlpack is problematic with device-dependent dtypes

2 participants