{"record":{"id":"5ead16ea66c76248","repo":"Lightning-AI/pytorch-lightning","slug":"type-self-name-to-tensorrt-requires-torc","errorCode":null,"errorMessage":"`{type(self).__name__}.to_tensorrt` requires `torch_tensorrt` to be installed. ","messagePattern":"`(.+?)\\.to_tensorrt` requires `torch_tensorrt` to be installed\\. ","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/module.py","lineNumber":1648,"sourceCode":"\n            class SimpleModel(LightningModule):\n                def __init__(self):\n                    super().__init__()\n                    self.l1 = torch.nn.Linear(in_features=64, out_features=4)\n\n                def forward(self, x):\n                    return torch.relu(self.l1(x.view(x.size(0), -1)\n\n            model = SimpleModel()\n            input_sample = torch.randn(1, 64)\n            exported_program = model.to_tensorrt(\n                file_path=\"export.ep\",\n                inputs=input_sample,\n            )\n\n        \"\"\"\n        if not _TORCH_TRT_AVAILABLE:\n            raise ModuleNotFoundError(\n                f\"`{type(self).__name__}.to_tensorrt` requires `torch_tensorrt` to be installed. \"\n            )\n\n        mode = self.training\n        device = self.device\n        if self.device.type != \"cuda\":\n            default_device = torch.device(default_device) if isinstance(default_device, str) else default_device\n\n            if not torch.cuda.is_available() or default_device.type != \"cuda\":\n                raise MisconfigurationException(\n                    f\"TensorRT only supports CUDA devices. The current device is {self.device}.\"\n                    f\" Please set the `default_device` argument to a CUDA device.\"\n                )\n\n            self.to(default_device)\n\n        if input_sample is None:\n            if self.example_input_array is None:","sourceCodeStart":1630,"sourceCodeEnd":1666,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/module.py#L1630-L1666","documentation":"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.","triggerScenarios":"Calling `model.to_tensorrt(...)` in any environment where `import torch_tensorrt` fails (not installed or incompatible with the installed torch/CUDA).","commonSituations":"Trying TensorRT export on a CPU-only machine, a container without torch_tensorrt, or after a torch upgrade broke torch_tensorrt ABI compatibility.","solutions":["pip install torch-tensorrt matching your torch and CUDA versions","Verify: python -c \"import torch_tensorrt\"","Ensure a CUDA-capable environment (see also error 366)"],"exampleFix":"# before\nmodel.to_tensorrt(\"model.ep\", input_sample=x)  # ModuleNotFoundError\n# after\n# pip install torch-tensorrt\nmodel.to_tensorrt(\"model.ep\", input_sample=x)","handlingStrategy":"validation","validationCode":"import importlib.util\nif importlib.util.find_spec(\"torch_tensorrt\") is None:\n    raise RuntimeError(\"torch_tensorrt not installed; cannot export TensorRT\")","typeGuard":null,"tryCatchPattern":"try:\n    model.to_tensorrt(path, input_sample=x)\nexcept ModuleNotFoundError:\n    model.to_onnx(\"fallback.onnx\", x)  # convert offline","preventionTips":["Install torch-tensorrt in the GPU export image only and skip TRT export elsewhere","Version-match torch-tensorrt to torch/CUDA"],"tags":["tensorrt","export","missing-dependency","lightning"],"backgroundTag":"missing-optional-dependency","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}