def simple_if(condition_val): condition = ov.constant(condition_val, dtype=np.bool) # then_body X_t = ov.parameter([2], np.float32, "X") Y_t = ov.parameter([2], np.float32, "Y") then_mul = ov.multiply(X_t, Y_t) then_body_res_1 = ov.result(then_mul) then_body = Model([then_body_res_1], [X_t, Y_t], "then_body_function") # else_body X_e = ov.parameter([2], np.float32, "X") Y_e = ov.parameter([2], np.float32, "Y") add_e = ov.add(X_e, Y_e) else_body_res_1 = ov.result(add_e) else_body = Model([else_body_res_1], [X_e, Y_e], "else_body_function") X = ov.constant([3, 4], dtype=np.float32) Y = ov.constant([2, 1], dtype=np.float32) if_node = ov.if_op(condition) if_node.set_then_body(then_body) if_node.set_else_body(else_body) if_node.set_input(X.output(0), X_t, X_e) if_node.set_input(Y.output(0), Y_t, Y_e) if_res = if_node.set_output(then_body_res_1, else_body_res_1) relu = ov.relu(if_res) return relu
def test_low_latency2(): X = opset8.parameter(Shape([32, 40, 10]), np.float32, "X") Y = opset8.parameter(Shape([32, 40, 10]), np.float32, "Y") M = opset8.parameter(Shape([32, 2, 10]), np.float32, "M") X_i = opset8.parameter(Shape([32, 2, 10]), np.float32, "X_i") Y_i = opset8.parameter(Shape([32, 2, 10]), np.float32, "Y_i") M_body = opset8.parameter(Shape([32, 2, 10]), np.float32, "M_body") sum = opset8.add(X_i, Y_i) Zo = opset8.multiply(sum, M_body) body = Model([Zo], [X_i, Y_i, M_body], "body_function") ti = opset8.tensor_iterator() ti.set_body(body) ti.set_sliced_input(X_i, X.output(0), 0, 2, 2, 39, 1) ti.set_sliced_input(Y_i, Y.output(0), 0, 2, 2, -1, 1) ti.set_invariant_input(M_body, M.output(0)) out0 = ti.get_iter_value(Zo.output(0), -1) out1 = ti.get_concatenated_slices(Zo.output(0), 0, 2, 2, 39, 1) result0 = opset8.result(out0) result1 = opset8.result(out1) model = Model([result0, result1], [X, Y, M]) m = Manager() m.register_pass(LowLatency2()) m.run_passes(model) # TODO: create TI which will be transformed by LowLatency2 assert count_ops(model, "TensorIterator") == [1]
def get_test_function(): element_type = Type.f32 param = Parameter(element_type, Shape([1, 3, 22, 22])) relu = ops.relu(param) func = Model([relu], [param], "test") assert func is not None return func
def test_infer_list_as_inputs(device): num_inputs = 4 input_shape = [2, 1] dtype = np.float32 params = [ops.parameter(input_shape, dtype) for _ in range(num_inputs)] model = Model(ops.relu(ops.concat(params, 1)), params) core = Core() compiled_model = core.compile_model(model, device) def check_fill_inputs(request, inputs): for input_idx in range(len(inputs)): assert np.array_equal( request.get_input_tensor(input_idx).data, inputs[input_idx]) request = compiled_model.create_infer_request() inputs = [np.random.normal(size=input_shape).astype(dtype)] request.infer(inputs) check_fill_inputs(request, inputs) inputs = [ np.random.normal(size=input_shape).astype(dtype) for _ in range(num_inputs) ] request.infer(inputs) check_fill_inputs(request, inputs)
def test_convolution_with_non_zero_padding(): element_type = Type.f32 image_shape = Shape([1, 1, 10, 10]) filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) parameter_list = [data, filters] image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) filter_arr = (np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)) * -1 filter_arr[0][0][1][1] = 1 strides = [1, 1] dilations = [2, 2] pads_begin = [2, 1] pads_end = [1, 2] model = ov.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Model([model], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(image_arr, filter_arr)[0] expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilations, pads_begin, pads_end).reshape([1, 1, 9, 9]) assert np.allclose(result, expected)
def test_convolution_simple(): element_type = Type.f32 image_shape = Shape([1, 1, 16, 16]) filter_shape = Shape([1, 1, 3, 3]) data = Parameter(element_type, image_shape) filters = Parameter(element_type, filter_shape) parameter_list = [data, filters] image_arr = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16) filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3) filter_arr[0][0][0][0] = -1 filter_arr[0][0][1][1] = -1 filter_arr[0][0][2][2] = -1 filter_arr[0][0][0][2] = -1 filter_arr[0][0][2][0] = -1 strides = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] dilations = [1, 1] model = ov.convolution(data, filters, strides, pads_begin, pads_end, dilations) function = Model([model], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(image_arr, filter_arr)[0] expected = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14) assert np.allclose(result, expected)
def test_ports_as_inputs(device): input_shape = [2, 2] param_a = ops.parameter(input_shape, np.float32) param_b = ops.parameter(input_shape, np.float32) model = Model(ops.add(param_a, param_b), [param_a, param_b]) core = Core() compiled = core.compile_model(model, device) request = compiled.create_infer_request() arr_1 = np.array([[1, 2], [3, 4]], dtype=np.float32) arr_2 = np.array([[3, 4], [1, 2]], dtype=np.float32) tensor1 = Tensor(arr_1) tensor2 = Tensor(arr_2) res = request.infer({ compiled.inputs[0]: tensor1, compiled.inputs[1]: tensor2 }) assert np.array_equal(res[compiled.outputs[0]], tensor1.data + tensor2.data) res = request.infer({ request.model_inputs[0]: tensor1, request.model_inputs[1]: tensor2 }) assert np.array_equal(res[request.model_outputs[0]], tensor1.data + tensor2.data)
def test_default_version_IR_V11_seperate_paths(): core = Core() xml_path = "./serialized_function.xml" bin_path = "./serialized_function.bin" shape = [100, 100, 2] parameter_a = ov.parameter(shape, dtype=np.float32, name="A") parameter_b = ov.parameter(shape, dtype=np.float32, name="B") parameter_c = ov.parameter(shape, dtype=np.float32, name="C") parameter_d = ov.parameter(shape, dtype=np.float32, name="D") model = ov.floor( ov.minimum(ov.abs(parameter_a), ov.multiply(parameter_b, parameter_c))) func = Model(model, [parameter_a, parameter_b, parameter_c], "Model") pass_manager = Manager() pass_manager.register_pass("Serialize", xml_path=xml_path, bin_path=bin_path, version="IR_V11") pass_manager.run_passes(func) res_func = core.read_model(model=xml_path, weights=bin_path) assert func.get_parameters() == res_func.get_parameters() assert func.get_ordered_ops() == res_func.get_ordered_ops() os.remove(xml_path) os.remove(bin_path)
def get_test_function(): param = ov.opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") param.get_output_tensor(0).set_names({"parameter"}) relu = ov.opset8.relu(param) res = ov.opset8.result(relu, name="result") res.get_output_tensor(0).set_names({"result"}) return Model([res], [param], "test")
def test_get_result_index(): input_shape = PartialShape([1]) param = ops.parameter(input_shape, dtype=np.float32, name="data") relu = ops.relu(param, name="relu") function = Model(relu, [param], "TestFunction") assert len(function.outputs) == 1 assert function.get_result_index(function.outputs[0]) == 0
def test_batched_tensors(device): core = Core() # TODO: remove when plugins will support set_input_tensors core.register_plugin("openvino_template_plugin", "TEMPLATE") batch = 4 one_shape = [1, 2, 2, 2] one_shape_size = np.prod(one_shape) batch_shape = [batch, 2, 2, 2] data1 = ops.parameter(batch_shape, np.float32) data1.set_friendly_name("input0") data1.get_output_tensor(0).set_names({"tensor_input0"}) data1.set_layout(Layout("N...")) constant = ops.constant([1], np.float32) op1 = ops.add(data1, constant) op1.set_friendly_name("Add0") res1 = ops.result(op1) res1.set_friendly_name("Result0") res1.get_output_tensor(0).set_names({"tensor_output0"}) model = Model([res1], [data1]) compiled = core.compile_model(model, "TEMPLATE") req = compiled.create_infer_request() # Allocate 8 chunks, set 'user tensors' to 0, 2, 4, 6 chunks buffer = np.zeros([batch * 2, *batch_shape[1:]], dtype=np.float32) tensors = [] for i in range(batch): # non contiguous memory (i*2) tensors.append( Tensor(np.expand_dims(buffer[i * 2], 0), shared_memory=True)) req.set_input_tensors(tensors) with pytest.raises(RuntimeError) as e: req.get_tensor("tensor_input0") assert "get_tensor shall not be used together with batched set_tensors/set_input_tensors" in str( e.value) actual_tensor = req.get_tensor("tensor_output0") actual = actual_tensor.data for test_num in range(0, 5): for i in range(0, batch): tensors[i].data[:] = test_num + 10 req.infer() # Adds '1' to each element # Reference values for each batch: _tmp = np.array([test_num + 11] * one_shape_size, dtype=np.float32).reshape([2, 2, 2]) for j in range(0, batch): assert np.array_equal(actual[j], _tmp)
def test_ngraph_preprocess_set_shape(): shape = [1, 1, 1] parameter_a = ops.parameter(shape, dtype=np.int32, name="A") model = parameter_a function = Model(model, [parameter_a], "TestFunction") @custom_preprocess_function def custom_crop(out_node: Output): start = ops.constant(np.array([1, 1, 1]), dtype=np.int32) stop = ops.constant(np.array([2, 2, 2]), dtype=np.int32) step = ops.constant(np.array([1, 1, 1]), dtype=np.int32) axis = ops.constant(np.array([0, 1, 2]), dtype=np.int32) return ops.slice(out_node, start, stop, step, axis) p = PrePostProcessor(function) inp = p.input() inp.tensor().set_shape([3, 3, 3]) inp.preprocess().custom(custom_crop) function = p.build() input_data = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8]], [[9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]).astype(np.int32) expected_output = np.array([[[13]]]).astype(np.float32) runtime = get_runtime() computation = runtime.computation(function) output = computation(input_data) assert np.equal(output, expected_output).all()
def test_ngraph_preprocess_output_postprocess(): shape = [2, 2] parameter_a = ops.parameter(shape, dtype=np.int32, name="A") model = parameter_a function = Model(model, [parameter_a], "TestFunction") layout1 = ov.Layout("NCHW") layout2 = ov.Layout("NHWC") layout3 = [0, 1] @custom_preprocess_function def custom_postprocess(output: Output): return ops.abs(output) p = PrePostProcessor(function) inp = p.input() inp.tensor().set_layout(layout1) inp.preprocess().convert_element_type(Type.f32).mean([1., 2.]) out = p.output() out.postprocess().convert_element_type(Type.f32) \ .convert_layout(layout2) \ .convert_layout(layout3).custom(custom_postprocess) function = p.build() input_data = np.array([[-1, -2], [-3, -4]]).astype(np.int32) expected_output = np.array([[2, 4], [4, 6]]).astype(np.float32) runtime = get_runtime() computation = runtime.computation(function) output = computation(input_data) assert np.equal(output, expected_output).all()
def test_ngraph_preprocess_spatial_static_shape(): shape = [2, 2, 2] parameter_a = ops.parameter(shape, dtype=np.int32, name="A") model = parameter_a function = Model(model, [parameter_a], "TestFunction") layout = ov.Layout("CHW") color_format = ColorFormat.RGB p = PrePostProcessor(function) inp = p.input() inp.tensor().set_layout(layout).set_spatial_static_shape( 2, 2).set_color_format(color_format, []) inp.preprocess().convert_element_type(Type.f32).mean([1., 2.]) inp.model().set_layout(layout) out = p.output() out.tensor().set_layout(layout).set_element_type(Type.f32) out.model().set_layout(layout) function = p.build() input_data = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]).astype(np.int32) expected_output = np.array([[[0, 1], [2, 3]], [[3, 4], [5, 6]]]).astype(np.float32) runtime = get_runtime() computation = runtime.computation(function) output = computation(input_data) assert np.equal(output, expected_output).all()
def test_input_shape_read_only(): shape = Shape([1, 10]) param = ov.parameter(shape, dtype=np.float32) model = Model(ov.relu(param), [param]) ref_shape = model.input().shape ref_shape[0] = Dimension(3) assert model.input().shape == shape
def test_ngraph_preprocess_postprocess_layout(): shape = [1, 1, 3, 3] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") model = parameter_a function = Model(model, [parameter_a], "TestFunction") layout1 = ov.Layout("NCWH") layout2 = ov.Layout("NCHW") p = PrePostProcessor(function) inp = p.input() inp.tensor().set_layout(layout1) inp.preprocess().mean(1.).convert_layout(layout2).reverse_channels() out = p.output() out.postprocess().convert_layout([0, 1, 2, 3]) function = p.build() input_data = np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]]).astype(np.float32) expected_output = np.array([[[[0, 3, 6], [1, 4, 7], [2, 5, 8]]]]).astype(np.float32) runtime = get_runtime() computation = runtime.computation(function) output = computation(input_data) assert np.equal(output, expected_output).all()
def test_simple_tensor_iterator(): X = ov.parameter(Shape([32, 40, 10]), np.float32, "X") Y = ov.parameter(Shape([32, 40, 10]), np.float32, "Y") M = ov.parameter(Shape([32, 2, 10]), np.float32, "M") X_i = ov.parameter(Shape([32, 2, 10]), np.float32, "X_i") Y_i = ov.parameter(Shape([32, 2, 10]), np.float32, "Y_i") M_body = ov.parameter(Shape([32, 2, 10]), np.float32, "M_body") sum = ov.add(X_i, Y_i) Zo = ov.multiply(sum, M_body) body = Model([Zo], [X_i, Y_i, M_body], "body_function") ti = ov.tensor_iterator() ti.set_body(body) ti.set_sliced_input(X_i, X.output(0), 0, 2, 2, 39, 1) ti.set_sliced_input(Y_i, Y.output(0), 0, 2, 2, -1, 1) ti.set_invariant_input(M_body, M.output(0)) out0 = ti.get_iter_value(Zo.output(0), -1) out1 = ti.get_concatenated_slices(Zo.output(0), 0, 2, 2, 39, 1) result0 = ov.result(out0) result1 = ov.result(out1) out0_shape = [32, 2, 10] out1_shape = [32, 40, 10] assert list(result0.get_output_shape(0)) == out0_shape assert list(result1.get_output_shape(0)) == out1_shape assert list(ti.get_output_shape(0)) == out0_shape assert list(ti.get_output_shape(1)) == out1_shape
def test_ngraph_preprocess_dump(): shape = [1, 3, 224, 224] parameter_a = ops.parameter(shape, dtype=np.float32, name="RGB_input") model = parameter_a function = Model(model, [parameter_a], "TestFunction") p = PrePostProcessor(function) p.input().tensor()\ .set_layout(ov.Layout("NHWC"))\ .set_element_type(Type.u8)\ .set_spatial_dynamic_shape() p.input().preprocess()\ .convert_element_type(Type.f32)\ .reverse_channels()\ .mean([1, 2, 3])\ .scale([4, 5, 6])\ .resize(ResizeAlgorithm.RESIZE_LINEAR) p.input().model().set_layout(ov.Layout("NCHW")) p_str = str(p) print(p) assert "Pre-processing steps (5):" in p_str assert "convert type (f32):" in p_str assert "reverse channels:" in p_str assert "mean (1,2,3):" in p_str assert "scale (4,5,6):" in p_str assert "resize to model width/height:" in p_str assert "Implicit pre-processing steps (1):" in p_str assert "convert layout " + ov.Layout("NCHW").to_string() in p_str
def test_ngraph_preprocess_mean_scale_convert(): shape = [2, 2] param1 = ops.parameter(shape, dtype=np.int32, name="A") param2 = ops.parameter(shape, dtype=np.int32, name="B") function = Model([param1, param2], [param1, param2], "TestFunction") @custom_preprocess_function def custom_preprocess(output: Output): return ops.abs(output) p = PrePostProcessor(function) inp2 = p.input(1) inp2.tensor().set_element_type(Type.i32) inp2.preprocess().convert_element_type(Type.f32).mean(1.).scale(2.) inp1 = p.input(0) inp1.preprocess().convert_element_type( Type.f32).mean(1.).custom(custom_preprocess) function = p.build() input_data1 = np.array([[0, 1], [2, -2]]).astype(np.int32) input_data2 = np.array([[1, 3], [5, 7]]).astype(np.int32) expected_output1 = np.array([[1, 0], [1, 3]]).astype(np.float32) expected_output2 = np.array([[0, 1], [2, 3]]).astype(np.float32) runtime = get_runtime() computation = runtime.computation(function) [output1, output2] = computation(input_data1, input_data2) assert np.equal(output1, expected_output1).all() assert np.equal(output2, expected_output2).all()
def test_ngraph_preprocess_steps(algorithm, color_format1, color_format2, is_failing): shape = [1, 1, 3, 3] parameter_a = ops.parameter(shape, dtype=np.float32, name="A") model = parameter_a function = Model(model, [parameter_a], "TestFunction") layout1 = ov.Layout("NCWH") layout2 = ov.Layout("NCHW") custom_processor = PrePostProcessor(function) inp = custom_processor.input() inp.tensor().set_layout(layout1).set_color_format(color_format1, []) inp.preprocess().mean(1.).resize( algorithm, 3, 3).convert_layout(layout2).convert_color(color_format2) if is_failing: with pytest.raises(RuntimeError) as e: function = custom_processor.build() assert "is not convertible to" in str(e.value) else: function = custom_processor.build() input_data = np.array([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]]).astype(np.float32) expected_output = np.array([[[[0, 3, 6], [1, 4, 7], [2, 5, 8]]]]).astype(np.float32) runtime = get_runtime() computation = runtime.computation(function) output = computation(input_data) assert np.equal(output, expected_output).all()
def test_parameter_index_invalid(): shape1 = PartialShape([1]) param1 = ops.parameter(shape1, dtype=np.float32, name="data1") relu = ops.relu(param1, name="relu") function = Model(relu, [param1], "TestFunction") shape2 = PartialShape([2]) param2 = ops.parameter(shape2, dtype=np.float32, name="data2") assert function.get_parameter_index(param2) == -1
def create_function1(shape1=[2, 2]): input1 = ops.parameter(shape1, dtype=np.float32, name="input1") input1.get_output_tensor(0).set_names({'input1a', 'input1b'}) relu1 = ops.relu(input1) res1 = ops.result(relu1, "res1") res1.get_output_tensor(0).set_names({'res1', 'res1a'}) function = Model(results=[res1], parameters=[input1], name="TestFunction") return function
def test_get_batch(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Model(add, [param1, param2], "TestFunction") param = func.get_parameters()[0] param.set_layout(Layout("NC")) assert get_batch(func) == 2
def get_model(): param = opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") param.get_output_tensor(0).set_names({"parameter"}) relu = opset8.relu(param) reshape = opset8.reshape(relu, opset8.shape_of(relu), False) res = opset8.result(reshape, name="result") res.get_output_tensor(0).set_names({"result"}) return Model([res], [param], "test")
def create_model_with_memory(input_shape, data_type): input_data = ops.parameter(input_shape, name="input_data", dtype=data_type) rv = ops.read_value(input_data, "var_id_667") add = ops.add(rv, input_data, name="MemoryAdd") node = ops.assign(add, "var_id_667") res = ops.result(add, "res") model = Model(results=[res], sinks=[node], parameters=[input_data], name="name") return model
def test_batched_tensors(device): batch = 4 one_shape = Shape([1, 2, 2, 2]) batch_shape = Shape([batch, 2, 2, 2]) one_shape_size = np.prod(one_shape) core = Core() core.register_plugin("openvino_template_plugin", "TEMPLATE") data1 = ops.parameter(batch_shape, np.float32) data1.set_friendly_name("input0") data1.get_output_tensor(0).set_names({"tensor_input0"}) data1.set_layout(Layout("N...")) constant = ops.constant([1], np.float32) op1 = ops.add(data1, constant) op1.set_friendly_name("Add0") res1 = ops.result(op1) res1.set_friendly_name("Result0") res1.get_output_tensor(0).set_names({"tensor_output0"}) model = Model([res1], [data1]) compiled = core.compile_model(model, "TEMPLATE") buffer = np.zeros([one_shape_size * batch * 2], dtype=np.float32) req = compiled.create_infer_request() tensors = [] for i in range(0, batch): _start = i * one_shape_size * 2 # Use of special constructor for Tensor. # It creates a Tensor from pointer, thus it requires only # one element from original buffer, and shape to "crop". tensor = Tensor(buffer[_start:(_start + 1)], one_shape) tensors.append(tensor) req.set_input_tensors(tensors) # using list overload! actual_tensor = req.get_tensor("tensor_output0") actual = actual_tensor.data for test_num in range(0, 5): for i in range(0, batch): tensors[i].data[:] = test_num + 10 req.infer() # Adds '1' to each element # Reference values for each batch: _tmp = np.array([test_num + 11] * one_shape_size, dtype=np.float32).reshape([2, 2, 2]) for j in range(0, batch): assert np.array_equal(actual[j], _tmp)
def test_compress_model_transformation(): node_constant = ov.opset8.constant(np.array([[0.0, 0.1, -0.1], [-2.5, 2.5, 3.0]], dtype=np.float32)) node_ceil = ov.opset8.ceiling(node_constant) func = Model(node_ceil, [], "TestFunction") assert func.get_ordered_ops()[0].get_element_type().get_type_name() == "f32" compress_model_transformation(func) assert func is not None assert func.get_ordered_ops()[0].get_element_type().get_type_name() == "f16"
def test_set_batch_default_batch_size(): param1 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data1") param2 = ops.parameter(Shape([2, 1]), dtype=np.float32, name="data2") add = ops.add(param1, param2) func = Model(add, [param1, param2], "TestFunction") func_param1 = func.get_parameters()[0] func_param1.set_layout(Layout("NC")) set_batch(func) assert func.is_dynamic()
def test_reshape(device): shape = Shape([1, 10]) param = ops.parameter(shape, dtype=np.float32) model = Model(ops.relu(param), [param]) ref_shape = model.input().partial_shape ref_shape[0] = 3 model.reshape(ref_shape) core = Core() compiled = core.compile_model(model, device) assert compiled.input().partial_shape == ref_shape
def test_get_results(device): core = Core() data = ops.parameter([10], np.float64) model = Model(ops.split(data, 0, 5), [data]) compiled = core.compile_model(model, device) request = compiled.create_infer_request() inputs = [np.random.normal(size=list(compiled.input().shape))] results = request.infer(inputs) for output in compiled.outputs: assert np.array_equal(results[output], request.results[output])