Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions python/tvm_ffi/cython/device.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,17 @@ cdef class Device:
}

_DEVICE_NAME_TO_TYPE = {
"llvm": DLDeviceType.kDLCPU,
"cpu": DLDeviceType.kDLCPU,
"c": DLDeviceType.kDLCPU,
"test": DLDeviceType.kDLCPU,
"cuda": DLDeviceType.kDLCUDA,
"nvptx": DLDeviceType.kDLCUDA,
"cl": DLDeviceType.kDLOpenCL,
"opencl": DLDeviceType.kDLOpenCL,
"vulkan": DLDeviceType.kDLVulkan,
"metal": DLDeviceType.kDLMetal,
Comment thread
tqchen marked this conversation as resolved.
"mps": DLDeviceType.kDLMetal,
"vpi": DLDeviceType.kDLVPI,
"rocm": DLDeviceType.kDLROCM,
"ext_dev": DLDeviceType.kDLExtDev,
"hexagon": DLDeviceType.kDLHexagon,
"wgpu": DLDeviceType.kDLWebGPU,
"webgpu": DLDeviceType.kDLWebGPU,
"maia": DLDeviceType.kDLMAIA,
"trn": DLDeviceType.kDLTrn,
Expand Down
17 changes: 17 additions & 0 deletions tests/python/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ def test_device_with_dev_id(
assert dev.index == expect_device_id


@pytest.mark.parametrize(
"alias, canonical_name",
[
("llvm", "cpu"),
("c", "cpu"),
("test", "cpu"),
("nvptx", "cuda"),
("cl", "opencl"),
],
)
def test_device_rejects_noncanonical_aliases(alias: str, canonical_name: str) -> None:
with pytest.raises(ValueError, match=rf"^Unknown device: {alias}$"):
tvm_ffi.device(alias)

assert tvm_ffi.device(canonical_name).type == canonical_name


@pytest.mark.parametrize("dev_type, dev_id", [("cpu:0:0", None), ("cpu:?", None), ("cpu:", None)])
def test_deive_type_error(dev_type: str, dev_id: int | None) -> None:
with pytest.raises(ValueError):
Expand Down
5 changes: 3 additions & 2 deletions tests/python/test_type_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,10 +855,11 @@ def test_device_str_converts(self) -> None:
result = _to_py_class_value(A(tvm_ffi.Device).convert("cuda:1"))
assert result == tvm_ffi.device("cuda", 1)

def test_device_bad_str_rejected(self) -> None:
@pytest.mark.parametrize("value", ["not_a_device", "llvm", "c", "test", "nvptx", "cl"])
def test_device_bad_str_rejected(self, value: str) -> None:
"""Invalid device strings remain type-conversion errors."""
with pytest.raises(TypeError, match="device"):
A(tvm_ffi.Device).convert("not_a_device")
A(tvm_ffi.Device).convert(value)

def test_device_base_class_passthrough_after_public_class_override(self) -> None:
"""The base Device cdef class remains accepted if _CLASS_DEVICE is overridden."""
Expand Down