|
| 1 | +import infini.ops |
| 2 | +import pytest |
| 3 | +import torch |
| 4 | + |
| 5 | +from tests.utils import Payload, empty_strided, get_stream, rand_strided |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.auto_act_and_assert |
| 9 | +@pytest.mark.parametrize( |
| 10 | + "shape, input_strides, out_strides, inplace", |
| 11 | + ( |
| 12 | + ((1, 3), None, None, False), |
| 13 | + ((1, 3), None, None, True), |
| 14 | + ((3, 3), None, None, False), |
| 15 | + ((3, 3), (5, 1), (5, 1), False), |
| 16 | + ((32, 20, 512), None, None, False), |
| 17 | + ((32, 20, 512), None, None, True), |
| 18 | + ((33, 333, 333), None, None, False), |
| 19 | + ((32, 256, 112, 112), None, None, False), |
| 20 | + ((3, 3, 13, 9, 17), None, None, False), |
| 21 | + ( |
| 22 | + (3, 3, 13, 9, 17), |
| 23 | + (19890, 6630, 510, 34, 1), |
| 24 | + (19890, 6630, 510, 34, 1), |
| 25 | + False, |
| 26 | + ), |
| 27 | + ), |
| 28 | +) |
| 29 | +@pytest.mark.parametrize( |
| 30 | + ("dtype", "rtol", "atol"), |
| 31 | + ( |
| 32 | + (torch.float32, 1e-7, 1e-7), |
| 33 | + (torch.float16, 1e-3, 1e-3), |
| 34 | + (torch.bfloat16, 1e-3, 1e-3), |
| 35 | + ), |
| 36 | +) |
| 37 | +def test_relu_infinilm( |
| 38 | + shape, input_strides, out_strides, inplace, dtype, device, rtol, atol |
| 39 | +): |
| 40 | + input = rand_strided(shape, input_strides, dtype=dtype, device=device) |
| 41 | + input.mul_(2).sub_(1) |
| 42 | + out = ( |
| 43 | + input |
| 44 | + if inplace |
| 45 | + else empty_strided(shape, out_strides, dtype=dtype, device=device) |
| 46 | + ) |
| 47 | + |
| 48 | + return Payload( |
| 49 | + _relu_infinilm, |
| 50 | + _torch_relu_infinilm, |
| 51 | + (input, out), |
| 52 | + {}, |
| 53 | + rtol=rtol, |
| 54 | + atol=atol, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def _relu_infinilm(input, out): |
| 59 | + infini.ops.relu_infinilm(input, out, stream=get_stream(input.device)) |
| 60 | + |
| 61 | + return out |
| 62 | + |
| 63 | + |
| 64 | +def _torch_relu_infinilm(input, out): |
| 65 | + result = torch.nn.functional.relu(input) |
| 66 | + out.copy_(result) |
| 67 | + |
| 68 | + return out |
0 commit comments