|
| 1 | +# Data Parallel Control (dpctl) |
| 2 | +# |
| 3 | +# Copyright 2020-2023 Intel Corporation |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import itertools |
| 18 | +import warnings |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +import pytest |
| 22 | +from numpy.testing import assert_allclose, assert_equal |
| 23 | + |
| 24 | +import dpctl.tensor as dpt |
| 25 | +from dpctl.tests.helper import get_queue_or_skip, skip_if_dtype_not_supported |
| 26 | + |
| 27 | +from .utils import ( |
| 28 | + _all_dtypes, |
| 29 | + _complex_fp_dtypes, |
| 30 | + _map_to_device_dtype, |
| 31 | + _real_fp_dtypes, |
| 32 | + _usm_types, |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize("dtype", _all_dtypes) |
| 37 | +def test_cbrt_out_type(dtype): |
| 38 | + q = get_queue_or_skip() |
| 39 | + skip_if_dtype_not_supported(dtype, q) |
| 40 | + |
| 41 | + X = dpt.asarray(0, dtype=dtype, sycl_queue=q) |
| 42 | + expected_dtype = np.cbrt(np.array(0, dtype=dtype)).dtype |
| 43 | + expected_dtype = _map_to_device_dtype(expected_dtype, q.sycl_device) |
| 44 | + assert dpt.cbrt(X).dtype == expected_dtype |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize("dtype", ["f2", "f4", "f8", "c8", "c16"]) |
| 48 | +def test_cbrt_output_contig(dtype): |
| 49 | + q = get_queue_or_skip() |
| 50 | + skip_if_dtype_not_supported(dtype, q) |
| 51 | + |
| 52 | + n_seq = 1027 |
| 53 | + |
| 54 | + X = dpt.linspace(0, 13, num=n_seq, dtype=dtype, sycl_queue=q) |
| 55 | + Xnp = dpt.asnumpy(X) |
| 56 | + |
| 57 | + Y = dpt.cbrt(X) |
| 58 | + tol = 8 * dpt.finfo(Y.dtype).resolution |
| 59 | + |
| 60 | + assert_allclose(dpt.asnumpy(Y), np.cbrt(Xnp), atol=tol, rtol=tol) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize("dtype", ["f2", "f4", "f8", "c8", "c16"]) |
| 64 | +def test_cbrt_output_strided(dtype): |
| 65 | + q = get_queue_or_skip() |
| 66 | + skip_if_dtype_not_supported(dtype, q) |
| 67 | + |
| 68 | + n_seq = 2054 |
| 69 | + |
| 70 | + X = dpt.linspace(0, 13, num=n_seq, dtype=dtype, sycl_queue=q)[::-2] |
| 71 | + Xnp = dpt.asnumpy(X) |
| 72 | + |
| 73 | + Y = dpt.cbrt(X) |
| 74 | + tol = 8 * dpt.finfo(Y.dtype).resolution |
| 75 | + |
| 76 | + assert_allclose(dpt.asnumpy(Y), np.cbrt(Xnp), atol=tol, rtol=tol) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize("usm_type", _usm_types) |
| 80 | +def test_cbrt_usm_type(usm_type): |
| 81 | + q = get_queue_or_skip() |
| 82 | + |
| 83 | + arg_dt = np.dtype("f4") |
| 84 | + input_shape = (10, 10, 10, 10) |
| 85 | + X = dpt.empty(input_shape, dtype=arg_dt, usm_type=usm_type, sycl_queue=q) |
| 86 | + X[..., 0::2] = 16.0 |
| 87 | + X[..., 1::2] = 23.0 |
| 88 | + |
| 89 | + Y = dpt.cbrt(X) |
| 90 | + assert Y.usm_type == X.usm_type |
| 91 | + assert Y.sycl_queue == X.sycl_queue |
| 92 | + assert Y.flags.c_contiguous |
| 93 | + |
| 94 | + expected_Y = np.empty(input_shape, dtype=arg_dt) |
| 95 | + expected_Y[..., 0::2] = np.cbrt(np.float32(16.0)) |
| 96 | + expected_Y[..., 1::2] = np.cbrt(np.float32(23.0)) |
| 97 | + tol = 8 * dpt.finfo(Y.dtype).resolution |
| 98 | + |
| 99 | + assert_allclose(dpt.asnumpy(Y), expected_Y, atol=tol, rtol=tol) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize("dtype", _all_dtypes) |
| 103 | +def test_cbrt_order(dtype): |
| 104 | + q = get_queue_or_skip() |
| 105 | + skip_if_dtype_not_supported(dtype, q) |
| 106 | + |
| 107 | + arg_dt = np.dtype(dtype) |
| 108 | + input_shape = (10, 10, 10, 10) |
| 109 | + X = dpt.empty(input_shape, dtype=arg_dt, sycl_queue=q) |
| 110 | + X[..., 0::2] = 16.0 |
| 111 | + X[..., 1::2] = 23.0 |
| 112 | + |
| 113 | + for ord in ["C", "F", "A", "K"]: |
| 114 | + for perms in itertools.permutations(range(4)): |
| 115 | + U = dpt.permute_dims(X[:, ::-1, ::-1, :], perms) |
| 116 | + Y = dpt.cbrt(U, order=ord) |
| 117 | + expected_Y = np.cbrt(dpt.asnumpy(U)) |
| 118 | + tol = 8 * max( |
| 119 | + dpt.finfo(Y.dtype).resolution, |
| 120 | + np.finfo(expected_Y.dtype).resolution, |
| 121 | + ) |
| 122 | + assert_allclose(dpt.asnumpy(Y), expected_Y, atol=tol, rtol=tol) |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.usefixtures("suppress_invalid_numpy_warnings") |
| 126 | +def test_cbrt_special_cases(): |
| 127 | + q = get_queue_or_skip() |
| 128 | + |
| 129 | + X = dpt.asarray( |
| 130 | + [dpt.nan, -1.0, 0.0, -0.0, dpt.inf, -dpt.inf], dtype="f4", sycl_queue=q |
| 131 | + ) |
| 132 | + Xnp = dpt.asnumpy(X) |
| 133 | + |
| 134 | + assert_equal(dpt.asnumpy(dpt.cbrt(X)), np.cbrt(Xnp)) |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.parametrize("dtype", _real_fp_dtypes) |
| 138 | +def test_cbrt_real_fp_special_values(dtype): |
| 139 | + q = get_queue_or_skip() |
| 140 | + skip_if_dtype_not_supported(dtype, q) |
| 141 | + |
| 142 | + nans_ = [dpt.nan, -dpt.nan] |
| 143 | + infs_ = [dpt.inf, -dpt.inf] |
| 144 | + finites_ = [-1.0, -0.0, 0.0, 1.0] |
| 145 | + inps_ = nans_ + infs_ + finites_ |
| 146 | + |
| 147 | + x = dpt.asarray(inps_, dtype=dtype) |
| 148 | + r = dpt.cbrt(x) |
| 149 | + |
| 150 | + with warnings.catch_warnings(): |
| 151 | + warnings.simplefilter("ignore") |
| 152 | + expected_np = np.cbrt(np.asarray(inps_, dtype=dtype)) |
| 153 | + |
| 154 | + expected = dpt.asarray(expected_np, dtype=dtype) |
| 155 | + tol = dpt.finfo(r.dtype).resolution |
| 156 | + |
| 157 | + assert dpt.allclose(r, expected, atol=tol, rtol=tol, equal_nan=True) |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.broken_complex |
| 161 | +@pytest.mark.parametrize("dtype", _complex_fp_dtypes) |
| 162 | +def test_cbrt_complex_fp_special_values(dtype): |
| 163 | + q = get_queue_or_skip() |
| 164 | + skip_if_dtype_not_supported(dtype, q) |
| 165 | + |
| 166 | + nans_ = [dpt.nan, -dpt.nan] |
| 167 | + infs_ = [dpt.inf, -dpt.inf] |
| 168 | + finites_ = [-1.0, -0.0, 0.0, 1.0] |
| 169 | + inps_ = nans_ + infs_ + finites_ |
| 170 | + c_ = [complex(*v) for v in itertools.product(inps_, repeat=2)] |
| 171 | + |
| 172 | + z = dpt.asarray(c_, dtype=dtype) |
| 173 | + r = dpt.cbrt(z) |
| 174 | + |
| 175 | + with warnings.catch_warnings(): |
| 176 | + warnings.simplefilter("ignore") |
| 177 | + expected_np = np.cbrt(np.asarray(c_, dtype=dtype)) |
| 178 | + |
| 179 | + expected = dpt.asarray(expected_np, dtype=dtype) |
| 180 | + tol = dpt.finfo(r.dtype).resolution |
| 181 | + |
| 182 | + assert dpt.allclose(r, expected, atol=tol, rtol=tol, equal_nan=True) |
0 commit comments