|
| 1 | +import infini.ops |
| 2 | +import pytest |
| 3 | +import torch |
| 4 | + |
| 5 | +from tests.utils import Payload, empty_strided, get_npu_stream, randn_strided |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.auto_act_and_assert |
| 9 | +@pytest.mark.parametrize( |
| 10 | + "shape, strides", |
| 11 | + ( |
| 12 | + ((1, 64), None), |
| 13 | + ((2, 128), None), |
| 14 | + ((4, 48, 64), None), |
| 15 | + ((2, 4, 2048), None), |
| 16 | + ((1, 64), (64, 1)), |
| 17 | + ((4, 48, 64), (3072, 64, 1)), |
| 18 | + ), |
| 19 | +) |
| 20 | +@pytest.mark.parametrize("eps", (1e-6, 1e-5)) |
| 21 | +@pytest.mark.parametrize("implementation_index", (0, 1)) |
| 22 | +@pytest.mark.parametrize( |
| 23 | + ("dtype", "rtol", "atol"), |
| 24 | + ( |
| 25 | + (torch.float32, 1e-4, 1e-4), |
| 26 | + (torch.float16, 1e-2, 1e-2), |
| 27 | + (torch.bfloat16, 2e-2, 1e-2), |
| 28 | + ), |
| 29 | +) |
| 30 | +def test_add_rms_norm( |
| 31 | + shape, |
| 32 | + strides, |
| 33 | + eps, |
| 34 | + implementation_index, |
| 35 | + dtype, |
| 36 | + device, |
| 37 | + rtol, |
| 38 | + atol, |
| 39 | +): |
| 40 | + active_indices = infini.ops.AddRmsNorm.active_implementation_indices(device) |
| 41 | + |
| 42 | + if implementation_index not in active_indices: |
| 43 | + pytest.skip(f"implementation `{implementation_index}` not active on `{device}`") |
| 44 | + |
| 45 | + weight_shape = (shape[-1],) |
| 46 | + x1 = randn_strided(shape, strides, dtype=dtype, device=device) |
| 47 | + x2 = randn_strided(shape, strides, dtype=dtype, device=device) |
| 48 | + gamma = randn_strided(weight_shape, None, dtype=dtype, device=device) |
| 49 | + y_out = empty_strided(shape, strides, dtype=dtype, device=device) |
| 50 | + x_out = empty_strided(shape, strides, dtype=dtype, device=device) |
| 51 | + |
| 52 | + return Payload( |
| 53 | + lambda *args, **kwargs: _add_rms_norm( |
| 54 | + *args, **kwargs, implementation_index=implementation_index |
| 55 | + ), |
| 56 | + _torch_add_rms_norm, |
| 57 | + (x1, x2, gamma), |
| 58 | + {"eps": eps, "y_out": y_out, "x_out": x_out}, |
| 59 | + rtol=rtol, |
| 60 | + atol=atol, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def _add_rms_norm(x1, x2, gamma, *, eps=1e-6, y_out=None, x_out=None, |
| 65 | + implementation_index=0): |
| 66 | + if x1.device.type == "npu": |
| 67 | + infini.ops.add_rms_norm( |
| 68 | + x1, x2, gamma, eps, y_out, x_out, |
| 69 | + implementation_index=implementation_index, |
| 70 | + stream=get_npu_stream(x1), |
| 71 | + ) |
| 72 | + else: |
| 73 | + infini.ops.add_rms_norm( |
| 74 | + x1, x2, gamma, eps, y_out, x_out, |
| 75 | + implementation_index=implementation_index, |
| 76 | + ) |
| 77 | + |
| 78 | + # Concatenate both outputs into a single flat tensor for allclose comparison. |
| 79 | + return torch.cat([y_out.contiguous().flatten(), x_out.contiguous().flatten()]) |
| 80 | + |
| 81 | + |
| 82 | +def _torch_add_rms_norm(x1, x2, gamma, *, eps=1e-6, y_out=None, x_out=None): |
| 83 | + x_sum = x1 + x2 |
| 84 | + |
| 85 | + if x_out is not None: |
| 86 | + x_out.copy_(x_sum) |
| 87 | + |
| 88 | + rms = torch.sqrt(torch.mean(x_sum.float() * x_sum.float(), dim=-1, |
| 89 | + keepdim=True) + eps) |
| 90 | + y = (x_sum.float() / rms * gamma.float()).to(x1.dtype) |
| 91 | + |
| 92 | + if y_out is not None: |
| 93 | + y_out.copy_(y) |
| 94 | + |
| 95 | + return torch.cat([y_out.contiguous().flatten(), x_out.contiguous().flatten()]) |
0 commit comments