Skip to main content

dl::TorchScriptBackend

dl::TorchScriptBackend

TorchScript inference backend — loads .pt files via torch::jit::load(). More…

#include <TorchScriptBackend.hpp>

Inherits from dl::InferenceBackend

Public Classes

Name
struct Impl

Public Functions

Name
TorchScriptBackend()
~TorchScriptBackend() override
TorchScriptBackend(TorchScriptBackend && )
TorchScriptBackend & operator=(TorchScriptBackend && )
virtual std::string name() const override
Human-readable backend name (e.g. “TorchScript”, “AOTInductor”).
virtual BackendType type() const override
The BackendType enum value for this backend.
virtual std::string fileExtension() const override
File extension this backend handles (e.g. “.pt”, “.pt2”, “.pte”).
virtual bool load(std::filesystem::path const & path) override
Load model weights/program from a file.
virtual bool isLoaded() const override
Whether a model is loaded and ready for execution.
virtual std::filesystem::path loadedPath() const override
Get the path of the currently loaded model.
virtual std::vector< at::Tensor > execute(std::vector< at::Tensor > const & inputs) override
Execute the default method (typically “forward”) with ordered inputs.
virtual std::vector< at::Tensor > execute(std::string const & method_name, std::vector< at::Tensor > const & inputs) override
Execute a named method with ordered inputs.

Additional inherited members

Public Functions inherited from dl::InferenceBackend

Name
virtual ~InferenceBackend() =default
InferenceBackend(InferenceBackend const & ) =delete

Protected Functions inherited from dl::InferenceBackend

Name
InferenceBackend() =default
InferenceBackend(InferenceBackend && ) =default

Detailed Description

class dl::TorchScriptBackend;

TorchScript inference backend — loads .pt files via torch::jit::load().

This backend uses the TorchScript interpreter built into libtorch to load and execute serialized models produced by torch.jit.trace() or torch.jit.script() in Python.

While TorchScript is deprecated by the PyTorch team, it remains fully functional in libtorch 2.9.0 and provides the simplest export path. Legacy models (EfficientSAM, scm) already use this format.

Key characteristics:

  • Interpreter-based execution with JIT fusion optimizations
  • Supports multiple named methods (e.g. “forward”, “encode”, “decode”)
  • Supports arbitrary dynamic shapes natively
  • Model runs on the device selected by DeviceManager (CPU or CUDA)

Public Functions Documentation

function TorchScriptBackend

TorchScriptBackend()

function ~TorchScriptBackend

~TorchScriptBackend() override

function TorchScriptBackend

TorchScriptBackend(
    TorchScriptBackend && 
)

function operator=

TorchScriptBackend & operator=(
    TorchScriptBackend && 
)

function name

virtual std::string name() const override

Human-readable backend name (e.g. “TorchScript”, “AOTInductor”).

Reimplements: dl::InferenceBackend::name

function type

virtual BackendType type() const override

The BackendType enum value for this backend.

Reimplements: dl::InferenceBackend::type

function fileExtension

virtual std::string fileExtension() const override

File extension this backend handles (e.g. “.pt”, “.pt2”, “.pte”).

Reimplements: dl::InferenceBackend::fileExtension

function load

virtual bool load(
    std::filesystem::path const & path
) override

Load model weights/program from a file.

Parameters:

  • path Path to the model file.

Return: true if loading succeeded.

Reimplements: dl::InferenceBackend::load

function isLoaded

virtual bool isLoaded() const override

Whether a model is loaded and ready for execution.

Reimplements: dl::InferenceBackend::isLoaded

function loadedPath

virtual std::filesystem::path loadedPath() const override

Get the path of the currently loaded model.

Reimplements: dl::InferenceBackend::loadedPath

function execute

virtual std::vector< at::Tensor > execute(
    std::vector< at::Tensor > const & inputs
) override

Execute the default method (typically “forward”) with ordered inputs.

Parameters:

  • inputs Ordered vector of input tensors.

Exceptions:

  • std::runtime_error if the model is not loaded or execution fails.

Return: Ordered vector of output tensors.

Reimplements: dl::InferenceBackend::execute

All tensors are at::Tensor (libtorch) — the common currency across all backends.

function execute

virtual std::vector< at::Tensor > execute(
    std::string const & method_name,
    std::vector< at::Tensor > const & inputs
) override

Execute a named method with ordered inputs.

Parameters:

  • method_name Name of the method to execute (e.g. “forward”, “encode”).
  • inputs Ordered vector of input tensors.

Exceptions:

  • std::runtime_error if the model is not loaded or execution fails.

Return: Ordered vector of output tensors.

Reimplements: dl::InferenceBackend::execute

Not all backends support multiple methods. Backends that don’t should ignore the method_name and execute the default model.


Updated on 2026-11-13 at 20:25:42 +0000