|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | + |
| 4 | +""" |
| 5 | +tf2onnx.rewrite - Rewrites a pattern from the tf layer_norm contrib op. |
| 6 | +Converts a mean/variance normalization pattern (using ReduceMean, RSqrt, Sub, Mul, etc.) into InstanceNormalization |
| 7 | +""" |
| 8 | +from onnx import TensorProto, helper |
| 9 | +from tf2onnx.graph_matcher import OpTypePattern, GraphMatcher |
| 10 | +from tf2onnx.graph_builder import GraphBuilder |
| 11 | + |
| 12 | + |
| 13 | +# pylint: disable=missing-docstring |
| 14 | + |
| 15 | +def rewrite_layer_normalization(g, ops): |
| 16 | + # Needs ConstantOfShape |
| 17 | + if g.opset <= 9: |
| 18 | + return ops |
| 19 | + |
| 20 | + inner_pattern = \ |
| 21 | + OpTypePattern('Rsqrt', inputs=[ |
| 22 | + OpTypePattern('Add', inputs=[ |
| 23 | + OpTypePattern('Mean', allow_reorder=False, inputs=[ |
| 24 | + OpTypePattern('Square', inputs=[ |
| 25 | + OpTypePattern('Sub', allow_reorder=False, inputs=[ |
| 26 | + OpTypePattern('*', name='input'), |
| 27 | + OpTypePattern('Mean', name='mean', allow_reorder=False, inputs=[ |
| 28 | + OpTypePattern('*', name='input_r2'), |
| 29 | + OpTypePattern('Const|ConstV2', name='mean_axes') |
| 30 | + ]) |
| 31 | + ]) |
| 32 | + ]), |
| 33 | + OpTypePattern('Const|ConstV2', name='variance_axes') |
| 34 | + ]), |
| 35 | + OpTypePattern('Const|ConstV2', name='epsilon') |
| 36 | + ]) |
| 37 | + ]) |
| 38 | + |
| 39 | + pattern0 = \ |
| 40 | + OpTypePattern('Add', name='bias_add', inputs=[ |
| 41 | + OpTypePattern('Mul', name='scale_mul', inputs=[ |
| 42 | + OpTypePattern('Mul', inputs=[ |
| 43 | + inner_pattern, |
| 44 | + OpTypePattern('*', name='scale') |
| 45 | + ]), |
| 46 | + OpTypePattern('Sub', inputs=[ |
| 47 | + OpTypePattern('*', name='input_r3'), |
| 48 | + OpTypePattern('Mean', name='mean_r2') |
| 49 | + ]) |
| 50 | + ]), |
| 51 | + OpTypePattern('*', name='bias') |
| 52 | + ]) |
| 53 | + pattern1 = \ |
| 54 | + OpTypePattern('Add', name='bias_add', inputs=[ |
| 55 | + OpTypePattern('Mul', name='scale_mul', inputs=[ |
| 56 | + OpTypePattern('Mul', inputs=[ |
| 57 | + inner_pattern, |
| 58 | + OpTypePattern('Sub', inputs=[ |
| 59 | + OpTypePattern('*', name='input_r3'), |
| 60 | + OpTypePattern('Mean', name='mean_r2') |
| 61 | + ]) |
| 62 | + ]), |
| 63 | + OpTypePattern('*', name='scale') |
| 64 | + ]), |
| 65 | + OpTypePattern('*', name='bias'), |
| 66 | + ]) |
| 67 | + pattern2 = \ |
| 68 | + OpTypePattern('Add', name='bias_add', inputs=[ |
| 69 | + OpTypePattern('Mul', name='scale_mul', inputs=[ |
| 70 | + OpTypePattern('Mul', inputs=[ |
| 71 | + OpTypePattern('*', name='scale'), |
| 72 | + OpTypePattern('Sub', inputs=[ |
| 73 | + OpTypePattern('*', name='input_r3'), |
| 74 | + OpTypePattern('Mean', name='mean_r2') |
| 75 | + ]) |
| 76 | + ]), |
| 77 | + inner_pattern |
| 78 | + ]), |
| 79 | + OpTypePattern('*', name='bias'), |
| 80 | + ]) |
| 81 | + |
| 82 | + pattern_list = [pattern0, pattern1, pattern2] |
| 83 | + |
| 84 | + for pattern in pattern_list: |
| 85 | + matcher = GraphMatcher(pattern, allow_reorder=True) |
| 86 | + match_results = list(matcher.match_ops(ops)) |
| 87 | + if match_results: |
| 88 | + for match in match_results: |
| 89 | + inp_node = match.get_op('input') |
| 90 | + rank = g.get_rank(inp_node.output[0]) |
| 91 | + node = match.get_op('bias_add') |
| 92 | + if inp_node.name != match.get_op('input_r2').name or inp_node.name != match.get_op('input_r3').name: |
| 93 | + continue |
| 94 | + if match.get_op('mean').name != match.get_op('mean_r2').name: |
| 95 | + continue |
| 96 | + inp = match.get_op('mean').input[0] |
| 97 | + if rank != 3: |
| 98 | + continue |
| 99 | + mean_axes = match.get_op('mean_axes').get_tensor_value(as_list=True) |
| 100 | + variance_axes = match.get_op('variance_axes').get_tensor_value(as_list=True) |
| 101 | + mean_axes = [a % rank for a in mean_axes] |
| 102 | + variance_axes = [a % rank for a in variance_axes] |
| 103 | + if mean_axes != [2] or variance_axes != [2]: |
| 104 | + continue |
| 105 | + epsilon = match.get_op('epsilon').get_tensor_value(as_list=False).flatten().tolist() |
| 106 | + if len(epsilon) != 1: |
| 107 | + continue |
| 108 | + scale = match.get_op('scale').output[0] |
| 109 | + bias = match.get_op('bias').output[0] |
| 110 | + shape = g.make_node("Shape", [inp]).output[0] |
| 111 | + dim_2_shape = GraphBuilder(g).make_slice( |
| 112 | + {"data": shape, "ends": [2], "starts": [1], "axes": [0]}) |
| 113 | + zero_tensor = helper.make_tensor("value", TensorProto.FLOAT, dims=[1], vals=[0]) |
| 114 | + one_tensor = helper.make_tensor("value", TensorProto.FLOAT, dims=[1], vals=[1]) |
| 115 | + zeros_of_shape = g.make_node("ConstantOfShape", [dim_2_shape], attr={'value': zero_tensor}).output[0] |
| 116 | + ones_of_shape = g.make_node("ConstantOfShape", [dim_2_shape], attr={'value': one_tensor}).output[0] |
| 117 | + norm = g.make_node("InstanceNormalization", [inp, ones_of_shape, zeros_of_shape], |
| 118 | + attr={'epsilon': epsilon[0]}, op_name_scope=node.name).output[0] |
| 119 | + mul = g.make_node("Mul", [norm, scale]).output[0] |
| 120 | + add = g.make_node("Add", [mul, bias]).output[0] |
| 121 | + g.replace_all_inputs(node.output[0], add) |
| 122 | + g.remove_node(node.name) |
| 123 | + return ops |
0 commit comments