Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / gather_test.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 unittest
18
19 import numpy as np
20
21 from extensions.ops.gather import Gather
22 from mo.front.common.partial_infer.utils import int64_array
23 from mo.graph.graph import Node
24 from mo.utils.unittest.graph import build_graph
25
26
27 class TestGatherPartialInfer(unittest.TestCase):
28     @staticmethod
29     def _create_graph():
30         nodes_attributes = {'gather_input': {'shape': None, 'value': None, 'kind': 'data'},
31                             'gather_input2': {'shape': None, 'value': None, 'kind': 'data'},
32                             'gather_node': {'op': 'Gather', 'kind': 'op'},
33                             'gather_output': {'shape': None, 'value': None, 'kind': 'data'}
34                             }
35         return build_graph(nodes_attributes,
36                            [
37                                ('gather_input', 'gather_node'), ('gather_node', 'gather_output'), ('gather_input2', 'gather_node')
38                            ],
39                            {
40                                'gather_input': {'shape': int64_array([10, 15]), 'value': np.ones((3, 15))},
41                                'gather_input2': {'shape': int64_array([2]), 'value': np.array([0, 2])},
42                                'gather_node': {'axis': 0},
43                            })
44
45     def test_gather_infer(self):
46         graph = self._create_graph()
47
48         gather_node = Node(graph, 'gather_node')
49         Gather.infer(gather_node)
50
51         exp_shape = int64_array([2, 15])
52         res_shape = graph.node['gather_output']['shape']
53         res_value = graph.node['gather_output']['value']
54
55         self.assertTrue(np.array_equal(exp_shape, res_shape),
56                         'shapes do not match expected: {} and given: {}'.format(exp_shape, res_shape))
57
58         self.assertTrue(np.array_equal(res_value, np.ones(exp_shape)),
59                         'shapes do not match expected: {} and given: {}'.format(exp_shape, res_shape))