Skip to content

Commit 27dc1a0

Browse files
authored
Merge branch 'main' into shoumikhin-patch-2
2 parents 87641e3 + 76a4062 commit 27dc1a0

File tree

10 files changed

+1190
-4
lines changed

10 files changed

+1190
-4
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,10 @@ if(EXECUTORCH_BUILD_PYBIND)
763763
list(APPEND _dep_libs xnnpack_backend XNNPACK xnnpack-microkernels-prod)
764764
endif()
765765

766+
if(EXECUTORCH_BUILD_VULKAN)
767+
list(APPEND _dep_libs vulkan_backend)
768+
endif()
769+
766770
# compile options for pybind
767771
set(_pybind_compile_options -Wno-deprecated-declarations -fPIC -frtti
768772
-fexceptions

backends/vulkan/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ set_target_properties(vulkan_schema PROPERTIES LINKER_LANGUAGE CXX)
101101
target_include_directories(
102102
vulkan_schema
103103
INTERFACE
104-
${SCHEMA_INCLUDE_DIR}
104+
$<BUILD_INTERFACE:${SCHEMA_INCLUDE_DIR}>
105105
$<BUILD_INTERFACE:${EXECUTORCH_ROOT}/third-party/flatbuffers/include>
106106
)
107107

backends/vulkan/partitioner/vulkan_partitioner.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# pyre-strict
88

99
import logging
10-
from typing import Any, Callable, Dict, final, List, Mapping, Optional, Tuple
10+
from typing import Any, Callable, Dict, final, List, Mapping, Optional, Set, Tuple
1111

1212
import executorch.backends.vulkan.utils as utils
1313

@@ -17,6 +17,7 @@
1717
get_op_features,
1818
has_impl,
1919
OpFeatures,
20+
OpKey,
2021
vulkan_supported_ops,
2122
)
2223

@@ -55,11 +56,17 @@ def __init__(
5556
texture_limits: utils.ImageExtents,
5657
buffer_limit: int,
5758
require_dynamic_shape: bool = False,
59+
operator_blocklist: Optional[Set[OpKey]] = None,
60+
operator_allowlist: Optional[Set[OpKey]] = None,
5861
) -> None:
5962
super().__init__()
6063
self.texture_limits: utils.ImageExtents = texture_limits
6164
self.buffer_limit = buffer_limit
6265
self.require_dynamic_shapes = require_dynamic_shape
66+
self.operator_blocklist: Set[OpKey] = (
67+
operator_blocklist if operator_blocklist is not None else set()
68+
)
69+
self.operator_allowlist = operator_allowlist
6370

6471
def op_node_is_compatible( # noqa: C901: Function is too complex
6572
self, node: torch.fx.Node, features: Optional[OpFeatures] = None
@@ -77,6 +84,17 @@ def op_node_is_compatible( # noqa: C901: Function is too complex
7784
assert isinstance(first_arg, torch._ops.OpOverload)
7885
target = first_arg.name()
7986

87+
# Operator allow list is only used for torch ops
88+
if (
89+
utils.is_torch_op_node(node)
90+
and (self.operator_allowlist is not None)
91+
and (target not in self.operator_allowlist)
92+
):
93+
return False, "op is not in allowlist"
94+
95+
if target in self.operator_blocklist:
96+
return False, "op is in blocklist"
97+
8098
# Extract the features for the node's operator, if no override was provided
8199
if features is None:
82100
if not has_impl(target):
@@ -93,7 +111,7 @@ def op_node_is_compatible( # noqa: C901: Function is too complex
93111
if op_repsets.any_is_empty():
94112
return (
95113
False,
96-
"No valid representations for a tensor in the operation",
114+
f"no valid representations for op {utils.node_io_str(node)}",
97115
)
98116

99117
return True, "Op is compatible"
@@ -277,6 +295,8 @@ class VulkanPartitioner(Partitioner):
277295
def __init__(
278296
self,
279297
compile_options: Optional[Dict[str, Any]] = None,
298+
operator_blocklist: Optional[List[OpKey]] = None,
299+
operator_allowlist: Optional[List[OpKey]] = None,
280300
) -> None:
281301
self.options: Dict[str, Any] = {}
282302
if compile_options is not None:
@@ -285,6 +305,18 @@ def __init__(
285305
compile_spec = parse_compile_options(self.options)
286306
self.delegation_spec = DelegationSpec(VulkanBackend.__name__, compile_spec)
287307

308+
self.operator_blocklist: Set[OpKey] = set()
309+
if operator_blocklist is not None:
310+
for entry in operator_blocklist or []:
311+
self.operator_blocklist.add(entry)
312+
313+
self.operator_allowlist: Optional[Set[OpKey]] = None
314+
if operator_allowlist is not None:
315+
self.operator_allowlist = set()
316+
for entry in operator_allowlist:
317+
assert self.operator_allowlist is not None
318+
self.operator_allowlist.add(entry)
319+
288320
def ops_to_not_decompose(
289321
self, ep: ExportedProgram
290322
) -> Tuple[List[torch._ops.OpOverload], Optional[Callable[[torch.fx.Node], bool]]]:
@@ -308,6 +340,8 @@ def partition(self, exported_program: ExportedProgram) -> PartitionResult:
308340
texture_limits,
309341
buffer_limit,
310342
require_dynamic_shape=self.options.get("require_dynamic_shapes", False),
343+
operator_blocklist=self.operator_blocklist,
344+
operator_allowlist=self.operator_allowlist,
311345
),
312346
allows_single_node_partition=True,
313347
)

backends/vulkan/runtime/gen_vulkan_spv.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,6 @@ def compile_spirv(shader_paths_pair) -> Tuple[str, str]:
10831083
for spv_out_path, glsl_out_path in pool.map(
10841084
compile_spirv, self.output_file_map.items()
10851085
):
1086-
print(spv_to_glsl_map)
10871086
spv_to_glsl_map[spv_out_path] = glsl_out_path
10881087

10891088
return spv_to_glsl_map
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
set -exu
9+
10+
# Initialize variables
11+
RUN_BUILD=false
12+
RUN_CORRECTNESS_TEST=false
13+
RUN_CLEAN=false
14+
RUN_RECOMPILE=false
15+
MODEL_NAME=""
16+
OUTPUT_DIRECTORY="."
17+
18+
# Parse arguments
19+
SKIP_NEXT=false
20+
for i in $(seq 1 $#); do
21+
if [[ "$SKIP_NEXT" == true ]]; then
22+
SKIP_NEXT=false
23+
continue
24+
fi
25+
26+
arg="${!i}"
27+
case $arg in
28+
--build|-b)
29+
RUN_BUILD=true
30+
;;
31+
--clean|-c)
32+
RUN_CLEAN=true
33+
;;
34+
--recompile|-rc)
35+
RUN_RECOMPILE=true
36+
;;
37+
--output_directory|-o)
38+
next_i=$((i + 1))
39+
if [[ $next_i -le $# ]]; then
40+
OUTPUT_DIRECTORY="${!next_i}"
41+
SKIP_NEXT=true
42+
else
43+
echo "Error: --output_directory|-o requires a value"
44+
exit 1
45+
fi
46+
;;
47+
--*|-*)
48+
echo "Unknown argument: $arg"
49+
exit 1
50+
;;
51+
*)
52+
if [[ -z "$MODEL_NAME" ]]; then
53+
MODEL_NAME="$arg"
54+
else
55+
echo "Multiple model names provided: $MODEL_NAME and $arg"
56+
exit 1
57+
fi
58+
;;
59+
esac
60+
done
61+
62+
# Determine execution mode based on parsed arguments
63+
if [[ "$RUN_BUILD" == true ]] && [[ -z "$MODEL_NAME" ]]; then
64+
# Build-only mode
65+
RUN_CORRECTNESS_TEST=false
66+
elif [[ "$RUN_BUILD" == true ]] && [[ -n "$MODEL_NAME" ]]; then
67+
# Build and test mode
68+
RUN_CORRECTNESS_TEST=true
69+
elif [[ "$RUN_BUILD" == false ]] && [[ -n "$MODEL_NAME" ]]; then
70+
# Test-only mode
71+
RUN_CORRECTNESS_TEST=true
72+
else
73+
echo "Invalid argument combination. Usage:"
74+
echo " $0 --build|-b [--clean|-c] [--recompile|-rc] [-o|--output_directory DIR] # Build-only mode"
75+
echo " $0 model_name [--build|-b] [--clean|-c] [--recompile|-rc] [-o|--output_directory DIR] # Test mode or build+test mode"
76+
exit 1
77+
fi
78+
79+
if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then
80+
PYTHON_EXECUTABLE=python3
81+
fi
82+
which "${PYTHON_EXECUTABLE}"
83+
84+
CMAKE_OUTPUT_DIR=cmake-out
85+
86+
# Only set EXPORTED_MODEL if running correctness test
87+
if [[ "${RUN_CORRECTNESS_TEST}" == true ]]; then
88+
EXPORTED_MODEL=${MODEL_NAME}_vulkan
89+
fi
90+
91+
92+
clean_build_directory() {
93+
echo "Cleaning build directory: ${CMAKE_OUTPUT_DIR}"
94+
rm -rf ${CMAKE_OUTPUT_DIR}
95+
}
96+
97+
recompile() {
98+
cmake --build cmake-out -j64 --target install
99+
}
100+
101+
build_core_libraries_and_devtools() {
102+
echo "Building core libraries and devtools with comprehensive Vulkan support..."
103+
104+
# Build core libraries with all required components
105+
cmake . \
106+
-DCMAKE_INSTALL_PREFIX=cmake-out \
107+
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
108+
-DEXECUTORCH_BUILD_KERNELS_CUSTOM_AOT=ON \
109+
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
110+
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
111+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
112+
-DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON \
113+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
114+
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
115+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
116+
-DEXECUTORCH_BUILD_EXECUTOR_RUNNER=ON \
117+
-DEXECUTORCH_BUILD_DEVTOOLS=ON \
118+
-DEXECUTORCH_BUILD_VULKAN=ON \
119+
-DEXECUTORCH_BUILD_XNNPACK=ON \
120+
-Bcmake-out && \
121+
cmake --build cmake-out -j64 --target install
122+
123+
# Build devtools example runner
124+
cmake examples/devtools \
125+
-DCMAKE_INSTALL_PREFIX=cmake-out \
126+
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
127+
-DEXECUTORCH_BUILD_VULKAN=ON \
128+
-Bcmake-out/examples/devtools && \
129+
cmake --build cmake-out/examples/devtools -j16 --config Release
130+
}
131+
132+
run_example_runner() {
133+
./${CMAKE_OUTPUT_DIR}/examples/devtools/example_runner -bundled_program_path "${OUTPUT_DIRECTORY}/${EXPORTED_MODEL}.bpte" -output_verification
134+
}
135+
136+
test_bundled_model_with_vulkan() {
137+
# Export model as bundled program with Vulkan backend
138+
"${PYTHON_EXECUTABLE}" -m examples.vulkan.export --model_name="${MODEL_NAME}" --output_dir="${OUTPUT_DIRECTORY}" --bundled
139+
140+
# Update exported model name for bundled program
141+
EXPORTED_MODEL="${MODEL_NAME}_vulkan"
142+
143+
# Verify the exported bundled model exists
144+
if [[ ! -f "${OUTPUT_DIRECTORY}/${EXPORTED_MODEL}.bpte" ]]; then
145+
echo "Error: Failed to export bundled model ${MODEL_NAME} with Vulkan backend"
146+
exit 1
147+
fi
148+
149+
# Note: Running bundled programs may require different executor runner
150+
echo "Bundled program created successfully. Use appropriate bundled program runner to test."
151+
152+
run_example_runner
153+
}
154+
155+
156+
# Main execution
157+
if [[ "${RUN_BUILD}" == true ]]; then
158+
if [[ "${RUN_CLEAN}" == true ]]; then
159+
clean_build_directory
160+
fi
161+
build_core_libraries_and_devtools
162+
fi
163+
164+
if [[ "${RUN_RECOMPILE}" == true ]]; then
165+
recompile
166+
fi
167+
168+
if [[ "${RUN_CORRECTNESS_TEST}" == true ]]; then
169+
echo "Testing ${MODEL_NAME} with Vulkan backend..."
170+
# Always use bundled program testing
171+
test_bundled_model_with_vulkan
172+
173+
# Check if test completed successfully
174+
if [[ $? -eq 0 ]]; then
175+
echo "Vulkan model test completed successfully!"
176+
else
177+
echo "Vulkan model test failed!"
178+
exit 1
179+
fi
180+
fi

0 commit comments

Comments
 (0)