|
| 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 | +import torch |
| 21 | + |
| 22 | +import tripy as tp |
| 23 | +from tests import helper |
| 24 | + |
| 25 | + |
| 26 | +class TestEqual: |
| 27 | + @pytest.mark.parametrize( |
| 28 | + "tensor_a, tensor_b, dtype", |
| 29 | + [ |
| 30 | + ([1, 2, 3], [1, 2, 3], tp.int64), |
| 31 | + ([1, 2, 3], [1, 2, 4], tp.int32), |
| 32 | + ([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], tp.float16), |
| 33 | + ([1.0, 2.0, 3.0], [1.0, 2.0, 1.0], tp.bfloat16), |
| 34 | + ([1.0, 2.0, 3.0], [1.0, 2.0, 3.00001], tp.float32), |
| 35 | + ([True, False, True], [True, False, True], tp.bool), |
| 36 | + ], |
| 37 | + ) |
| 38 | + def test_equal(self, tensor_a, tensor_b, dtype): |
| 39 | + # Convert to torch tensors for comparison |
| 40 | + torch_a = torch.tensor(tensor_a, dtype=self.torch_dtype(dtype)) |
| 41 | + torch_b = torch.tensor(tensor_b, dtype=self.torch_dtype(dtype)) |
| 42 | + |
| 43 | + # Convert to tripy tensors |
| 44 | + tp_a = tp.Tensor(tensor_a, dtype=dtype) |
| 45 | + tp_b = tp.Tensor(tensor_b, dtype=dtype) |
| 46 | + |
| 47 | + # Compare results |
| 48 | + torch_result = torch.equal(torch_a, torch_b) |
| 49 | + tp_result = tp.equal(tp_a, tp_b) |
| 50 | + |
| 51 | + assert torch_result == tp_result |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def torch_dtype(tp_dtype): |
| 55 | + # Map tripy dtypes to torch dtypes |
| 56 | + dtype_map = { |
| 57 | + tp.float32: torch.float32, |
| 58 | + tp.float16: torch.float16, |
| 59 | + tp.bfloat16: torch.bfloat16, |
| 60 | + tp.int32: torch.int32, |
| 61 | + tp.int64: torch.int64, |
| 62 | + tp.int8: torch.int8, |
| 63 | + tp.bool: torch.bool, |
| 64 | + } |
| 65 | + return dtype_map[tp_dtype] |
| 66 | + |
| 67 | + def test_equal_different_shapes(self): |
| 68 | + a = tp.Tensor([1, 2, 3]) |
| 69 | + b = tp.Tensor([1, 2, 3, 4]) |
| 70 | + |
| 71 | + with helper.raises( |
| 72 | + tp.TripyException, |
| 73 | + match=re.escape("size of operand dimension 0 (4) is not compatible with size of result dimension 0 (3)"), |
| 74 | + has_stack_info_for=[a, b], |
| 75 | + ): |
| 76 | + tp.equal(a, b) |
| 77 | + |
| 78 | + def test_equal_different_dtypes(self): |
| 79 | + a = tp.Tensor([1, 2, 3], dtype=tp.int32) |
| 80 | + b = tp.Tensor([1, 2, 3], dtype=tp.float32) |
| 81 | + |
| 82 | + with helper.raises( |
| 83 | + tp.TripyException, |
| 84 | + match=re.escape( |
| 85 | + "Parameters: 'a' and 'b' must have matching data types, but got: 'int32' and 'float32' respectively." |
| 86 | + ), |
| 87 | + has_stack_info_for=[a, b], |
| 88 | + ): |
| 89 | + tp.equal(a, b) |
0 commit comments