예제 #1
0
    def test_element_type(self, mock_argparse):
        main(argparse.ArgumentParser(), fem, 'openvino_mock_mo_frontend')
        stat = get_model_statistic()

        # verify that 'set_element_type' was called
        assert stat.set_element_type == 1
        assert stat.lastArgElementType == get_element_type(np.int8)
예제 #2
0
def test_eye_batch_shape(num_rows, num_columns, diagonal_index, batch_shape, out_type):
    num_rows_array = np.array([num_rows], np.int32)
    num_columns_array = np.array([num_columns], np.int32)
    diagonal_index_array = np.array([diagonal_index], np.int32)
    batch_shape_array = np.array(batch_shape, np.int32)
    num_rows_tensor = ov.constant(num_rows_array)
    num_columns_tensor = ov.constant(num_columns_array)
    diagonal_index_tensor = ov.constant(diagonal_index_array)
    batch_shape_tensor = ov.constant(batch_shape_array)

    # Create with param names
    eye_node = ov.eye(num_rows=num_rows_tensor,
                      num_columns=num_columns_tensor,
                      diagonal_index=diagonal_index_tensor,
                      batch_shape=batch_shape_tensor,
                      output_type=get_element_type_str(out_type))

    # Create with default orded
    eye_node = ov.eye(num_rows_tensor,
                      num_columns_tensor,
                      diagonal_index_tensor,
                      get_element_type_str(out_type),
                      batch_shape_tensor)

    output_shape = [*batch_shape, 1, 1]
    one_matrix = np.eye(num_rows, M=num_columns, k=diagonal_index, dtype=np.float32)
    expected_results = np.tile(one_matrix, output_shape)

    assert eye_node.get_type_name() == "Eye"
    assert eye_node.get_output_size() == 1
    assert eye_node.get_output_element_type(0) == get_element_type(out_type)
    assert tuple(eye_node.get_output_shape(0)) == expected_results.shape
예제 #3
0
def test_all_predicates():
    static_param = opset8.parameter(PartialShape([1, 3, 22, 22]), np.float32)
    dynamic_param = opset8.parameter(PartialShape([-1, 6]), np.long)
    fully_dynamic_param = opset8.parameter(PartialShape.dynamic())

    assert Matcher(WrapType("opset8.Parameter", consumers_count(0)), "Test").match(static_param)
    assert not Matcher(WrapType("opset8.Parameter", consumers_count(1)), "Test").match(static_param)

    assert Matcher(WrapType("opset8.Parameter", has_static_dim(1)), "Test").match(static_param)
    assert not Matcher(WrapType("opset8.Parameter", has_static_dim(0)), "Test").match(dynamic_param)

    assert Matcher(WrapType("opset8.Parameter", has_static_dims([0, 3])), "Test").match(static_param)
    assert not Matcher(WrapType("opset8.Parameter", has_static_dims([0, 1])), "Test").match(dynamic_param)

    assert Matcher(WrapType("opset8.Parameter", has_static_shape()), "Test").match(static_param)
    assert not Matcher(WrapType("opset8.Parameter", has_static_shape()), "Test").match(dynamic_param)

    assert Matcher(WrapType("opset8.Parameter", has_static_rank()), "Test").match(dynamic_param)
    assert not Matcher(WrapType("opset8.Parameter", has_static_rank()), "Test").match(fully_dynamic_param)

    assert Matcher(WrapType("opset8.Parameter",
                            type_matches(get_element_type(np.float32))), "Test").match(static_param)
    assert not Matcher(WrapType("opset8.Parameter",
                                type_matches(get_element_type(np.float32))), "Test").match(dynamic_param)

    assert Matcher(WrapType("opset8.Parameter",
                            type_matches_any([get_element_type(np.float32),
                                              get_element_type(np.long)])), "Test").match(static_param)
    assert Matcher(WrapType("opset8.Parameter",
                            type_matches_any([get_element_type(np.float32),
                                              get_element_type(np.long)])), "Test").match(dynamic_param)
예제 #4
0
def einsum_op_exec(input_shapes: list,
                   equation: str,
                   data_type: np.dtype,
                   with_value=False,
                   seed=202104):
    """Test Einsum operation for given input shapes, equation, and data type.

    It generates input data of given shapes and type, receives reference results using numpy,
    and tests IE implementation by matching with reference numpy results.
    :param input_shapes: a list of tuples with shapes
    :param equation: Einsum equation
    :param data_type: a type of input data
    :param with_value: if True - tests output data shape and type along with its value,
                       otherwise, tests only the output shape and type
    :param seed: a seed for random generation of input data
    """
    np.random.seed(seed)
    num_inputs = len(input_shapes)
    runtime = get_runtime()

    # set absolute tolerance based on the data type
    atol = 0.0 if np.issubdtype(data_type, np.integer) else 1e-04

    # generate input tensors
    ng_inputs = []
    np_inputs = []
    for i in range(num_inputs):
        input_i = np.random.random_integers(
            10, size=input_shapes[i]).astype(data_type)
        np_inputs.append(input_i)
        ng_inputs.append(ov.parameter(input_i.shape, dtype=data_type))

    expected_result = np.einsum(equation, *np_inputs)
    einsum_model = ov.einsum(ng_inputs, equation)

    # check the output shape and type
    assert einsum_model.get_type_name() == "Einsum"
    assert einsum_model.get_output_size() == 1
    assert list(einsum_model.get_output_shape(0)) == list(
        expected_result.shape)
    assert einsum_model.get_output_element_type(0) == get_element_type(
        data_type)

    # check inference result
    if with_value:
        computation = runtime.computation(einsum_model, *ng_inputs)
        actual_result = computation(*np_inputs)
        np.allclose(actual_result, expected_result, atol=atol)
예제 #5
0
def moc_pipeline(argv: argparse.Namespace, moc_front_end: FrontEnd):
    """
    Load input model and convert it to nGraph function
    :param: argv: parsed command line arguments
    :param: moc_front_end: Loaded Frontend for converting input model
    :return: converted nGraph function ready for serialization
    """
    input_model = moc_front_end.load(argv.input_model)

    user_shapes, outputs, freeze_placeholder = fe_user_data_repack(
        input_model, argv.placeholder_shapes, argv.placeholder_data_types,
        argv.output, argv.freeze_placeholder_with_value,
        moc_front_end.get_name())

    def check_places_are_same(places_original: List[Place],
                              places_new: List[Place]):
        """
        Check if set of new places is same as original or not.
        :param places_original: List[Place] Original model places
        :param places_new: List[Place] New list of places
        :return: True if new list of places is same as original
        """
        return len(places_original) == len(places_new) and len([
            item for item in places_original
            if any([item.is_equal(item2['node']) for item2 in places_new])
        ]) == len(places_original)

    def add_names_to_tensors(model: InputModel, places: List[Place]):
        """
        Adds additional names to some model input tensors. This helper should be used
        when a model modification is going to happen.
        :param model The input model loaded by a given frontend
        :param places An object containing Places and names that will be used for model modification
        """
        for new_input in places:
            if not hasattr(new_input, 'input_name'):
                continue

            try:
                model.add_name_for_tensor(new_input['node'],
                                          new_input['input_name'])
            except NotImplementedFailure as e:
                # some frontends might not implement this method
                log.warn(
                    'Could not add an additional name to a tensor pointed to by \'{}\'. Details: {}'
                    .format(new_input['input_name'], str(e)))

    enabled_transforms, disabled_transforms = get_enabled_and_disabled_transforms(
    )
    if 'ANALYSIS_JSON_PRINT' in enabled_transforms:
        # NOTE that model analysis is performed before applying user's settings (inputs's shapes etc.)
        framework_model = moc_front_end.decode(input_model)
        json_model_analysis_dump(framework_model)
        # a model is not processed further in json analysis mode
        sys.exit(0)

    inputs_equal = True
    if user_shapes:
        inputs_equal = check_places_are_same(input_model.get_inputs(),
                                             user_shapes)

    outputs_equal = True
    if outputs:
        outputs_equal = check_places_are_same(input_model.get_outputs(),
                                              outputs)
    log.debug('Inputs are same: {}, outputs are same: {}'.format(
        inputs_equal, outputs_equal))

    if not inputs_equal and not outputs_equal:
        log.debug('Using extract subgraph')
        new_input_places = [x['node'] for x in user_shapes]
        new_output_places = [x['node'] for x in outputs]
        add_names_to_tensors(input_model, user_shapes)
        input_model.extract_subgraph(new_input_places, new_output_places)
        # invalidation of existing Place objects could have happened in the operation above
        if user_shapes:
            user_shapes, outputs, freeze_placeholder = fe_user_data_repack(
                input_model, argv.placeholder_shapes,
                argv.placeholder_data_types, argv.output,
                argv.freeze_placeholder_with_value, moc_front_end.get_name())
    elif not inputs_equal:
        log.debug('Using override_all_inputs')
        add_names_to_tensors(input_model, user_shapes)
        new_input_places = [x['node'] for x in user_shapes]
        input_model.override_all_inputs(new_input_places)
        # invalidation of existing Place objects could have happened in the operation above
        names = [place.get_names()[0] for place in new_input_places]
        shapes = [shape for shape in argv.placeholder_shapes.values()]
        # we have to update names used to find nodes, since the original
        # ones where cut off the graph
        placeholder_shapes = dict(zip(names, shapes))
        if user_shapes:
            user_shapes, outputs, freeze_placeholder = fe_user_data_repack(
                input_model, placeholder_shapes, argv.placeholder_data_types,
                argv.output, argv.freeze_placeholder_with_value,
                moc_front_end.get_name())
    elif not outputs_equal:
        log.debug('Using override_all_outputs')
        add_names_to_tensors(input_model, user_shapes)
        new_output_places = [x['node'] for x in outputs]
        input_model.override_all_outputs(new_output_places)

    if user_shapes:
        for user_shape in user_shapes:
            if user_shape.get('shape') is not None:
                input_model.set_partial_shape(
                    user_shape['node'],
                    partial_shape_from_tuple(user_shape['shape']))
            if user_shape.get('data_type') is not None:
                data_type = get_element_type(user_shape['data_type'])
                log.debug('Set data type: {}'.format(data_type))
                input_model.set_element_type(user_shape['node'], data_type)

    def shape_to_array(shape: PartialShape):
        return [shape.get_dimension(i) for i in range(shape.rank.get_length())]

    # Set batch size
    if argv.batch is not None and argv.batch > 0:
        log.debug('Setting batch size to {}'.format(argv.batch))
        for place in input_model.get_inputs():
            old_partial_shape = input_model.get_partial_shape(place)
            old_shape_array = shape_to_array(
                old_partial_shape) if old_partial_shape.rank.is_static else []
            joined_name = ' '.join(place.get_names())
            validate_batch_in_shape(old_shape_array, joined_name)

            # Assume batch size is always 1-st dimension in shape
            # Keep other dimensions unchanged
            new_shape = [
                old_partial_shape.get_dimension(i)
                for i in range(old_partial_shape.rank.get_length())
            ]
            new_shape[0] = Dimension(argv.batch)

            new_partial_shape = PartialShape(new_shape)
            log.debug('Input: {}, Old shape: {}, New shape: {}'.format(
                joined_name, old_shape_array, new_shape))
            input_model.set_partial_shape(place, new_partial_shape)

    ngraph_function = moc_front_end.convert(input_model)
    return ngraph_function
예제 #6
0
def moc_pipeline(argv: argparse.Namespace, moc_front_end: FrontEnd):
    """
    Load input model and convert it to nGraph function
    :param: argv: parsed command line arguments
    :param: moc_front_end: Loaded Frontend for converting input model
    :return: converted nGraph function ready for serialization
    """
    input_model = moc_front_end.load(argv.input_model)

    user_shapes, outputs, freeze_placeholder = fe_user_data_repack(
        input_model, argv.placeholder_shapes, argv.placeholder_data_types,
        argv.output, argv.freeze_placeholder_with_value)

    def check_places_are_same(places_original: List[Place],
                              places_new: List[Place]):
        """
        Check if set of new places is same as original or not.
        :param places_original: List[Place] Original model places
        :param places_new: List[Place] New list of places
        :return: True if new list of places is same as original
        """
        return len(places_original) == len(places_new) and len([
            item for item in places_original
            if any([item.is_equal(item2['node']) for item2 in places_new])
        ]) == len(places_original)

    inputs_equal = True
    if user_shapes:
        inputs_equal = check_places_are_same(input_model.get_inputs(),
                                             user_shapes)

    outputs_equal = True
    if outputs:
        outputs_equal = check_places_are_same(input_model.get_outputs(),
                                              outputs)
    log.debug('Inputs are same: {}, outputs are same: {}'.format(
        inputs_equal, outputs_equal))

    if not inputs_equal and not outputs_equal:
        # Use ExtractSubgraph
        new_input_places = [x['node'] for x in user_shapes]
        new_output_places = [x['node'] for x in outputs]
        log.debug('Using extract subgraph')
        input_model.extract_subgraph(new_input_places, new_output_places)
    elif not inputs_equal:
        new_input_places = [x['node'] for x in user_shapes]
        log.debug('Using override_all_inputs')
        input_model.override_all_inputs(new_input_places)
    elif not outputs_equal:
        new_output_places = [x['node'] for x in outputs]
        log.debug('Using override_all_outputs')
        input_model.override_all_outputs(new_output_places)

    if user_shapes:
        for user_shape in user_shapes:
            if user_shape.get('shape') is not None:
                input_model.set_partial_shape(
                    user_shape['node'], PartialShape(user_shape['shape']))
            if user_shape.get('data_type') is not None:
                data_type = get_element_type(user_shape['data_type'])
                log.debug('Set data type: {}'.format(data_type))
                input_model.set_element_type(user_shape['node'], data_type)

    def shape_to_array(shape: PartialShape):
        return [shape.get_dimension(i) for i in range(shape.rank.get_length())]

    # Set batch size
    if argv.batch is not None and argv.batch > 0:
        log.debug('Setting batch size to {}'.format(argv.batch))
        for place in input_model.get_inputs():
            old_partial_shape = input_model.get_partial_shape(place)
            old_shape_array = shape_to_array(
                old_partial_shape) if old_partial_shape.rank.is_static else []
            joined_name = ' '.join(place.get_names())
            validate_batch_in_shape(old_shape_array, joined_name)

            # Assume batch size is always 1-st dimension in shape
            # Keep other dimensions unchanged
            new_shape = [
                old_partial_shape.get_dimension(i)
                for i in range(old_partial_shape.rank.get_length())
            ]
            new_shape[0] = Dimension(argv.batch)

            new_partial_shape = PartialShape(new_shape)
            log.debug('Input: {}, Old shape: {}, New shape: {}'.format(
                joined_name, old_shape_array, new_shape))
            input_model.set_partial_shape(place, new_partial_shape)

    ngraph_function = moc_front_end.convert(input_model)
    return ngraph_function