|
| 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 itertools |
| 30 | +import warnings |
| 31 | + |
| 32 | +import numpy as np |
| 33 | +import pytest |
| 34 | + |
| 35 | +# TODO: revert to `import dpctl.tensor...` |
| 36 | +# when dpnp fully migrates dpctl/tensor |
| 37 | +import dpctl_ext.tensor as dpt |
| 38 | +from dpnp.tests.tensor.elementwise.utils import ( |
| 39 | + _all_dtypes, |
| 40 | + _complex_fp_dtypes, |
| 41 | + _real_fp_dtypes, |
| 42 | + _usm_types, |
| 43 | +) |
| 44 | +from dpnp.tests.tensor.helper import ( |
| 45 | + get_queue_or_skip, |
| 46 | + skip_if_dtype_not_supported, |
| 47 | +) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("dtype", _all_dtypes) |
| 51 | +def test_abs_out_type(dtype): |
| 52 | + q = get_queue_or_skip() |
| 53 | + skip_if_dtype_not_supported(dtype, q) |
| 54 | + |
| 55 | + arg_dt = np.dtype(dtype) |
| 56 | + X = dpt.asarray(0, dtype=arg_dt, sycl_queue=q) |
| 57 | + if np.issubdtype(arg_dt, np.complexfloating): |
| 58 | + type_map = { |
| 59 | + np.dtype("c8"): np.dtype("f4"), |
| 60 | + np.dtype("c16"): np.dtype("f8"), |
| 61 | + } |
| 62 | + assert dpt.abs(X).dtype == type_map[arg_dt] |
| 63 | + |
| 64 | + r = dpt.empty_like(X, dtype=type_map[arg_dt]) |
| 65 | + dpt.abs(X, out=r) |
| 66 | + assert np.allclose(dpt.asnumpy(r), dpt.asnumpy(dpt.abs(X))) |
| 67 | + else: |
| 68 | + assert dpt.abs(X).dtype == arg_dt |
| 69 | + |
| 70 | + r = dpt.empty_like(X, dtype=arg_dt) |
| 71 | + dpt.abs(X, out=r) |
| 72 | + assert np.allclose(dpt.asnumpy(r), dpt.asnumpy(dpt.abs(X))) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("usm_type", _usm_types) |
| 76 | +def test_abs_usm_type(usm_type): |
| 77 | + q = get_queue_or_skip() |
| 78 | + |
| 79 | + arg_dt = np.dtype("i4") |
| 80 | + input_shape = (10, 10, 10, 10) |
| 81 | + X = dpt.empty(input_shape, dtype=arg_dt, usm_type=usm_type, sycl_queue=q) |
| 82 | + X[..., 0::2] = 1 |
| 83 | + X[..., 1::2] = 0 |
| 84 | + |
| 85 | + Y = dpt.abs(X) |
| 86 | + assert Y.usm_type == X.usm_type |
| 87 | + assert Y.sycl_queue == X.sycl_queue |
| 88 | + assert Y.flags.c_contiguous |
| 89 | + |
| 90 | + expected_Y = dpt.asnumpy(X) |
| 91 | + assert np.allclose(dpt.asnumpy(Y), expected_Y) |
| 92 | + |
| 93 | + |
| 94 | +def test_abs_types_property(): |
| 95 | + get_queue_or_skip() |
| 96 | + types = dpt.abs.types |
| 97 | + assert isinstance(types, list) |
| 98 | + assert len(types) > 0 |
| 99 | + assert types == dpt.abs.types_ |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize("dtype", _all_dtypes[1:]) |
| 103 | +def test_abs_order(dtype): |
| 104 | + q = get_queue_or_skip() |
| 105 | + skip_if_dtype_not_supported(dtype, q) |
| 106 | + |
| 107 | + arg_dt = np.dtype(dtype) |
| 108 | + exp_dt = np.abs(np.ones(tuple(), dtype=arg_dt)).dtype |
| 109 | + input_shape = (10, 10, 10, 10) |
| 110 | + X = dpt.empty(input_shape, dtype=arg_dt, sycl_queue=q) |
| 111 | + X[..., 0::2] = 1 |
| 112 | + X[..., 1::2] = 0 |
| 113 | + |
| 114 | + for perms in itertools.permutations(range(4)): |
| 115 | + U = dpt.permute_dims(X[:, ::-1, ::-1, :], perms) |
| 116 | + expected_Y = np.ones(U.shape, dtype=exp_dt) |
| 117 | + expected_Y[..., 1::2] = 0 |
| 118 | + expected_Y = np.transpose(expected_Y, perms) |
| 119 | + for ord in ["C", "F", "A", "K"]: |
| 120 | + Y = dpt.abs(U, order=ord) |
| 121 | + assert np.allclose(dpt.asnumpy(Y), expected_Y) |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.parametrize("dtype", ["c8", "c16"]) |
| 125 | +def test_abs_complex(dtype): |
| 126 | + q = get_queue_or_skip() |
| 127 | + skip_if_dtype_not_supported(dtype, q) |
| 128 | + |
| 129 | + arg_dt = np.dtype(dtype) |
| 130 | + input_shape = (10, 10, 10, 10) |
| 131 | + X = dpt.empty(input_shape, dtype=arg_dt, sycl_queue=q) |
| 132 | + Xnp = np.random.standard_normal( |
| 133 | + size=input_shape |
| 134 | + ) + 1j * np.random.standard_normal(size=input_shape) |
| 135 | + Xnp = Xnp.astype(arg_dt) |
| 136 | + X[...] = Xnp |
| 137 | + |
| 138 | + for ord in ["C", "F", "A", "K"]: |
| 139 | + for perms in itertools.permutations(range(4)): |
| 140 | + U = dpt.permute_dims(X[:, ::-1, ::-1, :], perms) |
| 141 | + Y = dpt.abs(U, order=ord) |
| 142 | + expected_Y = np.abs(np.transpose(Xnp[:, ::-1, ::-1, :], perms)) |
| 143 | + tol = dpt.finfo(Y.dtype).resolution |
| 144 | + np.testing.assert_allclose( |
| 145 | + dpt.asnumpy(Y), expected_Y, atol=tol, rtol=tol |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +def test_abs_out_overlap(): |
| 150 | + get_queue_or_skip() |
| 151 | + |
| 152 | + X = dpt.arange(-3, 3, 1, dtype="i4") |
| 153 | + expected = dpt.asarray([3, 2, 1, 0, 1, 2], dtype="i4") |
| 154 | + Y = dpt.abs(X, out=X) |
| 155 | + |
| 156 | + assert Y is X |
| 157 | + assert dpt.all(expected == X) |
| 158 | + |
| 159 | + X = dpt.arange(-3, 3, 1, dtype="i4") |
| 160 | + expected = expected[::-1] |
| 161 | + Y = dpt.abs(X, out=X[::-1]) |
| 162 | + assert Y is not X |
| 163 | + assert dpt.all(expected == X) |
| 164 | + |
| 165 | + |
| 166 | +@pytest.mark.parametrize("dtype", _real_fp_dtypes) |
| 167 | +def test_abs_real_fp_special_values(dtype): |
| 168 | + q = get_queue_or_skip() |
| 169 | + skip_if_dtype_not_supported(dtype, q) |
| 170 | + |
| 171 | + nans_ = [dpt.nan, -dpt.nan] |
| 172 | + infs_ = [dpt.inf, -dpt.inf] |
| 173 | + finites_ = [-1.0, -0.0, 0.0, 1.0] |
| 174 | + inps_ = nans_ + infs_ + finites_ |
| 175 | + |
| 176 | + x = dpt.asarray(inps_, dtype=dtype) |
| 177 | + r = dpt.abs(x) |
| 178 | + |
| 179 | + with warnings.catch_warnings(): |
| 180 | + warnings.simplefilter("ignore") |
| 181 | + expected_np = np.abs(np.asarray(inps_, dtype=dtype)) |
| 182 | + |
| 183 | + expected = dpt.asarray(expected_np, dtype=dtype) |
| 184 | + tol = dpt.finfo(r.dtype).resolution |
| 185 | + |
| 186 | + assert dpt.allclose(r, expected, atol=tol, rtol=tol, equal_nan=True) |
| 187 | + |
| 188 | + |
| 189 | +@pytest.mark.parametrize("dtype", _complex_fp_dtypes) |
| 190 | +def test_abs_complex_fp_special_values(dtype): |
| 191 | + q = get_queue_or_skip() |
| 192 | + skip_if_dtype_not_supported(dtype, q) |
| 193 | + |
| 194 | + nans_ = [dpt.nan, -dpt.nan] |
| 195 | + infs_ = [dpt.inf, -dpt.inf] |
| 196 | + finites_ = [-1.0, -0.0, 0.0, 1.0] |
| 197 | + inps_ = nans_ + infs_ + finites_ |
| 198 | + c_ = [complex(*v) for v in itertools.product(inps_, repeat=2)] |
| 199 | + |
| 200 | + z = dpt.asarray(c_, dtype=dtype) |
| 201 | + r = dpt.abs(z) |
| 202 | + |
| 203 | + with warnings.catch_warnings(): |
| 204 | + warnings.simplefilter("ignore") |
| 205 | + expected_np = np.abs(np.asarray(c_, dtype=dtype)) |
| 206 | + |
| 207 | + expected = dpt.asarray(expected_np, dtype=dtype) |
| 208 | + tol = dpt.finfo(r.dtype).resolution |
| 209 | + |
| 210 | + assert dpt.allclose(r, expected, atol=tol, rtol=tol, equal_nan=True) |
| 211 | + |
| 212 | + |
| 213 | +@pytest.mark.parametrize("dtype", _all_dtypes) |
| 214 | +def test_abs_alignment(dtype): |
| 215 | + q = get_queue_or_skip() |
| 216 | + skip_if_dtype_not_supported(dtype, q) |
| 217 | + |
| 218 | + x = dpt.ones(512, dtype=dtype) |
| 219 | + r = dpt.abs(x) |
| 220 | + |
| 221 | + r2 = dpt.abs(x[1:]) |
| 222 | + assert np.allclose(dpt.asnumpy(r[1:]), dpt.asnumpy(r2)) |
| 223 | + |
| 224 | + dpt.abs(x[:-1], out=r[1:]) |
| 225 | + assert np.allclose(dpt.asnumpy(r[1:]), dpt.asnumpy(r2)) |
0 commit comments