Test the ability to save `net` in ONNX format, reload it and validate with runtime. The value `inputs` is forward-passed through the `net` without gradient accumulation to do onnx export and PyTorch inference. PyTorch model inference is performed with CUDA if available, else CPU.
(net, *inputs, device=None, rtol=1e-4, atol=0.0)
| 807 | |
| 808 | |
| 809 | def test_onnx_save(net, *inputs, device=None, rtol=1e-4, atol=0.0): |
| 810 | """ |
| 811 | Test the ability to save `net` in ONNX format, reload it and validate with runtime. |
| 812 | The value `inputs` is forward-passed through the `net` without gradient accumulation |
| 813 | to do onnx export and PyTorch inference. |
| 814 | PyTorch model inference is performed with CUDA if available, else CPU. |
| 815 | Saved ONNX model is validated with onnxruntime, if available, else ONNX native implementation. |
| 816 | """ |
| 817 | # TODO: would be nice to use GPU if available, but it currently causes CI failures. |
| 818 | device = "cpu" |
| 819 | _, has_onnxruntime = optional_import("onnxruntime") |
| 820 | with tempfile.TemporaryDirectory() as tempdir: |
| 821 | convert_to_onnx( |
| 822 | model=net, |
| 823 | filename=os.path.join(tempdir, "model.onnx"), |
| 824 | verify=True, |
| 825 | inputs=inputs, |
| 826 | device=device, |
| 827 | use_ort=has_onnxruntime, |
| 828 | rtol=rtol, |
| 829 | atol=atol, |
| 830 | ) |
| 831 | |
| 832 | |
| 833 | def download_url_or_skip_test(*args, **kwargs): |
no test coverage detected
searching dependent graphs…