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_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 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)[0] computation = get_runtime().computation(ng_model_function) return computation(x, weights)
def import_and_compute_gemm(input_a, input_b, input_c, **kwargs): input_a, input_b, input_c = np.array(input_a), np.array(input_b), np.array(input_c) if kwargs.get('trans_a'): kwargs['transA'] = kwargs['trans_a'] del kwargs['trans_a'] if kwargs.get('trans_b'): kwargs['transB'] = kwargs['trans_b'] del kwargs['trans_b'] onnx_model = make_onnx_model_for_gemm_op(input_a, input_b, input_c, **kwargs) transformer = get_runtime() ng_model_function = import_onnx_model(onnx_model) computation = transformer.computation(ng_model_function) return computation(input_a, input_b, input_c)
def test_simple_graph(): node1 = make_node('Add', ['A', 'B'], ['X'], name='add_node1') node2 = make_node('Add', ['X', 'C'], ['Y'], name='add_node2') graph = make_graph([node1, node2], 'test_graph', [ make_tensor_value_info('A', onnx.TensorProto.FLOAT, [1]), make_tensor_value_info('B', onnx.TensorProto.FLOAT, [1]), make_tensor_value_info('C', onnx.TensorProto.FLOAT, [1]) ], [make_tensor_value_info('Y', onnx.TensorProto.FLOAT, [1])]) model = make_model(graph, producer_name='ngraph ONNXImporter') ng_model_function = import_onnx_model(model)[0] runtime = get_runtime() computation = runtime.computation(ng_model_function) assert np.array_equal(computation(1, 2, 3), np.array([6.0], dtype=np.float32)) assert np.array_equal(computation(4, 5, 6), np.array([15.0], dtype=np.float32))
def test_reshape_opset5(): original_shape = [2, 3, 4] test_cases = { 'reordered_dims': np.array([4, 2, 3], dtype=np.int64), 'reduced_dims': np.array([3, 8], dtype=np.int64), 'extended_dims': np.array([3, 2, 2, 2], dtype=np.int64), 'one_dim': np.array([24], dtype=np.int64), 'negative_dim': np.array([6, -1, 2], dtype=np.int64), } input_data = np.random.random_sample(original_shape).astype(np.float32) for test_name, shape in test_cases.items(): const_node = make_node('Constant', inputs=[], outputs=['const_shape'], value=onnx.helper.make_tensor( name='const_tensor', data_type=onnx.TensorProto.INT64, dims=shape.shape, vals=shape.flatten())) reshape_node = onnx.helper.make_node('Reshape', inputs=['data', 'const_shape'], outputs=['reshaped']) graph = make_graph([const_node, reshape_node], 'test_graph', [ make_tensor_value_info('data', onnx.TensorProto.FLOAT, input_data.shape) ], [make_tensor_value_info('reshaped', onnx.TensorProto.FLOAT, ())]) model = make_model(graph, producer_name='ngraph ONNX Importer') model.opset_import[0].version = 5 ng_model_function = import_onnx_model(model) runtime = get_runtime() computation = runtime.computation(ng_model_function) ng_results = computation(input_data) expected_output = np.reshape(input_data, shape) assert np.array_equal(ng_results[0], expected_output)