feat: add Megatron fused linear cross entropy backends - #1371
Open
justinhh4 wants to merge 7 commits into
Open
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Recompute FP32 exponentials from saved low-precision logits instead of persisting quantized exponentials. Align backend dispatch with the standard Function-class export and harden benchmark correctness checks. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Reuse shared validation, require the native CuTe path to run only on exact SM100 hardware, communicate dX in the projection dtype, preserve the monkey-patch TP limitation in the docs, and make benchmark memory collection discard stale autograd graphs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
justinhh4
force-pushed
the
justinhh4/megatron-flce-pr
branch
from
August 11, 2026 18:57
0311445 to
4ec705f
Compare
justinhh4
marked this pull request as ready for review
August 11, 2026 19:19
Repair Megatron's loaded cross-entropy bindings and add an opt-in GPT output-processor hook for fused linear cross entropy. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove benchmark- and example-specific README additions and keep the high-level API documentation concise. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
Add a Megatron-compatible fused vocab-parallel linear cross-entropy API with three local-compute backends:
Each implementation materializes one local low-precision projection buffer and saves it for backward. Backward recomputes FP32 exponentials from those logits, converts the buffer to dlogits in place, and reuses it for the projection gradients. Predicted-logit and sum-exp statistics share one NCCL SUM reduction, while the low-precision dX reduction runs asynchronously and overlaps dW/dBias computation.
Triton kernels follow Liger's standard flat
src/liger_kernel/ops/layout. The optional backends use the existingLIGER_KERNEL_IMPL=cutile|cutedslreplacement mechanism; this PR adds no new selector or backend registry. CuTe DSL requires exact SM100 hardware and reuses the_sm100_gemm.pyinfrastructure already onmain.Megatron integration
apply_liger_kernel_to_megatron(fused_linear_cross_entropy=True)installs FLCE through Megatron-Core 0.18'sGPTModel._postprocessoutput-processor hook. Standard labeled GPT forwards with a native BF16/FP16ColumnParallelLinearoutput layer route directly from hidden states to per-token loss; inference calls and caller-supplied output processors remain unchanged.The automatic path fails explicitly for unsupported configurations, including gathered logits, sequence parallelism, gradient-accumulation fusion, deferred embedding wgrad, disabled output dgrad reduction, MTP, MuP output scaling, and separately returned output bias.
LigerMegatronFusedLinearCrossEntropyremains available for explicit/custom wiring.The existing
cross_entropy=Truepatch now also repairs Megatron's import-time consumer bindings inLanguageModuleand thetensor_parallelpackage export, so both fused and unfused CE routes reach Liger even when Megatron was imported before patching.Correctness
test/megatronLanguageModuleCE routingmake checkstyleliger_kernel/ops/triton/packageB200 benchmark
Full forward + backward, TP4, 16,384 tokens, hidden size 4,096, global vocab 128,256, BF16:
All three Liger paths reduce measured peak Torch memory by about 42%. CuTile is 2.2% behind the materialized baseline full step and has the fastest forward. CuTe DSL is effectively at baseline full-step speed (0.4% faster in this run). The portable all-Triton backend is 6.7% slower on B200.
Peak memory is measured through the PyTorch allocator. The benchmark discards stale autograd graphs before each memory sample.
Follow-up
The faster NVSHMEM prototype is intentionally excluded: CUDA RDC, NVSHMEM runtime initialization, symmetric-workspace ownership, and packaging need a separate integration surface.