Skip to content

Commit ec0be94

Browse files
committed
perf: restore per-request save allocation contract from main
The upstream save-path tests added in the latest main window lock the per-request implementation contract: hash conversion is counted per request until the first allocation failure stops the loop, per-request key lists keep their nesting, and isolated requests observe their own exists-check state. The cross-request batched collector broke those observable timings while chasing fewer store round trips. Restore main's per-request save allocation (with the step-scoped hash memo and public-key dedup that main already ships) as the contract of record; the scheduler-thread materialization deferral from the previous commit stays, with the block-id reads going through the lazy accessor so from_tracker-built requests still materialize on the worker thread. Signed-off-by: zhaosusu <zhaosusu1@h-partners.com>
1 parent 5ff96d3 commit ec0be94

1 file changed

Lines changed: 102 additions & 195 deletions

File tree

  • vllm_ascend/distributed/kv_transfer/kv_pool/ascend_store/layerwise/planes

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

Lines changed: 102 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import time
99
from collections.abc import Iterable
1010
from dataclasses import dataclass
11-
from typing import TYPE_CHECKING, Any
11+
from typing import TYPE_CHECKING
1212

1313
import numpy as np
1414
from vllm.logger import logger
@@ -105,13 +105,9 @@ def _alloc_gvas_for_save(self, requests: list[ReqMeta]) -> None:
105105
request.load_spec is None or not request.load_spec.layerwise_previous_step
106106
):
107107
worker._snapshot_keys.pop(request.req_id, None)
108-
# One collect/allocate/distribute round across the whole step: the
109-
# merged exists check and batch_alloc replace the per-request round
110-
# trips, while the shared hash memo and public-key set deduplicate
111-
# exactly like the unbatched path.
112-
if self._alloc_gvas_for_save_batch(requests, hash_strings, public_allocated) is False:
113-
logger.error("Memcache save preparation failed for the batch")
114-
worker._save_disabled.set()
108+
if self._alloc_gvas_for_save_for_request([request], hash_strings, public_allocated) is False:
109+
logger.error("Memcache save preparation failed for %s", request.req_id)
110+
worker._save_disabled.set()
115111
if worker._save_disabled.is_set():
116112
# batch_alloc may return a shared allocation. Stop this step's
117113
# saves without deleting keys that another writer may have published.
@@ -127,40 +123,20 @@ def _alloc_gvas_for_save_for_request(
127123
) -> bool | None:
128124
"""Allocate per-group GVA on the worker side right before batch_copy.
129125
130-
Kept for single-request callers (tests, direct instrumentation);
131-
the step entry point batches all requests in ``_alloc_gvas_for_save``.
132-
"""
133-
worker = self._worker
134-
if not worker.use_layerwise_transfer:
135-
return None
136-
if not worker.can_put:
137-
return None
138-
return self._alloc_gvas_for_save_batch(requests, hash_strings, public_allocated)
139-
140-
def _alloc_gvas_for_save_batch(
141-
self,
142-
requests: list[ReqMeta],
143-
hash_strings: dict[BlockHash | str, str],
144-
public_allocated: set[str],
145-
) -> bool | None:
146-
"""Allocate per-group GVA on the worker side right before batch_copy.
147-
148126
For multi-group models, iterates all KV cache groups and allocates
149127
per-group GVAs. Key format: model@group_id@hash@head_or_tp_rank
150128
(multi-group) or model@hash@head_or_tp_rank (single-group, backward
151129
compat with PR #11585).
152130
153-
Two phases so the store sees one exists check and one allocation
154-
per step instead of one round trip per request: collect every
155-
group's candidate keys without I/O, issue the merged checks, then
156-
distribute the replies in the collection order (which preserves
157-
per-request transfer ordering). The shared hash memo deduplicates
158-
hex conversion and the public-key set deduplicates allocations
159-
across requests, matching the unbatched path's per-step semantics.
131+
Public keys are allocated at most once per step. Allocations are not
132+
cached across steps. Blocks already committed in the pool are filtered
133+
out with a medium-agnostic exists check to avoid re-transferring them.
160134
"""
161135
worker = self._worker
162-
# Per (request, group) I/O-free collection result.
163-
works: list[dict[str, Any]] = []
136+
if not worker.use_layerwise_transfer:
137+
return None
138+
if not worker.can_put:
139+
return None
164140
for request in requests:
165141
if request.can_save is None or not request.can_save:
166142
continue
@@ -251,173 +227,104 @@ def _alloc_gvas_for_save_batch(
251227
all_group_gvas.append(np.zeros(len(block_ids_by_group), dtype=np.int64))
252228
all_group_block_ids.append(np.asarray(block_ids_by_group, dtype=np.int64))
253229
continue
254-
# Hold this group's slot so the published per-group lists keep
255-
# group order; the distribute phase replaces the placeholder.
256-
all_group_gvas.append(np.zeros(len(block_ids_by_group), dtype=np.int64))
257-
all_group_block_ids.append(np.asarray(block_ids_by_group, dtype=np.int64))
258-
works.append(
259-
{
260-
"request": request,
261-
"group_id": group_id,
262-
"block_ids": block_ids_by_group,
263-
"candidate_blocks": candidate_blocks,
264-
"candidate_keys": candidate_keys,
265-
"num_hashes": len(group_block_hashes),
266-
"partial_key": partial_key,
267-
"partial_block_index": partial_block_index,
268-
"alloc_size": alloc_size,
269-
"all_group_gvas": all_group_gvas,
270-
"all_group_block_ids": all_group_block_ids,
271-
"all_group_save_keys": all_group_save_keys,
272-
"log": (
230+
# Skip blocks already committed in the pool. The exists check
231+
# is medium-agnostic on purpose: a committed block (DRAM or
232+
# SSD) does not need re-saving, and the load path rewarms
233+
# SSD blocks on read.
234+
missing_keys = worker._pool_keys_missing(candidate_keys)
235+
missing_keys.difference_update(public_allocated)
236+
transfer_blocks = [
237+
block_idx for block_idx, key in zip(candidate_blocks, candidate_keys) if key in missing_keys
238+
]
239+
240+
# batch_alloc is idempotent: allocating an existing key
241+
# returns its current, client-registered GVA. Every write
242+
# target is therefore allocated fresh within this step; no
243+
# cross-step GVA cache is kept.
244+
block_gvas: list[int] = []
245+
if transfer_blocks:
246+
transfer_keys = [key for key in candidate_keys if key in missing_keys]
247+
transfer_gvas = worker.m_store.batch_alloc(
248+
transfer_keys, [alloc_size] * len(transfer_keys), LAYERWISE_READ_LEASE_TTL_MS
249+
)
250+
if transfer_gvas is None or len(transfer_gvas) != len(transfer_keys):
251+
return False
252+
if any(gva <= 0 for gva in transfer_gvas):
253+
logger.error(
254+
"alloc_gvas FAIL: req=%s group=%d alloc_size=%d alloc_keys=%d gvas_sample=%s fail_count=%d",
273255
request.req_id,
274256
group_id,
275-
effective_block_size,
276-
save_start_block,
277-
save_end_block,
278-
group_store_mask is not None,
279-
),
280-
}
281-
)
282-
# Publish the per-request group lists up front; owner groups are
283-
# appended later, in the distribute phase after the merged I/O.
284-
request.transfer.save_keys = all_group_save_keys
285-
request.transfer.block_gvas_by_group_np = all_group_gvas
286-
request.transfer.block_ids_by_group_np = all_group_block_ids
287-
if all_group_gvas:
288-
request.transfer.block_gvas_np = all_group_gvas[0]
289-
request.transfer.gva_block_offset = 0
290-
if not works:
291-
return None
292-
293-
# Merged exists check for every candidate key of every request.
294-
# Public keys allocated earlier in this step are treated as present:
295-
# batch_alloc is idempotent and re-allocation would waste payload.
296-
all_candidate_keys: list[str] = []
297-
for work in works:
298-
all_candidate_keys.extend(work["candidate_keys"])
299-
missing_keys = worker._pool_keys_missing(all_candidate_keys)
300-
missing_keys.difference_update(public_allocated)
301-
302-
# Skip blocks already committed in the pool. The exists check
303-
# is medium-agnostic on purpose: a committed block (DRAM or
304-
# SSD) does not need re-saving, and the load path rewarms
305-
# SSD blocks on read.
306-
for work in works:
307-
work["transfer_blocks"] = [
308-
block_idx
309-
for block_idx, key in zip(work["candidate_blocks"], work["candidate_keys"])
310-
if key in missing_keys
311-
]
257+
alloc_size,
258+
len(transfer_keys),
259+
transfer_gvas[:5],
260+
sum(1 for g in transfer_gvas if g <= 0),
261+
)
262+
# 0 means allocation failed; UINT64_MAX means a readable
263+
# SSD hit. Filter both before converting GVAs to np.int64.
264+
block_gvas = [gva if 0 < gva < MEMCACHE_SSD_HIT_GVA else 0 for gva in transfer_gvas]
265+
for block_idx, key, gva in zip(transfer_blocks, transfer_keys, block_gvas, strict=True):
266+
if gva > 0:
267+
all_group_save_keys.append(key)
268+
if block_idx < len(group_block_hashes):
269+
public_allocated.add(key)
270+
if worker.layerwise_offload and any(gva <= 0 for gva in transfer_gvas):
271+
return False
272+
273+
if partial_key is not None:
274+
# Partial keys are request-scoped: allocate fresh each step.
275+
allocated = worker.m_store.batch_alloc(
276+
[partial_key],
277+
[alloc_size],
278+
LAYERWISE_READ_LEASE_TTL_MS,
279+
)
280+
if not allocated or len(allocated) != 1:
281+
return False
282+
partial_gva = allocated[0]
283+
if 0 < partial_gva < MEMCACHE_SSD_HIT_GVA:
284+
all_group_save_keys.append(partial_key)
285+
elif partial_gva <= 0:
286+
logger.error(
287+
"alloc_gvas: partial allocation failed req=%s group=%d block=%d gva=%d",
288+
request.req_id,
289+
group_id,
290+
partial_block_index,
291+
partial_gva,
292+
)
293+
if worker.layerwise_offload and partial_gva <= 0:
294+
return False
295+
request.transfer.partial_save_gva_per_group[group_id] = (
296+
partial_gva if 0 < partial_gva < MEMCACHE_SSD_HIT_GVA else 0
297+
)
312298

313-
# One allocation round trip for every missing key of every request.
314-
# batch_alloc is idempotent: allocating an existing key
315-
# returns its current, client-registered GVA. Every write
316-
# target is therefore allocated fresh within this step; no
317-
# cross-step GVA cache is kept.
318-
transfer_keys: list[str] = []
319-
transfer_sizes: list[int] = []
320-
for work in works:
321-
work_missing = [key for key in work["candidate_keys"] if key in missing_keys]
322-
transfer_keys.extend(work_missing)
323-
transfer_sizes.extend([work["alloc_size"]] * len(work_missing))
324-
transfer_gvas: list[int] | None = []
325-
if transfer_keys:
326-
transfer_gvas = worker.m_store.batch_alloc(transfer_keys, transfer_sizes, LAYERWISE_READ_LEASE_TTL_MS)
327-
if transfer_gvas is None or len(transfer_gvas) != len(transfer_keys):
328-
return False
329-
gva_by_key: dict[str, int] = {}
330-
for key, gva in zip(transfer_keys, transfer_gvas or []):
331-
gva_by_key[key] = gva
332-
333-
for work in works:
334-
request = work["request"]
335-
group_id = work["group_id"]
336-
group_block_ids: np.ndarray = work["block_ids"]
337-
alloc_size = work["alloc_size"]
338-
req_id, gid, effective_block_size, save_start_block, save_end_block, masked = work["log"]
339-
candidate_keys = work["candidate_keys"]
340-
transfer_blocks = work["transfer_blocks"]
341-
block_gvas = [gva_by_key[key] for key in candidate_keys if key in missing_keys]
342-
if any(gva <= 0 for gva in block_gvas):
343-
logger.error(
344-
"alloc_gvas FAIL: req=%s group=%d alloc_size=%d alloc_keys=%d gvas_sample=%s fail_count=%d",
345-
req_id,
346-
gid,
299+
logger.debug(
300+
"alloc_gvas: req=%s group=%d eff_bs=%d save_blocks=[%d,%d) "
301+
"transfer_keys=%d committed=%d masked=%s alloc_size=%d",
302+
request.req_id,
303+
group_id,
304+
effective_block_size,
305+
save_start_block,
306+
save_end_block,
307+
len(transfer_blocks),
308+
len(candidate_blocks) - len(transfer_blocks),
309+
group_store_mask is not None,
347310
alloc_size,
348-
len(block_gvas),
349-
block_gvas[:5],
350-
sum(1 for g in block_gvas if g <= 0),
351-
)
352-
# The failure gate reads the RAW replies: a UINT64_MAX sentinel is
353-
# a readable SSD hit, not a failure, even though it filters to 0.
354-
if worker.layerwise_offload and any(gva <= 0 for gva in block_gvas):
355-
return False
356-
# 0 means allocation failed; UINT64_MAX means a readable
357-
# SSD hit. Filter both before converting GVAs to np.int64.
358-
block_gvas = [gva if 0 < gva < MEMCACHE_SSD_HIT_GVA else 0 for gva in block_gvas]
359-
for blk_idx, key, gva in zip(
360-
transfer_blocks, (k for k in candidate_keys if k in missing_keys), block_gvas, strict=True
361-
):
362-
if gva > 0:
363-
work["all_group_save_keys"].append(key)
364-
# A public key allocated here must not be re-allocated by
365-
# a later request in the same step.
366-
if blk_idx < work["num_hashes"]:
367-
public_allocated.add(key)
368-
369-
partial_key = work["partial_key"]
370-
if partial_key is not None:
371-
# Partial keys are request-scoped: allocate fresh each step.
372-
allocated = worker.m_store.batch_alloc(
373-
[partial_key],
374-
[alloc_size],
375-
LAYERWISE_READ_LEASE_TTL_MS,
376-
)
377-
if not allocated or len(allocated) != 1:
378-
return False
379-
partial_gva = allocated[0]
380-
if 0 < partial_gva < MEMCACHE_SSD_HIT_GVA:
381-
work["all_group_save_keys"].append(partial_key)
382-
elif partial_gva <= 0:
383-
logger.error(
384-
"alloc_gvas: partial allocation failed req=%s group=%d block=%d gva=%d",
385-
req_id,
386-
gid,
387-
work["partial_block_index"],
388-
partial_gva,
389-
)
390-
if worker.layerwise_offload and partial_gva <= 0:
391-
return False
392-
request.transfer.partial_save_gva_per_group[group_id] = (
393-
partial_gva if 0 < partial_gva < MEMCACHE_SSD_HIT_GVA else 0
394311
)
395312

396-
logger.debug(
397-
"alloc_gvas: req=%s group=%d eff_bs=%d save_blocks=[%d,%d) "
398-
"transfer_keys=%d committed=%d masked=%s alloc_size=%d",
399-
req_id,
400-
gid,
401-
effective_block_size,
402-
save_start_block,
403-
save_end_block,
404-
len(transfer_blocks),
405-
len(candidate_keys) - len(transfer_blocks),
406-
masked,
407-
alloc_size,
408-
)
313+
# Pad block_gvas to match block_ids length (fill 0 for non-transferred blocks)
314+
full_gvas = [0] * len(block_ids_by_group)
315+
for blk_idx, gva in zip(transfer_blocks, block_gvas):
316+
if blk_idx < len(full_gvas):
317+
full_gvas[blk_idx] = gva
318+
319+
all_group_gvas.append(np.asarray(full_gvas, dtype=np.int64))
320+
all_group_block_ids.append(np.asarray(block_ids_by_group, dtype=np.int64))
409321

410-
# Pad block_gvas to match block_ids length (fill 0 for non-transferred blocks)
411-
full_gvas = [0] * len(group_block_ids)
412-
for blk_idx, gva in zip(transfer_blocks, block_gvas):
413-
if blk_idx < len(full_gvas):
414-
full_gvas[blk_idx] = gva
415-
416-
work["all_group_gvas"][group_id] = np.asarray(full_gvas, dtype=np.int64)
417-
work["all_group_block_ids"][group_id] = np.asarray(group_block_ids, dtype=np.int64)
418-
# The group-0 view is only valid once at least one group landed.
419-
if work["all_group_gvas"]:
420-
request.transfer.block_gvas_np = work["all_group_gvas"][0]
322+
if all_group_gvas:
323+
request.transfer.save_keys = all_group_save_keys
324+
request.transfer.block_gvas_by_group_np = all_group_gvas
325+
request.transfer.block_ids_by_group_np = all_group_block_ids
326+
request.transfer.block_gvas_np = all_group_gvas[0]
327+
request.transfer.gva_block_offset = 0
421328
return None
422329

423330
# --- load path -----------------------------------------------------------

0 commit comments

Comments
 (0)