Class TensorRTBackend#
Defined in File TensorRTBackend.h
Inheritance Relationships#
Base Type#
public BackendInterface
Class Documentation#
-
class TensorRTBackend : public BackendInterface#
The delegate ExecuTorch calls to run a TensorRT engine.
Registered under the backend id
TensorRTBackend, the name the partitioner writes into the.pte; a.pteproduced by torch_tensorrt.save(output_format=”executorch”) dispatches to it.Public Functions
-
bool is_available() const override#
- Returns:
Whether a usable CUDA device and TensorRT runtime are present.
-
::executorch::runtime::Result<::executorch::runtime::DelegateHandle*> init(::executorch::runtime::BackendInitContext &context, ::executorch::runtime::FreeableBuffer *processed, ::executorch::runtime::ArrayRef<::executorch::runtime::CompileSpec> compile_specs) const override#
Deserializes one engine from its processed blob into a handle.
-
::executorch::runtime::Error execute(::executorch::runtime::BackendExecutionContext &context, ::executorch::runtime::DelegateHandle *handle, ::executorch::runtime::Span<::executorch::runtime::EValue*> args) const override#
Binds
argsand runs the engine, selecting the optimization profile the calling thread’s OptimizationProfileGuard asked for, on the CUDA stream its executorch::extension::cuda::CallerStreamGuard selected.Returns once the work is finished, whatever memory it was given, so outputs are readable as soon as this returns. The stream in use must be on the engine’s device, and calls on one handle must not overlap each other or its destruction. Every buffer passed in must stay alive and unchanged until this returns. Freeing one while the call runs is not detectable here or anywhere below, and returns a plausible wrong answer rather than an error.
With the shared activation scratch pool (kSharedActivationScratchKey) one buffer per device backs every context created while the option was on. What a caller has to know:
Calls on two such handles on one device are serialized at submission. The backend does that itself, with a per-device lock held across the enqueue, and on a GPU without 64-bit stream memory operations until the call has finished. A handle loaded with the option off keeps its own scratch, and so does one whose engine needs no activation scratch under any shape; neither is affected.
A call can wait on the host for pooled work submitted earlier on that device, and a call that grows the pool can wait for an unbounded amount of it. If the caller parks work that only the caller will release once execute() returns, the call does not return. Do not hold a pooled call behind something it has to come back to release.
Capturing a CUDA graph around this delegate is not supported, with the option on or off. A handle that claims pooled scratch refuses a capturing selected stream with Error::NotSupported. Other streams are not checked. Do not overlap execution with a Global capture on any thread or a ThreadLocal capture on the calling thread, even with the option off or with an engine that needs no scratch: the call can invalidate it.
cudaDeviceReset() invalidates the pool without emptying it, and the next call on that device uses what it destroyed. There is no guard: do not reset a device this backend has run a pooled engine on.
Pool failures share error codes with other failures in execute(). Error::NotSupported can also mean an output resize was refused. Error::Internal can mean handoff event creation, cudaFree(nullptr), or the final stream synchronization failed. The CUDA call can fail because of a capture or an earlier device fault. Check the error log for the cause.
The mechanism behind all of that, the measurements, and which calls grow the pool are in the backend README rather than here.
-
::executorch::runtime::Error set_option(ET_UNUSED::executorch::runtime::BackendOptionContext &context, const ::executorch::runtime::Span<::executorch::runtime::BackendOption> &backend_options) override#
Applies the runtime backend options a caller passes to
executorch::runtime::set_option("TensorRTBackend", ...).The keys read are kSharedActivationScratchKey and kCudaGraphsKey, both booleans. A kCudaGraphsKey load option wins over this one, and an explicit false refuses a saved true. kSharedEnginesKey is load-only and is rejected here.
-
bool is_available() const override#