Skip to content

perf(trainer): add model.gemm_sm_carveout to keep SMs free for NCCL kernels - #4897

Open
MarioSieg wants to merge 4 commits into
mainfrom
mario/gemm-sm-carveout
Open

MarioSieg wants to merge 4 commits into
mainfrom
mario/gemm-sm-carveout

Conversation

@MarioSieg

@MarioSieg MarioSieg commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add model.gemm_sm_carveout (default unset, so off). When it is set, the RL trainer (rl/train.py) and the SFT/pretrain trainer (train.py) call torch._C._set_sm_carveout_experimental(n) before model setup, so cuBLAS and CUTLASS GEMMs, torch._grouped_mm included, leave n SMs free.
  • Why: an NCCL kernel that waits for its peers keeps holding its SMs. A GEMM launched over every SM then needs a second wave on the SMs it has left.
  • Measured next to one waiting single-CTA NCCL kernel: torch._grouped_mm was 1.9x slower and dense GEMMs 1.11x slower. With a carve-out of 4 they ran at 1.00-1.12x their normal speed. When no kernel waits, the carve-out costs about 1%.
  • Set the value to at least the CTAs of the NCCL kernels that overlap the GEMMs (NCCL_MAX_CTAS). A carve-out may change cuBLAS's kernel choice, so check losses rather than expecting bit-equality.
  • Documented in the FSDP knob table in docs/scaling.md.

Test plan

  • torch._C._set_sm_carveout_experimental / _get_sm_carveout_experimental exist and round-trip in torch 2.14.0, the pinned version (CPU build).
  • ModelConfig(gemm_sm_carveout=4) parses, the default is None, and 0 is rejected.
  • uvx ruff check / uvx ruff format --check on the changed files.
  • Not run on GPU.

Note

Low Risk
Opt-in performance tuning with a safe default of None; only affects GEMM SM scheduling on GPU when explicitly set.

Overview
Adds optional trainer.model.gemm_sm_carveout so cuBLAS/CUTLASS GEMMs (including torch._grouped_mm) reserve SMs instead of occupying the full GPU when NCCL collectives overlap.

When set, both the RL trainer and the SFT/pretrain trainer call torch._C._set_sm_carveout_experimental during startup, immediately after matmul precision is configured and before EP/parallel mesh setup. Default is unset (None), so behavior is unchanged unless configured.

The FSDP scaling docs now document the knob, including guidance to size it at least to overlapping NCCL CTAs (NCCL_MAX_CTAS) and expected tradeoffs (~1% GEMM cost when nothing is waiting vs. large slowdowns when a waiting NCCL kernel shares the device).

Reviewed by Cursor Bugbot for commit bdb1d11. Bugbot is set up for automated code reviews on this repo. Configure here.

…ernels

An NCCL kernel that waits for its peers holds its SMs, so a GEMM launched
over every SM runs a second wave next to it. model.gemm_sm_carveout makes
cuBLAS and CUTLASS GEMMs (torch._grouped_mm included) leave that many SMs
free, via torch._C._set_sm_carveout_experimental, in the RL and SFT
trainers. Off by default.
@garrett361

Copy link
Copy Markdown
Collaborator

It looks like we only used this with PP in experiments? Don't think it had much other purpose for being. So let's table it until we have more advanced support for PP in prime-rl, wdyt?

@MarioSieg
MarioSieg marked this pull request as ready for review December 3, 2026 19:44

This branch has not been deployed

No deployments
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.

2 participants