From 89509f256a945eb5ae34fc6aa4fa89c6ab803821 Mon Sep 17 00:00:00 2001 From: Ye Wang <52801275+wangyems@users.noreply.github.com> Date: Fri, 11 Sep 2020 11:46:31 -0700 Subject: [PATCH] Not fuse SkipLayerNorm when add has initializer input (#5123) --- .../python/tools/transformers/fusion_skiplayernorm.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/onnxruntime/python/tools/transformers/fusion_skiplayernorm.py b/onnxruntime/python/tools/transformers/fusion_skiplayernorm.py index a5cdb4391e..d71510d356 100644 --- a/onnxruntime/python/tools/transformers/fusion_skiplayernorm.py +++ b/onnxruntime/python/tools/transformers/fusion_skiplayernorm.py @@ -21,6 +21,17 @@ class FusionSkipLayerNormalization(Fusion): def fuse(self, node, input_name_to_nodes, output_name_to_node): add = self.model.get_parent(node, 0, output_name_to_node) + + # In some models there is input_ids->gather->add->LayerNorm and one of input of the + # add node is initializer with fixed shape which should not be fused into SkipLayerNorm + for add_input in add.input: + if self.model.get_initializer(add_input) != None: + return + + # The number of input node of add should be 2 + if len(self.model.get_parents(add)) != 2: + return + if add is not None and add.op_type == 'Add' and self.model.is_safe_to_fuse_nodes( [add, node], node.output, input_name_to_nodes, output_name_to_node): self.nodes_to_remove.extend([add, node])