|
| 1 | +import torch |
| 2 | +from torch.testing import FileCheck |
| 3 | + |
| 4 | +from torch.testing._internal.common_utils import \ |
| 5 | + (run_tests) |
| 6 | +from torch.testing._internal.jit_utils import JitTestCase |
| 7 | +from torch.testing._internal.common_device_type import \ |
| 8 | + (instantiate_device_type_tests, skipCPUIfNoLapack, skipCUDAIfNoMagma, onlyCPU) |
| 9 | + |
| 10 | +# Information for generating an alias test |
| 11 | +# NOTE: ending the alias_name with an underscore will interpret the test |
| 12 | +# as the test for an inplace method of that name |
| 13 | +class AliasInfo(object): |
| 14 | + __slots__ = ['alias_name', 'alias_op', 'original_name', 'original_op', |
| 15 | + 'get_input', 'get_args', 'decorators'] |
| 16 | + |
| 17 | + def __init__(self, |
| 18 | + alias_name, # the name of the alias |
| 19 | + alias_op, # the aliased op |
| 20 | + original_name, # the name of the original function |
| 21 | + original_op, # the original op |
| 22 | + get_input, # callable (device)->tensor that returns the first tensor argument |
| 23 | + *, |
| 24 | + get_args=lambda d: (), # callable (device)->tuple that returns additional positional arguments |
| 25 | + decorators=()): # decorators to apply to the test |
| 26 | + self.alias_name = alias_name |
| 27 | + self.alias_op = alias_op |
| 28 | + self.original_name = original_name |
| 29 | + self.original_op = original_op |
| 30 | + self.get_input = get_input |
| 31 | + self.get_args = get_args |
| 32 | + self.decorators = decorators |
| 33 | + |
| 34 | +alias_infos = ( |
| 35 | + AliasInfo('absolute', torch.absolute, 'abs', torch.abs, |
| 36 | + lambda d: torch.randn(20, device=d)), |
| 37 | + AliasInfo('absolute_', torch.Tensor.absolute_, 'abs_', torch.Tensor.abs_, |
| 38 | + lambda d: torch.randn(20, device=d)), |
| 39 | + AliasInfo('clip', torch.clip, 'clamp', torch.clamp, |
| 40 | + lambda d: torch.randn(20, device=d), get_args=lambda d: (.4, .6)), |
| 41 | + AliasInfo('clip_', torch.Tensor.clip_, 'clamp_', torch.Tensor.clamp_, |
| 42 | + lambda d: torch.randn(20, device=d), get_args=lambda d: (.4, .6)), |
| 43 | + AliasInfo('linalg.det', torch.linalg.det, 'det', torch.det, |
| 44 | + lambda d: torch.randn(10, 10, device=d), |
| 45 | + decorators=(skipCPUIfNoLapack, skipCUDAIfNoMagma)), |
| 46 | + # NOTE: only runs on CPU because it leaks CUDA memory |
| 47 | + # (see https://github.com/pytorch/pytorch/issues/43119) |
| 48 | + AliasInfo('outer', torch.outer, 'ger', torch.ger, |
| 49 | + lambda d: torch.randn(20, device=d), get_args=lambda d: (torch.randn(20, device=d),), |
| 50 | + decorators=(onlyCPU,)), |
| 51 | + AliasInfo('arccosh', torch.arccosh, 'acosh', torch.acosh, |
| 52 | + lambda d: torch.randn(20, device=d) + 2), |
| 53 | + AliasInfo('arccosh_', torch.Tensor.arccosh_, 'acosh_', torch.Tensor.acosh_, |
| 54 | + lambda d: torch.randn(20, device=d) + 2), |
| 55 | +) |
| 56 | + |
| 57 | +# Placeholder test class for validating that aliases are correctly |
| 58 | +# translated when scripted and traced |
| 59 | +class TestOpNormalization(JitTestCase): |
| 60 | + pass |
| 61 | + |
| 62 | +# Generates alias tests and adds them to the specified class (cls) |
| 63 | +def create_alias_tests(cls): |
| 64 | + for info in alias_infos: |
| 65 | + |
| 66 | + # Tests that the JIT remaps aliases to their original ops |
| 67 | + def _test_jit_op_alias_normalization(self, device, info=info): |
| 68 | + tensor = torch.tensor |
| 69 | + op = info.alias_op |
| 70 | + is_inplace = info.alias_name.endswith('_') |
| 71 | + |
| 72 | + # Checks that scripting converts aliases |
| 73 | + # NOTE: the code to test scripting must be generated since |
| 74 | + # scripting does not support splatting args or directly |
| 75 | + # calling torch.Tensor methods. The following |
| 76 | + # splats args after the first tensor by inlining them as constants. |
| 77 | + if is_inplace: |
| 78 | + fn_template = ''' |
| 79 | + def _fn(t): |
| 80 | + return t.{alias_name}({args}) |
| 81 | + ''' |
| 82 | + arg_string = ', '.join((str(arg) for arg in info.get_args(device))) |
| 83 | + script = fn_template.format(alias_name=info.alias_name, args=arg_string) |
| 84 | + else: |
| 85 | + fn_template = ''' |
| 86 | + def _fn(t): |
| 87 | + return op(t{args}) |
| 88 | + ''' |
| 89 | + arg_string = ", " + ', '.join((str(arg) for arg in info.get_args(device))) |
| 90 | + script = fn_template.format(args=arg_string) |
| 91 | + |
| 92 | + # Compiles script |
| 93 | + scripted = torch.jit.CompilationUnit(script)._fn |
| 94 | + |
| 95 | + # Acquires and checks the graph remaps the alias |
| 96 | + inp = info.get_input(device) |
| 97 | + scripted(inp.clone()) |
| 98 | + graph = scripted.graph_for(inp.clone()) |
| 99 | + FileCheck().check(info.original_name).check_not(info.alias_name).run(graph) |
| 100 | + |
| 101 | + # Checks that tracing converts aliases |
| 102 | + # NOTE: tracing has no problem splatting args |
| 103 | + args = info.get_args(device) |
| 104 | + |
| 105 | + def _fn(t, info=info, args=args): |
| 106 | + return info.alias_op(t, *args) |
| 107 | + |
| 108 | + traced = torch.jit.trace(_fn, (inp.clone(),)) |
| 109 | + traced(inp.clone()) |
| 110 | + graph = traced.graph_for(inp.clone()) |
| 111 | + FileCheck().check(info.original_name).check_not(info.alias_name).run(graph) |
| 112 | + |
| 113 | + # Applies decorators |
| 114 | + for decorator in info.decorators: |
| 115 | + _test_jit_op_alias_normalization = decorator(_test_jit_op_alias_normalization) |
| 116 | + |
| 117 | + test_name = "test_jit_op_alias_normalization_" + info.alias_name |
| 118 | + setattr(cls, test_name, _test_jit_op_alias_normalization) |
| 119 | + |
| 120 | + # Tests that the alias functions perform the same operation as the original |
| 121 | + def _test_alias_computation(self, device, info=info): |
| 122 | + alias_op = info.alias_op |
| 123 | + original_op = info.original_op |
| 124 | + |
| 125 | + inp = info.get_input(device) |
| 126 | + args = info.get_args(device) |
| 127 | + alias_result = alias_op(inp.clone(), *args) |
| 128 | + original_result = alias_op(inp.clone(), *args) |
| 129 | + |
| 130 | + self.assertEqual(alias_result, original_result, atol=0, rtol=0) |
| 131 | + |
| 132 | + # Applies decorators |
| 133 | + for decorator in info.decorators: |
| 134 | + _test_alias_computation = decorator(_test_alias_computation) |
| 135 | + |
| 136 | + test_name = "test_alias_computation_" + info.alias_name |
| 137 | + setattr(cls, test_name, _test_alias_computation) |
| 138 | + |
| 139 | + |
| 140 | +create_alias_tests(TestOpNormalization) |
| 141 | +instantiate_device_type_tests(TestOpNormalization, globals()) |
| 142 | + |
| 143 | +if __name__ == '__main__': |
| 144 | + run_tests() |
0 commit comments