|
| 1 | +import torch |
| 2 | +from torch.nn import functional as F |
| 3 | + |
| 4 | +from torch.testing._internal.common_utils import TestCase, run_tests |
| 5 | +from torch.testing import FileCheck |
| 6 | +import io |
| 7 | + |
| 8 | +class TestMetalRewritePass(TestCase): |
| 9 | + @staticmethod |
| 10 | + def validate_transformed_module( |
| 11 | + # To please flake |
| 12 | + self, |
| 13 | + pattern_count_map, |
| 14 | + data_shape, |
| 15 | + prepack_removal=False, |
| 16 | + fuse_clamping_ops=False): |
| 17 | + module_instance = self |
| 18 | + scripted_model = torch.jit.script(module_instance) |
| 19 | + scripted_model.eval() |
| 20 | + input_data = torch.normal(1, 20, size=data_shape) |
| 21 | + ref_result = scripted_model(input_data) |
| 22 | + torch._C._jit_pass_metal_insert_prepacked_ops(scripted_model._c) |
| 23 | + if fuse_clamping_ops or prepack_removal: |
| 24 | + scripted_model._c = torch._C._freeze_module(scripted_model._c) |
| 25 | + if fuse_clamping_ops: |
| 26 | + torch._C._jit_pass_metal_fuse_clamp_w_prepacked_conv(scripted_model._c) |
| 27 | + if prepack_removal: |
| 28 | + torch._C._jit_pass_metal_fold_prepacking_ops(scripted_model._c) |
| 29 | + |
| 30 | + buffer = io.BytesIO() |
| 31 | + torch.jit.save(scripted_model, buffer) |
| 32 | + buffer.seek(0) |
| 33 | + deserialized_scripted_model = torch.jit.load(buffer) |
| 34 | + for pattern, v in pattern_count_map.items(): |
| 35 | + if (v == 0): |
| 36 | + FileCheck().check(pattern).run(deserialized_scripted_model.graph) |
| 37 | + elif (v == -1): |
| 38 | + FileCheck().check_not(pattern).run(deserialized_scripted_model.graph) |
| 39 | + else: |
| 40 | + FileCheck().check_count(pattern, v, exactly=True).run(deserialized_scripted_model.graph) |
| 41 | + |
| 42 | + def test_conv(self): |
| 43 | + # Conv params |
| 44 | + batch_size = 2 |
| 45 | + input_channels_per_group = 6 |
| 46 | + height = 16 |
| 47 | + width = 16 |
| 48 | + output_channels_per_group = 6 |
| 49 | + groups = 4 |
| 50 | + kernel_h = kernel_w = 3 |
| 51 | + stride_h = stride_w = 1 |
| 52 | + pad_h = pad_w = 1 |
| 53 | + dilation = 1 |
| 54 | + input_channels = input_channels_per_group * groups |
| 55 | + output_channels = output_channels_per_group * groups |
| 56 | + kernels = (kernel_h, kernel_w) |
| 57 | + strides = (stride_h, stride_w) |
| 58 | + paddings = (pad_h, pad_w) |
| 59 | + dilations = (dilation, dilation) |
| 60 | + conv_weight_shape = (output_channels, input_channels_per_group, kernel_h, kernel_w) |
| 61 | + conv_bias_shape = (output_channels) |
| 62 | + |
| 63 | + class Conv2D(torch.nn.Module): |
| 64 | + def __init__(self): |
| 65 | + super(Conv2D, self).__init__() |
| 66 | + self.weight = torch.nn.Parameter(torch.Tensor(torch.rand(conv_weight_shape)), requires_grad=False) |
| 67 | + self.bias = torch.nn.Parameter(torch.Tensor(torch.rand(conv_bias_shape)), requires_grad=False) |
| 68 | + self.strides = strides |
| 69 | + self.paddings = paddings |
| 70 | + self.dilations = dilations |
| 71 | + self.groups = groups |
| 72 | + |
| 73 | + def forward(self, x): |
| 74 | + return F.conv2d(x, self.weight, self.bias, |
| 75 | + self.strides, self.paddings, self.dilations, self.groups) |
| 76 | + |
| 77 | + data_shape = (batch_size, input_channels, height, width) |
| 78 | + pattern_count_map = {"Tensor = aten::conv2d": -1, |
| 79 | + "metal_prepack::conv2d_prepack": 1, |
| 80 | + "metal_prepack::conv2d_run": 1} |
| 81 | + TestMetalRewritePass.validate_transformed_module(Conv2D(), pattern_count_map, data_shape) |
| 82 | + |
| 83 | + class Conv2DRelu(torch.nn.Module): |
| 84 | + def __init__(self): |
| 85 | + super(Conv2DRelu, self).__init__() |
| 86 | + self.weight = torch.nn.Parameter(torch.Tensor(torch.rand(conv_weight_shape)), requires_grad=False) |
| 87 | + self.bias = torch.nn.Parameter(torch.Tensor(torch.rand(conv_bias_shape)), requires_grad=False) |
| 88 | + self.strides = strides |
| 89 | + self.paddings = paddings |
| 90 | + self.dilations = dilations |
| 91 | + self.groups = groups |
| 92 | + |
| 93 | + def forward(self, x): |
| 94 | + o = F.conv2d(x, self.weight, self.bias, |
| 95 | + self.strides, self.paddings, self.dilations, self.groups) |
| 96 | + o = F.relu(o) |
| 97 | + return o |
| 98 | + |
| 99 | + data_shape = (batch_size, input_channels, height, width) |
| 100 | + pattern_count_map = {"Tensor = aten::conv2d": -1, |
| 101 | + "metal_prepack::conv2d_prepack": 1, |
| 102 | + "metal_prepack::conv2d_run": 1} |
| 103 | + TestMetalRewritePass.validate_transformed_module( |
| 104 | + Conv2DRelu(), pattern_count_map, data_shape) |
| 105 | + |
| 106 | + pattern_count_map["aten::relu"] = 1 |
| 107 | + pattern_count_map["metal_prepack::conv2d_prepack"] = -1 |
| 108 | + TestMetalRewritePass.validate_transformed_module( |
| 109 | + Conv2DRelu(), |
| 110 | + pattern_count_map, |
| 111 | + data_shape, |
| 112 | + prepack_removal=True) |
| 113 | + pattern_count_map["aten::relu"] = -1 |
| 114 | + TestMetalRewritePass.validate_transformed_module( |
| 115 | + Conv2DRelu(), |
| 116 | + pattern_count_map, |
| 117 | + data_shape, |
| 118 | + prepack_removal=True, |
| 119 | + fuse_clamping_ops=True) |
| 120 | + |
| 121 | + |
| 122 | + class Conv2DHardtanh(torch.nn.Module): |
| 123 | + def __init__(self): |
| 124 | + super(Conv2DHardtanh, self).__init__() |
| 125 | + self.weight = torch.nn.Parameter(torch.Tensor(torch.rand(conv_weight_shape)), requires_grad=False) |
| 126 | + self.bias = torch.nn.Parameter(torch.Tensor(torch.rand(conv_bias_shape)), requires_grad=False) |
| 127 | + self.strides = strides |
| 128 | + self.paddings = paddings |
| 129 | + self.dilations = dilations |
| 130 | + self.groups = groups |
| 131 | + |
| 132 | + def forward(self, x): |
| 133 | + o = F.conv2d(x, self.weight, self.bias, |
| 134 | + self.strides, self.paddings, self.dilations, self.groups) |
| 135 | + o = F.hardtanh(o) |
| 136 | + return o |
| 137 | + |
| 138 | + data_shape = (batch_size, input_channels, height, width) |
| 139 | + pattern_count_map = {"Tensor = aten::conv2d": -1, |
| 140 | + "metal_prepack::conv2d_prepack": 1, |
| 141 | + "metal_prepack::conv2d_run": 1} |
| 142 | + TestMetalRewritePass.validate_transformed_module(Conv2DHardtanh(), pattern_count_map, data_shape) |
| 143 | + pattern_count_map["aten::hardtanh"] = 1 |
| 144 | + pattern_count_map["metal_prepack::conv2d_prepack"] = -1 |
| 145 | + TestMetalRewritePass.validate_transformed_module( |
| 146 | + Conv2DHardtanh(), |
| 147 | + pattern_count_map, |
| 148 | + data_shape, |
| 149 | + prepack_removal=True) |
| 150 | + pattern_count_map["aten::hardtanh"] = -1 |
| 151 | + TestMetalRewritePass.validate_transformed_module( |
| 152 | + Conv2DRelu(), |
| 153 | + pattern_count_map, |
| 154 | + data_shape, |
| 155 | + prepack_removal=True, |
| 156 | + fuse_clamping_ops=True) |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + run_tests() |
0 commit comments