Repository navigation
Fix residual gradient in non-learnable LayerNorm backward - #447
rocketbastard wants to merge 4 commits into
Conversation
Accumulate the residual branch gradient for fused HSTU backward if dx_accumulate is provided.
…bedding_nv_cpu.py
Greptile SummaryThe PR adds residual-gradient accumulation to the non-learnable Triton LayerNorm backward path and introduces a standalone script for comparing fused BF16 HSTU training against a CPU FP32 reference.
Confidence Score: 4/5The PR is not yet safe to merge because the newly added comparison script retains unrestricted checkpoint deserialization and fails for supported target-batch configurations larger than four. The script still passes the CLI-selected checkpoint to Files Needing Attention: examples/hstu/compare_hstu_static_embedding_nv_cpu.py Important Files Changed
Reviews (2): Last reviewed commit: "Merge branch 'NVIDIA:main' into bug-fix-..." | Re-trigger Greptile |
| p.add_argument("--debug-gradients", action="store_true") | ||
| p.add_argument("--debug-gradient-steps", type=int, default=3) | ||
| args = p.parse_args() | ||
| weights = torch.load(args.weights, map_location="cpu", weights_only=False) |
There was a problem hiding this comment.
Unsafe checkpoint deserialization
When --weights references an attacker-controlled checkpoint, weights_only=False reconstructs arbitrary pickle objects before validating the expected tensor dictionary, allowing code execution with the user's privileges. How this was verified: The caller-controlled path reaches unrestricted torch.load deserialization without a guard, while all subsequently accessed checkpoint values are tensors in standard containers.
| weights = torch.load(args.weights, map_location="cpu", weights_only=False) | |
| weights = torch.load(args.weights, map_location="cpu", weights_only=True) |
| labels = torch.tensor([1, 4, 7, 0], device=device)[:nv_logits.shape[0]] | ||
| nv_loss = F.cross_entropy(nv_logits.float(), labels) |
There was a problem hiding this comment.
Fixed labels break configured batches
When --batch-size * --num-target-tokens exceeds four, this path produces that many logit rows but at most four labels, causing F.cross_entropy to raise a batch-size mismatch before the script reports losses or gradients. The CPU path repeats the same fixed-label construction.
Knowledge Base Used: HSTU Example: Training Flow
|
Hi @rocketbastard , thanks for your work! I think an efficient way is to accumulate the gradient inside the _layer_norm_bwd_dx kernel. Could you do it this way? And could you please simplify your utest (Considering the changes cover only an triton op, using modules like embedding is not necessary imo). Also move utest to testing folder. The utest lacks of numeric assert. Thanks again! |
Summary
Fix the non-learnable LayerNorm backward path so that it accumulates
the residual gradient supplied through
dx_accumulate.Fixes #446
Problem
The learnable backward path consumes
dx_accumulate, but thenon-learnable path calls
_layer_norm_bwd_dx()and immediately returns.This drops the residual-path gradient.
Changes
dx_accumulatebefore returning from the non-learnable path.Testing
examples/hstu/compare_hstu_static_embedding_nv_cpu.py