Skip to content

Commit eb6ad8f

Browse files
Denny991liutong
andauthored
[Fix] Pass natural-log LSE to the cuDNN sparse MLA backward (#2147)
b03bf01/fe0037f0 switched _cudnn_dsa_sparse_mla_backward_op from the log2-space LSE contract to natural-log LSE, but this backend kept forwarding the log2 value, so every GLM-5.3 training silently fed the cuDNN backward a 1.4427x-inflated LSE. Forward is unaffected; backward gradients come out systematically shrunk (grad_norm ~20-30% low) and loss drifts (+0.043 over 300 steps vs the pre-change baseline). Save and pass the natural-log softmax_lse instead. Co-authored-by: liutong <liutong@pjlab.org.cn>
1 parent 9c1f93c commit eb6ad8f

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎xtuner/v1/ops/sparse_mla/flash_mla_cudnn.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ def _flash_mla_cudnn_forward(
6969
scaling,
7070
d_v=value_dim,
7171
)
72-
# FlashMLA returns natural-log LSE; cuDNN's backward (below) expects log2-space LSE,
73-
# matching the same contract flash_mla.py uses for its (different) TileLang backward.
72+
# FlashMLA returns natural-log LSE. cuDNN's backward (below) also expects natural-log
73+
# LSE since b03bf018 switched _cudnn_dsa_sparse_mla_backward_op off the log2 contract;
74+
# the third slot keeps the legacy log2 value only for custom-op schema stability.
7475
return raw_output, softmax_lse, softmax_lse * 1.4426950408889634
7576

7677

@@ -84,20 +85,20 @@ def _(q: Tensor, kv: Tensor, indices: Tensor, scaling: float, value_dim: int) ->
8485

8586
def _setup_flash_mla_cudnn_context(ctx, inputs, output) -> None:
8687
q, kv, indices, scaling, _ = inputs
87-
raw_output, _, lse_log2 = output
88+
raw_output, softmax_lse, _ = output
8889
ctx.scaling = scaling
89-
ctx.save_for_backward(q, kv, indices, raw_output, lse_log2)
90+
ctx.save_for_backward(q, kv, indices, raw_output, softmax_lse)
9091

9192

9293
def _flash_mla_cudnn_backward(ctx, grad_output: Tensor, grad_lse: Tensor, grad_lse_log2: Tensor):
93-
q, kv, indices, raw_output, lse_log2 = ctx.saved_tensors
94+
q, kv, indices, raw_output, softmax_lse = ctx.saved_tensors
9495
dq, dkv = _cudnn_dsa_sparse_mla_backward_op(
9596
q,
9697
kv,
9798
raw_output,
9899
grad_output.contiguous(),
99100
indices,
100-
lse_log2,
101+
softmax_lse,
101102
ctx.scaling,
102103
)
103104
return dq, dkv, None, None, None

0 commit comments

Comments
 (0)