[PyTorch] Enable LayerNormLinear UB AG overlap under no_grad - #3546
Open
ravimajeti wants to merge 1 commit into
Open
ravimajeti wants to merge 1 commit into
ravimajeti wants to merge 1 commit into
Conversation
Signed-off-by: Ravi M <ravitejamajeti@gmail.com>
ravimajeti
force-pushed
the
codex/issue-2902
branch
from
September 18, 2026 09:01
cb47786 to
8906815
Compare
Contributor
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
LayerNormLinear's forward UserBuffers All-Gather overlap (ub_overlap_ag_fprop) was additionally gated ontorch.is_grad_enabled(). A caller could explicitly request the overlap and still have it silently disabled whenever the forward ran undertorch.no_grad().This matters for activation checkpointing/recomputation: with reentrant checkpointing the initial forward runs under
no_gradwhile the backward-time recompute runs with gradients enabled, so exactly one of the two identical forwards lost the overlapped communication/GEMM path — for a reason unconnected to the forward computation itself. Gradient mode governs autograd bookkeeping (what to save for backward); it is not a reason to suppress a requested forward communication overlap.This PR removes
is_grad_enabledfrom the forward-path selection only:Every other gradient-mode check in
_layernorm_linear_forward_impl(backward_needs_input, backward quantizer setup, saved tensors) is untouched.Scope:
LayerNormLinearonly.LayerNormMLPcarries the same gate on itsub_overlap_agandub_overlap_rs; it is not changed here and is left for a follow-up so it can get its own test and validation. (Lineardoes not have the gate.)Fixes #2902
Type of change
Changes
transformer_engine/pytorch/module/layernorm_linear.py: decoupleub_overlap_ag_fpropfromis_grad_enabled.tests/pytorch/distributed/run_layer_with_overlap.py: add--no-grad— runs the forward insidetorch.no_grad(), skips backward, compares the output against the non-overlap reference, and assertsfill_userbuffers_buffer_for_all_gather()was actually invoked (so the test checks that the overlap path ran, not only that the numbers match).tests/pytorch/distributed/test_comm_gemm_overlap.py: addtest_layernorm_linear_all_gather_overlap_no_grad, parametrized over BF16 (none) and FP8 (fp8_delayed_scaling).Testing
All runs use the repository's own test files with the same invocations as
qa/L1_pytorch_distributed_unittest/test.shandqa/L0_pytorch_unittest/test.sh, on 4x H100 SXM (NVLink), TP=4, PyTorch 2.11.0+cu128, CUDA 12.8, built from this branch as rebased onmain(ae34b34f).test_layernorm_linear_all_gather_overlap_no_grad[none]/[fp8_delayed_scaling](added here)AssertionError: Userbuffers AllGather overlap was not used under no_gradtests/pytorch/distributed/test_comm_gemm_overlap.py(154 cases: grad-enabledLayerNormLinearviatest_layers_with_overlap_bf16/fp8,Linear, multi-layerTransformerLayer, split/bulk overlaps,torch.compile)tests/pytorch/distributed/test_fusible_ops_with_userbuffers.pytests/pytorch/test_numerics.py -k "recompute or checkpoint"(170 cases incl. all 96test_gpt_full_activation_recomputevariants: FP8 on/off,DelayedScaling/Float8CurrentScaling, reentrant/non-reentrant)* The 10 failures are all
test_linear_with_overlap_compile[...](te.Linearundertorch.compile(fullgraph=True)) and are unrelated to this change: they fail identically on unmodifiedmain(ae34b34f) in this environment. With PyTorch 2.11.0 the custom-op registration indynamo/custom_op.pyis disabled at import (cannot import name 'is_opaque_constant_type' from 'torch._library.opaque_object'), sote.Lineartakes itstorch._dynamo.disabled eager fallback andfullgraph=TrueraisesUnsupported. They need a newer PyTorch build. The 89 skips are the mxfp8 / cuBLASMp hardware-and-build gates.Lint:
pre-commit run --fileson the three changed files passes all hooks (black with the repo's settings, vermin, whitespace/EOF checks);pylintreports no messages for the changed source file.Performance note (ad-hoc microbenchmark, not part of the test suite)
The fix makes the existing overlap path reachable under
no_grad; it does not add a new one, so the speedup is whatever UB all-gather overlap already gives on the grad-enabled path. For reference, a forward-only single-LayerNormLinearmicrobenchmark (TP=4, hidden 4096, seq 8192, 200 timed iterations after warmup, overlap vs.ub_overlap_ag=False) measured +12–14 % in BF16 (A100-SXM4) and +21–22 % in FP8 (H100-SXM), each reproduced twice. As with the grad-enabled path, overlap is a net loss at small shapes (e.g. −12 to −17 % at hidden ≤ 2048, seq ≤ 2048), where the overlap setup cost exceeds the all-gather latency it hides.Checklist:
🤖 Generated with Claude Code