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 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 test_lrn(): input_image_shape = (2, 3, 2, 1) input_image = np.arange(int(np.prod(input_image_shape))).reshape(input_image_shape).astype("f") axes = np.array([1], dtype=np.int64) runtime = get_runtime() model = ov.lrn(ov.constant(input_image), ov.constant(axes), alpha=1.0, beta=2.0, bias=1.0, size=3) computation = runtime.computation(model) result = computation() assert np.allclose( result, np.array( [ [[[0.0], [0.05325444]], [[0.03402646], [0.01869806]], [[0.06805293], [0.03287071]]], [[[0.00509002], [0.00356153]], [[0.00174719], [0.0012555]], [[0.00322708], [0.00235574]]], ], dtype=np.float32, ), ) # Test LRN default parameter values model = ov.lrn(ov.constant(input_image), ov.constant(axes)) computation = runtime.computation(model) result = computation() assert np.allclose( result, np.array( [ [[[0.0], [0.35355338]], [[0.8944272], [1.0606602]], [[1.7888544], [1.767767]]], [[[0.93704253], [0.97827977]], [[1.2493901], [1.2577883]], [[1.5617375], [1.5372968]]], ], dtype=np.float32, ), )
def test_random_uniform(): runtime = get_runtime() input_tensor = ov.constant(np.array([2, 4, 3], dtype=np.int32)) min_val = ov.constant(np.array([-2.7], dtype=np.float32)) max_val = ov.constant(np.array([3.5], dtype=np.float32)) random_uniform_node = ov.random_uniform(input_tensor, min_val, max_val, output_type="f32", global_seed=7461, op_seed=1546) computation = runtime.computation(random_uniform_node) random_uniform_results = computation() expected_results = np.array([[[2.8450181, -2.3457108, 2.2134445], [-1.0436587, 0.79548645, 1.3023183], [0.34447956, -2.0267959, 1.3989122], [0.9607613, 1.5363653, 3.117298]], [[1.570041, 2.2782724, 2.3193843], [3.3393657, 0.63299894, 0.41231918], [3.1739233, 0.03919673, -0.2136085], [-1.4519991, -2.277353, 2.630727]]], dtype=np.float32) assert np.allclose(random_uniform_results, expected_results)
def test_split(): runtime = get_runtime() input_tensor = ov.constant(np.array([0, 1, 2, 3, 4, 5], dtype=np.int32)) axis = ov.constant(0, dtype=np.int64) splits = 3 split_node = ov.split(input_tensor, axis, splits) computation = runtime.computation(split_node) split_results = computation() expected_results = np.array([[0, 1], [2, 3], [4, 5]], dtype=np.int32) assert np.allclose(split_results, expected_results)
def test_dft_3d(): runtime = get_runtime() input_data = build_fft_input_data() input_tensor = ov.constant(input_data) input_axes = ov.constant(np.array([0, 1, 2], dtype=np.int64)) dft_node = ov.dft(input_tensor, input_axes) computation = runtime.computation(dft_node) dft_results = computation() np_results = np.fft.fftn(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), axes=[0, 1, 2]).astype(np.complex64) expected_results = np.stack((np_results.real, np_results.imag), axis=-1) assert np.allclose(dft_results, expected_results, atol=0.0002)
def test_roll(): runtime = get_runtime() input = np.reshape(np.arange(10), (2, 5)) input_tensor = ov.constant(input) input_shift = ov.constant(np.array([-10, 7], dtype=np.int32)) input_axes = ov.constant(np.array([-1, 0], dtype=np.int32)) roll_node = ov.roll(input_tensor, input_shift, input_axes) computation = runtime.computation(roll_node) roll_results = computation() expected_results = np.roll(input, shift=(-10, 7), axis=(-1, 0)) assert np.allclose(roll_results, expected_results)
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_variadic_split(): runtime = get_runtime() input_tensor = ov.constant(np.array([[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]], dtype=np.int32)) axis = ov.constant(1, dtype=np.int64) splits = ov.constant(np.array([2, 4], dtype=np.int64)) v_split_node = ov.variadic_split(input_tensor, axis, splits) computation = runtime.computation(v_split_node) results = computation() split0 = np.array([[0, 1], [6, 7]], dtype=np.int32) split1 = np.array([[2, 3, 4, 5], [8, 9, 10, 11]], dtype=np.int32) assert np.allclose(results[0], split0) assert np.allclose(results[1], split1)
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 test_idft_2d(): runtime = get_runtime() expected_results = get_data() complex_input_data = np.fft.fft2(np.squeeze( expected_results.view(dtype=np.complex64), axis=-1), axes=[1, 2]).astype(np.complex64) input_data = np.stack((complex_input_data.real, complex_input_data.imag), axis=-1) input_tensor = ov.constant(input_data) input_axes = ov.constant(np.array([1, 2], dtype=np.int64)) dft_node = ov.idft(input_tensor, input_axes) computation = runtime.computation(dft_node) dft_results = computation() assert np.allclose(dft_results, expected_results, atol=0.000002)
def test_idft_2d_signal_size_1(): runtime = get_runtime() input_data = get_data() input_tensor = ov.constant(input_data) input_axes = ov.constant(np.array([0, 2], dtype=np.int64)) input_signal_size = ov.constant(np.array([4, 5], dtype=np.int64)) dft_node = ov.idft(input_tensor, input_axes, input_signal_size) computation = runtime.computation(dft_node) dft_results = computation() np_results = np.fft.ifft2(np.squeeze(input_data.view(dtype=np.complex64), axis=-1), s=[4, 5], axes=[0, 2]).astype(np.complex64) expected_results = np.stack((np_results.real, np_results.imag), axis=-1) assert np.allclose(dft_results, expected_results, atol=0.000002)
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_group_convolution_backprop_data_output_shape(): runtime = get_runtime() data_shape = [1, 1, 1, 10] filters_shape = [1, 1, 1, 1, 5] strides = [1, 1] data_node = ov.parameter(data_shape, name="Data", dtype=np.float32) filters_node = ov.parameter(filters_shape, name="Filters", dtype=np.float32) output_shape_node = ov.constant(np.array([1, 14], dtype=np.int64)) model = ov.group_convolution_backprop_data( data_node, filters_node, strides, output_shape_node, auto_pad="same_upper" ) data_value = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=np.float32).reshape( data_shape ) filters_value = np.array([1.0, 2.0, 3.0, 2.0, 1.0], dtype=np.float32).reshape(filters_shape) computation = runtime.computation(model, data_node, filters_node) result = computation(data_value, filters_value) expected = np.array( [0.0, 1.0, 4.0, 10.0, 18.0, 27.0, 36.0, 45.0, 54.0, 63.0, 62.0, 50.0, 26.0, 9.0], dtype=np.float32, ).reshape(1, 1, 1, 14) assert np.allclose(result, expected)
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_convolution_backprop_data(): runtime = get_runtime() output_spatial_shape = [9, 9] filter_shape = [1, 1, 3, 3] data_shape = [1, 1, 7, 7] strides = [1, 1] data_node = ov.parameter(shape=data_shape) filter_node = ov.parameter(shape=filter_shape) output_shape_node = ov.constant(np.array(output_spatial_shape, dtype=np.int64)) deconvolution = ov.convolution_backprop_data(data_node, filter_node, strides, output_shape_node) input_data = np.array( [ [ [ [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], [-20, -20, 20, 20, 0, 0, 0], ] ] ], dtype=np.float32, ) filter_data = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]], dtype=np.float32).reshape( 1, 1, 3, 3 ) model = runtime.computation(deconvolution, data_node, filter_node) result = model(input_data, filter_data) assert np.allclose( result, np.array( [ [ [ [-20.0, -20.0, 40.0, 40.0, -20.0, -20.0, 0.0, 0.0, 0.0], [-60.0, -60.0, 120.0, 120.0, -60.0, -60.0, 0.0, 0.0, 0.0], [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], [-80.0, -80.0, 160.0, 160.0, -80.0, -80.0, 0.0, 0.0, 0.0], [-60.0, -60.0, 120.0, 120.0, -60.0, -60.0, 0.0, 0.0, 0.0], [-20.0, -20.0, 40.0, 40.0, -20.0, -20.0, 0.0, 0.0, 0.0], ] ] ], dtype=np.float32, ), )
def test_constant_get_data_unsigned_integer(data_type): np.random.seed(133391) input_data = np.random.randn(2, 3, 4).astype(data_type) input_data = (np.iinfo(data_type).min + input_data * np.iinfo(data_type).max + input_data * np.iinfo(data_type).max) node = ops.constant(input_data, dtype=data_type) retrieved_data = node.get_data() assert np.allclose(input_data, retrieved_data)
def test_constant_get_data_signed_integer(data_type): np.random.seed(133391) input_data = np.random.randint(np.iinfo(data_type).min, np.iinfo(data_type).max, size=[2, 3, 4], dtype=data_type) node = ops.constant(input_data, dtype=data_type) retrieved_data = node.get_data() assert np.allclose(input_data, retrieved_data)
def test_constant_get_data_floating_point(data_type): np.random.seed(133391) input_data = np.random.randn(2, 3, 4).astype(data_type) min_value = -1.0e20 max_value = 1.0e20 input_data = min_value + input_data * max_value * data_type(2) node = ops.constant(input_data, dtype=data_type) retrieved_data = node.get_data() assert np.allclose(input_data, retrieved_data)
def test_constant_opset_numpy_type(): parameter_list = [] function = Model([ov.constant(np.arange(9).reshape(3, 3), np.float32)], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation()[0] expected = np.arange(9).reshape(3, 3) assert np.allclose(result, expected)
def simple_if_without_parameters(condition_val): condition = ov.constant(condition_val, dtype=np.bool) # then_body then_constant = ov.constant(0.7, dtype=np.float) then_body_res_1 = ov.result(then_constant) then_body = Model([then_body_res_1], []) # else_body else_const = ov.constant(9.0, dtype=np.float) else_body_res_1 = ov.result(else_const) else_body = Model([else_body_res_1], []) if_node = ov.if_op(condition.output(0)) if_node.set_then_body(then_body) if_node.set_else_body(else_body) 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 = 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_adaptive_avg_pool(): runtime = get_runtime() input = np.reshape([ 0.0, 4, 1, 3, -2, -5, -2, -2, 1, -3, 1, -3, -4, 0, -2, 1, -1, -2, 3, -1, -3, -1, -2, 3, 4, -3, -4, 1, 2, 0, -4, -5, -2, -2, -3, 2, 3, 1, -5, 2, -4, -2 ], (2, 3, 7)) input_tensor = ov.constant(input) output_shape = ov.constant(np.array([3], dtype=np.int32)) adaptive_pool_node = ov.adaptive_avg_pool(input_tensor, output_shape) computation = runtime.computation(adaptive_pool_node) adaptive_pool_results = computation() expected_results = np.reshape([ 1.66666663, 0.66666669, -3., -1.33333337, -1.66666663, -2.33333325, -0.66666669, 0., -0.33333334, 0., 1.33333337, -2., -0.66666669, -3.66666675, -2.33333325, 2., -0.66666669, -1.33333337 ], (2, 3, 3)) assert np.allclose(adaptive_pool_results, expected_results)
def test_adaptive_max_pool(): runtime = get_runtime() input = np.reshape([ 0, 4, 1, 3, -2, -5, -2, -2, 1, -3, 1, -3, -4, 0, -2, 1, -1, -2, 3, -1, -3, -1, -2, 3, 4, -3, -4, 1, 2, 0, -4, -5, -2, -2, -3, 2, 3, 1, -5, 2, -4, -2 ], (2, 3, 7)) input_tensor = ov.constant(input) output_shape = ov.constant(np.array([3], dtype=np.int32)) adaptive_pool_node = ov.adaptive_max_pool(input_tensor, output_shape) computation = runtime.computation(adaptive_pool_node) adaptive_pool_results = computation() expected_results = np.reshape( [4, 3, -2, 1, 1, 0, 1, 3, 3, 3, 4, 1, 2, -2, -2, 3, 2, 2], (2, 3, 3)) expected_indices = np.reshape( [1, 3, 4, 1, 3, 6, 1, 4, 4, 2, 3, 6, 0, 4, 4, 1, 4, 4], (2, 3, 3)) assert np.allclose(adaptive_pool_results, [expected_results, expected_indices])
def simple_if_without_parameters(condition_val): condition = ov.constant(condition_val, dtype=np.bool) # then_body then_constant = ov.constant(0.7, dtype=np.float) then_body_res_1 = ov.result(then_constant) then_body = GraphBody([], [then_body_res_1]) then_body_inputs = [] then_body_outputs = [TensorIteratorBodyOutputDesc(0, 0)] # else_body else_const = ov.constant(9.0, dtype=np.float) else_body_res_1 = ov.result(else_const) else_body = GraphBody([], [else_body_res_1]) else_body_inputs = [] else_body_outputs = [TensorIteratorBodyOutputDesc(0, 0)] if_node = ov.if_op(condition, [], (then_body, else_body), (then_body_inputs, else_body_inputs), (then_body_outputs, else_body_outputs)) relu = ov.relu(if_node) return relu
def test_node_output(): input_array = np.array([0, 1, 2, 3, 4, 5]) splits = 3 expected_shape = len(input_array) // splits input_tensor = ops.constant(input_array, dtype=np.int32) axis = ops.constant(0, dtype=np.int64) split_node = ops.split(input_tensor, axis, splits) split_node_outputs = split_node.outputs() assert len(split_node_outputs) == splits assert [output_node.get_index() for output_node in split_node_outputs] == [0, 1, 2] assert np.equal( [output_node.get_element_type() for output_node in split_node_outputs], input_tensor.get_element_type(), ).all() assert np.equal( [output_node.get_shape() for output_node in split_node_outputs], Shape([expected_shape]), ).all() assert np.equal( [ output_node.get_partial_shape() for output_node in split_node_outputs ], PartialShape([expected_shape]), ).all() output0 = split_node.output(0) output1 = split_node.output(1) output2 = split_node.output(2) assert [output0.get_index(), output1.get_index(), output2.get_index()] == [0, 1, 2]
def test_set_argument(): runtime = get_runtime() data1 = np.array([1, 2, 3]) data2 = np.array([4, 5, 6]) data3 = np.array([7, 8, 9]) node1 = ops.constant(data1, dtype=np.float32) node2 = ops.constant(data2, dtype=np.float32) node3 = ops.constant(data3, dtype=np.float32) node_add = ops.add(node1, node2) # Original arguments computation = runtime.computation(node_add) output = computation() assert np.allclose(data1 + data2, output) # Arguments changed by set_argument node_add.set_argument(1, node3.output(0)) output = computation() assert np.allclose(data1 + data3, output) # Arguments changed by set_argument node_add.set_argument(0, node3.output(0)) output = computation() assert np.allclose(data3 + data3, output) # Arguments changed by set_argument(OutputVector) node_add.set_arguments([node2.output(0), node3.output(0)]) output = computation() assert np.allclose(data2 + data3, output) # Arguments changed by set_arguments(NodeVector) node_add.set_arguments([node1, node2]) output = computation() assert np.allclose(data1 + data2, output)
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_node_factory_empty_topk_with_args_and_attrs(): dtype = np.int32 data = ov.parameter([2, 10], dtype=dtype, name="A") k = ov.constant(3, dtype=dtype, name="B") factory = NodeFactory("opset1") arguments = NodeFactory._arguments_as_outputs([data, k]) node = factory.create("TopK", None, None) node.set_arguments(arguments) node.set_attribute("axis", 1) node.set_attribute("mode", "max") node.set_attribute("sort", "value") node.validate() assert node.get_type_name() == "TopK" assert node.get_output_size() == 2 assert list(node.get_output_shape(0)) == [2, 3]