|
| 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 | +# Export model to CUDA/Metal format with optional quantization |
| 9 | + |
| 10 | +show_help() { |
| 11 | + cat << EOF |
| 12 | +Usage: export_model_artifact.sh <device> <hf_model> [quant_name] [output_dir] |
| 13 | +
|
| 14 | +Export a HuggingFace model to CUDA/Metal format with optional quantization. |
| 15 | +
|
| 16 | +Arguments: |
| 17 | + device cuda or metal (required) |
| 18 | +
|
| 19 | + hf_model HuggingFace model ID (required) |
| 20 | + Supported models: |
| 21 | + - mistralai/Voxtral-Mini-3B-2507 |
| 22 | + - openai/whisper series (whisper-{small, medium, large, large-v2, large-v3, large-v3-turbo}) |
| 23 | + - google/gemma-3-4b-it |
| 24 | +
|
| 25 | + quant_name Quantization type (optional, default: non-quantized) |
| 26 | + Options: |
| 27 | + - non-quantized |
| 28 | + - quantized-int4-tile-packed |
| 29 | + - quantized-int4-weight-only |
| 30 | +
|
| 31 | + output_dir Output directory for artifacts (optional, default: current directory) |
| 32 | +
|
| 33 | +Examples: |
| 34 | + export_model_artifact.sh metal "openai/whisper-small" |
| 35 | + export_model_artifact.sh cuda "mistralai/Voxtral-Mini-3B-2507" "quantized-int4-tile-packed" |
| 36 | + export_model_artifact.sh cuda "google/gemma-3-4b-it" "non-quantized" "./output" |
| 37 | +EOF |
| 38 | +} |
| 39 | + |
| 40 | +if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then |
| 41 | + show_help |
| 42 | + exit 0 |
| 43 | +fi |
| 44 | + |
| 45 | +if [ -z "${1:-}" ]; then |
| 46 | + echo "Error: hf_model argument is required" |
| 47 | + echo "Run with -h or --help for usage information" |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +set -eux |
| 52 | + |
| 53 | +DEVICE="$1" |
| 54 | +HF_MODEL="$2" |
| 55 | +QUANT_NAME="${3:-non-quantized}" |
| 56 | +OUTPUT_DIR="${4:-.}" |
| 57 | + |
| 58 | +case "$DEVICE" in |
| 59 | + cuda) |
| 60 | + ;; |
| 61 | + metal) |
| 62 | + ;; |
| 63 | + *) |
| 64 | + echo "Error: Unsupported device '$DEVICE'" |
| 65 | + echo "Supported devices: cuda, metal" |
| 66 | + exit 1 |
| 67 | + ;; |
| 68 | +esac |
| 69 | + |
| 70 | +# Determine model configuration based on HF model ID |
| 71 | +case "$HF_MODEL" in |
| 72 | + mistralai/Voxtral-Mini-3B-2507) |
| 73 | + MODEL_NAME="voxtral" |
| 74 | + TASK="multimodal-text-to-text" |
| 75 | + MAX_SEQ_LEN="1024" |
| 76 | + EXTRA_PIP="mistral-common librosa" |
| 77 | + PREPROCESSOR_FEATURE_SIZE="128" |
| 78 | + PREPROCESSOR_OUTPUT="voxtral_preprocessor.pte" |
| 79 | + ;; |
| 80 | + openai/whisper-*) |
| 81 | + MODEL_NAME="whisper" |
| 82 | + TASK="automatic-speech-recognition" |
| 83 | + MAX_SEQ_LEN="" |
| 84 | + EXTRA_PIP="librosa" |
| 85 | + PREPROCESSOR_OUTPUT="whisper_preprocessor.pte" |
| 86 | + if [[ "$HF_MODEL" == *"large-v3"* ]]; then |
| 87 | + PREPROCESSOR_FEATURE_SIZE="128" |
| 88 | + else |
| 89 | + PREPROCESSOR_FEATURE_SIZE="80" |
| 90 | + fi |
| 91 | + ;; |
| 92 | + google/gemma-3-4b-it) |
| 93 | + if [ "$DEVICE" = "metal" ]; then |
| 94 | + echo "Error: Export for device 'metal' is not yet tested for model '$HF_MODEL'" |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | + MODEL_NAME="gemma3" |
| 98 | + TASK="multimodal-text-to-text" |
| 99 | + MAX_SEQ_LEN="64" |
| 100 | + EXTRA_PIP="" |
| 101 | + PREPROCESSOR_FEATURE_SIZE="" |
| 102 | + PREPROCESSOR_OUTPUT="" |
| 103 | + ;; |
| 104 | + *) |
| 105 | + echo "Error: Unsupported model '$HF_MODEL'" |
| 106 | + echo "Supported models: mistralai/Voxtral-Mini-3B-2507, openai/whisper-{small, medium, large, large-v2, large-v3, large-v3-turbo}, google/gemma-3-4b-it" |
| 107 | + exit 1 |
| 108 | + ;; |
| 109 | +esac |
| 110 | + |
| 111 | +# Determine quantization args based on quant name |
| 112 | +case "$QUANT_NAME" in |
| 113 | + non-quantized) |
| 114 | + EXTRA_ARGS="" |
| 115 | + ;; |
| 116 | + quantized-int4-tile-packed) |
| 117 | + if [ "$DEVICE" = "metal" ]; then |
| 118 | + echo "Error: Metal backend does not yet support quantization '$QUANT_NAME'" |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | + EXTRA_ARGS="--qlinear 4w --qlinear_encoder 4w --qlinear_packing_format tile_packed_to_4d --qlinear_encoder_packing_format tile_packed_to_4d" |
| 122 | + ;; |
| 123 | + quantized-int4-weight-only) |
| 124 | + if [ "$DEVICE" = "metal" ]; then |
| 125 | + echo "Error: Metal backend does not yet support quantization '$QUANT_NAME'" |
| 126 | + exit 1 |
| 127 | + fi |
| 128 | + EXTRA_ARGS="--qlinear_encoder 4w" |
| 129 | + ;; |
| 130 | + *) |
| 131 | + echo "Error: Unsupported quantization '$QUANT_NAME'" |
| 132 | + echo "Supported quantizations: non-quantized, quantized-int4-tile-packed, quantized-int4-weight-only" |
| 133 | + exit 1 |
| 134 | + ;; |
| 135 | +esac |
| 136 | + |
| 137 | +echo "::group::Export $MODEL_NAME" |
| 138 | + |
| 139 | +if [ -n "$EXTRA_PIP" ]; then |
| 140 | + pip install $EXTRA_PIP |
| 141 | +fi |
| 142 | +pip list |
| 143 | + |
| 144 | +MAX_SEQ_LEN_ARG="" |
| 145 | +if [ -n "$MAX_SEQ_LEN" ]; then |
| 146 | + MAX_SEQ_LEN_ARG="--max_seq_len $MAX_SEQ_LEN" |
| 147 | +fi |
| 148 | + |
| 149 | +DEVICE_ARG="" |
| 150 | +if [ "$DEVICE" = "cuda" ]; then |
| 151 | + DEVICE_ARG="--device cuda" |
| 152 | +fi |
| 153 | + |
| 154 | +optimum-cli export executorch \ |
| 155 | + --model "$HF_MODEL" \ |
| 156 | + --task "$TASK" \ |
| 157 | + --recipe "$DEVICE" \ |
| 158 | + --dtype bfloat16 \ |
| 159 | + ${DEVICE_ARG} \ |
| 160 | + ${MAX_SEQ_LEN_ARG} \ |
| 161 | + ${EXTRA_ARGS} \ |
| 162 | + --output_dir ./ |
| 163 | + |
| 164 | +if [ -n "$PREPROCESSOR_OUTPUT" ]; then |
| 165 | + python -m executorch.extension.audio.mel_spectrogram \ |
| 166 | + --feature_size $PREPROCESSOR_FEATURE_SIZE \ |
| 167 | + --stack_output \ |
| 168 | + --max_audio_len 300 \ |
| 169 | + --output_file $PREPROCESSOR_OUTPUT |
| 170 | +fi |
| 171 | + |
| 172 | +test -f model.pte |
| 173 | +test -f aoti_${DEVICE}_blob.ptd |
| 174 | +if [ -n "$PREPROCESSOR_OUTPUT" ]; then |
| 175 | + test -f $PREPROCESSOR_OUTPUT |
| 176 | +fi |
| 177 | +echo "::endgroup::" |
| 178 | + |
| 179 | +echo "::group::Store $MODEL_NAME Artifacts" |
| 180 | +mkdir -p "${OUTPUT_DIR}" |
| 181 | +mv model.pte "${OUTPUT_DIR}/" |
| 182 | +mv aoti_${DEVICE}_blob.ptd "${OUTPUT_DIR}/" |
| 183 | +if [ -n "$PREPROCESSOR_OUTPUT" ]; then |
| 184 | + mv $PREPROCESSOR_OUTPUT "${OUTPUT_DIR}/" |
| 185 | +fi |
| 186 | +ls -al "${OUTPUT_DIR}" |
| 187 | +echo "::endgroup::" |
0 commit comments