[Refactor][OutputProcessor 1/8]: clean up multimodal payload accumulation and move payload logic into MultimodalPayload & directory refactor (#4980)
Signed-off-by: Boao Shi <aoibosh@connect.hku.hk> Signed-off-by: bjf-frz <frz123db@gmail.com> Co-authored-by: bjf-frz <frz123db@gmail.com>
This commit is contained in:
1 parent
795e4e0160
commit
a432b8a325
13 files changed
+1080
-1018
No files matched your search
@@ -0,0 +1,24 @@
|
||||
"""Shared helpers for multimodal output handling."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
def _to_cpu(x: Any) -> Any:
|
||||
"""Move a tensor to CPU; pass through non-tensors unchanged."""
|
||||
if isinstance(x, torch.Tensor):
|
||||
try:
|
||||
return x.detach().to("cpu", non_blocking=True).contiguous()
|
||||
except (RuntimeError, AttributeError):
|
||||
return x
|
||||
if isinstance(x, dict):
|
||||
return {str(sub_key): _to_cpu(sub_value) for sub_key, sub_value in x.items()}
|
||||
return x
|
||||
|
||||
|
||||
def _is_tensor_list(value: Any) -> bool:
|
||||
"""Return True if *value* is a non-empty list of tensors (deferred concat)."""
|
||||
return isinstance(value, list) and bool(value) and isinstance(value[0], torch.Tensor)
|
||||
Reference in new issue
Block a user