2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-06-03 11:20:17 -07:00
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2023-07-03 11:31:55 -07:00
|
|
|
"""Sequence and its related classes."""
|
2025-10-05 15:06:22 +01:00
|
|
|
|
2025-09-21 08:52:15 -07:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
from typing import TYPE_CHECKING, Any
|
2023-02-09 11:26:35 +00:00
|
|
|
|
2024-06-03 13:56:41 +08:00
|
|
|
import torch
|
|
|
|
|
|
2025-08-03 14:03:40 +03:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from vllm.v1.worker.kv_connector_model_runner_mixin import KVConnectorOutput
|
2025-08-26 16:43:28 +01:00
|
|
|
else:
|
|
|
|
|
KVConnectorOutput = Any
|
2025-08-03 14:03:40 +03:00
|
|
|
|
2024-02-20 21:55:57 -08:00
|
|
|
|
2024-10-10 12:39:36 -07:00
|
|
|
# cannot use msgspec.Struct here because Dynamo does not support it
|
|
|
|
|
@dataclass
|
|
|
|
|
class IntermediateTensors:
|
2024-07-02 10:58:08 -07:00
|
|
|
"""For all pipeline stages except the last, we need to return the hidden
|
|
|
|
|
states and residuals to be sent to the next stage. This data structure
|
|
|
|
|
contains the hidden states and residuals for a request.
|
2025-10-05 15:06:22 +01:00
|
|
|
|
2025-08-03 14:03:40 +03:00
|
|
|
Each stage also needs to handle its own kv_connector_output.
|
2024-07-02 10:58:08 -07:00
|
|
|
"""
|
|
|
|
|
|
2025-03-03 01:34:51 +00:00
|
|
|
tensors: dict[str, torch.Tensor]
|
2025-08-26 16:43:28 +01:00
|
|
|
kv_connector_output: KVConnectorOutput | None
|
2024-07-02 10:58:08 -07:00
|
|
|
|
2025-11-15 06:31:36 +02:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
tensors: dict[str, torch.Tensor],
|
|
|
|
|
kv_connector_output: KVConnectorOutput | None = None,
|
|
|
|
|
) -> None:
|
2025-01-08 18:46:43 +08:00
|
|
|
# manually define this function, so that
|
|
|
|
|
# Dynamo knows `IntermediateTensors()` comes from this file.
|
|
|
|
|
# Otherwise, dataclass will generate this function by evaluating
|
|
|
|
|
# a string, and we will lose the information about the source file.
|
|
|
|
|
self.tensors = tensors
|
2025-11-15 06:31:36 +02:00
|
|
|
self.kv_connector_output = kv_connector_output
|
2025-01-08 18:46:43 +08:00
|
|
|
|
2024-07-02 10:58:08 -07:00
|
|
|
def __getitem__(self, key: str | slice):
|
|
|
|
|
if isinstance(key, str):
|
|
|
|
|
return self.tensors[key]
|
|
|
|
|
elif isinstance(key, slice):
|
|
|
|
|
return self.__class__({k: v[key] for k, v in self.tensors.items()})
|
|
|
|
|
|
2024-12-13 18:40:07 +08:00
|
|
|
def __setitem__(self, key: str, value: torch.Tensor):
|
2024-07-02 10:58:08 -07:00
|
|
|
self.tensors[key] = value
|
|
|
|
|
|
2025-02-17 13:37:45 -08:00
|
|
|
def items(self):
|
|
|
|
|
return self.tensors.items()
|
|
|
|
|
|
2024-07-02 10:58:08 -07:00
|
|
|
def __len__(self):
|
|
|
|
|
return len(self.tensors)
|
|
|
|
|
|
|
|
|
|
def __eq__(self, other: object):
|
2025-08-18 17:58:11 +08:00
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
|
return False
|
|
|
|
|
if self.tensors.keys() != other.tensors.keys():
|
|
|
|
|
return False
|
|
|
|
|
return all(torch.equal(self.tensors[k], other.tensors[k]) for k in self.tensors)
|
2024-07-02 10:58:08 -07:00
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"IntermediateTensors(tensors={self.tensors})"
|