Skip to content

Run multi-head latent attention models under continuous batching - #50365

Open
qgallouedec wants to merge 6 commits into
cb-indexed-attentionfrom
cb-mla
Open

qgallouedec wants to merge 6 commits into
cb-indexed-attentionfrom
cb-mla

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What does this PR do?

Stacked on #50359. Runs multi-head latent attention models under continuous batching: DeepSeek V2/V3, Kimi K2 and the models built on them, plus the sparse-attention ones (DeepSeek V3.2, GLM-5, Mistral Large 4). DeepSeek V3 fails today on the first forward: the engine caches the expanded keys in a row sized from head_dim. The layers now declare their compressed latent and rotary key as rows, write and read them themselves, and rebuild keys and values before attention. The DSA indexers cache their keys as one more row and select on the packed batch under the engine's mask.

  • New test: engine logprobs match a plain forward (1e-6, fp32) for DeepSeek V3, V3.2 and GLM-5 with 8-key indexers; misordered rows fail it.

Part of #50352

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Pages changed in this PR:

The latent attention layers (DeepSeek V2/V3, Kimi K2 and the models built on them) declare their compressed
latent and rotary key as cache rows, write and read them through the cache, and rebuild the keys and values of
all attended tokens before the attention. The cache hands keys and values through for layers declaring no key
row, and only layers caching exactly a key and a value of the same size use the block table.

The sparse attention indexers (DeepSeek V3.2, GLM-5, Mistral Large 4 and the models built on them) cache their
keys as one more row and select on the packed batch under the engine's mask.
One 576-wide row per token on DeepSeek V3 instead of two: the layout paged MLA kernels read, and one write and one
read per layer.
@qgallouedec
qgallouedec requested a review from remi-or November 12, 2026 13:54
# Conflicts:
#	src/transformers/models/axk1/modeling_axk1.py
#	src/transformers/models/axk1/modular_axk1.py
#	src/transformers/models/axk2/modeling_axk2.py
#	src/transformers/models/axk2/modular_axk2.py
#	src/transformers/models/bailing_hybrid/modeling_bailing_hybrid.py
#	src/transformers/models/bailing_hybrid/modular_bailing_hybrid.py
#	src/transformers/models/deepseek_v2/modeling_deepseek_v2.py
#	src/transformers/models/deepseek_v2/modular_deepseek_v2.py
#	src/transformers/models/deepseek_v3/modeling_deepseek_v3.py
#	src/transformers/models/deepseek_v3/modular_deepseek_v3.py
#	src/transformers/models/deepseek_v32/modeling_deepseek_v32.py
#	src/transformers/models/deepseek_v32/modular_deepseek_v32.py
#	src/transformers/models/glm4_moe_lite/modeling_glm4_moe_lite.py
#	src/transformers/models/glm5_next/modeling_glm5_next.py
#	src/transformers/models/glm5_next/modular_glm5_next.py
#	src/transformers/models/glm_moe_dsa/modeling_glm_moe_dsa.py
#	src/transformers/models/glm_moe_dsa/modular_glm_moe_dsa.py
#	src/transformers/models/hy_v4/modeling_hy_v4.py
#	src/transformers/models/hy_v4/modular_hy_v4.py
#	src/transformers/models/kimi_linear/modeling_kimi_linear.py
#	src/transformers/models/kimi_linear/modular_kimi_linear.py
#	src/transformers/models/longcat_flash/modeling_longcat_flash.py
#	src/transformers/models/longcat_flash/modular_longcat_flash.py
#	src/transformers/models/minicpm3/modeling_minicpm3.py
#	src/transformers/models/minicpm3/modular_minicpm3.py
#	src/transformers/models/mistral4/modeling_mistral4.py
#	src/transformers/models/mistral4/modular_mistral4.py
#	src/transformers/models/mistral_large4/modeling_mistral_large4.py
#	src/transformers/models/mistral_large4/modular_mistral_large4.py
#	src/transformers/models/youtu/modeling_youtu.py
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: axk1, axk2, bailing_hybrid, deepseek_v2, deepseek_v3, deepseek_v32, dots3_note, glm4_moe_lite, glm5_next, glm_moe_dsa, hy_v4, kimi_linear, longcat_flash, minicpm3, mistral4, mistral_large4

This branch has not been deployed

No deployments
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.

2 participants