Fix of groupconv_to_conv() in MO IR reader (#17118)

* Fixed bug in groupconv_to_conv() method.

* Small correction.

* Test correction.
This commit is contained in:
Anastasiia Pnevskaia
2023-04-28 09:25:29 +02:00
committed by GitHub
parent dd0b1f0ab3
commit 269ed1d9cc
2 changed files with 36 additions and 55 deletions

View File

@@ -8,7 +8,7 @@ import numpy as np
from openvino.tools.mo.back.MaxPool import MaxPool
from openvino.tools.mo.back.TopKNormalizer import TopKNormalizer
from openvino.tools.mo.front.common.partial_infer.utils import int64_array
from openvino.tools.mo.front.common.partial_infer.utils import int64_array, strict_compare_tensors
from openvino.tools.mo.graph.graph import Graph, Node
from openvino.tools.mo.ops.Cast import Cast
from openvino.tools.mo.ops.GRU import GRU
@@ -197,12 +197,13 @@ def groupconv_to_conv(op: Node):
weights_node.value = np.reshape(weights_node.value, new_shape)
elif weights_node.type == 'Reshape':
# We remove reshape node added in ConvolutionWithGroupsResolver pass
assert weights_node.in_port(0).get_source().data.get_shape() == new_shape, \
assert strict_compare_tensors(weights_node.in_port(0).get_source().data.get_shape(), new_shape), \
'Weight shape and calculated shape mismatch in GroupConv node {}.'.format(op.name)
op.in_port(1).disconnect()
# We use add_destination method here to support case with multiple destinations of source port
weights_node.in_port(0).get_source().get_connection().add_destination(op.in_port(1))
weights_node.in_port(0).disconnect()
op.graph.remove_node(weights_node.id)
elif weights_node.type == 'Convert' and weights_node.destination_type == 'f32'\
and weights_node.in_port(0).get_source().node.type == 'Const':
# Support new FP16 IRs
@@ -212,7 +213,7 @@ def groupconv_to_conv(op: Node):
const_node.value = np.reshape(const_node.value, new_shape)
else:
assert op.in_port(1).get_source().data.get_shape() == new_shape, \
assert strict_compare_tensors(op.in_port(1).get_source().data.get_shape(), op.in_port(1).get_source().data.get_shape()), \
'Weight shape and calculated shape mismatch in GroupConv node {}.'.format(op.name)
# We need to set this attrs for correct shape infer as convolution
op['group'] = group

View File

@@ -6,85 +6,63 @@ import unittest
import numpy as np
from generator import generator, generate
import openvino.tools.mo.graph.graph
from openvino.tools.mo.graph.graph import Node
from openvino.tools.mo.utils.ir_engine.compare_graphs import compare_graphs
from openvino.tools.mo.utils.ir_reader.internal_ops.squeeze import SqueezeInternal
from openvino.tools.mo.utils.ir_reader.internal_ops.unsqueeze import UnsqueezeInternal
from openvino.tools.mo.utils.ir_reader.layer_to_class import groupconv_to_conv, restore_tensor_names
from unit_tests.utils.graph import build_graph
from unit_tests.utils.graph import connect_data,shaped_parameter, regular_op_with_shaped_data, connect, valued_const_with_data, shaped_const_with_data, result
from openvino.tools.mo.ops.op import Op
@generator
class TestFunction(unittest.TestCase):
@generate(*[([1, 32, 112, 112], [32, 1, 1, 3], [32, 1, 1, 1, 3], 32),
([1, 32, 112, 112], [32, 1, 1, 1, 3], None, 32),
])
def test_groupconv_to_conv(self, shape, weights_shape, reshape_shape, group):
weights_const = np.random.randn(*weights_shape).astype(np.float32)
nodes_attributes = {
'input': {'kind': 'op', 'type': 'Parameter'},
'input_data': {'shape': shape, 'kind': 'data'},
'group_conv': {'kind': 'op', 'type': 'GroupConvolution'},
'group_conv_data': {'shape': shape, 'kind': 'data'},
'conv': {'kind': 'op', 'type': 'Convolution', 'group': group},
'conv_data': {'shape': shape, 'kind': 'data'},
'weights': {'kind': 'op', 'type': 'Const', 'value': weights_const},
'weights_data': {'shape': weights_shape, 'kind': 'data'},
'reshape': {'kind': 'op', 'type': 'Reshape'},
'reshape_data': {'shape': reshape_shape, 'kind': 'data'},
'reshape_const': {'kind': 'op', 'type': 'Const'},
'reshape_const_data': {'shape': len(reshape_shape) if reshape_shape is not None else None, 'kind': 'data'},
'add': {'kind': 'op', 'type': 'Add'},
'add_data': {'shape': shape, 'kind': 'data'},
'add_const': {'kind': 'op', 'type': 'Const'},
'add_const_data': {'shape': [1, 32, 1, 1], 'kind': 'data'},
'result': {'kind': 'op', 'type': 'Result'}
**shaped_parameter('input', shape),
**regular_op_with_shaped_data('group_conv', shape, {'type': 'GroupConvolution'}),
**regular_op_with_shaped_data('conv', shape, {'type': 'Convolution'}),
**valued_const_with_data('weights', weights_const, weights_shape, {'type': 'Const'}),
**regular_op_with_shaped_data('reshape', reshape_shape, {'type': 'Reshape'}),
**shaped_const_with_data('reshape_const', len(reshape_shape) if reshape_shape is not None else None, {'type': 'Const'}),
**regular_op_with_shaped_data('add', shape, {'type': 'Add'}),
**shaped_const_with_data('add_const', [1, 32, 1, 1], {'type': 'Const'}),
**result("result")
}
edges = [('input', 'input_data'),
('input_data', 'group_conv'),
('weights', 'weights_data'),
('group_conv', 'group_conv_data'),
('group_conv_data', 'add'),
('add_const', 'add_const_data'),
('add_const_data', 'add'),
('add', 'add_data'),
('add_data', 'result'),
edges = [*connect('input:0', 'group_conv:0'),
*connect('group_conv:0', 'add:0'),
*connect('add_const:0', 'add:1'),
*connect('add:0', 'result:0'),
]
if reshape_shape is not None:
edges += [('weights_data', 'reshape'),
('reshape_const', 'reshape_const_data'),
('reshape_const_data', 'reshape'),
('reshape', 'reshape_data'),
('reshape_data', 'group_conv')]
edges += [*connect('weights:0', 'reshape:0'),
*connect('reshape_const:0', 'reshape:1'),
*connect('reshape:0', 'group_conv:1')]
else:
edges.append(('weights_data', 'group_conv'))
edges += [*connect('weights:0', 'group_conv:1')]
graph = build_graph(nodes_attributes, edges, nodes_with_edges_only=True)
graph = build_graph(nodes_attributes, edges)
reshape_node = None
if reshape_shape is None:
reshape_node = Node(graph, 'reshape')
graph_ref = build_graph(nodes_attributes,
[('input', 'input_data'),
('input_data', 'conv'),
('weights', 'weights_data'),
('weights_data', 'conv'),
('conv', 'conv_data'),
('conv_data', 'add'),
('add_const', 'add_const_data'),
('add_const_data', 'add'),
('add', 'add_data'),
('add_data', 'result'),
], nodes_with_edges_only=True)
[*connect('input:0', 'conv:0'),
*connect('weights:0', 'conv:1'),
*connect('conv:0', 'add:0'),
*connect('add_const:0', 'add:1'),
*connect('add:0', 'result:0'),
])
for op in graph.get_op_nodes(type='GroupConvolution'):
groupconv_to_conv(op)
@@ -94,6 +72,8 @@ class TestFunction(unittest.TestCase):
node = Node(graph_ref, 'weights')
node.value = weights_const
assert len(reshape_node.in_nodes()) == 0 and len(reshape_node.out_nodes()) == 0
(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
self.assertTrue(flag, resp)