File size: 9,873 Bytes
b9fc164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import glob
import torch
import torch.distributed as dist
from collections.abc import Sequence

from src.utils.fsdp_load import maybe_load_fsdp_model, pt_weights_iterator, safetensors_weights_iterator
from src.utils.logging import get_logger
from src.utils.lora import validate_lora_state_dict
from src.utils.constants import PRECISION_TO_TYPE
from src.utils.utils import build_from_config, get_obj_from_str


def _uses_pretrained_loading(config: dict | None) -> bool:
    if config is None or config.get("pretrained") is None:
        return False

    target = str(config.get("target", ""))
    try:
        cls = get_obj_from_str(target)
    except Exception:
        return target.startswith("diffusers.")

    return hasattr(cls, "from_pretrained")


def _strip_common_state_prefixes(state_dict: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]:
    for prefix in ("model.", "module.", "transformer."):
        stripped_state_dict = {}
        matched = 0
        for key, value in state_dict.items():
            if key.startswith(prefix):
                stripped_state_dict[key[len(prefix):]] = value
                matched += 1
            else:
                stripped_state_dict[key] = value
        if matched > 0:
            state_dict = stripped_state_dict
    return state_dict


def _normalize_train_modules(train_modules: str | Sequence[str] | None) -> list[str] | None:
    if train_modules is None:
        return None
    if isinstance(train_modules, str):
        train_modules = [train_modules]

    normalized = [module_name.strip() for module_name in train_modules if module_name and module_name.strip()]
    return normalized or None


def _configure_dit_trainable_modules(model: torch.nn.Module, cfg) -> None:
    logger = get_logger()
    train_modules = _normalize_train_modules(getattr(cfg, "dit_train_modules", None))

    if train_modules is None:
        model.requires_grad_(True)
        return

    model.requires_grad_(False)
    named_modules = dict(model.named_modules())
    missing_modules = [module_name for module_name in train_modules if module_name not in named_modules]
    if missing_modules:
        available_modules = [module_name for module_name in named_modules.keys() if module_name]
        preview = ", ".join(available_modules[:20])
        if len(available_modules) > 20:
            preview = f"{preview}, ..."
        raise ValueError(
            f"Unknown dit_train_modules: {missing_modules}. "
            f"Available DiT modules include: {preview}"
        )

    for module_name in train_modules:
        named_modules[module_name].requires_grad_(True)

    trainable_param_names = [name for name, param in model.named_parameters() if param.requires_grad]
    if not trainable_param_names:
        raise ValueError(f"No trainable DiT parameters were enabled for dit_train_modules={train_modules}.")

    logger.info(f"Training {len(trainable_param_names)} parameter tensors.")


def _load_vae(cfg, device: torch.device):
    """Load VAE weights and initialize complete modules absent from the checkpoint."""
    vae_kwargs = {"torch_dtype": PRECISION_TO_TYPE[cfg.vae_precision], "device": device}
    if not _uses_pretrained_loading(cfg.vae_arch_config):
        return build_from_config(cfg.vae_arch_config, **vae_kwargs)
    vae, loading = build_from_config(
        cfg.vae_arch_config, **vae_kwargs, low_cpu_mem_usage=False, output_loading_info=True,
    )
    missing_by_module = {}
    for key in loading["missing_keys"]:
        module_name, _, parameter_name = key.rpartition(".")
        missing_by_module.setdefault(module_name, set()).add(parameter_name)
    for module_name, missing in sorted(missing_by_module.items()):
        module = vae.get_submodule(module_name)
        parameters = set(dict(module.named_parameters(recurse=False)))
        reset = getattr(module, "reset_parameters", None)
        if missing != parameters or not callable(reset):
            raise ValueError(f"Cannot initialize incomplete VAE module {module_name!r}: missing={sorted(missing)}.")
        reset()
    return vae


def load_pipeline(cfg, dit, device: torch.device):
    """Build the frozen VAE, text encoder, scheduler, and editing pipeline."""
    vae = _load_vae(cfg, device).eval().requires_grad_(False)
    tokenizer, text_encoder = build_from_config(
        cfg.text_encoder_arch_config, torch_dtype=PRECISION_TO_TYPE[cfg.text_encoder_precision], device=device,
    )
    pipeline = build_from_config(
        cfg.pipeline_arch_config or {"target": "src.models.common.diffusion.pipelines.Pipeline"},
        vae=vae,
        tokenizer=tokenizer,
        text_encoder=text_encoder,
        transformer=dit,
        scheduler=build_from_config(cfg.scheduler_arch_config),
        args=cfg,
    )
    return pipeline.to(device)


def _read_dit_checkpoint(path, checkpoint_type):
    if checkpoint_type == "pt":
        state = dict(pt_weights_iterator([path]))
    elif checkpoint_type == "safetensor":
        files = [str(path)] if os.path.isfile(path) else sorted(glob.glob(os.path.join(str(path), "*.safetensors")))
        if not files:
            raise FileNotFoundError(f"No safetensors checkpoints found at {path}.")
        state = dict(safetensors_weights_iterator(files))
    else:
        raise ValueError(f"Unknown checkpoint type: {checkpoint_type!r}.")
    return state.get("model", state)


def _merge_generator_lora(model, checkpoint, cfg):
    from peft import LoraConfig
    from peft.utils import set_peft_model_state_dict

    weights = checkpoint["generator_lora"]
    adapter_config = checkpoint.get("generator_lora_config")
    if adapter_config is None:
        ranks = {weight.shape[0] for name, weight in weights.items() if ".lora_A." in name}
        if len(ranks) != 1:
            raise ValueError("The generator checkpoint must contain a uniform-rank LoRA adapter.")
        rank = ranks.pop()
        adapter_config = {
            "r": rank,
            "lora_alpha": getattr(cfg, "dmd_lora_alpha", rank),
            "target_modules": sorted({name.split(".lora_")[0] for name in weights if ".lora_" in name}),
            "bias": getattr(cfg, "dmd_lora_bias", "none"),
        }
    adapter_name = checkpoint.get("generator_adapter_name", "generator")
    model.add_adapter(LoraConfig(**adapter_config), adapter_name=adapter_name)
    validate_lora_state_dict(model, weights, adapter_name)
    result = set_peft_model_state_dict(model, weights, adapter_name=adapter_name)
    missing_adapter_keys = [name for name in result.missing_keys if ".lora_" in name]
    if missing_adapter_keys or result.unexpected_keys:
        raise ValueError("Generator LoRA weights do not match the configured model.")
    model.fuse_lora(adapter_names=[adapter_name], safe_fusing=True)
    model.unload_lora()


def load_dit(cfg, device: torch.device, *, use_fsdp: bool = True) -> torch.nn.Module:
    """Load DiT model with FSDP support."""
    logger = get_logger()

    state_dict = _read_dit_checkpoint(cfg.dit_ckpt, cfg.dit_ckpt_type) if cfg.dit_ckpt is not None else None
    adapter_checkpoint = None
    if state_dict is not None and "generator_lora" in state_dict:
        adapter_checkpoint = state_dict
        base_path = getattr(cfg, "dmd_base_ckpt", None) or getattr(cfg, "dmd_stage1_ckpt", None)
        if not base_path or os.path.abspath(base_path) == os.path.abspath(cfg.dit_ckpt):
            raise ValueError("Set JOYAI_STAGE1_CKPT or dmd_base_ckpt to the SFT base for generator LoRA inference.")
        state_dict = _read_dit_checkpoint(base_path, getattr(cfg, "dmd_base_ckpt_type", "pt"))

    dtype = PRECISION_TO_TYPE[cfg.dit_precision]
    use_pretrained_loading = _uses_pretrained_loading(cfg.dit_arch_config)
    if use_pretrained_loading:
        model_kwargs = {'torch_dtype': dtype, 'device': device}
    else:
        model_kwargs = {'dtype': dtype, 'device': device, 'args': cfg}
    model = build_from_config(cfg.dit_arch_config, **model_kwargs)
    if not use_fsdp or not dist.is_initialized() or dist.get_world_size() == 1:
        model.to(device=device)

    if state_dict is not None:
        state_dict = _strip_common_state_prefixes(state_dict)

        load_state_dict = {}
        for k, v in state_dict.items():
            if (
                k == "img_in.weight"
                and hasattr(model, "img_in")
                and model.img_in.weight.shape != v.shape
            ):
                v = v.reshape(model.img_in.weight.shape)
            load_state_dict[k] = v
        strict = not use_pretrained_loading
        missing_keys, unexpected_keys = model.load_state_dict(load_state_dict, strict=strict)
        if missing_keys:
            logger.warning(f"Missing keys when loading DiT: {missing_keys[:20]}")
        if unexpected_keys:
            logger.warning(f"Unexpected keys when loading DiT: {unexpected_keys[:20]}")

    if adapter_checkpoint is not None:
        _merge_generator_lora(model, adapter_checkpoint, cfg)
    _configure_dit_trainable_modules(model, cfg)

    if use_fsdp:
        model = maybe_load_fsdp_model(
            model=model,
            hsdp_shard_dim=cfg.hsdp_shard_dim,
            reshard_after_forward=cfg.reshard_after_forward,
            param_dtype=dtype,
            reduce_dtype=torch.float32,
            output_dtype=None,
            cpu_offload=cfg.cpu_offload,
            pin_cpu_memory=cfg.pin_cpu_memory,
        )

    total_params = sum(p.numel() for p in model.parameters())
    logger.info(f"Instantiate model with {total_params / 1e9:.2f}B parameters")

    param_dtypes = {param.dtype for param in model.parameters()}
    if len(param_dtypes) > 1:
        logger.warning(
            f"Model has mixed dtypes: {param_dtypes}. Converting to {dtype}")
        model = model.to(dtype)

    return model.eval()


__all__ = [
    "load_dit",
    "load_pipeline",
]