Skip to content

Commit d46a886

Browse files
authored
Add CMake targets for Native, Vulkan engine, ggml engine, and run_classification runner (#23811)
Summary: Add engine-independent CMake libraries for the native graph, PTN package reader, program runtime, engine interface, and standalone API. `EXECUTORCH_BUILD_NATIVE` enables the common libraries; `EXECUTORCH_BUILD_NATIVE_VULKAN` also enables `native_vulkan_engine`. Similarly, `EXECUTORCH_BUILD_NATIVE_GGML` enables the ggml engine. `EXECUTORCH_BUILD_NATIVE_VISION_RUNNER` (requires `EXECUTORCH_BUILD_NATIVE_VULKAN`) additionally builds the `examples/vision` classification runner as `run_classification` plus its `native_runner_utils` library. The runner links `native_vulkan_engine` and gflags and installs to `bin/`. Its existing `test_runner_utils.cpp` coverage runs as `native_runner_utils_test` under `EXECUTORCH_BUILD_TESTS`. Authored with Codex and Muse Code. Reviewed By: digantdesai Differential Revision: D125084367 Pull Request resolved: #23811
1 parent 9d5f6bc commit d46a886

13 files changed

Lines changed: 669 additions & 45 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ else()
328328
set(BUILD_TESTING OFF)
329329
endif()
330330

331+
if(EXECUTORCH_BUILD_NATIVE)
332+
set(JSON_Install ON)
333+
endif()
331334
add_subdirectory(third-party)
332335

333336
# Size-optimized builds disable exceptions, RTTI, and unwind tables.
@@ -781,14 +784,33 @@ elseif(EXECUTORCH_BUILD_CUDA)
781784
"find_dependency(CUDAToolkit)\n"
782785
)
783786
endif()
787+
if(EXECUTORCH_BUILD_NATIVE)
788+
string(APPEND _executorch_backend_dependencies
789+
"find_dependency(libzip CONFIG)\n"
790+
"find_dependency(nlohmann_json CONFIG)\n" "find_dependency(Threads)\n"
791+
)
792+
endif()
793+
if(EXECUTORCH_BUILD_NATIVE_GGML)
794+
string(
795+
APPEND _executorch_backend_dependencies
796+
"find_dependency(ggml 0.27 CONFIG)\n" "if(NOT TARGET executorch_ggml)\n"
797+
" add_library(executorch_ggml ALIAS ggml::ggml)\n" "endif()\n"
798+
)
799+
endif()
800+
if(EXECUTORCH_BUILD_VULKAN)
801+
string(APPEND _executorch_backend_dependencies "find_dependency(Threads)\n")
802+
endif()
784803
configure_file(
785804
tools/cmake/executorch-backend-dependencies.cmake.in
786805
${CMAKE_CURRENT_BINARY_DIR}/executorch-backend-dependencies.cmake @ONLY
787806
)
788807
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/executorch-backend-dependencies.cmake
789808
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ExecuTorch
790809
)
791-
if(EXECUTORCH_BUILD_VULKAN OR EXECUTORCH_BUILD_VGF)
810+
if(EXECUTORCH_BUILD_VULKAN
811+
OR EXECUTORCH_BUILD_VGF
812+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
813+
)
792814
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/vulkan_shared)
793815
endif()
794816

@@ -1593,6 +1615,10 @@ if(EXECUTORCH_BUILD_VULKAN)
15931615
list(APPEND _executorch_backends vulkan_backend vulkan_schema)
15941616
endif()
15951617

1618+
if(EXECUTORCH_BUILD_NATIVE)
1619+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/native)
1620+
endif()
1621+
15961622
if(EXECUTORCH_BUILD_WEBGPU)
15971623
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/webgpu)
15981624
list(APPEND _executorch_backends webgpu_backend)

‎backends/native/CMakeLists.txt‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
cmake_minimum_required(VERSION 3.24)
8+
9+
if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
10+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
11+
endif()
12+
13+
find_package(Threads REQUIRED)
14+
find_package(libzip CONFIG REQUIRED)
15+
if(NOT TARGET nlohmann_json::nlohmann_json)
16+
find_package(nlohmann_json CONFIG REQUIRED)
17+
endif()
18+
19+
add_subdirectory(runtime)
20+
21+
if(EXECUTORCH_BUILD_NATIVE_VULKAN)
22+
add_subdirectory(runtime/vulkan)
23+
endif()
24+
25+
if(EXECUTORCH_BUILD_NATIVE_GGML)
26+
add_subdirectory(runtime/engine/ggml)
27+
endif()
28+
29+
install(
30+
DIRECTORY runtime/
31+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/backends/native/runtime
32+
FILES_MATCHING
33+
PATTERN "*.h"
34+
PATTERN "test" EXCLUDE
35+
PATTERN "vulkan" EXCLUDE
36+
PATTERN "ggml" EXCLUDE
37+
)
38+
39+
if(EXECUTORCH_BUILD_NATIVE_VISION_RUNNER)
40+
add_subdirectory(examples)
41+
endif()
42+
43+
if(EXECUTORCH_BUILD_TESTS)
44+
add_subdirectory(test)
45+
endif()

‎backends/native/README.md‎

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,116 @@ An engine rejects a method whose semantics it cannot fully honor.
5757
| Vulkan | `runtime/vulkan/` | Builds on the ET-VK `ComputeGraph` and shaders. |
5858
| ggml | `runtime/engine/ggml/` | CPU, CUDA, HIP, and Metal variants. |
5959

60-
The runtime currently builds only with Meta's internal Buck setup; there is no
61-
open-source build yet.
60+
## Building with CMake
61+
62+
Enable `EXECUTORCH_BUILD_NATIVE=ON` to build the shared native runtime. With it,
63+
`EXECUTORCH_BUILD_NATIVE_VULKAN=ON` adds the Vulkan engine and
64+
`EXECUTORCH_BUILD_NATIVE_GGML=ON` adds the GGML engine. All three options default
65+
to `OFF`; the engine options are ignored unless `EXECUTORCH_BUILD_NATIVE=ON` and
66+
can be enabled together.
67+
68+
The native runtime requires C++20, a host `flatc`, the vendored FlatBuffers and
69+
nlohmann JSON headers, and an installed libzip package with the `libzip::zip`
70+
CMake target. Set `CMAKE_PREFIX_PATH` to the libzip installation if necessary.
71+
Native Vulkan also requires the Vulkan submodules and `glslc`, as described in
72+
the [Vulkan build instructions](../vulkan/README.md).
73+
74+
From the ExecuTorch checkout:
75+
76+
```sh
77+
cmake -S . -B cmake-out -DCMAKE_BUILD_TYPE=Release \
78+
-DEXECUTORCH_BUILD_NATIVE=ON -DEXECUTORCH_BUILD_NATIVE_VULKAN=ON
79+
cmake --build cmake-out --target native_api native_vulkan_engine
80+
cmake --install cmake-out --prefix install
81+
```
82+
83+
### Shared targets
84+
85+
| Target | Contents |
86+
| --- | --- |
87+
| `native_graph` | Graph IR, graph utilities, and memory planning |
88+
| `native_package` | PTN archive, safetensors, and owned byte readers |
89+
| `native_runtime` | Program deserialization, validation, and method metadata |
90+
| `native_engine` | Engine host, context, and executable interfaces |
91+
| `native_api` | Engine-independent `Model`, `Session`, and tensor API |
92+
| `native_vulkan_passes` | Native graph rewrites for Vulkan |
93+
| `native_vulkan_engine` | Vulkan host and executable implementation |
94+
| `native_ggml_engine` | GGML host, graph lowering, and executable implementation |
95+
| `vulkan_compute_api` | Vulkan device, allocator, and command API |
96+
| `vulkan_graph_runtime` | ComputeGraph, operator registrations, and shaders |
97+
98+
The common native targets have no dependency on the ExecuTorch executor or a
99+
specific engine. New engines link `native_engine` and implement its interfaces.
100+
Applications link `native_api` and the engines they use. The Vulkan engine and
101+
the existing `vulkan_backend` delegate share `vulkan_graph_runtime`; its static
102+
operator and shader registrations are retained automatically when linking.
103+
104+
All library targets are installed in `ExecuTorchTargets`. For example:
105+
106+
```cmake
107+
find_package(executorch CONFIG REQUIRED)
108+
target_link_libraries(my_app PRIVATE native_api native_vulkan_engine)
109+
```
110+
111+
With `EXECUTORCH_BUILD_TESTS=ON`, `native_runtime_test` covers the shared runtime
112+
and API. `native_vulkan_passes_test` covers the Vulkan graph rewrites without
113+
requiring a GPU. When the vision runner below is enabled,
114+
`native_runner_utils_test` covers its shared runner utilities. Run these with
115+
`ctest --test-dir cmake-out -R native_`.
116+
117+
### GGML engine
118+
119+
Native GGML builds the GGML subtree of the llama.cpp submodule at
120+
`backends/native/third-party/llama.cpp`, pinned to
121+
`620d2225cc436ab0b001e3200e590e3b286ef9ab`. This matches the Buck dependency
122+
used to validate this CMake integration. The build requires `GGML_CPU=ON`
123+
and `GGML_BACKEND_DL=OFF`, which are the defaults.
124+
125+
```sh
126+
git submodule update --init backends/native/third-party/llama.cpp
127+
cmake -S . -B cmake-out -DCMAKE_BUILD_TYPE=Release \
128+
-DEXECUTORCH_BUILD_NATIVE=ON -DEXECUTORCH_BUILD_NATIVE_GGML=ON \
129+
-DEXECUTORCH_BUILD_TESTS=ON
130+
cmake --build cmake-out --target native_api native_ggml_engine native_ggml_engine_test
131+
ctest --test-dir cmake-out -R native_ggml_engine_test --output-on-failure
132+
cmake --install cmake-out --prefix install
133+
```
134+
135+
Applications link `native_api` and `native_ggml_engine`, then create a
136+
`ptn::GgmlEngineHost`. Its default device is CPU. For GPU execution, configure
137+
ExecuTorch with the desired GGML backend (for example, `GGML_CUDA=ON`,
138+
`GGML_HIP=ON`, or `GGML_METAL=ON`) and set `ptn::GgmlEngineOptions::device` to
139+
`ptn::GgmlDevice::Gpu`. CPU support is required even when selecting a GPU.
140+
141+
The CMake installation includes the GGML libraries and package configuration.
142+
It also copies the submodule's upstream MIT license, including its copyright
143+
notice, to `share/licenses/executorch/ggml/LICENSE` under the installation
144+
prefix (or the corresponding `CMAKE_INSTALL_DATADIR` location).
145+
146+
`EXECUTORCH_BUILD_TESTS=ON` adds `native_ggml_engine_test`, which runs the existing
147+
CPU engine tests, including quantized linear execution, graph caching, and
148+
mutable buffers.
149+
150+
### Vision classification runner
151+
152+
Enable `EXECUTORCH_BUILD_NATIVE_VISION_RUNNER=ON` together with
153+
`EXECUTORCH_BUILD_NATIVE_VULKAN=ON` to build `run_classification` from
154+
`examples/vision`. The runner additionally requires gflags; an OSS build uses
155+
the vendored copy, and other builds resolve an installed gflags package.
156+
Running the runner requires a Vulkan device.
157+
158+
```sh
159+
cmake -S . -B cmake-out -DCMAKE_BUILD_TYPE=Release \
160+
-DEXECUTORCH_BUILD_NATIVE=ON -DEXECUTORCH_BUILD_NATIVE_VULKAN=ON \
161+
-DEXECUTORCH_BUILD_NATIVE_VISION_RUNNER=ON
162+
cmake --build cmake-out --target run_classification
163+
cmake --install cmake-out --prefix install
164+
install/bin/run_classification \
165+
--package mv2.ptn --input mv2_input.bin \
166+
--expected mv2_expected.bin --categories categories.txt
167+
```
168+
169+
Export the `.ptn`, input, reference logits, and labels with
170+
[`export.py`](../../examples/native/vision/export.py). The runner prints the
171+
top-5 logits and, when `--expected` is given, exits nonzero if the logits
172+
differ from the reference beyond `--rtol`/`--atol` (both default to `1e-3`).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
add_subdirectory(runner_utils)
8+
add_subdirectory(vision)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
add_library(native_runner_utils STATIC runner_utils.cpp)
8+
target_compile_features(native_runner_utils PUBLIC cxx_std_20)
9+
target_compile_options(
10+
native_runner_utils PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>
11+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fexceptions>
12+
)
13+
target_include_directories(
14+
native_runner_utils PUBLIC $<BUILD_INTERFACE:${EXECUTORCH_ROOT}/..>
15+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
if(NOT TARGET gflags AND NOT TARGET gflags::gflags)
8+
find_package(gflags REQUIRED)
9+
endif()
10+
if(TARGET gflags::gflags)
11+
set(_native_vision_gflags gflags::gflags)
12+
else()
13+
set(_native_vision_gflags gflags)
14+
endif()
15+
16+
add_executable(run_classification run_classification.cpp)
17+
target_link_libraries(
18+
run_classification
19+
PRIVATE ${_native_vision_gflags} native_engine native_package
20+
native_runner_utils native_runtime native_vulkan_engine
21+
)
22+
target_compile_options(
23+
run_classification PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>
24+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fexceptions>
25+
)
26+
27+
install(TARGETS run_classification RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
set(_native_schema_dir
8+
${CMAKE_CURRENT_BINARY_DIR}/include/executorch/backends/native/runtime
9+
)
10+
set(_native_schema_header ${_native_schema_dir}/native_graph_generated.h)
11+
add_custom_command(
12+
OUTPUT ${_native_schema_header}
13+
COMMAND ${CMAKE_COMMAND} -E make_directory ${_native_schema_dir}
14+
COMMAND
15+
flatc --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
16+
${_native_schema_dir}
17+
${CMAKE_CURRENT_SOURCE_DIR}/../serialization/native_graph.fbs
18+
DEPENDS flatc ${CMAKE_CURRENT_SOURCE_DIR}/../serialization/native_graph.fbs
19+
COMMENT "Generating native graph schema"
20+
VERBATIM
21+
)
22+
add_library(native_graph_schema INTERFACE ${_native_schema_header})
23+
target_include_directories(
24+
native_graph_schema
25+
INTERFACE
26+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
27+
$<BUILD_INTERFACE:${EXECUTORCH_ROOT}/third-party/flatbuffers/include>
28+
)
29+
add_custom_target(native_graph_schema_gen DEPENDS ${_native_schema_header})
30+
add_dependencies(native_graph_schema native_graph_schema_gen)
31+
32+
add_library(
33+
native_graph
34+
graph/Argument.cpp
35+
graph/Graph.cpp
36+
graph/MemoryPlanning.cpp
37+
graph/Node.cpp
38+
graph/Scalar.cpp
39+
graph/TensorMeta.cpp
40+
graph/Value.cpp
41+
graph/utils/GraphUtils.cpp
42+
)
43+
44+
add_library(native_reference_gguf_kquant utils/ReferenceGgufKQuant.cpp)
45+
target_link_libraries(native_reference_gguf_kquant PRIVATE Threads::Threads)
46+
47+
add_library(
48+
native_package deserialize/OwnedBytes.cpp deserialize/Package.cpp
49+
deserialize/SafeTensorsReader.cpp deserialize/ZipReader.cpp
50+
)
51+
target_link_libraries(
52+
native_package
53+
PUBLIC native_graph
54+
PRIVATE libzip::zip nlohmann_json::nlohmann_json Threads::Threads
55+
)
56+
57+
add_library(
58+
native_runtime Deserialize.cpp Program.cpp MethodMeta.cpp TensorInfo.cpp
59+
Validation.cpp
60+
)
61+
target_link_libraries(
62+
native_runtime
63+
PUBLIC native_graph native_package
64+
PRIVATE $<BUILD_INTERFACE:native_graph_schema> Threads::Threads
65+
)
66+
add_dependencies(native_runtime native_graph_schema_gen)
67+
68+
add_library(native_engine engine/Engine.cpp)
69+
target_link_libraries(native_engine PUBLIC native_runtime)
70+
71+
add_library(native_api api/Model.cpp)
72+
target_link_libraries(native_api PUBLIC native_engine)
73+
74+
foreach(target native_graph native_reference_gguf_kquant native_package
75+
native_runtime native_engine native_api
76+
)
77+
add_library(executorch::${target} ALIAS ${target})
78+
target_compile_features(${target} PUBLIC cxx_std_20)
79+
target_compile_options(
80+
${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>
81+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fexceptions>
82+
)
83+
target_include_directories(
84+
${target} PUBLIC $<BUILD_INTERFACE:${EXECUTORCH_ROOT}/..>
85+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
86+
)
87+
endforeach()
88+
89+
install(
90+
TARGETS native_graph native_reference_gguf_kquant native_package
91+
native_runtime native_engine native_api
92+
EXPORT ExecuTorchTargets
93+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
94+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
95+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
96+
)

0 commit comments

Comments
 (0)