xref: /aosp_15_r20/external/executorch/extension/pybindings/README.md (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1*523fa7a6SAndroid Build Coastguard Worker# ExecuTorch Python Module (WIP)
2*523fa7a6SAndroid Build Coastguard WorkerThis Python module, named `portable_lib`, provides a set of functions and classes for loading and executing bundled programs. To install it, run the fullowing command:
3*523fa7a6SAndroid Build Coastguard Worker
4*523fa7a6SAndroid Build Coastguard Worker```bash
5*523fa7a6SAndroid Build Coastguard WorkerEXECUTORCH_BUILD_PYBIND=ON \
6*523fa7a6SAndroid Build Coastguard Workerpip install . --no-build-isolation
7*523fa7a6SAndroid Build Coastguard Worker```
8*523fa7a6SAndroid Build Coastguard Worker
9*523fa7a6SAndroid Build Coastguard WorkerOr when installing the rest of dependencies:
10*523fa7a6SAndroid Build Coastguard Worker
11*523fa7a6SAndroid Build Coastguard Worker```bash
12*523fa7a6SAndroid Build Coastguard Workerinstall_requirements.sh --pybind
13*523fa7a6SAndroid Build Coastguard Worker```
14*523fa7a6SAndroid Build Coastguard Worker
15*523fa7a6SAndroid Build Coastguard Worker# Link Backends
16*523fa7a6SAndroid Build Coastguard Worker
17*523fa7a6SAndroid Build Coastguard WorkerYou can link the runtime against some backends to make sure a delegated or partitioned model can still run by Python module successfully:
18*523fa7a6SAndroid Build Coastguard Worker
19*523fa7a6SAndroid Build Coastguard Worker```bash
20*523fa7a6SAndroid Build Coastguard WorkerEXECUTORCH_BUILD_PYBIND=ON \
21*523fa7a6SAndroid Build Coastguard WorkerCMAKE_ARGS="-DEXECUTORCH_BUILD_COREML=ON -DEXECUTORCH_BUILD_MPS=ON -DEXECUTORCH_BUILD_XNNPACK=ON" \
22*523fa7a6SAndroid Build Coastguard Workerpip install . --no-build-isolation
23*523fa7a6SAndroid Build Coastguard Worker```
24*523fa7a6SAndroid Build Coastguard Worker
25*523fa7a6SAndroid Build Coastguard WorkerSimilarly, when installing the rest of dependencies:
26*523fa7a6SAndroid Build Coastguard Worker
27*523fa7a6SAndroid Build Coastguard Worker```bash
28*523fa7a6SAndroid Build Coastguard Workerinstall_requirements.sh --pybind coreml mps xnnpack
29*523fa7a6SAndroid Build Coastguard Worker```
30*523fa7a6SAndroid Build Coastguard Worker
31*523fa7a6SAndroid Build Coastguard Worker## Functions
32*523fa7a6SAndroid Build Coastguard Worker- `_load_for_executorch(path: str, enable_etdump: bool = False)`: Load a module from a file.
33*523fa7a6SAndroid Build Coastguard Worker- `_load_for_executorch_from_buffer(buffer: str, enable_etdump: bool = False)`: Load a module from a buffer.
34*523fa7a6SAndroid Build Coastguard Worker- `_load_for_executorch_from_bundled_program(ptr: str, enable_etdump: bool = False)`: Load a module from a bundled program.
35*523fa7a6SAndroid Build Coastguard Worker- `_load_bundled_program_from_buffer(buffer: str, non_const_pool_size: int = kDEFAULT_BUNDLED_INPUT_POOL_SIZE)`: Load a bundled program from a buffer.
36*523fa7a6SAndroid Build Coastguard Worker- `_dump_profile_results()`: Dump profile results.
37*523fa7a6SAndroid Build Coastguard Worker- `_get_operator_names()`: Get operator names.
38*523fa7a6SAndroid Build Coastguard Worker- `_create_profile_block()`: Create a profile block.
39*523fa7a6SAndroid Build Coastguard Worker- `_reset_profile_results()`: Reset profile results.
40*523fa7a6SAndroid Build Coastguard Worker## Classes
41*523fa7a6SAndroid Build Coastguard Worker### ExecuTorchModule
42*523fa7a6SAndroid Build Coastguard Worker- `load_bundled_input()`: Load bundled input.
43*523fa7a6SAndroid Build Coastguard Worker- `verify_result_with_bundled_expected_output(bundle: str, method_name: str, testset_idx: int, rtol: float = 1e-5, atol: float = 1e-8)`: Verify result with bundled expected output.
44*523fa7a6SAndroid Build Coastguard Worker- `plan_execute()`: Plan and execute.
45*523fa7a6SAndroid Build Coastguard Worker- `run_method()`: Run method.
46*523fa7a6SAndroid Build Coastguard Worker- `forward()`: Forward. This takes a pytree-flattend PyTorch-tensor-based input.
47*523fa7a6SAndroid Build Coastguard Worker- `has_etdump()`: Check if etdump is available.
48*523fa7a6SAndroid Build Coastguard Worker- `write_etdump_result_to_file()`: Write etdump result to a file.
49*523fa7a6SAndroid Build Coastguard Worker- `__call__()`: Call method.
50*523fa7a6SAndroid Build Coastguard Worker### BundledModule
51*523fa7a6SAndroid Build Coastguard WorkerThis class is currently empty and serves as a placeholder for future methods and attributes.
52*523fa7a6SAndroid Build Coastguard Worker## Note
53*523fa7a6SAndroid Build Coastguard WorkerAll functions and methods are guarded by a call guard that redirects `cout` and `cerr` to the Python environment.
54