def test_pad_opset_2(): x = np.ones((2, 2), dtype=np.float32) y = np.pad(x, pad_width=1, mode='constant') model = get_node_model('Pad', x, opset=2, pads=[1, 1, 1, 1]) ng_results = run_model(model, [x]) assert np.array_equal(ng_results, [y]) x = np.random.randn(1, 3, 4, 5).astype(np.float32) y = np.pad(x, pad_width=((0, 0), (0, 0), (1, 2), (3, 4)), mode='constant') model = get_node_model('Pad', x, opset=2, mode='constant', pads=[0, 0, 1, 3, 0, 0, 2, 4]) ng_results = run_model(model, [x]) assert np.array_equal(ng_results, [y]) # incorrect pads rank x = np.ones((2, 2), dtype=np.float32) model = get_node_model('Pad', x, opset=2, pads=[0, 1, 1, 3, 1, 2]) with pytest.raises(ValueError): run_model(model, [x]) # negative pads values model = get_node_model('Pad', x, opset=2, pads=[0, -1, -1, 3]) with pytest.raises(NotImplementedError): run_model(model, [x]) # no pads attribute model = get_node_model('Pad', x, opset=2) with pytest.raises(ValueError): import_onnx_model(model)[0]
def test_pad_opset_1(): x = np.ones((2, 2), dtype=np.float32) y = np.pad(x, pad_width=1, mode='constant') model = get_node_model('Pad', x, paddings=[1, 1, 1, 1]) ng_results = run_model(model, [x]) assert np.array_equal(ng_results, [y]) x = np.random.randn(1, 3, 4, 5).astype(np.float32) y = np.pad(x, pad_width=((0, 0), (0, 0), (1, 2), (3, 4)), mode='constant') model = get_node_model('Pad', x, mode='constant', paddings=[0, 0, 1, 3, 0, 0, 2, 4]) ng_results = run_model(model, [x]) assert np.array_equal(ng_results, [y]) # incorrect paddings rank x = np.ones((2, 2), dtype=np.float32) model = get_node_model('Pad', x, paddings=[0, 1, 1, 3, 1, 2]) with pytest.raises(ValueError): run_model(model, [x]) # no paddings arttribute model = get_node_model('Pad', x) with pytest.raises(ValueError): import_onnx_model(model)
def test_missing_op(): node = make_node('FakeOpName', ['A'], ['X'], name='missing_op_node') graph = make_graph( [node], 'test_graph', [make_tensor_value_info('A', onnx.TensorProto.FLOAT, [1])], [make_tensor_value_info('X', onnx.TensorProto.FLOAT, [1])]) model = make_model(graph, producer_name='ngraph ONNXImporter') with pytest.raises(RuntimeError) as exc_info: import_onnx_model(model) exc_args = exc_info.value.args assert 'unknown operations: FakeOpName' in str(exc_args[0] % exc_args[1:])
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 run_model(onnx_model, data_inputs): # type: (onnx.ModelProto, List[np.ndarray]) -> List[np.ndarray] """ Convert ONNX model to an ngraph model and perform computation on input data. :param onnx_model: ONNX ModelProto describing an ONNX model :param data_inputs: list of numpy ndarrays with input data :return: list of numpy ndarrays with computed output """ NgraphBackend.backend_name = pytest.config.getoption('backend', default='CPU') if NgraphBackend.supports_ngraph_device(NgraphBackend.backend_name): ng_model_function = import_onnx_model(onnx_model) runtime = get_runtime() computation = runtime.computation(ng_model_function) return computation(*data_inputs) else: raise RuntimeError('The requested nGraph backend <' + NgraphBackend.backend_name + '> is not supported!')
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)
def prepare(cls, onnx_model, device='CPU', **kwargs): # type: (onnx.ModelProto, str, Dict) -> NgraphBackendRep """Prepare backend representation of ONNX model.""" super(NgraphBackend, cls).prepare(onnx_model, device, **kwargs) ng_model_function = import_onnx_model(onnx_model) return NgraphBackendRep(ng_model_function, cls.backend_name)
def test_cast_errors(): np.random.seed(133391) input_data = np.ceil(np.random.rand(2, 3, 4) * 16) # missing 'to' attribute node = onnx.helper.make_node('Cast', inputs=['A'], outputs=['B']) input_tensors = [ make_tensor_value_info(name, onnx.TensorProto.FLOAT, value.shape) for name, value in zip(node.input, [input_data]) ] output_tensors = [ make_tensor_value_info(name, onnx.TensorProto.FLOAT16, value.shape) for name, value in zip(node.output, ()) ] # type: ignore graph = make_graph([node], 'compute_graph', input_tensors, output_tensors) model = make_model(graph, producer_name='NgraphBackend') with pytest.raises(RuntimeError): import_onnx_model(model) # unsupported data type representation node = onnx.helper.make_node('Cast', inputs=['A'], outputs=['B'], to=1.2345) input_tensors = [ make_tensor_value_info(name, onnx.TensorProto.FLOAT, value.shape) for name, value in zip(node.input, [input_data]) ] output_tensors = [ make_tensor_value_info(name, onnx.TensorProto.INT32, value.shape) for name, value in zip(node.output, ()) ] # type: ignore graph = make_graph([node], 'compute_graph', input_tensors, output_tensors) model = make_model(graph, producer_name='NgraphBackend') with pytest.raises(RuntimeError): import_onnx_model(model) # unsupported input tensor data type: node = onnx.helper.make_node('Cast', inputs=['A'], outputs=['B'], to=onnx.TensorProto.INT32) input_tensors = [ make_tensor_value_info(name, onnx.TensorProto.COMPLEX64, value.shape) for name, value in zip(node.input, [input_data]) ] output_tensors = [ make_tensor_value_info(name, onnx.TensorProto.INT32, value.shape) for name, value in zip(node.output, ()) ] # type: ignore graph = make_graph([node], 'compute_graph', input_tensors, output_tensors) model = make_model(graph, producer_name='NgraphBackend') with pytest.raises((RuntimeError, NgraphTypeError)): import_onnx_model(model) # unsupported output tensor data type: node = onnx.helper.make_node('Cast', inputs=['A'], outputs=['B'], to=onnx.TensorProto.COMPLEX128) input_tensors = [ make_tensor_value_info(name, onnx.TensorProto.FLOAT, value.shape) for name, value in zip(node.input, [input_data]) ] output_tensors = [ make_tensor_value_info(name, onnx.TensorProto.COMPLEX128, value.shape) for name, value in zip(node.output, ()) ] # type: ignore graph = make_graph([node], 'compute_graph', input_tensors, output_tensors) model = make_model(graph, producer_name='NgraphBackend') with pytest.raises(RuntimeError): import_onnx_model(model)