|
15 | 15 | import torch |
16 | 16 |
|
17 | 17 | from executorch.backends.qualcomm import _passes |
| 18 | +from executorch.backends.qualcomm._passes.fuse_batch_norm_with_conv import ( |
| 19 | + FuseBatchNormWithConv as FuseBatchNormWithConvQnn, |
| 20 | +) |
18 | 21 |
|
19 | 22 | # Also registers torch.ops.qnn_custom.hadamard_transform (asserted in RecomposeHadamard.test). |
20 | 23 | from executorch.backends.qualcomm.builders.custom_ops import _hadamard_matrix |
@@ -3864,6 +3867,137 @@ def test( |
3864 | 3867 | assertions.assert_no_target(gm, dq_targets) |
3865 | 3868 |
|
3866 | 3869 |
|
| 3870 | +class FuseBatchNormWithConv: |
| 3871 | + """The shared backends/transforms fold, run from the QNN edge pipeline. |
| 3872 | +
|
| 3873 | + Left in place, BatchNorm becomes a standalone QNN op whose per-channel |
| 3874 | + scale multiplies the convolution's fp16 rounding error on HTP. |
| 3875 | + """ |
| 3876 | + |
| 3877 | + class _Conv1dBn(torch.nn.Module): |
| 3878 | + def __init__(self, bias=True): |
| 3879 | + super().__init__() |
| 3880 | + self.conv = torch.nn.Conv1d(4, 8, kernel_size=3, bias=bias) |
| 3881 | + self.bn = torch.nn.BatchNorm1d(8) |
| 3882 | + self.eval() |
| 3883 | + |
| 3884 | + def forward(self, x): |
| 3885 | + return self.bn(self.conv(x)) |
| 3886 | + |
| 3887 | + class _DepthwiseConv2dBn(torch.nn.Module): |
| 3888 | + def __init__(self): |
| 3889 | + super().__init__() |
| 3890 | + self.dw = torch.nn.Conv2d(8, 8, 3, groups=8, bias=False) |
| 3891 | + self.bn1 = torch.nn.BatchNorm2d(8) |
| 3892 | + self.pw = torch.nn.Conv2d(8, 4, 1) |
| 3893 | + self.bn2 = torch.nn.BatchNorm2d(4) |
| 3894 | + self.eval() |
| 3895 | + |
| 3896 | + def forward(self, x): |
| 3897 | + x = torch.nn.functional.silu(self.bn1(self.dw(x))) |
| 3898 | + return self.bn2(self.pw(x)) |
| 3899 | + |
| 3900 | + class _ConvTranspose2dBn(torch.nn.Module): |
| 3901 | + """A transposed conv must NOT be folded: its weight is |
| 3902 | + [in, out/groups, *kernel], so scaling dim 0 hits the wrong axis.""" |
| 3903 | + |
| 3904 | + def __init__(self, cin=4, cout=8): |
| 3905 | + super().__init__() |
| 3906 | + self.conv = torch.nn.ConvTranspose2d(cin, cout, 3) |
| 3907 | + self.bn = torch.nn.BatchNorm2d(cout) |
| 3908 | + self.eval() |
| 3909 | + |
| 3910 | + def forward(self, x): |
| 3911 | + return self.bn(self.conv(x)) |
| 3912 | + |
| 3913 | + class _Conv3dBn(torch.nn.Module): |
| 3914 | + def __init__(self): |
| 3915 | + super().__init__() |
| 3916 | + self.conv = torch.nn.Conv3d(4, 8, kernel_size=3) |
| 3917 | + self.bn = torch.nn.BatchNorm3d(8) |
| 3918 | + self.eval() |
| 3919 | + |
| 3920 | + def forward(self, x): |
| 3921 | + return self.bn(self.conv(x)) |
| 3922 | + |
| 3923 | + # The pass matches both inference BatchNorm forms in the edge dialect. |
| 3924 | + _BN_OPS = { |
| 3925 | + exir_ops.edge.aten.native_batch_norm.default, |
| 3926 | + exir_ops.edge.aten._native_batch_norm_legit_no_training.default, |
| 3927 | + } |
| 3928 | + |
| 3929 | + @staticmethod |
| 3930 | + def _seed_batch_norms(module: torch.nn.Module) -> torch.nn.Module: |
| 3931 | + """Small running variances give the large BatchNorm scales that hurt |
| 3932 | + HTP fp16; folding them away is the point of the pass.""" |
| 3933 | + for m in module.modules(): |
| 3934 | + if isinstance(m, torch.nn.modules.batchnorm._BatchNorm): |
| 3935 | + m.running_mean.uniform_(-2, 2) |
| 3936 | + m.running_var.uniform_(1e-3, 1e-2) |
| 3937 | + return module |
| 3938 | + |
| 3939 | + @staticmethod |
| 3940 | + @unpack_pass_fixtures |
| 3941 | + def test( |
| 3942 | + subtests, |
| 3943 | + quantizer, |
| 3944 | + compile_spec, |
| 3945 | + backend_type: QnnExecuTorchBackendType, |
| 3946 | + assertions: Assertions, |
| 3947 | + pass_pipeline: PassPipeline, |
| 3948 | + ): |
| 3949 | + target_pass = FuseBatchNormWithConvQnn |
| 3950 | + conv = exir_ops.edge.aten.convolution.default |
| 3951 | + |
| 3952 | + cases = ( |
| 3953 | + ("conv1d_bn", FuseBatchNormWithConv._Conv1dBn(), (1, 4, 16), 1), |
| 3954 | + ( |
| 3955 | + "conv1d_bn_no_bias", |
| 3956 | + FuseBatchNormWithConv._Conv1dBn(bias=False), |
| 3957 | + (1, 4, 16), |
| 3958 | + 1, |
| 3959 | + ), |
| 3960 | + ( |
| 3961 | + "depthwise_conv2d_bn", |
| 3962 | + FuseBatchNormWithConv._DepthwiseConv2dBn(), |
| 3963 | + (1, 8, 16, 16), |
| 3964 | + 2, |
| 3965 | + ), |
| 3966 | + ("conv3d_bn", FuseBatchNormWithConv._Conv3dBn(), (1, 4, 8, 8, 8), 1), |
| 3967 | + ) |
| 3968 | + for msg, module, shape, conv_count in cases: |
| 3969 | + with subtests.test(msg=msg): |
| 3970 | + gm = pass_pipeline.lower_edge_ep( |
| 3971 | + module=FuseBatchNormWithConv._seed_batch_norms(module), |
| 3972 | + sample_input=(torch.randn(*shape),), |
| 3973 | + target_pass=target_pass, |
| 3974 | + backend_type=backend_type, |
| 3975 | + compile_spec=compile_spec, |
| 3976 | + quantizer=quantizer, |
| 3977 | + ).graph_module |
| 3978 | + # The BatchNorm is gone and the convolutions it fed survive, |
| 3979 | + # now carrying the folded weight and bias. |
| 3980 | + assertions.assert_no_target(gm, FuseBatchNormWithConv._BN_OPS) |
| 3981 | + assertions.assert_target_count(gm, conv, conv_count) |
| 3982 | + |
| 3983 | + # Folding a transposed conv would scale the wrong weight axis, so the |
| 3984 | + # pass has to leave its BatchNorm standing. |
| 3985 | + with subtests.test(msg="conv_transpose2d_bn_is_skipped"): |
| 3986 | + module = FuseBatchNormWithConv._seed_batch_norms( |
| 3987 | + FuseBatchNormWithConv._ConvTranspose2dBn() |
| 3988 | + ) |
| 3989 | + gm = pass_pipeline.lower_edge_ep( |
| 3990 | + module=module, |
| 3991 | + sample_input=(torch.randn(1, 4, 8, 8),), |
| 3992 | + target_pass=target_pass, |
| 3993 | + backend_type=backend_type, |
| 3994 | + compile_spec=compile_spec, |
| 3995 | + quantizer=quantizer, |
| 3996 | + ).graph_module |
| 3997 | + assertions.assert_target_count(gm, conv, 1) |
| 3998 | + assertions.assert_target_count(gm, FuseBatchNormWithConv._BN_OPS, 1) |
| 3999 | + |
| 4000 | + |
3867 | 4001 | class FuseConsecutiveCast: |
3868 | 4002 | class _ConsecutiveCast(torch.nn.Module): |
3869 | 4003 | def forward(self, x): |
|
0 commit comments