ENH: Generation of compatable device and dtype pairs for test_from_dlpack#443
ENH: Generation of compatable device and dtype pairs for test_from_dlpack#443prady0t wants to merge 1 commit into
test_from_dlpack#443Conversation
Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
| except Exception: | ||
| return False | ||
| else: | ||
| return True |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
Closes #440
This is a fairly minimal fix. The new
hh.device_dtype_pairsstrategy generates pairs of all possible (dtype, device) such that dtype is compatible with device.Running:
succeeds, which previously failed for
dtype = float64ondevice = no_float64