|
| 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