|
| 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 torch |
| 20 | + |
| 21 | +import tripy as tp |
| 22 | + |
| 23 | + |
| 24 | +class TestPooling: |
| 25 | + |
| 26 | + @pytest.mark.parametrize( |
| 27 | + "kernel_dims, stride, padding", |
| 28 | + [ |
| 29 | + ((3, 3), (1, 1), ((0, 0), (0, 0))), |
| 30 | + ((4, 4), (2, 2), ((1, 1), (2, 2))), |
| 31 | + ], |
| 32 | + ) |
| 33 | + @pytest.mark.parametrize("dtype", [tp.float32, tp.float16, tp.int8]) |
| 34 | + def test_maxpool_2d(self, kernel_dims, stride, padding, dtype): |
| 35 | + inp_tp = tp.reshape(tp.arange(64, dtype=dtype), (1, 1, 8, 8)) |
| 36 | + out = tp.maxpool(inp_tp, kernel_dims=kernel_dims, stride=stride, padding=padding) |
| 37 | + out_torch = torch.from_dlpack(out).to("cpu") |
| 38 | + |
| 39 | + torch_padding = (padding[0][0], padding[1][0]) |
| 40 | + pool_torch = torch.nn.MaxPool2d(kernel_size=kernel_dims, stride=stride, padding=torch_padding) |
| 41 | + expected = pool_torch(torch.from_dlpack(inp_tp).to("cpu")) |
| 42 | + assert torch.allclose(expected, out_torch) |
| 43 | + assert expected.shape == out_torch.shape |
| 44 | + |
| 45 | + @pytest.mark.parametrize( |
| 46 | + "kernel_dims, stride, padding", |
| 47 | + [ |
| 48 | + ((2, 2, 2), (2, 2, 2), ((0, 0), (1, 1), (1, 1))), |
| 49 | + ], |
| 50 | + ) |
| 51 | + @pytest.mark.parametrize("dtype", [tp.float32, tp.float16]) |
| 52 | + def test_maxpool_3d(self, kernel_dims, stride, padding, dtype): |
| 53 | + inp_tp = tp.reshape(tp.arange(512, dtype=dtype), (1, 1, 8, 8, 8)) |
| 54 | + out = tp.maxpool(inp_tp, kernel_dims=kernel_dims, stride=stride, padding=padding) |
| 55 | + out_torch = torch.from_dlpack(out).to("cpu") |
| 56 | + |
| 57 | + torch_padding = (padding[0][0], padding[1][0], padding[2][0]) |
| 58 | + pool_torch = torch.nn.MaxPool3d(kernel_size=kernel_dims, stride=stride, padding=torch_padding) |
| 59 | + expected = pool_torch(torch.from_dlpack(inp_tp).to("cpu")) |
| 60 | + assert torch.allclose(expected, out_torch) |
| 61 | + assert expected.shape == out_torch.shape |
0 commit comments