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