|
| 1 | +# |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 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 | + |
| 18 | +import pytest |
| 19 | +import re |
| 20 | + |
| 21 | +import cupy as cp |
| 22 | +import numpy as np |
| 23 | +import torch |
| 24 | + |
| 25 | +import tripy as tp |
| 26 | +from tests.helper import raises |
| 27 | + |
| 28 | + |
| 29 | +class TestStride: |
| 30 | + def assert_error_message(self, excinfo, tensor_type, expected_suggestion): |
| 31 | + error_message = str(excinfo.value) |
| 32 | + assert "Non-canonical strides are not supported for Tripy tensors." in error_message |
| 33 | + assert f"For {tensor_type}, use {expected_suggestion}" in error_message |
| 34 | + |
| 35 | + def tripy_byte_order_strides(self, data): |
| 36 | + return tuple(s * data.dtype.itemsize for s in tp.Tensor(data).stride()) |
| 37 | + |
| 38 | + def test_non_canonical_stride(self): |
| 39 | + # PyTorch test |
| 40 | + t_torch = torch.arange(12, dtype=torch.float32).reshape(3, 4) |
| 41 | + a_torch = t_torch.transpose(0, 1) |
| 42 | + with pytest.raises(tp.TripyException) as excinfo: |
| 43 | + tp.Tensor(a_torch) |
| 44 | + self.assert_error_message(excinfo, "PyTorch Tensor", "tensor.contiguous() or tensor.clone()") |
| 45 | + |
| 46 | + # No exception is thrown. |
| 47 | + print(tp.Tensor(a_torch.contiguous())) |
| 48 | + print(tp.Tensor(a_torch.clone(memory_format=torch.contiguous_format))) |
| 49 | + |
| 50 | + # CuPy test |
| 51 | + t_cupy = cp.arange(12, dtype=cp.float32).reshape(3, 4) |
| 52 | + a_cupy = t_cupy.transpose(1, 0) |
| 53 | + with pytest.raises(tp.TripyException) as excinfo: |
| 54 | + tp.Tensor(a_cupy) |
| 55 | + self.assert_error_message(excinfo, "CuPy Array", "cp.ascontiguousarray(array) or array.copy(order='C')") |
| 56 | + |
| 57 | + print(tp.Tensor(cp.ascontiguousarray(a_cupy))) |
| 58 | + print(tp.Tensor(a_cupy.copy(order="C"))) |
| 59 | + |
| 60 | + # NumPy test |
| 61 | + t_numpy = np.arange(12, dtype=np.float32).reshape(3, 4) |
| 62 | + a_numpy = t_numpy.transpose(1, 0) |
| 63 | + with pytest.raises(tp.TripyException) as excinfo: |
| 64 | + tp.Tensor(a_numpy) |
| 65 | + self.assert_error_message(excinfo, "NumPy Array", "np.ascontiguousarray(array) or array.copy(order='C')") |
| 66 | + |
| 67 | + print(tp.Tensor(np.ascontiguousarray(a_numpy))) |
| 68 | + print(tp.Tensor(a_numpy.copy(order="C"))) |
0 commit comments