|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +import torch |
| 5 | +import torch_tensorrt |
| 6 | +from torch.testing._internal.common_utils import TestCase, run_tests |
| 7 | + |
| 8 | + |
| 9 | +class TestRandomOperatorsFallBack(TestCase): |
| 10 | + """rand, randn and randperm used to have converters that called numpy during engine |
| 11 | + construction and froze one sample into the engine, so a compiled model returned the |
| 12 | + same values on every call. They now fall back to PyTorch like the rest of the family, |
| 13 | + which is the correct behaviour for an operator TensorRT cannot express. |
| 14 | + """ |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def _module_with(random_call): |
| 18 | + class WithRandom(torch.nn.Module): |
| 19 | + def __init__(self): |
| 20 | + super().__init__() |
| 21 | + self.layers = torch.nn.ModuleList( |
| 22 | + [torch.nn.Linear(64, 64) for _ in range(6)] |
| 23 | + ) |
| 24 | + |
| 25 | + def forward(self, x): |
| 26 | + out = x |
| 27 | + for layer in self.layers: |
| 28 | + out = torch.relu(layer(out)) |
| 29 | + return out + random_call(out) |
| 30 | + |
| 31 | + return WithRandom().eval().cuda() |
| 32 | + |
| 33 | + def _assert_not_frozen(self, module, inputs): |
| 34 | + compiled = torch_tensorrt.dynamo.compile( |
| 35 | + torch.export.export(module, tuple(inputs)), |
| 36 | + inputs=list(inputs), |
| 37 | + enabled_precisions={torch.float32}, |
| 38 | + truncate_double=True, |
| 39 | + ) |
| 40 | + first = compiled(*inputs).clone() |
| 41 | + second = compiled(*inputs).clone() |
| 42 | + self.assertFalse( |
| 43 | + torch.equal(first, second), |
| 44 | + "a compiled model containing a random operator returned identical values " |
| 45 | + "on two calls, so the values were frozen into the engine", |
| 46 | + ) |
| 47 | + |
| 48 | + def test_randn_is_not_frozen(self): |
| 49 | + inputs = [torch.randn(8, 64, device="cuda")] |
| 50 | + module = self._module_with( |
| 51 | + lambda out: torch.randn(out.shape, device=out.device) |
| 52 | + ) |
| 53 | + self._assert_not_frozen(module, inputs) |
| 54 | + |
| 55 | + def test_rand_is_not_frozen(self): |
| 56 | + inputs = [torch.randn(8, 64, device="cuda")] |
| 57 | + module = self._module_with(lambda out: torch.rand(out.shape, device=out.device)) |
| 58 | + self._assert_not_frozen(module, inputs) |
| 59 | + |
| 60 | + def test_manual_seed_is_honored(self): |
| 61 | + """Resetting torch.manual_seed has to reproduce the output. A frozen value also |
| 62 | + passes this, so it only checks the fallback path.""" |
| 63 | + inputs = [torch.randn(8, 64, device="cuda")] |
| 64 | + module = self._module_with( |
| 65 | + lambda out: torch.randn(out.shape, device=out.device) |
| 66 | + ) |
| 67 | + compiled = torch_tensorrt.dynamo.compile( |
| 68 | + torch.export.export(module, tuple(inputs)), |
| 69 | + inputs=list(inputs), |
| 70 | + enabled_precisions={torch.float32}, |
| 71 | + truncate_double=True, |
| 72 | + ) |
| 73 | + torch.manual_seed(0) |
| 74 | + first = compiled(*inputs).clone() |
| 75 | + torch.manual_seed(0) |
| 76 | + second = compiled(*inputs).clone() |
| 77 | + torch.testing.assert_close(first, second) |
| 78 | + |
| 79 | + def test_randperm_is_a_permutation(self): |
| 80 | + """randperm's correctness is exact, so this needs no tolerance: the output has to be |
| 81 | + a rearrangement of 0..n-1.""" |
| 82 | + |
| 83 | + class WithRandperm(torch.nn.Module): |
| 84 | + def forward(self, x): |
| 85 | + return torch.randperm(16, device=x.device) + (x.sum() * 0).long() |
| 86 | + |
| 87 | + inputs = [torch.randn(8, 64, device="cuda")] |
| 88 | + module = WithRandperm().eval().cuda() |
| 89 | + compiled = torch_tensorrt.dynamo.compile( |
| 90 | + torch.export.export(module, tuple(inputs)), |
| 91 | + inputs=list(inputs), |
| 92 | + min_block_size=1, |
| 93 | + enabled_precisions={torch.float32}, |
| 94 | + truncate_double=True, |
| 95 | + ) |
| 96 | + out = compiled(*inputs) |
| 97 | + self.assertEqual(out.dtype, module(*inputs).dtype) |
| 98 | + torch.testing.assert_close( |
| 99 | + out.sort().values, torch.arange(16, device=out.device, dtype=out.dtype) |
| 100 | + ) |
| 101 | + |
| 102 | + def test_random_operators_are_not_registered(self): |
| 103 | + """The direct check. |
| 104 | +
|
| 105 | + Whether a graph ends up with a frozen value depends on the partitioner, which on |
| 106 | + some stacks declines the surrounding block for unrelated reasons, so an end to end |
| 107 | + test can pass either way. What this change actually does is take these three |
| 108 | + operators out of the registry, so assert that. |
| 109 | + """ |
| 110 | + from torch_tensorrt.dynamo.conversion._ConverterRegistry import ( |
| 111 | + DYNAMO_CONVERTERS, |
| 112 | + ) |
| 113 | + |
| 114 | + for target in ( |
| 115 | + torch.ops.aten.rand.default, |
| 116 | + torch.ops.aten.randn.default, |
| 117 | + torch.ops.aten.randperm.default, |
| 118 | + ): |
| 119 | + self.assertNotIn( |
| 120 | + target, |
| 121 | + DYNAMO_CONVERTERS, |
| 122 | + f"{target} is still registered, so it can be folded into an engine at " |
| 123 | + "build time instead of running fresh on every call", |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + run_tests() |
0 commit comments