|
| 1 | +# Data Parallel Control (dpctl) |
| 2 | +# |
| 3 | +# Copyright 2020-2025 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 numpy as np |
| 18 | +import pytest |
| 19 | + |
| 20 | +import dpctl.tensor as dpt |
| 21 | +import dpctl.tensor._copy_utils as cu |
| 22 | +from dpctl.tests.helper import get_queue_or_skip |
| 23 | + |
| 24 | + |
| 25 | +def test_copy_utils_empty_like_orderK(): |
| 26 | + get_queue_or_skip() |
| 27 | + a = dpt.empty((10, 10), dtype=dpt.int32, order="F") |
| 28 | + X = cu._empty_like_orderK(a, dpt.int32, a.usm_type, a.device) |
| 29 | + assert X.flags["F"] |
| 30 | + |
| 31 | + |
| 32 | +def test_copy_utils_empty_like_orderK_invalid_args(): |
| 33 | + get_queue_or_skip() |
| 34 | + with pytest.raises(TypeError): |
| 35 | + cu._empty_like_orderK([1, 2, 3], dpt.int32, "device", None) |
| 36 | + with pytest.raises(TypeError): |
| 37 | + cu._empty_like_pair_orderK( |
| 38 | + [1, 2, 3], |
| 39 | + ( |
| 40 | + 1, |
| 41 | + 2, |
| 42 | + 3, |
| 43 | + ), |
| 44 | + dpt.int32, |
| 45 | + (3,), |
| 46 | + "device", |
| 47 | + None, |
| 48 | + ) |
| 49 | + |
| 50 | + a = dpt.empty(10, dtype=dpt.int32) |
| 51 | + with pytest.raises(TypeError): |
| 52 | + cu._empty_like_pair_orderK( |
| 53 | + a, |
| 54 | + ( |
| 55 | + 1, |
| 56 | + 2, |
| 57 | + 3, |
| 58 | + ), |
| 59 | + dpt.int32, |
| 60 | + (10,), |
| 61 | + "device", |
| 62 | + None, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def test_copy_utils_from_numpy_empty_like_orderK(): |
| 67 | + q = get_queue_or_skip() |
| 68 | + |
| 69 | + a = np.empty((10, 10), dtype=np.int32, order="C") |
| 70 | + r0 = cu._from_numpy_empty_like_orderK(a, dpt.int32, "device", q) |
| 71 | + assert r0.flags["C"] |
| 72 | + |
| 73 | + b = np.empty((10, 10), dtype=np.int32, order="F") |
| 74 | + r1 = cu._from_numpy_empty_like_orderK(b, dpt.int32, "device", q) |
| 75 | + assert r1.flags["F"] |
| 76 | + |
| 77 | + c = np.empty((2, 3, 4), dtype=np.int32, order="C") |
| 78 | + c = np.transpose(c, (1, 0, 2)) |
| 79 | + r2 = cu._from_numpy_empty_like_orderK(c, dpt.int32, "device", q) |
| 80 | + assert not r2.flags["C"] and not r2.flags["F"] |
| 81 | + |
| 82 | + |
| 83 | +def test_copy_utils_from_numpy_empty_like_orderK_invalid_args(): |
| 84 | + with pytest.raises(TypeError): |
| 85 | + cu._from_numpy_empty_like_orderK([1, 2, 3], dpt.int32, "device", None) |
| 86 | + |
| 87 | + |
| 88 | +def test_gh_2055(): |
| 89 | + """ |
| 90 | + Test that `dpt.asarray` works on contiguous NumPy arrays with `order="K"` |
| 91 | + when dimensions are permuted. |
| 92 | +
|
| 93 | + See: https://github.com/IntelPython/dpctl/issues/2055 |
| 94 | + """ |
| 95 | + get_queue_or_skip() |
| 96 | + |
| 97 | + a = np.ones((2, 3, 4), dtype=dpt.int32) |
| 98 | + a_t = np.transpose(a, (2, 0, 1)) |
| 99 | + r = dpt.asarray(a_t) |
| 100 | + assert not r.flags["C"] and not r.flags["F"] |
0 commit comments