[EC Connector] Added Build Connector Worker Meta for EC Connector (#49585)

Signed-off-by: yewentao256 <zhyanwentao@126.com>
Signed-off-by: omerpaz95 <omerpaz95@gmail.com>
Signed-off-by: omerpaz95 <73347585+omerpaz95@users.noreply.github.com>
Co-authored-by: yewentao256 <zhyanwentao@126.com>
Co-authored-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com>
This commit is contained in:
omerpaz95
2026-08-15 15:40:38 -07:00
committed by GitHub
co-authored by yewentao256 Wentao Ye
parent c94cdd0ae0
commit fa9d67f782
16 changed files with 430 additions and 16 deletions
@@ -20,6 +20,8 @@ The class provides the following primitives:
get_finished() - called with ids of finished requests, returns
ids of requests that have completed async sending/recving.
build_connector_worker_meta() - builds metadata to be sent
back to the scheduler-side connector
"""
import enum
@@ -56,6 +58,27 @@ class ECConnectorMetadata(ABC): # noqa: B024
pass
class ECConnectorWorkerMetadata(ABC):
"""
Abstract Metadata used to communicate back
Worker ECConnector -> Scheduler ECConnector.
Each worker can output its own metadata.
For a single engine step, all metadata objects returned by workers
will be aggregated using the `aggregate` method below, before
being passed to the Scheduler ECConnector.
"""
@abstractmethod
def aggregate(
self, other: "ECConnectorWorkerMetadata"
) -> "ECConnectorWorkerMetadata":
"""
Aggregate metadata with another `ECConnectorWorkerMetadata` object.
"""
pass
class ECConnectorBase(ABC):
def __init__(self, vllm_config: "VllmConfig", role: ECConnectorRole):
self._connector_metadata: ECConnectorMetadata | None = None
@@ -190,6 +213,16 @@ class ECConnectorBase(ABC):
"""
return None, None
def build_connector_worker_meta(self) -> ECConnectorWorkerMetadata | None:
"""
Build the ECConnector worker metadata for this engine step.
Returns:
ECConnectorWorkerMetadata: the worker metadata.
None if no worker metadata is available.
"""
return None
# ==============================
# Scheduler-side methods
# ==============================
@@ -0,0 +1,51 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""EC connector helper utilities."""
from vllm.v1.outputs import ECConnectorOutput, ModelRunnerOutput
class ECOutputAggregator:
"""Merge every worker's EC connector output onto the single
ModelRunnerOutput that reaches the scheduler.
Mirrors KVOutputAggregator: only `output_rank`'s output is returned to the
scheduler, but the EC connector may have run on any rank.
"""
def aggregate(
self, outputs: list[ModelRunnerOutput | None], output_rank: int = 0
) -> ModelRunnerOutput | None:
output = outputs[output_rank]
if not output:
return None
finished_sending = set[str]()
finished_recving = set[str]()
worker_meta = None
for model_runner_output in outputs:
assert model_runner_output is not None
ec_output = model_runner_output.ec_connector_output
if not ec_output:
continue
finished_sending |= ec_output.finished_sending or set()
finished_recving |= ec_output.finished_recving or set()
if meta := ec_output.ec_connector_worker_meta:
worker_meta = (
meta if worker_meta is None else worker_meta.aggregate(meta)
)
aggregated = ECConnectorOutput(
finished_sending=finished_sending or None,
finished_recving=finished_recving or None,
ec_connector_worker_meta=worker_meta,
)
if aggregated.is_empty():
output.ec_connector_output = None
return output
# `output` is the shared empty output whenever `output_rank` had no work,
# so attach through the copy-on-write helper.
return ModelRunnerOutput.with_ec_conn_output(output, aggregated)
+2
View File
@@ -173,6 +173,8 @@ class EngineCore:
)
if self.scheduler.connector is not None: # type: ignore
self.model_executor.init_kv_output_aggregator(self.scheduler.connector) # type: ignore
if self.scheduler.ec_connector is not None: # type: ignore
self.model_executor.init_ec_output_aggregator()
mm_registry = MULTIMODAL_REGISTRY
self.mm_receiver_cache = mm_registry.engine_receiver_cache_from_config(
+5
View File
@@ -9,6 +9,7 @@ from typing import TYPE_CHECKING, Literal, TypeVar, overload
import vllm.envs as envs
from vllm.config import VllmConfig
from vllm.distributed.ec_transfer.ec_connector.utils import ECOutputAggregator
from vllm.distributed.kv_transfer.kv_connector.utils import KVOutputAggregator
from vllm.distributed.kv_transfer.kv_connector.v1.base import (
KVConnectorHandshakeMetadata,
@@ -110,6 +111,7 @@ class Executor(ABC):
self.is_sleeping = False
self.sleeping_tags: set[str] = set()
self.kv_output_aggregator: KVOutputAggregator | None = None
self.ec_output_aggregator: ECOutputAggregator | None = None
@abstractmethod
def _init_executor(self) -> None:
@@ -283,6 +285,9 @@ class Executor(ABC):
connector, self.parallel_config.world_size
)
def init_ec_output_aggregator(self) -> None:
self.ec_output_aggregator = ECOutputAggregator()
@cached_property # Avoid unnecessary RPC calls
def supported_tasks(self) -> tuple[SupportedTask, ...]:
output: list[tuple[SupportedTask, ...]]
+20 -6
View File
@@ -29,6 +29,7 @@ import vllm.envs as envs
from vllm.config import VllmConfig
from vllm.distributed import destroy_distributed_environment, destroy_model_parallel
from vllm.distributed.device_communicators.shm_broadcast import Handle, MessageQueue
from vllm.distributed.ec_transfer.ec_connector.utils import ECOutputAggregator
from vllm.distributed.kv_transfer.kv_connector.utils import KVOutputAggregator
from vllm.distributed.parallel_state import (
get_dcp_group,
@@ -343,6 +344,7 @@ class MultiprocExecutor(Executor):
non_block=non_block,
timeout=envs.VLLM_EXECUTE_MODEL_TIMEOUT_SECONDS,
kv_output_aggregator=self.kv_output_aggregator,
ec_output_aggregator=self.ec_output_aggregator,
)
def sample_tokens( # type: ignore[override]
@@ -355,6 +357,7 @@ class MultiprocExecutor(Executor):
non_block=non_block,
timeout=envs.VLLM_EXECUTE_MODEL_TIMEOUT_SECONDS,
kv_output_aggregator=self.kv_output_aggregator,
ec_output_aggregator=self.ec_output_aggregator,
)
def execute_dummy_batch(self) -> None:
@@ -375,9 +378,10 @@ class MultiprocExecutor(Executor):
non_block: bool = False,
unique_reply_rank: int | None = None,
kv_output_aggregator: KVOutputAggregator | None = None,
ec_output_aggregator: ECOutputAggregator | None = None,
) -> Any:
"""Returns single result if unique_reply_rank and/or kv_output_aggregator
is provided, otherwise list."""
"""Returns single result if unique_reply_rank and/or an output
aggregator is provided, otherwise list."""
assert self.rpc_broadcast_mq is not None, (
"collective_rpc should not be called on follower node"
)
@@ -387,11 +391,21 @@ class MultiprocExecutor(Executor):
deadline = None if timeout is None else time.monotonic() + timeout
kwargs = kwargs or {}
if kv_output_aggregator is not None:
aggregators = [a for a in (kv_output_aggregator, ec_output_aggregator) if a]
aggregate: Callable[[Any], Any]
if aggregators:
output_rank = None
aggregate: Callable[[Any], Any] = partial(
kv_output_aggregator.aggregate, output_rank=unique_reply_rank or 0
)
def _aggregate(outputs: Any) -> Any:
# Each aggregator merges its own connector's output onto
# outputs[output_rank] in place and returns it, so chaining is safe.
rank = unique_reply_rank or 0
result = outputs[rank]
for a in aggregators:
result = a.aggregate(outputs, output_rank=rank)
return result
aggregate = _aggregate
else:
output_rank = unique_reply_rank
aggregate = lambda x: x
+13
View File
@@ -89,6 +89,19 @@ class RayDistributedExecutor(Executor):
# KV connector setup
self.has_connector = self.vllm_config.kv_transfer_config is not None
if (
self.vllm_config.ec_transfer_config is not None
and self.parallel_config.world_size > 1
):
raise NotImplementedError(
"EC connector worker metadata is not supported with the "
"legacy Ray executor when world_size > 1: only the output "
"of a single worker is fetched, silently dropping the "
"other workers' EC connector state. Set "
"VLLM_USE_RAY_V2_EXECUTOR_BACKEND=1 to use RayExecutorV2, "
"or use the multiprocessing executor instead."
)
self.uses_sampler = self.vllm_config.model_config.runner_type != "pooling" and (
self.vllm_config.ec_transfer_config is None
or self.vllm_config.ec_transfer_config.is_ec_consumer
+36
View File
@@ -14,6 +14,7 @@ from vllm.compilation.cuda_graph import CUDAGraphStat
from vllm.v1.core.sched.output import SchedulerOutput
if TYPE_CHECKING:
from vllm.distributed.ec_transfer.ec_connector.base import ECConnectorWorkerMetadata
from vllm.distributed.kv_events import KVConnectorKVEvents
from vllm.distributed.kv_transfer.kv_connector.v1.base import (
KVConnectorWorkerMetadata,
@@ -23,6 +24,7 @@ else:
KVConnectorStats = object
KVConnectorWorkerMetadata = object
KVConnectorKVEvents = object
ECConnectorWorkerMetadata = object
class LogprobsLists(NamedTuple):
@@ -292,6 +294,14 @@ class ECConnectorOutput:
# [mm_hash]
finished_sending: set[str] | None = None
finished_recving: set[str] | None = None
ec_connector_worker_meta: ECConnectorWorkerMetadata | None = None
def is_empty(self):
return (
not self.finished_sending
and not self.finished_recving
and not self.ec_connector_worker_meta
)
# ModelRunnerOutput is serialized and sent to the scheduler process.
@@ -362,6 +372,32 @@ class ModelRunnerOutput:
output.kv_connector_output = kv_connector_output
return output
@staticmethod
def with_ec_conn_output_only(
ec_connector_output: ECConnectorOutput | None,
) -> "ModelRunnerOutput":
"""Return an otherwise-empty output carrying `ec_connector_output`."""
return ModelRunnerOutput.with_ec_conn_output(
EMPTY_MODEL_RUNNER_OUTPUT, ec_connector_output
)
@staticmethod
def with_ec_conn_output(
output: "ModelRunnerOutput",
ec_connector_output: ECConnectorOutput | None,
) -> "ModelRunnerOutput":
"""Return `output` carrying `ec_connector_output`.
The shared empty output is copied rather than written to, so callers
must use the return value.
"""
if ec_connector_output is None or ec_connector_output.is_empty():
return output
if output is EMPTY_MODEL_RUNNER_OUTPUT:
output = copy(EMPTY_MODEL_RUNNER_OUTPUT)
output.ec_connector_output = ec_connector_output
return output
# ModelRunnerOutput wrapper for async scheduling.
class AsyncModelRunnerOutput(ABC):
+25 -2
View File
@@ -9,7 +9,11 @@ import torch
from vllm.config import VllmConfig
from vllm.distributed.ec_transfer import get_ec_transfer, has_ec_transfer
from vllm.distributed.ec_transfer.ec_connector.base import ECConnectorBase
from vllm.v1.outputs import ECConnectorOutput
from vllm.v1.outputs import (
EMPTY_MODEL_RUNNER_OUTPUT,
ECConnectorOutput,
ModelRunnerOutput,
)
if TYPE_CHECKING:
from vllm.v1.core.sched.output import SchedulerOutput
@@ -25,6 +29,12 @@ class ECConnector:
) -> Generator[ECConnectorOutput | None, None, None]:
yield None
def no_forward(
self,
scheduler_output: "SchedulerOutput",
) -> ModelRunnerOutput:
return EMPTY_MODEL_RUNNER_OUTPUT
class ActiveECConnector(ECConnector):
def __init__(
@@ -33,9 +43,11 @@ class ActiveECConnector(ECConnector):
encoder_cache: dict[str, torch.Tensor],
) -> None:
self.encoder_cache = encoder_cache
self.save_new_caches = vllm_config.is_ec_producer_only
self.ec_connector = get_ec_transfer()
assert isinstance(self.ec_connector, ECConnectorBase)
# Every producer offloads freshly computed encoder outputs, including
# an ec_both node that also reloads them.
self.save_new_caches = self.ec_connector.is_producer
@contextmanager
def maybe_get_output(
@@ -65,8 +77,19 @@ class ActiveECConnector(ECConnector):
output.finished_sending, output.finished_recving = (
ec_connector.get_finished(scheduler_output.finished_req_ids)
)
output.ec_connector_worker_meta = ec_connector.build_connector_worker_meta()
ec_connector.clear_connector_metadata()
def no_forward(
self,
scheduler_output: "SchedulerOutput",
) -> ModelRunnerOutput:
# EC send/recv even if no work to do.
with self.maybe_get_output(scheduler_output) as ec_connector_output:
pass
return ModelRunnerOutput.with_ec_conn_output_only(ec_connector_output)
NO_OP_EC_CONNECTOR = ECConnector()
+29 -7
View File
@@ -65,6 +65,7 @@ from vllm.v1.core.sched.output import GrammarOutput, SchedulerOutput
from vllm.v1.kv_cache_interface import KVCacheConfig, MambaSpec
from vllm.v1.outputs import (
DraftTokenIds,
ECConnectorOutput,
ModelRunnerOutput,
RoutedExpertsTensors,
make_empty_encoder_model_runner_output,
@@ -1393,6 +1394,15 @@ class GPUModelRunner(LoRAModelRunnerMixin):
idx_mapping, num_sampled, self.req_states.num_computed_tokens.gpu
)
def _merge_ec_connector_no_forward(
self, scheduler_output: SchedulerOutput, output: ModelRunnerOutput
) -> ModelRunnerOutput:
"""Let the EC connector send/recv on a step with no work to run."""
return ModelRunnerOutput.with_ec_conn_output(
output,
self.ec_connector.no_forward(scheduler_output).ec_connector_output,
)
@torch.inference_mode()
def execute_model(
self,
@@ -1414,7 +1424,9 @@ class GPUModelRunner(LoRAModelRunnerMixin):
if scheduler_output.total_num_scheduled_tokens == 0:
# No need to run the model.
empty_output = self.kv_connector.no_forward(scheduler_output)
return empty_output
return self._merge_ec_connector_no_forward(
scheduler_output, empty_output
)
# Get batch descriptor and sync across DP ranks.
num_reqs = len(scheduler_output.num_scheduled_tokens)
@@ -1455,7 +1467,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
if batch_desc.num_tokens == 0:
# All DP ranks have zero tokens to run.
empty_output = self.kv_connector.no_forward(scheduler_output)
return empty_output
return self._merge_ec_connector_no_forward(scheduler_output, empty_output)
if not dummy_run:
# Common case.
@@ -1572,9 +1584,10 @@ class GPUModelRunner(LoRAModelRunnerMixin):
input_ids = None
if self.is_encoder_only:
output = make_empty_encoder_model_runner_output(scheduler_output)
output.ec_connector_output = ec_connector_output
return output
return ModelRunnerOutput.with_ec_conn_output(
make_empty_encoder_model_runner_output(scheduler_output),
ec_connector_output,
)
model_inputs = {
"input_ids": input_ids,
@@ -1679,6 +1692,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
hidden_states=hidden_states,
aux_hidden_states=aux_hidden_states,
finished_req_ids=finished_req_ids,
ec_connector_output=ec_connector_output,
routed_experts=routed_experts,
)
@@ -1702,6 +1716,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
hidden_states = self.execute_model_state.hidden_states
aux_hidden_states = self.execute_model_state.aux_hidden_states
finished_req_ids = self.execute_model_state.finished_req_ids
ec_connector_output = self.execute_model_state.ec_connector_output
routed_experts = self.execute_model_state.routed_experts
self.execute_model_state = None
@@ -1721,7 +1736,9 @@ class GPUModelRunner(LoRAModelRunnerMixin):
# Post-step KV connector related operations.
kv_connector_output = self.kv_connector.post_forward(finished_req_ids)
return ModelRunnerOutput.with_kv_conn_output_only(kv_connector_output)
# The first PP rank holds the encoder cache, so pass its EC output on.
output = ModelRunnerOutput.with_kv_conn_output_only(kv_connector_output)
return ModelRunnerOutput.with_ec_conn_output(output, ec_connector_output)
# Last rank: sample tokens
hidden_states, input_batch = pcp.maybe_restore_pcp_for_sampling(
@@ -1838,6 +1855,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
# Post-step KV connector related operations.
kv_connector_output = self.kv_connector.post_forward(finished_req_ids)
model_runner_output.kv_connector_output = kv_connector_output
model_runner_output.ec_connector_output = ec_connector_output
return async_output
@@ -1854,6 +1872,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
input_batch = self.execute_model_state.input_batch
hidden_states = self.execute_model_state.hidden_states
finished_req_ids = self.execute_model_state.finished_req_ids
ec_connector_output = self.execute_model_state.ec_connector_output
self.execute_model_state = None
# Post-step KV connector related operations.
@@ -1861,7 +1880,8 @@ class GPUModelRunner(LoRAModelRunnerMixin):
if not self.is_last_pp_rank:
self.postprocess_num_computed_tokens(input_batch)
return ModelRunnerOutput.with_kv_conn_output_only(kv_connector_output)
output = ModelRunnerOutput.with_kv_conn_output_only(kv_connector_output)
return ModelRunnerOutput.with_ec_conn_output(output, ec_connector_output)
assert self.pooling_runner is not None
pooler_output, finished_mask = self.pooling_runner.pool(
@@ -1873,6 +1893,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
req_ids=input_batch.req_ids,
req_id_to_index={req_id: i for i, req_id in enumerate(input_batch.req_ids)},
kv_connector_output=kv_connector_output,
ec_connector_output=ec_connector_output,
)
async_output = AsyncPoolingOutput(
model_runner_output=model_runner_output,
@@ -1962,6 +1983,7 @@ class ExecuteModelState(NamedTuple):
hidden_states: torch.Tensor | None
aux_hidden_states: list[torch.Tensor] | None
finished_req_ids: set[str]
ec_connector_output: ECConnectorOutput | None
routed_experts: RoutedExpertsTensors | None