Esempio n. 1
0
    def test_pnorm(self):
        graph = build_graph(self.nodes_attributes,
                            [('placeholder', 'in_node'), ('in_node', 'pnorm'),
                             ('pnorm', 'pnorm_data'),
                             ('pnorm_data', 'out_placeholder')])
        ReplacePNormNodePattern().find_and_replace_pattern(graph)

        ref_graph = build_graph(
            {
                'in_placeholder': {
                    'kind': 'op',
                    'op': None
                },
                'in_node': {
                    'kind': 'data',
                    'shape': [1, 3500]
                },
                'pow_const': {
                    'kind': 'op',
                    'value': 2.0
                },
                'pow_const_d': {
                    'kind': 'data'
                },
                'pow': {
                    'kind': 'op',
                    'op': 'Pow'
                },
                'pow_data': {
                    'kind': 'data'
                },
                'reshape': {
                    'kind': 'op',
                    'op': 'Reshape'
                },
                'reshape_data': {
                    'kind': 'data'
                },
                'const': {
                    'kind': 'op',
                    'op': 'Const',
                    'value': [1, 350, 10]
                },
                'const_data': {
                    'kind': 'data'
                },
                'reduce': {
                    'kind': 'op',
                    'op': 'ReduceSum'
                },
                'reduce_data': {
                    'kind': 'data'
                },
                'const_1': {
                    'kind': 'op',
                    'op': 'Const',
                    'value': 2
                },
                'const_data_1': {
                    'kind': 'data'
                },
                'invpow_const': {
                    'kind': 'op',
                    'value': 0.5
                },
                'invpow_const_d': {
                    'kind': 'data'
                },
                'invpow': {
                    'kind': 'op',
                    'op': 'Pow'
                },
                'invpow_data': {
                    'kind': 'data'
                },
                'out_placeholder': {
                    'kind': 'op',
                    'op': 'placeholder'
                },
            }, [
                ('in_placeholder', 'in_node'),
                ('in_node', 'pow', {
                    'in': 0
                }),
                ('pow', 'pow_data'),
                ('pow_data', 'reshape', {
                    'in': 0
                }),
                ('reshape', 'reshape_data'),
                ('const', 'const_data'),
                ('const_data', 'reshape', {
                    'in': 1
                }),
                ('reshape_data', 'reduce', {
                    'in': 0
                }),
                ('const_1', 'const_data_1'),
                ('const_data_1', 'reduce', {
                    'in': 1
                }),
                ('reduce', 'reduce_data'),
                ('reduce_data', 'invpow', {
                    'in': 0
                }),
                ('invpow', 'invpow_data'),
                ('invpow_data', 'out_placeholder'),
                ('pow_const', 'pow_const_d'),
                ('invpow_const', 'invpow_const_d'),
                ('pow_const_d', 'pow', {
                    'in': 1
                }),
                ('invpow_const_d', 'invpow', {
                    'in': 1
                }),
            ])

        (flag, resp) = compare_graphs(graph,
                                      ref_graph,
                                      'out_placeholder',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Esempio n. 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