Skip to content
Open
20 changes: 20 additions & 0 deletions atom/kv_transfer/engine_step_hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-License-Identifier: MIT
# Copyright (C) 2026, Advanced Micro Devices, Inc. All rights reserved.
# Copyright (c) 2026 ATOM Project Contributors
"""The engine step hooks KV transfer installs (`EngineCore.step_hooks`).

A step hook has two methods: `before_schedule()`, called ahead of every
`schedule()` of the engine loop, and `on_forward_done(seconds)`, called after
every real or DP dummy forward with its wall time. The engine knows nothing
else of them.
"""

from __future__ import annotations


def kv_transfer_step_hooks(engine, config) -> tuple:
"""The step hooks for an engine with KV transfer configured."""
from atom.kv_transfer.offload.load_wait import load_wait_hook

hooks = (load_wait_hook(engine, config),)
return tuple(hook for hook in hooks if hook is not None)
29 changes: 28 additions & 1 deletion atom/kv_transfer/offload/_offload_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import numpy as np
import torch

from atom.kv_transfer import progress_doorbell
from atom.kv_transfer.disaggregation.types import (
ConnectorCompletion,
KVConnectorOutput,
Expand Down Expand Up @@ -238,6 +239,26 @@ def build_offload_engine(
return engine, cfg, meta


class LoadExecutor(ThreadPoolExecutor):
"""The load pool: every job rings the KV progress doorbell when it ends
(`ATOM_OFFLOAD_LOAD_EVENT_WAIT`; a plain pool otherwise).

A load job records its terminal result (`_done_load` / `_failed_load`)
before it returns, so the ring follows the record; `get_finished` rearms
the doorbell before it drains them. With no doorbell installed
(`progress_doorbell.install`) the ring is a no-op.
"""

def submit(self, fn, /, *args, **kwargs):
future = super().submit(fn, *args, **kwargs)
future.add_done_callback(_ring_kv_progress)
return future


def _ring_kv_progress(_future) -> None:
progress_doorbell.ring()


class OffloadWorkerMixin:
"""Executor plumbing + completion reporting shared by offload workers.

Expand Down Expand Up @@ -290,7 +311,10 @@ def _init_worker_common(
self._save_executor = ThreadPoolExecutor(
max_workers=n_save, thread_name_prefix=f"{thread_name_prefix}-save"
)
self._load_executor = ThreadPoolExecutor(
load_pool = (
LoadExecutor if envs.ATOM_OFFLOAD_LOAD_EVENT_WAIT else ThreadPoolExecutor
)
self._load_executor = load_pool(
max_workers=n_load, thread_name_prefix=f"{thread_name_prefix}-load"
)
self._lock = threading.Lock()
Expand Down Expand Up @@ -491,6 +515,9 @@ def _record_save_failure(self, req) -> None:
self._done_save.add(self._save_completion_id(req))

def get_finished(self) -> KVConnectorOutput:
# Before the drain: a load recorded after this either is in this answer
# or rings the doorbell again (`progress_doorbell`).
progress_doorbell.rearm()
with self._lock:
dl, fl, ds = self._drain_common_completions_locked()
completions = set(self._connector_completions)
Expand Down
4 changes: 4 additions & 0 deletions atom/kv_transfer/offload/hybrid/dsv4/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import torch

from atom.kv_transfer import progress_doorbell
from atom.kv_transfer.disaggregation.base import (
KVConnectorBase,
KVConnectorSchedulerBase,
Expand Down Expand Up @@ -1773,6 +1774,9 @@ def get_finished(self) -> KVConnectorOutput:
# - finished_loading wakes successfully loaded requests.
# - failed_loading wakes them for recompute using already allocated blocks.
# - finished_saving releases blocks whose free was deferred during save.
# Rearm before the drain: a load recorded after this either is in this
# answer or rings the doorbell again (`progress_doorbell`).
progress_doorbell.rearm()
with self._lock:
dl, fl, ds = self._drain_common_completions_locked()
dss = set(self._done_sidecar_save)
Expand Down
Loading
Loading