| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import dataclasses |
| import json |
| import os |
|
|
| import torch |
|
|
|
|
| @dataclasses.dataclass |
| class ModelConfig: |
| model_name: str |
|
|
| |
| num_layers: int = None |
| hidden_size: int = None |
| ffn_hidden_size: int = None |
| num_attention_heads: int = None |
| num_query_groups: int = 1 |
| kv_channels: int = None |
| layernorm_epsilon: float = 1e-6 |
| apply_layernorm_1p: bool = False |
| x_rescale_factor: float = 1.0 |
| half_channel_vae: bool = False |
| params_dtype: torch.dtype = None |
|
|
| |
| patch_size: int = 2 |
| t_patch_size: int = 1 |
| in_channels: int = 4 |
| out_channels: int = 4 |
| cond_hidden_ratio: float = 0.25 |
| caption_channels: int = 4096 |
| caption_max_length: int = 800 |
| xattn_cond_hidden_ratio: float = 1.0 |
| cond_gating_ratio: float = 1.0 |
| gated_linear_unit: bool = False |
|
|
|
|
| @dataclasses.dataclass |
| class RuntimeConfig: |
| |
| cfg_number: int = None |
| cfg_t_range: list = dataclasses.field( |
| default_factory=lambda: [0, 0.0217, 0.1000, 0.3, 0.999] |
| ) |
| prev_chunk_scales: list = dataclasses.field( |
| default_factory=lambda: [1.5, 1.5, 1.5, 1.5, 1.5] |
| ) |
| text_scales: list = dataclasses.field(default_factory=lambda: [7.5, 7.5, 7.5, 7.5, 7.5]) |
|
|
| noise2clean_kvrange: list = dataclasses.field(default_factory=list) |
| clean_chunk_kvrange: int = -1 |
| clean_t: float = 1.0 |
|
|
| |
| seed: int = 1234 |
| num_frames: int = 128 |
| video_size_h: int = None |
| video_size_w: int = None |
| num_steps: int = 64 |
| window_size: int = 4 |
| fps: int = 24 |
| chunk_width: int = 6 |
|
|
| |
| t5_pretrained: str = None |
| t5_device: str = "cuda" |
| vae_pretrained: str = None |
| scale_factor: float = 0.18215 |
| temporal_downsample_factor: int = 4 |
| load: str = None |
|
|
|
|
| @dataclasses.dataclass |
| class EngineConfig: |
| |
| distributed_backend: str = "nccl" |
| distributed_timeout_minutes: int = 10 |
| pp_size: int = 1 |
| cp_size: int = 1 |
| cp_strategy: str = "none" |
| ulysses_overlap_degree: int = 1 |
|
|
| |
| fp8_quant: bool = False |
|
|
| |
| distill_nearly_clean_chunk_threshold: float = 0.3 |
| shortcut_mode: str = "8,16,16" |
| distill: bool = False |
|
|
| |
| kv_offload: bool = False |
| enable_cuda_graph: bool = False |
|
|
|
|
| @dataclasses.dataclass |
| class MagiConfig: |
| model_config: ModelConfig |
| runtime_config: RuntimeConfig |
| engine_config: EngineConfig |
|
|
| @classmethod |
| def _check_missing_fields(cls, config_dict: dict, required_fields: list): |
| actual_fields = set(config_dict.keys()) |
| missing_fields = set(required_fields) - actual_fields |
| if missing_fields: |
| raise ValueError(f"Missing fields in the configuration file: {', '.join(missing_fields)}") |
|
|
| @classmethod |
| def _create_nested_config(cls, config_dict: dict, config_name: str, config_cls): |
| nested_config_dict = config_dict.get(config_name, {}) |
| cls._check_missing_fields(nested_config_dict, config_cls.__dataclass_fields__.keys()) |
| return config_cls(**nested_config_dict) |
|
|
| @classmethod |
| def _create_config_from_dict(cls, config_dict: dict): |
| cls._check_missing_fields(config_dict, cls.__dataclass_fields__.keys()) |
|
|
| |
| model_config = cls._create_nested_config(config_dict, "model_config", ModelConfig) |
| runtime_config = cls._create_nested_config(config_dict, "runtime_config", RuntimeConfig) |
| engine_config = cls._create_nested_config(config_dict, "engine_config", EngineConfig) |
|
|
| return cls(model_config=model_config, runtime_config=runtime_config, engine_config=engine_config) |
|
|
| @classmethod |
| def from_json(cls, json_path: str): |
| def simple_json_decoder(dct): |
| dtype_map = {"torch.bfloat16": torch.bfloat16, "torch.float16": torch.float16, "torch.float32": torch.float32} |
| if 'params_dtype' in dct: |
| dct['params_dtype'] = dtype_map[dct['params_dtype']] |
| return dct |
|
|
| with open(json_path, "r") as f: |
| config_dict = json.load(f, object_hook=simple_json_decoder) |
| magi_config = cls._create_config_from_dict(config_dict) |
|
|
| def post_validation(magi_config): |
| if magi_config.engine_config.fp8_quant or magi_config.engine_config.distill: |
| assert ( |
| magi_config.runtime_config.cfg_number == 1 |
| ), "Please set `cfg_number: 1` in config.json for distill or quant model" |
| else: |
| assert magi_config.runtime_config.cfg_number == 3, "Please set `cfg_number: 3` in config.json for base model" |
|
|
| post_validation(magi_config) |
|
|
| return magi_config |
|
|
| def to_json(self, json_path: str): |
| class SimpleJSONEncoder(json.JSONEncoder): |
| def default(self, obj): |
| if isinstance(obj, torch.dtype): |
| return str(obj) |
| return super().default(obj) |
|
|
| |
| os.makedirs(os.path.dirname(json_path), exist_ok=True) |
|
|
| config_dict = { |
| "model_config": dataclasses.asdict(self.model_config), |
| "runtime_config": dataclasses.asdict(self.runtime_config), |
| "engine_config": dataclasses.asdict(self.engine_config), |
| } |
| with open(json_path, "w") as f: |
| json.dump(config_dict, f, indent=4, cls=SimpleJSONEncoder) |
|
|