Skip to content

Commit 6a45755

Browse files
fix(offload): make the load-wait hold DP-group aware; install it only for early-load tiers
Review round 6 (#3772), items 1 and 2. The hold stays on by default. Item 1 (option a): the hold's bound now sees the DP group. - DPEngineCoreProc._execute_dummy_batch calls the step hooks' on_forward_done with the dummy forward's wall time, so an idle rank's tick is the group's step, not its own last (often long prefill) forward. - _sync_dp_state sends a fifth flag on its existing MAX all_reduce, this rank's Scheduler.has_running_decode(), and keeps the reduced value as dp_group_decoding (no new collective; kept between syncs). - LoadWaitState.budget_s(..., group_decoding=True) returns 0: while any rank decodes, a hold would add to that decode's ITL at the next collective. With no decode anywhere (a P/D prefill instance, the measured case) the bound is unchanged. Item 2: load_wait_hook returns None unless the engine's KV connector has an offload tier that loads early (supports_early_load: Mooncake Store), so a dense or DSV4 offload tier, whose LoadExecutor also rings the doorbell, gets no hook. Env and docs rows say so instead of "inert". Tests: dummy forward updates the tick; a peer decode makes the budget 0 and keeps a step from holding; a prefill-only group keeps the hold and its bound; _sync_dp_state carries any rank's decode; no hook for a dense / missing connector. The two _sync_dp_state tests follow the five-flag payload.
1 parent 8196edf commit 6a45755

9 files changed

Lines changed: 230 additions & 42 deletions

File tree

‎atom/kv_transfer/offload/load_wait.py‎

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
(`LoadWaitState.budget_s` over `load_wait_tradeoff`), woken by the
2525
doorbell, ending as soon as every started load landed. The hold delays
2626
every DP peer at the forward collective; the bound charges that through
27-
`cost * dp_size`, never exceeds one tick and is ~0 while decodes run.
27+
`cost * dp_size`, never exceeds one tick, and is 0 while any rank of the
28+
DP group has a running decode (`DPEngineCoreProc.dp_group_decoding`,
29+
carried on the existing per-step `_sync_dp_state` reduction).
2830
29-
Rank-local throughout: no collective runs here and the step count is
30-
unchanged, so DP lockstep holds.
31+
Installed only for a KV connector whose offload tier loads early
32+
(`supports_early_load`: Mooncake Store); other connectors get no hook. No
33+
collective runs here and the step count is unchanged, so DP lockstep holds.
3134
"""
3235

3336
from __future__ import annotations
@@ -46,9 +49,11 @@
4649
class LoadWaitState:
4750
"""The load wait's inputs and counters.
4851
49-
`tick_s` is the wall time of the last forward this engine ran
50-
(`OffloadLoadWait.on_forward_done`): the step cadence the waits are
51-
measured against. 0 until a forward ran, so a cold engine never waits.
52+
`tick_s` is the wall time of the last forward this engine ran, a DP
53+
dummy forward included (`OffloadLoadWait.on_forward_done`): the group's
54+
step cadence the waits are measured against, not this rank's last real
55+
(often long prefill) forward. 0 until a forward ran, so a cold engine
56+
never waits.
5257
"""
5358

5459
LOG_INTERVAL_S = 60.0
@@ -59,7 +64,7 @@ def __init__(self, dp_size: int) -> None:
5964
self._last_log = time.monotonic()
6065
self._reset()
6166

62-
def budget_s(self, gain: int, cost: int) -> float:
67+
def budget_s(self, gain: int, cost: int, *, group_decoding: bool = False) -> float:
6368
"""How long holding this step for `gain` loading requests is worth.
6469
6570
Each request that lands in time prefills one forward (`tick_s`) earlier;
@@ -68,10 +73,16 @@ def budget_s(self, gain: int, cost: int) -> float:
6873
the router keeps the ranks alike, so the group carries about `dp_size`
6974
times this rank's count). The hold pays while it stays under
7075
`tick_s * gain / (cost * dp_size)`, and it is never longer than one
71-
tick: past that the request is no later than without the hold. With a
72-
running decode, `cost` is the whole decode batch and the bound is ~0.
76+
tick: past that the request is no later than without the hold.
77+
78+
`group_decoding`: some rank of the DP group has a running decode
79+
(`DPEngineCoreProc.dp_group_decoding`, reduced on the per-step state
80+
sync). The hold would add to that decode's ITL at the next forward
81+
collective, and this rank cannot count it, so nothing is worth
82+
holding: 0. Without DP the engine's own decode stops the pre-phase
83+
(`prestart_tier_loads`) before any hold.
7384
"""
74-
if gain <= 0 or self.tick_s <= 0.0:
85+
if gain <= 0 or self.tick_s <= 0.0 or group_decoding:
7586
return 0.0
7687
return min(self.tick_s, self.tick_s * gain / (max(1, cost) * self.dp_size))
7788

@@ -129,11 +140,6 @@ def maybe_log(self, label: str) -> None:
129140
# -- scheduler-side passes (read the scheduler; no state of their own on it) ----
130141

131142

132-
def has_running_decode(scheduler) -> bool:
133-
"""Whether `running` holds anything besides partial prefills."""
134-
return len(scheduler.running) > scheduler._partial_prefill_count
135-
136-
137143
def prestart_tier_loads(scheduler) -> list:
138144
"""Run this tick's early tier load ahead of `schedule()` and return the
139145
requests it parked.
@@ -152,7 +158,7 @@ def prestart_tier_loads(scheduler) -> list:
152158
`_schedule()`. That is an ordering requirement, not a tuning choice; the
153159
pass inside `_schedule()`, behind the reservation, runs instead.
154160
"""
155-
if not scheduler.waiting or has_running_decode(scheduler):
161+
if not scheduler.waiting or scheduler.has_running_decode():
156162
return []
157163
scheduler._prefetch_waiting_lookups()
158164
early_load = scheduler._early_load
@@ -225,11 +231,12 @@ def load_wait_tradeoff(scheduler, loading: list) -> tuple[int, int]:
225231

226232
class OffloadLoadWait:
227233
"""Engine step hook (`EngineCore.step_hooks`): `before_schedule` ahead of
228-
every `schedule()`, `on_forward_done` after every real forward.
234+
every `schedule()`, `on_forward_done` after every forward (real or DP
235+
dummy).
229236
230237
Reads the engine it serves (`scheduler`, `runner_mgr`, `label`, the KV
231-
progress event and `_collect_kv_transfer_progress`) and keeps everything
232-
else here.
238+
progress event, `_collect_kv_transfer_progress` and, under DP,
239+
`dp_group_decoding`) and keeps everything else here.
233240
"""
234241

235242
def __init__(self, engine, dp_size: int, *, hold: bool) -> None:
@@ -264,7 +271,10 @@ def before_schedule(self) -> None:
264271
state.steps += 1
265272
state.loads += len(started)
266273
if self.hold:
267-
budget = state.budget_s(*load_wait_tradeoff(scheduler, started))
274+
budget = state.budget_s(
275+
*load_wait_tradeoff(scheduler, started),
276+
group_decoding=getattr(engine, "dp_group_decoding", False),
277+
)
268278
if budget > 0.0:
269279
t0 = time.monotonic()
270280
pending = self._wait_for_loads(started, t0 + budget)
@@ -369,9 +379,16 @@ def _wait_for_loads(self, loading, deadline: float) -> int:
369379

370380
def load_wait_hook(engine, config) -> OffloadLoadWait | None:
371381
"""The offload load wait for `engine`, or None when it is off
372-
(`ATOM_OFFLOAD_LOAD_EVENT_WAIT=0`)."""
382+
(`ATOM_OFFLOAD_LOAD_EVENT_WAIT=0`) or the engine's KV connector has no
383+
offload tier that loads early (`supports_early_load` False: no connector,
384+
a P/D-only connector, dense or DSV4 offload). `LoadExecutor` rings the
385+
doorbell on every offload tier, so without this gate a non-early tier
386+
would wait for a fresh answer after each of its loads for nothing."""
373387
if not envs.ATOM_OFFLOAD_LOAD_EVENT_WAIT:
374388
return None
389+
connector = getattr(engine.scheduler, "kv_connector", None)
390+
if connector is None or not connector.supports_early_load:
391+
return None
375392
return OffloadLoadWait(
376393
engine,
377394
config.parallel_config.data_parallel_size,

‎atom/model_engine/engine_core.py‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ class EngineCore:
135135
# (`share_cache` routing); the data-parallel engine turns it on.
136136
_report_backlog = False
137137
# Objects called around every step: `before_schedule()` ahead of
138-
# `schedule()`, `on_forward_done(seconds)` after a real forward
139-
# (`atom.kv_transfer.engine_step_hooks`). None by default.
138+
# `schedule()`, `on_forward_done(seconds)` after a real forward or a DP
139+
# dummy forward (`atom.kv_transfer.engine_step_hooks`). None by default.
140140
step_hooks: tuple = ()
141141

142142
def __init__(self, config: Config, input_address: str, output_address: str):
@@ -1164,6 +1164,8 @@ def __init__(self, config: Config, input_address: str, output_address: str):
11641164
self._idle_when_blocked = self._resolve_idle_when_blocked(config)
11651165
# Reduced by `_sync_dp_state`: some rank's vote hid unfinished work.
11661166
self._dp_group_hidden_work = False
1167+
# Reduced by `_sync_dp_state`: some rank of the group has a running decode.
1168+
self.dp_group_decoding = False
11671169

11681170
def _resolve_idle_when_blocked(self, config: Config) -> bool:
11691171
"""`ATOM_DP_IDLE_WHEN_BLOCKED`: whether this rank reports work to its DP
@@ -1390,7 +1392,12 @@ def _validate_dp_sync_interval(self, interval: int | None) -> int:
13901392
return value
13911393

13921394
def _execute_dummy_batch(self):
1395+
fwd_start = time.monotonic()
13931396
fwd_out = self.runner_mgr.call_func("dummy_execution", wait_out=True)
1397+
# The dummy is paired with the peers' forwards: its wall time is the
1398+
# group's step, which is what a step hook measures against.
1399+
for hook in self.step_hooks:
1400+
hook.on_forward_done(time.monotonic() - fwd_start)
13941401
if self.scheduler.running:
13951402
# The tokens this rank's last real step sampled come back with this
13961403
# forward (deferred output), and land like any step's.
@@ -1406,8 +1413,14 @@ def _sync_dp_state(
14061413
"""MAX-reduce this rank's flags over the DP group. `local_hidden`
14071414
(ATOM_DP_IDLE_WHEN_BLOCKED: unfinished work the vote left out) is
14081415
reduced too, into `_dp_group_hidden_work`; every rank sends it, so the
1409-
collective is the same whatever each rank's setting."""
1416+
collective is the same whatever each rank's setting. So is whether
1417+
this rank has a running decode (`Scheduler.has_running_decode`), into
1418+
`dp_group_decoding`: some rank of the group decodes, so anything that
1419+
delays this rank's next forward delays those decodes at the collective
1420+
too (a step hook reads it). Kept until the next sync."""
1421+
local_decoding = self.scheduler.has_running_decode()
14101422
self._dp_group_hidden_work = local_hidden
1423+
self.dp_group_decoding = local_decoding
14111424
if self._shutting_down:
14121425
return local_has_unfinished, True, local_held
14131426

@@ -1418,16 +1431,18 @@ def _sync_dp_state(
14181431
int(local_shutdown),
14191432
int(local_held),
14201433
int(local_hidden),
1434+
int(local_decoding),
14211435
],
14221436
dtype=torch.int64,
14231437
device="cpu",
14241438
)
14251439
torch.distributed.all_reduce(
14261440
state_tensor, op=torch.distributed.ReduceOp.MAX, group=self.dp_group
14271441
)
1428-
# One decode for the four flags.
1429-
unfinished, shutdown, held, hidden = state_tensor.tolist()
1442+
# One decode for the five flags.
1443+
unfinished, shutdown, held, hidden, decoding = state_tensor.tolist()
14301444
self._dp_group_hidden_work = hidden == 1
1445+
self.dp_group_decoding = decoding == 1
14311446
return unfinished == 1, shutdown == 1, held == 1
14321447
except RuntimeError as e:
14331448
logger.warning(f"{self.label}: _sync_dp_state failed: {e}")

‎atom/model_engine/scheduler.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,10 @@ def is_finished(self):
20832083
and not self.deferred_free_blocks
20842084
)
20852085

2086+
def has_running_decode(self) -> bool:
2087+
"""Whether `running` holds anything besides partial prefills."""
2088+
return len(self.running) > self._partial_prefill_count
2089+
20862090
def has_forward_work(self) -> bool:
20872091
"""Whether a scheduling pass on this rank may produce a batch now.
20882092

‎atom/utils/envs.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,16 +1549,19 @@ def _dp_sync_interval_env() -> int | None:
15491549
# single-engine and DP loops runs the early tier load ahead of schedule()
15501550
# and ships its loads, workers ring a doorbell when a load job ends, and
15511551
# after a ring the engine fetches one completion answer before schedule()
1552-
# so a landed load is admitted one forward earlier. Inert without an
1553-
# offload tier that loads early (Mooncake Store).
1552+
# so a landed load is admitted one forward earlier. Installed only when
1553+
# the KV connector's offload tier loads early (`supports_early_load`:
1554+
# Mooncake Store); any other connector (dense or DSV4 offload, P/D only)
1555+
# gets no hook, so the flag changes nothing there.
15541556
"ATOM_OFFLOAD_LOAD_EVENT_WAIT": lambda: _flag_env(
15551557
"ATOM_OFFLOAD_LOAD_EVENT_WAIT", "1"
15561558
),
15571559
# Default on (0 turns it off). With ATOM_OFFLOAD_LOAD_EVENT_WAIT, also
15581560
# hold the step for the loads it started while the trade pays
15591561
# (`LoadWaitState.budget_s`: tick * gain / (cost * dp_size), at most one
1560-
# forward, ~0 while decodes run). The DP peers the hold delays are charged
1561-
# through cost * dp_size.
1562+
# forward, 0 while any rank of the DP group has a running decode). The DP
1563+
# peers the hold delays are charged through cost * dp_size, and their
1564+
# decodes through the group flag carried on the per-step DP state sync.
15621565
"ATOM_OFFLOAD_LOAD_WAIT_HOLD": lambda: _flag_env(
15631566
"ATOM_OFFLOAD_LOAD_WAIT_HOLD", "1"
15641567
),

‎docs/distributed_guide.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,17 @@ The `stateless_init_dp_group()` method (in `ParallelConfig`) calls `stateless_in
109109

110110
### Synchronized busy loop
111111

112-
The DP busy loop overrides the base `EngineCore.busy_loop()` to synchronize state across DP ranks: every round by default, every N paired rounds after a busy sync with `ATOM_DP_SYNC_INTERVAL` (see [DP state sync interval](#dp-state-sync-interval)). The `_sync_dp_state()` method packs three signals into an int64 tensor and performs a single `AllReduce(MAX)`:
112+
The DP busy loop overrides the base `EngineCore.busy_loop()` to synchronize state across DP ranks: every round by default, every N paired rounds after a busy sync with `ATOM_DP_SYNC_INTERVAL` (see [DP state sync interval](#dp-state-sync-interval)). The `_sync_dp_state()` method packs five signals into an int64 tensor and performs a single `AllReduce(MAX)`:
113113

114114
```python
115-
# State synced: [has_unfinished, shutdown, held]
115+
# State synced: [has_unfinished, shutdown, held, hidden, decoding]
116116
state_tensor = torch.tensor(
117117
[
118-
1 if local_has_unfinished else 0,
119-
1 if local_shutdown else 0,
120-
1 if local_held else 0,
118+
int(local_has_unfinished),
119+
int(local_shutdown),
120+
int(local_held),
121+
int(local_hidden), # ATOM_DP_IDLE_WHEN_BLOCKED: work the vote left out
122+
int(local_decoding), # Scheduler.has_running_decode()
121123
],
122124
dtype=torch.int64, device="cpu",
123125
)
@@ -130,6 +132,7 @@ This ensures:
130132
- **All ranks run a step together**: when any rank has unfinished work, every rank steps, an idle one with a dummy batch. The step's phase and padded shape are agreed per step by `ForwardMode.decide`.
131133
- **Graceful shutdown**: all ranks must agree before exiting.
132134
- **One hold holds the group**: a rank paused, offloaded or fenced for a weight sync holds every rank, so their forwards stay paired.
135+
- **The group's decodes are visible to every rank**: `dp_group_decoding` is set when any rank has a running decode, so a rank that would delay its next forward (the offload load-wait hold, `ATOM_OFFLOAD_LOAD_WAIT_HOLD`) does not add to a peer's ITL.
133136

134137
### Dummy batch execution
135138

0 commit comments

Comments
 (0)