Repository navigation
perf(trainer): keep router-replay routes compact until the GPU, shard them for CP on the host - #4215
Merged
Merged
Conversation
… them for CP on the host, free the batch after the step
faresobeid
marked this pull request as ready for review
October 22, 2026 14:50
mikasenghaas
approved these changes
Oct 22, 2026
faresobeid
added a commit
that referenced
this pull request
Oct 24, 2026
Re-pin deps/verifiers to verifiers#2765 (6e235e7) and deps/renderers to renderers#170 (424ba10), both merged with main. Read by-handle routing rows in their wire dtype and expand routed layers on GPU after the host CP shard (#4215); read masks over the full sequence when score centering is on (#4176).
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.
Router-replay routes arrive from the orchestrator in vLLM's wire dtype (uint8 for ≤256 experts, uint16 above). On main the trainer:
DataLoader._micro_batch_to_tensor(4x the uint8 bytes, 2x the uint16 bytes);[1, seq, layers, top_k]tensor to the GPU and only then takes the CP chunk. The chunk is a view, so the full tensor stays allocated on the GPU;dataloader.get_batch()returns, so two converted batches are alive while the next one is received.This PR keeps routes in the wire dtype on the host, takes the CP chunk on the host, copies only that chunk to the GPU and widens it to int32 there. It also drops the step's micro batches after the micro-step loop. The model receives the same int32 tensor as before.
Memory
One trainer rank, 16 micro batches × 65,536 tokens (1M tokens), CP=4, two steps. Routes are the only per-token array that changes. Host peak is RSS above the received frame, sampled after each micro batch is converted. GPU is the peak allocation for the routes in one micro step (H200).
Host memory is per rank. Every CP rank of a data rank still receives the full payload, so a node holds 8x these numbers.
Overlap
data.py/train.py._micro_batch_to_tensor.Note
Low Risk
Behavior-preserving memory optimization for MoE router replay; model still receives int32 routed experts after CP shard. Minor risk only if CP sharding or dtype handling on CPU diverged from the old GPU path.
Overview
Router-replay
routed_expertsstay in vLLM wire dtype (uint8/uint16) on the host longer, instead of widening to int32 inDataLoader._micro_batch_to_tensor.In the training loop, routes are no longer copied to GPU before context-parallel (CP) sharding. CP slicing runs on the host; only the rank’s chunk is moved to CUDA and cast to int32 right before forward—same int32 input to the model, but less host and GPU memory (especially when CP>1, since the full sequence no longer has to live on GPU as a view).
After the micro-batch forward/backward loop, the step
del micro_batches, micro_batchdrops the converted batch so the next receive/convert does not keep two batches alive at once.Reviewed by Cursor Bugbot for commit c5204b9. Bugbot is set up for automated code reviews on this repo. Configure here.