Skip to content

Fix residual gradient in non-learnable LayerNorm backward - #447

Closed
rocketbastard wants to merge 4 commits into
NVIDIA:mainfrom
rocketbastard:bug-fix-nonlearnable-layernorm-residual-grad
Closed

rocketbastard wants to merge 4 commits into
NVIDIA:mainfrom
rocketbastard:bug-fix-nonlearnable-layernorm-residual-grad

Conversation

@rocketbastard

Copy link
Copy Markdown
Contributor

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 the
non-learnable path calls _layer_norm_bwd_dx() and immediately returns.
This drops the residual-path gradient.

Changes

  • Accumulate dx_accumulate before returning from the non-learnable path.

Testing

examples/hstu/compare_hstu_static_embedding_nv_cpu.py

Accumulate the residual branch gradient for fused HSTU backward if dx_accumulate is provided.
This script compares the NV zero-static-embedding ranking loss with a CPU FP32 reference, implementing both NV and CPU models for performance evaluation.
@greptile-apps

greptile-apps Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The 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.

  • Adds dx_accumulate into the computed input gradient before returning from non-learnable LayerNorm backward.
  • Adds model-state, loss, update, and gradient comparison utilities for the NV and CPU paths.

Confidence Score: 4/5

The 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 torch.load with weights_only=False, and both comparison paths still derive labels from fixed four-element tensors even though the configured batch and target count can produce more than four logits.

Files Needing Attention: examples/hstu/compare_hstu_static_embedding_nv_cpu.py

Important Files Changed

Filename Overview
examples/hstu/ops/triton_ops/triton_layer_norm.py The non-learnable backward branch now incorporates the residual gradient before returning its input gradient.
examples/hstu/compare_hstu_static_embedding_nv_cpu.py Adds a standalone BF16-versus-FP32 HSTU comparison and gradient-debugging script.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security 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.

Suggested change
weights = torch.load(args.weights, map_location="cpu", weights_only=False)
weights = torch.load(args.weights, map_location="cpu", weights_only=True)

Comment on lines +252 to +253
labels = torch.tensor([1, 4, 7, 0], device=device)[:nv_logits.shape[0]]
nv_loss = F.cross_entropy(nv_logits.float(), labels)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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

@JacoCheung

Copy link
Copy Markdown
Collaborator

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!

@JacoCheung
JacoCheung self-requested a review August 5, 2026 08:52
@rocketbastard rocketbastard closed this by deleting the head repository Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]Non-learnable LayerNorm backward drops residual gradient

2 participants