def test_max_pool_kernel_shape3x3(): rt = get_runtime() # array([[[[ 0.5, 1.5, 2.5, 3.5], # [ 4.5, 5.5, 6.5, 7.5], # [ 8.5, 9.5, 10.5, 11.5], # [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] kernel_shape = [3, 3] rounding_type = "floor" auto_pad = None index_et = "i32" data_node = ov.parameter(data.shape, name="A", dtype=np.float32) maxpool_node = ov.max_pool( data_node, strides, dilations, pads_begin, pads_end, kernel_shape, rounding_type, auto_pad, index_et, ) comp = rt.computation(maxpool_node, data_node) result = comp(data) expected = np.array([[[[10.5, 11.5], [14.5, 15.5]]]], dtype=np.float32) assert np.allclose(result[0], expected)
def test_max_pool_non_zero_pads(): rt = get_runtime() # array([[[[ 0.5, 1.5, 2.5, 3.5], # [ 4.5, 5.5, 6.5, 7.5], # [ 8.5, 9.5, 10.5, 11.5], # [12.5, 13.5, 14.5, 15.5]]]], dtype=float32) data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4)) strides = [1, 1] dilations = [1, 1] pads_begin = [1, 1] pads_end = [1, 1] # 0 0 , 0 , 0 , 0, 0 # 0 [ 0.5, 1.5, 2.5, 3.5], 0, # 0 [ 4.5, 5.5, 6.5, 7.5], 0, # 0 [ 8.5, 9.5, 10.5, 11.5], 0, # 0 [12.5, 13.5, 14.5, 15.5], 0 # 0 0 , 0 , 0 , 0, 0 kernel_shape = [2, 2] rounding_type = "floor" auto_pad = None index_et = "i32" data_node = ov.parameter(data.shape, name="A", dtype=np.float32) maxpool_node = ov.max_pool( data_node, strides, dilations, pads_begin, pads_end, kernel_shape, rounding_type, auto_pad, index_et, ) comp = rt.computation(maxpool_node, data_node) result = comp(data) expected = np.array( [[[ [0.5, 1.5, 2.5, 3.5, 3.5], [4.5, 5.5, 6.5, 7.5, 7.5], [8.5, 9.5, 10.5, 11.5, 11.5], [12.5, 13.5, 14.5, 15.5, 15.5], [12.5, 13.5, 14.5, 15.5, 15.5], ]]], dtype=np.float32, ) expected_idx = np.array( [[[ [0, 1, 2, 3, 3], [4, 5, 6, 7, 7], [8, 9, 10, 11, 11], [12, 13, 14, 15, 15], [12, 13, 14, 15, 15], ]]], dtype=np.int32, ) assert np.allclose(result[0], expected) assert np.allclose(result[1], expected_idx)
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)