1212 from pathlib import Path
1313
1414 import torch
15- from executorch.runtime import Verification, Runtime, Program, Method
15+ from executorch.runtime import Runtime, Program, Method
1616
1717 et_runtime: Runtime = Runtime.get()
18- program: Program = et_runtime.load_program(
19- Path("/tmp/program.pte"),
20- verification=Verification.Minimal,
21- )
18+ program: Program = et_runtime.load_program(Path("/tmp/program.pte"))
2219 print("Program methods:", program.method_names)
2320 forward: Method = program.load_method("forward")
2421
4037
4138Example usage with ETDump generation:
4239
40+ Note: ETDump requires building ExecuTorch with event tracing enabled
41+ (CMake option ``EXECUTORCH_ENABLE_EVENT_TRACER=ON``).
42+
4343.. code-block:: python
4444
4545 from pathlib import Path
4646 import os
4747
4848 import torch
49- from executorch.runtime import Verification, Runtime, Program, Method
49+ from executorch.runtime import Runtime, Program, Method
5050
5151 # Create program with etdump generation enabled
5252 et_runtime: Runtime = Runtime.get()
5353 program: Program = et_runtime.load_program(
5454 Path("/tmp/program.pte"),
55- verification=Verification.Minimal,
5655 enable_etdump=True,
57- debug_buffer_size=1e7, # A large buffer size to ensure that all debug info is captured
56+ debug_buffer_size=int( 1e7), # 10MB buffer to capture all debug info
5857 )
5958
6059 # Load method and execute
7675
7776.. code-block:: text
7877
79- Program methods: {'forward'}
8078 ETDump file created: True
8179 Debug file created: True
8280 Directory contents: ['program.pte', 'etdump_output.etdp', 'debug_output.bin']
81+
82+ Example usage with backend and operator introspection:
83+
84+ .. code-block:: python
85+
86+ from executorch.runtime import Runtime
87+
88+ runtime = Runtime.get()
89+
90+ # Check available backends
91+ backends = runtime.backend_registry.registered_backend_names
92+ print(f"Available backends: {backends}")
93+
94+ # Check if a specific backend is available
95+ if runtime.backend_registry.is_available("XnnpackBackend"):
96+ print("XNNPACK backend is available")
97+
98+ # List all registered operators
99+ operators = runtime.operator_registry.operator_names
100+ print(f"Number of registered operators: {len(operators)}")
101+
102+ Example output:
103+
104+ .. code-block:: text
105+
106+ Available backends: ['XnnpackBackend', ...] # Depends on your build configuration
107+ XNNPACK backend is available
108+ Number of registered operators: 247 # Depends on linked kernels
83109"""
84110
85111import functools
@@ -113,19 +139,22 @@ def execute(self, inputs: Sequence[Any]) -> Sequence[Any]:
113139 """Executes the method with the given inputs.
114140
115141 Args:
116- inputs: The inputs to the method .
142+ inputs: A sequence of input values, typically torch.Tensor objects .
117143
118144 Returns:
119- The outputs of the method .
145+ A list of output values, typically torch.Tensor objects .
120146 """
121147 return self ._method (inputs )
122148
123149 @property
124150 def metadata (self ) -> MethodMeta :
125151 """Gets the metadata for the method.
126152
153+ The metadata includes information about input and output specifications,
154+ such as tensor shapes, data types, and memory requirements.
155+
127156 Returns:
128- The metadata for the method.
157+ The MethodMeta object containing method specifications .
129158 """
130159 return self ._method .method_meta ()
131160
@@ -148,9 +177,7 @@ def __init__(self, program: ExecuTorchProgram, data: Optional[bytes]) -> None:
148177
149178 @property
150179 def method_names (self ) -> Set [str ]:
151- """
152- Returns method names of the `Program` as a set of strings.
153- """
180+ """Returns method names of the Program as a set of strings."""
154181 return set (self ._methods .keys ())
155182
156183 def load_method (self , name : str ) -> Optional [Method ]:
@@ -170,13 +197,13 @@ def load_method(self, name: str) -> Optional[Method]:
170197 return method
171198
172199 def metadata (self , method_name : str ) -> MethodMeta :
173- """Gets the metadata for the specified method.
200+ """Gets the metadata for the specified method without loading it .
174201
175202 Args:
176203 method_name: The name of the method.
177204
178205 Returns:
179- The outputs of the method.
206+ The metadata for the method, including input/output specifications .
180207 """
181208 return self ._program .method_meta (method_name )
182209
@@ -201,14 +228,17 @@ def __init__(self, legacy_module: ModuleType) -> None:
201228
202229 @property
203230 def registered_backend_names (self ) -> List [str ]:
204- """
205- Returns the names of all registered backends as a list of strings.
206- """
231+ """Returns the names of all registered backends as a list of strings."""
207232 return self ._legacy_module ._get_registered_backend_names ()
208233
209234 def is_available (self , backend_name : str ) -> bool :
210- """
211- Returns the names of all registered backends as a list of strings.
235+ """Checks if a specific backend is available in the runtime.
236+
237+ Args:
238+ backend_name: The name of the backend to check (e.g., "XnnpackBackend").
239+
240+ Returns:
241+ True if the backend is available, False otherwise.
212242 """
213243 return self ._legacy_module ._is_available (backend_name )
214244
@@ -222,9 +252,7 @@ def __init__(self, legacy_module: ModuleType) -> None:
222252
223253 @property
224254 def operator_names (self ) -> Set [str ]:
225- """
226- Returns the names of all registered operators as a set of strings.
227- """
255+ """Returns the names of all registered operators as a set of strings."""
228256 return set (self ._legacy_module ._get_operator_names ())
229257
230258
@@ -233,6 +261,10 @@ class Runtime:
233261
234262 This can be used to concurrently load and execute any number of ExecuTorch
235263 programs and methods.
264+
265+ Attributes:
266+ backend_registry: Registry for querying available hardware backends.
267+ operator_registry: Registry for querying available operators/kernels.
236268 """
237269
238270 @staticmethod
@@ -261,11 +293,17 @@ def load_program(
261293 """Loads an ExecuTorch program from a PTE binary.
262294
263295 Args:
264- data: The binary program data to load; typically PTE data.
265- verification: level of program verification to perform.
296+ data: The binary program data to load. Can be a file path (str or Path),
297+ bytes/bytearray, or a file-like object.
298+ verification: Level of program verification to perform (Minimal or InternalConsistency).
299+ Default is InternalConsistency.
300+ enable_etdump: If True, enables ETDump profiling for runtime performance analysis.
301+ Default is False.
302+ debug_buffer_size: Size of the debug buffer in bytes for ETDump data.
303+ Only used when enable_etdump=True. Default is 0.
266304
267305 Returns:
268- The loaded program .
306+ The loaded Program instance .
269307 """
270308 if isinstance (data , (Path , str )):
271309 p = self ._legacy_module ._load_program (
@@ -275,20 +313,21 @@ def load_program(
275313 program_verification = verification ,
276314 )
277315 return Program (p , data = None )
278- elif isinstance (data , BinaryIO ):
279- data_bytes = data .read ()
280- elif isinstance (data , bytearray ):
281- data_bytes = bytes (data )
282316 elif isinstance (data , bytes ):
283317 data_bytes = data
318+ elif isinstance (data , bytearray ):
319+ data_bytes = bytes (data )
320+ elif hasattr (data , "read" ):
321+ # File-like object with read() method
322+ data_bytes = data .read ()
284323 else :
285324 raise TypeError (
286325 f"Expected data to be bytes, bytearray, a path to a .pte file, or a file-like object, but got { type (data ).__name__ } ."
287326 )
288327 p = self ._legacy_module ._load_program_from_buffer (
289328 data_bytes ,
290- enable_etdump = False ,
291- debug_buffer_size = 0 ,
329+ enable_etdump = enable_etdump ,
330+ debug_buffer_size = debug_buffer_size ,
292331 program_verification = verification ,
293332 )
294333
0 commit comments