Spaces:
Build error
Build error
File size: 15,675 Bytes
9f818c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: OpenMDW-1.1
import abc
import functools
import importlib
import json
import os
import tomllib
from collections.abc import Callable as Callable2
from collections.abc import Mapping, Sequence
from dataclasses import fields, is_dataclass
from types import UnionType
from typing import Any, List, Literal, Optional, TypeVar, Union, get_args, get_origin
import attrs
import torch
import yaml
from omegaconf import DictConfig, ListConfig, OmegaConf
from cosmos_framework.utils.lazy_config import LazyCall, LazyDict, instantiate
from cosmos_framework.utils.lazy_config.lazy import get_default_params
T = TypeVar("T")
def from_dict(
x: dict, clazz: str | type | None = None, force_construct_target: bool | None = None, field_name: str = ""
) -> T: ...
def to_dict(x: T, field_name: str = "", hydra_compat: bool = True) -> dict: ...
def from_yaml(path: str | None = None, clazz: type | None = None, file_like_or_str=None) -> T:
if path:
assert os.path.exists(path), f"{path} does not exist"
with open(path) as in_f:
return from_dict(yaml.safe_load(in_f), clazz=clazz)
elif file_like_or_str:
return from_dict(yaml.safe_load(file_like_or_str), clazz=clazz)
else:
raise ValueError("expected file_like_or_str or path to not be None")
def from_toml(path: str | None = None, clazz: type | None = None, file_like_or_str=None) -> T:
if path:
assert os.path.exists(path), f"{path} does not exist"
with open(path, "rb") as in_f:
return from_dict(tomllib.load(in_f), clazz=clazz)
elif file_like_or_str:
if isinstance(file_like_or_str, (bytes, bytearray)):
return from_dict(tomllib.loads(file_like_or_str.decode("utf-8")), clazz=clazz)
return from_dict(tomllib.loads(file_like_or_str), clazz=clazz)
else:
raise ValueError("expected file_like_or_str or path to not be None")
def _yaml_safe(obj: Any) -> Any:
# primitives
if obj is None or isinstance(obj, (bool, int, float, str)):
return obj
# dict-like
if isinstance(obj, Mapping):
return {str(k): _yaml_safe(v) for k, v in obj.items()}
# list/tuple-like (but not strings/bytes)
if isinstance(obj, Sequence) and not isinstance(obj, (str, bytes, bytearray)):
return [_yaml_safe(v) for v in obj]
# classes / functions / bound methods -> import path
if hasattr(obj, "__module__") and hasattr(obj, "__qualname__"):
return f"{obj.__module__}.{obj.__qualname__}"
# torch dtype, Path, enums, dataclasses, etc.
return str(obj)
def to_yaml(config: T, out_path: str | None = None) -> str | None:
config_dict = to_dict(config)
safe_dict = _yaml_safe(config_dict)
if out_path is not None:
with open(out_path, "w") as f:
yaml.safe_dump(
safe_dict,
f,
sort_keys=False,
default_flow_style=False,
allow_unicode=True,
)
return None
return yaml.safe_dump(
safe_dict,
sort_keys=False,
default_flow_style=False,
allow_unicode=True,
)
def load_callable(name: str) -> Callable2 | None:
if not name:
return None
idx = name.rfind(".")
assert idx != -1, "expected <module_name>.<name>"
module_name = name[0:idx]
fn_name = name[idx + 1 :]
mod = importlib.import_module(module_name)
return getattr(mod, fn_name)
def maybe_load_callable(name: str | Callable2 | None) -> Callable2 | None:
if isinstance(name, str):
return load_callable(name)
return name
def maybe_idx(x: Any, idx: int) -> Any:
if idx < 0 or idx >= len(x):
return None
return x[idx]
def is_attrs(x: Any) -> bool:
return hasattr(x, "__attrs_attrs__")
def to_qualitified_name(x) -> str:
# Handle functools.partial explicitly
if isinstance(x, functools.partial):
fn = x.func
fn_name = to_qualitified_name(fn)
# args/keywords may contain non-serializable stuff; stringify safely
args = []
if x.args:
args = [repr(a) for a in x.args]
kwargs = {}
if x.keywords:
kwargs = {str(k): repr(v) for k, v in x.keywords.items()}
if args or kwargs:
return f"functools.partial({fn_name}, args={args}, kwargs={kwargs})"
return f"functools.partial({fn_name})"
# Normal callable/class/module qualified name
mod = getattr(x, "__module__", None)
qn = getattr(x, "__qualname__", None)
if mod and qn:
return f"{mod}.{qn}"
# Some callables only have __name__
name = getattr(x, "__name__", None)
if mod and name:
return f"{mod}.{name}"
# Fallback: repr
return repr(x)
def is_optional(x: type) -> bool:
origin = get_origin(x)
args = get_args(x)
return origin is Optional or (origin in (Union, UnionType) and len(args) == 2 and type(None) in args)
def _to_dict_value(x: T, field_type: type, metadata: dict, field_name: str = ""):
t = type(x)
# attrs specific
if x is attrs.NOTHING or x is None:
return None
# torch specifics
elif field_type in (torch.memory_format, torch.dtype):
return str(x)
# i4 specific types
elif field_type == LazyCall:
result = _to_dict_value(x, field_type._target, metadata, field_name)
return result
elif field_type in (DictConfig, LazyDict):
if "_target_" in x:
default_params = get_default_params(x["_target_"])
for default_key, default_v in default_params.items():
if default_key not in x:
x[default_key] = default_v
result = _to_dict_value(x, dict, metadata, field_name)
object_type = getattr(x._metadata, "object_type", None)
if object_type and (is_dataclass(object_type) or is_attrs(object_type)):
result.setdefault("_target_", to_qualitified_name(object_type))
return result
elif field_type == ListConfig:
return _to_dict_value(x, list, metadata, field_name)
# general python types + dataclasses + attrs
# * meta types
elif field_type == type or field_type == abc.ABCMeta:
return to_qualitified_name(x)
elif get_origin(field_type) is type:
return to_qualitified_name(x)
elif callable(x) or get_origin(field_type) is Callable2:
if callable(x):
return to_qualitified_name(x)
else:
assert isinstance(x, str), f"{x.__class__=}"
return x
elif is_dataclass(t) or is_attrs(t):
return to_dict(x, field_name=field_name)
# * built-in composites types
elif is_optional(field_type):
return _to_dict_value(x, get_args(field_type)[0], metadata)
elif get_origin(field_type) in (Union, UnionType):
raise AssertionError("unions are not implemented yet!")
# * primitives
elif t in (dict,) or field_type in (dict,) or get_origin(field_type) in (dict,):
return {
_to_dict_value(
k,
maybe_idx(get_args(field_type), 0) or type(k),
metadata,
field_name=f"{field_name}.{k}.key",
): _to_dict_value(
v,
maybe_idx(get_args(field_type), 1) or type(v),
metadata,
field_name=f"{field_name}.{k}",
)
for k, v in x.items()
}
elif (
t
in (
tuple,
list,
)
or field_type
in (
tuple,
list,
)
or get_origin(field_type) in (tuple, list)
):
if field_type is None or field_type not in (
tuple,
list,
):
field_type = list
return field_type(
[
_to_dict_value(xx, maybe_idx(get_args(field_type), 0) or type(xx), metadata, field_name + f"[{i}]")
for i, xx in enumerate(x)
]
)
elif field_type in (int, str, float, bool):
result = field_type(x)
return result
else: # catch all for everything else
return x
def to_dict(x: T, field_name: str = "", hydra_compat: bool = True) -> dict:
if is_dataclass(x):
result = {}
if hydra_compat:
result["_target_"] = to_qualitified_name(x.__class__)
for f in fields(x):
if hydra_compat and f.name == "defaults":
continue
result[f.name] = _to_dict_value(
x.__dict__[f.name],
f.type,
f.metadata,
field_name=field_name + f".{f.name}" if field_name else f.name,
)
return result
elif is_attrs(x):
# references:
# - https://github.com/python-attrs/attrs/blob/main/src/attr/_funcs.py
attrs.resolve_types(x.__class__)
result = {}
if hydra_compat:
result["_target_"] = to_qualitified_name(x.__class__)
for f in attrs.fields(x.__class__):
if hydra_compat and f.name == "defaults":
continue
result[f.name] = _to_dict_value(
getattr(x, f.name),
f.type,
f.metadata,
field_name=field_name + f".{f.name}" if field_name else f.name,
)
return result
def _from_dict_value(
x: T,
field_type: type,
concrete_type: type,
field_name: str,
force_construct_target: bool | None = None,
):
is_dc_type = is_dataclass(field_type)
is_attrs_type = is_attrs(field_type)
origin = get_origin(field_type) or field_type
args = get_args(field_type)
if x is None:
return None
elif field_type in (torch.memory_format, torch.dtype):
return maybe_load_callable(x)
elif field_type == LazyCall:
return _from_dict_value(x, field_type._target, concrete_type, field_name=field_name)
elif is_dc_type or is_attrs_type:
if concrete_type == str:
assert isinstance(x, str)
if x.endswith(".json"):
json_value = json.loads(x)
return from_dict(
json_value, field_type, force_construct_target=force_construct_target, field_name=field_name
)
elif x.endswith(".yaml"):
yaml_value = yaml.safe_load(x)
return from_dict(
yaml_value, field_type, force_construct_target=force_construct_target, field_name=field_name
)
else:
raise AssertionError(f"unexpected string: {x}")
else:
assert not isinstance(x, str)
return from_dict(x, field_type, field_name=field_name)
elif field_type in (DictConfig, LazyDict) or origin in (dict,):
construct_target = x.get("_recursive_", field_type == DictConfig)
if force_construct_target is not None:
construct_target = force_construct_target
target_value = x.get("_target_")
target_cls = maybe_load_callable(target_value)
if target_value and construct_target and (is_dataclass(target_cls) or is_attrs(target_cls)):
result = from_dict(x, target_cls, force_construct_target=force_construct_target, field_name=field_name)
else:
result = {
_from_dict_value(
k,
maybe_idx(get_args(field_type), 0) or type(k),
type(k),
field_name=f"{field_name}.{k}.key",
force_construct_target=construct_target,
): _from_dict_value(
v,
maybe_idx(get_args(field_type), 1) or type(v),
type(v),
field_name=f"{field_name}.{k}",
force_construct_target=construct_target,
)
for k, v in x.items()
}
if field_type in (DictConfig, LazyDict):
result = OmegaConf.structured(result, flags={"allow_objects": True})
if construct_target:
result = instantiate(result)
if "_target_" in result:
result["_target_"] = maybe_load_callable(result["_target_"])
elif construct_target and target_cls: # instantiate a regular class from a dict
special_keys = {
"_target_",
"_recursive_",
"_convert_",
"_args_",
"_kwargs_",
}
constructable_items = {
k: v for k, v in result.items() if not (isinstance(k, str) and k in special_keys)
}
result = target_cls(**constructable_items)
return result
elif field_type is ListConfig or origin in (
list,
List,
):
return [
_from_dict_value(
xx, maybe_idx(get_args(field_type), 0) or type(xx), type(xx), field_name=f"{field_type}[{i}]"
)
for i, xx in enumerate(x)
]
elif is_optional(field_type):
return _from_dict_value(x, args[0], type(x), field_name=field_name)
elif origin in (Union, UnionType):
raise AssertionError("unions are not implemented yet!")
elif origin is Callable2 or origin is type:
return maybe_load_callable(x)
elif field_type in (int, float, str, bool):
return x
elif field_type is type(None) or field_type == Any: # no typing
return x
elif origin is Literal:
allowed = get_args(field_type)
if x not in allowed:
raise TypeError(
f"value {x!r} not in {field_type} (allowed={allowed}, field={field_name})"
)
return x
else:
raise TypeError(
f"unexpected type: {field_type} (origin={origin}, concrete_type={concrete_type}, args={args}, x={x})"
)
def from_dict(
x: dict, clazz: type | None = None, force_construct_target: bool | None = None, field_name: str = ""
) -> T:
if clazz is None:
assert "_target_" in x
clazz = maybe_load_callable(x["_target_"])
assert is_dataclass(clazz) or is_attrs(clazz), f"{clazz} is not a dataclass or attrs"
if is_dataclass(clazz):
construct_args = {}
for f in fields(clazz):
if f.name in x:
construct_args[f.name] = _from_dict_value(
x[f.name],
f.type,
type(x[f.name]),
field_name=field_name + "." + f.name if field_name else f.name,
force_construct_target=force_construct_target,
)
elif is_optional(f.type):
construct_args[f.name] = None
return clazz(**construct_args)
elif is_attrs(clazz):
attrs.resolve_types(clazz)
construct_args = {}
for f in attrs.fields(clazz):
if f.name in x:
construct_args[f.name] = _from_dict_value(
x[f.name],
f.type,
type(x[f.name]),
field_name=field_name + "." + f.name if field_name else f.name,
force_construct_target=force_construct_target,
)
elif is_optional(f.type):
construct_args[f.name] = None
return clazz(**construct_args)
|