Export the model checkpoint to the given filepath as a TensorRT engine-based TorchScript. Currently, this API only supports converting models whose inputs are all tensors. Note: NVIDIA Volta support (GPUs with compute capability 7.0) has been removed starting with TensorRT 10.5. Rev
(
net_id: str | None = None,
filepath: PathLike | None = None,
ckpt_file: str | None = None,
meta_file: str | Sequence[str] | None = None,
config_file: str | Sequence[str] | None = None,
key_in_ckpt: str | None = None,
precision: str | None = None,
input_shape: Sequence[int] | None = None,
use_trace: bool | None = None,
dynamic_batchsize: Sequence[int] | None = None,
device: int | None = None,
use_onnx: bool | None = None,
onnx_input_names: Sequence[str] | None = None,
onnx_output_names: Sequence[str] | None = None,
args_file: str | None = None,
converter_kwargs: Mapping | None = None,
**override: Any,
)
| 1569 | |
| 1570 | |
| 1571 | def trt_export( |
| 1572 | net_id: str | None = None, |
| 1573 | filepath: PathLike | None = None, |
| 1574 | ckpt_file: str | None = None, |
| 1575 | meta_file: str | Sequence[str] | None = None, |
| 1576 | config_file: str | Sequence[str] | None = None, |
| 1577 | key_in_ckpt: str | None = None, |
| 1578 | precision: str | None = None, |
| 1579 | input_shape: Sequence[int] | None = None, |
| 1580 | use_trace: bool | None = None, |
| 1581 | dynamic_batchsize: Sequence[int] | None = None, |
| 1582 | device: int | None = None, |
| 1583 | use_onnx: bool | None = None, |
| 1584 | onnx_input_names: Sequence[str] | None = None, |
| 1585 | onnx_output_names: Sequence[str] | None = None, |
| 1586 | args_file: str | None = None, |
| 1587 | converter_kwargs: Mapping | None = None, |
| 1588 | **override: Any, |
| 1589 | ) -> None: |
| 1590 | """ |
| 1591 | Export the model checkpoint to the given filepath as a TensorRT engine-based TorchScript. |
| 1592 | Currently, this API only supports converting models whose inputs are all tensors. |
| 1593 | Note: NVIDIA Volta support (GPUs with compute capability 7.0) has been removed starting with TensorRT 10.5. |
| 1594 | Review the TensorRT Support Matrix for which GPUs are supported. |
| 1595 | |
| 1596 | There are two ways to export a model: |
| 1597 | 1, Torch-TensorRT way: PyTorch module ---> TorchScript module ---> TensorRT engine-based TorchScript. |
| 1598 | 2, ONNX-TensorRT way: PyTorch module ---> TorchScript module ---> ONNX model ---> TensorRT engine ---> |
| 1599 | TensorRT engine-based TorchScript. |
| 1600 | |
| 1601 | When exporting through the first way, some models suffer from the slowdown problem, since Torch-TensorRT |
| 1602 | may only convert a little part of the PyTorch model to the TensorRT engine. However when exporting through |
| 1603 | the second way, some Python data structures like `dict` are not supported. And some TorchScript models are |
| 1604 | not supported by the ONNX if exported through `torch.jit.script`. |
| 1605 | |
| 1606 | Typical usage examples: |
| 1607 | |
| 1608 | .. code-block:: bash |
| 1609 | |
| 1610 | python -m monai.bundle trt_export --net_id <network definition> --filepath <export path> \ |
| 1611 | --ckpt_file <checkpoint path> --input_shape <input shape> --dynamic_batchsize <batch range> ... |
| 1612 | |
| 1613 | Args: |
| 1614 | net_id: ID name of the network component in the config, it must be `torch.nn.Module`. |
| 1615 | filepath: filepath to export, if filename has no extension, it becomes `.ts`. |
| 1616 | ckpt_file: filepath of the model checkpoint to load. |
| 1617 | meta_file: filepath of the metadata file, if it is a list of file paths, the content of them will be merged. |
| 1618 | config_file: filepath of the config file to save in the TensorRT based TorchScript model and extract network |
| 1619 | information, the saved key in the model is the config filename without extension, and the saved config |
| 1620 | value is always serialized in JSON format no matter the original file format is JSON or YAML. |
| 1621 | it can be a single file or a list of files. if `None`, must be provided in `args_file`. |
| 1622 | key_in_ckpt: for nested checkpoint like `{"model": XXX, "optimizer": XXX, ...}`, specify the key of model |
| 1623 | weights. if not nested checkpoint, no need to set. |
| 1624 | precision: the weight precision of the converted TensorRT engine based TorchScript models. Should be 'fp32' or 'fp16'. |
| 1625 | input_shape: the input shape that is used to convert the model. Should be a list like [N, C, H, W] or |
| 1626 | [N, C, H, W, D]. If not given, will try to parse from the `metadata` config. |
| 1627 | use_trace: whether using `torch.jit.trace` to convert the PyTorch model to a TorchScript model and then convert to |
| 1628 | a TensorRT engine based TorchScript model or an ONNX model (if `use_onnx` is True). |
nothing calls this directly
no test coverage detected
searching dependent graphs…