#!/usr/bin/env python3 """How much of a multimodal request's compute is the vision encoder? Qwen3-VL-8B, counted in FLOPs. Shapes from Qwen/Qwen3-VL-8B-Instruct config.json: vision tower 27 layers, hidden 1152, MLP 4304, 16x16 patches, 2x2 spatial merge (4 patches -> 1 language token), full attention over each image's patches, DeepStack mergers at layers 8, 16, 24; language model 36 layers, hidden 4096, MLP 12288, 32 query heads / 8 KV heads of 128, about 8.2B parameters outside the vision tower. FLOPs only (2 per multiply-add). The encoder and the language model's prefill are both compute-bound, so the FLOP ratio is a fair first estimate of their time ratio on the same GPU; decode is memory-bound and is reported separately in tokens, not FLOPs. python3 encoder_share.py """ V_LAYERS, V_H, V_MLP, PATCH, MERGE = 27, 1152, 4304, 16, 2 L_LAYERS, L_H, L_MLP, L_Q, L_KV, L_HD = 36, 4096, 12288, 32, 8, 128 OUT = 4096 # merger output width = language hidden size def vit_flops(w, h): n = (w // PATCH) * (h // PATCH) embed = 2 * n * (3 * 2 * PATCH * PATCH) * V_H # temporal patch of 2 frames linear = 2 * n * V_LAYERS * (4 * V_H * V_H + 2 * V_H * V_MLP) attn = V_LAYERS * 4 * n * n * V_H # QK^T and AV, full attention per image tokens = n // (MERGE * MERGE) merger_in = V_H * MERGE * MERGE merger = 4 * 2 * tokens * (merger_in * merger_in + merger_in * OUT) # main + 3 DeepStack mergers return embed + linear + attn + merger, n, tokens def llm_prefill_flops(t_new, t_ctx=0): per_token = 2 * (L_LAYERS * (L_H * (L_Q + 2 * L_KV) * L_HD + L_Q * L_HD * L_H + 3 * L_H * L_MLP)) attn = L_LAYERS * 4 * L_Q * L_HD * (t_new * t_ctx + t_new * t_new / 2) # causal return per_token * t_new + attn def main(): print("one image encoder TFLOP patches LLM tokens prefill of those tokens TFLOP encoder / prefill encoder share") for w, h in [(512, 512), (1024, 1024), (1536, 1536), (2048, 2048)]: e, n, t = vit_flops(w, h) p = llm_prefill_flops(t) print(f" {w}x{h:<14d} {e / 1e12:12.1f} {n:9d} {t:11d} {p / 1e12:30.1f} {e / p:18.2f} {e / (e + p):14.0%}") print("\nwhole requests (images + text prompt; output tokens are decode, not counted in FLOPs)") cases = [ ("chat: 1 photo 1024x1024, 300 text tokens, 400 out", [(1024, 1024)], 300, 400), ("document QA: 5 pages 1536x1536, 200 text, 6 out", [(1536, 1536)] * 5, 200, 6), ("screenshot agent: 1 screen 2048x1152, 6,000 history recomputed, 150 out", [(2048, 1152)], 6000, 150), ("screenshot agent, history in the prefix cache: 1 new screen + 100 new tokens", [(2048, 1152)], (100, 6000), 150), ] for label, imgs, text, out in cases: e = sum(vit_flops(w, h)[0] for w, h in imgs) img_tokens = sum(vit_flops(w, h)[2] for w, h in imgs) new_text, cached = text if isinstance(text, tuple) else (text, 0) text = new_text p = llm_prefill_flops(img_tokens + text, cached) print(f" {label}\n encoder {e / 1e12:6.1f} TFLOP, prefill {p / 1e12:6.1f} TFLOP " f"({img_tokens + text:,} new tokens{f' on {cached:,} cached' if cached else ''}): " f"encoder = {e / (e + p):.0%} of the prompt's compute; {out} tokens to decode") if __name__ == "__main__": main()