def test_split_1d(): # 1D data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float32) node = onnx.helper.make_node("Split", inputs=["input"], outputs=["z", "w"], axis=0) expected_outputs = [ np.array([1.0, 2.0, 3.0]).astype(np.float32), np.array([4.0, 5.0, 6.0]).astype(np.float32), ] ng_results = run_node(node, [data]) assert all_arrays_equal(ng_results, expected_outputs) splits = np.array([2, 3, 1]).astype(np.int64) node = onnx.helper.make_node( "Split", inputs=["input", "splits"], outputs=["y", "z", "w"], axis=0 ) expected_outputs = [ np.array([1.0, 2.0]).astype(np.float32), np.array([3.0, 4.0, 5.0]).astype(np.float32), np.array([6.0]).astype(np.float32), ] ng_results = run_node(node, [data, splits]) assert all_arrays_equal(ng_results, expected_outputs) # Default values data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float32) node = onnx.helper.make_node("Split", inputs=["input"], outputs=["y", "z", "w"]) expected_outputs = [ np.array([1.0, 2.0]).astype(np.float32), np.array([3.0, 4.0]).astype(np.float32), np.array([5.0, 6.0]).astype(np.float32), ] ng_results = run_node(node, [data]) assert all_arrays_equal(ng_results, expected_outputs) splits = np.array([2, 4]).astype(np.int64) node = onnx.helper.make_node( "Split", inputs=["input", "splits"], outputs=["y", "z"] ) expected_outputs = [ np.array([1.0, 2.0]).astype(np.float32), np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32), ] ng_results = run_node(node, [data, splits]) assert all_arrays_equal(ng_results, expected_outputs)
def test_logical_not(): input_data = np.array([[False, True, True], [False, True, False], [False, False, True]]) 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_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_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 _, 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=list(in_args), outputs=["output"], axis=i, ) expected_output = np.concatenate(values, i) ng_results = run_node(node, list(values)) 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_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_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_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_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_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_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_reshape(): input_data = np.arange(2560, dtype=np.int32).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_hardsigmoid(): def hardsigmoid(data, alpha=0.2, beta=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_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]) 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]) # 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]) with pytest.raises(RuntimeError): node = onnx.helper.make_node("LogSoftmax", inputs=["x"], outputs=["y"], axis=3) ng_results = run_node(node, [data])
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_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])
def test_eye_like(shape, shift): input_tensor = np.arange(np.prod(shape)).reshape(shape) node = onnx.helper.make_node("EyeLike", inputs=["x"], outputs=["y"], k=shift) result = run_node(node, [input_tensor])[0] assert np.allclose(result, np.eye(shape[0], shape[1], k=shift))
def test_pool_average_3d(ndarray_1x1x4x4): x = np.broadcast_to(ndarray_1x1x4x4, (1, 1, 4, 4, 4)) node = onnx.helper.make_node( "AveragePool", inputs=["x"], outputs=["y"], kernel_shape=(2, 2, 2), strides=(2, 2, 2) ) y = np.array([[[13.5, 15.5], [21.5, 23.5]], [[13.5, 15.5], [21.5, 23.5]]], dtype=np.float32).reshape( [1, 1, 2, 2, 2] ) ng_results = run_node(node, [x]) assert np.array_equal(ng_results, [y])
def test_unsqueeze(): data = np.random.randn(3, 4, 5).astype(np.float32) expected_output = np.expand_dims(data, axis=0) axes = np.array([0]).astype(np.int64) node = onnx.helper.make_node("Unsqueeze", inputs=["x", "axes"], outputs=["y"]) ng_results = run_node(node, [data, axes]) assert np.array_equal(ng_results, [expected_output]) expected_output = np.reshape(data, [1, 3, 4, 5, 1]) axes = np.array([0, 4]).astype(np.int64) node = onnx.helper.make_node("Unsqueeze", inputs=["x", "axes"], outputs=["y"]) ng_results = run_node(node, [data, axes]) assert np.array_equal(ng_results, [expected_output]) expected_output = np.reshape(data, [1, 3, 1, 4, 5]) axes = np.array([0, 2]).astype(np.int64) node = onnx.helper.make_node("Unsqueeze", inputs=["x", "axes"], outputs=["y"]) ng_results = run_node(node, [data, axes]) 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_squeeze(): data = np.arange(6, dtype=np.int32).reshape([1, 2, 3, 1]) expected_output = data.reshape([2, 3]) axes = np.array([0, 3]).astype(np.int64) node = onnx.helper.make_node("Squeeze", inputs=["x", "axes"], outputs=["y"]) ng_results = run_node(node, [data, axes]) 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) axes = np.array([0]).astype(np.int64) node = onnx.helper.make_node("Squeeze", inputs=["x", "axes"], outputs=["y"]) ng_results = run_node(node, [data, axes]) 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.int32) 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_softsign(): def softsign(x): return x / (1 + np.abs(x)) np.random.seed(133391) data = np.random.randn(3, 4, 5).astype(np.float32) node = onnx.helper.make_node("Softsign", inputs=["x"], outputs=["y"]) expected = softsign(data) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected])
def test_softplus(): def softplus(x): return np.where(x < 20, np.log(np.exp(x) + 1), x) np.random.seed(133391) data = np.random.randn(3, 4, 5).astype(np.float32) node = onnx.helper.make_node("Softplus", inputs=["x"], outputs=["y"]) expected = softplus(data) ng_results = run_node(node, [data]) assert np.allclose(ng_results, [expected])
def test_split_2d_splits_input(): data = np.arange(8, dtype=np.int32).reshape(2, 4) splits = np.array([3, 1]).astype(np.int64) node = onnx.helper.make_node( "Split", inputs=["x", "splits"], outputs=["a", "b"], axis=1 ) expected_outputs = [ np.array([[0, 1, 2], [4, 5, 6]], dtype=np.int32), np.array([[3], [7]], dtype=np.int32), ] ng_results = run_node(node, [data, splits]) assert all_arrays_equal(ng_results, expected_outputs)
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_reduce_l1(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.abs(input_data), keepdims=True, axis=reduction_axes) node = onnx.helper.make_node("ReduceL1", 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.abs(input_data), keepdims=False, axis=reduction_axes) node = onnx.helper.make_node("ReduceL1", 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_constant_err(): values = np.random.randn(5, 5).astype(np.float16) node = onnx.helper.make_node( "Constant", inputs=[], outputs=["values"], value=onnx.helper.make_tensor( name="const_tensor", data_type=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(np.float16)], dims=values.shape, vals=values.flatten(), ), ) ng_results = run_node(node, []) assert np.allclose(ng_results, [values])
def test_random_normal(): mean = 100.0 scale = 10.0 node = onnx.helper.make_node( "RandomNormal", inputs=[], outputs=["y"], mean=mean, scale=scale, seed=10.0, shape=(30, 30), ) result = run_node(node, [])[0] assert result.shape == (30, 30) assert len(np.unique(result)) == 900 assert np.allclose(np.mean(result), mean, rtol=0.05) assert np.allclose(np.std(result), scale, rtol=0.05)
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)