def moc_emit_ir(ngraph_function: Function, argv: argparse.Namespace): output_dir = argv.output_dir if argv.output_dir != '.' else os.getcwd() network = function_to_cnn(ngraph_function) from mo.back.offline_transformations import apply_user_transformations, apply_moc_transformations apply_user_transformations(network, parse_transform(argv.transform)) apply_moc_transformations(network) orig_model_name = os.path.normpath( os.path.join(output_dir, argv.model_name)) network.serialize(orig_model_name + ".xml", orig_model_name + ".bin") del argv.feManager # add meta information to IR append_ir_info(file=orig_model_name, meta_info=get_meta_info(argv), mean_data=None, input_names=None) print('[ SUCCESS ] Generated IR version {} model.'.format( get_ir_version(argv))) print('[ SUCCESS ] XML file: {}.xml'.format(orig_model_name)) print('[ SUCCESS ] BIN file: {}.bin'.format(orig_model_name)) return 0
def test_infer_dynamic_network_with_set_blob_twice(): from conftest import create_encoder import ngraph as ng shape, p_shape = [1, 4, 20, 20], [(0, 5), 4, 20, 20] ref_shape1, ref_shape2 = [2, 4, 20, 20], [3, 4, 20, 20] function = create_encoder(shape) net = ng.function_to_cnn(function) net.reshape({"data": p_shape}) ie_core = ie.IECore() ie_core.register_plugin("templatePlugin", "TEMPLATE") exec_net = ie_core.load_network(net, "TEMPLATE") request = exec_net.requests[0] td = request.input_blobs['data'].tensor_desc td.dims = ref_shape1 blob = ie.Blob(td) request.set_blob("data", blob) request.infer({"data": np.ones(ref_shape1)}) assert exec_net.requests[0].input_blobs[ "data"].tensor_desc.dims == ref_shape1 assert request.output_blobs['out'].tensor_desc.dims == ref_shape1 td = request.input_blobs['data'].tensor_desc td.dims = ref_shape2 blob = ie.Blob(td) request.set_blob("data", blob) request.infer({"data": np.ones(ref_shape2)}) assert exec_net.requests[0].input_blobs[ "data"].tensor_desc.dims == ref_shape2 assert request.output_blobs['out'].tensor_desc.dims == ref_shape2
def test_incorrect_reshape(): function = create_relu([1, 3, 22, 22]) net = ng.function_to_cnn(function) with pytest.raises(ValueError) as e: net.reshape({"data": [(2, 4, 6), 3, 22, 22]}) assert "Incorrect PartialShape dimension definition '(2, 4, 6)' " \ "in shape '[(2, 4, 6), 3, 22, 22]', expected one or two values for a dimension! " in str(e.value)
def ngraph_embedding(ids, vocab_embeddings, vocab_size, embedding_dim, padding_idx, sparse): """ decomposing embedding with ngraph ops. """ import ngraph as ng from ngraph import opset8 as opset from openvino.inference_engine import IECore if vocab_embeddings is None: # vocab_embeddings = np.zeros( (vocab_size, embedding_dim)).astype("float32") node_ids = ng.parameter(shape=ids.shape, name='ids', dtype=ids.dtype) node_w = ng.parameter(shape=vocab_embeddings.shape, name='w', dtype=vocab_embeddings.dtype) if padding_idx == -1: padding_idx += vocab_size if padding_idx is not None: ''' mask W ''' masked_embeddings = np.ones(vocab_embeddings.shape, dtype='int64') masked_embeddings[padding_idx, :] = 0 # mask node_mask = ng.constant(masked_embeddings, name='mask', dtype=vocab_embeddings.dtype) node_masked_w = ng.multiply(node_w, node_mask) node_axis = ng.constant([0], name='const0', dtype=np.int64) node_gather = opset.gather(data=node_masked_w if padding_idx else node_w, indices=node_ids, axis=node_axis, batch_dims=0) graph = ng.result(node_gather, name='y') parameters = [node_ids, node_w] inputs_dict = {'ids': ids, "w": vocab_embeddings} # function = ng.Function(graph, parameters, "embedding") ie_network = ng.function_to_cnn(function) ie = IECore() executable_network = ie.load_network(ie_network, 'CPU') output = executable_network.infer(inputs_dict) return output
def test_set_blob_with_incorrect_name(): function = create_encoder([4, 4, 20, 20]) net = ng.function_to_cnn(function) ie_core = ie.IECore() ie_core.register_plugin("ov_template_plugin", "TEMPLATE") exec_net = ie_core.load_network(net, "TEMPLATE") tensor_desc = exec_net.requests[0].input_blobs["data"].tensor_desc tensor_desc.dims = [4, 4, 20, 20] blob = ie.Blob(tensor_desc) with pytest.raises(RuntimeError) as e: exec_net.requests[0].set_blob("incorrect_name", blob) assert f"Failed to find input or output with name: 'incorrect_name'" in str(e.value)
def test_is_dynamic(): function = create_relu([-1, 3, 20, 20]) net = ng.function_to_cnn(function) ie = IECore() ie.register_plugin("openvino_template_plugin", "TEMPLATE") exec_net = ie.load_network(net, "TEMPLATE") assert exec_net.outputs["out"].is_dynamic p_shape = ng.partial_shape_from_data(exec_net.outputs["out"]) assert isinstance(p_shape, ng.impl.PartialShape) with pytest.raises(RuntimeError) as e: exec_net.outputs["out"].shape assert "Cannot return dims for Data with dynamic shapes!" in str(e.value)
def test_blob_set_shape_after_async_infer(): function = create_encoder([1, 4, 20, 20]) net = ng.function_to_cnn(function) net.reshape({"data": [(1, 5), 4, 20, 20]}) ie_core = IECore() ie_core.register_plugin("templatePlugin", "TEMPLATE") exec_net = ie_core.load_network(net, "TEMPLATE") request = exec_net.requests[0] request.async_infer({"data": np.ones([4, 4, 20, 20])}) with pytest.raises(RuntimeError) as e: request.input_blobs['data'].set_shape([3, 4, 20, 20]) assert "REQUEST_BUSY" in str(e.value) request.wait()
def test_async_infer_dynamic_network_3_requests(shapes): function = create_encoder([3, 4, 20, 20]) net = ng.function_to_cnn(function) net.reshape({"data": [3, 4, (20, 50), (20, 50)]}) ie_core = ie.IECore() ie_core.register_plugin("ov_template_plugin", "TEMPLATE") exec_net = ie_core.load_network(net, "TEMPLATE", num_requests=3) for i,request in enumerate(exec_net.requests): request.async_infer({"data": np.ones(shapes[i])}) for i,request in enumerate(exec_net.requests): status = request.wait(ie.WaitMode.RESULT_READY) assert status == ie.StatusCode.OK assert request.output_blobs['out'].tensor_desc.dims == shapes[i]
def test_infer_dynamic_network_without_set_shape(shape, p_shape, ref_shape): function = create_encoder(shape) net = ng.function_to_cnn(function) net.reshape({"data": p_shape}) ie_core = ie.IECore() ie_core.register_plugin("ov_template_plugin", "TEMPLATE") exec_net = ie_core.load_network(net, "TEMPLATE") exec_net.infer({"data": np.ones(ref_shape)}) assert exec_net.requests[0].input_blobs["data"].tensor_desc.dims == ref_shape request = exec_net.requests[0] request.async_infer({"data": np.ones(ref_shape)}) status = request.wait(ie.WaitMode.RESULT_READY) assert status == ie.StatusCode.OK assert request.output_blobs['out'].tensor_desc.dims == ref_shape
def test_create_two_exec_net(): function = create_relu([ ng.Dimension(0, 5), ng.Dimension(4), ng.Dimension(20), ng.Dimension(20) ]) net = ng.function_to_cnn(function) ie_core = IECore() ie_core.register_plugin("ov_template_plugin", "TEMPLATE") exec_net1 = ie_core.load_network(net, "TEMPLATE", num_requests=2) assert ng.function_from_cnn(net) != None exec_net2 = ie_core.load_network(net, "TEMPLATE", num_requests=2) assert ng.function_from_cnn(net) != None
def test_infer_dynamic_network_twice(): shape, p_shape = [1, 4, 20, 20], [(0,5), 4, 20, 20] ref_shape1, ref_shape2 = [2, 4, 20, 20], [3, 4, 20, 20] function = create_encoder(shape) net = ng.function_to_cnn(function) net.reshape({"data": p_shape}) ie_core = ie.IECore() ie_core.register_plugin("ov_template_plugin", "TEMPLATE") exec_net = ie_core.load_network(net, "TEMPLATE") request = exec_net.requests[0] request.infer({"data": np.ones(ref_shape1)}) assert exec_net.requests[0].input_blobs["data"].tensor_desc.dims == ref_shape1 assert request.output_blobs['out'].tensor_desc.dims == ref_shape1 request.infer({"data": np.ones(ref_shape2)}) assert exec_net.requests[0].input_blobs["data"].tensor_desc.dims == ref_shape2 assert request.output_blobs['out'].tensor_desc.dims == ref_shape2
def test_set_blob_after_async_infer(): from conftest import create_ngraph_function import ngraph as ng function = create_ngraph_function([ng.Dimension(0,5), ng.Dimension(4), ng.Dimension(20), ng.Dimension(20)]) net = ng.function_to_cnn(function) ie_core = ie.IECore() ie_core.register_plugin("templatePlugin", "TEMPLATE") exec_net = ie_core.load_network(net, "TEMPLATE") request = exec_net.requests[0] tensor_desc = request.input_blobs['data'].tensor_desc tensor_desc.dims = [2, 4, 20, 20] blob = ie.Blob(tensor_desc) request.async_infer({"data": np.ones([4, 4, 20, 20])}) with pytest.raises(RuntimeError) as e: request.set_blob("data", blob) assert "REQUEST_BUSY" in str(e.value)
def test_reshape_with_partial_shape(device, shape, p_shape): function = create_relu(shape) net = ng.function_to_cnn(function) net.reshape({"data": p_shape}) changedFunction = ng.function_from_cnn(net) p_shape = ng.impl.PartialShape(p_shape) assert changedFunction.get_parameters()[0].get_partial_shape().is_dynamic assert changedFunction.get_results()[0].get_output_partial_shape( 0).is_dynamic assert function.get_parameters()[0].get_partial_shape().is_dynamic assert function.get_results()[0].get_output_partial_shape(0).is_dynamic assert changedFunction.get_parameters()[0].get_partial_shape() == p_shape assert changedFunction.get_results()[0].get_output_partial_shape( 0) == p_shape assert function.get_parameters()[0].get_partial_shape() == p_shape assert function.get_results()[0].get_output_partial_shape(0) == p_shape
def test_set_blob_after_async_infer(): function = create_encoder([1, 4, 20, 20]) net = ng.function_to_cnn(function) net.reshape({"data": [(0, 5), 4, 20, 20]}) ie_core = ie.IECore() ie_core.register_plugin("ov_template_plugin", "TEMPLATE") exec_net = ie_core.load_network(net, "TEMPLATE") request = exec_net.requests[0] tensor_desc = request.input_blobs['data'].tensor_desc tensor_desc.dims = [2, 4, 20, 20] blob = ie.Blob(tensor_desc) request.async_infer({"data": np.ones([4, 4, 20, 20])}) with pytest.raises(RuntimeError) as e: request.set_blob("data", blob) assert "REQUEST_BUSY" in str(e.value) request.wait()
def test_set_blob_with_incorrect_size(): function = create_encoder([4, 4, 20, 20]) net = ng.function_to_cnn(function) ie_core = ie.IECore() ie_core.register_plugin("ov_template_plugin", "TEMPLATE") exec_net = ie_core.load_network(net, "TEMPLATE") tensor_desc = exec_net.requests[0].input_blobs["data"].tensor_desc tensor_desc.dims = [tensor_desc.dims[0]*2, 4, 20, 20] blob = ie.Blob(tensor_desc) print(exec_net.requests[0].output_blobs) with pytest.raises(RuntimeError) as e: exec_net.requests[0].set_blob("data", blob) assert f"Input blob size is not equal network input size" in str(e.value) with pytest.raises(RuntimeError) as e: exec_net.requests[0].set_blob("out", blob) assert f"Output blob size is not equal network output size" in str(e.value)
def test_infer_dynamic_network_with_set_blob(shape, p_shape, ref_shape): from conftest import create_ngraph_function import ngraph as ng function = create_ngraph_function(shape) net = ng.function_to_cnn(function) net.reshape({"data": p_shape}) ie_core = ie.IECore() ie_core.register_plugin("templatePlugin", "TEMPLATE") exec_net = ie_core.load_network(net, "TEMPLATE") tensor_desc = exec_net.requests[0].input_blobs["data"].tensor_desc tensor_desc.dims = ref_shape blob = ie.Blob(tensor_desc) exec_net.requests[0].set_blob("data", blob) assert exec_net.requests[0].input_blobs["data"].tensor_desc.dims == ref_shape request = exec_net.requests[0] request.infer({"data": np.ones(ref_shape)}) request.async_infer({"data": np.ones(ref_shape)}) status = request.wait(ie.WaitMode.RESULT_READY) assert status == ie.StatusCode.OK assert request.output_blobs["out"].tensor_desc.dims == ref_shape
def test_is_dynamic(): from conftest import create_ngraph_function import ngraph as ng function = create_ngraph_function([-1, 3, 20, 20]) net = ng.function_to_cnn(function) assert net.input_info["data"].input_data.is_dynamic assert net.outputs["out"].is_dynamic p_shape = ng.partial_shape_from_data(net.input_info["data"].input_data) assert isinstance(p_shape, ng.impl.PartialShape) p_shape = ng.partial_shape_from_data(net.outputs["out"]) assert isinstance(p_shape, ng.impl.PartialShape) with pytest.raises(RuntimeError) as e: net.input_info["data"].input_data.shape assert "Cannot return dims for Data with dynamic shapes!" in str(e.value) ie = IECore() ie.register_plugin("templatePlugin", "TEMPLATE") exec_net = ie.load_network(net, "TEMPLATE") assert exec_net.input_info["data"].input_data.is_dynamic p_shape = ng.partial_shape_from_data( exec_net.input_info["data"].input_data) assert isinstance(p_shape, ng.impl.PartialShape)