Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 31 additions & 66 deletions tests/v1/kv_connector/unit/offloading_connector/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
OffloadPolicy,
ReqContext,
RequestOffloadingContext,
StepResult,
StoreTrigger,
get_offload_block_hash,
get_offload_group_idx,
Expand Down Expand Up @@ -2537,27 +2536,6 @@ def test_cache_reset_without_in_flight_jobs_holds_nothing():
scheduler.shutdown()


def test_cascade_after_the_last_store_keeps_engine_stepping():
"""The last store's report cascades it to a secondary tier after
on_schedule_end(), so the engine keeps stepping until that job is done."""
scheduler, manager = _make_tiering_engine_scheduler([{"type": "example"}])
try:
stored = _store_in_flight(scheduler)
output = scheduler.schedule()
_report_jobs(scheduler, output, stored)
assert manager.has_pending_work()

for _ in range(10):
if not scheduler.has_requests():
break
output = scheduler.schedule()
scheduler.update_from_output(output, create_model_runner_output([]))
assert not scheduler.has_requests()
assert not manager.has_pending_work()
finally:
scheduler.shutdown()


def test_cache_reset_twice_before_a_step_still_flushes():
"""A second reset before the engine steps must not assert: RL loops call
reset_prefix_cache(reset_connector=True) every iteration, and EngineCore
Expand Down Expand Up @@ -3010,61 +2988,51 @@ def test_step_releases_the_manager_before_the_model_runs(request_runner):
assert lock.__enter__.call_count == lock.__exit__.call_count


def test_has_pending_push_work_follows_the_step_result(request_runner):
"""It reads the last StepResult and never enters the manager.
def test_has_pending_push_work_does_not_open_the_step_region(request_runner):
"""It must take a short region, never the step-scoped one.

The engine asks this before schedule(), so entering the manager here could
leave the step-scoped region open on an idle engine.
The engine asks this before schedule() and returns early when the answer is
False, so a step-scoped acquire here would never reach the release in
build_connector_meta and would hold the manager for good.
"""
runner = request_runner(
block_size=4,
num_gpu_blocks=10,
async_scheduling=False,
)
scheduler = runner.connector_scheduler
lock = runner.manager.lock
# No step has reported yet.
assert scheduler.has_pending_push_work() is True

runner.manager.on_schedule_end.return_value = StepResult(pending=True)
runner.scheduler.schedule()
entered = lock.__enter__.call_count
assert scheduler.has_pending_push_work() is True
runner.manager.has_pending_work.return_value = False

runner.manager.on_schedule_end.return_value = StepResult()
runner.scheduler.schedule()
assert scheduler.has_pending_push_work() is False
assert lock.__enter__.call_count == entered + 1
assert scheduler._step_region is None
lock = runner.manager.lock
assert lock.__enter__.call_count == lock.__exit__.call_count


def test_step_result_events_and_stats_are_reported_once(request_runner):
"""Results of several steps accumulate until take_events() and
get_stats() drain them, as with async scheduling."""
def test_take_events_drains_the_manager_inside_the_region(request_runner):
"""take_events must not hold the manager across its own iteration.

manager.take_events() is a generator whose backing list is cleared once
exhausted, so draining it lazily would both pin the manager for as long as
the consumer takes and lose events appended in the meantime.
"""
runner = request_runner(
block_size=4,
num_gpu_blocks=10,
async_scheduling=False,
)
scheduler = runner.connector_scheduler
keys = [make_offload_key(bytes([i]) * 32, 0) for i in range(2)]
for key in keys:
stats = OffloadingConnectorStats()
stats.increase_counter(_ConnectorMetricName.ALLOCATION_FAILURE)
runner.manager.on_schedule_end.return_value = StepResult(
events=[OffloadingEvent(keys=[key], medium=Medium.CPU, removed=True)],
stats=stats,
)
runner.scheduler.schedule()
runner.manager.take_events.return_value = iter(())
runner.manager.reset_mock()

events = list(scheduler.take_events())
assert len(events) == 2
assert all(isinstance(event, BlockRemoved) for event in events)
assert list(scheduler.take_events()) == []
stats = scheduler.get_stats()
assert stats is not None
assert stats.reduce()[_ConnectorMetricName.ALLOCATION_FAILURE] == 2
assert scheduler.get_stats() is None

assert events == []
assert runner.manager.take_events.call_count == 1
assert scheduler._step_region is None
lock = runner.manager.lock
assert lock.__enter__.call_count == 1
assert lock.__exit__.call_count == 1


def test_scheduler_reports_lookup_async_delay_on_resolve(request_runner):
Expand Down Expand Up @@ -3094,6 +3062,7 @@ def test_max_offload_tokens_zero_does_not_record_pending_lookups(request_runner)
async_scheduling=False,
)
runner.manager.lookup.return_value = LookupResult.RETRY
runner.manager.take_events.return_value = []
runner.manager.prepare_store.side_effect = lambda keys, req_context, trigger: (
generate_store_output(keys)
)
Expand Down Expand Up @@ -3127,13 +3096,12 @@ def test_abort_before_hit_uses_placeholder_then_later_hit_heals_removal(
)
raw_events: list[OffloadingEvent] = []

def on_schedule_end(context):
events = list(raw_events)
def take_raw_events():
yield from raw_events
raw_events.clear()
return StepResult(events=events)

runner.manager.lookup.return_value = LookupResult.RETRY
runner.manager.on_schedule_end.side_effect = on_schedule_end
runner.manager.take_events.side_effect = take_raw_events
runner.manager.prepare_store.side_effect = lambda keys, req_context, trigger: (
generate_store_output([])
)
Expand All @@ -3152,7 +3120,6 @@ def on_schedule_end(context):
assert not tracker._pending_event_metadata

raw_events.append(OffloadingEvent(keys=[key], medium=Medium.CPU, removed=False))
runner.scheduler.schedule()
events = list(runner.connector_scheduler.take_events())
assert len(events) == 1
assert isinstance(events[0], BlockStored)
Expand All @@ -3174,7 +3141,6 @@ def on_schedule_end(context):
assert key in tracker._pending_event_metadata

raw_events.append(OffloadingEvent(keys=[key], medium=Medium.CPU, removed=True))
runner.scheduler.schedule()
[event] = runner.connector_scheduler.take_events()
assert isinstance(event, BlockRemoved)
assert event.medium == MEDIUM_CPU
Expand Down Expand Up @@ -3212,13 +3178,12 @@ def lookup(key, req_context):
raw_events.append(OffloadingEvent(keys=[key], medium=Medium.CPU, removed=False))
return LookupResult.HIT

def on_schedule_end(context):
events = list(raw_events)
def take_raw_events():
yield from raw_events
raw_events.clear()
return StepResult(events=events)

runner.manager.lookup.side_effect = lookup
runner.manager.on_schedule_end.side_effect = on_schedule_end
runner.manager.take_events.side_effect = take_raw_events
runner.manager.prepare_store.side_effect = lambda keys, req_context, trigger: (
generate_store_output([])
)
Expand Down
3 changes: 1 addition & 2 deletions tests/v1/kv_connector/unit/offloading_connector/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
OffloadKey,
PrepareStoreOutput,
RequestOffloadingContext,
StepResult,
TransferResult,
make_offload_key,
)
Expand Down Expand Up @@ -224,7 +223,7 @@ def __init__(self, config: OffloadingConfig):
self.manager.lookup.return_value = LookupResult.MISS
self.manager.prepare_store.return_value.keys_to_retry = []
self.manager.get_load_source.return_value = CacheHitSource.EXTERNAL_UNSPECIFIED
self.manager.on_schedule_end.return_value = StepResult()
self.manager.get_stats.return_value = None
self.manager.config_info.return_value = [{}]
self.manager.on_new_request.return_value = RequestOffloadingContext()

Expand Down
38 changes: 0 additions & 38 deletions tests/v1/kv_offload/cpu/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
OffloadKey,
PrepareStoreOutput,
ReqContext,
ScheduleEndContext,
StoreTrigger,
make_offload_key,
)
Expand Down Expand Up @@ -171,43 +170,6 @@ def test_cpu_stored_event_carries_request_context():
assert event.req_context is req_context


def test_cpu_schedule_end_reports_events_and_stats_once():
manager = make_cpu_manager(num_chunks=2, enable_events=True)
manager.prepare_store(to_keys([1]), _EMPTY_REQ_CTX, trigger=StoreTrigger.ON_COMPUTE)
manager.complete_store(
to_keys([1]), _EMPTY_REQ_CTX, trigger=StoreTrigger.ON_COMPUTE
)
context = ScheduleEndContext(new_req_ids=(), preempted_req_ids=())

result = manager.on_schedule_end(context)

assert not result.pending
verify_events(result.events, expected_stores=({1},))
assert result.stats is not None
assert CPUOffloadingMetrics.CPU_CACHE_USAGE_PERC in result.stats.reduce()
assert manager.on_schedule_end(context).events == []


def test_reset_drops_undrained_stored_events():
manager = make_cpu_manager(num_chunks=2, enable_events=True)
manager.prepare_store(
to_keys([1, 2]), _EMPTY_REQ_CTX, trigger=StoreTrigger.ON_COMPUTE
)
manager.complete_store(
to_keys([1, 2]), _EMPTY_REQ_CTX, trigger=StoreTrigger.ON_COMPUTE
)
list(manager.take_events())
# Evicts chunk 1, then stores chunk 3; neither event is drained yet.
manager.prepare_store(to_keys([3]), _EMPTY_REQ_CTX, trigger=StoreTrigger.ON_COMPUTE)
manager.complete_store(
to_keys([3]), _EMPTY_REQ_CTX, trigger=StoreTrigger.ON_COMPUTE
)

manager.reset_cache()

verify_events(manager.take_events(), expected_evictions=({1},))


def test_cpu_eviction_removed_precedes_stored():
"""An eviction is announced before the store that reuses its capacity."""
manager = make_cpu_manager(num_chunks=2, enable_events=True)
Expand Down
14 changes: 0 additions & 14 deletions tests/v1/kv_offload/tiering/p2p/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,6 @@ def test_B2_idle_manager_does_not_keep_engine_busy(self):

from vllm.distributed.kv_transfer.kv_connector.v1.offloading.scheduler import (
OffloadingConnectorScheduler,
_ManagerStepState,
)
from vllm.distributed.kv_transfer.kv_connector.v1.offloading_connector import (
OffloadingConnector,
Expand All @@ -1344,12 +1343,6 @@ def test_B2_idle_manager_does_not_keep_engine_busy(self):
connector_scheduler.manager = manager
connector_scheduler._job_statuses = {}
connector_scheduler._reset_fence = {}
connector_scheduler._step_state = _ManagerStepState()
connector_scheduler._step_state.update(
manager.on_schedule_end(
ScheduleEndContext(new_req_ids=(), preempted_req_ids=())
)
)
connector = object.__new__(OffloadingConnector)
connector.connector_scheduler = connector_scheduler
scheduler = object.__new__(Scheduler)
Expand Down Expand Up @@ -1381,7 +1374,6 @@ def test_B2_no_poll_thread_keeps_engine_busy(self):

from vllm.distributed.kv_transfer.kv_connector.v1.offloading.scheduler import (
OffloadingConnectorScheduler,
_ManagerStepState,
)
from vllm.distributed.kv_transfer.kv_connector.v1.offloading_connector import (
OffloadingConnector,
Expand Down Expand Up @@ -1410,12 +1402,6 @@ def test_B2_no_poll_thread_keeps_engine_busy(self):
connector_scheduler.manager = manager
connector_scheduler._job_statuses = {}
connector_scheduler._reset_fence = {}
connector_scheduler._step_state = _ManagerStepState()
connector_scheduler._step_state.update(
manager.on_schedule_end(
ScheduleEndContext(new_req_ids=(), preempted_req_ids=())
)
)
connector = object.__new__(OffloadingConnector)
connector.connector_scheduler = connector_scheduler
scheduler = object.__new__(Scheduler)
Expand Down
7 changes: 4 additions & 3 deletions tests/v1/kv_offload/tiering/test_fs_tier.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,8 +1190,8 @@ def test_events_require_global_kv_events_flag(tmp_path):


def test_cascade_store_emits_fs_event_through_tiering_manager(tmp_path):
"""A GPU->CPU->fs cascade surfaces the tier-owned FS stored event in the
TieringOffloadingManager's aggregated StepResult."""
"""A GPU->CPU->fs cascade surfaces the tier-owned FS stored event via the
TieringOffloadingManager's aggregated take_events()."""
from vllm.v1.kv_offload.tiering.manager import (
CPUPrimaryTierOffloadingManager,
TieringOffloadingManager,
Expand Down Expand Up @@ -1225,7 +1225,8 @@ def test_cascade_store_emits_fs_event_through_tiering_manager(tmp_path):
ctx = ScheduleEndContext(new_req_ids=[], preempted_req_ids=())
deadline = time.monotonic() + 5.0
while time.monotonic() < deadline and not events:
events.extend(manager.on_schedule_end(ctx).events)
manager.on_schedule_end(ctx)
events.extend(manager.take_events())
time.sleep(0.01)

fs_events = [e for e in events if e.medium == Medium.STORAGE]
Expand Down
15 changes: 8 additions & 7 deletions tests/v1/kv_offload/tiering/test_tiering_offloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
OffloadPolicy,
ReqContext,
ScheduleEndContext,
StepResult,
StoreTrigger,
TierFilter,
TierMatcher,
Expand Down Expand Up @@ -359,12 +358,11 @@ def manager_setup(self):
secondary_tiers=[self.secondary_tier1, self.secondary_tier2],
)

def _simulate_on_schedule_end(
self, new_req_ids: list[str] | None = None
) -> StepResult:
def _simulate_on_schedule_end(self, new_req_ids: list[str] | None = None):
"""Simulate end of scheduler step: lifecycle flush + drain events."""
ctx = ScheduleEndContext(new_req_ids=new_req_ids or [], preempted_req_ids=())
return self.manager.on_schedule_end(ctx)
self.manager.on_schedule_end(ctx)
list(self.manager.take_events())

def _start_request(self, req_context: ReqContext = _CTX):
if req_context.req_id not in self.manager._req_state:
Expand Down Expand Up @@ -641,7 +639,9 @@ def submit_load(job_metadata):
self._simulate_on_schedule_end()

# End of step 2: processes the completed promotion jobs
stats = self._simulate_on_schedule_end().stats
self._simulate_on_schedule_end()

stats = self.manager.get_stats()
assert stats is not None
reduced = stats.reduce()
assert (
Expand Down Expand Up @@ -935,7 +935,8 @@ def test_lookup_does_not_report_async_delay_for_promotion(self, manager_setup):
assert self.manager.lookup(chunk, _CTX) is LookupResult.HIT_PENDING

# Promotion is not treated as unresolved secondary lookup time.
stats = self._simulate_on_schedule_end(new_req_ids=[_CTX.req_id]).stats
self._simulate_on_schedule_end(new_req_ids=[_CTX.req_id])
stats = self.manager.get_stats()
assert stats is not None
reduced = stats.reduce()
assert (
Expand Down
Loading
Loading