|
| 1 | +# Ministral-3-3B VLM: E2E Export & Inference Demo |
| 2 | + |
| 3 | +This example demonstrates how to export |
| 4 | +[Ministral-3-3B-Instruct-2512](https://huggingface.co/mistralai/Ministral-3-3B-Instruct-2512) |
| 5 | +vision-language model to ONNX using |
| 6 | +[mobius](https://github.com/onnxruntime/mobius) and run inference |
| 7 | +with ONNX Runtime GenAI. |
| 8 | + |
| 9 | +Ministral-3-3B is a multimodal (VLM) model combining a Pixtral |
| 10 | +vision encoder with a Mistral text decoder. All three sub-models |
| 11 | +are exported via mobius, with optional Olive quantization for the |
| 12 | +text decoder. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +Install mobius from the repo root: |
| 17 | + |
| 18 | +```bash |
| 19 | +pip install -e '.[transformers]' |
| 20 | +``` |
| 21 | + |
| 22 | +Install additional dependencies: |
| 23 | + |
| 24 | +```bash |
| 25 | +pip install -r requirements.txt |
| 26 | +``` |
| 27 | + |
| 28 | +Install ONNX Runtime GenAI: |
| 29 | + |
| 30 | +| Device | Install Command | |
| 31 | +|--------|------------------| |
| 32 | +| CPU | `pip install onnxruntime-genai` | |
| 33 | +| GPU | `pip install onnxruntime-genai-cuda` | |
| 34 | + |
| 35 | +Optional — for INT4/FP16 text decoder quantization: |
| 36 | + |
| 37 | +```bash |
| 38 | +pip install olive-ai |
| 39 | +``` |
| 40 | + |
| 41 | +## Steps |
| 42 | + |
| 43 | +### 1. Export Models |
| 44 | + |
| 45 | +**Pure mobius (FP16, all 3 models):** |
| 46 | + |
| 47 | +```bash |
| 48 | +python optimize.py |
| 49 | +``` |
| 50 | + |
| 51 | +**With Olive INT4 quantization for text decoder (CPU):** |
| 52 | + |
| 53 | +```bash |
| 54 | +python optimize.py --olive-config cpu_and_mobile/text.json |
| 55 | +``` |
| 56 | + |
| 57 | +**With Olive FP16 for text decoder (CUDA):** |
| 58 | + |
| 59 | +```bash |
| 60 | +python optimize.py --olive-config cuda/text.json --ep cuda |
| 61 | +``` |
| 62 | + |
| 63 | +**Custom options:** |
| 64 | + |
| 65 | +```bash |
| 66 | +python optimize.py --output-dir output/ministral3 --dtype f32 |
| 67 | +``` |
| 68 | + |
| 69 | +This runs: |
| 70 | +- **Mobius** for all 3 ONNX models (text decoder, vision |
| 71 | + encoder, embedding) with pretrained weights |
| 72 | +- **Mobius genai integration** for `genai_config.json`, |
| 73 | + `processor_config.json`, and tokenizer files |
| 74 | +- **(Optional) Olive** for text decoder quantization |
| 75 | + (INT4/FP16 with GQA) |
| 76 | + |
| 77 | +### 2. Output Structure |
| 78 | + |
| 79 | +``` |
| 80 | +models/ |
| 81 | +├── decoder/ |
| 82 | +│ ├── model.onnx # Text decoder |
| 83 | +│ └── model.onnx.data |
| 84 | +├── vision/ |
| 85 | +│ ├── model.onnx # Pixtral vision encoder |
| 86 | +│ └── model.onnx.data |
| 87 | +├── embedding/ |
| 88 | +│ ├── model.onnx # Embedding fusion model |
| 89 | +│ └── model.onnx.data |
| 90 | +├── genai_config.json # Runtime configuration |
| 91 | +├── processor_config.json # Image preprocessing |
| 92 | +├── tokenizer.json |
| 93 | +└── tokenizer_config.json |
| 94 | +``` |
| 95 | + |
| 96 | +### 3. Run Inference |
| 97 | + |
| 98 | +```bash |
| 99 | +# Text-only |
| 100 | +python inference.py --prompt "What is the capital of France?" |
| 101 | + |
| 102 | +# Image + text |
| 103 | +python inference.py --image photo.jpg --prompt "Describe this" |
| 104 | + |
| 105 | +# Interactive mode |
| 106 | +python inference.py --interactive |
| 107 | +``` |
| 108 | + |
| 109 | +### 4. Evaluate on AI2D |
| 110 | + |
| 111 | +```bash |
| 112 | +# ONNX only (default: 100 samples) |
| 113 | +python eval.py --model_path models |
| 114 | + |
| 115 | +# Both ONNX and PyTorch side-by-side |
| 116 | +python eval.py --pytorch_model mistralai/Ministral-3-3B-Instruct-2512 |
| 117 | +``` |
| 118 | + |
| 119 | +## Notes |
| 120 | + |
| 121 | +- HuggingFace checkpoint uses FP8 quantized weights. Mobius |
| 122 | + dequantizes automatically during weight loading. |
| 123 | +- The tokenizer class (`TokenizersBackend`) is automatically |
| 124 | + remapped to `LlamaTokenizer` by the genai integration. |
| 125 | +- Pixtral vision supports dynamic image sizes (multiples of |
| 126 | + 28, up to 1540x1540). |
| 127 | + |
| 128 | +## References |
| 129 | + |
| 130 | +- [Pixtral/Ministral3 VLM support (PR #130)](https://github.com/onnxruntime/mobius/pull/130) |
| 131 | +- [onnxruntime-genai PR #2077](https://github.com/microsoft/onnxruntime-genai/pull/2077) |
0 commit comments