def test_insert_select_0(self):
        graph = build_graph(
            {
                'in_node': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'placeholder_1': {
                    'kind': 'op',
                    'op': None
                },
                'placeholder_data_1': {
                    'kind': 'data',
                    'shape': [1, 13]
                },
                'memory': {
                    'kind': 'op',
                    'op': 'Assign'
                },
            }, [('in_node', 'placeholder_1'),
                ('placeholder_1', 'placeholder_data_1'),
                ('placeholder_data_1', 'memory')],
            nodes_with_edges_only=True)
        ref_graph = graph.copy()
        AddSelectBeforeMemoryNodePattern().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph, ref_graph, 'memory')
        self.assertTrue(flag, resp)
Exemple #2
0
def driver(argv, input_model, output_model_name, output_dir):
    log_step(argv.steps, 'LOAD')
    meta_info = get_meta_info(argv)

    EltwiseChecker.enabled = False

    try:
        graph = load_kaldi_model(input_model)
    except Exception as e:
        raise Error('Model Optimizer is not able to parse Kaldi model {}. '.format(input_model) +
                    refer_to_faq_msg(91)) from e
    graph.check_empty_graph('load_kaldi_nnet_model')
    graph.graph['cmd_params'] = argv
    graph.graph['fw'] = 'kaldi'

    if graph.graph['cmd_params'].generate_experimental_IR_V10:
        version = 10
    else:
        version = 6
    graph.graph['ir_version'] = 2 if argv.generate_deprecated_IR_V2 else version

    update_extractors_with_extensions(kaldi_type_extractors)
    extract_node_attrs(graph, lambda node: kaldi_extractor(node))

    # --------------------------------- LOAD END ------------------------------------------------------
    log_step(argv.steps, 'FRONT')
    ReplaceLSTMNodePattern().find_and_replace_pattern(graph)
    class_registration.apply_replacements(graph, class_registration.ClassType.FRONT_REPLACER)
    log_step(argv.steps, 'MIDDLE')
    graph = partial_infer(graph)

    ReplacePNormNodePattern().find_and_replace_pattern(graph)
    ReplaceMemoryOffsetNodePattern().find_and_replace_pattern(graph)
    ReplaceMemoryOffsetWithMemoryNodePattern().find_and_replace_pattern(graph)
    RemoveMemoryDuplicationPattern().find_and_replace_pattern(graph)
    MergeNeighborSplicePattern().find_and_replace_pattern(graph)
    RemoveUselessCropsPattern().find_and_replace_pattern(graph)
    RemoveIdentity().find_and_replace_pattern(graph)
    graph_clean_up(graph)

    AddSelectBeforeMemoryNodePattern().find_and_replace_pattern(graph)

    ReplaceSpliceNodePattern().find_and_replace_pattern(graph)
    graph_clean_up(graph)

    # The order is intentional, firstly eliminate repeated, then remove redundant
    FuseRepeatedReshapes().find_and_replace_pattern(graph)
    EliminateRedundantReshape().find_and_replace_pattern(graph)
    graph_clean_up(graph)
    graph.check_empty_graph('partial_infer')
    if argv.counts:
        try:
            counts = read_counts_file(argv.counts)
        except Exception as e:
            raise Error('Model Optimizer is not able to read counts file {}'.format(argv.counts) +
                        refer_to_faq_msg(92)) from e

        apply_biases_to_last_layer(graph, counts)

    if argv.remove_output_softmax:
        RemoveLastSoftMaxPattern().find_and_replace_pattern(graph)
        graph_clean_up(graph)
        log.debug("After removing softmax")
        graph.print_graph_stat()

    log_step(argv.steps, 'BACK')
    LeakyReluToReluWithNegativeSlope().find_and_replace_pattern(graph)
    TransposeToPermute().find_and_replace_pattern(graph)
    DivideToEltwises().find_and_replace_pattern(graph)
    SubtractToEltwises().find_and_replace_pattern(graph)
    SimpleEltwiseToEltwiseOp().find_and_replace_pattern(graph)
    for_graph_and_each_sub_graph_recursively(graph, convert_matmul_to_fully_connected)

    # Intentionally after all transformations
    if argv.remove_memory:
        CutMemory().find_and_replace_pattern(graph)
        graph_clean_up(graph)
    ParameterToInput().find_and_replace_pattern(graph)

    KaldiRemoveMemoryOutputBackReplacementPattern().find_and_replace_pattern(graph)
    ForceStrictPrecision().find_and_replace_pattern(graph)
    remove_const_ops(graph)
    CreateConstNodesReplacement().find_and_replace_pattern(graph)

    remove_output_ops(graph)
    log_step(argv.steps, 'EMIT')
    prepare_emit_ir(graph, argv.data_type, output_dir, output_model_name, meta_info=meta_info)
    return 0
Exemple #3
0
    def test_insert_select_1(self):
        graph = build_graph({'in_node': {'kind': 'data', 'shape': [1, 13]},
                             'placeholder_1': {'kind': 'op', 'op': None},
                             'placeholder_data_1': {'kind': 'data', 'shape': [1, 13]},
                             'splice_1': {'kind': 'op', 'op': 'Splice', 'context': np.array([-2, -1, 0, 1, 2])},
                             'splice_data_1': {'kind': 'data', 'shape': [1, 13]},
                             'placeholder_2': {'kind': 'op', 'op': None},
                             'placeholder_data_2': {'kind': 'data', 'shape': [1, 26]},
                             'memory': {'kind': 'op', 'op': 'Assign', 'index': 0},
                             },
                            [('in_node', 'placeholder_1'), ('placeholder_1', 'placeholder_data_1'),
                             ('placeholder_data_1', 'splice_1'), ('splice_1', 'splice_data_1'),
                             ('splice_data_1', 'placeholder_2'), ('placeholder_2', 'placeholder_data_2'),
                             ('placeholder_data_2', 'memory')
                             ],
                            nodes_with_edges_only=True)
        AddSelectBeforeMemoryNodePattern().find_and_replace_pattern(graph)
        ref_graph = build_graph({'in_node': {'kind': 'data', 'shape': [1, 13]},
                                 'placeholder_1': {'kind': 'op', 'op': None},
                                 'placeholder_data_1': {'kind': 'data', 'shape': [1, 13]},
                                 'splice_1': {'kind': 'op', 'op': 'Splice', 'context': np.array([-2, -1, 0, 1, 2])},
                                 'splice_data_1': {'kind': 'data', 'shape': [1, 13]},
                                 'placeholder_2': {'kind': 'op', 'op': None},

                                 'shape': {'kind': 'op', 'op': 'ShapeOf'},
                                 'shape_data': {'kind': 'data'},
                                 'crop_batch': {'kind': 'op', 'op': 'Crop', 'offset': int64_array([0])},
                                 'crop_batch_data': {'kind': 'data'},
                                 'crop_batch_dim':{'kind': 'op', 'op': 'Const', 'value': int64_array([1])},
                                 'crop_batch_dim_data': {'kind': 'data'},
                                 'second_dim': {'kind': 'op', 'op': 'Const', 'value': int64_array([5])},
                                 'second_dim_data': {'kind': 'data'},
                                 'gather_shape': {'kind': 'op', 'op': 'Concat'},
                                 'gather_shape_data': {'kind': 'data'},
                                 'fill_value': {'kind': 'op', 'op': 'Const', 'value': int64_array([0])},
                                 'fill_value_data': {'kind': 'data'},
                                 'broadcast': {'kind': 'op', 'op': 'Broadcast'},
                                 'broadcast_data': {'kind': 'data'},

                                 'memory_in': {'kind': 'op', 'op': 'ReadValue', 'shape': int64_array([5])},
                                 'memory_in_data': {'kind': 'data'},
                                 'memory_out': {'kind': 'op', 'op': 'Assign', 'shape': int64_array([5])},
                                 'memory_out_data': {'kind': 'data'},
                                 'result': {'kind': 'op', 'op': 'Result'},
                                 'crop_in': {'kind': 'op', 'op': 'Crop', 'axis': 1, 'offset': 1, 'dim': 4},
                                 'crop_in_data': {'kind': 'data'},
                                 'crop_out': {'kind': 'op', 'op': 'Crop', 'axis': 1, 'offset': 0, 'dim': 1},
                                 'crop_out_data': {'kind': 'data'},
                                 'equal': {'kind': 'op', 'op': 'Equal'},
                                 'equal_data': {'kind': 'data'},
                                 'select': {'kind': 'op', 'op': 'Select'},
                                 'select_out_data': {'kind': 'data', 'shape': [1, 26]},
                                 'const_0': {'kind': 'op', 'op': 'Const'},
                                 'const_0_data': {'kind': 'data'},
                                 'const_1': {'kind': 'op', 'op': 'Const'},
                                 'const_1_data': {'kind': 'data'},
                                 'concat': {'kind': 'op', 'op': 'Concat'},
                                 'concat_data': {'kind': 'data'},

                                 'placeholder_data_2': {'kind': 'data', 'shape': [1, 26]},
                                 'memory': {'kind': 'op', 'op': 'Assign'},
                                 },
                                [('in_node', 'placeholder_1'), ('placeholder_1', 'placeholder_data_1'),
                                 ('placeholder_data_1', 'splice_1'), ('splice_1', 'splice_data_1'),
                                 ('splice_data_1', 'placeholder_2'), ('placeholder_2', 'placeholder_data_2'),
                                 ('placeholder_data_2', 'select', {'in': 1}),

                                 ('placeholder_data_2', 'shape'), ('shape', 'shape_data'),
                                 ('shape_data', 'crop_batch'), ('crop_batch', 'crop_batch_data'),
                                 ('crop_batch_dim', 'crop_batch_dim_data'),
                                 ('crop_batch_dim_data', 'crop_batch', {'in': 1}),
                                 ('second_dim', 'second_dim_data'), ('second_dim_data', 'gather_shape', {'in': 1}),
                                 ('crop_batch_data', 'gather_shape', {'in': 0}), ('gather_shape', 'gather_shape_data'),
                                 ('fill_value', 'fill_value_data'), ('fill_value_data', 'broadcast', {'in': 0}),
                                 ('gather_shape_data', 'broadcast', {'in': 1}), ('broadcast', 'broadcast_data'),
                                 ('broadcast_data', 'memory_in'),

                                 ('memory_in', 'memory_in_data'), ('memory_in_data', 'crop_in'),
                                 ('crop_in', 'crop_in_data'), ('crop_in_data', 'concat', {'in': 0}),
                                 ('const_1', 'const_1_data'), ('const_1_data', 'concat', {'in': 1}),
                                 ('concat', 'concat_data'), ('concat_data', 'memory_out'),
                                 ('memory_out', 'memory_out_data'), ('memory_out_data', 'result'),
                                 ('concat_data', 'crop_out'), ('crop_out', 'crop_out_data'),
                                 ('crop_out_data', 'equal', {'in': 1}), ('const_1_data', 'equal', {'in': 0}),
                                 ('equal', 'equal_data'),
                                 ('equal_data', 'select', {'in': 0}),

                                 ('const_0', 'const_0_data'), ('const_0_data', 'select', {'in': 2}),
                                 ('select', 'select_out_data'),
                                 ('select_out_data', 'memory')
                                 ],
                                nodes_with_edges_only=True
                                )

        (flag, resp) = compare_graphs(graph, ref_graph, 'memory')
        self.assertTrue(flag, resp)
Exemple #4
0
    def test_insert_select_3(self):
        graph = build_graph({'in_node': {'kind': 'data', 'shape': [1, 13]},
                             'placeholder_1': {'kind': 'op', 'op': None},
                             'placeholder_data_1': {'kind': 'data', 'shape': [1, 13]},
                             'splice_1': {'kind': 'op', 'op': 'Splice', 'context': np.array([-2, -1, 0, 1, 2])},
                             'splice_data_1': {'kind': 'data', 'shape': [1, 65]},
                             'splice_2': {'kind': 'op', 'op': 'Splice', 'context': np.array([-1, 0, 1])},
                             'splice_data_2': {'kind': 'data', 'shape': [1, 39]},
                             'placeholder_2': {'kind': 'op', 'op': None},
                             'placeholder_data_2': {'kind': 'data', 'shape': [1, 26]},
                             'memory': {'kind': 'op', 'op': 'Memory', 'index': 0},
                             },
                            [('in_node', 'placeholder_1'), ('placeholder_1', 'placeholder_data_1'),
                             ('placeholder_data_1', 'splice_1'), ('splice_1', 'splice_data_1'),
                             ('splice_data_1', 'splice_2'), ('splice_2', 'splice_data_2'),
                             ('splice_data_2', 'placeholder_2'), ('placeholder_2', 'placeholder_data_2'),
                             ('placeholder_data_2', 'memory')
                             ],
                            nodes_with_edges_only=True)
        AddSelectBeforeMemoryNodePattern().find_and_replace_pattern(graph)
        ref_graph = build_graph({'in_node': {'kind': 'data', 'shape': [1, 13]},
                                 'placeholder_1': {'kind': 'op', 'op': None},
                                 'placeholder_data_1': {'kind': 'data', 'shape': [1, 13]},
                                 'splice_1': {'kind': 'op', 'op': 'Splice', 'context': np.array([-2, -1, 0, 1, 2])},
                                 'splice_data_1': {'kind': 'data', 'shape': [1, 65]},
                                 'splice_2': {'kind': 'op', 'op': 'Splice', 'context': np.array([-1, 0, 1])},
                                 'splice_data_2': {'kind': 'data', 'shape': [1, 39]},
                                 'placeholder_2': {'kind': 'op', 'op': None},

                                 'memory_in': {'kind': 'op', 'op': 'Memory', 'shape': int64_array([7])},
                                 'memory_in_data': {'kind': 'data'},
                                 'memory_out': {'kind': 'op', 'op': 'Memory', 'shape': int64_array([7])},
                                 'memory_out_data': {'kind': 'data'},
                                 'result': {'kind': 'op', 'op': 'Result'},
                                 'crop_in': {'kind': 'op', 'op': 'Crop', 'axis': 1, 'offset': 1, 'dim': 6},
                                 'crop_in_data': {'kind': 'data'},
                                 'crop_out': {'kind': 'op', 'op': 'Crop', 'axis': 1, 'offset': 0, 'dim': 1},
                                 'crop_out_data': {'kind': 'data'},
                                 'select': {'kind': 'op', 'op': 'Select'},
                                 'select_out_data': {'kind': 'data', 'shape': [1, 26]},
                                 'const_0': {'kind': 'op', 'op': 'Const'},
                                 'const_0_data': {'kind': 'data'},
                                 'const_1': {'kind': 'op', 'op': 'Const'},
                                 'const_1_data': {'kind': 'data'},
                                 'concat': {'kind': 'op', 'op': 'Concat'},
                                 'concat_data': {'kind': 'data'},

                                 'placeholder_data_2': {'kind': 'data', 'shape': [1, 26]},
                                 'memory': {'kind': 'op', 'op': 'Memory', 'index': 0},
                                 },
                                [('in_node', 'placeholder_1'), ('placeholder_1', 'placeholder_data_1'),
                                 ('placeholder_data_1', 'splice_1'), ('splice_1', 'splice_data_1'),
                                 ('splice_data_1', 'splice_2'), ('splice_2', 'splice_data_2'),
                                 ('splice_data_2', 'placeholder_2'), ('placeholder_2', 'placeholder_data_2'),
                                 ('placeholder_data_2', 'select', {'in': 1}),

                                 ('memory_in', 'memory_in_data'), ('memory_in_data', 'crop_in'),
                                 ('crop_in', 'crop_in_data'), ('crop_in_data', 'concat', {'in': 0}),
                                 ('const_1', 'const_1_data'), ('const_1_data', 'concat', {'in': 1}),
                                 ('concat', 'concat_data'), ('concat_data', 'memory_out'),
                                 ('memory_out', 'memory_out_data'), ('memory_out_data', 'result'),
                                 ('concat_data', 'crop_out'), ('crop_out', 'crop_out_data'),
                                 ('crop_out_data', 'select', {'in': 0}),
                                 ('const_0', 'const_0_data'), ('const_0_data', 'select', {'in': 2}),

                                 ('select', 'select_out_data'),
                                 ('select_out_data', 'memory')
                                 ],
                                nodes_with_edges_only=True
                                )

        (flag, resp) = compare_graphs(graph, ref_graph, 'memory')
        self.assertTrue(flag, resp)