Skip to content

mtmd: cast vision K/V before attention permutation - #207

Draft
liangliangchang wants to merge 1 commit into
gfx11from
lichang.vision-kv-cast-gfx11
Draft

liangliangchang wants to merge 1 commit into
gfx11from
lichang.vision-kv-cast-gfx11

Conversation

@liangliangchang

Copy link
Copy Markdown

Moves the vision K/V conversion to FP16 before the attention permutation, so eligible FP32 tensors use a contiguous conversion instead of a strided copy. The values are still converted to FP16 at the same point in the mathematical computation; attention precision and image budgets are unchanged.

Enable with MTMD_VISION_KV_CAST_EARLY=1. The existing path remains the default and is also available with =0. The guard requires both K and V to be FP32; other types keep their existing path.

Fresh Qwen3-VL-4B HTTP TTFT tests on Strix Halo, with identical model files, requests, prompt positions, microbatch size, and checkpoints/cache reuse disabled. Each arm has two warmups per process and ten retained measurements across an A/B/B/A sequence.

Test Cast after permutation Cast before permutation Time saved
This PR alone on gfx11 1023.00 ms 999.48 ms 2.30%
Combined optimization stack 679.96 ms 662.97 ms 2.50%

The earlier combined-stack trace had 48 strided conversion launches costing 17.68 ms per prefill. This change addresses that traffic. Gemma-4-E2B was essentially unchanged in isolation (0.12%); the improvement is workload dependent.

Built independently from gfx11 and exercised through complete VLM requests. Source style and whitespace checks passed. This PR has no dependency on the attention, BLAS, or MMDQ PRs. The Qwen window PR edits the nearby conversion guards, so combining them needs a small source conflict resolution that retains both behaviors.

AI assistance: Codex helped with the implementation, validation, and description. The change is submitted to the AMD fork for human review.

Use contiguous FP32-to-FP16 conversion before constructing attention views. Keep the existing path available through MTMD_VISION_KV_CAST_EARLY.

Assisted-by: Codex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant