Skip to content

Commit 924ad3d

Browse files
JakeStevensfacebook-github-bot
authored andcommitted
Add CMake targets for the native Vulkan 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`. `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 1725d0b commit 924ad3d

12 files changed

Lines changed: 546 additions & 43 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 23 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 OR EXECUTORCH_BUILD_NATIVE_VULKAN)
332+
set(JSON_Install ON)
333+
endif()
331334
add_subdirectory(third-party)
332335

333336
# On Apple, pybind11 SHARED extension modules must not link libpython directly.
@@ -838,14 +841,29 @@ elseif(EXECUTORCH_BUILD_CUDA)
838841
"find_dependency(CUDAToolkit)\n"
839842
)
840843
endif()
844+
if(EXECUTORCH_BUILD_NATIVE OR EXECUTORCH_BUILD_NATIVE_VULKAN)
845+
string(APPEND _executorch_backend_dependencies
846+
"find_dependency(libzip CONFIG)\n"
847+
"find_dependency(nlohmann_json CONFIG)\n"
848+
)
849+
endif()
850+
if(EXECUTORCH_BUILD_NATIVE
851+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
852+
OR EXECUTORCH_BUILD_VULKAN
853+
)
854+
string(APPEND _executorch_backend_dependencies "find_dependency(Threads)\n")
855+
endif()
841856
configure_file(
842857
tools/cmake/executorch-backend-dependencies.cmake.in
843858
${CMAKE_CURRENT_BINARY_DIR}/executorch-backend-dependencies.cmake @ONLY
844859
)
845860
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/executorch-backend-dependencies.cmake
846861
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ExecuTorch
847862
)
848-
if(EXECUTORCH_BUILD_VULKAN OR EXECUTORCH_BUILD_VGF)
863+
if(EXECUTORCH_BUILD_VULKAN
864+
OR EXECUTORCH_BUILD_VGF
865+
OR EXECUTORCH_BUILD_NATIVE_VULKAN
866+
)
849867
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/vulkan_shared)
850868
endif()
851869

@@ -1686,6 +1704,10 @@ if(EXECUTORCH_BUILD_VULKAN)
16861704
list(APPEND _executorch_backends vulkan_backend vulkan_schema)
16871705
endif()
16881706

1707+
if(EXECUTORCH_BUILD_NATIVE OR EXECUTORCH_BUILD_NATIVE_VULKAN)
1708+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/native)
1709+
endif()
1710+
16891711
if(EXECUTORCH_BUILD_WEBGPU)
16901712
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/webgpu)
16911713
list(APPEND _executorch_backends webgpu_backend)

‎backends/native/CMakeLists.txt‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
install(
35+
DIRECTORY runtime/
36+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/backends/native/runtime
37+
FILES_MATCHING
38+
PATTERN "*.h"
39+
PATTERN "test" EXCLUDE
40+
PATTERN "vulkan" EXCLUDE
41+
)
42+
43+
if(EXECUTORCH_BUILD_NATIVE_VISION_RUNNER)
44+
if(NOT EXECUTORCH_BUILD_NATIVE_VULKAN)
45+
message(
46+
FATAL_ERROR
47+
"EXECUTORCH_BUILD_NATIVE_VISION_RUNNER requires EXECUTORCH_BUILD_NATIVE_VULKAN=ON"
48+
)
49+
endif()
50+
add_subdirectory(examples)
51+
endif()
52+
53+
if(EXECUTORCH_BUILD_TESTS)
54+
add_subdirectory(test)
55+
endif()

‎backends/native/README.md‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
Both options default to `OFF`.
6+
7+
The native runtime requires C++20, a host `flatc`, the vendored FlatBuffers and
8+
nlohmann JSON headers, and an installed libzip package with the `libzip::zip`
9+
CMake target. Set `CMAKE_PREFIX_PATH` to the libzip installation if necessary.
10+
Native Vulkan also requires the Vulkan submodules and `glslc`, as described in
11+
the [Vulkan build instructions](../vulkan/README.md).
12+
13+
From the ExecuTorch checkout:
14+
15+
```sh
16+
cmake -S . -B cmake-out -DCMAKE_BUILD_TYPE=Release \
17+
-DEXECUTORCH_BUILD_NATIVE_VULKAN=ON
18+
cmake --build cmake-out --target native_api native_vulkan_engine
19+
cmake --install cmake-out --prefix install
20+
```
21+
22+
## Shared targets
23+
24+
| Target | Contents |
25+
| --- | --- |
26+
| `native_graph` | Graph IR, graph utilities, and memory planning |
27+
| `native_package` | PTN archive, safetensors, and owned byte readers |
28+
| `native_runtime` | Program deserialization, validation, and method metadata |
29+
| `native_engine` | Engine host, context, and executable interfaces |
30+
| `native_api` | Engine-independent `Model`, `Session`, and tensor API |
31+
| `native_vulkan_passes` | Native graph rewrites for Vulkan |
32+
| `native_vulkan_engine` | Vulkan host and executable implementation |
33+
| `vulkan_compute_api` | Vulkan device, allocator, and command API |
34+
| `vulkan_graph_runtime` | ComputeGraph, operator registrations, and shaders |
35+
36+
The common native targets have no dependency on the ExecuTorch executor or a
37+
specific engine. New engines link `native_engine` and implement its interfaces.
38+
Applications link `native_api` and the engines they use. The Vulkan engine and
39+
the existing `vulkan_backend` delegate share `vulkan_graph_runtime`; its static
40+
operator and shader registrations are retained automatically when linking.
41+
42+
All library targets are installed in `ExecuTorchTargets`. For example:
43+
44+
```cmake
45+
find_package(executorch CONFIG REQUIRED)
46+
target_link_libraries(my_app PRIVATE native_api native_vulkan_engine)
47+
```
48+
49+
With `EXECUTORCH_BUILD_TESTS=ON`, `native_runtime_test` covers the shared runtime
50+
and API. `native_vulkan_passes_test` covers the Vulkan graph rewrites without
51+
requiring a GPU. When the vision runner below is enabled,
52+
`native_runner_utils_test` covers its shared runner utilities. Run these with
53+
`ctest --test-dir cmake-out -R native_`.
54+
55+
## Vision classification runner
56+
57+
Enable `EXECUTORCH_BUILD_NATIVE_VISION_RUNNER=ON` together with
58+
`EXECUTORCH_BUILD_NATIVE_VULKAN=ON` to build `run_classification` from
59+
`examples/vision`. The runner additionally requires gflags; an OSS build uses
60+
the vendored copy, and other builds resolve an installed gflags package.
61+
Running the runner requires a Vulkan device.
62+
63+
```sh
64+
cmake -S . -B cmake-out -DCMAKE_BUILD_TYPE=Release \
65+
-DEXECUTORCH_BUILD_NATIVE_VULKAN=ON \
66+
-DEXECUTORCH_BUILD_NATIVE_VISION_RUNNER=ON
67+
cmake --build cmake-out --target run_classification
68+
cmake --install cmake-out --prefix install
69+
install/bin/run_classification \
70+
--package mv2.ptn --input mv2_input.bin \
71+
--expected mv2_expected.bin --categories categories.txt
72+
```
73+
74+
Export the `.ptn`, input, reference logits, and labels with
75+
[`export.py`](../../examples/native/vision/export.py). The runner prints the
76+
top-5 logits and, when `--expected` is given, exits nonzero if the logits
77+
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: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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(
45+
native_package deserialize/OwnedBytes.cpp deserialize/Package.cpp
46+
deserialize/SafeTensorsReader.cpp deserialize/ZipReader.cpp
47+
)
48+
target_link_libraries(
49+
native_package
50+
PUBLIC native_graph
51+
PRIVATE libzip::zip nlohmann_json::nlohmann_json Threads::Threads
52+
)
53+
54+
add_library(
55+
native_runtime Deserialize.cpp Program.cpp MethodMeta.cpp TensorInfo.cpp
56+
Validation.cpp
57+
)
58+
target_link_libraries(
59+
native_runtime
60+
PUBLIC native_graph native_package
61+
PRIVATE $<BUILD_INTERFACE:native_graph_schema> Threads::Threads
62+
)
63+
add_dependencies(native_runtime native_graph_schema_gen)
64+
65+
add_library(native_engine engine/Engine.cpp)
66+
target_link_libraries(native_engine PUBLIC native_runtime)
67+
68+
add_library(native_api api/Model.cpp)
69+
target_link_libraries(native_api PUBLIC native_engine)
70+
71+
foreach(target native_graph native_package native_runtime native_engine
72+
native_api
73+
)
74+
add_library(executorch::${target} ALIAS ${target})
75+
target_compile_features(${target} PUBLIC cxx_std_20)
76+
target_compile_options(
77+
${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>
78+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fexceptions>
79+
)
80+
target_include_directories(
81+
${target} PUBLIC $<BUILD_INTERFACE:${EXECUTORCH_ROOT}/..>
82+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
83+
)
84+
endforeach()
85+
86+
install(
87+
TARGETS native_graph native_package native_runtime native_engine native_api
88+
EXPORT ExecuTorchTargets
89+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
90+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
91+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
92+
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 vulkan_graph_runtime)
8+
add_subdirectory(
9+
${EXECUTORCH_ROOT}/backends/vulkan
10+
${CMAKE_CURRENT_BINARY_DIR}/vulkan_runtime
11+
)
12+
endif()
13+
14+
file(GLOB _native_vulkan_passes_srcs CONFIGURE_DEPENDS
15+
${CMAKE_CURRENT_SOURCE_DIR}/passes/*.cpp
16+
)
17+
add_library(native_vulkan_passes ${_native_vulkan_passes_srcs})
18+
target_link_libraries(native_vulkan_passes PUBLIC native_graph)
19+
20+
add_library(
21+
native_vulkan_engine VulkanConstantMaterializationTracker.cpp
22+
VulkanEngine.cpp VulkanEngineExecutable.cpp
23+
)
24+
add_library(executorch::native_vulkan_engine ALIAS native_vulkan_engine)
25+
target_link_libraries(
26+
native_vulkan_engine
27+
PUBLIC native_engine
28+
PRIVATE native_vulkan_passes vulkan_graph_runtime
29+
)
30+
31+
foreach(target native_vulkan_passes native_vulkan_engine)
32+
target_compile_options(
33+
${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>
34+
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fexceptions>
35+
)
36+
endforeach()
37+
38+
install(
39+
TARGETS native_vulkan_passes native_vulkan_engine
40+
EXPORT ExecuTorchTargets
41+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
42+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
43+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
44+
)
45+
install(
46+
FILES VulkanEngine.h
47+
DESTINATION
48+
${CMAKE_INSTALL_INCLUDEDIR}/executorch/backends/native/runtime/vulkan
49+
)
50+
install(
51+
DIRECTORY passes/
52+
DESTINATION
53+
${CMAKE_INSTALL_INCLUDEDIR}/executorch/backends/native/runtime/vulkan/passes
54+
FILES_MATCHING
55+
PATTERN "*.h"
56+
)

0 commit comments

Comments
 (0)