Skip to content

[Qwen3_5]Remove unnecessary masked_fill_ in torch_chunk_gated_delta_rule attention computation: "attn = (q_i @ k_i.transpose(-1, -2) * decay_mask[:, :, i]).masked_fill_(mask, 0)"#45215

Merged
Rocketknight1 merged 4 commits intohuggingface:mainfrom
ENg-122:test_main
Apr 9, 2026

Conversation

@ENg-122
Copy link
Copy Markdown

@ENg-122 ENg-122 commented Apr 3, 2026

What does this PR do?

Remove unnecessary masked_fill_(mask, 0) call in torch_chunk_gated_delta_rule.

The decay_mask computed earlier already encodes the causal/lower-triangular structure (upper-triangle values are zero), so masking the attention scores again with masked_fill_(mask, 0) is redundant and adds unnecessary overhead.

Before:
attn = (q_i @ k_i.transpose(-1, -2) * decay_mask[:, :, i]).masked_fill_(mask, 0)

After:
attn = (q_i @ k_i.transpose(-1, -2) * decay_mask[:, :, i])

  • [√] I confirm that this is not a pure code agent PR.

Before submitting

  • [×] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • [√] Did you read the contributor guideline,
    Pull Request section?
  • [×] Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • [√] Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • [×] Did you write any new necessary tests?

Who can review?

@ArthurZucker @Cyrilvallez

ENg-122 and others added 3 commits April 3, 2026 16:48
Signed-off-by: zj <2716634506@qq.com>
…ule attention computation

Signed-off-by: zj <2716634506@qq.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 3, 2026

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

run-slow: olmo_hybrid, qwen3_5, qwen3_5_moe, qwen3_next

Copy link
Copy Markdown
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

This looks right to me! There is a minor danger of NaNs, I guess, but I think it's fine - multiplication of float32 Q/K values in any remotely reasonable range should not yield NaN

@Rocketknight1 Rocketknight1 enabled auto-merge April 8, 2026 13:04
@Rocketknight1 Rocketknight1 added this pull request to the merge queue Apr 8, 2026
@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.

@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 8, 2026
@Rocketknight1 Rocketknight1 added this pull request to the merge queue Apr 9, 2026
Merged via the queue into huggingface:main with commit 0aff0db Apr 9, 2026
22 checks passed
sirzechs66 pushed a commit to sirzechs66/transformers that referenced this pull request Apr 18, 2026
…ule attention computation: "attn = (q_i @ k_i.transpose(-1, -2) * decay_mask[:, :, i]).masked_fill_(mask, 0)" (huggingface#45215)

* [Qwen3_5]Remove excess mask

Signed-off-by: zj <2716634506@qq.com>

* [Qwen3_5]Remove unnecessary masked_fill_ in torch_chunk_gated_delta_rule attention computation

Signed-off-by: zj <2716634506@qq.com>

* Fix: remove brackets to match generated code format

---------

Signed-off-by: zj <2716634506@qq.com>
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.

3 participants