Skip to content

Commit 93f0f55

Browse files
titaiwangmsCopilot
andauthored
Add Ministral-3-3B VLM olive-recipe demo + enhance genai integration (#161)
## Summary Add an end-to-end olive-recipe demo for Ministral-3-3B VLM and enhance the mobius genai integration with Pixtral processor config support and tokenizer class remapping. ## Changes ### Core: genai integration enhancements (`src/mobius/integrations/ort_genai/`) - **`_write_processor_config`**: Enhanced to generate full ORT-extensions image transform pipeline for VL models. Dispatches by model type (Pixtral: longest_edge resize; generic: smart_resize). Derives normalization params from HF processor config. - **`_fix_tokenizer_config`**: New function with `_TOKENIZER_CLASS_REMAP` dict to remap unsupported tokenizer classes (e.g. `TokenizersBackend` → `LlamaTokenizer`). Called automatically from `write_ort_genai_config()`. - 4 new tests (31 total passing) ### Example: olive-recipe demo (`examples/olive/ministral-3-3b-vlm/`) - **optimize.py**: Pure mobius export (`build()` → `save()` → `write_ort_genai_config()`) with optional Olive quantization via `--olive-config` - **inference.py**: ORT GenAI multimodal inference (text-only, image+text, interactive) - **eval.py**: AI2D benchmark evaluation (ONNX vs PyTorch comparison) - Olive configs for CPU (INT4) and CUDA (FP16) quantization - README with setup, export, inference, and evaluation instructions ### Skills - New `olive-recipe` skill documenting the mobius + Olive hybrid pipeline pattern - Updated `ort-genai-config` skill with Pixtral processor config and tokenizer remap info ## References - [Issue #158](#158) - [PR #130](#130) (Pixtral/Ministral3 VLM support) - [olive-recipes PR #352](https://github.com/microsoft/olive-recipes/pull/352) - [onnxruntime-genai PR #2077](https://github.com/microsoft/onnxruntime-genai/pull/2077) Closes #158 --------- Signed-off-by: Ti-Tai Wang <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent c35d0c1 commit 93f0f55

12 files changed

Lines changed: 1554 additions & 24 deletions

File tree

‎.github/copilot-instructions.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,5 @@ See the `adding-a-new-model` skill for the full guide.
158158
### Git commits
159159

160160
Linear commit history. Always signoff commits with --signoff.
161+
162+
- **Write descriptive commit messages.** Title should summarize the change; body should explain what and why.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Generated model artifacts
2+
models/
3+
output/
4+
5+
# Python bytecode
6+
__pycache__/
7+
*.pyc
8+
9+
# Olive cache
10+
.olive-cache/
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"input_model": {
3+
"type": "HfModel",
4+
"model_path": "mistralai/Ministral-3-3B-Instruct-2512"
5+
},
6+
"passes": {
7+
"convert": {
8+
"type": "ModelBuilder",
9+
"precision": "int4",
10+
"int4_accuracy_level": 4,
11+
"extra_options": {
12+
"filename": "text.onnx"
13+
}
14+
}
15+
},
16+
"no_artifacts": true,
17+
"output_dir": "cpu_and_mobile/models/text.onnx"
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"input_model": {
3+
"type": "HfModel",
4+
"model_path": "mistralai/Ministral-3-3B-Instruct-2512"
5+
},
6+
"passes": {
7+
"convert": {
8+
"type": "ModelBuilder",
9+
"precision": "fp16",
10+
"extra_options": {
11+
"filename": "text.onnx"
12+
}
13+
}
14+
},
15+
"engine": {
16+
"target": {
17+
"type": "LocalSystem",
18+
"accelerators": [
19+
{
20+
"device": "gpu",
21+
"execution_providers": [
22+
"CUDAExecutionProvider"
23+
]
24+
}
25+
]
26+
}
27+
},
28+
"no_artifacts": true,
29+
"output_dir": "cuda/models/text.onnx"
30+
}

0 commit comments

Comments
 (0)