Skip to content

Commit ca1f052

Browse files
authored
Auto-enable Vulkan in builds, and update docs for Vulkan backend
Differential Revision: D109868652 Pull Request resolved: pytorch#20525
1 parent 5b001f4 commit ca1f052

5 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎.ci/scripts/wheel/pre_build_script.sh‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,8 @@ if [[ "${EXECUTORCH_BUILD_VULKAN:-0}" != "0" \
122122
echo "${_glslc_prefix}/bin" >> "${GITHUB_PATH}"
123123
echo "glslc installed: $(command -v glslc)"
124124
fi
125+
else
126+
# This is the Vulkan equivalent of the Windows CUDA force-off above (#20527).
127+
export CMAKE_ARGS="${CMAKE_ARGS:-} -DEXECUTORCH_BUILD_VULKAN=OFF"
128+
echo "CMAKE_ARGS=${CMAKE_ARGS}" >> "${GITHUB_ENV}"
125129
fi

‎docs/source/backends-overview.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Backends are the bridge between your exported model and the hardware it runs on.
2424
| [CUDA](backends/cuda/cuda-overview.md) | Linux/Windows | GPU | NVIDIA GPU acceleration |
2525
| [Core ML](backends/coreml/coreml-overview.md) | iOS, macOS | NPU/GPU/CPU | Apple devices, high performance |
2626
| [Metal Performance Shaders](backends/mps/mps-overview.md) | iOS, macOS | GPU | Apple GPU acceleration |
27-
| [Vulkan](backends/vulkan/vulkan-overview.md) | Android | GPU | Android GPU acceleration |
27+
| [Vulkan](backends/vulkan/vulkan-overview.md) | Android, Linux, Windows | GPU | Android devices (mature); Desktops (experimental) |
2828
| [Qualcomm](backends-qualcomm) | Android | NPU | Qualcomm SoCs |
2929
| [MediaTek](backends-mediatek) | Android | NPU | MediaTek SoCs |
3030
| [Arm Ethos-U](backends/arm-ethos-u/arm-ethos-u-overview.md) | Embedded | NPU | Arm MCUs |

‎docs/source/backends/vulkan/vulkan-overview.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
The ExecuTorch Vulkan (ET-VK) backend enables ExecuTorch models to execute on
44
GPUs via the cross-platform [Vulkan API](https://www.vulkan.org/). Although the
55
Vulkan API support is almost ubiquitous among modern GPUs, the ExecuTorch Vulkan
6-
backend is currently developed with a specific focus for **Android GPUs**.
6+
backend is developed with a focus on **Android GPUs**, and support for desktop
7+
platforms is experimental.
78

89
## Features
910

‎install_utils.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# LICENSE file in the root directory of this source tree.
77

88
import functools
9+
import os
910
import platform
1011
import re
1112
import subprocess
@@ -55,6 +56,45 @@ def is_cuda_available() -> bool:
5556
return False
5657

5758

59+
def is_vulkan_available() -> bool:
60+
"""
61+
Check if the Vulkan shader compiler (glslc) is available on the system.
62+
63+
glslc is the only build-time dependency for the Vulkan backend; the Vulkan
64+
loader itself is dlopen()ed at runtime via volk. Restricted to Linux and
65+
Windows, the desktop GPU platforms the backend supports (macOS would require
66+
MoltenVK).
67+
68+
glslc is looked up on PATH and, failing that, under $VULKAN_SDK/{bin,Bin} to
69+
match the find_program() HINTS the build uses (see pybind.cmake and
70+
ShaderLibrary.cmake): the Windows Vulkan SDK sets VULKAN_SDK but does not add
71+
its bin directory to PATH, so a PATH-only probe would miss it there.
72+
73+
Returns:
74+
True if glslc is available on a supported platform, False otherwise.
75+
"""
76+
if sys.platform not in ("linux", "win32"):
77+
return False
78+
candidates = ["glslc"]
79+
vulkan_sdk = os.environ.get("VULKAN_SDK")
80+
if vulkan_sdk:
81+
glslc_name = "glslc.exe" if sys.platform == "win32" else "glslc"
82+
candidates += [
83+
os.path.join(vulkan_sdk, "bin", glslc_name),
84+
os.path.join(vulkan_sdk, "Bin", glslc_name),
85+
]
86+
for glslc in candidates:
87+
try:
88+
# Only the exit status matters, so skip text=True; keep the except
89+
# tight to avoid masking things like UnicodeDecodeError.
90+
subprocess.run([glslc, "--version"], capture_output=True, check=True)
91+
return True
92+
except (OSError, subprocess.SubprocessError):
93+
# glslc missing or not runnable -> unavailable; try the next candidate.
94+
continue
95+
return False
96+
97+
5898
@functools.lru_cache(maxsize=1)
5999
def _get_cuda_version():
60100
"""

‎setup.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,33 @@ def run(self): # noqa C901
890890
):
891891
cmake_configuration_args += ["-DEXECUTORCH_BUILD_CUDA=ON"]
892892

893+
# Unlike CUDA, Vulkan also needs its third-party submodules, which
894+
# aren't in the default checkout, along with glslc. A partial checkout
895+
# no-ops here rather than failing in CMake.
896+
vulkan_third_party = os.path.join(
897+
os.path.dirname(os.path.abspath(__file__)),
898+
"backends",
899+
"vulkan",
900+
"third-party",
901+
)
902+
vulkan_submodules_present = all(
903+
os.path.exists(os.path.join(vulkan_third_party, *parts))
904+
for parts in (
905+
("volk", "volk.c"),
906+
("Vulkan-Headers", "include", "vulkan", "vulkan.h"),
907+
("VulkanMemoryAllocator", "include", "vk_mem_alloc.h"),
908+
)
909+
)
910+
if (
911+
not minimal_build
912+
and vulkan_submodules_present
913+
and install_utils.is_vulkan_available()
914+
and install_utils.is_cmake_option_on(
915+
cmake_configuration_args, "EXECUTORCH_BUILD_VULKAN", default=True
916+
)
917+
):
918+
cmake_configuration_args += ["-DEXECUTORCH_BUILD_VULKAN=ON"]
919+
893920
# Check if QNN SDK is available (via QNN_SDK_ROOT env var), and if so,
894921
# enable building the Qualcomm backend by default.
895922
qnn_sdk_root = os.environ.get("QNN_SDK_ROOT", "").strip()

0 commit comments

Comments
 (0)