def test_depth_to_space(): b, c, h, w = shape = (2, 8, 3, 3) blocksize = 2 data = np.random.random_sample(shape).astype(np.float32) tmp = np.reshape(data, [b, blocksize, blocksize, c // (blocksize**2), h, w]) tmp = np.transpose(tmp, [0, 3, 4, 1, 5, 2]) expected_output = np.reshape( tmp, [b, c // (blocksize**2), h * blocksize, w * blocksize]) node = onnx.helper.make_node('DepthToSpace', inputs=['x'], outputs=['y'], blocksize=blocksize) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output]) # (1, 4, 2, 3) input tensor data = 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]]]]).astype(np.float32) # (1, 1, 4, 6) output tensor expected_output = 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]]]]).astype(np.float32) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output])
def test_logsoftmax(): def logsoftmax_2d(x): max_x = np.max(x, axis=1).reshape((-1, 1)) exp_x = np.exp(x - max_x) return x - max_x - np.log(np.sum(exp_x, axis=1).reshape((-1, 1))) np.random.seed(133391) data = np.random.randn(3, 4, 5).astype(np.float32) node = onnx.helper.make_node('LogSoftmax', inputs=['x'], outputs=['y'], axis=0) expected = logsoftmax_2d(data.reshape(1, 60)).reshape(3, 4, 5) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) node = onnx.helper.make_node('LogSoftmax', inputs=['x'], outputs=['y'], axis=1) expected = logsoftmax_2d(data.reshape(3, 20)).reshape(3, 4, 5) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) # default axis is 1 node = onnx.helper.make_node('LogSoftmax', inputs=['x'], outputs=['y']) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) node = onnx.helper.make_node('LogSoftmax', inputs=['x'], outputs=['y'], axis=2) expected = logsoftmax_2d(data.reshape(12, 5)).reshape(3, 4, 5) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) with pytest.raises(RuntimeError): node = onnx.helper.make_node('LogSoftmax', inputs=['x'], outputs=['y'], axis=3) ng_results = run_node(node, [data])
def test_split_1d(): # 1D data = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32) node = onnx.helper.make_node('Split', inputs=['input'], outputs=['z', 'w'], axis=0) expected_outputs = [np.array([1., 2., 3.]).astype(np.float32), np.array([4., 5., 6.]).astype(np.float32)] ng_results = run_node(node, [data]) assert all_arrays_equal(ng_results, expected_outputs) node = onnx.helper.make_node('Split', inputs=['input'], outputs=['y', 'z', 'w'], axis=0, split=[2, 3, 1]) expected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5.]).astype(np.float32), np.array([6.]).astype(np.float32)] ng_results = run_node(node, [data]) assert all_arrays_equal(ng_results, expected_outputs) # Default values data = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32) node = onnx.helper.make_node('Split', inputs=['input'], outputs=['y', 'z', 'w']) expected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)] ng_results = run_node(node, [data]) assert all_arrays_equal(ng_results, expected_outputs) node = onnx.helper.make_node('Split', inputs=['input'], outputs=['y', 'z'], split=[2, 4]) expected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)] ng_results = run_node(node, [data]) assert all_arrays_equal(ng_results, expected_outputs)
def test_reduce_sum_square(reduction_axes): shape = [2, 4, 3, 2] np.random.seed(133391) input_data = np.random.uniform(-100, 100, shape).astype(np.float32) expected = np.sum(np.square(input_data), keepdims=True, axis=reduction_axes) node = onnx.helper.make_node('ReduceSumSquare', inputs=['x'], outputs=['y'], axes=reduction_axes) ng_result = np.array(run_node(node, [input_data]).pop()) assert np.array_equal(expected.shape, ng_result.shape) assert np.allclose(expected, ng_result) expected = np.sum(np.square(input_data), keepdims=False, axis=reduction_axes) node = onnx.helper.make_node('ReduceSumSquare', inputs=['x'], outputs=['y'], keepdims=0, axes=reduction_axes) ng_result = np.array(run_node(node, [input_data]).pop()) assert np.array_equal(expected.shape, ng_result.shape) assert np.allclose(expected, ng_result)
def test_hardmax_special_cases(): def hardmax_2d(data): return np.eye(data.shape[1], dtype=data.dtype)[np.argmax(data, axis=1)] np.random.seed(133391) data = np.random.rand(3, 4, 5).astype(np.float32) # default axis=1 expected = hardmax_2d(data.reshape(3, 20)).reshape(3, 4, 5) node = onnx.helper.make_node('Hardmax', inputs=['x'], outputs=['y']) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) with pytest.raises(RuntimeError): node = onnx.helper.make_node('Hardmax', inputs=['x'], outputs=['y'], axis=-1) ng_results = run_node(node, [data]) with pytest.raises(RuntimeError): node = onnx.helper.make_node('Hardmax', inputs=['x'], outputs=['y'], axis=3) ng_results = run_node(node, [data]) # For multiple occurrences of the maximal values, the first occurrence is selected # for one-hot output data = np.array([[3, 3, 3, 1]]).astype(np.float32) expected = np.array([[1, 0, 0, 0]]).astype(np.float32) node = onnx.helper.make_node('Hardmax', inputs=['x'], outputs=['y']) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected])
def test_unsqueeze(): data = np.random.randn(3, 4, 5).astype(np.float32) expected_output = np.expand_dims(data, axis=0) node = onnx.helper.make_node('Unsqueeze', inputs=['x'], outputs=['y'], axes=[0]) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output]) expected_output = np.reshape(data, [1, 3, 4, 5, 1]) node = onnx.helper.make_node('Unsqueeze', inputs=['x'], outputs=['y'], axes=[0, 4]) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output]) expected_output = np.reshape(data, [1, 3, 1, 4, 5]) node = onnx.helper.make_node('Unsqueeze', inputs=['x'], outputs=['y'], axes=[0, 2]) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output])
def test_reshape_opset5_param_err(): original_shape = [2, 3, 4] output_shape = np.array([4, 2, 3], dtype=np.int64) input_data = np.random.random_sample(original_shape).astype(np.float32) reshape_node = onnx.helper.make_node('Reshape', inputs=['x', 'y'], outputs=['z']) # if reshape input is an instance of `Parameter` class then it should raise error with pytest.raises(NotImplementedError): run_node(reshape_node, [input_data, output_shape], opset_version=5)
def test_flatten_exception(): data = np.arange(120).reshape(2, 3, 4, 5) node = onnx.helper.make_node('Flatten', inputs=['x'], outputs=['y'], axis=5) with pytest.raises(ValueError): run_node(node, [data])
def test_transpose(): data = np.arange(120).reshape(2, 3, 4, 5) node = onnx.helper.make_node('Transpose', inputs=['x'], outputs=['y']) expected_output = data.T ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output]) node = onnx.helper.make_node('Transpose', inputs=['x'], outputs=['y'], perm=(3, 1, 0, 2)) expected_output = np.transpose(data, axes=(3, 1, 0, 2)) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output])
def test_squeeze(): data = np.arange(6).reshape(1, 2, 3, 1) expected_output = data.reshape(2, 3) node = onnx.helper.make_node('Squeeze', inputs=['x'], outputs=['y'], axes=[0, 3]) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output]) data = np.random.randn(1, 3, 4, 5).astype(np.float32) expected_output = np.squeeze(data, axis=0) node = onnx.helper.make_node('Squeeze', inputs=['x'], outputs=['y'], axes=[0]) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output])
def test_clip_default(): np.random.seed(133391) data = -100. + np.random.randn(3, 4, 5).astype(np.float32) * 200.0 node = onnx.helper.make_node('Clip', inputs=['x'], outputs=['y'], min=0.) expected = np.clip(data, np.float32(0.), np.finfo(np.float32).max) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) node = onnx.helper.make_node('Clip', inputs=['x'], outputs=['y'], max=0.) expected = np.clip(data, np.finfo(np.float32).min, np.float32(0.)) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected])
def test_batch_norm_test_node(): data = np.arange(48).reshape((1, 3, 4, 4)).astype(np.float32) scale = np.ones((3,)).astype(np.float32) # Gamma bias = np.zeros((3,)).astype(np.float32) # Beta mean = np.mean(data, axis=(0, 2, 3)) var = np.var(data, axis=(0, 2, 3)) expected_output = np.array( [[[[-1.62694025, -1.41001487, -1.19308949, -0.97616416], [-0.75923878, -0.54231346, -0.32538807, -0.10846269], [0.10846269, 0.32538807, 0.54231334, 0.75923872], [0.9761641, 1.19308949, 1.41001487, 1.62694025]], [[-1.62694049, -1.41001511, -1.19308972, -0.97616434], [-0.7592392, -0.54231358, -0.32538843, -0.10846281], [0.10846233, 0.32538795, 0.5423131, 0.75923872], [0.97616386, 1.19308949, 1.41001463, 1.62694025]], [[-1.62694025, -1.41001511, -1.19308949, -0.97616434], [-0.75923872, -0.54231358, -0.32538795, -0.10846233], [0.10846233, 0.32538795, 0.54231358, 0.7592392], [0.97616386, 1.19308949, 1.41001511, 1.62694073]]]], dtype=np.float32) node = make_batch_norm_node() result = run_node(node, [data, scale, bias, mean, var])[0] assert np.allclose(result, expected_output, rtol=1e-04, atol=1e-08) scale = np.broadcast_to(0.1, (3,)).astype(np.float32) # Gamma bias = np.broadcast_to(1, (3,)).astype(np.float32) # Beta expected_output = np.array( [[[[0.83730596, 0.85899848, 0.88069105, 0.90238357], [0.92407608, 0.94576865, 0.96746117, 0.98915374], [1.01084626, 1.03253877, 1.05423129, 1.07592392], [1.09761643, 1.11930895, 1.14100146, 1.16269398]], [[0.83730596, 0.85899854, 0.88069105, 0.90238357], [0.92407608, 0.94576865, 0.96746117, 0.98915374], [1.01084626, 1.03253877, 1.05423141, 1.07592392], [1.09761643, 1.11930895, 1.14100146, 1.16269398]], [[0.83730596, 0.85899848, 0.88069105, 0.90238357], [0.92407614, 0.94576865, 0.96746117, 0.98915374], [1.01084626, 1.03253877, 1.05423141, 1.07592392], [1.09761643, 1.11930895, 1.14100146, 1.16269398]]]], dtype=np.float32) node = make_batch_norm_node() result = run_node(node, [data, scale, bias, mean, var])[0] assert np.allclose(result, expected_output, rtol=1e-04, atol=1e-08)
def test_reshape_opset5_param_err(): original_shape = [2, 3, 4] output_shape = np.array([4, 2, 3], dtype=np.int64) input_data = np.random.random_sample(original_shape).astype(np.float32) reshape_node = onnx.helper.make_node('Reshape', inputs=['x', 'y'], outputs=['z']) ng_result = run_node(reshape_node, [input_data, output_shape], opset_version=5) assert ng_result[0].shape == output_shape
def test_reshape(): input_data = np.arange(2560).reshape(16, 4, 4, 10) reshape_node = onnx.helper.make_node('Reshape', inputs=['x'], outputs=['y'], shape=(256, 10)) expected_output = input_data.reshape(256, 10) ng_results = run_node(reshape_node, [input_data], opset_version=4) assert np.array_equal(ng_results, [expected_output])
def test_pool_global_average_3d(ndarray_1x1x4x4): x = np.broadcast_to(ndarray_1x1x4x4, (1, 1, 4, 4, 4)) node = onnx.helper.make_node('GlobalAveragePool', inputs=['x'], outputs=['y']) y = np.array([18.5], dtype=np.float32).reshape(1, 1, 1, 1, 1) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y])
def test_pool_average(ndarray_1x1x4x4): x = ndarray_1x1x4x4 node = onnx.helper.make_node('AveragePool', inputs=['x'], outputs=['y'], kernel_shape=(2, 2), strides=(2, 2)) y = np.array([[13.5, 15.5], [21.5, 23.5]], dtype=np.float32).reshape(1, 1, 2, 2) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y]) node = onnx.helper.make_node('AveragePool', inputs=['x'], outputs=['y'], kernel_shape=(2, 2), strides=(2, 2), pads=(1, 1, 1, 1)) y = np.array([[11, 12.5, 14], [17, 18.5, 20], [23, 24.5, 26]], dtype=np.float32).reshape(1, 1, 3, 3) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y])
def test_logical_not(): input_data = np.array([[0, 1, -1], [0, 1, -1], [0, 1, -1]]) expected_output = np.logical_not(input_data) node = onnx.helper.make_node('Not', inputs=['X'], outputs=['Y']) ng_results = run_node(node, [input_data]) assert np.array_equal(ng_results, [expected_output])
def test_mean(): data = [np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])] node = onnx.helper.make_node('Mean', inputs=['data_0', 'data_1', 'data_2'], outputs=['y']) expected_output = reduce(np.add, data) / len(data) ng_results = run_node(node, data) assert np.array_equal(ng_results, [expected_output])
def test_reduce_log_sum_default_axes(): shape = [2, 4, 3, 2] np.random.seed(133391) input_data = np.random.uniform(0, 1, shape).astype(np.float32) expected = np.log(np.sum(input_data, keepdims=True)) node = onnx.helper.make_node('ReduceLogSum', inputs=['x'], outputs=['y']) ng_result = np.array(run_node(node, [input_data]).pop()) assert np.array_equal(expected.shape, ng_result.shape) assert np.allclose(expected, ng_result) expected = np.log(np.sum(input_data, keepdims=False)) node = onnx.helper.make_node('ReduceLogSum', inputs=['x'], outputs=['y'], keepdims=0) ng_result = np.array(run_node(node, [input_data]).pop()) assert np.array_equal(expected.shape, ng_result.shape) assert np.allclose(expected, ng_result)
def test_variadic(onnx_op, numpy_func): data = [np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])] node = onnx.helper.make_node(onnx_op, inputs=['data_0', 'data_1', 'data_2'], outputs=['y']) expected_output = reduce(numpy_func, data) ng_results = run_node(node, data) assert np.array_equal(ng_results, [expected_output])
def test_identity(): np.random.seed(133391) shape = [2, 4] input_data = np.random.randn(*shape).astype(np.float32) identity_node = make_node('Identity', inputs=['x'], outputs=['y']) ng_results = run_node(identity_node, [input_data]) assert np.array_equal(ng_results, [input_data]) node1 = make_node('Add', inputs=['A', 'B'], outputs=['add1'], name='add_node1') node2 = make_node('Identity', inputs=['add1'], outputs=['identity1'], name='identity_node1') node3 = make_node('Abs', inputs=['identity1'], outputs=['Y'], name='abs_node1') graph = make_graph([node1, node2, node3], 'test_graph', [ make_tensor_value_info('A', onnx.TensorProto.FLOAT, shape), make_tensor_value_info('B', onnx.TensorProto.FLOAT, shape) ], [make_tensor_value_info('Y', onnx.TensorProto.FLOAT, shape)]) model = make_model(graph, producer_name='ngraph ONNX Importer') ng_model_function = import_onnx_model(model) runtime = get_runtime() computation = runtime.computation(ng_model_function) ng_results = computation(input_data, input_data) expected_result = np.abs(input_data + input_data) assert np.array_equal(ng_results[0], expected_result)
def import_and_compute(op_type, input_data, **node_attrs): data_inputs = [np.array(input_data)] node = onnx.helper.make_node(op_type, inputs=['x'], outputs=['y'], **node_attrs) return run_node(node, data_inputs).pop()
def test_concat(): a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6]]) node = onnx.helper.make_node('Concat', inputs=['x'], outputs=['z'], axis=0) ng_results = run_node(node, [a]) assert np.array_equal(ng_results, [a]) expected_output = np.concatenate((a, b), axis=0) node = onnx.helper.make_node('Concat', inputs=['x', 'y'], outputs=['z'], axis=0) ng_results = run_node(node, [a, b]) assert np.array_equal(ng_results, [expected_output]) a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6]]).T expected_output = np.concatenate((a, b), axis=1) node = onnx.helper.make_node('Concat', inputs=['x', 'y'], outputs=['z'], axis=1) ng_results = run_node(node, [a, b]) assert np.array_equal(ng_results, [expected_output]) test_cases = { '1d': ([1, 2], [3, 4]), '2d': ([[1, 2], [3, 4]], [[5, 6], [7, 8]]), '3d': ([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], [[[9, 10], [11, 12]], [[13, 14], [15, 16]]]), } for test_case, values in test_cases.items(): values = [np.asarray(v) for v in values] for i in range(len(values[0].shape)): in_args = ['value' + str(k) for k in range(len(values))] node = onnx.helper.make_node( 'Concat', inputs=[s for s in in_args], outputs=['output'], axis=i, ) expected_output = np.concatenate(values, i) ng_results = run_node(node, [v for v in values]) assert np.array_equal(ng_results, [expected_output])
def test_pool_global_max(ndarray_1x1x4x4): node = onnx.helper.make_node('GlobalMaxPool', inputs=['x'], outputs=['y']) x = ndarray_1x1x4x4 y = np.array([26], dtype=np.float32).reshape(1, 1, 1, 1) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y])
def test_logical(onnx_op, numpy_func, data_type): node = onnx.helper.make_node(onnx_op, inputs=['A', 'B'], outputs=['C'], broadcast=1) input_a = np.array([[0, 1, -1], [0, 1, -1], [0, 1, -1]]).astype(data_type) input_b = np.array([[0, 0, 0], [1, 1, 1], [-1, -1, -1]]).astype(data_type) expected_output = numpy_func(input_a, input_b) ng_results = run_node(node, [input_a, input_b], opset_version=4) assert np.array_equal(ng_results, [expected_output]) input_a = np.array([[0, 1, -1], [0, 1, -1], [0, 1, -1]]).astype(data_type) input_b = np.array(1).astype(data_type) expected_output = numpy_func(input_a, input_b) ng_results = run_node(node, [input_a, input_b], opset_version=4) assert np.array_equal(ng_results, [expected_output])
def test_flatten(axis, expected_output): data = np.arange(120).reshape(2, 3, 4, 5) node = onnx.helper.make_node('Flatten', inputs=['x'], outputs=['y'], axis=axis) ng_results = run_node(node, [data]) assert np.array_equal(ng_results, [expected_output])
def test_hardsigmoid(): def hardsigmoid(data, alpha=float(0.2), beta=float(0.5)): return np.clip(alpha * data + beta, 0, 1) np.random.seed(133391) alpha = np.random.rand() beta = np.random.rand() data = np.random.rand(3, 4, 5).astype(np.float32) expected = hardsigmoid(data, alpha, beta) node = onnx.helper.make_node('HardSigmoid', inputs=['x'], outputs=['y'], alpha=alpha, beta=beta) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected]) expected = hardsigmoid(data) node = onnx.helper.make_node('HardSigmoid', inputs=['x'], outputs=['y']) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected])
def test_pool_max(ndarray_1x1x4x4): node = onnx.helper.make_node('MaxPool', inputs=['x'], outputs=['y'], kernel_shape=(2, 2), strides=(2, 2)) x = ndarray_1x1x4x4 y = np.array([[16, 18], [24, 26]], dtype=np.float32).reshape(1, 1, 2, 2) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y])
def test_parametric_relu(x, slope): def parametic_relu(x, slope): return np.where(x < 0, slope * x, x) x, slope = np.array(x).astype(np.float32), np.array(slope).astype( np.float32) expected_output = parametic_relu(x, slope) node = onnx.helper.make_node('PRelu', inputs=['x', 'slope'], outputs=['y']) output = run_node(node, [x, slope]).pop() assert np.allclose(output, expected_output)
def test_hardmax(axis, dim1, dim2): def hardmax_2d(data): return np.eye(data.shape[1], dtype=data.dtype)[np.argmax(data, axis=1)] np.random.seed(133391) data = np.random.rand(3, 4, 5).astype(np.float32) expected = hardmax_2d(data.reshape(dim1, dim2)).reshape(3, 4, 5) node = onnx.helper.make_node('Hardmax', inputs=['x'], outputs=['y'], axis=axis) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected])