Skip to content

Commit 0382da4

Browse files
JakeStevensmeta-codesync[bot]
authored andcommitted
Add CMake targets for the native Vulkan engine and run_classification runner (pytorch#23811)
Summary: Pull Request resolved: pytorch#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 57b1200 commit 0382da4

13 files changed

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