m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.3 KiB

6 months ago
  1. # -------------------------------------------------------------------------
  2. # Copyright (c) Microsoft Corporation. All rights reserved.
  3. # Licensed under the MIT License.
  4. # --------------------------------------------------------------------------
  5. from logging import getLogger
  6. from typing import Dict
  7. from fusion_base import Fusion
  8. from onnx import helper
  9. from onnx_model import OnnxModel
  10. logger = getLogger(__name__)
  11. class FusionBiasSplitGelu(Fusion):
  12. def __init__(self, model: OnnxModel):
  13. super().__init__(model, "BiasSplitGelu", "Gelu")
  14. def fuse(self, gelu_node, input_name_to_nodes: Dict, output_name_to_node: Dict):
  15. """
  16. [root] --->Add --------------------> Slice ---------------> Mul -->
  17. | ^ ^
  18. | | |
  19. +----------------------------+---Slice --> Gelu---+
  20. | | ^
  21. | |-----|
  22. | | |
  23. | Mul Mul
  24. | ^ ^
  25. v | |
  26. Shape ---> Gather --> Add --> Div --+
  27. """
  28. if gelu_node.output[0] not in input_name_to_nodes:
  29. return
  30. children = input_name_to_nodes[gelu_node.output[0]]
  31. if len(children) != 1 or children[0].op_type != "Mul":
  32. return
  33. mul_after_gelu = children[0]
  34. slice_before_gelu = self.model.match_parent(gelu_node, "Slice", 0, output_name_to_node)
  35. if slice_before_gelu is None:
  36. return
  37. if self.model.find_constant_input(slice_before_gelu, -1, delta=0.001) != 3:
  38. return
  39. add_output = slice_before_gelu.input[0]
  40. start_index_nodes = self.model.match_parent_path(
  41. slice_before_gelu,
  42. ["Div", "Add", "Gather", "Shape", "Add"],
  43. [1, 0, 0, 0, 0],
  44. output_name_to_node, # Mul(1) is optional
  45. )
  46. if start_index_nodes is None:
  47. start_index_nodes = self.model.match_parent_path(
  48. slice_before_gelu,
  49. ["Mul", "Div", "Add", "Gather", "Shape", "Add"],
  50. [1, 0, 0, 0, 0, 0],
  51. output_name_to_node,
  52. )
  53. if start_index_nodes is None or start_index_nodes[-2].input[0] != add_output:
  54. return
  55. end_index_nodes = self.model.match_parent_path(slice_before_gelu, ["Mul", "Div"], [2, 0], output_name_to_node)
  56. if (
  57. end_index_nodes is None or end_index_nodes[1] not in start_index_nodes
  58. ): # the Div is parent of both two Mul nodes
  59. return
  60. slice_before_mul = self.model.match_parent(mul_after_gelu, "Slice", 0, output_name_to_node)
  61. if slice_before_mul is None:
  62. return
  63. if (
  64. slice_before_mul.input[2] != slice_before_gelu.input[1]
  65. ): # end index of slice_before_mul is start index of slice_before_gelu
  66. return
  67. subgraph_nodes = start_index_nodes + [
  68. end_index_nodes[0],
  69. mul_after_gelu,
  70. gelu_node,
  71. slice_before_mul,
  72. slice_before_gelu,
  73. ]
  74. subgraph_output = mul_after_gelu.output[0]
  75. if not self.model.is_safe_to_fuse_nodes(
  76. subgraph_nodes, [subgraph_output], input_name_to_nodes, output_name_to_node
  77. ):
  78. logger.info("Skip fuse BiasSplitGelu since it is not safe to fuse the subgraph.")
  79. return
  80. add_node = start_index_nodes[-1]
  81. bias_index, _value = self.model.get_constant_input(add_node)
  82. if not isinstance(bias_index, int):
  83. return
  84. self.nodes_to_remove.extend(subgraph_nodes)
  85. node_name = self.model.create_node_name("BiasSplitGelu", name_prefix="BiasSplitGelu")
  86. fused_node = helper.make_node(
  87. "BiasSplitGelu",
  88. inputs=[add_node.input[1 - bias_index], add_node.input[bias_index]],
  89. outputs=[subgraph_output],
  90. name=node_name,
  91. )
  92. fused_node.domain = "com.microsoft"
  93. self.nodes_to_add.append(fused_node)
  94. self.node_name_to_graph_name[node_name] = self.this_graph_name