|
| 1 | +import numpy |
| 2 | +import pytest |
| 3 | +from dpctl import SyclDeviceCreationError, get_devices, select_default_device |
| 4 | +from dpctl.tensor._tensor_impl import default_device_complex_type |
| 5 | + |
| 6 | +import dpnp |
| 7 | +from dpnp.tests.helper import ( |
| 8 | + has_support_aspect64, |
| 9 | + is_win_platform, |
| 10 | + numpy_version, |
| 11 | +) |
| 12 | + |
| 13 | +info = dpnp.__array_namespace_info__() |
| 14 | +default_device = select_default_device() |
| 15 | + |
| 16 | + |
| 17 | +def test_capabilities(): |
| 18 | + caps = info.capabilities() |
| 19 | + assert caps["boolean indexing"] is True |
| 20 | + assert caps["data-dependent shapes"] is True |
| 21 | + assert caps["max dimensions"] == 64 |
| 22 | + |
| 23 | + |
| 24 | +def test_default_device(): |
| 25 | + assert info.default_device() == default_device |
| 26 | + |
| 27 | + |
| 28 | +def test_default_dtypes(): |
| 29 | + dtypes = info.default_dtypes() |
| 30 | + assert ( |
| 31 | + dtypes["real floating"] |
| 32 | + == dpnp.default_float_type() |
| 33 | + == dpnp.asarray(0.0).dtype |
| 34 | + ) |
| 35 | + # TODO: add dpnp.default_complex_type() function |
| 36 | + assert ( |
| 37 | + dtypes["complex floating"] |
| 38 | + == default_device_complex_type(default_device) |
| 39 | + == dpnp.asarray(0.0j).dtype |
| 40 | + ) |
| 41 | + if not is_win_platform() or numpy_version() >= "2.0.0": |
| 42 | + # numpy changed default integer on Windows since 2.0 |
| 43 | + assert dtypes["integral"] == dpnp.intp == dpnp.asarray(0).dtype |
| 44 | + assert ( |
| 45 | + dtypes["indexing"] == dpnp.intp == dpnp.argmax(dpnp.zeros(10)).dtype |
| 46 | + ) |
| 47 | + |
| 48 | + with pytest.raises( |
| 49 | + TypeError, match="Unsupported type for device argument:" |
| 50 | + ): |
| 51 | + info.default_dtypes(device=1) |
| 52 | + |
| 53 | + |
| 54 | +def test_dtypes_all(): |
| 55 | + dtypes = info.dtypes() |
| 56 | + assert dtypes == ( |
| 57 | + { |
| 58 | + "bool": dpnp.bool_, |
| 59 | + "int8": numpy.int8, # TODO: replace with dpnp.int8 |
| 60 | + "int16": numpy.int16, # TODO: replace with dpnp.int16 |
| 61 | + "int32": dpnp.int32, |
| 62 | + "int64": dpnp.int64, |
| 63 | + "uint8": numpy.uint8, # TODO: replace with dpnp.uint8 |
| 64 | + "uint16": numpy.uint16, # TODO: replace with dpnp.uint16 |
| 65 | + "uint32": numpy.uint32, # TODO: replace with dpnp.uint32 |
| 66 | + "uint64": numpy.uint64, # TODO: replace with dpnp.uint64 |
| 67 | + "float32": dpnp.float32, |
| 68 | + } |
| 69 | + | ({"float64": dpnp.float64} if has_support_aspect64() else {}) |
| 70 | + | {"complex64": dpnp.complex64} |
| 71 | + | ({"complex128": dpnp.complex128} if has_support_aspect64() else {}) |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +dtype_categories = { |
| 76 | + "bool": {"bool": dpnp.bool_}, |
| 77 | + "signed integer": { |
| 78 | + "int8": numpy.int8, # TODO: replace with dpnp.int8 |
| 79 | + "int16": numpy.int16, # TODO: replace with dpnp.int16 |
| 80 | + "int32": dpnp.int32, |
| 81 | + "int64": dpnp.int64, |
| 82 | + }, |
| 83 | + "unsigned integer": { # TODO: replace with dpnp dtypes once available |
| 84 | + "uint8": numpy.uint8, |
| 85 | + "uint16": numpy.uint16, |
| 86 | + "uint32": numpy.uint32, |
| 87 | + "uint64": numpy.uint64, |
| 88 | + }, |
| 89 | + "integral": ("signed integer", "unsigned integer"), |
| 90 | + "real floating": {"float32": dpnp.float32} |
| 91 | + | ({"float64": dpnp.float64} if has_support_aspect64() else {}), |
| 92 | + "complex floating": {"complex64": dpnp.complex64} |
| 93 | + | ({"complex128": dpnp.complex128} if has_support_aspect64() else {}), |
| 94 | + "numeric": ("integral", "real floating", "complex floating"), |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize("kind", dtype_categories) |
| 99 | +def test_dtypes_kind(kind): |
| 100 | + expected = dtype_categories[kind] |
| 101 | + if isinstance(expected, tuple): |
| 102 | + assert info.dtypes(kind=kind) == info.dtypes(kind=expected) |
| 103 | + else: |
| 104 | + assert info.dtypes(kind=kind) == expected |
| 105 | + |
| 106 | + |
| 107 | +def test_dtypes_tuple(): |
| 108 | + dtypes = info.dtypes(kind=("bool", "integral")) |
| 109 | + assert dtypes == { |
| 110 | + "bool": dpnp.bool_, |
| 111 | + "int8": numpy.int8, # TODO: replace with dpnp.int8 |
| 112 | + "int16": numpy.int16, # TODO: replace with dpnp.int16 |
| 113 | + "int32": dpnp.int32, |
| 114 | + "int64": dpnp.int64, |
| 115 | + "uint8": numpy.uint8, # TODO: replace with dpnp.uint8 |
| 116 | + "uint16": numpy.uint16, # TODO: replace with dpnp.uint16 |
| 117 | + "uint32": numpy.uint32, # TODO: replace with dpnp.uint32 |
| 118 | + "uint64": numpy.uint64, # TODO: replace with dpnp.uint64 |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +def test_dtypes_invalid_kind(): |
| 123 | + with pytest.raises(ValueError, match="Unrecognized data type kind"): |
| 124 | + info.dtypes(kind="invalid") |
| 125 | + |
| 126 | + |
| 127 | +def test_dtypes_invalid_device(): |
| 128 | + with pytest.raises(SyclDeviceCreationError, match="Could not create"): |
| 129 | + info.dtypes(device="str") |
| 130 | + |
| 131 | + |
| 132 | +def test_devices(): |
| 133 | + assert info.devices() == get_devices() |
0 commit comments