[diffusion] disagg: handle numpy arrays in cross-role transfer field extraction (#31325)
Co-authored-by: Bingxu Chen <bingxche@amd.com>
This commit is contained in:
co-authored by
Bingxu Chen
parent
9668d9ea72
commit
50c118704a
@@ -19,6 +19,7 @@ import threading
|
|||||||
import time
|
import time
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
import zmq
|
import zmq
|
||||||
|
|
||||||
@@ -137,13 +138,17 @@ def _is_tensor_like(value) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def _to_json_serializable(value):
|
def _to_json_serializable(value):
|
||||||
if isinstance(value, torch.Tensor):
|
if isinstance(value, (torch.Tensor, np.ndarray)):
|
||||||
return value.tolist()
|
return value.tolist()
|
||||||
|
if isinstance(value, np.generic):
|
||||||
|
return value.item()
|
||||||
if isinstance(value, (list, tuple)):
|
if isinstance(value, (list, tuple)):
|
||||||
converted = []
|
converted = []
|
||||||
for item in value:
|
for item in value:
|
||||||
if isinstance(item, torch.Tensor):
|
if isinstance(item, (torch.Tensor, np.ndarray)):
|
||||||
converted.append(item.tolist())
|
converted.append(item.tolist())
|
||||||
|
elif isinstance(item, np.generic):
|
||||||
|
converted.append(item.item())
|
||||||
else:
|
else:
|
||||||
converted.append(item)
|
converted.append(item)
|
||||||
return converted
|
return converted
|
||||||
@@ -152,7 +157,17 @@ def _to_json_serializable(value):
|
|||||||
|
|
||||||
def _is_default(value, field_info) -> bool:
|
def _is_default(value, field_info) -> bool:
|
||||||
if field_info.default is not dataclasses.MISSING:
|
if field_info.default is not dataclasses.MISSING:
|
||||||
return value == field_info.default
|
# ``value == default`` may be element-wise for array/tensor-valued
|
||||||
|
# fields (numpy ndarray, torch.Tensor) or even raise for list-of-array
|
||||||
|
# values. Only treat the field as default when the comparison reduces
|
||||||
|
# to a real boolean; otherwise it is not equal to a scalar default.
|
||||||
|
try:
|
||||||
|
eq = value == field_info.default
|
||||||
|
if isinstance(eq, (bool, np.bool_)):
|
||||||
|
return bool(eq)
|
||||||
|
except (ValueError, RuntimeError):
|
||||||
|
pass
|
||||||
|
return False
|
||||||
if field_info.default_factory is not dataclasses.MISSING:
|
if field_info.default_factory is not dataclasses.MISSING:
|
||||||
if isinstance(value, (list, dict)) and len(value) == 0:
|
if isinstance(value, (list, dict)) and len(value) == 0:
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user