|
| 1 | +import pytest |
| 2 | + |
| 3 | +flashinfer = pytest.importorskip("flashinfer") |
| 4 | +import torch |
| 5 | +import torch.nn as nn |
| 6 | +import torch_tensorrt |
| 7 | +from parameterized import parameterized |
| 8 | +from torch.testing._internal.common_utils import run_tests |
| 9 | +from torch_tensorrt._enums import dtype |
| 10 | + |
| 11 | +from ..conversion.harness import DispatchTestCase |
| 12 | + |
| 13 | + |
| 14 | +@torch.library.custom_op("flashinfer::rmsnorm", mutates_args=()) # type: ignore[misc] |
| 15 | +def flashinfer_rmsnorm( |
| 16 | + input: torch.Tensor, weight: torch.Tensor, eps: float = 1e-6 |
| 17 | +) -> torch.Tensor: |
| 18 | + return flashinfer.norm.rmsnorm(input, weight) |
| 19 | + |
| 20 | + |
| 21 | +@torch.library.register_fake("flashinfer::rmsnorm") |
| 22 | +def _(input: torch.Tensor, weight: torch.Tensor, b: float = 1e-6) -> torch.Tensor: |
| 23 | + return input |
| 24 | + |
| 25 | + |
| 26 | +torch_tensorrt.dynamo.conversion.plugins.custom_op( |
| 27 | + "flashinfer::rmsnorm", supports_dynamic_shapes=True |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +class TestAutomaticPlugin(DispatchTestCase): |
| 32 | + @parameterized.expand( |
| 33 | + [ |
| 34 | + ((64, 64), (64,), torch.float16), |
| 35 | + ((256, 256), (256,), torch.float16), |
| 36 | + ] |
| 37 | + ) |
| 38 | + def test_rmsnorm_float(self, input_shape, weight_shape, data_type): |
| 39 | + class rmsnorm(nn.Module): |
| 40 | + def forward(self, input, weight): |
| 41 | + return torch.ops.flashinfer.rmsnorm.default(input, weight) |
| 42 | + |
| 43 | + inputs = [ |
| 44 | + torch.randn(input_shape, device="cuda", dtype=data_type), |
| 45 | + torch.randn(weight_shape, device="cuda", dtype=data_type), |
| 46 | + ] |
| 47 | + |
| 48 | + self.run_test(rmsnorm(), inputs, precision=dtype.f16) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + run_tests() |
0 commit comments