Skip to content

Move value padding into the attention interfaces that need it#47451

Merged
hmellor merged 4 commits into
mainfrom
fix-mla-padding-condition
Jul 22, 2026
Merged

Move value padding into the attention interfaces that need it#47451
hmellor merged 4 commits into
mainfrom
fix-mla-padding-condition

Conversation

@hmellor

@hmellor hmellor commented Jul 21, 2026

Copy link
Copy Markdown
Member

CI

MLA attention forwards currently use is_flash_attention_requested(self.config) as a stand-in for "the selected attention kernel requires equal query/value head dimensions". If true, it then pads value_states up to qk_head_dim before attention and slicing the output back afterwards:

if is_flash_attention_requested(self.config) and self.qk_head_dim != self.v_head_dim:
    value_states = F.pad(value_states, [0, self.qk_head_dim - self.v_head_dim])

This PR moves this padding into the attention interfaces that need it: flash_attention_forward and paged_attention_forward

@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.

Comment thread src/transformers/models/deepseek_v2/modeling_deepseek_v2.py Outdated
@hmellor hmellor changed the title Use metadata instead of string matching to pad value_states in MLA models Move value padding into the attention interfaces that need it Jul 21, 2026

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's wait on @remi-or for CB but imo this is exactly what we want!!

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: deepseek_v2, deepseek_v3, glm4_moe_lite, longcat_flash, minicpm3, mistral4, youtu

@hmellor
hmellor force-pushed the fix-mla-padding-condition branch from 33fe028 to e19ce0e Compare July 21, 2026 16:16
@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 29847672236:1
Result: success | Jobs: 2 | Tests: 5 | Failures: 1 | Duration: 42s

@remi-or remi-or left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@hmellor
hmellor added this pull request to the merge queue Jul 22, 2026
Merged via the queue into main with commit 65d834f Jul 22, 2026
208 checks passed
@hmellor
hmellor deleted the fix-mla-padding-condition branch July 22, 2026 06:18
SangbumChoi added a commit to SangbumChoi/transformers that referenced this pull request Jul 22, 2026
* upstream/main: (39 commits)
  Remove deprecated training args and `is_fast` property (huggingface#46917)
  Consistent output shape from `get_image_features` (huggingface#46405)
  Fix multi-device mxfp4 dequantization race in `_convert_moe_packed_tensors` (huggingface#47423)
  fix failed test cases for qwen3_omni_moe model (huggingface#47449)
  Fix Hunyuan-VL PIL image resize parity with reference preprocessing (huggingface#47233)
  Move `value` padding into the attention interfaces that need it (huggingface#47451)
  Simplify function dispatch for linear attention (huggingface#47450)
  [cache] Allow sliding window layers to be roll-backed for speculative decoding (huggingface#47447)
  Fix double-shifted training loss in GitForCausalLM (huggingface#47395)
  Fix CohereASR training-loss double-shift (same as Moonshine fix huggingface#46784) (huggingface#46895)
  Warn when `group_by_length` is silently ignored for iterable datasets (huggingface#47379)
  Update bug report list (huggingface#46607)
  Fix shape mismatch in KyutaiSpeechToText `generate()` last window (huggingface#46952)
  Optimize flash attention max seqlen computation in vision attention (huggingface#47170)
  fix: remove unreachable return in special token builder (huggingface#47420)
  Add Harry to slow CI (huggingface#47454)
  BLT: vectorize patch length processing (huggingface#47385)
  Fix `TrackioCallback` fails to log evaluation metrics after training ends (huggingface#46935)
  [Kimi] add integration tests (huggingface#47383)
  Fix typo in `MusicgenForCausalLM.generate()` (huggingface#46974)
  ...
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.

4 participants