ExecuTorch Runtime Python API Reference¶
The Python executorch.runtime
module wraps the C++ ExecuTorch runtime. It can load and execute serialized .pte
program files: see the Export to ExecuTorch Tutorial for how to convert a PyTorch nn.Module
to an ExecuTorch .pte
program file. Execution accepts and returns torch.Tensor
values, making it a quick way to validate the correctness of the program.
For detailed information on how APIs evolve and the deprecation process, please refer to the ExecuTorch API Life Cycle and Deprecation Policy.
Example usage:
from pathlib import Path
import torch
from executorch.runtime import Verification, Runtime, Program, Method
et_runtime: Runtime = Runtime.get()
program: Program = et_runtime.load_program(
Path("/tmp/program.pte"),
verification=Verification.Minimal,
)
print("Program methods:", program.method_names)
forward: Method = program.load_method("forward")
inputs = (torch.ones(2, 2), torch.ones(2, 2))
outputs = forward.execute(inputs)
print(f"Ran forward({inputs})")
print(f" outputs: {outputs}")
Example output:
Program methods: ('forward', 'forward2')
Ran forward((tensor([[1., 1.],
[1., 1.]]), tensor([[1., 1.],
[1., 1.]])))
outputs: [tensor([[1., 1.],
[1., 1.]])]
- class executorch.runtime.Runtime(*, legacy_module)[source]¶
An instance of the ExecuTorch runtime environment.
This can be used to concurrently load and execute any number of ExecuTorch programs and methods.
- class executorch.runtime.OperatorRegistry(legacy_module)[source]¶
The registry of operators that are available to the runtime.
- property operator_names¶
Returns the names of all registered operators as a set of strings.
- class executorch.runtime.Program(module, data)[source]¶
An ExecuTorch program, loaded from binary PTE data.
This can be used to load the methods/models defined by the program.
- load_method(name)[source]¶
Loads a method from the program.
- Parameters
name – The name of the method to load.
- Returns
The loaded method.
- property method_names¶
Returns method names of the Program as a set of strings.
- class executorch.runtime.Method(method_name, module)[source]¶
An ExecuTorch method, loaded from a Program. This can be used to execute the method with inputs.
- execute(inputs)[source]¶
Executes the method with the given inputs.
- Parameters
inputs – The inputs to the method.
- Returns
The outputs of the method.
- property metadata¶
Gets the metadata for the method.
- Returns
The metadata for the method.