From 1dfda6b1cf55b70d73751c8fd89088c20b4e828e Mon Sep 17 00:00:00 2001 From: Jiacheng Huang Date: Fri, 26 Jun 2026 16:59:25 +0800 Subject: [PATCH] test: add `clip` operator coverage --- tests/test_clip.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/test_clip.py diff --git a/tests/test_clip.py b/tests/test_clip.py new file mode 100644 index 00000000..e8a9265a --- /dev/null +++ b/tests/test_clip.py @@ -0,0 +1,67 @@ +import pytest +import torch + +import infini.ops + +from tests.utils import Payload, empty_strided, get_stream, randn_strided + + +_SHAPE_CASES = ( + ((13, 4), None, None), + ((13, 4), (10, 1), (10, 1)), + ((13, 4, 4), None, None), + ((13, 4, 4), (20, 4, 1), (20, 4, 1)), + ((16, 5632), None, None), + ((4, 4, 5632), None, None), +) + +_FLOAT_DTYPE_CASES = ( + (torch.float32, 0.0, 0.0), + (torch.float16, 0.0, 0.0), + (torch.bfloat16, 0.0, 0.0), +) + + +@pytest.mark.auto_act_and_assert +@pytest.mark.parametrize("shape, input_strides, out_strides", _SHAPE_CASES) +@pytest.mark.parametrize(("dtype", "rtol", "atol"), _FLOAT_DTYPE_CASES) +def test_clip( + shape, + input_strides, + out_strides, + dtype, + device, + implementation_index, + rtol, + atol, +): + input = randn_strided(shape, input_strides, dtype=dtype, device=device) + out = empty_strided(shape, out_strides, dtype=dtype, device=device) + + return Payload( + lambda input, out: _clip(input, out, implementation_index), + _torch_clip, + (input, out), + {}, + rtol=rtol, + atol=atol, + ) + + +def _clip(input, out, implementation_index): + infini.ops.clip( + input, + -0.5, + 0.5, + out, + stream=get_stream(input.device), + implementation_index=implementation_index, + ) + + return out + + +def _torch_clip(input, out): + out.copy_(torch.clip(input, min=-0.5, max=0.5)) + + return out