[EPD] Add ECMooncakeConnector for encoder cache over Mooncake TransferEngine (#41567)

Signed-off-by: Teng Ma <sima.mt@alibaba-inc.com>
Signed-off-by: Tianyu Guo <guoty@inferact.ai>
Signed-off-by: Zhou ziheng <jiaranran2@gmail.com>
Signed-off-by: jiangkuaixue123 <jiangxiaozhou111@163.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Tianyu Guo <guoty@inferact.ai>
Co-authored-by: Zhou ziheng <jiaranran2@gmail.com>
Co-authored-by: jiangkuaixue123 <jiangxiaozhou111@163.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Teng Ma
2026-09-07 06:21:43 +00:00
committed by GitHub
co-authored by Cursor Tianyu Guo Zhou ziheng jiangkuaixue123 OpenAI Codex
parent f7f060d253
commit 6fbb00b188
30 changed files with 5774 additions and 301 deletions
@@ -167,6 +167,38 @@ def test_get_freed_mm_hashes_clears_freed_list():
assert manager.get_freed_mm_hashes() == []
def test_referencing_a_freeable_entry_protects_it_from_eviction():
"""A waiting request can hold an entry it is not scheduled for yet.
Entries with no referent are evictable. A request that is deferred (e.g.
by an EC connector waiting on another item) still needs the items it
already has, and a connector that hands out one transfer per request
cannot always produce the same item a second time.
"""
manager = EncoderCacheManager(cache_size=10)
owner = MockRequest("owner", ["a"], [5])
waiter = MockRequest("waiter", ["a"], [5])
newcomer = MockRequest("newcomer", ["b"], [6])
manager.allocate(owner, 0)
manager.free_encoder_input(owner, 0)
assert "a" in manager.freeable
# The waiting request references it; it is no longer reclaimable.
assert manager.check_and_update_cache(waiter, 0)
assert "a" not in manager.freeable
# 'b' no longer fits, and 'a' must not be taken away to make room.
assert not manager.can_allocate(newcomer, 0, int(1e9), 0)
assert "a" in manager.cached
assert manager.get_freed_mm_hashes() == []
# Once the waiter is done with it, the entry is reclaimable again.
manager.free_encoder_input(waiter, 0)
assert manager.can_allocate(newcomer, 0, int(1e9), 0)
assert manager.get_freed_mm_hashes() == ["a"]
def test_reallocated_hash_is_not_reported_as_freed():
manager = EncoderCacheManager(cache_size=8)
req_a = MockRequest("reqA", ["a"], [4])
+89 -3
View File
@@ -5643,6 +5643,68 @@ def test_free_encoder_inputs_respects_unconfirmed_placeholders():
assert manager.get_cached_input_ids(request) == set()
def test_free_encoder_inputs_notifies_the_ec_connector():
"""The connector learns the item is consumed, not just that the request ended.
Connectors hold per-item transfer state (remote buffers, reservations);
waiting for `request_finished` pins it for the whole generation.
"""
scheduler = create_scheduler(model="llava-hf/llava-1.5-7b-hf")
mm_start_pos, mm_length = 50, 100
request = create_requests(
num_requests=1,
num_tokens=mm_start_pos + mm_length + 10,
mm_positions=[[PlaceholderRange(offset=mm_start_pos, length=mm_length)]],
)[0]
scheduler.encoder_cache_manager.allocate(request, 0)
scheduler.ec_connector = Mock()
request.num_computed_tokens = mm_start_pos + mm_length - 1
scheduler._free_encoder_inputs(request)
scheduler.ec_connector.update_state_after_free.assert_not_called()
request.num_computed_tokens = mm_start_pos + mm_length
scheduler._free_encoder_inputs(request)
scheduler.ec_connector.update_state_after_free.assert_called_once_with(request, 0)
def test_unavailable_encoder_input_fails_the_request_as_retryable():
"""An encoder input the connector gave up on must end the request.
`FinishReason.ERROR` is the retryable channel the KV connector already uses
for load failures, so the caller can re-issue; deferring instead parked the
request until the client timed out.
"""
scheduler = create_scheduler(model="llava-hf/llava-1.5-7b-hf")
request = create_requests(
num_requests=1,
num_tokens=160,
mm_positions=[[PlaceholderRange(offset=50, length=100)]],
)[0]
scheduler.add_request(request)
scheduler.ec_connector = Mock()
scheduler.ec_connector.take_unavailable_requests.return_value = {request.request_id}
# `request_finished` reports (delay_free, params) for the request teardown.
scheduler.ec_connector.request_finished.return_value = (False, None)
scheduler_output = scheduler.schedule()
outputs = scheduler.update_from_output(
scheduler_output,
ModelRunnerOutput(
req_ids=[request.request_id],
req_id_to_index={request.request_id: 0},
sampled_token_ids=[[]],
logprobs=None,
prompt_logprobs_dict={},
pooler_output=[],
),
)
assert request.status == RequestStatus.FINISHED_ERROR
engine_outputs = outputs[0].outputs
assert [o.finish_reason for o in engine_outputs] == [FinishReason.ERROR]
def test_free_encoder_inputs_defers_for_eagle_lookahead():
"""With EAGLE speculative decoding, the encoder input is retained one extra
position so the drafter's +1 look-ahead mm-embedding gather (which reads one
@@ -5840,9 +5902,8 @@ def test_ec_connector_ensure_cache_available_defers_request(use_kv_connector):
# ensure_cache_available must have been called with (request, num_computed_tokens=0)
# for a brand-new request that has no cached tokens yet.
scheduler.ec_connector.ensure_cache_available.assert_called_once_with(
request_deferred, 0
)
ensure_call = scheduler.ec_connector.ensure_cache_available.call_args
assert ensure_call.args == (request_deferred, 0)
# Deferred request must NOT be scheduled
assert request_deferred.request_id not in output.num_scheduled_tokens
_assert_right_encoder_cache_allocated(scheduler, expected_total_allocated=0)
@@ -5874,6 +5935,31 @@ def test_ec_connector_ensure_cache_available_defers_request(use_kv_connector):
_assert_right_encoder_inputs(output, expected_total_reqs=0)
def test_ec_connector_defers_running_request_for_async_reload():
scheduler = create_scheduler(
model="llava-hf/llava-1.5-7b-hf",
max_num_batched_tokens=32,
use_ec_connector=True,
ec_role="ec_consumer",
)
request = create_requests(
num_requests=1,
num_tokens=128,
mm_positions=[[PlaceholderRange(offset=48, length=32)]],
req_ids=["request"],
)[0]
scheduler.ec_connector.ensure_cache_available = Mock(side_effect=[True, False])
scheduler.add_request(request)
first_output = scheduler.schedule()
assert first_output.num_scheduled_tokens[request.request_id] == 32
second_output = scheduler.schedule()
assert request.request_id not in second_output.num_scheduled_tokens
ensure_call = scheduler.ec_connector.ensure_cache_available.call_args
assert ensure_call.args[:2] == (request, 32)
def test_ec_connector_pending_prefetch_only_checks_future_mm_features():
"""Test that future mm feature filtering only yields features beyond
the computed token frontier.