Repository navigation
Conversation
HF's sdpa path calls the attention module with attention_mask=None (no padding) or a boolean 4D mask (padding). MLAAttentionBridge only added a float mask when one was given, so a DeepSeek-V2/V3 or GLM-4.7-Flash model running sdpa attended bidirectionally, and a boolean mask was added as 0/1. Reuse AttentionBridge._apply_reconstruct_attention_mask like the other reconstructing bridges.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
MLAAttentionBridge.forward(DeepSeek-V2/V3, GLM-4.7-Flash) recomputes attention by hand, but it only applies a mask when one is passed, and only as an additive float mask:When the wrapped HF model runs with
attn_implementation="sdpa"(the transformers default), HF does not build a float 4D mask for the attention modules:attention_mask=Noneand relies on SDPA'sis_causal, so the bridge attends bidirectionally;True= attend), which the bridge adds to the scores as 0/1, so neither the causal mask nor the padding is applied.boot_transformersforces eager attention, so it is not affected.build_bridge_from_moduleleaves the supplied model alone (as documented), so a DeepSeek model loaded with default settings and wrapped this way gets wrong logits, and earlier positions depend on later tokens. With a tiny randomDeepseekV3ForCausalLMon sdpa (dev2ad49297, transformers 5.20.1):Eager models were already correct and are unchanged.
The fix replaces those three lines with
AttentionBridge._apply_reconstruct_attention_mask, which the other reconstructing bridges (PositionEmbeddingsAttentionBridge,JointQKVAttentionBridge, ...) already use: a 4D mask is used as given (bool or float), otherwise the causal mask is built, with the KV-cache offset handled. The compatibility-mode-infsentinel handling is the same as before.GlmMoeDsaAttentionBridgehas its own forward and forces eager, so it is not touched.Tests (
test_mla_attention_bridge.py):test_output_matches_hfalready compared the bridge against the sdpa HF attention module withattention_mask=None, but withmax_diff < 0.15, which was loose enough to hide the missing causal mask. It is now1e-5(the actual difference after the fix is around 1e-7).test_boolean_padding_mask_matches_hfpasses a left-padded boolean 4D mask to both and compares the non-padded positions.Both fail on dev and pass with the fix.
Type of change
Checklist:
Local runs:
tests/uniton the locked environment (transformers 5.13.0, torch 2.11.0): 7676 passed, 0 failed; the MLA/DeepSeek/GLM-4.7-Flash/GLM-MoE-DSA unit and integration tests on both 5.13.0 and 5.20.1 (126 passed each), plus black, isort, pycln andmypy ..