Skip to content

Commit 9df506e

Browse files
SS-JIAclaude
andcommitted
[ET-VK][build] Fix Windows Vulkan/Android build and llama ETDump profiling
Summary: Three additive, backward-compatible fixes needed to build the ExecuTorch Vulkan backend and profile the llama runner from a Windows host. Each is guarded so it is a no-op on non-Windows / non-profiling builds. - backends/vulkan/runtime/gen_vulkan_spv.py: normalize CRLF -> LF at the top of preprocess() before the trailing-backslash macro-continuation regex. GLSL templates checked out with CRLF otherwise leave a lone backslash that escape() turns into an unterminated Python string literal ("SyntaxError: unterminated string literal") during shader codegen. - third-party/CMakeLists.txt: allow importing a prebuilt host flatc/flatcc via FLATC_EXECUTABLE / FLATCC_EXECUTABLE instead of building them from source. The existing WIN32 host-tool build only supports the Visual Studio generator (VS-only CMAKE_GENERATOR_PLATFORM, .exe-less BUILD_BYPRODUCTS), which fails under the Ninja generator required by the Android NDK cross-compile. - examples/models/llama/CMakeLists.txt: link etdump and define ET_EVENT_TRACER_ENABLED when the etdump target exists (i.e. core libs built with EXECUTORCH_BUILD_DEVTOOLS=ON + EXECUTORCH_ENABLE_EVENT_TRACER=ON). This enables ETDump collection and makes the Vulkan delegate emit per-dispatch GPU timestamps for per-shader profiling. Test Plan: Cross-compiled ExecuTorch core libs + llama_main for Android arm64 with the Vulkan backend enabled from a Windows host (NDK r29, Ninja generator, imported host flatc/flatcc). Confirmed libvulkan_backend.a is linked and llama_main runs on a Galaxy S24 (Adreno 750): Qwen3 0.6B ~80 tok/s decode, Llama 3.2 1B 8da4w ~60 tok/s decode. With DEVTOOLS/EVENT_TRACER on, collected an ETDump containing per-shader Vulkan events and analyzed prefill/decode breakdown via custom_inspector.py --mode llm. Reviewers: Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 3389d51 commit 9df506e

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

backends/vulkan/runtime/gen_vulkan_spv.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,12 @@ def escape(line: str) -> str:
641641
def preprocess(
642642
input_text: str, variables: Dict[str, Any], input_path: str = "codegen"
643643
) -> str:
644+
# Normalize line endings first. Templates checked out with CRLF (common on
645+
# Windows) otherwise break the trailing-backslash handling below: in
646+
# re.MULTILINE, $ matches immediately before \n, so a CR would sit between a
647+
# trailing \ and the line end and defeat the r"\\$" match, leaving a lone
648+
# backslash that escape() turns into an unterminated Python string literal.
649+
input_text = input_text.replace("\r\n", "\n").replace("\r", "\n")
644650
# Workaround to handle source files using \ to extend mecros to a new line
645651
input_text = re.sub(r"\\$", r"\\\\", input_text, flags=re.MULTILINE)
646652

examples/models/llama/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,21 @@ if(ANDROID)
241241
list(APPEND link_libraries log)
242242
endif()
243243

244+
# ETDump profiling support (OSS build). The llama runner does not link etdump or
245+
# define ET_EVENT_TRACER_ENABLED by default. When the core libs are built with
246+
# EXECUTORCH_BUILD_DEVTOOLS=ON + EXECUTORCH_ENABLE_EVENT_TRACER=ON, find_package
247+
# imports the etdump target; linking it and defining the macro enables ETDump
248+
# collection and makes the Vulkan delegate emit per-dispatch GPU timestamps.
249+
if(TARGET etdump)
250+
list(APPEND link_libraries etdump)
251+
endif()
252+
244253
add_executable(llama_main ${_srcs})
245254

255+
if(TARGET etdump)
256+
target_compile_definitions(llama_main PRIVATE ET_EVENT_TRACER_ENABLED)
257+
endif()
258+
246259
# Copy MLX metallib for runtime if MLX delegate is enabled
247260
if(TARGET mlxdelegate)
248261
executorch_target_copy_mlx_metallib(llama_main)

third-party/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ else()
4646
set(_flatbuffers_ep_additional_args)
4747
endif()
4848

49+
# Allow reusing a prebuilt host flatc instead of building it from source. This is
50+
# required when cross-compiling with the Ninja generator on Windows: the WIN32
51+
# branch above only supports the Visual Studio generator (it passes the VS-only
52+
# CMAKE_GENERATOR_PLATFORM), and the flatbuffers_ep host sub-build otherwise
53+
# fails under Ninja. Pass -DFLATC_EXECUTABLE=<path to a host flatc(.exe)>.
54+
if(FLATC_EXECUTABLE)
55+
add_executable(flatc IMPORTED GLOBAL)
56+
set_target_properties(flatc PROPERTIES IMPORTED_LOCATION "${FLATC_EXECUTABLE}")
57+
else()
4958
# We use ExternalProject to build flatc from source to force it target the host.
5059
# Otherwise, flatc will target the project's toolchain (i.e. iOS, or Android).
5160
ExternalProject_Add(
@@ -86,6 +95,7 @@ else()
8695
flatc PROPERTIES IMPORTED_LOCATION ${INSTALL_DIR}/bin/flatc
8796
)
8897
endif()
98+
endif()
8999

90100
# TODO: re-enable once flatbuffers is added as a subdirectory.
91101
# set(FLATBUFFERS_BUILD_FLATC OFF) set(FLATBUFFERS_INSTALL OFF)
@@ -104,6 +114,14 @@ else()
104114
set(_flatcc_extra_cmake_args)
105115
endif()
106116

117+
# Allow reusing a prebuilt host flatcc (see the flatc note above). Needed for the
118+
# Ninja-on-Windows cross-compile, where the flatcc_ep host sub-build fails. Pass
119+
# -DFLATCC_EXECUTABLE=<path to a host flatcc(.exe)>. flatcc_cli is only consumed
120+
# by devtools/etdump, so this imported target is unused when devtools is off.
121+
if(FLATCC_EXECUTABLE)
122+
add_executable(flatcc_cli IMPORTED GLOBAL)
123+
set_target_properties(flatcc_cli PROPERTIES IMPORTED_LOCATION "${FLATCC_EXECUTABLE}")
124+
else()
107125
# Similar to flatbuffers, we want to build flatcc for the host. See inline
108126
# comments in the flatbuffers ExternalProject_Add for more details.
109127
ExternalProject_Add(
@@ -142,6 +160,7 @@ else()
142160
flatcc_cli PROPERTIES IMPORTED_LOCATION ${INSTALL_DIR}/bin/flatcc
143161
)
144162
endif()
163+
endif()
145164

146165
set(FLATCC_RTONLY
147166
ON

0 commit comments

Comments
 (0)