Merged
[perf] [master] Optimize DVM GroupNorm, matmul, allocation and optional permute lowering #47449
[perf] [master] Optimize DVM GroupNorm, matmul, allocation and optional permute lowering #47449
Merged
SorryNaCNAdd on Sep 30
Total: 4 files changed+40-5
@@ -8,6 +8,8 @@ debug_mode = False
8dump_fx_test = False8dump_fx_test = False
9# Enable DVM cat fusion only when explicitly requested.9# Enable DVM cat fusion only when explicitly requested.
10enable_cat = False10enable_cat = False
11+# Enable DVM permute fusion; permute lowering can emit reinterpret_tensor views.
12+enable_fusion_permute = False
11# Configure operator packets/overloads before the DVM backend is loaded.13# Configure operator packets/overloads before the DVM backend is loaded.
12# Disabling takes precedence when an operator occurs in both lists.14# Disabling takes precedence when an operator occurs in both lists.
13disable_decomp_list = []15disable_decomp_list = []
@@ -30,7 +30,6 @@ decomps_to_exclude_npu = [
30 aten.max_pool2d_with_indices,30 aten.max_pool2d_with_indices,
31 aten.max_pool2d_with_indices_backward,31 aten.max_pool2d_with_indices_backward,
32 aten.native_batch_norm,32 aten.native_batch_norm,
33- aten.native_group_norm,
34 aten.nll_loss2d_backward,33 aten.nll_loss2d_backward,
35 aten.nll_loss2d_forward,34 aten.nll_loss2d_forward,
36 aten.nll_loss_backward,35 aten.nll_loss_backward,
@@ -300,6 +299,9 @@ def patch_decomp():
300 global _dvm_inductor_decomp_patched299 global _dvm_inductor_decomp_patched
301 if _dvm_inductor_decomp_patched:300 if _dvm_inductor_decomp_patched:
302 return301 return
302+ from ..decomposition import _override_matmul_should_fold_for_npu
hbhu_bin

这是复用mlir里的吗

likedislike
SorryNaCN
SorryNaCN
Oct 8 Comments:
likedislike
303+ 
304+ _override_matmul_should_fold_for_npu()
303 _disable_cia_decompositions()305 _disable_cia_decompositions()
304 remove_decompositions(inductor_decomp.decompositions, decomps_to_exclude_npu)306 remove_decompositions(inductor_decomp.decompositions, decomps_to_exclude_npu)
305 for op, fn in dvm_decompositions.items():307 for op, fn in dvm_decompositions.items():
@@ -16,9 +16,7 @@ from torch_npu._inductor.ascend_npu_ir.ascend_npu_ir.npu.codegen.meta_kernel imp
16 NpuMetaScheduling,16 NpuMetaScheduling,
17 create_fx_from_snodes_by_traced_graph,17 create_fx_from_snodes_by_traced_graph,
18)18)
19-from torch_npu._inductor.ascend_npu_ir.ascend_npu_ir.npu.codegen.wrapper import (19+from .wrapper import NpuDvmWrapperCodeGen
20- NpuMlirWrapperCodeGen,
21-)
22from torch_npu._inductor.ascend_npu_ir.ascend_npu_ir.npu.inductor_patch import (20from torch_npu._inductor.ascend_npu_ir.ascend_npu_ir.npu.inductor_patch import (
23 lowering as npu_lowering,21 lowering as npu_lowering,
24)22)
@@ -92,6 +90,7 @@ anir_config.GENERATE_LIST = [
92 aten.unsqueeze,90 aten.unsqueeze,
93 aten.squeeze,91 aten.squeeze,
94 aten.reshape,92 aten.reshape,
93+ aten.permute,
95 torch.ops.higher_order.while_loop,94 torch.ops.higher_order.while_loop,
96 torch.ops.higher_order.while_loop_stack_output,95 torch.ops.higher_order.while_loop_stack_output,
97 aten.copy,96 aten.copy,
@@ -112,6 +111,8 @@ anir_config.GENERATE_LIST = [
112def _is_node_supported_by_dvm_rule(node, allow_common_rule=False):111def _is_node_supported_by_dvm_rule(node, allow_common_rule=False):
113 if node.target == aten.cat.default and not dvm_config.enable_cat:112 if node.target == aten.cat.default and not dvm_config.enable_cat:
114 return False113 return False
114+ if node.target == aten.permute.default and not dvm_config.enable_fusion_permute:
115+ return False
115 if node.target == aten.clone.default and dvm_config.view_fusion_level != 2:116 if node.target == aten.clone.default and dvm_config.view_fusion_level != 2:
116 return False117 return False
117 if node.target in (118 if node.target in (
@@ -514,7 +515,7 @@ class DvmMlirFusionPatch:
514 if dvm_config.enable_matmul_fusion:515 if dvm_config.enable_matmul_fusion:
515 patch_dvm_matmul_template_fusion()516 patch_dvm_matmul_template_fusion()
516 register_backend_for_device(517 register_backend_for_device(
517- "npu", NpuDvmScheduling, NpuMlirWrapperCodeGen518+ "npu", NpuDvmScheduling, NpuDvmWrapperCodeGen
518 )519 )
519 DvmMlirFusionPatch._enabled = True520 DvmMlirFusionPatch._enabled = True
520 521 
@@ -0,0 +1,30 @@
1+from torch._inductor import config
2+from torch_npu._inductor.ascend_npu_ir.ascend_npu_ir.npu.codegen.wrapper import (
3+ NpuMlirWrapperCodeGen,
4+)
5+from torch_npu._inductor.triton_experimental.codegen.wrapper import NPUWrapperCodeGen
6+ 
7+ 
8+class NpuDvmWrapperCodeGen(NpuMlirWrapperCodeGen):
9+ @staticmethod
10+ def create(is_subgraph, subgraph_name, parent_wrapper, partition_signatures=None):
11+ if is_subgraph:
12+ return NpuMlirWrapperCodeGen.create(
13+ is_subgraph, subgraph_name, parent_wrapper, partition_signatures
14+ )
15+ return NpuDvmWrapperCodeGen()
16+ 
17+ def write_header(self):
18+ super().write_header()
19+ self.header.writeline("empty_strided_npu = torch_npu._C._empty_strided_npu")
20+ 
21+ def make_allocation(
22+ self, name, device, dtype, shape, stride, allocation_shape=None, is_pinned=False
23+ ):
24+ if device.type != "npu" or config.test_configs.track_memory_lifecycle or is_pinned:
25+ return super().make_allocation(
26+ name, device, dtype, shape, stride, allocation_shape, is_pinned
27+ )
28+ return NPUWrapperCodeGen.make_allocation(
29+ self, name, device, dtype, shape, stride, allocation_shape, is_pinned
30+ )