forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
334 lines (256 loc) · 10.7 KB
/
__init__.py
File metadata and controls
334 lines (256 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
"""
Example usage:
.. code-block:: python
from pathlib import Path
import torch
from executorch.runtime import Runtime, Program, Method
et_runtime: Runtime = Runtime.get()
program: Program = et_runtime.load_program(Path("/tmp/program.pte"))
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:
.. code-block:: text
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:
Note: ETDump requires building ExecuTorch with event tracing enabled
(CMake option ``EXECUTORCH_ENABLE_EVENT_TRACER=ON``).
.. code-block:: python
from pathlib import Path
import os
import torch
from executorch.runtime import Runtime, Program, Method
# Create program with etdump generation enabled
et_runtime: Runtime = Runtime.get()
program: Program = et_runtime.load_program(
Path("/tmp/program.pte"),
enable_etdump=True,
debug_buffer_size=int(1e7), # 10MB buffer to capture all debug info
)
# 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:
.. code-block:: text
ETDump file created: True
Debug file created: True
Directory contents: ['program.pte', 'etdump_output.etdp', 'debug_output.bin']
Example usage with backend and operator introspection:
.. code-block:: python
from executorch.runtime import Runtime
runtime = Runtime.get()
# Check available backends
backends = runtime.backend_registry.registered_backend_names
print(f"Available backends: {backends}")
# Check if a specific backend is available
if runtime.backend_registry.is_available("XnnpackBackend"):
print("XNNPACK backend is available")
# List all registered operators
operators = runtime.operator_registry.operator_names
print(f"Number of registered operators: {len(operators)}")
Example output:
.. code-block:: text
Available backends: ['XnnpackBackend', ...] # Depends on your build configuration
XNNPACK backend is available
Number of registered operators: 247 # Depends on linked kernels
"""
import functools
from pathlib import Path
from types import ModuleType
from typing import Any, BinaryIO, Dict, List, Optional, Sequence, Set, Union
try:
from executorch.extension.pybindings.portable_lib import ( # type: ignore[import-not-found]
ExecuTorchMethod,
ExecuTorchProgram,
MethodMeta,
Verification,
)
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
"Prebuilt <site-packages>/extension/pybindings/_portable_lib.so "
"is not found. Please reinstall ExecuTorch from pip."
) from e
class Method:
"""An ExecuTorch method, loaded from a Program.
This can be used to execute the method with inputs.
"""
def __init__(self, method: ExecuTorchMethod) -> None:
self._method = method
def execute(self, inputs: Sequence[Any]) -> Sequence[Any]:
"""Executes the method with the given inputs.
Args:
inputs: A sequence of input values, typically torch.Tensor objects.
Returns:
A list of output values, typically torch.Tensor objects.
"""
return self._method(inputs)
@property
def metadata(self) -> MethodMeta:
"""Gets the metadata for the method.
The metadata includes information about input and output specifications,
such as tensor shapes, data types, and memory requirements.
Returns:
The MethodMeta object containing method specifications.
"""
return self._method.method_meta()
class Program:
"""An ExecuTorch program, loaded from binary PTE data.
This can be used to load the methods/models defined by the program.
"""
def __init__(self, program: ExecuTorchProgram, data: Optional[bytes]) -> None:
# Hold the data so the program is not freed.
self._data = data
self._program = program
self._methods: Dict[str, Optional[Method]] = {}
# The names of the methods are preemptively added to the dictionary,
# but only map to None until they are loaded.
for method_idx in range(self._program.num_methods()):
self._methods[self._program.get_method_name(method_idx)] = None
@property
def method_names(self) -> Set[str]:
"""Returns method names of the Program as a set of strings."""
return set(self._methods.keys())
def load_method(self, name: str) -> Optional[Method]:
"""Loads a method from the program.
Args:
name: The name of the method to load.
Returns:
The loaded method.
"""
method = self._methods[name]
if method is None:
method = Method(self._program.load_method(name))
self._methods[name] = method
return method
def metadata(self, method_name: str) -> MethodMeta:
"""Gets the metadata for the specified method without loading it.
Args:
method_name: The name of the method.
Returns:
The metadata for the method, including input/output specifications.
"""
return self._program.method_meta(method_name)
def write_etdump_result_to_file(
self, etdump_path: str, debug_buffer_path: str
) -> None:
"""Writes the etdump and debug result to a file.
Args:
etdump_path: The path to the etdump file.
debug_buffer_path: The path to the debug buffer file.
"""
self._program.write_etdump_result_to_file(etdump_path, debug_buffer_path)
class BackendRegistry:
"""The registry of backends that are available to the runtime."""
def __init__(self, legacy_module: ModuleType) -> None:
# TODO: Expose the kernel callables to Python.
self._legacy_module = legacy_module
@property
def registered_backend_names(self) -> List[str]:
"""Returns the names of all registered backends as a list of strings."""
return self._legacy_module._get_registered_backend_names()
def is_available(self, backend_name: str) -> bool:
"""Checks if a specific backend is available in the runtime.
Args:
backend_name: The name of the backend to check (e.g., "XnnpackBackend").
Returns:
True if the backend is available, False otherwise.
"""
return self._legacy_module._is_available(backend_name)
class OperatorRegistry:
"""The registry of operators that are available to the runtime."""
def __init__(self, legacy_module: ModuleType) -> None:
# TODO: Expose the kernel callables to Python.
self._legacy_module = legacy_module
@property
def operator_names(self) -> Set[str]:
"""Returns the names of all registered operators as a set of strings."""
return set(self._legacy_module._get_operator_names())
class Runtime:
"""An instance of the ExecuTorch runtime environment.
This can be used to concurrently load and execute any number of ExecuTorch
programs and methods.
Attributes:
backend_registry: Registry for querying available hardware backends.
operator_registry: Registry for querying available operators/kernels.
"""
@staticmethod
@functools.lru_cache(maxsize=1)
def get() -> "Runtime":
"""Gets the Runtime singleton."""
import executorch.extension.pybindings.portable_lib as legacy_module # type: ignore[import-not-found]
return Runtime(legacy_module=legacy_module)
def __init__(self, *, legacy_module: ModuleType) -> None:
# Public attributes.
self.backend_registry = BackendRegistry(legacy_module)
self.operator_registry = OperatorRegistry(legacy_module)
# Private attributes.
self._legacy_module = legacy_module
def load_program(
self,
data: Union[bytes, bytearray, BinaryIO, Path, str],
*,
verification: Verification = Verification.InternalConsistency,
enable_etdump: bool = False,
debug_buffer_size: int = 0,
) -> Program:
"""Loads an ExecuTorch program from a PTE binary.
Args:
data: The binary program data to load. Can be a file path (str or Path),
bytes/bytearray, or a file-like object.
verification: Level of program verification to perform (Minimal or InternalConsistency).
Default is InternalConsistency.
enable_etdump: If True, enables ETDump profiling for runtime performance analysis.
Default is False.
debug_buffer_size: Size of the debug buffer in bytes for ETDump data.
Only used when enable_etdump=True. Default is 0.
Returns:
The loaded Program instance.
"""
if isinstance(data, (Path, str)):
p = self._legacy_module._load_program(
str(data),
enable_etdump=enable_etdump,
debug_buffer_size=debug_buffer_size,
program_verification=verification,
)
return Program(p, data=None)
elif isinstance(data, bytes):
data_bytes = data
elif isinstance(data, bytearray):
data_bytes = bytes(data)
elif hasattr(data, "read"):
# File-like object with read() method
data_bytes = data.read()
else:
raise TypeError(
f"Expected data to be bytes, bytearray, a path to a .pte file, or a file-like object, but got {type(data).__name__}."
)
p = self._legacy_module._load_program_from_buffer(
data_bytes,
enable_etdump=enable_etdump,
debug_buffer_size=debug_buffer_size,
program_verification=verification,
)
return Program(p, data=data_bytes)