Qwen3-VL
Introduction
This codebase includes the Qwen3-VL family of models and currently supports the model variants:
- Qwen3-VL-4B-Instruct: Qwen/Qwen3-VL-4B-Instruct
- Qwen3-VL-8B-Instruct: Qwen/Qwen3-VL-8B-Instruct
Set environment variables
# at $TT_METAL_HOME
source env_set.sh qwen3
Prerequisites
Install additional python dependencies:
pip install -r models/demos/qwen3_vl/requirements.txt
How to Run
For a single user example:
HF_MODEL=<model_name> pytest models/bos_model/qwen3_vl/demo/demo.py -k 'performance and batch-one' --img_size 256 256
Notes:
<model_name>is the HuggingFace model repo string, e.g.Qwen/Qwen3-VL-4B-Instruct,Qwen/Qwen3-VL-8B-Instruct-kis the pytest filter; to run a specific test, use-k <test_name>; additional test names are listed inmodels/bos_model/qwen3_vl/demo/demo.py- You can use
batch-one,batch-two,batch-4, ... for batch size selection combined withperformanceoraccuracy. - Currently batch sizes only support 1, 2, 4, 8, 16, 20
performance: use BFP8 weight datatype casting for improved inference speedaccuracy: use BF16 weight datatype for accurate output
- You can use
--img_size- optional flag to specify the input resolution for vision (e.g.--img_size 256 256)- The default setting is to use the full resolution of each image, but it can be resized by HF's smart resizer before model running.
- The built-in batch-16 and batch-20 cases use
256 256when--img_sizeis omitted. Qwen3-VL keeps the text embedding and three deepstack streams live during the merge, so this deterministic high-batch size avoids exceeding single-device DRAM inaccuracymode. An explicit--img_sizestill takes precedence.
Tracy report:
HF_MODEL=<model_name> python -m tracy --op-support-count 10000 -m -r -p -v "pytest models/bos_model/qwen3_vl/demo/demo.py -k 'performance and profiler' --timeout 10000"
profileris a special test case for tracy recording or ttnn-visualizer. We've fixed profiling environment for stable device time analysis, so please use this test case for profiling.- img_size = [256, 256] (force resizing), max_batch_size = 1, repeat_batches = 1, max_seq_len = 4096, max_generated_tokens = 3, stop_at_eos = False
- This produces one prefill token and two decode tokens. Decode TPS excludes the prefill token and compile overhead; the second decode provides the steady-state timing sample.
- The default Tracy command keeps model trace capture enabled;
TT_METAL_DEVICE_PROFILER=1alone is compatible with this traced path. --op-support-count 10000reserves enough device-profiler DRAM space between the profiler reads performed at safe demo/generator boundaries.
- Tracy's output is saved in
generated/profiler/reports. For how to view the reports, please refer to tt-perf-report.
Run for ttnn-visualizer Profiler
-
Install
ttnn-visualizerin the active Python environment:python3 -m pip install ttnn-visualizer -
First, export ENV using script file
source models/bos_model/export_l1_vis.sh <EXPERIMENT_NAME><EXPERIMENT_NAME>: set the experiment name as you like (for example,qwen3_vl)
-
Second, run model
HF_MODEL=<model_name> pytest models/bos_model/qwen3_vl/demo/demo.py -k 'performance and profiler' --timeout 10000--timeoutis required because per-op graph logging makes a trace-disabled vision run substantially slower than its normal execution. The demo's pytest timeout marker also selects 10000 seconds while graph reporting is enabled, so it does not override this command with the normal 900-second limit.- The Hugging Face reference model is loaded before TTNN graph tracing starts. The exported TTNN logging/graph configuration makes the demo automatically disable model trace capture for this visualizer run.
- If the model has finished running successfully, the result report will be generated in the following path (
generated/ttnn/reports/<EXPERIMENT_NAME>_MMDD_hhmm/)
-
Third, run ttnn-visualizer
ttnn-visualizer --profiler-path <REPORT_PATH><REPORT_PATH>: It is the path mentioned in the previous step- visit http://localhost:8000/ using your web-browser
-
If the experiment has finished, please run the following command to clear the environment variables
source models/bos_model/unset_l1_vis.sh
Model Runner (For SQA)
Sequence length requirements
max_seq_lenis the total context budget for the text prompt, image/video tokens, chat-template tokens, and generated tokens.- The Model Runner argument
max_gen_len(CLI:--max_gen_len) reserves the maximum number of generated tokens. max_seq_lenmust be strictly greater thanmax_gen_len, but this is only the minimum requirement. The processed multimodal prompt and generated tokens must satisfymultimodal_prompt_tokens + max_gen_len <= max_seq_len.- Higher image resolutions, additional images, and more video frames consume more input tokens. If the input exceeds the limit, reduce
--res, shorten the prompt, reducemax_gen_len, or increasemax_seq_len.
CLI
HF_MODEL=Qwen/Qwen3-VL-8B-Instruct python models/bos_model/qwen3_vl/run_qwen3.py
- Please check
--helpfor other option info
Python REPL
- Execute Python with HF_MODEL set
HF_MODEL=Qwen/Qwen3-VL-8B-Instruct python - Run the model
>>> from models.bos_model.qwen3_vl.run_qwen3 import get_mesh_device, close_mesh_device, qwen3_runner
>>> mesh_device = get_mesh_device()
>>> result = qwen3_runner(device=mesh_device, batch_size=1, num_iters=1)
>>> close_mesh_device(mesh_device) - Test with custom prompt
>>> mesh_device = get_mesh_device()
>>> result2 = qwen3_runner(device=mesh_device, batch_size=1, num_iters=1, input_images=["https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"], queries=["please describe this image"])
>>> result3 = qwen3_runner(device=mesh_device, batch_size=2, num_iters=1, input_images=["https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",""], queries=["please describe this image","please count 1 to 50"])
>>> close_mesh_device(mesh_device)
Streaming-LLM
-
StreamingLLM-style decode can be enabled with:
HF_MODEL=Qwen/Qwen3-VL-8B-Instruct python models/bos_model/qwen3_vl/run_qwen3.py --enable_streaming_llm --res 256 256--res 256 256keeps the multimodal input size deterministic and aligned with the Qwen3-VL profiling configuration. This keeps the first sink blocks and the latest recent blocks visible through the paged KVpage_table. We refer to the paper and streaming-llm github. -
This streaming LLM implementation is supported only when page attention is enabled.
-
Differences from the original implementation:
- Block-granular sink/recent window: TT paged attention addresses KV cache by page blocks, so requested sink/recent sizes are rounded to
page_block_size. For example,sink_token=4withpage_block_size=32keeps one full sink block, i.e. 32 tokens. - No re-shifting RoPE for key cache: The original implementation applies RoPE differently to Q and K, using normal
position_idsfor Q and cache-localkey_position_idsfor K. In this TT path, KV cache stores K after RoPE has already been applied, so cached keys cannot be re-rotated by only changingpage_table. Instead, compact decode positions keep absolute RoPE behavior by adjustingrope_delta. - Decode-stage only: The original implementation overrides the attention
forward, so the same StreamingLLM behavior is used through the model forward path. In TT, prefill and decode use separate paths, and Qwen-VL prefill can run through chunked prefill, so this implementation applies StreamingLLM only to the decode-stage view for now. Prefill still uses the normal TT prefill path.
- Block-granular sink/recent window: TT paged attention addresses KV cache by page blocks, so requested sink/recent sizes are rounded to