Lightning-AI/pytorch-lightning · error · ValueError

TensorRT with IR mode 'ts' only supports output format 'torc

Error message

TensorRT with IR mode 'ts' only supports output format 'torchscript'. The current output format is {output_format}.

What it means

When to_tensorrt uses ir='ts' (TorchScript IR), the only savable artifact is a TorchScript file, so output_format must be 'torchscript'. Requesting another output_format (e.g. 'engine' or 'onnx') with ir='ts' raises ValueError at save time.

Source

Thrown at src/lightning/pytorch/core/module.py:1692

        input_sample = copy.deepcopy((input_sample,) if isinstance(input_sample, torch.Tensor) else input_sample)
        input_sample = self._on_before_batch_transfer(input_sample)
        input_sample = self._apply_batch_transfer_handler(input_sample)

        with _jit_is_scripting() if ir == "ts" else nullcontext():
            trt_obj = torch_tensorrt.compile(
                module=self.eval(),
                ir=ir,
                inputs=input_sample,
                **compile_kwargs,
            )
        self.train(mode)
        self.to(device)

        if file_path is not None:
            if ir == "ts":
                if output_format != "torchscript":
                    raise ValueError(
                        "TensorRT with IR mode 'ts' only supports output format 'torchscript'."
                        f" The current output format is {output_format}."
                    )
                assert isinstance(trt_obj, (torch.jit.ScriptModule, torch.jit.ScriptFunction)), (
                    f"Expected TensorRT object to be a ScriptModule, but got {type(trt_obj)}."
                )
                # Because of https://github.com/pytorch/TensorRT/issues/3775,
                # we'll need to take special care for the ScriptModule
                torch.jit.save(trt_obj, file_path)
            else:
                torch_tensorrt.save(
                    trt_obj,
                    file_path,
                    inputs=input_sample,
                    output_format=output_format,
                    retrace=retrace,
                )
        return trt_obj

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Use ir="ts" with output_format="torchscript" (the default)
  2. Or switch ir to a mode that supports your desired output format (e.g. ir="dlgraph" for engine/onnx export)

Example fix

# before
model.to_tensorrt("model.ep", ir="ts", output_format="engine")
# after
model.to_tensorrt("model.ts", ir="ts", output_format="torchscript")
Defensive patterns

Strategy: type-guard

Validate before calling

if ir == "ts":
    assert output_format == "torchscript"
model.to_tensorrt(path, ir=ir, output_format=output_format)

Type guard

def valid_trt_combo(ir: str, fmt: str) -> bool:
    return ir != "ts" or fmt == "torchscript"

Prevention

When it happens

Trigger: Calling model.to_tensorrt(path, ir="ts", output_format="engine") or any format other than "torchscript" while ir="ts" and file_path is set.

Common situations: Mixing parameters copied from an ir='dlgraph' example while keeping the default ir='ts', or assuming any ir/format combination is valid.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/03e0123919d7e691. Report an issue: GitHub.