[Common] Remove redundant grad_logits zero-initialization in fused router backward kernels#2745
Conversation
Greptile SummaryThis PR removes dead-store zero-initialization loops for
Confidence Score: 5/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Loop iteration begins\ntoken_offset_cur_warp] --> B[Load local_grad from grad_probs\nLoad local_act_from_fwd, routing_map\ninto shared memory]
B --> C{score_function == 2?}
C -- Yes --> D[Compute sqrtsoftplus output\ninto local_comp_buf]
C -- No --> E
D --> E{topk > 1 AND\nsigmoid or sqrtsoftplus?}
E -- Yes --> F[Post-processing bwd\nrouted → update grad\nnon-routed → local_grad = 0.0]
E -- No --> G
F --> G{!use_pre_softmax AND\nscore_function == 1?}
G -- Yes --> H[Softmax bwd on topk scores]
G -- No --> I
H --> I[Backward of topk:\nset non-routed positions to 0.0]
I --> J{score_function branches}
J -- Pre-softmax bwd --> K[apply_softmax_bwd_on_float]
J -- Sigmoid bwd --> L[apply_sigmoid_bwd_on_float]
J -- Sqrtsoftplus bwd --> M[apply_sqrtsoftplus_bwd_on_float]
J -- None --> N
K & L & M --> N[Final write: grad_logits for ALL num_experts positions\nREMOVED: prior zero-init of grad_logits was dead store here]
style N fill:#d4edda,stroke:#28a745
|
|
@roycho96 You need to sign-off all your commits, probably via a force push. |
…ward kernels Signed-off-by: Sung Hyun Cho <hope5487@gmail.com>
44073b7 to
63f692f
Compare
Squashed into a single commit with sign-off. Thanks for the review! |
|
/te-ci |
Cherry-pick upstream fixes: - NVIDIA#2720: Fix sigmoid/sqrtsoftplus normalization in fused_score_for_moe_aux_loss to always normalize (not only when topk > 1), matching forward and backward. - NVIDIA#2745: Remove redundant grad_logits zero-initialization in both backward kernels, since all positions are unconditionally overwritten. Bug fixes: - Fix online softmax (apply_softmax_on_float_v1) NaN when data_size < 32. When two empty lanes (max=-inf, sum=0) merge during warp reduction, expf(-inf - (-inf)) = expf(NaN) = NaN, and 0*NaN = NaN in IEEE 754, contaminating valid lanes. Guard with finite checks on max values. - Restore intermediate_output initialization for post-softmax path. Only K positions are written in this path; uninitialized memory at the remaining E-K positions can contain NaN that propagates through the backward. Other: - Restore original multi-pass softmax as apply_softmax_on_float (template), add online softmax as apply_softmax_on_float_v1 with NaN guard. - Switch kernel call sites to use apply_softmax_on_float_v1. - Remove dead includes (sstream, utils.cuh) and commented-out debug logging.
Description
Both fused_topk_with_score_function_backward_kernel and fused_score_for_moe_aux_loss_backward_kernel zero-initialize grad_logits at the beginning of each loop iteration. However, all positions are unconditionally overwritten by the final write at the end of the same iteration, making the zero-initialization a dead store.
Removing it eliminates num_tokens * num_experts * sizeof(dtype) bytes of unnecessary HBM writes per backward kernel call.
Type of change
Changes
Checklist: