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 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 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 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 = GraphBody([X_t, Y_t], [then_body_res_1]) then_body_inputs = [ TensorIteratorInvariantInputDesc(1, 0), TensorIteratorInvariantInputDesc(2, 1) ] then_body_outputs = [TensorIteratorBodyOutputDesc(0, 0)] # 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 = GraphBody([X_e, Y_e], [else_body_res_1]) else_body_inputs = [ TensorIteratorInvariantInputDesc(1, 0), TensorIteratorInvariantInputDesc(2, 1) ] else_body_outputs = [TensorIteratorBodyOutputDesc(0, 0)] X = ov.constant([3, 4], dtype=np.float32) Y = ov.constant([2, 1], dtype=np.float32) if_node = ov.if_op(condition, [X, Y], (then_body, else_body), (then_body_inputs, else_body_inputs), (then_body_outputs, else_body_outputs)) relu = ov.relu(if_node) return relu
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 create_diff_if_with_two_outputs(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") mmul_t = ov.matmul(X_t, Y_t, False, False) mul_t = ov.multiply(Y_t, X_t) then_body_res_1 = ov.result(mmul_t) then_body_res_2 = ov.result(mul_t) then_body = GraphBody([X_t, Y_t], [then_body_res_1, then_body_res_2]) then_body_inputs = [ TensorIteratorInvariantInputDesc(1, 0), TensorIteratorInvariantInputDesc(2, 1) ] then_body_outputs = [ TensorIteratorBodyOutputDesc(0, 0), TensorIteratorBodyOutputDesc(1, 1) ] # else_body X_e = ov.parameter([2], np.float32, "X") Z_e = ov.parameter([], np.float32, "Z") mul_e = ov.multiply(X_e, Z_e) else_body_res_1 = ov.result(Z_e) else_body_res_2 = ov.result(mul_e) else_body = GraphBody([X_e, Z_e], [else_body_res_1, else_body_res_2]) else_body_inputs = [ TensorIteratorInvariantInputDesc(1, 0), TensorIteratorInvariantInputDesc(3, 1) ] else_body_outputs = [ TensorIteratorBodyOutputDesc(0, 0), TensorIteratorBodyOutputDesc(1, 1) ] X = ov.constant([3, 4], dtype=np.float32) Y = ov.constant([2, 1], dtype=np.float32) Z = ov.constant(4.0, dtype=np.float32) if_node = ov.if_op(condition, [X, Y, Z], (then_body, else_body), (then_body_inputs, else_body_inputs), (then_body_outputs, else_body_outputs)) return if_node
def create_simple_if_with_two_outputs(condition_val): condition = ov.constant(condition_val, dtype=np.bool) # then_body X_t = ov.parameter([], np.float32, "X") Y_t = ov.parameter([], np.float32, "Y") Z_t = ov.parameter([], np.float32, "Z") add_t = ov.add(X_t, Y_t) mul_t = ov.multiply(Y_t, Z_t) then_body_res_1 = ov.result(add_t) then_body_res_2 = ov.result(mul_t) then_body = GraphBody([X_t, Y_t, Z_t], [then_body_res_1, then_body_res_2]) then_body_inputs = [ TensorIteratorInvariantInputDesc(1, 0), TensorIteratorInvariantInputDesc(2, 1), TensorIteratorInvariantInputDesc(3, 2) ] then_body_outputs = [ TensorIteratorBodyOutputDesc(0, 0), TensorIteratorBodyOutputDesc(1, 1) ] # else_body X_e = ov.parameter([], np.float32, "X") Z_e = ov.parameter([], np.float32, "Z") W_e = ov.parameter([], np.float32, "W") add_e = ov.add(X_e, W_e) pow_e = ov.power(W_e, Z_e) else_body_res_1 = ov.result(add_e) else_body_res_2 = ov.result(pow_e) else_body = GraphBody([X_e, Z_e, W_e], [else_body_res_1, else_body_res_2]) else_body_inputs = [ TensorIteratorInvariantInputDesc(1, 0), TensorIteratorInvariantInputDesc(3, 1), TensorIteratorInvariantInputDesc(4, 2) ] else_body_outputs = [ TensorIteratorBodyOutputDesc(0, 0), TensorIteratorBodyOutputDesc(1, 1) ] X = ov.constant(15.0, dtype=np.float32) Y = ov.constant(-5.0, dtype=np.float32) Z = ov.constant(4.0, dtype=np.float32) W = ov.constant(2.0, dtype=np.float32) if_node = ov.if_op(condition, [X, Y, Z, W], (then_body, else_body), (then_body_inputs, else_body_inputs), (then_body_outputs, else_body_outputs)) return if_node
def test_simple_loop(): X = ov.parameter(Shape([32, 1, 10]), np.float32, "X") Y = ov.parameter(Shape([32, 1, 10]), np.float32, "Y") M = ov.parameter(Shape([32, 1, 10]), np.float32, "M") input_shape = Shape([]) current_iteration = ov.parameter(Shape([1]), np.int64) X_i = ov.parameter(input_shape, np.float32) Y_i = ov.parameter(input_shape, np.float32) M_body = ov.parameter(input_shape, np.float32) bool_val = np.array([1], dtype=np.bool) bool_val[0] = True body_condition = ov.constant(bool_val) trip_count = ov.constant(10, dtype=np.int64) exec_condition = ov.constant(True, dtype=np.bool) sum = ov.add(X_i, Y_i) Zo = ov.multiply(sum, M_body) body = Model([body_condition, Zo], [current_iteration, X_i, Y_i, M_body], "body_function") loop = ov.loop(trip_count, exec_condition) loop.set_function(body) loop.set_invariant_input(X_i, X.output(0)) loop.set_invariant_input(Y_i, Y.output(0)) loop.set_merged_input(M_body, M.output(0), Zo.output(0)) loop.set_special_body_ports([-1, 0]) out0 = loop.get_iter_value(body_condition.output(0), -1) out1 = loop.get_iter_value(Zo.output(0), -1) out2 = loop.get_concatenated_slices(Zo.output(0), 0, 1, 1, -1, 1) result0 = ov.result(out0) result1 = ov.result(out1) result2 = ov.result(out2) out0_shape = [1] out1_shape = [32, 1, 10] out2_shape = [32, 10, 10] assert list(result0.get_output_shape(0)) == out0_shape assert list(result1.get_output_shape(0)) == out1_shape assert list(result2.get_output_shape(0)) == out2_shape assert list(loop.get_output_shape(0)) == out0_shape assert list(loop.get_output_shape(1)) == out1_shape assert list(loop.get_output_shape(2)) == out2_shape
def create_diff_if_with_two_outputs(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") mmul_t = ov.matmul(X_t, Y_t, False, False) mul_t = ov.multiply(Y_t, X_t) then_body_res_1 = ov.result(mmul_t) then_body_res_2 = ov.result(mul_t) then_body = Model([then_body_res_1, then_body_res_2], [X_t, Y_t], "then_body_function") # else_body X_e = ov.parameter([2], np.float32, "X") Z_e = ov.parameter([], np.float32, "Z") mul_e = ov.multiply(X_e, Z_e) else_body_res_1 = ov.result(Z_e) else_body_res_2 = ov.result(mul_e) else_body = Model([else_body_res_1, else_body_res_2], [X_e, Z_e], "else_body_function") X = ov.constant([3, 4], dtype=np.float32) Y = ov.constant([2, 1], dtype=np.float32) Z = ov.constant(4.0, 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, None) if_node.set_input(Z.output(0), None, Z_e) if_node.set_output(then_body_res_1, else_body_res_1) if_node.set_output(then_body_res_2, else_body_res_2) return if_node
def test_simple_if_basic(): condition = ov.constant(True, 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") then_body_inputs = [ InvariantInputDescription(1, 0), InvariantInputDescription(2, 1) ] else_body_outputs = [BodyOutputDescription(0, 0)] if_node = ov.if_op(condition.output(0)) if_node.set_function(0, then_body) assert if_node.get_function(0) == then_body if_node.set_input_descriptions(0, then_body_inputs) if_node.set_output_descriptions(1, else_body_outputs) input_desc = if_node.get_input_descriptions(0) output_desc = if_node.get_output_descriptions(1) assert len(input_desc) == len(then_body_inputs) assert len(output_desc) == len(else_body_outputs) for i in range(len(input_desc)): assert input_desc[i].get_type_info( ) == then_body_inputs[i].get_type_info() assert input_desc[i].input_index == then_body_inputs[i].input_index assert input_desc[i].body_parameter_index == then_body_inputs[ i].body_parameter_index for i in range(len(output_desc)): assert output_desc[i].get_type_info( ) == else_body_outputs[i].get_type_info() assert output_desc[i].output_index == else_body_outputs[i].output_index assert output_desc[i].body_value_index == else_body_outputs[ i].body_value_index
def create_simple_if_with_two_outputs(condition_val): condition = ov.constant(condition_val, dtype=np.bool) # then_body X_t = ov.parameter([], np.float32, "X") Y_t = ov.parameter([], np.float32, "Y") Z_t = ov.parameter([], np.float32, "Z") add_t = ov.add(X_t, Y_t) mul_t = ov.multiply(Y_t, Z_t) then_body_res_1 = ov.result(add_t) then_body_res_2 = ov.result(mul_t) then_body = Model([then_body_res_1, then_body_res_2], [X_t, Y_t, Z_t], "then_body_function") # else_body X_e = ov.parameter([], np.float32, "X") Z_e = ov.parameter([], np.float32, "Z") W_e = ov.parameter([], np.float32, "W") add_e = ov.add(X_e, W_e) pow_e = ov.power(W_e, Z_e) else_body_res_1 = ov.result(add_e) else_body_res_2 = ov.result(pow_e) else_body = Model([else_body_res_1, else_body_res_2], [X_e, Z_e, W_e], "else_body_function") X = ov.constant(15.0, dtype=np.float32) Y = ov.constant(-5.0, dtype=np.float32) Z = ov.constant(4.0, dtype=np.float32) W = ov.constant(2.0, 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, None) if_node.set_input(Z.output(0), Z_t, Z_e) if_node.set_input(W.output(0), None, W_e) if_node.set_output(then_body_res_1, else_body_res_1) if_node.set_output(then_body_res_2, else_body_res_2) return if_node
def test_add_with_mul(): element_type = Type.f32 shape = Shape([4]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) C = Parameter(element_type, shape) parameter_list = [A, B, C] function = Model([ov.multiply(ov.add(A, B), C)], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, A, B, C) result = computation( np.array([1, 2, 3, 4], dtype=np.float32), np.array([5, 6, 7, 8], dtype=np.float32), np.array([9, 10, 11, 12], dtype=np.float32), )[0] a_arr = np.array([1, 2, 3, 4], dtype=np.float32) b_arr = np.array([5, 6, 7, 8], dtype=np.float32) c_arr = np.array([9, 10, 11, 12], dtype=np.float32) result_arr_ref = (a_arr + b_arr) * c_arr assert np.allclose(result, result_arr_ref)
def binary_op(op_str, a, b): if op_str == "+": return a + b elif op_str == "Add": return ov.add(a, b) elif op_str == "-": return a - b elif op_str == "Sub": return ov.subtract(a, b) elif op_str == "*": return a * b elif op_str == "Mul": return ov.multiply(a, b) elif op_str == "/": return a / b elif op_str == "Div": return ov.divide(a, b) elif op_str == "Equal": return ov.equal(a, b) elif op_str == "Greater": return ov.greater(a, b) elif op_str == "GreaterEq": return ov.greater_equal(a, b) elif op_str == "Less": return ov.less(a, b) elif op_str == "LessEq": return ov.less_equal(a, b) elif op_str == "Maximum": return ov.maximum(a, b) elif op_str == "Minimum": return ov.minimum(a, b) elif op_str == "NotEqual": return ov.not_equal(a, b) elif op_str == "Power": return ov.power(a, b)
def test_loop_basic(): bool_val = np.array([1], dtype=np.bool) bool_val[0] = True condition = ov.constant(bool_val) trip_count = ov.constant(16, dtype=np.int32) # Body parameters body_timestep = ov.parameter([], np.int32, "timestep") body_data_in = ov.parameter([1, 2, 2], np.float32, "body_in") body_prev_cma = ov.parameter([2, 2], np.float32, "body_prev_cma") body_const_one = ov.parameter([], np.int32, "body_const_one") body_ports = [-1, 2] # CMA = cumulative moving average prev_cum_sum = ov.multiply(ov.convert(body_timestep, "f32"), body_prev_cma) curr_cum_sum = ov.add(prev_cum_sum, ov.squeeze(body_data_in, [0])) elem_cnt = ov.add(body_const_one, body_timestep) curr_cma = ov.divide(curr_cum_sum, ov.convert(elem_cnt, "f32")) cma_hist = ov.unsqueeze(curr_cma, [0]) # TI inputs data = ov.parameter([16, 2, 2], np.float32, "data") # Iterations count zero = ov.constant(0, dtype=np.int32) one = ov.constant(1, dtype=np.int32) initial_cma = ov.constant(np.zeros([2, 2], dtype=np.float32), dtype=np.float32) iter_cnt = ov.range(zero, np.int32(16), np.int32(1)) body_const_condition = ov.constant(bool_val) graph_body = Model( [curr_cma, cma_hist, body_const_condition], [body_timestep, body_data_in, body_prev_cma, body_const_one], "body_function") ti_slice_input_desc = [ # timestep # input_idx, body_param_idx, start, stride, part_size, end, axis SliceInputDescription(2, 0, 0, 1, 1, -1, 0), # data SliceInputDescription(3, 1, 0, 1, 1, -1, 0), ] ti_merged_input_desc = [ # body prev/curr_cma MergedInputDescription(4, 2, 0), ] ti_invariant_input_desc = [ # body const one InvariantInputDescription(5, 3), ] # TI outputs ti_body_output_desc = [ # final average BodyOutputDescription(0, 0, -1), ] ti_concat_output_desc = [ # history of cma ConcatOutputDescription(1, 1, 0, 1, 1, -1, 0), ] loop = ov.loop(trip_count, condition) loop.set_function(graph_body) loop.set_special_body_ports(body_ports) loop.set_sliced_input(body_timestep, iter_cnt.output(0), 0, 1, 1, 0, 0) loop.set_sliced_input(body_data_in, data.output(0), 0, 1, 1, 0, 0) # set different end parameter loop.set_input_descriptions(ti_slice_input_desc) loop.set_merged_input(body_prev_cma, initial_cma.output(0), curr_cma.output(0)) loop.set_invariant_input(body_const_one, one.output(0)) loop.get_iter_value(curr_cma.output(0), -1) loop.get_concatenated_slices(cma_hist.output(0), 0, 1, 1, -1, 0) assert loop.get_function() == graph_body assert loop.get_special_body_ports() == body_ports assert loop.get_num_iterations() == 16 input_desc = loop.get_input_descriptions() output_desc = loop.get_output_descriptions() assert len(input_desc) == len(ti_slice_input_desc) + \ len(ti_merged_input_desc) + len(ti_invariant_input_desc) assert len( output_desc) == len(ti_body_output_desc) + len(ti_concat_output_desc) for i in range(len(ti_slice_input_desc)): assert input_desc[i].get_type_info( ) == ti_slice_input_desc[i].get_type_info() assert input_desc[i].input_index == ti_slice_input_desc[i].input_index assert input_desc[i].body_parameter_index == ti_slice_input_desc[ i].body_parameter_index assert input_desc[i].start == ti_slice_input_desc[i].start assert input_desc[i].stride == ti_slice_input_desc[i].stride assert input_desc[i].part_size == ti_slice_input_desc[i].part_size assert input_desc[i].end == ti_slice_input_desc[i].end assert input_desc[i].axis == ti_slice_input_desc[i].axis for i in range(len(ti_merged_input_desc)): assert input_desc[ len(ti_slice_input_desc) + i].get_type_info() == ti_merged_input_desc[i].get_type_info() assert input_desc[len(ti_slice_input_desc) + i].input_index == ti_merged_input_desc[i].input_index assert input_desc[len(ti_slice_input_desc) + i].body_parameter_index == ti_merged_input_desc[ i].body_parameter_index assert input_desc[ len(ti_slice_input_desc) + i].body_value_index == ti_merged_input_desc[i].body_value_index for i in range(len(ti_concat_output_desc)): assert output_desc[ len(ti_body_output_desc) + i].get_type_info() == ti_concat_output_desc[i].get_type_info() assert output_desc[ len(ti_body_output_desc) + i].output_index == ti_concat_output_desc[i].output_index assert output_desc[ len(ti_body_output_desc) + i].body_value_index == ti_concat_output_desc[i].body_value_index assert output_desc[len(ti_body_output_desc) + i].start == ti_concat_output_desc[i].start assert output_desc[len(ti_body_output_desc) + i].stride == ti_concat_output_desc[i].stride assert output_desc[len(ti_body_output_desc) + i].part_size == ti_concat_output_desc[i].part_size assert output_desc[len(ti_body_output_desc) + i].end == ti_concat_output_desc[i].end assert output_desc[len(ti_body_output_desc) + i].axis == ti_concat_output_desc[i].axis
from openvino.runtime import opset1 from openvino.runtime import opset2 from openvino.runtime import opset3 from openvino.runtime import opset4 from openvino.runtime import opset5 from openvino.runtime import opset6 from openvino.runtime import opset7 from openvino.runtime import opset8 # Helper functions for openvino module from openvino.runtime.ie_api import tensor_from_file from openvino.runtime.ie_api import compile_model # Extend Node class to support binary operators Node.__add__ = opset8.add Node.__sub__ = opset8.subtract Node.__mul__ = opset8.multiply Node.__div__ = opset8.divide Node.__truediv__ = opset8.divide Node.__radd__ = lambda left, right: opset8.add(right, left) Node.__rsub__ = lambda left, right: opset8.subtract(right, left) Node.__rmul__ = lambda left, right: opset8.multiply(right, left) Node.__rdiv__ = lambda left, right: opset8.divide(right, left) Node.__rtruediv__ = lambda left, right: opset8.divide(right, left) Node.__eq__ = opset8.equal Node.__ne__ = opset8.not_equal Node.__lt__ = opset8.less Node.__le__ = opset8.less_equal Node.__gt__ = opset8.greater Node.__ge__ = opset8.greater_equal