Lightning-AI/pytorch-lightning · error · ModuleNotFoundError

`{type(self).__name__}.to_tensorrt` requires `torch_tensorrt

Error message

`{type(self).__name__}.to_tensorrt` requires `torch_tensorrt` to be installed. 

What it means

LightningModule.to_tensorrt() depends on the `torch_tensorrt` package. The guard `_TORCH_TRT_AVAILABLE` is False when the import fails, and the method immediately raises ModuleNotFoundError before doing any work.

Source

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

            class SimpleModel(LightningModule):
                def __init__(self):
                    super().__init__()
                    self.l1 = torch.nn.Linear(in_features=64, out_features=4)

                def forward(self, x):
                    return torch.relu(self.l1(x.view(x.size(0), -1)

            model = SimpleModel()
            input_sample = torch.randn(1, 64)
            exported_program = model.to_tensorrt(
                file_path="export.ep",
                inputs=input_sample,
            )

        """
        if not _TORCH_TRT_AVAILABLE:
            raise ModuleNotFoundError(
                f"`{type(self).__name__}.to_tensorrt` requires `torch_tensorrt` to be installed. "
            )

        mode = self.training
        device = self.device
        if self.device.type != "cuda":
            default_device = torch.device(default_device) if isinstance(default_device, str) else default_device

            if not torch.cuda.is_available() or default_device.type != "cuda":
                raise MisconfigurationException(
                    f"TensorRT only supports CUDA devices. The current device is {self.device}."
                    f" Please set the `default_device` argument to a CUDA device."
                )

            self.to(default_device)

        if input_sample is None:
            if self.example_input_array is None:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. pip install torch-tensorrt matching your torch and CUDA versions
  2. Verify: python -c "import torch_tensorrt"
  3. Ensure a CUDA-capable environment (see also error 366)

Example fix

# before
model.to_tensorrt("model.ep", input_sample=x)  # ModuleNotFoundError
# after
# pip install torch-tensorrt
model.to_tensorrt("model.ep", input_sample=x)
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util
if importlib.util.find_spec("torch_tensorrt") is None:
    raise RuntimeError("torch_tensorrt not installed; cannot export TensorRT")

Try / catch

try:
    model.to_tensorrt(path, input_sample=x)
except ModuleNotFoundError:
    model.to_onnx("fallback.onnx", x)  # convert offline

Prevention

When it happens

Trigger: Calling `model.to_tensorrt(...)` in any environment where `import torch_tensorrt` fails (not installed or incompatible with the installed torch/CUDA).

Common situations: Trying TensorRT export on a CPU-only machine, a container without torch_tensorrt, or after a torch upgrade broke torch_tensorrt ABI compatibility.

Understand the failure class

Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.

Related errors


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