Skip to content

Commit 90ec82f

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 0641030 commit 90ec82f

13 files changed

Lines changed: 700 additions & 43 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 40 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
# Size-optimized builds disable exceptions, RTTI, and unwind tables.
@@ -781,14 +787,40 @@ elseif(EXECUTORCH_BUILD_CUDA)
781787
"find_dependency(CUDAToolkit)\n"
782788
)
783789
endif()
790+
if(EXECUTORCH_BUILD_NATIVE
791+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
792+
OR EXECUTORCH_BUILD_NATIVE_GGML
793+
)
794+
string(APPEND _executorch_backend_dependencies
795+
"find_dependency(libzip CONFIG)\n"
796+
"find_dependency(nlohmann_json CONFIG)\n"
797+
)
798+
endif()
799+
if(EXECUTORCH_BUILD_NATIVE_GGML)
800+
string(
801+
APPEND _executorch_backend_dependencies
802+
"find_dependency(ggml 0.27 CONFIG)\n" "if(NOT TARGET executorch_ggml)\n"
803+
" add_library(executorch_ggml ALIAS ggml::ggml)\n" "endif()\n"
804+
)
805+
endif()
806+
if(EXECUTORCH_BUILD_NATIVE
807+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
808+
OR EXECUTORCH_BUILD_NATIVE_GGML
809+
OR EXECUTORCH_BUILD_VULKAN
810+
)
811+
string(APPEND _executorch_backend_dependencies "find_dependency(Threads)\n")
812+
endif()
784813
configure_file(
785814
tools/cmake/executorch-backend-dependencies.cmake.in
786815
${CMAKE_CURRENT_BINARY_DIR}/executorch-backend-dependencies.cmake @ONLY
787816
)
788817
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/executorch-backend-dependencies.cmake
789818
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ExecuTorch
790819
)
791-
if(EXECUTORCH_BUILD_VULKAN OR EXECUTORCH_BUILD_VGF)
820+
if(EXECUTORCH_BUILD_VULKAN
821+
OR EXECUTORCH_BUILD_VGF
822+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
823+
)
792824
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/vulkan_shared)
793825
endif()
794826

@@ -1593,6 +1625,13 @@ if(EXECUTORCH_BUILD_VULKAN)
15931625
list(APPEND _executorch_backends vulkan_backend vulkan_schema)
15941626
endif()
15951627

1628+
if(EXECUTORCH_BUILD_NATIVE
1629+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
1630+
OR EXECUTORCH_BUILD_NATIVE_GGML
1631+
)
1632+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/native)
1633+
endif()
1634+
15961635
if(EXECUTORCH_BUILD_WEBGPU)
15971636
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/webgpu)
15981637
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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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`).
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)