Utility to convert a model into TorchScript model and save to file, with optional input / output data verification. Args: model: source PyTorch model to save. filename_or_obj: if not None, specify a file-like object (has to implement write and flush) or a st
(
model: nn.Module,
filename_or_obj: Any | None = None,
extra_files: dict | None = None,
verify: bool = False,
inputs: Sequence[Any] | None = None,
device: torch.device | None = None,
rtol: float = 1e-4,
atol: float = 0.0,
use_trace: bool = False,
**kwargs,
)
| 794 | |
| 795 | |
| 796 | def convert_to_torchscript( |
| 797 | model: nn.Module, |
| 798 | filename_or_obj: Any | None = None, |
| 799 | extra_files: dict | None = None, |
| 800 | verify: bool = False, |
| 801 | inputs: Sequence[Any] | None = None, |
| 802 | device: torch.device | None = None, |
| 803 | rtol: float = 1e-4, |
| 804 | atol: float = 0.0, |
| 805 | use_trace: bool = False, |
| 806 | **kwargs, |
| 807 | ): |
| 808 | """ |
| 809 | Utility to convert a model into TorchScript model and save to file, |
| 810 | with optional input / output data verification. |
| 811 | |
| 812 | Args: |
| 813 | model: source PyTorch model to save. |
| 814 | filename_or_obj: if not None, specify a file-like object (has to implement write and flush) |
| 815 | or a string containing a file path name to save the TorchScript model. |
| 816 | extra_files: map from filename to contents which will be stored as part of the save model file. |
| 817 | for more details: https://pytorch.org/docs/stable/generated/torch.jit.save.html. |
| 818 | verify: whether to verify the input and output of TorchScript model. |
| 819 | if `filename_or_obj` is not None, load the saved TorchScript model and verify. |
| 820 | inputs: input test data to verify model, should be a sequence of data, every item maps to a argument |
| 821 | of `model()` function. |
| 822 | device: target device to verify the model, if None, use CUDA if available. |
| 823 | rtol: the relative tolerance when comparing the outputs of PyTorch model and TorchScript model. |
| 824 | atol: the absolute tolerance when comparing the outputs of PyTorch model and TorchScript model. |
| 825 | use_trace: whether to use `torch.jit.trace` to export the TorchScript model. |
| 826 | kwargs: other arguments except `obj` for `torch.jit.script()` or `torch.jit.trace()` (if use_trace is True) |
| 827 | to convert model, for more details: https://pytorch.org/docs/master/generated/torch.jit.script.html. |
| 828 | |
| 829 | """ |
| 830 | model.eval() |
| 831 | with torch.no_grad(): |
| 832 | if use_trace: |
| 833 | if inputs is None: |
| 834 | raise ValueError("Missing input data for tracing convert.") |
| 835 | script_module = torch.jit.trace(model, example_inputs=inputs, **kwargs) |
| 836 | else: |
| 837 | script_module = torch.jit.script(model, **kwargs) |
| 838 | if filename_or_obj is not None: |
| 839 | torch.jit.save(m=script_module, f=filename_or_obj, _extra_files=extra_files) |
| 840 | |
| 841 | if verify: |
| 842 | if device is None: |
| 843 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 844 | if inputs is None: |
| 845 | raise ValueError("Missing input data for verification.") |
| 846 | |
| 847 | inputs = [i.to(device) if isinstance(i, torch.Tensor) else i for i in inputs] |
| 848 | ts_model = torch.jit.load(filename_or_obj) if filename_or_obj is not None else script_module |
| 849 | ts_model.eval().to(device) |
| 850 | model = model.to(device) |
| 851 | |
| 852 | with torch.no_grad(): |
| 853 | set_determinism(seed=0) |
searching dependent graphs…