{"record":{"id":"fe438fdc92582702","repo":"Lightning-AI/pytorch-lightning","slug":"could-not-export-to-tensorrt-since-neither-input","errorCode":null,"errorMessage":"Could not export to TensorRT since neither `input_sample` nor `model.example_input_array` attribute is set.","messagePattern":"Could not export to TensorRT since neither `input_sample` nor `model\\.example_input_array` attribute is set\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/module.py","lineNumber":1667,"sourceCode":"                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:\n                raise ValueError(\n                    \"Could not export to TensorRT since neither `input_sample` nor\"\n                    \" `model.example_input_array` attribute is set.\"\n                )\n            input_sample = self.example_input_array\n\n        import torch_tensorrt\n\n        input_sample = copy.deepcopy((input_sample,) if isinstance(input_sample, torch.Tensor) else input_sample)\n        input_sample = self._on_before_batch_transfer(input_sample)\n        input_sample = self._apply_batch_transfer_handler(input_sample)\n\n        with _jit_is_scripting() if ir == \"ts\" else nullcontext():\n            trt_obj = torch_tensorrt.compile(\n                module=self.eval(),\n                ir=ir,\n                inputs=input_sample,\n                **compile_kwargs,\n            )","sourceCodeStart":1649,"sourceCodeEnd":1685,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/module.py#L1649-L1685","documentation":"Like to_onnx, TensorRT compilation needs a real input tensor to trace the model. If neither the `input_sample` argument nor `model.example_input_array` exists, Lightning raises ValueError.","triggerScenarios":"Calling `model.to_tensorrt(\"model.ep\")` without input_sample on a model lacking example_input_array.","commonSituations":"Exporting a newly written LightningModule for inference deployment without having set an example input.","solutions":["Pass input_sample=torch.randn(1, *input_shape) (on the right device)","Or set self.example_input_array in the model's __init__"],"exampleFix":"# before\nmodel.to_tensorrt(\"model.ep\")\n# after\nmodel.to_tensorrt(\"model.ep\", input_sample=torch.randn(1, 3, 224, 224, device=\"cuda\"))","handlingStrategy":"validation","validationCode":"sample = input_sample if input_sample is not None else getattr(model, \"example_input_array\", None)\nif sample is None:\n    raise ValueError(\"input required for TensorRT export\")\nmodel.to_tensorrt(path, input_sample=sample)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass input_sample with realistic shape/dtype for deployment"],"tags":["tensorrt","example-input","lightning"],"backgroundTag":"missing-required-argument","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}