|
32 | 32 |
|
33 | 33 | import enum
|
34 | 34 | from abc import ABC, abstractmethod
|
35 |
| -from typing import TYPE_CHECKING, Any, Optional |
| 35 | +from typing import TYPE_CHECKING, Any, Callable, Optional |
36 | 36 |
|
37 | 37 | import torch
|
| 38 | +import msgspec |
| 39 | +from pydantic_core import core_schema |
38 | 40 |
|
39 | 41 | from vllm.logger import init_logger
|
40 | 42 | from vllm.v1.core.sched.output import SchedulerOutput
|
@@ -62,18 +64,58 @@ class KVConnectorMetadata:
|
62 | 64 | Abstract Metadata used to communicate between the
|
63 | 65 | Scheduler KVConnector and Worker KVConnector.
|
64 | 66 | """
|
65 |
| - pass |
| 67 | + |
| 68 | + def __init__(self): |
| 69 | + pass |
| 70 | + |
66 | 71 |
|
| 72 | +class KVConnectorHandshakeMetadata( |
| 73 | + msgspec.Struct, |
| 74 | + omit_defaults=True, # type: ignore[call-arg] |
| 75 | + # required for @cached_property. |
| 76 | + dict=True): |
| 77 | + """ |
| 78 | + Metadata optionally used for out of band connector handshake between P/D workers. |
| 79 | + """ |
| 80 | + connector_type: str = "base" |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def __get_pydantic_core_schema__( |
| 84 | + cls, |
| 85 | + _source_type: Any, |
| 86 | + _handler: Callable[[Any], core_schema.CoreSchema] |
| 87 | + ) -> core_schema.CoreSchema: |
| 88 | + """bridge msgspec.Struct with pydantic for schema generation""" |
| 89 | + return core_schema.no_info_after_validator_function( |
| 90 | + cls, |
| 91 | + core_schema.dict_schema() |
| 92 | + ) |
| 93 | + |
| 94 | +class KVConnectorTransferMetadata( |
| 95 | + msgspec.Struct, |
| 96 | + omit_defaults=True, # type: ignore[call-arg] |
| 97 | + dict=True): |
| 98 | + """ |
| 99 | + Wrapper for transfer handshake metadata sent between engine and utils. |
| 100 | + """ |
| 101 | + tensor_parallel_rank: int |
| 102 | + data_parallel_rank: int |
| 103 | + content: Optional[dict] |
| 104 | + |
67 | 105 |
|
68 | 106 | class KVConnectorBase_V1(ABC):
|
69 | 107 |
|
70 |
| - def __init__(self, vllm_config: "VllmConfig", role: KVConnectorRole): |
| 108 | + def __init__(self, |
| 109 | + vllm_config: "VllmConfig", |
| 110 | + role: KVConnectorRole): |
71 | 111 | logger.warning(
|
72 | 112 | "Initializing KVConnectorBase_V1. This API is experimental and "
|
73 | 113 | "subject to change in the future as we iterate the design.")
|
74 | 114 | self._connector_metadata = KVConnectorMetadata()
|
75 | 115 | self._vllm_config = vllm_config
|
76 | 116 | self._role = role
|
| 117 | + self._handshake_metadata: Optional[KVConnectorHandshakeMetadata] = None |
| 118 | + |
77 | 119 |
|
78 | 120 | @property
|
79 | 121 | def role(self) -> KVConnectorRole:
|
@@ -104,7 +146,7 @@ def clear_connector_metadata(self) -> None:
|
104 | 146 | """
|
105 | 147 | self._connector_metadata = KVConnectorMetadata()
|
106 | 148 |
|
107 |
| - def _get_connector_metadata(self) -> KVConnectorMetadata: |
| 149 | + def get_connector_metadata(self) -> KVConnectorMetadata: |
108 | 150 | """Get the connector metadata.
|
109 | 151 |
|
110 | 152 | This function should only be called inside the connector.
|
@@ -201,6 +243,31 @@ def get_finished(
|
201 | 243 | """
|
202 | 244 | return None, None
|
203 | 245 |
|
| 246 | + def set_handshake_metadata( |
| 247 | + self, handshake_metadata: KVConnectorHandshakeMetadata) -> None: |
| 248 | + """ |
| 249 | + Set the handshake metadata for the connector. |
| 250 | +
|
| 251 | + This metadata is used for out-of-band connector handshake |
| 252 | + between P/D workers. |
| 253 | + |
| 254 | + Args: |
| 255 | + handshake_metadata (KVConnectorHandshakeMetadata): the handshake |
| 256 | + metadata. |
| 257 | + """ |
| 258 | + self._handshake_metadata = handshake_metadata |
| 259 | + |
| 260 | + |
| 261 | + def get_handshake_metadata( |
| 262 | + self) -> Optional[KVConnectorHandshakeMetadata]: |
| 263 | + """ |
| 264 | + Get the handshake metadata for the connector. |
| 265 | +
|
| 266 | + Returns: |
| 267 | + KVConnectorHandshakeMetadata: the handshake metadata. |
| 268 | + """ |
| 269 | + return self._handshake_metadata |
| 270 | + |
204 | 271 | # ==============================
|
205 | 272 | # Scheduler-side methods
|
206 | 273 | # ==============================
|
|
0 commit comments