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 = ng.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_depth_to_space(): runtime = get_runtime() data_value = np.array( [[ [[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]], [[12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23]], ]], dtype=np.float32, ) mode = "blocks_first" block_size = np.float32(2) data_shape = [1, 4, 2, 3] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.depth_to_space(parameter_data, mode, block_size) computation = runtime.computation(model, parameter_data) result = computation(data_value) expected = np.array( [[[[0, 6, 1, 7, 2, 8], [12, 18, 13, 19, 14, 20], [3, 9, 4, 10, 5, 11], [15, 21, 16, 22, 17, 23]]]], dtype=np.float32, ) assert np.allclose(result, expected)
def import_and_compute_with_axes_as_const(op_type, data, axes, **node_attrs): data_input = np.array(data) axes_input = np.array(axes, dtype=int) axes_const_node = onnx.helper.make_node( "Constant", inputs=[], outputs=["const_axes"], value=onnx.helper.make_tensor( name="const_axes", data_type=onnx.TensorProto.INT64, dims=axes_input.shape, vals=axes_input.flatten(), ), ) node = onnx.helper.make_node(op_type, inputs=["x", "const_axes"], outputs=["y"], **node_attrs) graph = onnx.helper.make_graph( [axes_const_node, node], "test_graph", [ onnx.helper.make_tensor_value_info("x", onnx.TensorProto.FLOAT, data_input.shape) ], [onnx.helper.make_tensor_value_info("y", onnx.TensorProto.FLOAT, ())], ) model = onnx.helper.make_model(graph, producer_name="ngraph ONNX Importer") model.opset_import[0].version = 13 ng_model_function = import_onnx_model(model) runtime = get_runtime() computation = runtime.computation(ng_model_function) return computation(data_input)[0]
def test_group_convolution_operator(): runtime = get_runtime() data_shape = [1, 4, 2, 2] filters_shape = [2, 1, 2, 1, 1] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) parameter_filters = ng.parameter(filters_shape, name="Filters", dtype=np.float32) data_value = np.arange(start=1.0, stop=17.0, dtype=np.float32).reshape(data_shape) filters_value = np.arange(start=1.0, stop=5.0, dtype=np.float32).reshape(filters_shape) strides = [1, 1] dilations = [1, 1] pads_begin = [0, 0] pads_end = [0, 0] model = ng.group_convolution(parameter_data, parameter_filters, strides, pads_begin, pads_end, dilations) computation = runtime.computation(model, parameter_data, parameter_filters) result = computation(data_value, filters_value) expected = np.array([11, 14, 17, 20, 79, 86, 93, 100], dtype=np.float32).reshape(1, 2, 2, 2) assert np.allclose(result, expected)
def test_random_uniform(): runtime = get_runtime() input_tensor = ng.constant(np.array([2, 4, 3], dtype=np.int32)) min_val = ng.constant(np.array([-2.7], dtype=np.float32)) max_val = ng.constant(np.array([3.5], dtype=np.float32)) random_uniform_node = ng.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_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 = ng.parameter(data.shape, name="A", dtype=np.float32) maxpool_node = ng.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_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 = ng.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 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 = ng.constant(input) output_shape = ng.constant(np.array([3], dtype=np.int32)) adaptive_pool_node = ng.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 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 = ng.parameter(data.shape, name="A", dtype=np.float32) maxpool_node = ng.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_grn_operator(): runtime = get_runtime() data_value = np.arange(start=1.0, stop=25.0, dtype=np.float32).reshape([1, 2, 3, 4]) bias = np.float32(1e-6) data_shape = [1, 2, 3, 4] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.grn(parameter_data, bias) computation = runtime.computation(model, parameter_data) result = computation(data_value) expected = np.array( [[ [ [0.0766965, 0.14142136, 0.19611613, 0.24253564], [0.28216633, 0.31622776, 0.34570536, 0.37139067], [0.39391932, 0.41380295, 0.4314555, 0.4472136], ], [ [0.9970545, 0.98994946, 0.9805807, 0.97014254], [0.9593655, 0.9486833, 0.9383431, 0.9284767], [0.91914505, 0.9103665, 0.9021342, 0.8944272], ], ]], dtype=np.float32, ) assert np.allclose(result, expected)
def run_op_node(input_data, op_fun, *args): # type: (Union[NumericData, List[NumericData]], Callable, *Any) -> List[NumericData] """Run computation on node performing `op_fun`. `op_fun` has to accept a node as an argument. This function converts passed raw input data to nGraph Constant Node and that form is passed to `op_fun`. :param input_data: The input data for performed computation. :param op_fun: The function handler for operation we want to carry out. :param args: The arguments passed to operation we want to carry out. :return: The result from computations. """ runtime = get_runtime() comp_args = [] op_fun_args = [] comp_inputs = [] for idx, data in enumerate(input_data): node = None if np.isscalar(data): node = ng.parameter([], name=ascii_uppercase[idx], dtype=_get_numpy_dtype(data)) else: node = ng.parameter(data.shape, name=ascii_uppercase[idx], dtype=data.dtype) op_fun_args.append(node) comp_args.append(node) comp_inputs.append(data) op_fun_args.extend(args) node = op_fun(*op_fun_args) computation = runtime.computation(node, *comp_args) return computation(*comp_inputs)
def test_mvn_operator(): runtime = get_runtime() data_shape = [3, 3, 3, 1] axes = [0, 2, 3] normalize_variance = True eps = np.float32(1e-9) eps_mode = "outside_sqrt" data_value = np.array( [ [ [[0.8439683], [0.5665144], [0.05836735]], [[0.02916367], [0.12964272], [0.5060197]], [[0.79538304], [0.9411346], [0.9546573]], ], [ [[0.17730942], [0.46192095], [0.26480448]], [[0.6746842], [0.01665257], [0.62473077]], [[0.9240844], [0.9722341], [0.11965699]], ], [ [[0.41356155], [0.9129373], [0.59330076]], [[0.81929934], [0.7862604], [0.11799799]], [[0.69248444], [0.54119414], [0.07513223]], ], ], dtype=np.float32, ) parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) model = ng.mvn(parameter_data, axes, normalize_variance, eps, eps_mode) computation = runtime.computation(model, parameter_data) result = computation(data_value) expected = np.array( [ [ [[1.3546423], [0.33053496], [-1.5450814]], [[-1.2106764], [-0.8925952], [0.29888135]], [[0.38083088], [0.81808794], [0.85865635]], ], [ [[-1.1060555], [-0.05552877], [-0.78310335]], [[0.83281356], [-1.250282], [0.67467856]], [[0.7669372], [0.9113869], [-1.6463585]], ], [ [[-0.23402764], [1.6092131], [0.42940593]], [[1.2906139], [1.1860244], [-0.92945826]], [[0.0721334], [-0.38174], [-1.7799333]], ], ], dtype=np.float32, ) assert np.allclose(result, expected)
def __init__(self, ng_model_function, device="CPU"): # type: (List[Function], str) -> None super().__init__() self.device = device self.ng_model_function = ng_model_function self.runtime = get_runtime() self.computation = self.runtime.computation(ng_model_function)
def import_and_compute_matmul(input_left, input_right): input_data_left = np.array(input_left) input_data_right = np.array(input_right) onnx_model = make_onnx_model_for_matmul_op(input_data_left, input_data_right) transformer = get_runtime() ng_model_function = import_onnx_model(onnx_model) computation = transformer.computation(ng_model_function) return computation(input_data_left, input_data_right)[0]
def test_softsign_with_array(data_type): data = np.random.rand(32, 5).astype(data_type) expected = np.divide(data, np.abs(data) + 1) runtime = get_runtime() result = runtime.computation(ng.softsign(data, "SoftSign"))() assert np.allclose(result, expected, 1e-6, 1e-6)
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 = ng.parameter(shape=data_shape) filter_node = ng.parameter(shape=filter_shape) output_shape_node = ng.constant(np.array(output_spatial_shape, dtype=np.int64)) deconvolution = ng.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_softsign_with_parameters(numpy_type): data = np.random.rand(4, 2).astype(numpy_type) expected = np.divide(data, np.abs(data) + 1) runtime = get_runtime() param = ng.parameter(data.shape, numpy_type, name="Data") result = runtime.computation(ng.softsign(param, "SoftSign"), param)(data) assert np.allclose(result, expected, 1e-6, 1e-3)
def import_and_compute_conv(x, weights, transpose=False, **attributes): x, weights = np.array(x), np.array(weights) onnx_model = make_onnx_model_for_conv_op(x.shape, weights.shape, transpose=transpose, **attributes) ng_model_function = import_onnx_model(onnx_model) computation = get_runtime().computation(ng_model_function) return computation(x, weights)[0]
def test_onehot(): runtime = get_runtime() param = ng.parameter([3], dtype=np.int32) model = ng.one_hot(param, 3, 1, 0, 0) computation = runtime.computation(model, param) expected = np.eye(3)[np.array([1, 0, 2])] input_data = np.array([1, 0, 2], dtype=np.int32) result = computation(input_data) assert np.allclose(result, expected)
def run_function(function, *inputs, expected): runtime = get_runtime() computation = runtime.computation(function) actual = computation(*inputs) assert len(actual) == len(expected) for i in range(len(actual)): np.testing.assert_allclose(expected[i], actual[i], rtol=1e-3, atol=1e-6)
def test_split(): runtime = get_runtime() input_tensor = ng.constant(np.array([0, 1, 2, 3, 4, 5], dtype=np.int32)) axis = ng.constant(0, dtype=np.int64) splits = 3 split_node = ng.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_bad_data_shape(): A = ng.parameter(shape=[2, 2], name="A", dtype=np.float32) B = ng.parameter(shape=[2, 2], name="B") model = A + B runtime = get_runtime() computation = runtime.computation(model, A, B) value_a = np.array([[1, 2]], dtype=np.float32) value_b = np.array([[5, 6], [7, 8]], dtype=np.float32) with pytest.raises(UserInputError): computation(value_a, value_b)
def test_cum_sum(input_shape, cumsum_axis, reverse): input_data = np.arange(np.prod(input_shape)).reshape(input_shape) if reverse: expected = np.cumsum(input_data[::-1], axis=cumsum_axis)[::-1] else: expected = np.cumsum(input_data, axis=cumsum_axis) runtime = get_runtime() node = ng.cum_sum(input_data, cumsum_axis, reverse=reverse) computation = runtime.computation(node) result = computation() assert np.allclose(result, expected)
def test_idft_3d(): runtime = get_runtime() expected_results = get_data() complex_input_data = np.fft.fft2(np.squeeze(expected_results.view(dtype=np.complex64), axis=-1), axes=[0, 1, 2]).astype(np.complex64) input_data = np.stack((complex_input_data.real, complex_input_data.imag), axis=-1) input_tensor = ng.constant(input_data) input_axes = ng.constant(np.array([0, 1, 2], dtype=np.int64)) dft_node = ng.idft(input_tensor, input_axes) computation = runtime.computation(dft_node) dft_results = computation() assert np.allclose(dft_results, expected_results, atol=0.000003)
def test_constant(): element_type = Type.f32 parameter_list = [] function = Function( [Constant(element_type, Shape([3, 3]), list(range(9)))], 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 test_roll(): runtime = get_runtime() input = np.reshape(np.arange(10), (2, 5)) input_tensor = ng.constant(input) input_shift = ng.constant(np.array([-10, 7], dtype=np.int32)) input_axes = ng.constant(np.array([-1, 0], dtype=np.int32)) roll_node = ng.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 test_concat(): a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6]]) axis = 0 expected = np.concatenate((a, b), axis=0) runtime = get_runtime() parameter_a = ng.parameter(list(a.shape), name="A", dtype=np.float32) parameter_b = ng.parameter(list(b.shape), name="B", dtype=np.float32) node = ng.concat([parameter_a, parameter_b], axis) computation = runtime.computation(node, parameter_a, parameter_b) result = computation(a, b) assert np.allclose(result, expected)
def test_elu_operator_with_scalar_and_array(): runtime = get_runtime() data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32) alpha_value = np.float32(3) model = ng.elu(data_value, alpha_value) computation = runtime.computation(model) result = computation() expected = np.array([[-2.9797862, 1.0], [-2.5939941, 3.0]], dtype=np.float32) assert np.allclose(result, expected)
def test_squeeze_operator(): runtime = get_runtime() data_shape = [1, 2, 1, 3, 1, 1] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) data_value = np.arange(6.0, dtype=np.float32).reshape([1, 2, 1, 3, 1, 1]) axes = [2, 4] model = ng.squeeze(parameter_data, axes) computation = runtime.computation(model, parameter_data) result = computation(data_value) expected = np.arange(6.0, dtype=np.float32).reshape([1, 2, 3, 1]) assert np.allclose(result, expected)
def test_unsqueeze(): runtime = get_runtime() data_shape = [3, 4, 5] parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32) data_value = np.arange(60.0, dtype=np.float32).reshape(3, 4, 5) axes = [0, 4] model = ng.unsqueeze(parameter_data, axes) computation = runtime.computation(model, parameter_data) result = computation(data_value) expected = np.arange(60.0, dtype=np.float32).reshape([1, 3, 4, 5, 1]) assert np.allclose(result, expected)