[Feature] add support for Qwen3-omni (#55)
Signed-off-by: Rein Yang <ruiruyang2@gmail.com> Co-authored-by: Rein <rein@Reins-MacBook-Air.local>
This commit is contained in:
1 parent
a5800cd2c2
commit
0a4e5a8710
47 files changed
+8064
-478
No files matched your search
@@ -1,3 +1,4 @@
|
||||
from ast import Dict
|
||||
from typing import Any, Callable, Optional, Union
|
||||
|
||||
import torch
|
||||
@@ -33,7 +34,7 @@ class OmniRequestState(RequestState):
|
||||
):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.mm_type: Optional[str] = None
|
||||
self.mm_accumulated: Optional[torch.Tensor] = None
|
||||
self.mm_accumulated: Optional[Dict[str, Any]] = None
|
||||
|
||||
@classmethod
|
||||
def from_new_request(
|
||||
@@ -92,33 +93,71 @@ class OmniRequestState(RequestState):
|
||||
log_stats=log_stats,
|
||||
)
|
||||
|
||||
def add_multimodal_tensor(self, tensor: Optional[torch.Tensor], mm_type: Optional[str]) -> None:
|
||||
"""Add a multimodal tensor to the accumulated outputs.
|
||||
|
||||
Accumulates multimodal tensors (e.g., image latents, audio waveforms)
|
||||
that are produced incrementally during generation. Tensors are
|
||||
concatenated along the first dimension.
|
||||
|
||||
Args:
|
||||
tensor: Optional tensor to add. If None, this is a no-op.
|
||||
mm_type: Optional modality type identifier (e.g., "image", "audio").
|
||||
Used to categorize the output type.
|
||||
"""
|
||||
if tensor is None:
|
||||
def add_multimodal_tensor(self, payload: Optional[Any], mm_type: Optional[str]) -> None:
|
||||
if payload is None:
|
||||
return
|
||||
try:
|
||||
if mm_type:
|
||||
self.mm_type = (mm_type or "").lower()
|
||||
t = tensor.detach()
|
||||
try:
|
||||
t = t.to("cpu")
|
||||
except Exception:
|
||||
# Best-effort CPU move; keep original device if conversion fails
|
||||
logger.debug("Failed to move multimodal tensor to CPU", exc_info=True)
|
||||
if self.mm_accumulated is None:
|
||||
self.mm_accumulated = t
|
||||
|
||||
# Normalize incoming payload to dict on CPU
|
||||
def _to_cpu(x):
|
||||
if isinstance(x, torch.Tensor):
|
||||
try:
|
||||
return x.detach().to("cpu").contiguous()
|
||||
except Exception:
|
||||
return x
|
||||
return x
|
||||
|
||||
if isinstance(payload, dict):
|
||||
incoming: Dict[str, Any] = {}
|
||||
# Optional remap: if producer used "model_outputs" or "hidden", rename to mm_type
|
||||
# to keep a consistent key namespace per engine_core_output_type.
|
||||
remapped_tensor = dict(payload)
|
||||
target_key = self.mm_type or "hidden"
|
||||
if "model_outputs" in remapped_tensor:
|
||||
remapped_tensor[target_key] = remapped_tensor.pop("model_outputs")
|
||||
elif "hidden" in remapped_tensor and target_key != "hidden":
|
||||
remapped_tensor[target_key] = remapped_tensor.pop("hidden")
|
||||
for k, v in remapped_tensor.items():
|
||||
if isinstance(v, dict):
|
||||
incoming[k] = {str(sk): _to_cpu(sv) for sk, sv in v.items()}
|
||||
else:
|
||||
incoming[k] = _to_cpu(v)
|
||||
else:
|
||||
self.mm_accumulated = torch.cat([self.mm_accumulated, t], dim=0)
|
||||
key = self.mm_type or "hidden"
|
||||
incoming = {key: _to_cpu(payload)}
|
||||
|
||||
if self.mm_accumulated is None:
|
||||
self.mm_accumulated = incoming
|
||||
else:
|
||||
# Merge keys; concatenate tensors along token dim when possible
|
||||
for k, v in incoming.items():
|
||||
if k not in self.mm_accumulated:
|
||||
self.mm_accumulated[k] = v
|
||||
else:
|
||||
existing = self.mm_accumulated[k]
|
||||
if isinstance(v, torch.Tensor) and isinstance(existing, torch.Tensor):
|
||||
try:
|
||||
self.mm_accumulated[k] = torch.cat([existing, v], dim=0) # type: ignore[index]
|
||||
except Exception:
|
||||
self.mm_accumulated[k] = v
|
||||
elif isinstance(v, dict) and isinstance(existing, dict):
|
||||
# Merge nested dicts by concatenating tensors along token dim when possible
|
||||
for sk, sv in v.items():
|
||||
if sk not in existing:
|
||||
existing[sk] = sv
|
||||
continue
|
||||
ev = existing[sk]
|
||||
if isinstance(sv, torch.Tensor) and isinstance(ev, torch.Tensor):
|
||||
try:
|
||||
existing[sk] = torch.cat([ev, sv], dim=0)
|
||||
except Exception:
|
||||
existing[sk] = sv
|
||||
else:
|
||||
existing[sk] = sv
|
||||
else:
|
||||
self.mm_accumulated[k] = v
|
||||
except Exception:
|
||||
# Log and continue without crashing the output pipeline
|
||||
logger.exception("Error accumulating multimodal tensor")
|
||||
@@ -178,18 +217,15 @@ class OmniRequestState(RequestState):
|
||||
base_output = super()._new_completion_output(token_ids, finish_reason, stop_reason)
|
||||
try:
|
||||
if self.mm_accumulated is not None:
|
||||
tensor = self.mm_accumulated
|
||||
try:
|
||||
tensor = tensor.detach().to("cpu")
|
||||
except Exception:
|
||||
logger.debug(
|
||||
"Failed to move accumulated multimodal tensor to CPU",
|
||||
exc_info=True,
|
||||
)
|
||||
# Attach on the completion output for downstream consumers.
|
||||
# Attach accumulated multimodal dict on the completion output
|
||||
if not hasattr(base_output, "multimodal_output"):
|
||||
setattr(base_output, "multimodal_output", {})
|
||||
setattr(base_output, "multimodal_output", {self.mm_type: tensor})
|
||||
mm_out = getattr(base_output, "multimodal_output")
|
||||
if isinstance(mm_out, dict):
|
||||
for k, v in self.mm_accumulated.items():
|
||||
mm_out[k] = v
|
||||
else:
|
||||
setattr(base_output, "multimodal_output", self.mm_accumulated)
|
||||
except Exception:
|
||||
logger.exception("Error in _new_completion_output")
|
||||
return base_output
|
||||
@@ -369,10 +405,9 @@ class MultimodalOutputProcessor(VLLMOutputProcessor):
|
||||
# Attach accumulated multimodal payload if any
|
||||
try:
|
||||
if isinstance(req_state, OmniRequestState) and req_state.mm_accumulated is not None:
|
||||
mm_key = req_state.mm_type or "latents"
|
||||
if not hasattr(ro, "multimodal_output"):
|
||||
setattr(ro, "multimodal_output", {})
|
||||
ro.multimodal_output[mm_key] = req_state.mm_accumulated
|
||||
ro.multimodal_output = req_state.mm_accumulated
|
||||
except Exception:
|
||||
logger.exception("Error attaching multimodal payload in process_outputs")
|
||||
if req_state.queue is not None:
|
||||
|
||||
Reference in new issue
Block a user