Skip to content

Commit f20c867

Browse files
JakeStevensmeta-codesync[bot]
authored andcommitted
Add CMake targets for the native Vulkan engine and run_classification runner (#23811)
Summary: Pull Request resolved: #23811 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`. `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. Differential Revision: D125084367
1 parent 21ecd8f commit f20c867

13 files changed

Lines changed: 678 additions & 43 deletions

File tree

‎CMakeLists.txt‎

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

331+
if(EXECUTORCH_BUILD_NATIVE
332+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
333+
OR EXECUTORCH_BUILD_NATIVE_GGML
334+
)
335+
set(JSON_Install ON)
336+
endif()
331337
add_subdirectory(third-party)
332338

333339
# On Apple, pybind11 SHARED extension modules must not link libpython directly.
@@ -838,14 +844,38 @@ elseif(EXECUTORCH_BUILD_CUDA)
838844
"find_dependency(CUDAToolkit)\n"
839845
)
840846
endif()
847+
if(EXECUTORCH_BUILD_NATIVE
848+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
849+
OR EXECUTORCH_BUILD_NATIVE_GGML
850+
)
851+
string(APPEND _executorch_backend_dependencies
852+
"find_dependency(libzip CONFIG)\n"
853+
"find_dependency(nlohmann_json CONFIG)\n"
854+
)
855+
endif()
856+
if(EXECUTORCH_BUILD_NATIVE_GGML)
857+
string(APPEND _executorch_backend_dependencies
858+
"find_dependency(ggml 0.24 CONFIG)\n"
859+
)
860+
endif()
861+
if(EXECUTORCH_BUILD_NATIVE
862+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
863+
OR EXECUTORCH_BUILD_NATIVE_GGML
864+
OR EXECUTORCH_BUILD_VULKAN
865+
)
866+
string(APPEND _executorch_backend_dependencies "find_dependency(Threads)\n")
867+
endif()
841868
configure_file(
842869
tools/cmake/executorch-backend-dependencies.cmake.in
843870
${CMAKE_CURRENT_BINARY_DIR}/executorch-backend-dependencies.cmake @ONLY
844871
)
845872
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/executorch-backend-dependencies.cmake
846873
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ExecuTorch
847874
)
848-
if(EXECUTORCH_BUILD_VULKAN OR EXECUTORCH_BUILD_VGF)
875+
if(EXECUTORCH_BUILD_VULKAN
876+
OR EXECUTORCH_BUILD_VGF
877+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
878+
)
849879
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/vulkan_shared)
850880
endif()
851881

@@ -1686,6 +1716,13 @@ if(EXECUTORCH_BUILD_VULKAN)
16861716
list(APPEND _executorch_backends vulkan_backend vulkan_schema)
16871717
endif()
16881718

1719+
if(EXECUTORCH_BUILD_NATIVE
1720+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
1721+
OR EXECUTORCH_BUILD_NATIVE_GGML
1722+
)
1723+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/native)
1724+
endif()
1725+
16891726
if(EXECUTORCH_BUILD_WEBGPU)
16901727
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/webgpu)
16911728
list(APPEND _executorch_backends webgpu_backend)

‎backends/native/CMakeLists.txt‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 EXECUTORCH_ROOT)
10+
get_filename_component(
11+
EXECUTORCH_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE
12+
)
13+
endif()
14+
15+
include(GNUInstallDirs)
16+
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
17+
18+
if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
19+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
20+
endif()
21+
22+
find_package(Threads REQUIRED)
23+
find_package(libzip CONFIG REQUIRED)
24+
if(NOT TARGET nlohmann_json::nlohmann_json)
25+
find_package(nlohmann_json CONFIG REQUIRED)
26+
endif()
27+
28+
add_subdirectory(runtime)
29+
30+
if(EXECUTORCH_BUILD_NATIVE_VULKAN)
31+
add_subdirectory(runtime/vulkan)
32+
endif()
33+
34+
if(EXECUTORCH_BUILD_NATIVE_GGML)
35+
add_subdirectory(runtime/engine/ggml)
36+
endif()
37+
38+
install(
39+
DIRECTORY runtime/
40+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/backends/native/runtime
41+
FILES_MATCHING
42+
PATTERN "*.h"
43+
PATTERN "test" EXCLUDE
44+
PATTERN "vulkan" EXCLUDE
45+
PATTERN "ggml" EXCLUDE
46+
)
47+
48+
if(EXECUTORCH_BUILD_NATIVE_VISION_RUNNER)
49+
if(NOT EXECUTORCH_BUILD_NATIVE_VULKAN)
50+
message(
51+
FATAL_ERROR
52+
"EXECUTORCH_BUILD_NATIVE_VISION_RUNNER requires EXECUTORCH_BUILD_NATIVE_VULKAN=ON"
53+
)
54+
endif()
55+
add_subdirectory(examples)
56+
endif()
57+
58+
if(EXECUTORCH_BUILD_TESTS)
59+
add_subdirectory(test)
60+
endif()

‎backends/native/README.md‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 requires an installed GGML 0.24 or newer package providing
60+
`ggml::ggml`, built with `GGML_CPU=ON` and `GGML_BACKEND_DL=OFF`. For example,
61+
build the GGML sources from llama.cpp revision
62+
`60081bb2b5b3294165a4d67c5cbeebe74c868014` (the version used by the Buck build):
63+
64+
```sh
65+
cmake -S /path/to/llama.cpp/ggml -B ggml-build \
66+
-DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PWD/ggml-install" \
67+
-DGGML_CPU=ON -DGGML_BACKEND_DL=OFF \
68+
-DGGML_BUILD_TESTS=OFF -DGGML_BUILD_EXAMPLES=OFF
69+
cmake --build ggml-build --parallel
70+
cmake --install ggml-build
71+
cmake -S . -B cmake-out -DCMAKE_BUILD_TYPE=Release \
72+
-DEXECUTORCH_BUILD_NATIVE_GGML=ON -DEXECUTORCH_BUILD_TESTS=ON \
73+
-DCMAKE_PREFIX_PATH="$PWD/ggml-install"
74+
cmake --build cmake-out --target native_api native_ggml_engine native_ggml_engine_test
75+
ctest --test-dir cmake-out -R native_ggml_engine_test --output-on-failure
76+
cmake --install cmake-out --prefix install
77+
```
78+
79+
Applications link `native_api` and `native_ggml_engine`, then create a
80+
`ptn::GgmlEngineHost`. Its default device is CPU. For GPU execution, build GGML
81+
with the desired backend (for example, `GGML_CUDA=ON`, `GGML_HIP=ON`, or
82+
`GGML_METAL=ON`) and set `ptn::GgmlEngineOptions::device` to
83+
`ptn::GgmlDevice::Gpu`. The GGML package supplies the backend libraries and their
84+
dependencies. CPU support is required even when selecting a GPU.
85+
86+
`EXECUTORCH_BUILD_TESTS=ON` adds `native_ggml_engine_test`, which runs the existing
87+
CPU engine tests, including quantized linear execution, graph caching, and
88+
mutable buffers.
89+
90+
## Vision classification runner
91+
92+
Enable `EXECUTORCH_BUILD_NATIVE_VISION_RUNNER=ON` together with
93+
`EXECUTORCH_BUILD_NATIVE_VULKAN=ON` to build `run_classification` from
94+
`examples/vision`. The runner additionally requires gflags; an OSS build uses
95+
the vendored copy, and other builds resolve an installed gflags package.
96+
Running the runner requires a Vulkan device.
97+
98+
```sh
99+
cmake -S . -B cmake-out -DCMAKE_BUILD_TYPE=Release \
100+
-DEXECUTORCH_BUILD_NATIVE_VULKAN=ON \
101+
-DEXECUTORCH_BUILD_NATIVE_VISION_RUNNER=ON
102+
cmake --build cmake-out --target run_classification
103+
cmake --install cmake-out --prefix install
104+
install/bin/run_classification \
105+
--package mv2.ptn --input mv2_input.bin \
106+
--expected mv2_expected.bin --categories categories.txt
107+
```
108+
109+
Export the `.ptn`, input, reference logits, and labels with
110+
[`export.py`](../../examples/native/vision/export.py). The runner prints the
111+
top-5 logits and, when `--expected` is given, exits nonzero if the logits
112+
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 EXECUTORCH_BUILD_NATIVE_VULKAN)
8+
message(
9+
FATAL_ERROR "run_classification requires EXECUTORCH_BUILD_NATIVE_VULKAN=ON"
10+
)
11+
endif()
12+
13+
if(NOT TARGET gflags AND NOT TARGET gflags::gflags)
14+
find_package(gflags REQUIRED)
15+
endif()
16+
if(TARGET gflags::gflags)
17+
set(_native_vision_gflags gflags::gflags)
18+
else()
19+
set(_native_vision_gflags gflags)
20+
endif()
21+
22+
add_executable(run_classification run_classification.cpp)
23+
target_link_libraries(
24+
run_classification
25+
PRIVATE ${_native_vision_gflags} native_engine native_package
26+
native_runner_utils native_runtime native_vulkan_engine
27+
)
28+
target_compile_options(
29+
run_classification PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>
30+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fexceptions>
31+
)
32+
33+
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)