def test_strided_slice(): input_tensor = np.arange(2 * 3 * 4, dtype=np.float32).reshape((2, 3, 4)) begin = np.array([1, 0], dtype=np.int32) end = np.array([0, 0], dtype=np.int32) strides = np.array([1, 1], dtype=np.int32) begin_mask = np.array([0, 0, 0], dtype=np.int32) end_mask = np.array([0, 0, 0], dtype=np.int32) new_axis_mask = np.array([0, 1, 0], dtype=np.int32) shrink_axis_mask = np.array([1, 0, 0], dtype=np.int32) ellipsis_mask = np.array([0, 0, 0], dtype=np.int32) result = run_op_node( [input_tensor], ov.strided_slice, begin, end, strides, begin_mask, end_mask, new_axis_mask, shrink_axis_mask, ellipsis_mask, ) expected = np.array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], dtype=np.float32).reshape((1, 3, 4)) assert np.allclose(result, expected)
def test_convert_to_uint(destination_type, expected_type): np.random.seed(133391) input_data = np.ceil(np.random.rand(2, 3, 4) * 16).astype(np.float32) expected = np.array(input_data, dtype=expected_type) result = run_op_node([input_data], ng.convert, destination_type) assert np.allclose(result, expected) assert np.array(result).dtype == expected_type
def test_broadcast_1(): input_data = np.array([1, 2, 3]) new_shape = [3, 3] expected = [[1, 2, 3], [1, 2, 3], [1, 2, 3]] result = run_op_node([input_data], ng.broadcast, new_shape) assert np.allclose(result, expected)
def test_convert_to_float(destination_type, rand_range, in_dtype, expected_type): np.random.seed(133391) input_data = np.random.randint(*rand_range, size=(2, 2), dtype=in_dtype) expected = np.array(input_data, dtype=expected_type) result = run_op_node([input_data], ng.convert, destination_type) assert np.allclose(result, expected) assert np.array(result).dtype == expected_type
def test_range(): start = 5 stop = 35 step = 5 result = run_op_node([start, stop, step], ng.range) assert np.allclose(result, [5, 10, 15, 20, 25, 30])
def test_unary_op_array(ng_api_fn, numpy_fn, range_start, range_end): np.random.seed(133391) input_data = (range_start + np.random.rand(2, 3, 4) * (range_end - range_start)).astype(np.float32) expected = numpy_fn(input_data) result = run_op_node([input_data], ng_api_fn) assert np.allclose(result, expected, rtol=0.001)
def test_shape_of(): input_tensor = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32) result = run_op_node([input_tensor], ov.shape_of) assert np.allclose(result, [3, 3])
def test_unary_op_scalar(ng_api_fn, numpy_fn, input_data): expected = numpy_fn(input_data) result = run_op_node([input_data], ng_api_fn) assert np.allclose(result, expected) result = run_op_numeric_data(input_data, ng_api_fn) assert np.allclose(result, expected)
def test_broadcast_3(): input_data = np.array([1, 2, 3], dtype=np.int32) new_shape = [3, 3] axis_mapping = [0] expected = [[1, 1, 1], [2, 2, 2], [3, 3, 3]] result = run_op_node([input_data], ops.broadcast, new_shape, axis_mapping, "EXPLICIT") assert np.allclose(result, expected)
def test_reduce_mean_op(ng_api_helper, numpy_function, reduction_axes): shape = [2, 4, 3, 2] np.random.seed(133391) input_data = np.random.randn(*shape).astype(np.float32) expected = numpy_function(input_data, axis=tuple(reduction_axes)) result = run_op_node([input_data], ng_api_helper, reduction_axes) assert np.allclose(result, expected)
def test_power_v1(): A = np.arange(48, dtype=np.float32).reshape((8, 1, 6, 1)) B = np.arange(20, dtype=np.float32).reshape((4, 1, 5)) expected = np.power(A, B) result = run_op_node([A, B], ov.power) assert np.allclose(result, expected)
def test_select(): cond = [[False, False], [True, False], [True, True]] then_node = [[-1, 0], [1, 2], [3, 4]] else_node = [[11, 10], [9, 8], [7, 6]] excepted = [[11, 10], [1, 8], [3, 4]] result = run_op_node([cond, then_node, else_node], ng.select) assert np.allclose(result, excepted)
def test_multiply(): A = np.arange(48, dtype=np.int32).reshape((8, 1, 6, 1)) B = np.arange(35, dtype=np.int32).reshape((7, 1, 5)) expected = np.multiply(A, B) result = run_op_node([A, B], ov.multiply) assert np.allclose(result, expected)
def test_select(): cond = np.array([[False, False], [True, False], [True, True]]) then_node = np.array([[-1, 0], [1, 2], [3, 4]], dtype=np.int32) else_node = np.array([[11, 10], [9, 8], [7, 6]], dtype=np.int32) excepted = np.array([[11, 10], [1, 8], [3, 4]], dtype=np.int32) result = run_op_node([cond, then_node, else_node], ng.select) assert np.allclose(result, excepted)
def test_logical_not(input_data): expected = np.logical_not(input_data) result = run_op_node([input_data], ng.logical_not) assert np.allclose(result, expected) result = run_op_numeric_data(input_data, ng.logical_not) assert np.allclose(result, expected)
def test_transpose(): input_tensor = np.arange(3 * 3 * 224 * 224).reshape((3, 3, 224, 224)) input_order = np.array([0, 2, 3, 1]) result = run_op_node([input_tensor, input_order], ng.transpose) expected = np.transpose(input_tensor, input_order) assert np.allclose(result, expected)
def test_tile(): input_tensor = np.arange(6, dtype=np.int32).reshape((2, 1, 3)) repeats = np.array([2, 1], dtype=np.int32) result = run_op_node([input_tensor], ng.tile, repeats) expected = np.array([0, 1, 2, 0, 1, 2, 3, 4, 5, 3, 4, 5]).reshape((2, 2, 3)) assert np.allclose(result, expected)
def test_softmax(): axis = 1 input_tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) result = run_op_node([input_tensor], ov.softmax, axis) expected = [[0.09003056, 0.24472842, 0.6652409], [0.09003056, 0.24472842, 0.6652409]] assert np.allclose(result, expected)
def test_softmax(): axis = 0 input_tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) result = run_op_node([input_tensor], ng.softmax, axis) expected = [[0.00426978, 0.01160646, 0.03154963], [0.08576079, 0.23312202, 0.6336913]] assert np.allclose(result, expected)
def test_erf(): input_tensor = np.array([-1.0, 0.0, 1.0, 2.5, 3.14, 4.0], dtype=np.float32) expected = [-0.842701, 0.0, 0.842701, 0.999593, 0.999991, 1.0] result = run_op_node([input_tensor], ng.erf) assert np.allclose(result, expected) result = run_op_numeric_data(input_tensor, ng.erf) assert np.allclose(result, expected)
def test_reshape_v1(): A = np.arange(1200, dtype=np.float32).reshape((2, 5, 5, 24)) shape = np.array([0, -1, 4], dtype=np.int32) special_zero = True expected_shape = np.array([2, 150, 4]) expected = np.reshape(A, expected_shape) result = run_op_node([A], ng.reshape, shape, special_zero) assert np.allclose(result, expected)
def test_one_hot(): data = np.array([0, 1, 2], dtype=np.int32) depth = 2 on_value = 5 off_value = 10 axis = -1 excepted = [[5, 10], [10, 5], [10, 10]] result = run_op_node([data, depth, on_value, off_value], ov.one_hot, axis) assert np.allclose(result, excepted)
def test_sigmoid(): input_data = np.array([-3.14, -1.0, 0.0, 2.71001, 1000.0], dtype=np.float32) result = run_op_node([input_data], ng.sigmoid) def sigmoid(x): return 1.0 / (1.0 + np.exp(-x)) expected = np.array(list(map(sigmoid, input_data))) assert np.allclose(result, expected)
def test_gather(): input_data = np.array([1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2], np.float32).reshape((3, 3)) input_indices = np.array([0, 2], np.int32).reshape(1, 2) input_axes = np.array([1], np.int32) expected = np.array([1.0, 1.2, 2.0, 2.2, 3.0, 3.2], dtype=np.float32).reshape((3, 1, 2)) result = run_op_node([input_data], ng.gather, input_indices, input_axes) assert np.allclose(result, expected)
def test_batch_norm_inference(): data = [[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]] gamma = [2.0, 3.0, 4.0] beta = [0.0, 0.0, 0.0] mean = [0.0, 0.0, 0.0] variance = [1.0, 1.0, 1.0] epsilon = 9.99e-06 excepted = [[2.0, 6.0, 12.0], [-2.0, -6.0, -12.0]] result = run_op_node([data, gamma, beta, mean, variance], ng.batch_norm_inference, epsilon) assert np.allclose(result, excepted)
def test_batch_norm_inference(): data = np.array([[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]], dtype=np.float32) gamma = np.array([2.0, 3.0, 4.0], dtype=np.float32) beta = np.array([0.0, 0.0, 0.0], dtype=np.float32) mean = np.array([0.0, 0.0, 0.0], dtype=np.float32) variance = np.array([1.0, 1.0, 1.0], dtype=np.float32) epsilon = 9.99e-06 excepted = np.array([[2.0, 6.0, 12.0], [-2.0, -6.0, -12.0]], dtype=np.float32) result = run_op_node([data, gamma, beta, mean, variance], ov.batch_norm_inference, epsilon) assert np.allclose(result, excepted)
def test_gather_batch_dims_1_negative_indices(): input_data = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], np.float32) input_indices = np.array([[0, 1, -2], [-2, 0, 0]], np.int32) input_axis = np.array([1], np.int32) batch_dims = 1 expected = np.array([[1, 2, 4], [9, 6, 6]], np.float32) result = run_op_node([input_data], ov.gather, input_indices, input_axis, batch_dims) assert np.allclose(result, expected)
def test_matmul(shape_a, shape_b, transpose_a, transpose_b): np.random.seed(133391) left_input = -100.0 + np.random.rand(*shape_a).astype(np.float32) * 200.0 right_input = -100.0 + np.random.rand(*shape_b).astype(np.float32) * 200.0 result = run_op_node([left_input, right_input], ov.matmul, transpose_a, transpose_b) if transpose_a: left_input = np.transpose(left_input) if transpose_b: right_input = np.transpose(right_input) expected = np.matmul(left_input, right_input) assert np.allclose(result, expected)
def test_round_away(): float_dtype = np.float32 data = ng.parameter(Shape([3, 10]), dtype=float_dtype, name="data") node = ng.round(data, "HALF_AWAY_FROM_ZERO") assert node.get_type_name() == "Round" assert node.get_output_size() == 1 assert list(node.get_output_shape(0)) == [3, 10] assert node.get_output_element_type(0) == Type.f32 input_tensor = np.array([-2.5, -1.5, -0.5, 0.5, 0.9, 1.5, 2.3, 2.5, 3.5], dtype=np.float32) expected = [-3.0, -2.0, -1.0, 1.0, 1.0, 2.0, 2.0, 3.0, 4.0] result = run_op_node([input_tensor], ng.round, "HALF_AWAY_FROM_ZERO") assert np.allclose(result, expected)
def test_mvn_no_variance(): data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.float32).reshape([1, 3, 3, 3]) axes = np.array([2, 3], dtype=np.int64) epsilon = 1e-9 normalize_variance = False eps_mode = "outside_sqrt" excepted = np.array([-4, -3, -2, -1, 0, 1, 2, 3, 4, -4, -3, -2, -1, 0, 1, 2, 3, 4, -4, -3, -2, -1, 0, 1, 2, 3, 4], dtype=np.float32).reshape([1, 3, 3, 3]) result = run_op_node([data], ov.mvn, axes, normalize_variance, epsilon, eps_mode) assert np.allclose(result, excepted)