Instructions to use inclusionAI/Ling-3.0-flash with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
Fix non-causal MLA attention under the default sdpa (and flash_attention_2) mode
This is the issue reported for Ling-3.0-tiny in
Ling-3.0-tiny#15 and
inclusionAI/Ling#27. It applies here too:
Ling-3.0-flash's eager_attention_forward and its MLA layers are the same code. This PR is the same fix as
https://huggingface.co/inclusionAI/Ling-3.0-tiny/discussions/20.
What's wrong
The MLA attention layers (BailingMoeV3MultiLatentAttention, which the MTP layer uses too) always calleager_attention_forward, whatever attn_implementation is. That function only adds the mask it is
handed:
- Under
sdpa, the default, the model's mask helper returnsNonefor unpadded input without a cache, and
leaves causality to the kernel'sis_causalflag. So these layers attend to future tokens in any unpadded
forward over a full sequence: prefill, teacher-forced training, scoring. (With padding, or after a cache, the
helper builds a 4D mask and the layers are masked correctly.) - Under
flash_attention_2, the model passesNonewhen nothing is padded, so every multi-token forward
attends to future tokens; with padding it passes a 2D mask, which this function slices as if it were 4D,
raising anIndexError.
Decode steps themselves (one query) are unaffected, but the prompt's prefill is not, so generations differ
from eager's too.
The fix
eager_attention_forward applies a 4D mask exactly as before. When it gets no 4D mask, it builds the causal
mask itself for a causal module with more than one query (offset by the cached keys, so a chunked prefill
after a cache is masked correctly too under flash_attention_2) and applies a 2D padding mask if one is
given, so padded flash_attention_2 inputs work instead of failing. One-token decode without a mask is
unchanged. Every attention mode then computes exactly what eager computes. One function, +12 / −1, identical
to the Ling-3.0-tiny change.
How it was checked
- On Ling-3.0-tiny's real weights, with this same function (Ling-3.0-flash's weights were not run):
Ling-3.0-tiny in BF16 with transformers 4.57.6, four prompts, every position's next-token logits compared
withattn_implementation="eager". Before, undersdpa: top-1 agreement with eager 0.61–0.80, max
|logit diff| 4.1–16.7. With the fix: logits equal to eager, and the 32-token greedy text equal on 4 of 4
prompts. - This file's function on synthetic tensors (CPU), against PyTorch's
scaled_dot_product_attention:- prefill without a mask equals causal attention (max |diff| 3e-7; before, 2.5);
- one-token decode is bit-identical to before;
- a chunked prefill after cached keys matches the offset causal mask;
- the 4D-mask path is bit-identical to before;
- a 2D left-padding mask gives causal plus padding masking;
- a non-causal module stays unmasked.
- The real-weight comparison covers
sdpa, the default.flash_attention_2was checked only on the function
with synthetic tensors (the no-mask and 2D-padding cases above), not on a model.
Check it yourself
No weights needed. This runs the function from the file against PyTorch's causal attention:
import ast, torch
src = open("modeling_bailing_moe_v3.py").read() # main, or this PR
fn = next(n for n in ast.parse(src).body if getattr(n, "name", "") == "eager_attention_forward")
ns = {"torch": torch, "nn": torch.nn, "repeat_kv2": lambda x, n: x}
exec("from __future__ import annotations\n" + ast.get_source_segment(src, fn), ns)
attn = torch.nn.Module(); attn.is_causal, attn.num_key_value_groups = True, 1 # as in the MLA layers
q, k, v = (torch.randn(1, 2, 6, 8) for _ in range(3))
out, _ = ns["eager_attention_forward"](attn, q, k, v, None, scaling=8**-0.5) # sdpa mode, no padding: no mask
ref = torch.nn.functional.scaled_dot_product_attention(q, k, v, is_causal=True).transpose(1, 2)
print((out - ref).abs().max().item()) # main: ~1 to 3 (attends to future tokens); this PR: ~1e-7
Several Ling-3.0-flash variants (FP8, INT4, FP4, base, Fin, Sante) ship Ling-3.0-tiny's version of this file
rather than this one, so they get the fix from the Ling-3.0-tiny change once they carry the updated copy.