2 Copyright (c) 2018 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
19 from mo.middle.replacement import MiddleReplacementPattern
20 from mo.ops.eltwise import Eltwise
21 from mo.ops.power import Power
24 class FusedBatchNormNonConstant(MiddleReplacementPattern):
26 Replaces FusedBatchNorm(input, beta, gamma, mean, variance) with non-constant mean and variance,
27 but with constant beta and gamma to a sub-expression consisting of a combinatin of Eltwise and Power
28 layers and ScaleShift.
36 ('op', dict(kind='op', op='FusedBatchNorm'))],
38 node_attrs=['kind', 'op'],
41 def replace_pattern(self, graph: nx.MultiDiGraph, match: dict):
43 if (node.data_format != b'NHWC' or
44 len(node.in_nodes()) != 5 or
45 node.in_node(0).value is not None or # input
46 node.in_node(1).value is None or # scale
47 node.in_node(2).value is None or # offset
48 node.in_node(3).value is not None or # mean
49 node.in_node(4).value is not None or # variance
50 node.in_node(1).value.ndim != 1 or
51 node.in_node(2).value.ndim != 1):
54 scale_mul = Eltwise(graph, dict(operation='mul', name=node.name + '/scale_mul_'))
55 shift_add = Eltwise(graph, dict(operation='sum', name=node.name + '/shift_add_'))
56 mean_add = Eltwise(graph, dict(operation='sum', name=node.name + '/mean_add_'))
57 variance_mul = Eltwise(graph, dict(operation='mul', name=node.name + '/variance_mul_'))
59 mean_negate = Power(graph, dict(scale=-1, name=node.name + '/mean_negate_'))
60 mean_arg = mean_add.create_node_with_data([
62 mean_negate.create_node_with_data([node.in_node(3)])])
64 variance_square = Power(graph, dict(power=2, name=node.name + '/variance_square_'))
65 variance_denom = Power(graph, dict(shift=node.eps, power=-0.5, name=node.name + '/variance_denom_'))
66 variance_arg = variance_mul.create_node_with_data([
68 variance_denom.create_node_with_data([node.in_node(4)])])
70 shift_add.create_node_with_data([
71 scale_mul.create_node_with_data([
75 data_nodes=node.out_node())
77 node.graph.remove_node(node.id)