|
| 1 | +# Summary |
| 2 | + |
| 3 | +This example demonstrates how to export and run Mistral's [Voxtral](https://huggingface.co/mistralai/Voxtral-Mini-3B-2507) audio multimodal model locally on ExecuTorch. |
| 4 | + |
| 5 | +# Exporting the model |
| 6 | +To export the model, we use [Optimum ExecuTorch](https://github.com/huggingface/optimum-executorch), a repo that enables exporting models straight from the source - from HuggingFace's Transformers repo. |
| 7 | + |
| 8 | +## Setting up Optimum ExecuTorch |
| 9 | +Install through pip package: |
| 10 | +``` |
| 11 | +pip install optimum-excecutorch |
| 12 | +``` |
| 13 | + |
| 14 | +Or install from source: |
| 15 | +``` |
| 16 | +git clone https://github.com/huggingface/optimum-executorch.git |
| 17 | +cd optimum-executorch |
| 18 | +python install_dev.py |
| 19 | +``` |
| 20 | + |
| 21 | +We are currently working on a Transformers pin bump for Optimum. In the meantime, manually override the Transformers dep to the earliest compatible version. |
| 22 | +``` |
| 23 | +pip install git+https://github.com/huggingface/transformers@6121e9e46c4fc4e5c91d9f927aef5490691850cf#egg=transformers |
| 24 | +``` |
| 25 | + |
| 26 | +## Using the export CLI |
| 27 | +We export Voxtral using the Optimum CLI, which will export `model.pte` to the `voxtral` output directory: |
| 28 | +``` |
| 29 | +optimum-cli export executorch |
| 30 | + --model "mistralai/Voxtral-Mini-3B-2507" |
| 31 | + --task "multimodal-text-to-text" |
| 32 | + --recipe "xnnpack" |
| 33 | + --use_custom_sdpa |
| 34 | + --use_custom_kv_cache |
| 35 | + --qlinear 8da4w |
| 36 | + --qembedding 4w |
| 37 | + --output_dir="voxtral |
| 38 | +``` |
| 39 | + |
| 40 | +This exports Voxtral with XNNPack backend acceleration and 4-bit weight/8-bit activation linear quantization. |
| 41 | + |
| 42 | +# [Optional] Exporting the audio preprocessor |
| 43 | +The exported model takes in a mel spectrogram input tensor as its audio inputs. |
| 44 | +We provide a simple way to transform raw audio data into a mel spectrogram by exporting a version of Voxtral's audio preprocessor used directly by Transformers. |
| 45 | + |
| 46 | +``` |
| 47 | +python -m executorch.extension.audio.mel_spectrogram --feature_size 128 --output_file voxtral_preprocessor.pte |
| 48 | +``` |
| 49 | + |
| 50 | +# Running the model |
| 51 | +To run the model, we will use the Voxtral runner, which utilizes ExecuTorch's MultiModal runner API. |
| 52 | +The Voxtral runner will do the following things: |
| 53 | +1. [Optional] Pass the raw audio tensor into exported preprocessor to produce a mel spectrogram tensor. |
| 54 | +2. [If starting directly with an already processed audio input tensor] Format the inputs to the multimodal runner (metadata tokens, audio tokens, text tokens, etc.). |
| 55 | +3. Feed the formatted inputs to the multimodal modal runner. |
| 56 | + |
| 57 | +## Building the multimodal runner |
| 58 | +``` |
| 59 | +# Build and install ExecuTorch |
| 60 | +cmake --preset llm -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=cmake-out -DEXECUTORCH_ENABLE_LOGGING=ON && cmake --build cmake-out -j16 --target install --config Release |
| 61 | +
|
| 62 | +# Build and install Voxtral runner |
| 63 | +cmake -DCMAKE_INSTALL_PREFIX=cmake-out -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release -Bcmake-out/examples/models/voxtral examples/models/voxtral && cmake --build cmake-out/examples/models/voxtral -j16 --config Release |
| 64 | +``` |
| 65 | + |
| 66 | +## Running the model |
| 67 | +You can download the `tekken.json` tokenizer from [Voxtral's HuggingFace repo](https://huggingface.co/mistralai/Voxtral-Mini-3B-2507). |
| 68 | +``` |
| 69 | +./cmake-out/examples/models/voxtral/voxtral_runner |
| 70 | + --model_path voxtral/model.pte |
| 71 | + --tokenizer_path path/to/tekken.json |
| 72 | + --prompt "What can you tell me about this audio?" |
| 73 | + --audio_path ~/models/voxtral/audio_input.bin |
| 74 | +``` |
| 75 | + |
| 76 | +Example output: |
| 77 | +``` |
| 78 | +The speaker in this audio seems to be talking about their concerns about a device called the model or maybe they're just talking about the model in general. They mention that the model was trained with the speaker for inference, which suggests that |
| 79 | + the model was trained based on the speaker's data or instructions. They also mention that the volume is quite small, which could imply that the speaker is trying to control the volume of the model's output, likely because they are concerned about how loud the model's responses might |
| 80 | +PyTorchObserver {"prompt_tokens":388,"generated_tokens":99,"model_load_start_ms":0,"model_load_end_ms":0,"inference_start_ms":1756351346381,"inference_end_ms":1756351362602,"prompt_eval_end_ms":1756351351435,"first_token_ms":1756351351435,"aggregate_sampling_time_ms":99,"SCALING_FACTOR_UNITS_PER_SECOND":1000} |
| 81 | +I 00:00:24.036773 executorch:stats.h:104] Prompt Tokens: 388 Generated Tokens: 99 |
| 82 | +I 00:00:24.036800 executorch:stats.h:110] Model Load Time: 0.000000 (seconds) |
| 83 | +I 00:00:24.036805 executorch:stats.h:117] Total inference time: 16.221000 (seconds) Rate: 6.103200 (tokens/second) |
| 84 | +I 00:00:24.036815 executorch:stats.h:127] Prompt evaluation: 5.054000 (seconds) Rate: 76.770875 (tokens/second) |
| 85 | +I 00:00:24.036819 executorch:stats.h:136] Generated 99 tokens: 11.167000 (seconds) Rate: 8.865407 (tokens/second) |
| 86 | +I 00:00:24.036822 executorch:stats.h:147] Time to first generated token: 5.054000 (seconds) |
| 87 | +I 00:00:24.036828 executorch:stats.h:153] Sampling time over 487 tokens: 0.099000 (seconds) |
| 88 | +``` |
| 89 | + |
| 90 | +You can easily produce an `.bin` for the audio input in Python like this: |
| 91 | +``` |
| 92 | +# t = some torch.Tensor |
| 93 | +with open("tensor.bin", "wb") as f: |
| 94 | + f.write(t.numpy().tobytes()) |
| 95 | +``` |
0 commit comments