Repository navigation
[PyTorch] Add SiLUGate fusible op - #3726
VikingDeng wants to merge 3 commits into
Conversation
Add a basic op that computes rmsnorm(x) * silu(gate) with the gate as an extra input, built on the existing rmsnorm_fwd/rmsnorm_bwd kernels. The normalized output and its gradient are rounded to the input dtype around the gate, matching Megatron-LM's unfused GDN path. Quantized output is not supported yet. First step of NVIDIA#3724. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: VikingDeng <emmmvkdeng@gmail.com>
There was a problem hiding this comment.
This seems like a rather large and bespoke operation to treat as a first-class citizen. How about we separate the RMSNorm and gate logic by just implementing a SiLUGate basic op? Then you can reproduce this function with Sequential(RMSNorm(...), SiLUGate()). You can easily change to a different norm or a different gating function (if we add more in the future).
Once you have a fused kernel, the right design is to expose it in a fused op. This has several advantages:
- Your fused kernel doesn't need to be fully general. Only enable the fusion for the cases that the kernel supports, and in other cases we fallback to the unfused impl.
- If you only fuse the forward pass or only fuse the backward pass, your implementation only needs to consider that case.
Per review, keep the norm and the gate separate: gated RMSNorm is now Sequential(RMSNorm(...), SiLUGate()). A fused op can be added once there is a fused kernel. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: viking <emmmvkdeng@gmail.com>
|
Thanks, that's cleaner. I replaced |
|
@timmoon10 when you have time, could you take another look? This is now just the |
[Medium impact] The PR appears safe to merge, with no outstanding findings. SummaryAdds
Reviews (2) · Last reviewed commit: "[PyTorch] Cover negative gates in SiLUGa..." · Reviewed by Greptile |
Draw gates from [-6, 4) so both tests exercise the negative side of SiLU, including the zero of its derivative near -1.28. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: viking <emmmvkdeng@gmail.com>
Description
First step of #3724, reworked after review: instead of a dedicated
RMSNormGatedop, this adds a smallSiLUGatebasic op, so gated RMSNorm isSiLUGatetakes the gate as an extra input and computesy = x * silu(gate). The SiLU part is computed in FP32;yanddxare returned in the input dtype anddgatein the gate's dtype. Output quantization isn't supported, so it returns an unquantized tensor and the next op quantizes. A fused norm+gate kernel can come later as a fused op that only covers the cases the kernel supports.Refs #3724, NVIDIA/Megatron-LM#6757
Type of change
Changes
transformer_engine/pytorch/ops/basic/silu_gate.py: newSiLUGatebasic op with one extra input (the gate).transformer_engine.pytorch.opsand add it to the API docs.tests/pytorch/test_fusible_ops.py:test_silu_gatechecks y, dx and dgate against a reference for each dtype and two shapes.test_rmsnorm_silu_gatechecksSequential(RMSNorm, SiLUGate)end to end (y, dx, dweight, dgate), with zero-centered gamma on and off. The reference rounds the normalized output and its gradient to the input dtype like the op chain does, instead of loosening tolerances.Tested on an RTX 3090 (SM86), torch 2.13.0+cu130:
I haven't run it on Hopper/Blackwell yet.
AI assistance: I used Claude Code to help write the code and tests. I reviewed the change, ran the validation above on my own GPU, and I'll handle the review.
Checklist: