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'}
Ran forward((tensor([[1., 1.],
[1., 1.]]), tensor([[1., 1.],
[1., 1.]])))
outputs: [tensor([[2., 2.],
[2., 2.]])]
Example usage with ETDump generation:
from pathlib import Path
import os
import torch
from executorch.runtime import Verification, Runtime, Program, Method
# Create program with etdump generation enabled
et_runtime: Runtime = Runtime.get()
program: Program = et_runtime.load_program(
Path("/tmp/program.pte"),
verification=Verification.Minimal,
enable_etdump=True,
debug_buffer_size=1e7, # A large buffer size to ensure that all debug info is captured
)
# Load method and execute
forward: Method = program.load_method("forward")
inputs = (torch.ones(2, 2), torch.ones(2, 2))
outputs = forward.execute(inputs)
# Write etdump result to file
etdump_file = "/tmp/etdump_output.etdp"
debug_file = "/tmp/debug_output.bin"
program.write_etdump_result_to_file(etdump_file, debug_file)
# Check that files were created
print(f"ETDump file created: {os.path.exists(etdump_file)}")
print(f"Debug file created: {os.path.exists(debug_file)}")
print("Directory contents:", os.listdir("/tmp"))
Example output:
Program methods: {'forward'}
ETDump file created: True
Debug file created: True
Directory contents: ['program.pte', 'etdump_output.etdp', 'debug_output.bin']
- 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.
- load_program(data, *, verification=<Verification.InternalConsistency: 1>, enable_etdump=False, debug_buffer_size=0)[source]¶
Loads an ExecuTorch program from a PTE binary.
- Parameters
data – The binary program data to load; typically PTE data.
verification – level of program verification to perform.
- Returns
The loaded program.
- 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(program, 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)[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.