Skip to content

perf(training): accelerate grad clipping with fused Triton kernel - #3815

Open
piyushumate wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
piyushumate:piyushumate/perf/fused-grad-clipping-kernel
Open

perf(training): accelerate grad clipping with fused Triton kernel#3815
piyushumate wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
piyushumate:piyushumate/perf/fused-grad-clipping-kernel

Conversation

@piyushumate

@piyushumate piyushumate commented Sep 5, 2026

Copy link
Copy Markdown

What does this PR do ?

Accelerates gradient norm calculation in _clip_grad_norm_impl by up to 6.49x on NVIDIA H100 using a fused, zero-allocation 2D multi-tensor Triton reduction kernel, resolving CPU driver bottlenecks in distributed and MoE LLM training without memory overhead (#3719).

Changelog

  • Implemented nemo_automodel/components/training/_fused_grad_clipping.py with 2D chunk-parallel Triton kernels (_multi_tensor_max_2d_kernel, _multi_tensor_scaled_l2_2d_kernel) supporting bfloat16, float16, float32, and float64.
  • Updated _clip_grad_norm_impl in nemo_automodel/components/training/utils.py to dispatch to the fused Triton kernel when running on CUDA, with clean eager fallback for CPU/non-CUDA and preservation of all DTensor sharding placements and device mesh communications.
  • Added comprehensive unit and functional tests in tests/functional_tests/llm_pretrain_and_kd/test_fused_grad_clipping.py.
  • Verified strict numerical parity against PyTorch eager baseline on NVIDIA H100 (relative difference: 0.0002%, well within BF16 1.6% precision).

Benchmark Results (NVIDIA H100 SXM 80GB)

Workload: 500 gradient matrices totaling 10.26 Billion elements (21.00 GB) across realistic 70B/MoE model layers:

Implementation Latency (ms) Speedup vs Baseline Speedup vs Foreach Memory Overhead
Sequential Baseline (main) 176.211 ms 1.00x 0 MB (Sequential)
PyTorch Foreach 33.833 ms 5.21x 1.00x ~21.00 GB (Temporary Buffer)
Fused Triton 2D Kernel 27.150 ms 6.49x 1.25x 0 MB (Zero-Alloc)
  • Latency reduction: From 176.2 ms down to 27.1 ms (saving ~149 ms per step, or ~4.14 hours of cluster GPU time per 100k steps).
  • Zero extra memory: In-place fused accumulation avoids the ~21 GB intermediate buffer required by torch._foreach_div.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you add or update any necessary documentation?

Additional Information

…n multi-tensor kernel (NVIDIA-NeMo#3719)

In distributed and MoE LLM training (e.g. 70B, Nemotron, DeepSeek), models have 500+ gradient matrices. The existing _clip_grad_norm_impl loops through each parameter's local gradient tensor twice sequentially via Python eager ops:
1. Pass 1: g.detach().abs().max() (500 kernel launches)
2. Pass 2: g.detach().abs().div(scale).square().sum() (1,500+ kernel launches)

This creates severe CPU driver overhead (~176 ms on H100 SXM 80GB) and starves GPU execution. Furthermore, using PyTorch _foreach_div allocates massive intermediate buffers (~21 GB for a 70B model).

This commit implements a fused, zero-allocation 2D multi-tensor Triton kernel (_fused_grad_clipping.py) that parallelizes chunk reductions across all SMs in double-precision accumulation:
- 6.49x speedup over sequential baseline (176.2 ms -> 27.1 ms on NVIDIA H100 SXM 80GB)
- 1.25x speedup over PyTorch Foreach without allocating 21 GB of intermediate memory
- 100% numerical parity confirmed on H100 (relative diff 0.0002%, well within BF16 1.6% precision)
- Clean, automatic fallback to sequential execution on CPU/non-CUDA or when Triton is unavailable
- Unit and functional tests added in tests/functional_tests/llm_pretrain_and_kd/test_fused_grad_clipping.py

Resolves NVIDIA-NeMo#3719.

Signed-off-by: piyushumate <piyushumate@users.noreply.github.com>
@copy-pr-bot

copy-pr-bot Bot commented Sep 5, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@piyushumate
piyushumate marked this pull request as ready for review September 5, 2026 07:19
@piyushumate
piyushumate requested a review from a team as a code owner September 5, 2026 07:19
@piyushumate

Copy link
Copy Markdown
Author

cc - @akoumpa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gradient clipping kernel

1 participant