Implement LookupTableInsert shape inference (#2348)
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / LookupTableInsert.py
1 """
2  Copyright (C) 2020 Intel Corporation
3
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
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 """
16
17 import numpy as np
18
19 from mo.front.common.partial_infer.utils import int64_array
20 from mo.graph.graph import Node, Graph
21 from mo.ops.op import Op
22
23
24 class LookupTableInsert(Op):
25     '''
26     This operation has only output control flow edges and no output data edges in some models.
27     And for these cases implementation of the shape inference is needed since the shape inference is executed
28     before control flow edges resolving. This operation has non-tensor output so the output shape is empty.
29     '''
30     enabled = False
31     op = 'LookupTableInsert'
32
33     def __init__(self, graph: Graph, attrs: dict):
34         mandatory_props = {
35             'type': None,
36             'op': self.op,
37             'infer': self.infer,
38             'in_ports_count': 3,
39             'out_ports_count': 1,
40         }
41         super().__init__(graph, mandatory_props, attrs)
42
43     @staticmethod
44     def infer(node: Node):
45         node_name = node.soft_get('name', node.id)
46         connected_in_ports = [port for port in node.in_ports().values() if not port.disconnected()]
47         assert len(connected_in_ports) == 3, \
48             "Incorrect number of inputs for {} node".format(node_name)
49
50         # check shapes of input tensors
51         keys_shape = node.in_port(1).data.get_shape()
52         values_shape = node.in_port(2).data.get_shape()
53         assert np.array_equal(keys_shape, values_shape), \
54             'Shapes of tensors with keys and values must be equal for {} node'.format(node_name)
55
56         # set output shape that must be empty
57         # since output is not a tensor
58         node.out_port(0).data.set_shape(int64_array([]))