How refit moves new weights to the engines, and which design to keep

PR #4801 ยท main 5e271174 ยท #4801 1a2eca34 ยท #5025 62868966 ยท Megatron-LM e0a4bfed

Three pages on this PR, one question each. This one has everything about refit.

Before
Three ways to send weights exist on main. Each backend supports a different subset and keeps the engine layout in a different place.
This PR
Lets refit shards differ in width (one width per engine), so Megatron + Dynamo can number prefill and decode ranks. Nothing changes for today's equal-width fleets.
Status
Works, but it is a third copy of the same running total. The last section proposes one layout interface; nothing there is implemented.
Background. After each training step, the trainer's GPUs send new weights to every inference GPU over NCCL groups. Trainer ranks come first, then each engine's ranks. The example everywhere: 8 trainer GPUs (TP 2, PP 2, DP 2) and one prefill engine at TP 4 plus two decode engines at TP 2. One weight to follow: W [8192, 4096] = [output features, input features], split by rows under TP, so a TP 2 trainer rank holds [4096, 4096], a TP 4 prefill rank [2048, 4096] and a TP 2 decode rank [4096, 4096]. Dotted words have a short explanation: hover or tap them.

Three ways to send the weights

1 ยท Packed broadcast: WHOLE weight PP 0 ยท copy A (DP 0) copy B (DP 1) PP 1 ยท later layers, not W t0 full W t1 t2 t3 t4 t5 t6 t7 r8 0โ€“2047 r9 2048โ€“4095 r10 4096โ€“6143 r11 6144โ€“8191 r12 0โ€“4095 r13 4096โ€“8191 r14 0โ€“4095 r15 4096โ€“8191 prefill P0 (TP 4) decode D0 decode D1 1 group (orange box): all 16 ranks t0 holds the full W [8192, 4096] and broadcasts it; NCCL moves it along a ring or tree, so every rank in the group gets all 8192 rows, t1โ€“t7 included. Each engine rank keeps only its own rows (below its box) and drops the rest. Its TP does not matter. Needs: where each engine starts. 2 ยท nccl_reshard: SHARD to shard, NeMo RL's plan PP 0 ยท copy A (DP 0) copy B (DP 1) PP 1 ยท later layers, not W t0 rows 0โ€“4095 t1 4096โ€“8191 t2 rows 0โ€“4095 t3 4096โ€“8191 t4 t5 t6 t7 r8 0โ€“2047 r9 2048โ€“4095 r10 4096โ€“6143 r11 6144โ€“8191 r12 0โ€“4095 r13 4096โ€“8191 r14 0โ€“4095 r15 4096โ€“8191 prefill P0 (TP 4) decode D0 decode D1 5 groups, one per job: ยท misc (16): unsharded params, broadcast as in 1 ยท prefill ร— PP 0 (8): t0โ€“t3 + r8โ€“r11 (blue) ยท decode ร— PP 0 (8): t0โ€“t3 + r12โ€“r15 (green) ยท prefill ร— PP 1, decode ร— PP 1: t4โ€“t7 (idle for W) Each piece is sent once, by the FIRST trainer rank that holds it (t0 or t1); copy B stays idle. D0 and D1 each get half, then swap halves (dashed: all-gather). Needs: both sides' TP/PP, per role. 3 ยท mcore: SHARD to shard, Megatron-Core's plan PP 0 ยท copy A (DP 0) copy B (DP 1) PP 1 ยท later layers, not W t0 rows 0โ€“4095 t1 4096โ€“8191 t2 rows 0โ€“4095 t3 4096โ€“8191 t4 t5 t6 t7 r8 0โ€“2047 r9 2048โ€“4095 r10 4096โ€“6143 r11 6144โ€“8191 r12 0โ€“4095 r13 4096โ€“8191 r14 0โ€“4095 r15 4096โ€“8191 prefill P0 (TP 4) decode D0 decode D1 1 group of 16 (orange box), point to point Every rank shares its own parallel groups in one all-gather; Megatron-Core plans every send. Each engine rank receives exactly its rows, no all-gather. Sender copy: dst % 2, then (dst // 2) % 2. Here t0 and t3 send 8192 rows each, t1 and t2 only 4096: uneven (hand trace, not run). Needs from NeMo RL: each engine's offset. trainer rank (TP 2, PP 2, DP 2) prefill rank decode rank one NCCL group W [8192, 4096] = [output features, input features], split by rows under TP. Labels under boxes: the rows of W that rank holds or needs.
Every NCCL group, one row each (number in a cell = that rank's number inside the group) t0 t1 t2 t3 t4 t5 t6 t7 r8 r9 r10 r11 r12 r13 r14 r15 trainer: PP 0 = t0โ€“t3, PP 1 = t4โ€“t7 engines: P0 = r8โ€“r11, D0 = r12โ€“r13, D1 = r14โ€“r15 1 ยท packed: model_update_group 16 W 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2 ยท nccl_reshard: misc 16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2 ยท prefill ร— PP 0 4 + 4 = 8 W 0 1 2 3 4 5 6 7 2 ยท prefill ร— PP 1 4 + 4 = 8 0 1 2 3 4 5 6 7 2 ยท decode ร— PP 0 4 + 4 = 8 W 0 1 2 3 4 5 6 7 2 ยท decode ร— PP 1 4 + 4 = 8 0 1 2 3 4 5 6 7 2 ยท decode copies, split off 2 + 2 W 0 0 1 1 3 ยท mcore: refit group 16 W 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 nccl_reshard group count = 1 misc + (roles ร— trainer PP stages) = 1 + 2 ร— 2 = 5. Each bulk group = the trainer ranks of one PP stage (8 / 2 = 4) + one role's engine ranks (4): 8. A layer's shards travel only in its stage's groups. With trainer PP 4 instead: 1 + 2 ร— 4 = 9 groups, each of 2 + 4 = 6. Each trainer rank joins misc + one group per role (3 here); each engine rank joins misc + its role's group for every stage (3 here). W marks the groups that carry W, a PP 0 layer. D0 and D1 need the same rows: each gets half from the trainer, then they swap halves inside a pair split off the decode group.
backend1 ยท packed broadcast2 ยท nccl_reshard3 ยท mcorewho knows the layout
TRT-LLM P/D (main)โœ“โœ“ MoE experts onlynot offeredthe generation object, per role
Megatron, no Dynamo (main)โœ“โœ“โœ“the synchronizer reads the config (_gen_layouts); MCore for 3
Megatron + Dynamo P/D (this stack)โœ“โœ— refusedโœ“ nccl or glooRefitMembership widths (offsets only); MCore for 3
vLLM P/D (#4920)โœ“โœ— refusedn/aits own init_collective

Read each panel top to bottom: the trainer's rows of W at the top, what each engine rank must end up with at the bottom, and every transfer of W in between. Two families. Whole weight (1): every engine rank gets every tensor and drops the rows it does not hold, so the engine's TP does not matter to the sender. Only rank numbers are needed. Shard to shard (2 and 3): an M-to-N transfer, like one DTensor to another, where each piece goes only to the ranks that hold it. Building those groups and the send plan needs the parallel layout of both the trainer and the engines. The two shard-to-shard ways differ only in who knows that layout. nccl_reshard gets it from NeMo RL: TRT-LLM's get_nccl_reshard_layouts() returns one layout per role, and the synchronizer builds one group per role and trainer PP stage. mcore gets it from Megatron-Core: every rank shares its own parallel groups in one all-gather, so NeMo RL passes only rank offsets. Behind Dynamo, nccl_reshard is refused by a config check, and would also fail: the synchronizer reads generation.worker_group, which MegatronDynamoGeneration raises on, and it reads one shared layout from config, not one per role.

Details that change the picture. nccl_reshard sends only part of the model shard to shard: MoE experts on TRT-LLM, plus dense layers on Megatron. The rest still goes whole over the misc group. mcore sends every parameter shard to shard, as Megatron's own shards rather than HF tensors. Its mcore_generation_config.refit_backend (nccl, gloo, nccl_m2n, nvshmem) only picks how the bytes move; the plan is the same for all four (copy service). Defaults: refit_transport: null everywhere, which means the packed broadcast when not colocated and CUDA IPC when colocated. Colocated Megatron must set mcore (check). Behind Dynamo, the stack allows mcore because "each engine joins the refit group at its own offset" (commit 3843ef69). It gives no reason for refusing nccl_reshard.

Engines of different widths: what #4801 changes

With the packed broadcast, each engine only needs to know where its ranks start: the sum of the widths before it. In the figure, prefill P0 starts at r8, D0 at r12 = 8 + 4, D1 at r14 = 8 + 4 + 2.

# the one key that makes engine widths differ (Megatron + Dynamo)
policy.generation:
  disaggregation: {enabled: true, num_prefill_engines: 1, num_decode_engines: 2}
  mcore_generation_config: {tensor_model_parallel_size: 2}
  dynamo_cfg:
    prefill_engine: {tensor_model_parallel_size: 4}   # widths (4, 2, 2); TRT-LLM: trtllm_cfg.prefill_engine

Refit plan, before (main)

# driver, weight synchronizer: plan the refit group (also after a shard dies)
def plan_refit_membership(surviving, dp_size, total_workers, trainers):
    width = total_workers // dp_size                          # one width for every shard
    prefix = {s: i * width for i, s in enumerate(surviving)}  # ranks after the trainers
    return Membership(world=trainers + len(surviving) * width, prefix, width)
# driver, fan out the new group
vLLM:     leader = workers[shard * width]                     # first worker of the shard
Megatron: ranks for workers[shard * width : + width]

Refit plan, after (this PR)

# driver, same place; example: 8 trainer ranks, shards (4, 2, 2), shard 0 dead
def plan_refit_membership(surviving, dp_size, total_workers, trainers,
                          shard_widths=None):                        # no caller passes it in this PR
    widths = shard_widths or (total_workers // dp_size,) * dp_size   # (4, 2, 2)
    prefix = {s: i * width for i, s in enumerate(surviving)}
    for s in sorted(surviving): prefix[s] = next; next += widths[s]  # {1: 0, 2: 2}
    return Membership(world=trainers + next, prefix, widths)         # world = 8 + 4 = 12
def worker_offset(shard): return sum(widths[:shard])                 # 0, 4, 6: dead shards count
vLLM:     leader = workers[membership.worker_offset(shard)]          # equal widths on every vLLM fleet
Megatron: ranks for workers[worker_offset(shard) : + widths[shard]]
# the same running total, computed in three places today
TRT-LLM (main):   _engine_owner_indices = prefix sums of engine TP  # [0, 4, 6]
vLLM P/D (#4920): rank_prefix = prefix sums in init_collective      # [0, 4, 6]
Megatron (#4801): RefitMembership.worker_offset(engine)             # [0, 4, 6], engine = refit shard

If an engine dies, survivors are packed together again (prefixes {1: 0, 2: 2}), but for Megatron + Dynamo #5025 refuses anything but the full fleet, so only vLLM uses that case, with equal widths.

๐Ÿ’ฌ C11 โ€” dynamo_config.py:179

Which design to keep โ€” proposed, not in any PR

Nothing below is implemented. Keep TRT-LLM's shape: each generation object describes its own layout, one entry per role, and shared code builds every group and plan from that one description. It is plain data, so it can be tested without GPUs. It works the same for vLLM, TRT-LLM and Megatron, with or without P/D or Dynamo. Membership is left to track only which replicas are alive. Megatron-Core's own plan is neat, since each rank describes itself and nothing can drift from the real model, but it only works Megatron to Megatron, and it has no refit deadline and no rebuild path. Keep it as an optional fast path, fed by the same layouts.

# proposed: one method per backend, everything else written once
class GenerationInterface:
    def refit_layouts(self) -> list[RefitLayout]                   # one per role
RefitLayout(role, tp, ep, etp, pp, engines, rank_prefixes, world)  # TRT-LLM's layout dict, typed
# shared code, backend-neutral
offsets = prefix sums over refit_layouts()                         # packed broadcast
groups  = one per (layout, trainer PP stage)                       # nccl_reshard, as today
mcore   = prepare_swap_model_weights(dst_rank_offset=offset, ...)  # Megatron only; offsets from the same layouts
membership = which replicas are alive                              # liveness only, as TRT-LLM keeps it

The packed broadcast is the one transport every backend shares, so it is the fair comparison. Same example, three designs, top to bottom: what each step knows and passes on.

TRT-LLM P/D (main) Megatron + Dynamo (#4801 + #5025) Proposed: one layout method generation object (driver) _plan_engines: roles [P, D, D] TP per engine [4, 2, 2] _engine_owner_indices = [0, 4, 6] engine records: widths (4, 2, 2) full_fleet_membership() โ†’ shared RefitMembership(widths) refit_layouts() โ†’ plain data: [(P0, 4), (D0, 2), (D1, 2)] every backend, same shape โ†’ weight synchronizer (driver) receives: no layout at all world = 8 train GPUs + 8 inference GPUs = 16 receives: inference world size 8 world = 8 + 8 = 16 its own membership: None receives: the layouts prefix = running total [0, 4, 6] world = 8 + 4 + 2 + 2 = 16 โ†’ Policy.init_collective 8 trainer workers init_collective(world 16, offset 0) t0โ€“t7 โ†’ ranks 0โ€“7 init_collective(world 16, offset 0) t0โ€“t7 โ†’ ranks 0โ€“7 init_collective(world 16, offset 0) t0โ€“t7 โ†’ ranks 0โ€“7 โ†’ generation.init_collective engine workers one call per engine: init_collective_async( rank_prefix = 0 | 4 | 6) engine_rank_offsets = [8, 12, 14] one call per engine record: init_collective(rank_offset) same call for every backend: init_collective( rank_prefix = [0, 4, 6]) worker joins as rank = 8 + prefix + local rank P0 8โ€“11, D0 12โ€“13, D1 14โ€“15 rank = offset + local rank P0 8โ€“11, D0 12โ€“13, D1 14โ€“15 rank = 8 + prefix + local rank P0 8โ€“11, D0 12โ€“13, D1 14โ€“15 where the running total of engine widths is computed Same ranks in every column. What differs is where the running total lives: inside one backend (TRT-LLM), in the shared RefitMembership but used only by Dynamo (#4801), or once in shared code from plain data (proposed).

What #4801's change touches. The shared refit plan (RefitMembership) feeds vLLM and plain Megatron, for the packed broadcast and for nccl_reshard. Their engines all have equal widths, so the result is unchanged. TRT-LLM never reads it, and mcore on plain Megatron does not use it either. The only caller that passes uneven widths is Megatron + Dynamo, and all it reads back is each engine's starting rank (engine_rank_offsets), for both the packed broadcast and mcore.

Where RefitMembership fits. It is an existing fleet-health class on main (membership.py): which inference shards are alive, and where each starts, so the synchronizer can rebuild the refit group after a shard dies. #4801 adds one width per shard to it, but the only user of uneven widths, Megatron + Dynamo, reads back just the starting ranks (engine_rank_offsets). In the proposal, membership goes back to meaning only "which engines are alive", the widths come from the layout, and the synchronizer adds up the widths of the alive engines.

Migration, without touching TRT-LLM in #4801:

  1. #4801 now (review comment C15; worked version in draft terrykong/RL#57): Megatron + Dynamo keeps its starting ranks private, a running total over its own shard_widths, as TRT-LLM keeps _engine_owner_indices. The shared refit code stays as on main.
  2. Follow-up PR: add an optional refit_layouts(). The synchronizer uses it when a backend has it and keeps today's behavior otherwise. TRT-LLM and Dynamo implement it from data they already hold.
  3. Then: vLLM and plain Megatron implement it, with equal widths.
  4. Finally: remove the fallback and the per-backend running totals.

One list, two kinds of starting rank. The packed broadcast and mcore number engines across all of them (P0 0, D0 4, D1 6). nccl_reshard's per-role groups number them within one role (P0 0; D0 0, D1 2). TRT-LLM computes both today, in two places. One layout list gives both.

casepacked broadcastnccl_reshardmcore
Megatron + Dynamo, P/Dstarts [0, 4, 6]per-role layouts give per-role groups; this unlocks itdst_rank_offset = 8 + start; maybe num_dst_pools per role (to check)
no P/D (vLLM, Megatron, TRT-LLM)one role, equal widths: 0, w, 2wone layoutone engine set
TRT-LLM nccl_reshardโ€”get_nccl_reshard_layouts() already is this layout; it becomes the shared methodnot offered
rebuild after an engine diesalive set, running total over survivorssameno rebuild path today
So what. Three refit designs keep layout knowledge in three places: the generation object, the synchronizer and Megatron-Core. #4801 adds a fourth spot, RefitMembership. One refit_layouts() method per backend would serve all three transports, let Megatron + Dynamo use nccl_reshard, and make #4801's width change unneeded. The review links work for others only after the review is published.