Signed-off-by: Megha Agarwal <agarwalmegha1308@gmail.com> Signed-off-by: meghaagr13 <divyanshsinghvi@gmail.com> Signed-off-by: Taichang Zhou <tzhouam@connect.ust.hk> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com> Co-authored-by: meghaagr13 <divyanshsinghvi@gmail.com> Co-authored-by: Boao Shi <aoibosh@connect.hku.hk> Co-authored-by: Taichang Zhou <tzhouam@connect.ust.hk> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
49 files changed
+985
-327
No files matched your search
@@ -6,6 +6,7 @@ This module defines structured types for multimodal outputs.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator, Mapping
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
@@ -13,10 +14,15 @@ import torch
|
||||
from vllm.outputs import CompletionOutput
|
||||
|
||||
|
||||
@dataclass
|
||||
class MultimodalPayload:
|
||||
@dataclass(eq=False)
|
||||
class MultimodalPayload(Mapping):
|
||||
"""Structured multimodal output payload.
|
||||
|
||||
Implements ``collections.abc.Mapping`` so that ``isinstance(payload, dict)``
|
||||
style checks in downstream code can be replaced with duck-typing, and
|
||||
``payload.get(key)``, ``payload[key]``, ``key in payload``, ``len(payload)``
|
||||
all work seamlessly for both tensors and metadata.
|
||||
|
||||
Attributes:
|
||||
tensors: Dictionary mapping modality/key names to their tensors.
|
||||
metadata: Optional dictionary for non-tensor metadata
|
||||
@@ -35,18 +41,49 @@ class MultimodalPayload:
|
||||
|
||||
@property
|
||||
def is_empty(self) -> bool:
|
||||
"""Return True if the payload has no tensors."""
|
||||
return len(self.tensors) == 0
|
||||
"""Return True if the payload has no tensors and no metadata."""
|
||||
return len(self.tensors) == 0 and len(self.metadata) == 0
|
||||
|
||||
def get(self, key: str) -> torch.Tensor | None:
|
||||
"""Get a tensor by key, returning None if not found."""
|
||||
return self.tensors.get(key)
|
||||
def get(self, key: str, default: Any = None) -> Any:
|
||||
"""Get a value by key, searching tensors first then metadata."""
|
||||
if key in self.tensors:
|
||||
return self.tensors[key]
|
||||
return self.metadata.get(key, default)
|
||||
|
||||
def __contains__(self, key: str) -> bool:
|
||||
return key in self.tensors
|
||||
return key in self.tensors or key in self.metadata
|
||||
|
||||
def __getitem__(self, key: str) -> Any:
|
||||
"""Dict-like indexing: search tensors first, then metadata."""
|
||||
if key in self.tensors:
|
||||
return self.tensors[key]
|
||||
if key in self.metadata:
|
||||
return self.metadata[key]
|
||||
raise KeyError(key)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.tensors)
|
||||
return len(self.tensors) + len(self.metadata)
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
yield from self.tensors
|
||||
yield from self.metadata
|
||||
|
||||
def __bool__(self) -> bool:
|
||||
return bool(self.tensors) or bool(self.metadata)
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
"""Support equality with plain dicts and other Mappings."""
|
||||
if isinstance(other, MultimodalPayload):
|
||||
return self.tensors == other.tensors and self.metadata == other.metadata
|
||||
if isinstance(other, Mapping):
|
||||
return dict(self) == dict(other)
|
||||
return NotImplemented
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
"""Convert back to a plain dict (tensors + metadata merged)."""
|
||||
result: dict[str, Any] = dict(self.tensors)
|
||||
result.update(self.metadata)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict[str, Any] | None) -> MultimodalPayload | None:
|
||||
|
||||
Reference in new issue
Block a user