Пример #1
0
    def test_broadcast_with_range_positive_test(self):
        graph = build_graph({
            **regular_op_with_shaped_data('shape', [2], {'type': 'Parameter'}),
            **valued_const_with_data('value', np.arange(0, 384).reshape((1, 384))),
            **regular_op_with_empty_data('bc', {'type': 'Broadcast'}),
            **result(),
        }, [
            *connect('value', '0:bc'),
            *connect('shape', '1:bc'),
            *connect('bc', 'output'),
        ], nodes_with_edges_only=True)
        ExpandRangeConstant().find_and_replace_pattern(graph)

        graph_ref = build_graph({
            **regular_op_with_shaped_data('shape', [2], {'type': 'Parameter'}),

            # start
            **valued_const_with_data('start', np.array(0)),
            # limit
            **valued_const_with_data('minus_one', np.array(-1)),
            **valued_const_with_data('zero', np.array(0)),
            **regular_op_with_empty_data('range_dim', {'type': 'Gather'}),
            # delta
            **valued_const_with_data('delta', np.array(1)),
            **regular_op_with_empty_data('range', {'type': 'Range'}),

            # keep dims
            **valued_const_with_data('axes', np.array([0])),
            **regular_op_with_empty_data('keep_shape', {'type': 'Unsqueeze'}),

            **regular_op_with_empty_data('bc', {'type': 'Broadcast'}),
            **result(),
        }, [
            *connect('start', '0:range'),
            *connect('shape', '0:range_dim'),
            *connect('minus_one', '1:range_dim'),
            *connect('zero', '2:range_dim'),
            *connect('range_dim', '1:range'),
            *connect('delta', '2:range'),
            *connect('range', '0:keep_shape'),
            *connect('axes', '1:keep_shape'),
            *connect('keep_shape', '0:bc'),
            *connect_data('shape', '1:bc'),
            *connect('bc', 'output'),
        ], nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #2
0
    def test_scatterelements_value_infer(self, data, indices, updates, axis,
                                         ref_res):
        nodes = {
            **valued_const_with_data('data', np.array(data)),
            **valued_const_with_data('indices', int64_array(indices)),
            **valued_const_with_data('updates', np.array(updates)),
            **valued_const_with_data('axis', int64_array(axis)),
            **regular_op_with_empty_data('scatter_elements', {
                'op': 'ScatterElementsUpdate',
                'axis': axis
            }),
            **result()
        }

        graph = build_graph(nodes_attrs=nodes,
                            edges=[
                                *connect('data', '0:scatter_elements'),
                                *connect('indices', '1:scatter_elements'),
                                *connect('updates', '2:scatter_elements'),
                                *connect('axis', '3:scatter_elements'),
                                *connect('scatter_elements', 'output')
                            ],
                            nodes_with_edges_only=True)
        graph.stage = 'middle'

        scatter_el_node = Node(graph, 'scatter_elements')
        ScatterElementsUpdate.infer(scatter_el_node)

        res_output_shape = scatter_el_node.out_node().shape
        self.assertTrue(
            np.array_equal(int64_array(ref_res).shape, res_output_shape))

        res_output_value = scatter_el_node.out_node().value
        self.assertTrue(np.array_equal(ref_res, res_output_value))
Пример #3
0
    def test_broadcast(self, data, target_shape, axes_mapping=None, mode='numpy', ref_out=None, test_raising=False):
        if ref_out is not None:
            input = valued_const_with_data('data', int64_array(data))
        else:
            input = shaped_data('data', int64_array(data))

        nodes = {
            **input,
            **valued_const_with_data('target_shape', int64_array(target_shape)),
            **regular_op_with_empty_data('broadcast', {'op': 'Broadcast', 'mode': mode}),
        }

        edges = [('data', 'broadcast'),
                 ('target_shape', 'broadcast'),
                 ('broadcast', 'broadcast_d')]

        if axes_mapping is not None:
            nodes.update(**valued_const_with_data('axes_mapping', int64_array(axes_mapping)))
            edges.append(('axes_mapping', 'broadcast'))
        graph = build_graph(nodes, edges)

        broadcast_node = Node(graph, 'broadcast')
        if test_raising:
            self.assertRaises(AssertionError, Broadcast.infer, broadcast_node)
            return

        Broadcast.infer(broadcast_node)
        if ref_out is not None:
            self.assertTrue(np.array_equal(broadcast_node.out_node().value, np.array(ref_out)))
        else:
            self.assertTrue(np.array_equal(broadcast_node.out_node().shape, np.array(target_shape)))
Пример #4
0
        def test_slice_infer(self, inp_value, starts, ends, axes, steps, expected, inp_shape=None):
            if inp_value is None:
                input_node = shaped_data('data_1', int64_array(inp_shape))
            else:
                input_node = valued_data('data_1', int64_array(inp_value))

            nodes = {
                **input_node,
                **regular_op_with_empty_data('slice', {'op': 'Slice'}),
                **valued_const_with_data('starts', int64_array(starts)),
                **valued_const_with_data('ends', int64_array(ends)),
                **valued_const_with_data('axes', int64_array(axes)),
                **valued_const_with_data('steps', int64_array(steps)),
            }

            graph = build_graph(nodes,
                                [('data_1', 'slice'),
                                 *connect('starts', '1:slice'),
                                 *connect('ends', '2:slice'),
                                 *connect('axes', '3:slice'),
                                 *connect('steps', '4:slice'),
                                 *connect('slice', 'slice_d')])

            graph.stage = 'middle'
            slice_node = Node(graph, 'slice')

            Slice.infer(slice_node)
            if inp_value is not None:
                self.assertTrue(np.array_equal(slice_node.out_node().value, expected))
            else:
                self.assertTrue(np.array_equal(slice_node.out_node().shape, expected))
Пример #5
0
        def test_slice_infer_negative(self, inp_value, starts, ends, axes, steps, expected, inp_shape=None):
            if inp_value is None:
                input_node = shaped_data('data_1', int64_array(inp_shape))
            else:
                input_node = valued_data('data_1', int64_array(inp_value))

            def convert_args(val, name=''):
                if val is not None:
                    return valued_const_with_data(name, int64_array(val))
                else:
                    return shaped_const_with_data(name, [0])  #fake shape

            starts = convert_args(starts, 'starts')
            ends = convert_args(ends, 'ends')
            axes = convert_args(axes, 'axes')
            steps = convert_args(steps, 'steps')

            nodes = { **input_node,
                      **regular_op_with_empty_data('slice', {'op': 'Slice'}),
                      **starts, **ends, **axes, **steps }

            graph = build_graph(nodes,
                                [('data_1', 'slice'),
                                 *connect('starts', '1:slice'),
                                 *connect('ends', '2:slice'),
                                 *connect('axes', '3:slice'),
                                 *connect('steps', '4:slice'),
                                 *connect('slice', 'slice_d')])

            graph.stage = 'middle'
            slice_node = Node(graph, 'slice')
            self.assertRaises(Error, Slice.infer, slice_node)
Пример #6
0
    def test_gatherelements_value_infer(self, data, indices, axis, ref_res):
        nodes = {
            **valued_const_with_data('data', int64_array(data)),
            **valued_const_with_data('indices', int64_array(indices)),
            **regular_op_with_empty_data('gather_elements', {
                'op': 'GatherElements',
                'axis': axis
            }),
            **result()
        }

        graph = build_graph(nodes_attrs=nodes,
                            edges=[
                                *connect('data', '0:gather_elements'),
                                *connect('indices', '1:gather_elements'),
                                *connect('gather_elements', 'output')
                            ],
                            nodes_with_edges_only=True)
        graph.stage = 'middle'

        gather_el_node = Node(graph, 'gather_elements')
        GatherElements.infer(gather_el_node)

        res_output_shape = gather_el_node.out_node().shape
        self.assertTrue(
            np.array_equal(int64_array(ref_res).shape, res_output_shape))

        res_output_value = gather_el_node.out_node().value
        if res_output_value is not None:
            self.assertTrue(
                np.array_equal(int64_array(ref_res), res_output_value))
Пример #7
0
    def test_pool_v2_to_attributed_pool(self):
        nodes = {
            **shaped_const_with_data('input', int64_array([200, 200])),
            **valued_const_with_data('windows', int64_array([4, 4])),
            **valued_const_with_data('strides', int64_array([4, 4])),
            **regular_op_with_empty_data(
                'pool_v2', {
                    'op': 'PoolingV2',
                    'pad': [2, 2],
                    'spatial_dims': [1, 2],
                    'auto_pad': 'same_upper',
                    'output_spatial_shape': [2, 3],
                    'pad_spatial_shape': [1, 2],
                    'pool_method': 'max',
                    'permute_attrs': None
                }),
            **regular_op_with_empty_data(
                'pool_v1', {
                    'type': 'Pooling',
                    'pad': [2, 2],
                    'spatial_dims': [1, 2],
                    'auto_pad': 'same_upper',
                    'output_spatial_shape': [2, 3],
                    'pad_spatial_shape': [1, 2],
                    'pool_method': 'max'
                }),
            **result('output')
        }

        edges = [
            *connect('input', 'pool_v2:0'),
            *connect('windows', 'pool_v2:1'),
            *connect('strides', 'pool_v2:2'),
            *connect('pool_v2', 'output'),
        ]

        graph = build_graph(nodes, edges, nodes_with_edges_only=True)
        PoolV2ToAttributedPool().find_and_replace_pattern(graph)

        ref_graph = build_graph(
            nodes,
            [*connect('input', 'pool_v1'), *connect('pool_v1', 'output')],
            nodes_with_edges_only=True)
        (flag, resp) = compare_graphs(graph, ref_graph, 'output')
        self.assertTrue(flag, resp)
Пример #8
0
    def test_attributed_slice_replacer(self, attributed_slice_attrs):
        nodes = {
            **regular_op_with_empty_data('input', {'type': 'Parameter'}),
            **regular_op_with_empty_data('attributed_slice', attributed_slice_attrs),
            **result(),

            # nodes after replacement
            **const('start', np.array([0, 0])),
            **const('end', np.array([1, -1])),
            **const('axis', np.array(np.array([0, 1]))),
            **regular_op_with_empty_data('slice', {
                'op': 'Slice',
                'type': None
            }),
        }

        graph = build_graph(nodes_attrs=nodes,
                            edges=[
                                ('input', 'attributed_slice'),
                                ('attributed_slice', 'output'),
                            ],
                            nodes_with_edges_only=True)
        graph.stage = 'front'

        AttributedSliceToSliceReplacer().find_and_replace_pattern(graph)

        graph_ref = build_graph(nodes_attrs=nodes,
                                edges=[
                                    ('input', 'slice'),
                                    *connect_front('start', '1:slice'),
                                    *connect_front('end', '2:slice'),
                                    *connect_front('axis', '3:slice'),
                                    ('slice', 'output'),
                                ],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #9
0
    def test_one(self):
        nodes = {
            **regular_op_with_empty_data('input', {'type': 'Parameter'}),
            **regular_op_with_empty_data('some_op', {
                'type': 'SomeOp',
                'name': 'some_op_name'
            }),
            **regular_op_with_empty_data(
                'fake_output', {
                    'type': None,
                    'kind': 'op',
                    'op': 'FakeOutput',
                    'name': 'my_output_name'
                }),
            **result('result'),
        }
        edges = [
            *connect('input', 'some_op'),
            *connect('some_op', 'fake_output'),
            *connect('fake_output', 'result'),
        ]
        graph = build_graph(nodes, edges)

        edges_ref = [
            *connect('input', 'some_op'),
            *connect('some_op', 'result'),
        ]

        graph_ref = build_graph(nodes, edges_ref,
                                {'some_op': {
                                    'name': 'my_output_name'
                                }})

        FakeOutputResolver().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph, graph_ref, 'result')
        self.assertTrue(flag, resp)
    def test_v10_group_convolution_resolver(self):
        nodes = {
            **regular_op_with_shaped_data('input', [1, 3, 224, 224], {
                                              'type': 'Parameter'
                                          }),
            **valued_const_with_data('weights', np.ones([3, 8, 7, 7])),
            **const_with_data('dim', int64_array([3, 8, 1, 7, 7])),
            **regular_op_with_empty_data('reshape', {'type': 'Reshape'}),
            **regular_op_with_shaped_data('convolution', None, {
                'type': 'Convolution',
                'group': 3,
                'output': 24
            }),
            **result(),
        }
        graph = build_graph(nodes, [
            *connect('input', '0:convolution'),
            *connect('weights', '1:convolution'),
            *connect('convolution', 'output'),
        ],
                            nodes_with_edges_only=True)

        V10ConvolutionWithGroupsResolver().find_and_replace_pattern(graph)

        nodes['convolution']['type'] = 'GroupConvolution'
        del nodes['convolution']['group']

        graph_ref = build_graph(nodes, [
            *connect('input', '0:convolution'),
            *connect('weights', '0:reshape'),
            *connect('dim', '1:reshape'),
            *connect('reshape', '1:convolution'),
            *connect('convolution', 'output'),
        ],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      last_node='output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #11
0
    def test_multi(self):
        nodes = {
            **regular_op_with_empty_data('input', {'type': 'Parameter'}),
            **regular_op_with_empty_data('some_op', {'type': 'SomeOp', 'name': 'some_op_name'}),
            **empty_data('some_op_d2'),
            **regular_op_with_empty_data('fake_output1',
                                         {'type': None, 'kind': 'op', 'op': 'FakeOutput', 'name': 'my_output_name1'}),
            **regular_op_with_empty_data('fake_output2',
                                         {'type': None, 'kind': 'op', 'op': 'FakeOutput', 'name': 'my_output_name2'}),

            **const_with_data('const1', int64_array(0)),
            **const_with_data('const2', int64_array(0)),
            **regular_op_with_empty_data('add1', {'type': None, 'kind': 'op', 'op': 'Add', 'name': 'my_output_name1'}),
            **regular_op_with_empty_data('add2', {'type': None, 'kind': 'op', 'op': 'Add', 'name': 'my_output_name2'}),
            **result('result1'),
            **result('result2'),
        }
        edges = [*connect('input', 'some_op'),
                 *connect('some_op', 'fake_output1'),
                 ('some_op', 'some_op_d2'),
                 ('some_op_d2', 'fake_output2'),
                 *connect('fake_output1', 'result1'),
                 *connect('fake_output2', 'result2'),
                 ]
        graph = build_graph(nodes, edges)

        edges_ref = [*connect('input', 'some_op'),
                     *connect('some_op', '0:add1'),
                     *connect('const1', '1:add1'),
                     ('some_op', 'some_op_d2'),
                     ('some_op_d2', 'add2', {'in': 0}),
                     *connect('const2', '1:add2'),
                     *connect('add1', 'result1'),
                     *connect('add2', 'result2'),
                     ]

        graph_ref = build_graph(nodes, edges_ref)

        FakeOutputResolver().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph, graph_ref, 'result1')
        self.assertTrue(flag, resp)
Пример #12
0
 limitations under the License.
"""

import unittest

import numpy as np
from generator import generator, generate

from extensions.front.rank_decomposer import RankDecomposer
from mo.front.common.partial_infer.utils import int64_array
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import build_graph, regular_op_with_empty_data, result, connect, \
    valued_const_with_data

nodes = lambda output_type: {
    **regular_op_with_empty_data('input', {'type': 'Parameter'}),
    **regular_op_with_empty_data('rank', {
        'op': 'Rank',
        'type': None,
        'output_type': output_type,
        'name': 'my_rank'
    }),
    **result(),
    **regular_op_with_empty_data('shape', {
        'type': 'ShapeOf',
        'output_type': output_type
    }),
    **regular_op_with_empty_data('rank_1D', {
        'type': 'ShapeOf',
        'output_type': output_type
    }),
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import unittest

import numpy as np

from extensions.front.scatter_normalizer import ScatterNormalizer
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import build_graph, result, connect, \
    regular_op_with_empty_data

nodes = {
    **regular_op_with_empty_data('placeholder_1', {'type': 'Parameter'}),
    **regular_op_with_empty_data('placeholder_2', {'type': 'Parameter'}),
    **regular_op_with_empty_data('placeholder_3', {'type': 'Parameter'}),
    **regular_op_with_empty_data('node', {
        'op': 'ScatterElementsUpdate',
        'is_scatter': True
    }),
    **regular_op_with_empty_data('axis', {
        'type': 'Const',
        'value': None
    }),
    **result(),
}

edges = [
    *connect('placeholder_1', '0:node'),
Пример #14
0
# SPDX-License-Identifier: Apache-2.0

import numpy as np
import unittest
from generator import generator, generate

from extensions.ops.Cast import Cast
from mo.middle.passes.convert_data_type import packed_U4, packed_I4
from mo.middle.passes.infer import partial_infer
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import valued_const_with_data, regular_op_with_empty_data, \
    result, build_graph, connect

nodes = lambda value, dst_type: {
    **valued_const_with_data('value', np.array(value)),
    **regular_op_with_empty_data('convert', {'dst_type': dst_type, 'infer': Cast.infer}),
    **result(),
}


@generator
class CastTest(unittest.TestCase):
    """
    Example of checking:
        7 == 0111,           padded to 0111 0000, results in 112
        7 == 0111, 8 == 1000 packed to 0111 1000, results in 120

        -8 == 1000,          padded to 1000 0000, results in 128
    """

    @generate(*[
Пример #15
0
from extensions.front.tf.identityN_to_identity import IdentityN_to_Identity
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import result, regular_op_with_shaped_data, \
    regular_op_with_empty_data, build_graph, connect, empty_data

nodes = {
    **regular_op_with_shaped_data('placeholder_0', [1, 227, 227, 3], {
                                      'type': 'Parameter'
                                  }),
    **regular_op_with_shaped_data('placeholder_1', [1, 227, 227, 3], {
                                      'type': 'Parameter'
                                  }),
    **regular_op_with_empty_data(
        'identityN', {
            'op': 'IdentityN',
            'type': None,
            'data_types': [np.int32, np.float],
            'name': 'my_identity'
        }),
    **empty_data('identityN_1_d'),
    **regular_op_with_empty_data(
        'identity0', {
            'op': 'Identity',
            'type': None,
            'data_type': np.int32,
            'name': 'my_identity/0_port'
        }),
    **regular_op_with_empty_data(
        'identity1', {
            'op': 'Identity',
            'type': None,
Пример #16
0
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import unittest

import numpy as np

from extensions.front.tf.TFSliceToSlice import TFSliceToSliceReplacer
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import build_graph, regular_op_with_empty_data, result, const, connect_front

nodes = {
    **regular_op_with_empty_data('input', {'type': 'Parameter'}),
    **regular_op_with_empty_data('tfslice', {
        'op': 'TFSlice',
        'type': None
    }),
    **const('begin', np.array(0)),
    **const('size', np.array([-1])),
    **regular_op_with_empty_data('john_doe', {
        'op': 'Sum',
        'type': None
    }),
    **result(),

    # nodes after replacement
    **const('minus_one', np.array(-1)),
    **regular_op_with_empty_data('shapeof', {
Пример #17
0
    def get_graphs(input_shape, reshape_0_pattern, order, reshape_1_pattern,
                   block_size):
        nodes = {
            **regular_op_with_shaped_data(
                'input', input_shape, {
                    'type': 'Parameter',
                    'shape': int64_array(input_shape),
                    'infer': Parameter.infer
                }),
            **valued_const_with_data('reshape_0_pattern',
                                     int64_array(reshape_0_pattern)),
            **regular_op_with_empty_data('reshape_0', {
                'type': 'Reshape',
                'infer': Reshape.infer
            }),
            **valued_const_with_data('order', int64_array(order)),
            **regular_op_with_empty_data('transpose', {
                'type': 'Transpose',
                'infer': Transpose.infer
            }),
            **valued_const_with_data('reshape_1_pattern',
                                     int64_array(reshape_1_pattern)),
            **regular_op_with_empty_data('reshape_1', {
                'type': 'Reshape',
                'infer': Reshape.infer,
                'name': 'final_reshape'
            }),
            **result(),
        }
        edges = [
            *connect('input', '0:reshape_0'),
            *connect('reshape_0_pattern', '1:reshape_0'),
            *connect('reshape_0', '0:transpose'),
            *connect('order', '1:transpose'),
            *connect('transpose', '0:reshape_1'),
            *connect('reshape_1_pattern', '1:reshape_1'),
            *connect('reshape_1', 'output'),
        ]
        graph = build_graph(nodes,
                            edges,
                            nodes_with_edges_only=True,
                            cli=Namespace())
        for node in graph.get_op_nodes():
            node['op'] = node['type']
        graph.clean_up()

        ref_nodes = {
            **regular_op_with_shaped_data(
                'input', input_shape, {
                    'type': 'Parameter',
                    'shape': int64_array(input_shape),
                    'infer': Parameter.infer
                }),
            **regular_op_with_empty_data(
                'depth_to_space', {
                    'type': 'DepthToSpace',
                    'infer': DepthToSpaceOp.infer,
                    'name': 'final_reshape',
                    'block_size': block_size
                }),
            **result()
        }
        ref_edges = [
            *connect('input', 'depth_to_space'),
            *connect('depth_to_space', 'output')
        ]
        graph_ref = build_graph(ref_nodes,
                                ref_edges,
                                nodes_with_edges_only=True)
        for node in graph_ref.get_op_nodes():
            node['op'] = node['type']
        graph_ref.clean_up()
        graph.graph['layout'] = 'NCHW'
        graph_ref.graph['layout'] = 'NCHW'

        return graph, graph_ref
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import unittest

import numpy as np

from extensions.front.onnx.MvnOnnxToMvn import MvnOnnxToMvn
from mo.front.common.partial_infer.utils import int64_array
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import build_graph, regular_op_with_empty_data, result, const, connect_front

nodes = {
    **regular_op_with_empty_data('input', {'type': 'Parameter'}),
    **regular_op_with_empty_data(
        'mvn_onnx', {
            'op': 'MVNOnnx',
            'axes': int64_array([2, 3]),
            'eps': 1e-9,
            'eps_mode': 'outside_sqrt',
            'normalize_variance': 1
        }),
    **result(),

    # nodes after replacement
    **const('axes', int64_array([2, 3])),
    **regular_op_with_empty_data('mvn', {
        'op': 'MVN',
        'type': None
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import unittest

import numpy as np

from extensions.front.caffe.MVNCaffeToMVN import MVNCaffeToMVN
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import build_graph, regular_op_with_empty_data, result, const, connect_front

nodes = {
    **regular_op_with_empty_data('input', {'type': 'Parameter'}),
    **regular_op_with_empty_data('mvn_caffe', {'op': 'MVNCaffe'}),
    **result(),

    # nodes after replacement
    **const('start_1', np.array(1)),
    **const('start_2', np.array(2)),
    **const('step', np.array(1)),
    **regular_op_with_empty_data('rank', {
        'op': 'Rank',
        'type': None
    }),
    **regular_op_with_empty_data('range', {
        'op': 'Range',
        'type': None
    }),
Пример #20
0
    def get_graphs(input_shape, reshape_0_pattern, order, reshape_1_pattern,
                   group):
        nodes = {
            **regular_op_with_shaped_data(
                'input', input_shape, {
                    'type': 'Parameter',
                    'shape': int64_array(input_shape),
                    'infer': Parameter.infer
                }),
            **valued_const_with_data('reshape_0_pattern',
                                     int64_array(reshape_0_pattern)),
            **regular_op_with_empty_data('reshape_0', {
                'type': 'Reshape',
                'infer': Reshape.infer
            }),
            **valued_const_with_data('order', int64_array(order)),
            **regular_op_with_empty_data('transpose', {
                'type': 'Transpose',
                'infer': Transpose.infer
            }),
            **valued_const_with_data('reshape_1_pattern',
                                     int64_array(reshape_1_pattern)),
            **regular_op_with_empty_data('reshape_1', {
                'type': 'Reshape',
                'infer': Reshape.infer,
                'name': 'final_reshape'
            }),
            **result(),
        }
        edges = [
            *connect('input', '0:reshape_0'),
            *connect('reshape_0_pattern', '1:reshape_0'),
            *connect('reshape_0', '0:transpose'),
            *connect('order', '1:transpose'),
            *connect('transpose', '0:reshape_1'),
            *connect('reshape_1_pattern', '1:reshape_1'),
            *connect('reshape_1', 'output'),
        ]
        graph = build_graph(nodes, edges, nodes_with_edges_only=True)
        for node in graph.get_op_nodes():
            node['op'] = node['type']
        graph.clean_up()

        ref_nodes = {
            **regular_op_with_shaped_data(
                'input', input_shape, {
                    'type': 'Parameter',
                    'shape': int64_array(input_shape),
                    'infer': Parameter.infer
                }),
            **regular_op_with_empty_data(
                'shuffle_channel', {
                    'type': 'ShuffleChannels',
                    'infer': ShuffleChannels.infer,
                    'name': 'final_reshape',
                    'group': group
                }),
            **result()
        }
        ref_edges = [
            *connect('input', 'shuffle_channel'),
            *connect('shuffle_channel', 'output')
        ]
        graph_ref = build_graph(ref_nodes,
                                ref_edges,
                                nodes_with_edges_only=True)
        for node in graph_ref.get_op_nodes():
            node['op'] = node['type']
        graph_ref.clean_up()

        return graph, graph_ref
Пример #21
0
import numpy as np

from extensions.middle.SliceConverter import ConvertSlice
from mo.front.common.partial_infer.utils import int64_array
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import build_graph, regular_op_with_shaped_data, valued_const_with_data, \
    regular_op_with_empty_data, result, connect, connect_data

nodes_attributes = {
    **regular_op_with_shaped_data('input', [2, 3, 300, 300], {
                                      'type': 'Parameter',
                                      'op': 'Parameter'
                                  }),
    **regular_op_with_empty_data('starts', {
        'op': 'Const',
        'type': 'Const'
    }),
    **regular_op_with_empty_data('ends', {
        'op': 'Const',
        'type': 'Const'
    }),
    **regular_op_with_empty_data('axes', {
        'op': 'Const',
        'type': 'Const'
    }),
    **regular_op_with_empty_data('steps', {
        'op': 'Const',
        'type': 'Const'
    }),
    **regular_op_with_empty_data('slice', {
        'op': 'Slice',
Пример #22
0
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import unittest

from extensions.front.tf.floor_div_decomposition import FloorDivDecomposition
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import build_graph, result, connect, \
    connect_data, regular_op_with_empty_data

nodes = {
    **regular_op_with_empty_data('placeholder_1', {
        'type': 'Parameter',
        'op': 'Parameter'
    }),
    **regular_op_with_empty_data('placeholder_2', {
        'type': 'Parameter',
        'op': 'Parameter'
    }),
    **regular_op_with_empty_data('floor_div', {
        'op': 'FloorDiv',
        'type': None,
        'name': 'my_floor_div'
    }),
    **regular_op_with_empty_data('div', {
        'type': 'Divide',
        'op': 'Div'
    }),
    **regular_op_with_empty_data('floor', {
Пример #23
0
 def test_convert_slice_to_strided_slice_without_axes_and_steps(self):
     graph = build_graph(nodes_attrs={
         **regular_op_with_shaped_data('input', int64_array([2, 5, 10]), {
                                           'type': 'Parameter'
                                       }),
         **valued_const_with_data('start', np.array([0, 0, 0])),
         **valued_const_with_data('end', np.array([1, 3, 5])),
         **regular_op_with_empty_data('slice', {
             'type': None,
             'op': 'Slice'
         }),
         **result('result')
     },
                         edges=[
                             *connect('input', 'slice'),
                             *connect('start', '1:slice'),
                             *connect('end', '2:slice'),
                             *connect('slice', 'result')
                         ])
     ref_graph = build_graph(nodes_attrs={
         **regular_op_with_shaped_data('input', int64_array([2, 5, 10]), {
                                           'type': 'Parameter'
                                       }),
         **valued_const_with_data('start', np.array([0, 0, 0])),
         **valued_const_with_data('begin_first_part', int64_array([])),
         **valued_const_with_data('begin_last_part', int64_array([])),
         **regular_op_with_empty_data('convert_start', {
             'op': 'Cast',
             'type': 'Convert',
             'dst_type': np.int64
         }),
         **regular_op_with_empty_data('ss_begin', {
             'type': 'Concat',
             'op': 'Concat',
             'axis': 0
         }),
         **valued_const_with_data('end', np.array([1, 3, 5])),
         **valued_const_with_data('end_first_part', int64_array([])),
         **valued_const_with_data('end_last_part', int64_array([])),
         **regular_op_with_empty_data('convert_end', {
             'op': 'Cast',
             'type': 'Convert',
             'dst_type': np.int64
         }),
         **regular_op_with_empty_data('ss_end', {
             'type': 'Concat',
             'op': 'Concat',
             'axis': 0
         }),
         **const('ss_steps', int64_array([1, 1, 1])),
         **empty_data('ss_steps_d'),
         **regular_op_with_empty_data(
             'ss', {
                 'op': 'StridedSlice',
                 'type': 'StridedSlice',
                 'begin_mask': int64_array([1, 1, 1]),
                 'end_mask': int64_array([1, 1, 1]),
                 'new_axis_mask': np.zeros(3, dtype=np.int64),
                 'shrink_axis_mask': np.zeros(3, dtype=np.int64),
                 'ellipsis_mask': np.zeros(3, dtype=np.int64)
             }),
         **result('result')
     },
                             edges=[
                                 *connect('input', 'ss'),
                                 *connect('begin_first_part', 'ss_begin'),
                                 *connect('start', 'convert_start'),
                                 *connect('convert_start', '1:ss_begin'),
                                 *connect('begin_last_part', '2:ss_begin'),
                                 *connect('ss_begin', '1:ss'),
                                 *connect('end_first_part', 'ss_end'),
                                 *connect('end', 'convert_end'),
                                 *connect('convert_end', '1:ss_end'),
                                 *connect('end_last_part', '2:ss_end'),
                                 *connect('ss_end', '2:ss'),
                                 *connect('ss_steps', '3:ss'),
                                 *connect('ss', 'result')
                             ])
     ConvertSlice().find_and_replace_pattern(graph)
     (flag, resp) = compare_graphs(graph,
                                   ref_graph,
                                   'result',
                                   check_op_attrs=True)
     self.assertTrue(flag, resp)
Пример #24
0
 limitations under the License.
"""

import unittest

import numpy as np
from generator import generator, generate

from extensions.front.tf.SizeReplacer import SizeFrontReplacer
from mo.front.common.partial_infer.utils import int64_array
from mo.utils.ir_engine.compare_graphs import compare_graphs
from mo.utils.unittest.graph import build_graph, regular_op_with_empty_data, result, connect, \
    valued_const_with_data

nodes = lambda output_type: {
    **regular_op_with_empty_data('input', {'type': 'Parameter'}),
    **regular_op_with_empty_data('size', {
        'op': 'Size',
        'type': None,
        'output_type': output_type,
        'name': 'my_size'
    }),
    **result(),
    **regular_op_with_empty_data('shape', {
        'type': 'ShapeOf',
        'output_type': output_type
    }),
    **valued_const_with_data('zero', int64_array([0])),
    **regular_op_with_empty_data('reduce', {
        'type': 'ReduceProd',
        'keep_dims': False
Пример #25
0
 def test_convert_slice_to_strided_slice(self, input_shape, start, end,
                                         axes, steps, ss_begin_parts: tuple,
                                         ss_end_parts: tuple, ss_steps,
                                         ss_begin_mask, ss_end_mask):
     graph = build_graph(
         nodes_attrs={
             **regular_op_with_shaped_data('input', input_shape, {
                 'type': 'Parameter'
             }),
             **valued_const_with_data('start', start),
             **valued_const_with_data('end', end),
             **valued_const_with_data('axes', axes),
             **valued_const_with_data('steps', steps),
             **regular_op_with_empty_data('slice', {
                 'type': None,
                 'op': 'Slice'
             }),
             **result('result')
         },
         edges=[
             *connect('input', 'slice'), *connect('start', '1:slice'),
             *connect('end', '2:slice'), *connect('axes', '3:slice'),
             *connect('steps', '4:slice'), *connect('slice', 'result')
         ])
     ref_graph = build_graph(nodes_attrs={
         **regular_op_with_shaped_data('input', input_shape, {
             'type': 'Parameter'
         }),
         **valued_const_with_data('start', start),
         **valued_const_with_data('begin_first_part', ss_begin_parts[0]),
         **valued_const_with_data('begin_last_part', ss_begin_parts[1]),
         **regular_op_with_empty_data('convert_start', {
             'op': 'Cast',
             'type': 'Convert',
             'dst_type': np.int64
         }),
         **regular_op_with_empty_data('ss_begin', {
             'type': 'Concat',
             'op': 'Concat',
             'axis': 0
         }),
         **valued_const_with_data('end', end),
         **valued_const_with_data('end_first_part', ss_end_parts[0]),
         **valued_const_with_data('end_last_part', ss_end_parts[1]),
         **regular_op_with_empty_data('convert_end', {
             'op': 'Cast',
             'type': 'Convert',
             'dst_type': np.int64
         }),
         **regular_op_with_empty_data('ss_end', {
             'type': 'Concat',
             'op': 'Concat',
             'axis': 0
         }),
         **const('ss_steps', ss_steps),
         **empty_data('ss_steps_d'),
         **regular_op_with_empty_data(
             'ss', {
                 'op': 'StridedSlice',
                 'type': 'StridedSlice',
                 'begin_mask': ss_begin_mask,
                 'end_mask': ss_end_mask,
                 'new_axis_mask': np.zeros(len(input_shape), dtype=np.int64),
                 'shrink_axis_mask': np.zeros(len(input_shape),
                                              dtype=np.int64),
                 'ellipsis_mask': np.zeros(len(input_shape), dtype=np.int64)
             }),
         **result('result')
     },
                             edges=[
                                 *connect('input', 'ss'),
                                 *connect('begin_first_part', 'ss_begin'),
                                 *connect('start', 'convert_start'),
                                 *connect('convert_start', '1:ss_begin'),
                                 *connect('begin_last_part', '2:ss_begin'),
                                 *connect('ss_begin', '1:ss'),
                                 *connect('end_first_part', 'ss_end'),
                                 *connect('end', 'convert_end'),
                                 *connect('convert_end', '1:ss_end'),
                                 *connect('end_last_part', '2:ss_end'),
                                 *connect('ss_end', '2:ss'),
                                 *connect('ss_steps', '3:ss'),
                                 *connect('ss', 'result')
                             ])
     ConvertSlice().find_and_replace_pattern(graph)
     (flag, resp) = compare_graphs(graph,
                                   ref_graph,
                                   'result',
                                   check_op_attrs=True)
     self.assertTrue(flag, resp)