Skip to content

mtmd: cast the attention mask to f16 once, not once per layer - #30241

Closed
Peterc3-dev wants to merge 2 commits into
ggml-org:masterfrom
Peterc3-dev:mtmd-mask-cast-once
Closed

Peterc3-dev wants to merge 2 commits into
ggml-org:masterfrom
Peterc3-dev:mtmd-mask-cast-once

Conversation

@Peterc3-dev

Copy link
Copy Markdown

What

With flash attention, clip_graph::build_attn cast the mask to f16 on every call. A same-type ggml_cast is still a full GGML_OP_CPY, so:

  • qwen2vl.cpp already casts its window mask to f16 once, but each of the 28 windowed layers still ran a redundant f16→f16 copy of the whole n_pos² mask.
  • Models that pass an f32 mask through build_vit_opts (attn_mask / attn_mask_layers) ran one f32→f16 cast per layer.

Two small changes:

  1. build_attn: only cast when kq_mask->type != GGML_TYPE_F16.
  2. build_vit: cast each distinct mask once before the layer loop (deduplicated by tensor pointer), gated on flash_attn_type == CLIP_FLASH_ATTN_TYPE_ENABLED. The non-FA path is unchanged.

Measured (Vulkan, AMD Radeon 890M / RDNA 3.5, RADV, Mesa 26.2.4)

Qwen2.5-VL-3B (Q4_K_M text, f16 mmproj), 1024×1024 image = 5476 patch tokens, -ngl 99, --temp 0:

CPY ops in encode CPY time encode (median of 4, alternating)
master d1d3c33 93 120 ms 2648 ms
this PR 65 85 ms 2593 ms

Generated text is byte-identical. SmolVLM2-2.2B (goes through build_vit, no mask) also byte-identical, same encode time.

Not exercised on hardware here: a model that passes a mask via build_vit_opts (deepseekocr2, mimo-audio, muse-glimmer). The change there is the same cast, moved earlier.

🤖 Generated with Claude Code

https://claude.ai/code/session_017V9bu3AeCUmiCUhbB8ab2u

Peterc3-dev and others added 2 commits October 9, 2026 12:39
A same-type ggml_cast is still a full copy. Callers that cast the
window mask to f16 once (qwen2vl) still paid one 2x(n_pos^2) f16 copy
per windowed layer. On Qwen2.5-VL-3B at 1024x1024 (5476 patch tokens,
28 windowed layers) that is 28 CPY ops, about 34 ms of a 2.6 s encode
on a Radeon 890M (Vulkan). Output is unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017V9bu3AeCUmiCUhbB8ab2u
With flash attention, build_attn cast the f32 mask to f16 on every
layer. Models that pass the same mask to many layers through
build_vit_opts (attn_mask or attn_mask_layers) paid one n_pos^2 copy
per layer. Cast each distinct mask once before the layer loop; the
per-layer cast in build_attn is now skipped because the mask is f16.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017V9bu3AeCUmiCUhbB8ab2u
@Peterc3-dev
Peterc3-dev requested a review from a team as a code owner October 9, 2026 16:47
@github-actions github-actions Bot added the mtmd Related to multimodal functionality (video/image/audio) label Oct 9, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Oct 9, 2026

Copy link
Copy Markdown

Hi @Peterc3-dev, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

  • AI-generated content: While code is allowed to be generated by AI, please write the PR description and commit messages on your own without the help of AI.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@ggml-gh-bot ggml-gh-bot Bot added the draft PR will be changed to draft by github-actions bot label Oct 9, 2026
@github-actions
github-actions Bot marked this pull request as draft October 9, 2026 16:54
@github-actions github-actions Bot removed the draft PR will be changed to draft by github-actions bot label Oct 9, 2026
@ngxson

ngxson commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

this is quite hacky, proper impl is to set f16 from input, same pattern as llama-graph. PTAL: #30259

@ngxson

ngxson commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

closing since this PR is not respecting AGENTS.md

@ngxson ngxson closed this Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mtmd Related to multimodal functionality (video/image/audio)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants