def test_discrete_type_info(): data_shape = [6, 12, 10, 24] data_parameter = ov.parameter(data_shape, name="Data", dtype=np.float32) k = np.int32(3) axis = np.int32(1) n1 = ov.topk(data_parameter, k, axis, "max", "value") n2 = ov.topk(data_parameter, k, axis, "max", "value") n3 = ov.sin(0.2) assert n1.type_info.name == "TopK" assert n3.type_info.name == "Sin" assert n1.get_type_info().name == "TopK" assert n3.get_type_info().name == "Sin" assert n1.type_info.name == n2.type_info.name assert n1.type_info.version == n2.type_info.version assert n1.type_info.parent == n2.type_info.parent assert n1.get_type_info().name == n2.get_type_info().name assert n1.get_type_info().version == n2.get_type_info().version assert n1.get_type_info().parent == n2.get_type_info().parent assert n1.get_type_info().name != n3.get_type_info().name assert n1.get_type_info().name > n3.get_type_info().name assert n1.get_type_info().name >= n3.get_type_info().name assert n3.get_type_info().name < n1.get_type_info().name assert n3.get_type_info().name <= n1.get_type_info().name
def test_sink_function_ctor(): input_data = ops.parameter([2, 2], name="input_data", dtype=np.float32) 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") function = Model(results=[res], sinks=[node], parameters=[input_data], name="TestFunction") ordered_ops = function.get_ordered_ops() op_types = [op.get_type_name() for op in ordered_ops] assert op_types == ["Parameter", "ReadValue", "Add", "Assign", "Result"] assert len(function.get_ops()) == 5 assert function.get_output_size() == 1 assert function.get_output_op(0).get_type_name() == "Result" assert function.get_output_element_type(0) == input_data.get_element_type() assert list(function.get_output_shape(0)) == [2, 2] assert (function.get_parameters()[0].get_partial_shape()) == PartialShape( [2, 2]) assert len(function.get_parameters()) == 1 assert len(function.get_results()) == 1 assert function.get_friendly_name() == "TestFunction"
def test_pad_constant(): input_data = np.arange(1, 13).reshape([3, 4]).astype(np.int32) pads_begin = np.array([0, 1], dtype=np.int32) pads_end = np.array([2, 3], dtype=np.int32) input_param = ov.parameter(input_data.shape, name="input", dtype=np.int32) model = ov.pad(input_param, pads_begin, pads_end, "constant", arg_pad_value=np.array(100, dtype=np.int32)) runtime = get_runtime() computation = runtime.computation(model, input_param) result = computation(input_data) expected = np.array([ [100, 1, 2, 3, 4, 100, 100, 100], [100, 5, 6, 7, 8, 100, 100, 100], [100, 9, 10, 11, 12, 100, 100, 100], [100, 100, 100, 100, 100, 100, 100, 100], [100, 100, 100, 100, 100, 100, 100, 100], ]) assert np.allclose(result, expected)
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_group_convolution_backprop_data(): runtime = get_runtime() data_shape = [1, 1, 3, 3] filters_shape = [1, 1, 1, 3, 3] strides = [2, 2] output_padding = [1, 1] pads_begin = [1, 1] pads_end = [1, 1] data_node = ov.parameter(data_shape, name="Data", dtype=np.float32) filters_node = ov.parameter(filters_shape, name="Filters", dtype=np.float32) model = ov.group_convolution_backprop_data(data_node, filters_node, strides, None, pads_begin, pads_end, output_padding=output_padding) data_value = np.array( [ 0.16857791, -0.15161794, 0.08540368, 0.1820628, -0.21746576, 0.08245695, 0.1431433, -0.43156421, 0.30591947, ], dtype=np.float32, ).reshape(data_shape) filters_value = np.array( [ -0.06230065, 0.37932432, -0.25388849, 0.33878803, 0.43709868, -0.22477469, 0.04118127, -0.44696793, 0.06373066, ], dtype=np.float32, ).reshape(filters_shape) computation = runtime.computation(model, data_node, filters_node) result = computation(data_value, filters_value) expected = np.array( [ 0.07368518, -0.08925839, -0.06627201, 0.06301362, 0.03732984, -0.01919658, -0.00628807, -0.02817563, -0.01472169, 0.04392925, -0.00689478, -0.01549204, 0.07957941, -0.11459791, -0.09505399, 0.07681622, 0.03604182, -0.01853423, -0.0270785, -0.00680824, -0.06650258, 0.08004665, 0.07918708, 0.0724144, 0.06256775, -0.17838378, -0.18863615, 0.20064656, 0.133717, -0.06876295, -0.06398046, -0.00864975, 0.19289537, -0.01490572, -0.13673618, 0.01949645, ], dtype=np.float32, ).reshape(1, 1, 6, 6) assert np.allclose(result, expected)
def test_space_to_depth_operator(): runtime = get_runtime() data_shape = [1, 2, 4, 4] data_value = np.arange(start=0, stop=32, step=1.0, dtype=np.float32).reshape(data_shape) mode = "blocks_first" block_size = 2 parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32) model = ov.space_to_depth(parameter_data, mode, block_size) computation = runtime.computation(model, parameter_data) result = computation(data_value) expected = np.array( [ 0, 2, 8, 10, 16, 18, 24, 26, 1, 3, 9, 11, 17, 19, 25, 27, 4, 6, 12, 14, 20, 22, 28, 30, 5, 7, 13, 15, 21, 23, 29, 31, ], dtype=np.float32, ).reshape(1, 8, 2, 2) assert np.allclose(result, expected) batch_size = 2 input_size = 3 hidden_size = 3 X_shape = [batch_size, input_size] H_t_shape = [batch_size, hidden_size] W_shape = [hidden_size, input_size] R_shape = [hidden_size, hidden_size] B_shape = [hidden_size] parameter_X = ov.parameter(X_shape, name="X", dtype=np.float32) parameter_H_t = ov.parameter(H_t_shape, name="H_t", dtype=np.float32) parameter_W = ov.parameter(W_shape, name="W", dtype=np.float32) parameter_R = ov.parameter(R_shape, name="R", dtype=np.float32) parameter_B = ov.parameter(B_shape, name="B", dtype=np.float32) X_value = np.array( [0.3432185, 0.612268, 0.20272376, 0.9513413, 0.30585995, 0.7265472], dtype=np.float32).reshape(X_shape) H_t_value = np.array( [0.12444675, 0.52055854, 0.46489045, 0.4983964, 0.7730452, 0.28439692], dtype=np.float32).reshape(H_t_shape) W_value = np.array( [ 0.41930267, 0.7872176, 0.89940447, 0.23659843, 0.24676207, 0.17101714, 0.3147149, 0.6555601, 0.4559603, ], dtype=np.float32, ).reshape(W_shape) R_value = np.array( [ 0.8374871, 0.86660194, 0.82114047, 0.71549815, 0.18775631, 0.3182116, 0.25392973, 0.38301638, 0.85531586, ], dtype=np.float32, ).reshape(R_shape) B_value = np.array([1.0289404, 1.6362579, 0.4370661], dtype=np.float32).reshape(B_shape) activations = ["sigmoid"] activation_alpha = [] activation_beta = [] clip = 2.88 model = ov.rnn_cell( parameter_X, parameter_H_t, parameter_W, parameter_R, parameter_B, hidden_size, activations, activation_alpha, activation_beta, clip, ) computation = runtime.computation(model, parameter_X, parameter_H_t, parameter_W, parameter_R, parameter_B) result = computation(X_value, H_t_value, W_value, R_value, B_value) expected = np.array( [0.94126844, 0.9036043, 0.841243, 0.9468489, 0.934215, 0.873708], dtype=np.float32).reshape(batch_size, hidden_size) assert np.allclose(result, expected)
def test_fake_quantize(): runtime = get_runtime() data_value = np.arange(24.0, dtype=np.float32).reshape(1, 2, 3, 4) input_low_value = np.float32(0) input_high_value = np.float32(23) output_low_value = np.float32(2) output_high_value = np.float32(16) levels = np.float32(4) data_shape = [1, 2, 3, 4] bound_shape = [] parameter_data = ov.parameter(data_shape, name="data", dtype=np.float32) parameter_input_low = ov.parameter(bound_shape, name="input_low", dtype=np.float32) parameter_input_high = ov.parameter(bound_shape, name="input_high", dtype=np.float32) parameter_output_low = ov.parameter(bound_shape, name="output_low", dtype=np.float32) parameter_output_high = ov.parameter(bound_shape, name="output_high", dtype=np.float32) model = ov.fake_quantize( parameter_data, parameter_input_low, parameter_input_high, parameter_output_low, parameter_output_high, levels, ) computation = runtime.computation( model, parameter_data, parameter_input_low, parameter_input_high, parameter_output_low, parameter_output_high, ) result = computation(data_value, input_low_value, input_high_value, output_low_value, output_high_value) expected = np.array( [[[ [ [2.0, 2.0, 2.0, 2.0], [6.6666669, 6.6666669, 6.6666669, 6.6666669], [6.6666669, 6.6666669, 6.6666669, 6.6666669], ], [ [11.33333301, 11.33333301, 11.33333301, 11.33333301], [11.33333301, 11.33333301, 11.33333301, 11.33333301], [16.0, 16.0, 16.0, 16.0], ], ]]], dtype=np.float32, ) assert np.allclose(result, expected)
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
def test_parameter_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 function.get_parameter_index(param) == 0
def create_test_model(): 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) return Model(add, [param1, param2], "TestFunction")
def test_reverse_sequence(): input_data = np.array( [ 0, 0, 3, 0, 6, 0, 9, 0, 1, 0, 4, 0, 7, 0, 10, 0, 2, 0, 5, 0, 8, 0, 11, 0, 12, 0, 15, 0, 18, 0, 21, 0, 13, 0, 16, 0, 19, 0, 22, 0, 14, 0, 17, 0, 20, 0, 23, 0, ], dtype=np.int32, ).reshape([2, 3, 4, 2]) seq_lengths = np.array([1, 2, 1, 2], dtype=np.int32) batch_axis = 2 sequence_axis = 1 input_param = ov.parameter(input_data.shape, name="input", dtype=np.int32) seq_lengths_param = ov.parameter(seq_lengths.shape, name="sequence lengths", dtype=np.int32) model = ov.reverse_sequence(input_param, seq_lengths_param, batch_axis, sequence_axis) runtime = get_runtime() computation = runtime.computation(model, input_param, seq_lengths_param) result = computation(input_data, seq_lengths) expected = np.array([ 0, 0, 4, 0, 6, 0, 10, 0, 1, 0, 3, 0, 7, 0, 9, 0, 2, 0, 5, 0, 8, 0, 11, 0, 12, 0, 16, 0, 18, 0, 22, 0, 13, 0, 15, 0, 19, 0, 21, 0, 14, 0, 17, 0, 20, 0, 23, 0, ], ).reshape([1, 2, 3, 4, 2]) assert np.allclose(result, expected)
def get_test_function(): # Parameter->Relu->Result param = opset8.parameter(PartialShape([1, 3, 22, 22]), name="parameter") relu = opset8.relu(param.output(0)) res = opset8.result(relu.output(0), name="result") return Model([res], [param], "test")
def test_reshape_with_python_types(device): model = create_test_model() def check_shape(new_shape): for input in model.inputs: assert input.partial_shape == new_shape shape1 = [1, 4] new_shapes = {input: shape1 for input in model.inputs} model.reshape(new_shapes) check_shape(PartialShape(shape1)) shape2 = [1, 6] new_shapes = {input.any_name: shape2 for input in model.inputs} model.reshape(new_shapes) check_shape(PartialShape(shape2)) shape3 = [1, 8] new_shapes = {i: shape3 for i, input in enumerate(model.inputs)} model.reshape(new_shapes) check_shape(PartialShape(shape3)) shape4 = [1, -1] new_shapes = {input: shape4 for input in model.inputs} model.reshape(new_shapes) check_shape(PartialShape([Dimension(1), Dimension(-1)])) shape5 = [1, (1, 10)] new_shapes = {input: shape5 for input in model.inputs} model.reshape(new_shapes) check_shape(PartialShape([Dimension(1), Dimension(1, 10)])) shape6 = [Dimension(3), Dimension(3, 10)] new_shapes = {input: shape6 for input in model.inputs} model.reshape(new_shapes) check_shape(PartialShape(shape6)) shape7 = "1..10, ?" new_shapes = {input: shape7 for input in model.inputs} model.reshape(new_shapes) check_shape(PartialShape(shape7)) # reshape mixed keys shape8 = [(1, 20), -1] new_shapes = {"data1": shape8, 1: shape8} model.reshape(new_shapes) check_shape(PartialShape([Dimension(1, 20), Dimension(-1)])) # reshape with one input param = ops.parameter([1, 3, 28, 28]) model = Model(ops.relu(param), [param]) shape9 = [-1, 3, (28, 56), (28, 56)] model.reshape(shape9) check_shape( PartialShape([ Dimension(-1), Dimension(3), Dimension(28, 56), Dimension(28, 56) ])) shape10 = "?,3,..224,..224" model.reshape(shape10) check_shape( PartialShape([ Dimension(-1), Dimension(3), Dimension(-1, 224), Dimension(-1, 224) ])) # check exceptions shape10 = [1, 1, 1, 1] with pytest.raises(TypeError) as e: model.reshape({model.input().node: shape10}) assert "Incorrect key type <class 'openvino.pyopenvino.op.Parameter'> to reshape a model, " \ "expected keys as openvino.runtime.Output, int or str." in str(e.value) with pytest.raises(TypeError) as e: model.reshape({0: range(1, 9)}) assert "Incorrect value type <class 'range'> to reshape a model, " \ "expected values as openvino.runtime.PartialShape, str, list or tuple." in str(e.value)
time.sleep(0.000001) # increment reference counting of args while running func args_ = args # noqa: F841 'assigned to but never used' gil_released = True thread = Thread(target=detect_gil) thread.start() func(*args) if not gil_released: pytest.xfail(reason="Depend on condition race") thread.join() device = os.environ.get("TEST_DEVICE") if os.environ.get("TEST_DEVICE") else "CPU" core = Core() core.set_property({"PERF_COUNT": "YES"}) param = ops.parameter([224, 224]) model = Model(ops.relu(param), [param]) compiled = core.compile_model(model, device) infer_queue = AsyncInferQueue(compiled, 1) user_stream = io.BytesIO() # AsyncInferQueue def test_gil_released_async_infer_queue_start_async(): infer_queue.start_async() check_gil_released_safe(infer_queue.start_async) def test_gil_released_async_infer_queue_is_ready(): infer_queue.start_async()