Skip to content

Commit 784a069

Browse files
Sicheng JiaSS-JIA
authored andcommitted
[ET-VK][examples] Create export script for Vulkan examples
Summary: Title says it all! Introduce scripts to facilitate exporting and testing Vulkan delegate models. These scripts will be used in the next PR to add CI testing for Vulkan lowered models. ghstack-source-id: 3d7f096 ghstack-comment-id: 3176582674 Pull-Request: #13294
1 parent 524de37 commit 784a069

File tree

5 files changed

+681
-0
lines changed

5 files changed

+681
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
-DEXECUTORCH_BUILD_TESTS=ON \
121+
-Bcmake-out && \
122+
cmake --build cmake-out -j64 --target install
123+
124+
# Build devtools example runner
125+
cmake examples/devtools \
126+
-DCMAKE_INSTALL_PREFIX=cmake-out \
127+
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
128+
-DEXECUTORCH_BUILD_VULKAN=ON \
129+
-Bcmake-out/examples/devtools && \
130+
cmake --build cmake-out/examples/devtools -j16 --config Release
131+
}
132+
133+
run_example_runner() {
134+
./${CMAKE_OUTPUT_DIR}/examples/devtools/example_runner -bundled_program_path "${OUTPUT_DIRECTORY}/${EXPORTED_MODEL}.bpte" -output_verification
135+
}
136+
137+
test_bundled_model_with_vulkan() {
138+
# Export model as bundled program with Vulkan backend
139+
"${PYTHON_EXECUTABLE}" -m examples.vulkan.export --model_name="${MODEL_NAME}" --output_dir="${OUTPUT_DIRECTORY}" --bundled
140+
141+
# Update exported model name for bundled program
142+
EXPORTED_MODEL="${MODEL_NAME}_vulkan"
143+
144+
# Verify the exported bundled model exists
145+
if [[ ! -f "${OUTPUT_DIRECTORY}/${EXPORTED_MODEL}.bpte" ]]; then
146+
echo "Error: Failed to export bundled model ${MODEL_NAME} with Vulkan backend"
147+
exit 1
148+
fi
149+
150+
# Note: Running bundled programs may require different executor runner
151+
echo "Bundled program created successfully. Use appropriate bundled program runner to test."
152+
153+
run_example_runner
154+
}
155+
156+
157+
# Main execution
158+
if [[ "${RUN_BUILD}" == true ]]; then
159+
if [[ "${RUN_CLEAN}" == true ]]; then
160+
clean_build_directory
161+
fi
162+
build_core_libraries_and_devtools
163+
fi
164+
165+
if [[ "${RUN_RECOMPILE}" == true ]]; then
166+
recompile
167+
fi
168+
169+
if [[ "${RUN_CORRECTNESS_TEST}" == true ]]; then
170+
echo "Testing ${MODEL_NAME} with Vulkan backend..."
171+
# Always use bundled program testing
172+
test_bundled_model_with_vulkan
173+
174+
# Check if test completed successfully
175+
if [[ $? -eq 0 ]]; then
176+
echo "Vulkan model test completed successfully!"
177+
else
178+
echo "Vulkan model test failed!"
179+
exit 1
180+
fi
181+
fi

examples/vulkan/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Vulkan Delegate Export Examples
2+
3+
This directory contains scripts for exporting models with the Vulkan delegate in ExecuTorch. Vulkan delegation allows you to run your models on devices with Vulkan-capable GPUs, potentially providing significant performance improvements over CPU execution.
4+
5+
## Scripts
6+
7+
- `export.py`: Basic export script for models to use with Vulkan delegate
8+
- `aot_compiler.py`: Advanced export script with quantization support
9+
10+
## Usage
11+
12+
### Basic Export
13+
14+
```bash
15+
python -m executorch.examples.vulkan.export -m <model_name> -o <output_dir>
16+
```
17+
18+
### Export with Quantization (Experimental)
19+
20+
```bash
21+
python -m executorch.examples.vulkan.aot_compiler -m <model_name> -q -o <output_dir>
22+
```
23+
24+
### Dynamic Shape Support
25+
26+
```bash
27+
python -m executorch.examples.vulkan.export -m <model_name> -d -o <output_dir>
28+
```
29+
30+
### Additional Options
31+
32+
- `-s/--strict`: Export with strict mode (default: True)
33+
- `-a/--segment_alignment`: Specify segment alignment in hex (default: 0x1000)
34+
- `-e/--external_constants`: Save constants in external .ptd file (default: False)
35+
- `-r/--etrecord`: Generate and save an ETRecord to the given file location
36+
37+
## Examples
38+
39+
```bash
40+
# Export MobileNetV2 with Vulkan delegate
41+
python -m executorch.examples.vulkan.export -m mobilenet_v2 -o ./exported_models
42+
43+
# Export MobileNetV3 with quantization
44+
python -m executorch.examples.vulkan.aot_compiler -m mobilenet_v3 -q -o ./exported_models
45+
46+
# Export with dynamic shapes
47+
python -m executorch.examples.vulkan.export -m mobilenet_v2 -d -o ./exported_models
48+
49+
# Export with ETRecord for debugging
50+
python -m executorch.examples.vulkan.export -m mobilenet_v2 -r ./records/mobilenet_record.etrecord -o ./exported_models
51+
```
52+
53+
## Supported Operations
54+
55+
The Vulkan delegate supports various operations including:
56+
57+
- Basic arithmetic (add, subtract, multiply, divide)
58+
- Activations (ReLU, Sigmoid, Tanh, etc.)
59+
- Convolutions (Conv1d, Conv2d, ConvTranspose2d)
60+
- Pooling operations (MaxPool2d, AvgPool2d)
61+
- Linear/Fully connected layers
62+
- BatchNorm, GroupNorm
63+
- Various tensor operations (cat, reshape, permute, etc.)
64+
65+
For a complete list of supported operations, refer to the Vulkan delegate implementation in the ExecuTorch codebase.
66+
67+
## Debugging and Optimization
68+
69+
If you encounter issues with Vulkan delegation:
70+
71+
1. Use `-r/--etrecord` to generate an ETRecord for debugging
72+
2. Check if your operations are supported by the Vulkan delegate
73+
3. Ensure your Vulkan drivers are up to date
74+
4. Try using the export script with `--strict False` if strict mode causes issues
75+
76+
## Requirements
77+
78+
- Vulkan runtime libraries (libvulkan.so.1)
79+
- A Vulkan-capable GPU with appropriate drivers
80+
- PyTorch with Vulkan support

examples/vulkan/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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.

0 commit comments

Comments
 (0)