Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / common / partial_infer / inner_product.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 numpy as np
18
19 from mo.front.common.partial_infer.utils import assign_dims_to_weights, int64_array
20 from mo.front.common.partial_infer.utils import mark_input_bins
21 from mo.ops.op import PermuteAttrs
22 from mo.utils.error import Error
23
24
25 def caffe_inner_product(node):
26     input_shape = node.in_node(0).shape
27     if input_shape is None:
28         return
29     batches = input_shape[0]
30     input_channels = np.prod(input_shape[1:])
31     if not node.has_valid('out-size'):
32         node['out-size'] = (np.prod(node.in_node(1).shape) / input_channels).astype(np.int64)
33     output_channels = node['out-size']
34
35     weights_shape = np.array([output_channels, input_channels], dtype=np.int64)
36
37     # In case if original weight layout is IO we transpose them
38     if np.array_equal(node.in_node(1).shape, weights_shape[::-1]) and node.soft_get('transpose_weights') is True:
39         node.in_node(1).value = np.transpose(node.in_node(1).value)
40
41     node.out_node().shape = np.array([batches, output_channels], dtype=np.int64)
42     # Back propagation of shape to weights
43     node.in_node(1).shape = np.array(weights_shape)
44     node.in_node(1).value.shape = node.in_node(1).shape
45
46     mark_input_bins(node)
47     assign_dims_to_weights(node.in_node(1), None, 1, 0, 2)
48     PermuteAttrs.set_permutation(node.in_node(1), node, None)
49
50
51 def onnx_matmul_infer(node):
52     if len(node.in_nodes()) != 2:
53         raise Error("Wrong number of input nodes for {} node. Should be 2 instead of {}".format(node.name,
54                                                                                                 len(node.in_nodes())))
55     input_0_shape = node.in_node(0).shape
56     input_1_shape = node.in_node(1).shape
57
58     input_shapes = [node.in_node(port).shape for port in node.in_nodes()]
59     max_len = max([len(shape) for shape in input_shapes])
60     new_input_shapes = [np.concatenate([np.ones(max_len - len(input_shapes[i])), input_shapes[i]], axis=0)
61                         for i in range(len(input_shapes))]
62
63     node.out_node().shape = np.concatenate([np.maximum(*[shape[0:-2] for shape in new_input_shapes]),
64                                             [input_0_shape[-2], input_1_shape[-1]]], axis=0)