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 = Function([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 = Function([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 binary_op_comparison(op_str): element_type = Type.f32 shape = Shape([2, 2]) A = Parameter(element_type, shape) B = Parameter(element_type, shape) parameter_list = [A, B] function = Function([binary_op(op_str, A, B)], parameter_list, "test") a_arr = np.array([[1, 5], [3, 2]], dtype=np.float32) b_arr = np.array([[2, 4], [3, 1]], dtype=np.float32) runtime = get_runtime() computation = runtime.computation(function, A, B) result = computation(a_arr, b_arr)[0] expected = binary_op_ref(op_str, a_arr, b_arr) assert np.allclose(result, expected)
def test_create_IENetwork_from_nGraph(): element_type = Type.f32 param = Parameter(element_type, Shape([1, 3, 22, 22])) relu = ov.relu(param) func = Function([relu], [param], "test") cnnNetwork = IENetwork(func) assert cnnNetwork is not None func2 = cnnNetwork.get_function() assert func2 is not None assert len(func2.get_ops()) == 3
def test_select(): element_type = Type.f32 A = Parameter(Type.boolean, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) parameter_list = [A, B, C] function = Function([ov.select(A, B, C)], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation( np.array([[True, False]], dtype=np.bool), np.array([[5, 6]], dtype=np.float32), np.array([[7, 8]], dtype=np.float32), )[0] expected = np.array([[5, 8]]) assert np.allclose(result, expected)
def test_runtime_info(): test_shape = PartialShape([1, 1, 1, 1]) test_type = Type.f32 test_param = Parameter(test_type, test_shape) relu_node = ops.relu(test_param) runtime_info = relu_node.get_rt_info() runtime_info["affinity"] = "test_affinity" relu_node.set_friendly_name("testReLU") runtime_info_after = relu_node.get_rt_info() assert runtime_info_after["affinity"] == "test_affinity"
def test_concat(): element_type = Type.f32 A = Parameter(element_type, Shape([1, 2])) B = Parameter(element_type, Shape([1, 2])) C = Parameter(element_type, Shape([1, 2])) parameter_list = [A, B, C] axis = 0 function = Function([ov.concat([A, B, C], axis)], parameter_list, "test") a_arr = np.array([[1, 2]], dtype=np.float32) b_arr = np.array([[5, 6]], dtype=np.float32) c_arr = np.array([[7, 8]], dtype=np.float32) runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(a_arr, b_arr, c_arr)[0] expected = np.concatenate((a_arr, b_arr, c_arr), axis) assert np.allclose(result, expected)
def test_reshape(): element_type = Type.f32 shape = Shape([2, 3]) A = Parameter(element_type, shape) parameter_list = [A] function = Function([ov.reshape(A, Shape([3, 2]), special_zero=False)], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(np.array(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), dtype=np.float32))[0] expected = np.reshape(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), (3, 2)) assert np.allclose(result, expected)
def test_broadcast(): element_type = Type.f32 A = Parameter(element_type, Shape([3])) parameter_list = [A] function = Function([ov.broadcast(A, [3, 3])], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(np.array([1, 2, 3], dtype=np.float32))[0] a_arr = np.array([[0], [0], [0]], dtype=np.float32) b_arr = np.array([[1, 2, 3]], dtype=np.float32) expected = np.add(a_arr, b_arr) assert np.allclose(result, expected)
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 = Function([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 unary_op_exec(op_str, input_list): """ input_list needs to have deep length of 4 """ element_type = Type.f32 shape = Shape(np.array(input_list).shape) A = Parameter(element_type, shape) parameter_list = [A] function = Function([unary_op(op_str, A)], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(np.array(input_list, dtype=np.float32))[0] expected = unary_op_ref(op_str, np.array(input_list, dtype=np.float32)) assert np.allclose(result, expected)
def test_max_pool(): # test 1d element_type = Type.f32 shape = Shape([1, 1, 10]) A = Parameter(element_type, shape) parameter_list = [A] input_arr = np.arange(10, dtype=np.float32).reshape([1, 1, 10]) window_shape = [3] strides = [1] * len(window_shape) dilations = [1] * len(window_shape) pads_begin = [0] * len(window_shape) pads_end = [0] * len(window_shape) rounding_type = "floor" auto_pad = "explicit" idx_elem_type = "i32" model = ov.max_pool( A, strides, dilations, pads_begin, pads_end, window_shape, rounding_type, auto_pad, idx_elem_type, ) function = Function([model], parameter_list, "test") runtime = get_runtime() computation = runtime.computation(function, *parameter_list) result = computation(input_arr)[0] expected = (np.arange(8) + 2).reshape(1, 1, 8) assert np.allclose(result, expected) # test 1d with strides strides = [2] pads_begin = [0] * len(window_shape) pads_end = [0] * len(window_shape) model = ov.max_pool( A, strides, dilations, pads_begin, pads_end, window_shape, rounding_type, auto_pad, idx_elem_type, ) function = Function([model], parameter_list, "test") size = 4 computation = runtime.computation(function, *parameter_list) result = computation(input_arr)[0] expected = ((np.arange(size) + 1) * 2).reshape(1, 1, size) assert np.allclose(result, expected) # test 2d element_type = Type.f32 shape = Shape([1, 1, 10, 10]) A = Parameter(element_type, shape) parameter_list = [A] input_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10) window_shape = [3, 3] strides = [1, 1] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] model = ov.max_pool( A, strides, dilations, pads_begin, pads_end, window_shape, rounding_type, auto_pad, idx_elem_type, ) function = Function([model], parameter_list, "test") computation = runtime.computation(function, *parameter_list) result = computation(input_arr)[0] expected = ((np.arange(100).reshape(10, 10))[2:, 2:]).reshape(1, 1, 8, 8) assert np.allclose(result, expected) # test 2d with strides strides = [2, 2] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] model = ov.max_pool( A, strides, dilations, pads_begin, pads_end, window_shape, rounding_type, auto_pad, idx_elem_type, ) function = Function([model], parameter_list, "test") computation = runtime.computation(function, *parameter_list) result = computation(input_arr)[0] size = 4 expected = ((np.arange(100).reshape(10, 10))[2::2, 2::2]).reshape(1, 1, size, size) assert np.allclose(result, expected)