Skip to content

Commit b44cc5b

Browse files
committed
fix: defer transfer array materialization off the scheduler thread
from_tracker eagerly converted every group's block ids and gvas to numpy arrays on the scheduler critical path; main only materializes arrays on the worker thread. At 256 concurrent requests with 256 blocks x 5 groups this added ~13.5 ms to every scheduler step (and ~192 ms at 4096 blocks), directly inflating TTFT on prefill-heavy workloads; an E2E benchmark on DeepSeek-V4-Flash measured ~1% throughput/TTFT regression. Store the tracker's id lists by reference instead and materialize them lazily on first array access, which happens on the worker thread (planes/gva read paths now go through get_block_ids_array). The lazy source shares identity with ReqMeta.block_ids_by_group, so pickling the metadata payload costs no extra bytes. Empty gvas conversions reuse one shared empty array. Measured: scheduler-side from_tracker drops 13.51 ms -> 0.22 ms per step (256 req x 256 blocks x 5 groups); the one-time materialization runs on the worker thread where main performs the same conversion. Full ascend_store + kv_offload suite: zero per-test changes. Signed-off-by: zhaosusu <zhaosusu1@h-partners.com>
1 parent 8e1def2 commit b44cc5b

2 files changed

Lines changed: 27 additions & 20 deletions

File tree

‎vllm_ascend/distributed/kv_transfer/kv_pool/ascend_store/layerwise/planes/gva.py‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,7 @@ def _alloc_gvas_for_save_for_request(self, requests: list[ReqMeta]) -> bool | No
128128
all_group_save_keys: list[str] = []
129129
request.transfer.partial_save_gva_per_group = [0] * worker.num_kv_cache_groups
130130
for group_id in range(worker.num_kv_cache_groups):
131-
block_ids_by_group = (
132-
request.transfer.block_ids_by_group_np[group_id]
133-
if (
134-
request.transfer.block_ids_by_group_np is not None
135-
and group_id < len(request.transfer.block_ids_by_group_np)
136-
)
137-
else request.transfer.block_ids_np
138-
)
131+
block_ids_by_group = request.get_block_ids_array(group_id)
139132
if block_ids_by_group is None:
140133
raise RuntimeError(f"Block IDs are not initialized for request {request.req_id}")
141134
save_owner = worker._is_layerwise_save_owner(group_id)
@@ -376,14 +369,7 @@ def _prepare_load_gvas_for_request(self, requests: list[ReqMeta]) -> bool | None
376369
full_blocks, get_saved_key = snapshot
377370
partial_block_index = worker._resolve_layerwise_load_partial(cached_tokens, effective_block_size)
378371

379-
block_ids_by_group = (
380-
request.transfer.block_ids_by_group_np[group_id]
381-
if (
382-
request.transfer.block_ids_by_group_np is not None
383-
and group_id < len(request.transfer.block_ids_by_group_np)
384-
)
385-
else request.transfer.block_ids_np
386-
)
372+
block_ids_by_group = request.get_block_ids_array(group_id)
387373
if (
388374
worker.layerwise_offload
389375
and cached_tokens > 0

‎vllm_ascend/distributed/kv_transfer/kv_pool/ascend_store/transfer_metadata.py‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
if TYPE_CHECKING:
1313
from vllm_ascend.distributed.kv_transfer.kv_pool.ascend_store.request_state import RequestTracker
1414

15+
# Shared read-only empty array; the save path rebinds rather than mutates.
16+
_EMPTY_IDS: np.ndarray = np.empty(0, dtype=np.int64)
17+
1518

1619
@dataclass
1720
class RequestTransferState:
@@ -35,6 +38,11 @@ class RequestTransferState:
3538
load_last_block_key: str | None = None
3639
store_masks: tuple[Sequence[bool] | None, ...] | None = None
3740
load_masks: tuple[Sequence[bool] | None, ...] | None = None
41+
# Lazy list source for scheduler-built state. The np conversion is
42+
# deferred to the worker thread (first array access) so the scheduler
43+
# critical path never pays it; pickled, it shares identity with
44+
# ReqMeta.block_ids_by_group and costs no extra payload bytes.
45+
ids_source: list[list[int]] | None = None
3846

3947
@classmethod
4048
def from_legacy_fields(cls, **fields: Any) -> RequestTransferState:
@@ -50,23 +58,36 @@ def from_legacy_fields(cls, **fields: Any) -> RequestTransferState:
5058

5159
@classmethod
5260
def from_tracker(cls, tracker: RequestTracker) -> RequestTransferState:
53-
grouped_ids = [np.asarray(ids, dtype=np.int64) for ids in tracker.allocated_block_ids_by_group]
61+
# Keep the scheduler side allocation-free for ids: the tracker lists
62+
# are shared by reference and converted once on the worker thread.
5463
return cls(
55-
block_ids_np=grouped_ids[0] if grouped_ids else np.empty(0, dtype=np.int64),
56-
block_ids_by_group_np=grouped_ids or None,
57-
block_gvas_np=np.asarray(tracker.block_gvas, dtype=np.int64),
64+
block_gvas_np=np.asarray(tracker.block_gvas, dtype=np.int64) if tracker.block_gvas else _EMPTY_IDS,
5865
block_gvas_by_group_np=[np.asarray(gvas, dtype=np.int64) for gvas in tracker.block_gvas_by_group]
5966
if tracker.block_gvas_by_group
6067
else None,
6168
gva_block_offset=tracker.gva_block_offset,
69+
ids_source=tracker.allocated_block_ids_by_group,
6270
)
6371

72+
def _materialize_ids(self) -> None:
73+
source = self.ids_source
74+
if source is None:
75+
return
76+
grouped_ids: list[np.ndarray] = [np.asarray(ids, dtype=np.int64) for ids in source]
77+
self.block_ids_by_group_np = grouped_ids or None
78+
self.block_ids_np = grouped_ids[0] if grouped_ids else _EMPTY_IDS
79+
self.ids_source = None
80+
6481
def get_block_ids_array(self, group_id: int) -> np.ndarray | None:
82+
if self.ids_source is not None:
83+
self._materialize_ids()
6584
if self.block_ids_by_group_np is not None and group_id < len(self.block_ids_by_group_np):
6685
return self.block_ids_by_group_np[group_id]
6786
return self.block_ids_np
6887

6988
def get_transfer_arrays(self, group_id: int, is_save: bool = True) -> tuple[np.ndarray | None, np.ndarray | None]:
89+
if self.ids_source is not None:
90+
self._materialize_ids()
7091
grouped_ids = self.block_ids_by_group_np
7192
grouped_gvas = self.block_gvas_by_group_np if is_save else self.load_block_gvas_by_group_np
7293
if (

0 commit comments

Comments
 (0)