Files
openvino/model-optimizer/extensions/back/TransposeReduceFusing.py
T
Evgeny Lazarev 125a462400 Enable ReduceL1 and ReduceL2 operations (#1799)
* Initial version of ReduceL1, ReduceL2 and ReduceLp enabling in the MO

* Added operations ReduceL1 and ReduceL2 to nGraph

* Removed ReduceLp. Added ReduceL1 and ReduceL2

* Separated specification of ReduceLp into ReduceL1 and ReduceL2

* Updated ReduceL1 and ReduceL2 specification

* Fixed ReduceL1 and ReduceL2 type prop tests

* Implemented nGraph transformation to decompose ReduceL1 and ReduceL2. Disabled them for CPU and GPU plugins

* Updated supported framework layers

* Added unit tests for ReduceL1 and ReduceL2 reference implementation

* Fixed ReduceXXX operations reference implementation by adding support for a new parameter 'keep_dims'

* Fixed constant folding for v0::Any

* Added ReduceL1 and ReduceL2 to Python API

* Implemented ReduceL1 and ReduceL2 decomposition tests and fixed ReduceL2 decomposition

* Added specific creator for ReduceXXX operations instead of NodeBuilders

* Fixed conversion ReduceXXX to CNNLayer

* Fixed parser for ReduceLogicalXXX operations
2020-08-17 16:32:53 +03:00

81 lines
3.3 KiB
Python

"""
Copyright (C) 2018-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from typing import Dict
import numpy as np
from extensions.back.FuseTransposesSequence import FuseTransposesSequence
from extensions.back.ReduceMerge import ReduceMerge
from extensions.ops.ReduceOps import reduce_map
from extensions.ops.gather import Gather
from mo.back.replacement import BackReplacementPattern
from mo.front.common.partial_infer.utils import int64_array
from mo.front.tf.graph_utils import create_op_with_const_inputs
from mo.graph.graph import Graph, Node
class TransposeReduce(BackReplacementPattern):
"""
Fuse Transpose--->Reduce to Reduce with correct reduce axis input
"""
# TODO: Make another implementation, this is a temporary solution for one case
enabled = True
force_clean_up = True
def run_before(self):
return [ReduceMerge]
def run_after(self):
return [FuseTransposesSequence]
@staticmethod
def pattern():
return dict(
nodes=[
('transpose_const', dict(kind='op', type='Const', value=lambda v: v is not None and
np.array_equal(v, int64_array([0, 2, 3, 1])))),
('transpose_const_data', dict(kind='data')),
('transpose', dict(kind='op', type='Transpose')),
('transpose_data', dict(kind='data')),
('reduce_const', dict(kind='op', type='Const', value=lambda v: v is not None and
np.array_equal(v, int64_array([1, 2])))),
('reduce_const_data', dict(kind='data')),
('reduce', dict(kind='op', type=lambda t: t in reduce_map.keys(), keep_dims=False))
],
edges=[
('transpose_const', 'transpose_const_data'),
('transpose_const_data', 'transpose', {'in': 1}),
('transpose', 'transpose_data'),
('transpose_data', 'reduce', {'in': 0}),
('reduce_const', 'reduce_const_data'),
('reduce_const_data', 'reduce', {'in': 1})
]
)
@staticmethod
def replace_pattern(graph: Graph, match: Dict[str, Node]):
transpose = match['transpose']
reduce = match['reduce']
gather = create_op_with_const_inputs(graph, op=Gather, port_value_dict={2: int64_array(0)},
op_attrs={'name': reduce.name + 'Gather'})
transpose.in_port(1).get_connection().set_destination(gather.in_port(0))
reduce.in_port(1).get_connection().set_destination(gather.in_port(1))
gather.out_port(0).connect(reduce.in_port(1))
transpose.out_port(0).disconnect()
transpose.in_port(0).get_connection().set_destination(reduce.in_port(0))