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)[0]
def import_and_compute(op_type, input_data_left, input_data_right, opset=4, **node_attributes): inputs = [np.array(input_data_left), np.array(input_data_right)] onnx_node = onnx.helper.make_node(op_type, inputs=['x', 'y'], outputs=['z'], **node_attributes) input_tensors = [ make_tensor_value_info(name, onnx.TensorProto.FLOAT, value.shape) for name, value in zip(onnx_node.input, inputs) ] output_tensors = [ make_tensor_value_info(name, onnx.TensorProto.FLOAT, value.shape) for name, value in zip(onnx_node.output, ()) ] # type: ignore graph = make_graph([onnx_node], 'compute_graph', input_tensors, output_tensors) model = make_model(graph, producer_name='NgraphBackend') model.opset_import[0].version = opset return run_model(model, inputs)[0]
def test_cast_to_bool(val_type, input_data): expected = np.array(input_data, dtype=val_type) model = get_node_model('Cast', input_data, opset=6, to=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[val_type]) result = run_model(model, [input_data]) assert np.allclose(result, expected)
def test_cast_to_uint(val_type): np.random.seed(133391) input_data = np.ceil(np.random.rand(2, 3, 4) * 16) expected = np.array(input_data, dtype=val_type) model = get_node_model('Cast', input_data, opset=6, to=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[val_type]) result = run_model(model, [input_data]) assert np.allclose(result, expected)
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)
def test_cast_to_float(val_type, range_start, range_end, in_dtype): np.random.seed(133391) input_data = np.random.randint(range_start, range_end, size=(2, 2), dtype=in_dtype) expected = np.array(input_data, dtype=val_type) model = get_node_model('Cast', input_data, opset=6, to=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[val_type]) result = run_model(model, [input_data]) assert np.allclose(result, expected)