Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / ops / tile.py
1 """
2  Copyright (c) 2018-2019 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 logging as log
18 import numpy as np
19
20 from mo.graph.graph import Node, Graph
21 from mo.ops.op import Op, PermuteAttrs
22
23
24 class Tile(Op):
25     op = 'Tile'
26     enabled = True
27
28     def __init__(self, graph: Graph, attrs: dict):
29         super().__init__(graph, {
30             'kind': 'op',
31             'type': __class__.op,
32             'op': __class__.op,
33             'in_ports_count': 1,
34             'out_ports_count': 1,
35             'infer': Tile.infer
36         }, attrs)
37
38     def supported_attrs(self):
39         return ['axis', 'tiles']
40
41     @staticmethod
42     def infer(node: Node):
43         shape = node.in_node().shape
44         if shape is None:
45             log.error("Undefined shape for the input tiles for the Tile operation '{}'.".format(node.node))
46             return
47         shape = np.copy(shape)
48
49         if len(node.in_nodes()) == 2:
50             tile_array = node.in_node(1).value
51             if tile_array is None:
52                 log.error('A tile values are None for a node "{}".'.format(node.name))
53                 return
54             if len(shape) != len(tile_array):
55                 log.error('Shape mismatch for a node "{}": {} vs {}.'.format(node.name, shape.shape, tile_array.shape))
56                 return
57             non_one_tile = np.argwhere(tile_array != 1)
58             if len(non_one_tile) == 0:
59                 log.info(
60                     'Redundant "Tile" operation "{}" with tile values for all dimensions equal to 1.'.format(node.name))
61                 node['axis'] = 0
62                 node['tiles'] = 1
63             elif len(non_one_tile) == 1:
64                 node['axis'] = non_one_tile[0][0]
65                 node['tiles'] = tile_array[node['axis']]
66             else:
67                 node['type'] = None
68                 log.warning("Tile operation with more than one dimension not equal to 1 is not supported.")
69                 # do not return here to allow infer shape and values for the constant propagation case
70             node.graph.remove_edge(node.in_node(1).id, node.id)
71         elif len(node.in_nodes()) == 1:  # case when tiled dimension and count are specified in node attributes
72             if not node.has_valid('axis') or not node.has_valid('tiles'):
73                 log.error('Mandatory attributes "axis" or "tiles" are not specified for a Tile node "{}"'.
74                           format(node.name))
75                 return
76             tile_array = np.ones([len(shape)], dtype=np.int64)
77             tile_array[node.axis] = node.tiles
78         else:
79             log.error('Unsupported number of input parameters to Tile node "{}"'.format(node.name))
80             return
81
82         PermuteAttrs.create_permute_attrs(node, attrs=[('axis', 'input:0')])
83         node.out_node().shape = shape * tile_array
84         if node.in_node(0).value is not None:
85             node.out_node().value = np.tile(node.in_node(0).value, tile_array)