Skip to content

Commit c70bcd1

Browse files
committed
Update multimodal docs with new models
1 parent be7748b commit c70bcd1

5 files changed

Lines changed: 184 additions & 4 deletions

File tree

docs/tutorials/posttraining/multimodal.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ This document provides a guide to use the multimodal functionalities in MaxText
88

99
We also provide a [colab](https://github.com/AI-Hypercomputer/maxtext/blob/main/src/maxtext/examples/multimodal_gemma3_demo.ipynb) for multimodal features demonstration. The following table provides a list of models and modalities we currently support:
1010

11-
| Models | Input Modalities | Output Modalities |
12-
| :--------------------------------------------- | :--------------- | :---------------- |
13-
| - Gemma3-4B/12B/27B<br>- Llama4-Scout/Maverick | Text, images | Text |
11+
| Models | Input: Text | Input: Image | Input: Video | Input: Audio | Output |
12+
| :-------------------------- | :---------: | :----------: | :----------: | :----------: | :----: |
13+
| **Gemma3** (4B/12B/27B) ||| | | Text |
14+
| **Gemma4** (26B/31B) ||| | | Text |
15+
| **Llama4** (Scout/Maverick) ||| | | Text |
16+
| **Qwen3-Omni** ||||| Text |
17+
| **Qwen3.5** (35B/397B) |||| | Text |
1418

1519
## Introduction
1620

@@ -73,6 +77,8 @@ MaxText supports multimodal decoding, allowing you to input text with multiple i
7377

7478
Since each model uses a unique native chatting template from its pretraining, we've implemented these specific templates within `multimodal_utils.py` and apply them directly to your prompt.
7579

80+
### Decode with text+image
81+
7682
To run a forward pass and verify the model's output, use the following command:
7783

7884
```shell
@@ -124,10 +130,47 @@ python -m maxtext.inference.decode \
124130

125131
For larger models such as Llama4-Scout/Maverick, we suggest to run the decoding on a TPU cluster such as v5p-16.
126132

133+
### Decode with text+video+audio
134+
135+
For models that support video input (e.g., Qwen3-Omni and Qwen3.5), pass a video file via `video_path`. For Qwen3-Omni, which also supports audio, set `use_audio_in_video=true` to additionally process the embedded audio track. Since the required token budget scales with video length and resolution, set `max_prefill_predict_length` accordingly.
136+
137+
```shell
138+
# Qwen3-Omni decode with video + audio
139+
export MAXTEXT_CKPT_PATH=<Checkpoint GCS path> # gs://my-bucket/path for Qwen3-Omni
140+
python -m maxtext.inference.decode \
141+
model_name=qwen3-omni-30b-a3b \
142+
tokenizer_path=Qwen/Qwen3-Omni-30B-A3B-Instruct \
143+
tokenizer_type=huggingface \
144+
load_parameters_path=${MAXTEXT_CKPT_PATH?}/0/items \
145+
per_device_batch_size=1 \
146+
run_name=ht_test \
147+
steps=1 \
148+
async_checkpointing=false \
149+
scan_layers=false \
150+
use_multimodal=true \
151+
use_audio_in_video=true \
152+
prompt='What can you see and hear? Answer in one short sentence.' \
153+
video_path='tests/assets/test_video.mp4' \
154+
max_prefill_predict_length=1250 \
155+
max_target_length=1280 \
156+
add_bos=false \
157+
attention='dot_product' \
158+
```
159+
160+
The expected output will look similar to:
161+
162+
```
163+
Input `<|im_start|>user
164+
<|vision_start|><|video_pad|><|vision_end|>What can you see and hear? Answer in one short sentence.<|im_end|>
165+
<|im_start|>assistant
166+
` -> `A roaring Tyrannosaurus rex animatronic is displayed in a museum exhibit.
167+
```
168+
127169
## Supervised Fine-Tuning
128170

129171
Supervised Fine-Tuning (SFT) of multimodal LLMs in MaxText focuses specifically on post-training; we don't yet support pre-training multimodal models from scratch. The SFT process typically involves training on Visual Question Answering (VQA) datasets where the model learns to generate accurate text responses based on both visual and textual inputs. During this fine-tuning phase, we recommend to freeze the pre-trained encoder layers (such as vision transformers) to preserve their learned visual representations, while the projection layers and LLM decoder components remain trainable. This selective training strategy allows the model to adapt the cross-modal alignment and text generation capabilities without disrupting the robust feature extraction abilities of the encoders, ultimately leading to improved performance on multimodal understanding and reasoning tasks while maintaining computational efficiency. This is achieved by setting `freeze_vision_encoder_params=True` in [sft-vision-chartqa.yml](https://github.com/AI-Hypercomputer/maxtext/blob/main/src/maxtext/configs/post_train/sft-vision-chartqa.yml).
130-
Here, we use [ChartQA](https://huggingface.co/datasets/HuggingFaceM4/ChartQA) as an example to demonstrate SFT functionality:
172+
173+
**Text+image SFT is supported for all models listed above.** The following example uses Gemma3-4B with the [ChartQA](https://huggingface.co/datasets/HuggingFaceM4/ChartQA) dataset:
131174

132175
```shell
133176
export MAXTEXT_CKPT_PATH=<your-checkpoints-path> # either set to an already available MaxText ckpt or to the one we just converted in the previous step

tests/end_to_end/tpu/gemma3/Run_Gemma3.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
We provide examples for checkpoint conversion and decoding/training/finetuning Gemma3 in test scripts at [tests/end_to_end/tpu/gemma3](https://github.com/AI-Hypercomputer/maxtext/tree/main/tests/end_to_end/tpu/gemma3).
2222

23+
For multimodal functionality, see the [Multimodal Support guide](../../../../../docs/tutorials/posttraining/multimodal.md).
24+
2325

2426
## Pre-training
2527
You can train from scratch to generate a new checkpoint. One example command to run pretraining Gemma3-4B model is as follows:

tests/end_to_end/tpu/llama4/Run_Llama4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* LLama4 Maverick (17B-128E)
2323
* Llama4 Maverick (17B-128E-Instruct)
2424

25+
For multimodal functionality, see the [Multimodal Support guide](../../../../docs/tutorials/posttraining/multimodal.md).
2526

2627
## Checkpoint conversion
2728
Currently, we support converting both [PyTorch](https://www.llama.com/) and [HuggingFace](https://huggingface.co/collections/meta-llama/llama-4-67f0c30d9fe03840bc9d0164) checkpoints. Note that we recommend using the `huggingface-cli download` command with environment variable
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
3+
# This file is documentation for how to get started with Qwen3-Omni-30B-A3B.
4+
# This e2e script was tested on a v5p-8 TPU VM.
5+
6+
# This file runs Step 1 on CPU/TPU.
7+
# 1. Convert the HuggingFace checkpoint (bf16) to MaxText-compatible checkpoint (bf16):
8+
# Unscanned format is used here as it is better suited for decoding.
9+
# 2. Run multimodal decoding: text+image, and text+video+audio.
10+
# ---
11+
# Example Usage:
12+
#
13+
# export HF_TOKEN=<your_hf_token>
14+
# export BASE_OUTPUT_PATH=gs://your-gcs-bucket/qwen3-omni-30b-a3b_maxtext_ckpt
15+
# bash tests/end_to_end/tpu/qwen/moe/qwen3-omni-30b-a3b/1_test_qwen3_omni_30b_a3b.sh
16+
# ---
17+
18+
set -ex
19+
20+
export MODEL_NAME="${MODEL_NAME:-qwen3-omni-30b-a3b}"
21+
export TOKENIZER_PATH="${TOKENIZER_PATH:-Qwen/Qwen3-Omni-30B-A3B-Instruct}"
22+
23+
# (Optional) Path to your local Hugging Face checkpoint
24+
export HF_MODEL_PATH="${HF_MODEL_PATH:-}"
25+
26+
# Base output path for MaxText checkpoint and SFT output.
27+
export BASE_OUTPUT_PATH="${BASE_OUTPUT_PATH:-gs://your-gcs-bucket/qwen3-omni-30b-a3b_maxtext_ckpt}"
28+
29+
if [ -z "${HF_TOKEN}" ]; then
30+
echo "Error: HF_TOKEN environment variable is not set. Please export your Hugging Face token."
31+
echo "Example: export HF_TOKEN=hf_..."
32+
exit 1
33+
fi
34+
35+
# Strip trailing slash from base path to avoid malformed URIs
36+
BASE_OUTPUT_PATH=${BASE_OUTPUT_PATH%/}
37+
echo "Using BASE_OUTPUT_PATH = ${BASE_OUTPUT_PATH}"
38+
39+
# Install torch for checkpoint conversion
40+
python3 -m pip install torch --index-url https://download.pytorch.org/whl/cpu
41+
42+
# Setup local HF path argument if one was provided
43+
HF_LOCAL_ARG=""
44+
if [ -n "${HF_MODEL_PATH}" ]; then
45+
HF_LOCAL_ARG="hf_model_path=${HF_MODEL_PATH}"
46+
fi
47+
48+
# ---
49+
# Step 1: Checkpoint Conversion
50+
# Convert HuggingFace checkpoint to MaxText unscanned format (better for decoding).
51+
# use_multimodal=true is required to include vision/audio encoder weights.
52+
# ---
53+
JAX_PLATFORMS=cpu python3 -m maxtext.checkpoint_conversion.to_maxtext src/maxtext/configs/base.yml \
54+
model_name=${MODEL_NAME} \
55+
base_output_directory=${BASE_OUTPUT_PATH}/unscanned \
56+
hf_access_token=${HF_TOKEN} \
57+
scan_layers=false \
58+
use_multimodal=true \
59+
--lazy_load_tensors=False \
60+
${HF_LOCAL_ARG}
61+
62+
UNSCANNED_CKPT_PATH=${BASE_OUTPUT_PATH}/unscanned/0/items
63+
64+
# ---
65+
# Step 2a: Multimodal Decode — text + image
66+
# Uses a test image from the repo assets.
67+
# max_prefill_predict_length accounts for image tokens (~256) + text prompt tokens.
68+
# ---
69+
python3 -m maxtext.inference.decode src/maxtext/configs/base.yml \
70+
model_name=${MODEL_NAME} \
71+
tokenizer_path=${TOKENIZER_PATH} \
72+
tokenizer_type=huggingface \
73+
load_parameters_path=${UNSCANNED_CKPT_PATH} \
74+
hf_access_token=${HF_TOKEN} \
75+
per_device_batch_size=1 \
76+
run_name=qwen3_omni_decode_image \
77+
steps=1 \
78+
async_checkpointing=false \
79+
scan_layers=false \
80+
use_multimodal=true \
81+
prompt='Describe this image in one sentence.' \
82+
image_path='tests/assets/test_image.jpg' \
83+
max_prefill_predict_length=512 \
84+
max_target_length=542 \
85+
add_bos=false \
86+
attention=dot_product \
87+
ici_tensor_parallelism=4
88+
89+
# ---
90+
# Step 2b: Multimodal Decode — text + video + audio
91+
# Passes a video file and enables audio processing from the video track.
92+
# max_prefill_predict_length is set higher to accommodate video frame tokens (~1126)
93+
# plus audio tokens (~77) plus text prompt tokens.
94+
# ---
95+
python3 -m maxtext.inference.decode src/maxtext/configs/base.yml \
96+
model_name=${MODEL_NAME} \
97+
tokenizer_path=${TOKENIZER_PATH} \
98+
tokenizer_type=huggingface \
99+
load_parameters_path=${UNSCANNED_CKPT_PATH} \
100+
hf_access_token=${HF_TOKEN} \
101+
per_device_batch_size=1 \
102+
run_name=qwen3_omni_decode_video \
103+
steps=1 \
104+
async_checkpointing=false \
105+
scan_layers=false \
106+
use_multimodal=true \
107+
use_audio_in_video=true \
108+
prompt='What can you see and hear? Answer in one short sentence.' \
109+
video_path='tests/assets/test_video.mp4' \
110+
max_prefill_predict_length=1240 \
111+
max_target_length=1280 \
112+
add_bos=false \
113+
attention=dot_product \
114+
ici_tensor_parallelism=4

tests/end_to_end/tpu/qwen/moe/run_qwen_moe.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Qwen3 is a family of open-source large language models from the Qwen team at Ali
99

1010
- **Qwen3-480B-A35B**
1111

12+
- **Qwen3-Omni-30B-A3B**
13+
1214
- **Qwen3.5-397B-A17B**
1315

1416
- **Qwen3.5-35B-A3B**
@@ -17,6 +19,8 @@ For more details on Qwen3 architecture, see the [Qwen3 Technical Report](https:/
1719

1820
For more details on Qwen3.5 architecture, see the [Qwen3.5 Blog](https://qwen.ai/blog?id=qwen3.5)
1921

22+
For multimodal functionality (image, video, and audio input), see the [Multimodal Support guide](../../../../../docs/tutorials/posttraining/multimodal.md).
23+
2024
* * * * *
2125

2226
Checkpoint Conversion
@@ -153,6 +157,22 @@ export MAXTEXT_CHECKPOINT_PATH=gs://your-gcs-bucket/qwen3-480b-a35b_maxtext_ckpt
153157
bash tests/end_to_end/tpu/qwen/moe/qwen3-480b-a35b/1_test_qwen3_480b_a35b.sh
154158
```
155159

160+
### Qwen3-Omni-30B-A3B
161+
162+
```bash
163+
# 1. Export your Hugging Face token
164+
export HF_TOKEN="your_hf_token_here"
165+
166+
# 2. Set the base path for conversion and SFT outputs
167+
export BASE_OUTPUT_PATH=gs://<YOUR-GCS-BUCKET>/qwen3-omni-30b-a3b_maxtext_ckpt
168+
169+
# (Optional) Set the path if you are using a local Hugging Face checkpoint instead of downloading
170+
# export HF_MODEL_PATH=/path/to/local/qwen3-omni-30b-a3b_hf_checkpoint
171+
172+
# 3. Execute the conversion and multimodal decode verification
173+
bash tests/end_to_end/tpu/qwen/moe/qwen3-omni-30b-a3b/1_test_qwen3_omni_30b_a3b.sh
174+
```
175+
156176
### Qwen3.5-35B-A3B
157177

158178
```bash

0 commit comments

Comments
 (0)