|
| 1 | +# Native runtime CMake targets |
| 2 | + |
| 3 | +Enable `EXECUTORCH_BUILD_NATIVE=ON` to build the shared native runtime, or |
| 4 | +`EXECUTORCH_BUILD_NATIVE_VULKAN=ON` to build it together with the Vulkan engine. |
| 5 | +`EXECUTORCH_BUILD_NATIVE_GGML=ON` builds it together with the GGML engine. |
| 6 | +All three options default to `OFF`; the engine options can be enabled together. |
| 7 | + |
| 8 | +The native runtime requires C++20, a host `flatc`, the vendored FlatBuffers and |
| 9 | +nlohmann JSON headers, and an installed libzip package with the `libzip::zip` |
| 10 | +CMake target. Set `CMAKE_PREFIX_PATH` to the libzip installation if necessary. |
| 11 | +Native Vulkan also requires the Vulkan submodules and `glslc`, as described in |
| 12 | +the [Vulkan build instructions](../vulkan/README.md). |
| 13 | + |
| 14 | +From the ExecuTorch checkout: |
| 15 | + |
| 16 | +```sh |
| 17 | +cmake -S . -B cmake-out -DCMAKE_BUILD_TYPE=Release \ |
| 18 | + -DEXECUTORCH_BUILD_NATIVE_VULKAN=ON |
| 19 | +cmake --build cmake-out --target native_api native_vulkan_engine |
| 20 | +cmake --install cmake-out --prefix install |
| 21 | +``` |
| 22 | + |
| 23 | +## Shared targets |
| 24 | + |
| 25 | +| Target | Contents | |
| 26 | +| --- | --- | |
| 27 | +| `native_graph` | Graph IR, graph utilities, and memory planning | |
| 28 | +| `native_package` | PTN archive, safetensors, and owned byte readers | |
| 29 | +| `native_runtime` | Program deserialization, validation, and method metadata | |
| 30 | +| `native_engine` | Engine host, context, and executable interfaces | |
| 31 | +| `native_api` | Engine-independent `Model`, `Session`, and tensor API | |
| 32 | +| `native_vulkan_passes` | Native graph rewrites for Vulkan | |
| 33 | +| `native_vulkan_engine` | Vulkan host and executable implementation | |
| 34 | +| `native_ggml_engine` | GGML host, graph lowering, and executable implementation | |
| 35 | +| `vulkan_compute_api` | Vulkan device, allocator, and command API | |
| 36 | +| `vulkan_graph_runtime` | ComputeGraph, operator registrations, and shaders | |
| 37 | + |
| 38 | +The common native targets have no dependency on the ExecuTorch executor or a |
| 39 | +specific engine. New engines link `native_engine` and implement its interfaces. |
| 40 | +Applications link `native_api` and the engines they use. The Vulkan engine and |
| 41 | +the existing `vulkan_backend` delegate share `vulkan_graph_runtime`; its static |
| 42 | +operator and shader registrations are retained automatically when linking. |
| 43 | + |
| 44 | +All library targets are installed in `ExecuTorchTargets`. For example: |
| 45 | + |
| 46 | +```cmake |
| 47 | +find_package(executorch CONFIG REQUIRED) |
| 48 | +target_link_libraries(my_app PRIVATE native_api native_vulkan_engine) |
| 49 | +``` |
| 50 | + |
| 51 | +With `EXECUTORCH_BUILD_TESTS=ON`, `native_runtime_test` covers the shared runtime |
| 52 | +and API. `native_vulkan_passes_test` covers the Vulkan graph rewrites without |
| 53 | +requiring a GPU. When the vision runner below is enabled, |
| 54 | +`native_runner_utils_test` covers its shared runner utilities. Run these with |
| 55 | +`ctest --test-dir cmake-out -R native_`. |
| 56 | + |
| 57 | +## GGML engine |
| 58 | + |
| 59 | +Native GGML builds the GGML subtree of the llama.cpp submodule at |
| 60 | +`backends/native/third-party/llama.cpp`, pinned to |
| 61 | +`620d2225cc436ab0b001e3200e590e3b286ef9ab`. This matches the Buck dependency |
| 62 | +used to validate this CMake integration. The build requires `GGML_CPU=ON` |
| 63 | +and `GGML_BACKEND_DL=OFF`, which are the defaults. |
| 64 | + |
| 65 | +```sh |
| 66 | +git submodule update --init backends/native/third-party/llama.cpp |
| 67 | +cmake -S . -B cmake-out -DCMAKE_BUILD_TYPE=Release \ |
| 68 | + -DEXECUTORCH_BUILD_NATIVE_GGML=ON -DEXECUTORCH_BUILD_TESTS=ON |
| 69 | +cmake --build cmake-out --target native_api native_ggml_engine native_ggml_engine_test |
| 70 | +ctest --test-dir cmake-out -R native_ggml_engine_test --output-on-failure |
| 71 | +cmake --install cmake-out --prefix install |
| 72 | +``` |
| 73 | + |
| 74 | +Applications link `native_api` and `native_ggml_engine`, then create a |
| 75 | +`ptn::GgmlEngineHost`. Its default device is CPU. For GPU execution, configure |
| 76 | +ExecuTorch with the desired GGML backend (for example, `GGML_CUDA=ON`, |
| 77 | +`GGML_HIP=ON`, or `GGML_METAL=ON`) and set `ptn::GgmlEngineOptions::device` to |
| 78 | +`ptn::GgmlDevice::Gpu`. CPU support is required even when selecting a GPU. |
| 79 | + |
| 80 | +The CMake installation includes the GGML libraries and package configuration. |
| 81 | +It also copies the submodule's upstream MIT license, including its copyright |
| 82 | +notice, to `share/licenses/executorch/ggml/LICENSE` under the installation |
| 83 | +prefix (or the corresponding `CMAKE_INSTALL_DATADIR` location). |
| 84 | + |
| 85 | +`EXECUTORCH_BUILD_TESTS=ON` adds `native_ggml_engine_test`, which runs the existing |
| 86 | +CPU engine tests, including quantized linear execution, graph caching, and |
| 87 | +mutable buffers. |
| 88 | + |
| 89 | +## Vision classification runner |
| 90 | + |
| 91 | +Enable `EXECUTORCH_BUILD_NATIVE_VISION_RUNNER=ON` together with |
| 92 | +`EXECUTORCH_BUILD_NATIVE_VULKAN=ON` to build `run_classification` from |
| 93 | +`examples/vision`. The runner additionally requires gflags; an OSS build uses |
| 94 | +the vendored copy, and other builds resolve an installed gflags package. |
| 95 | +Running the runner requires a Vulkan device. |
| 96 | + |
| 97 | +```sh |
| 98 | +cmake -S . -B cmake-out -DCMAKE_BUILD_TYPE=Release \ |
| 99 | + -DEXECUTORCH_BUILD_NATIVE_VULKAN=ON \ |
| 100 | + -DEXECUTORCH_BUILD_NATIVE_VISION_RUNNER=ON |
| 101 | +cmake --build cmake-out --target run_classification |
| 102 | +cmake --install cmake-out --prefix install |
| 103 | +install/bin/run_classification \ |
| 104 | + --package mv2.ptn --input mv2_input.bin \ |
| 105 | + --expected mv2_expected.bin --categories categories.txt |
| 106 | +``` |
| 107 | + |
| 108 | +Export the `.ptn`, input, reference logits, and labels with |
| 109 | +[`export.py`](../../examples/native/vision/export.py). The runner prints the |
| 110 | +top-5 logits and, when `--expected` is given, exits nonzero if the logits |
| 111 | +differ from the reference beyond `--rtol`/`--atol` (both default to `1e-3`). |
0 commit comments