def test_convert_blob_to_int32_with_force_precision_error(self):
        graph = build_graph(
            nodes_attributes, [('data_node', 'op_node', {
                'bin': 1
            })], {
                'data_node': {
                    'value': np.array([4.0, 3.0, 2.0, 1.1], dtype=np.float64)
                }
            })

        with self.assertRaisesRegex(Error, '.*results in rounding.*'):
            convert_blobs(graph, "I32")
    def test_convert_blob_to_int32_with_force_precision(self):
        graph = build_graph(
            nodes_attributes, [('data_node', 'op_node', {
                'bin': 1
            })], {
                'data_node': {
                    'value': np.array([4.0, 3.0, 2.0, 1.0], dtype=np.float64)
                }
            })

        convert_blobs(graph, "I32")
        result_value = graph.node['data_node']['value']
        self.assertTrue(result_value.dtype == np.int32)
        self.assertListEqual(list(result_value), [4, 3, 2, 1])
    def test_convert_blob_to_fp16_from_fp64_overflow(self):
        graph = build_graph(
            nodes_attributes, [('data_node', 'op_node', {
                'bin': 1
            })], {
                'data_node': {
                    'value': np.array([4.0, 3.0, 2.0, 1e10], dtype=np.float64)
                }
            })

        convert_blobs(graph, "FP16")
        result_value = graph.node['data_node']['value']
        self.assertTrue(result_value.dtype == np.float16)
        self.assertListEqual(list(result_value), [4, 3, 2, np.inf])
Example #4
0
def prepare_emit_ir(graph: Graph, data_type: str, output_dir: str, output_model_name: str,
                    mean_data: [list, None] = None, input_names: list = None, meta_info: dict = None):
    if input_names is None:
        input_names = []
    if meta_info is None:
        meta_info = {}
    graph.strict_mode = False

    # convert Parameter data types
    convert_data_type.convert_parameters_data_type(graph, data_type)
    # convert blobs (usually weights and biases)
    for sub_graph in [graph] + collect_sub_graphs(graph):
        convert_data_type.convert_blobs(sub_graph, data_type)

    # restore data type for specific inputs/outputs of specific ops to the data types required by nGraph
    if not graph.graph['cmd_params'].generate_deprecated_IR_V7:
        for_graph_and_each_sub_graph_recursively(graph, convert_inputs_of_specific_ops)

    if graph.graph['cmd_params'].generate_experimental_IR_V10:
        for_graph_and_each_sub_graph_recursively(graph, OpVersioning().find_and_replace_pattern)

    # do not run the type inference in sub-graphs. It will be called automatically as part of the type inference of
    # the TensorIterator nodes
    type_infer(graph)
    RemoveUselessConvert().find_and_replace_pattern(graph)

    for sub_graph in [graph] + collect_sub_graphs(graph):
        op_order, data_order = determined_sort(get_sorted_outputs(sub_graph))
        mapping = {v: u for u, v in enumerate(op_order)}
        mapping.update({v: u for u, v in enumerate(data_order, start=len(sub_graph))})
        relabel_nodes_inplace_safe(sub_graph, mapping)
        port_renumber(sub_graph)

    tensor_names.propagate_op_name_to_tensor(graph)

    bin_file = os.path.join(output_dir, '{}.bin'.format(output_model_name))
    serialize_constants(graph, bin_file)

    mean_offset = None
    mean_size = None
    if mean_data:
        mean_offset, mean_size = serialize_mean_image(bin_file, mean_data=mean_data)

    generate_ie_ir(graph=graph,
                   file_name=os.path.join(output_dir, '{}.xml'.format(output_model_name)),
                   input_names=input_names,
                   mean_offset=mean_offset,
                   mean_size=mean_size,
                   meta_info=meta_info)
    tensor_names.output_tensor_names_map(graph, os.path.join(output_dir, '{}.mapping'.format(output_model_name)))
def build_range_test_graphs(start=0,
                            limit=10,
                            delta=1,
                            dst_type_str='FP16',
                            src_type_str='FP32',
                            returns_shape_value=None):
    nodes = {
        **valued_const_with_data('start', float32_array(start)),
        **valued_const_with_data('limit', float32_array(limit)),
        **valued_const_with_data('delta', float32_array(delta)),
        **regular_op_with_empty_data(
            'range', {
                'type': 'Range',
                'op': 'Range',
                'returns_shape_value': returns_shape_value,
                'output_type': data_type_str_to_np(src_type_str),
                'infer': Range.infer
            }),
        **result('res'),
    }

    nodes_ref = deepcopy(nodes)
    nodes_ref.update({
        **regular_op_with_empty_data(
            'range', {
                'type': 'Range',
                'op': 'Range',
                'returns_shape_value': returns_shape_value,
                'output_type': data_type_str_to_np(dst_type_str),
                'infer': Range.infer
            }),
    })

    edges = [
        *connect('start', '0:range'),
        *connect('limit', '1:range'),
        *connect('delta', '2:range'),
        *connect('range', 'res'),
    ]
    graph = build_graph(nodes, edges)
    graph_ref = build_graph(nodes_ref, edges)

    graph = partial_infer(graph)

    graph.graph['cmd_params'].data_type = dst_type_str
    convert_blobs(graph, dst_type_str)
    return graph, graph_ref
def build_cast_test_graphs(input_data, dst_type_str='FP16'):
    nodes = {
        **valued_const_with_data('input', float32_array(input_data)),
        **regular_op_with_empty_data(
            'cast', {
                'type': 'Convert',
                'op': 'Cast',
                'dst_type': np.float32,
                'infer': Cast.infer
            }),
        **result('res'),
    }

    nodes_ref = deepcopy(nodes)
    nodes_ref.update({
        **regular_op_with_empty_data(
            'cast', {
                'type': 'Convert',
                'op': 'Cast',
                'dst_type': data_type_str_to_np(dst_type_str),
                'infer': Cast.infer
            }),
    })

    edges = [
        *connect('input', 'cast'),
        *connect('cast', 'res'),
    ]
    graph = build_graph(nodes, edges)
    graph_ref = build_graph(nodes_ref, edges)

    graph = partial_infer(graph)

    graph.graph['cmd_params'].data_type = dst_type_str
    convert_blobs(graph, dst_type_str)
    return graph, graph_ref