+9








7bc1dae095
Co-authored-by: yhyang201 <yhyang201@gmail.com> Co-authored-by: yizhang2077 <1109276519@qq.com> Co-authored-by: Xinyuan Tong <xinyuantong.cs@gmail.com> Co-authored-by: ispobock <ispobaoke@gmail.com> Co-authored-by: JiLi <leege233@gmail.com> Co-authored-by: CHEN Xi <78632976+RubiaCx@users.noreply.github.com> Co-authored-by: laixin <xielx@shanghaitech.edu.cn> Co-authored-by: SolitaryThinker <wlsaidhi@gmail.com> Co-authored-by: jzhang38 <a1286225768@gmail.com> Co-authored-by: BrianChen1129 <yongqichcd@gmail.com> Co-authored-by: Kevin Lin <42618777+kevin314@users.noreply.github.com> Co-authored-by: Edenzzzz <wtan45@wisc.edu> Co-authored-by: rlsu9 <r3su@ucsd.edu> Co-authored-by: Jinzhe Pan <48981407+eigensystem@users.noreply.github.com> Co-authored-by: foreverpiano <pianoqwz@qq.com> Co-authored-by: RandNMR73 <notomatthew31@gmail.com> Co-authored-by: PorridgeSwim <yz3883@columbia.edu> Co-authored-by: Jiali Chen <90408393+gary-chenjl@users.noreply.github.com>
62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
# Copied and adapted from: https://github.com/hao-ai-lab/FastVideo
|
|
|
|
import argparse
|
|
from typing import Any
|
|
|
|
|
|
def update_config_from_args(
|
|
config: Any, args_dict: dict[str, Any], prefix: str = "", pop_args: bool = False
|
|
) -> bool:
|
|
"""
|
|
Update configuration object from arguments dictionary.
|
|
|
|
Args:
|
|
config: The configuration object to update
|
|
args_dict: Dictionary containing arguments
|
|
prefix: Prefix for the configuration parameters in the args_dict.
|
|
If None, assumes direct attribute mapping without prefix.
|
|
"""
|
|
# Handle top-level attributes (no prefix)
|
|
args_not_to_remove = [
|
|
"model_path",
|
|
]
|
|
args_to_remove = []
|
|
if prefix.strip() == "":
|
|
for key, value in args_dict.items():
|
|
if hasattr(config, key) and value is not None:
|
|
if key == "text_encoder_precisions" and isinstance(value, list):
|
|
setattr(config, key, tuple(value))
|
|
else:
|
|
setattr(config, key, value)
|
|
if pop_args:
|
|
args_to_remove.append(key)
|
|
else:
|
|
# Handle nested attributes with prefix
|
|
prefix_with_dot = f"{prefix}."
|
|
for key, value in args_dict.items():
|
|
if key.startswith(prefix_with_dot) and value is not None:
|
|
attr_name = key[len(prefix_with_dot) :]
|
|
if hasattr(config, attr_name):
|
|
setattr(config, attr_name, value)
|
|
if pop_args:
|
|
args_to_remove.append(key)
|
|
|
|
if pop_args:
|
|
for key in args_to_remove:
|
|
if key not in args_not_to_remove:
|
|
args_dict.pop(key)
|
|
|
|
return len(args_to_remove) > 0
|
|
|
|
|
|
def clean_cli_args(args: argparse.Namespace) -> dict[str, Any]:
|
|
"""
|
|
Clean the arguments by removing the ones that not explicitly provided by the user.
|
|
"""
|
|
provided_args = {}
|
|
for k, v in vars(args).items():
|
|
if v is not None and hasattr(args, "_provided") and k in args._provided:
|
|
provided_args[k] = v
|
|
|
|
return provided_args
|