|
| 1 | +# ***************************************************************************** |
| 2 | +# Copyright (c) 2026, Intel Corporation |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions are met: |
| 7 | +# - Redistributions of source code must retain the above copyright notice, |
| 8 | +# this list of conditions and the following disclaimer. |
| 9 | +# - Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +# this list of conditions and the following disclaimer in the documentation |
| 11 | +# and/or other materials provided with the distribution. |
| 12 | +# - Neither the name of the copyright holder nor the names of its contributors |
| 13 | +# may be used to endorse or promote products derived from this software |
| 14 | +# without specific prior written permission. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 26 | +# THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | +# ***************************************************************************** |
| 28 | + |
| 29 | +import dpctl |
| 30 | +import pytest |
| 31 | + |
| 32 | +# TODO: revert to `import dpctl.tensor...` |
| 33 | +# when dpnp fully migrates dpctl/tensor |
| 34 | +import dpctl_ext.tensor as dpt |
| 35 | + |
| 36 | + |
| 37 | +class Dummy: |
| 38 | + @staticmethod |
| 39 | + def abs(a): |
| 40 | + return a |
| 41 | + |
| 42 | + @staticmethod |
| 43 | + def add(a, b): |
| 44 | + if isinstance(a, dpt.usm_ndarray): |
| 45 | + return a |
| 46 | + else: |
| 47 | + return b |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def subtract(a, b): |
| 51 | + if isinstance(a, dpt.usm_ndarray): |
| 52 | + return a |
| 53 | + else: |
| 54 | + return b |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def multiply(a, b): |
| 58 | + if isinstance(a, dpt.usm_ndarray): |
| 59 | + return a |
| 60 | + else: |
| 61 | + return b |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize("namespace", [dpt, Dummy()]) |
| 65 | +def test_fp_ops(namespace): |
| 66 | + try: |
| 67 | + X = dpt.ones(1) |
| 68 | + except dpctl.SyclDeviceCreationError: |
| 69 | + pytest.skip("No SYCL devices available") |
| 70 | + X._set_namespace(namespace) |
| 71 | + assert X.__array_namespace__() is namespace |
| 72 | + X[0] = -2.5 |
| 73 | + X.__abs__() |
| 74 | + X.__add__(1.0) |
| 75 | + X.__radd__(1.0) |
| 76 | + X.__sub__(1.0) |
| 77 | + X.__rsub__(1.0) |
| 78 | + X.__mul__(1.0) |
| 79 | + X.__rmul__(1.0) |
| 80 | + X.__truediv__(1.0) |
| 81 | + X.__rtruediv__(1.0) |
| 82 | + X.__floordiv__(1.0) |
| 83 | + X.__rfloordiv__(1.0) |
| 84 | + X.__pos__() |
| 85 | + X.__neg__() |
| 86 | + X.__eq__(-2.5) |
| 87 | + X.__ne__(-2.5) |
| 88 | + X.__le__(-2.5) |
| 89 | + X.__ge__(-2.5) |
| 90 | + X.__gt__(-2.0) |
| 91 | + X.__iadd__(X) |
| 92 | + X.__isub__(X) |
| 93 | + X.__imul__(X) |
| 94 | + X.__itruediv__(1.0) |
| 95 | + X.__ifloordiv__(1.0) |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize("namespace", [dpt, Dummy()]) |
| 99 | +def test_int_ops(namespace): |
| 100 | + try: |
| 101 | + X = dpt.usm_ndarray(1, "i4") |
| 102 | + except dpctl.SyclDeviceCreationError: |
| 103 | + pytest.skip("No SYCL devices available") |
| 104 | + X._set_namespace(namespace) |
| 105 | + assert X.__array_namespace__() is namespace |
| 106 | + X.__lshift__(2) |
| 107 | + X.__rshift__(2) |
| 108 | + X.__rlshift__(2) |
| 109 | + X.__rrshift__(2) |
| 110 | + X.__ilshift__(2) |
| 111 | + X.__irshift__(2) |
| 112 | + X.__and__(X) |
| 113 | + X.__rand__(X) |
| 114 | + X.__iand__(X) |
| 115 | + X.__or__(X) |
| 116 | + X.__ror__(X) |
| 117 | + X.__ior__(X) |
| 118 | + X.__xor__(X) |
| 119 | + X.__rxor__(X) |
| 120 | + X.__ixor__(X) |
| 121 | + X.__invert__() |
| 122 | + X.__mod__(5) |
| 123 | + X.__rmod__(5) |
| 124 | + X.__imod__(5) |
| 125 | + X.__pow__(2) |
| 126 | + X.__rpow__(2) |
| 127 | + X.__ipow__(2) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize("namespace", [dpt, Dummy()]) |
| 131 | +def test_mat_ops(namespace): |
| 132 | + try: |
| 133 | + M = dpt.eye(3, 3) |
| 134 | + except dpctl.SyclDeviceCreationError: |
| 135 | + pytest.skip("No SYCL devices available") |
| 136 | + M._set_namespace(namespace) |
| 137 | + assert M.__array_namespace__() is namespace |
| 138 | + M.__matmul__(M) |
| 139 | + M.__imatmul__(M) |
| 140 | + M.__rmatmul__(M) |
| 141 | + |
| 142 | + |
| 143 | +@pytest.mark.parametrize("namespace", [dpt, Dummy()]) |
| 144 | +def test_comp_ops(namespace): |
| 145 | + try: |
| 146 | + X = dpt.asarray(1, dtype="u8") |
| 147 | + except dpctl.SyclDeviceCreationError: |
| 148 | + pytest.skip("No SYCL devices available") |
| 149 | + X._set_namespace(namespace) |
| 150 | + assert X.__array_namespace__() is namespace |
| 151 | + assert X.__gt__(-1) |
| 152 | + assert X.__ge__(-1) |
| 153 | + assert not X.__lt__(-1) |
| 154 | + assert not X.__le__(-1) |
| 155 | + assert not X.__eq__(-1) |
| 156 | + assert X.__ne__(-1) |
0 commit comments