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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
import torch, json, os, inspect
from ..core import ModelConfig, load_state_dict, QuantizeConfig
from ..utils.controlnet import ControlNetInput
from .base_pipeline import PipelineUnit


class GeneralUnit_RemoveCache(PipelineUnit):
    # Only used for training
    def __init__(self, required_params=tuple(), force_remove_params_shared=tuple(), force_remove_params_posi=tuple(), force_remove_params_nega=tuple()):
        super().__init__(take_over=True)
        self.required_params = required_params
        self.force_remove_params_shared = force_remove_params_shared
        self.force_remove_params_posi = force_remove_params_posi
        self.force_remove_params_nega = force_remove_params_nega

    def process_params(self, inputs, required_params, force_remove_params):
        inputs_ = {}
        for name, param in inputs.items():
            if name in required_params and name not in force_remove_params:
                inputs_[name] = param
        return inputs_

    def process(self, pipe, inputs_shared, inputs_posi, inputs_nega):
        inputs_shared = self.process_params(inputs_shared, self.required_params, self.force_remove_params_shared)
        inputs_posi = self.process_params(inputs_posi, self.required_params, self.force_remove_params_posi)
        inputs_nega = self.process_params(inputs_nega, self.required_params, self.force_remove_params_nega)
        return inputs_shared, inputs_posi, inputs_nega


class GeneralUnit_TemplateProcessInputs(PipelineUnit):
    # Only used for training
    def __init__(self, data_processor):
        super().__init__(
            input_params=("template_inputs",),
            output_params=("template_inputs",),
        )
        self.data_processor = data_processor
    
    def process(self, pipe, template_inputs):
        if not hasattr(pipe, "template_model") or template_inputs is None:
            return {}
        if self.data_processor is not None:
            template_inputs = self.data_processor(**template_inputs)
        template_inputs = pipe.template_model.process_inputs(pipe=pipe, **template_inputs)
        return {"template_inputs": template_inputs}


class GeneralUnit_TemplateForward(PipelineUnit):
    # Only used for training
    def __init__(self, use_gradient_checkpointing=False, use_gradient_checkpointing_offload=False):
        super().__init__(
            input_params=("template_inputs",),
            output_params=("kv_cache",),
            onload_model_names=("template_model",)
        )
        self.use_gradient_checkpointing = use_gradient_checkpointing
        self.use_gradient_checkpointing_offload = use_gradient_checkpointing_offload
    
    def process(self, pipe, template_inputs):
        if not hasattr(pipe, "template_model") or template_inputs is None:
            return {}
        template_cache = pipe.template_model.forward(
            **template_inputs,
            pipe=pipe,
            use_gradient_checkpointing=self.use_gradient_checkpointing,
            use_gradient_checkpointing_offload=self.use_gradient_checkpointing_offload,
        )
        return template_cache


class DiffusionTrainingModule(torch.nn.Module):
    def __init__(self):
        super().__init__()
        
        
    def to(self, *args, **kwargs):
        # DeepSpeed calls .to(device) during initialize even when the H3 model
        # was constructed directly on that device. Comfy INT8 parameters carry
        # weakrefs and torch._apply cannot swap them a second time.
        if getattr(self, "_skip_redundant_to", False):
            requested = kwargs.get("device", args[0] if args else None)
            if requested is not None:
                device = torch.device(requested)
                if all(p.device == device for p in self.parameters()):
                    return self
        for name, model in self.named_children():
            model.to(*args, **kwargs)
        return self
        
        
    def trainable_modules(self):
        trainable_modules = filter(lambda p: p.requires_grad, self.parameters())
        return trainable_modules
    
    
    def trainable_param_names(self):
        trainable_param_names = list(filter(lambda named_param: named_param[1].requires_grad, self.named_parameters()))
        trainable_param_names = set([named_param[0] for named_param in trainable_param_names])
        return trainable_param_names
    
    
    def add_lora_to_model(self, model, target_modules, lora_rank, lora_alpha=None, upcast_dtype=None):
        from peft import LoraConfig, inject_adapter_in_model
        if lora_alpha is None:
            lora_alpha = lora_rank
        if isinstance(target_modules, list) and len(target_modules) == 1:
            target_modules = target_modules[0]
        lora_config = LoraConfig(r=lora_rank, lora_alpha=lora_alpha, target_modules=target_modules)
        model = inject_adapter_in_model(lora_config, model)
        if upcast_dtype is not None:
            for param in model.parameters():
                if param.requires_grad:
                    param.data = param.to(upcast_dtype)
        return model


    def mapping_lora_state_dict(self, state_dict):
        new_state_dict = {}
        for key, value in state_dict.items():
            if "lora_A.weight" in key or "lora_B.weight" in key:
                new_key = key.replace("lora_A.weight", "lora_A.default.weight").replace("lora_B.weight", "lora_B.default.weight")
                new_state_dict[new_key] = value
            elif "lora_A.default.weight" in key or "lora_B.default.weight" in key:
                new_state_dict[key] = value
        return new_state_dict


    def export_trainable_state_dict(self, state_dict, remove_prefix=None):
        trainable_param_names = self.trainable_param_names()
        state_dict = {name: param for name, param in state_dict.items() if name in trainable_param_names}
        if remove_prefix is not None:
            state_dict_ = {}
            for name, param in state_dict.items():
                if name.startswith(remove_prefix):
                    name = name[len(remove_prefix):]
                state_dict_[name] = param
            state_dict = state_dict_
        return state_dict
    
    
    def transfer_data_to_device(self, data, device, torch_float_dtype=None):
        if data is None:
            return data
        elif isinstance(data, torch.Tensor):
            data = data.to(device)
            if torch_float_dtype is not None and data.dtype in [torch.float, torch.float16, torch.bfloat16]:
                data = data.to(torch_float_dtype)
            return data
        elif isinstance(data, tuple):
            data = tuple(self.transfer_data_to_device(x, device, torch_float_dtype) for x in data)
            return data
        elif isinstance(data, list):
            data = list(self.transfer_data_to_device(x, device, torch_float_dtype) for x in data)
            return data
        elif isinstance(data, dict):
            data = {i: self.transfer_data_to_device(data[i], device, torch_float_dtype) for i in data}
            return data
        else:
            return data
    
    def parse_vram_config(self, fp8=False, offload=False, device="cpu"):
        if fp8:
            return {
                "offload_dtype": torch.float8_e4m3fn,
                "offload_device": device,
                "onload_dtype": torch.float8_e4m3fn,
                "onload_device": device,
                "preparing_dtype": torch.float8_e4m3fn,
                "preparing_device": device,
                "computation_dtype": torch.bfloat16,
                "computation_device": device,
            }
        elif offload:
            return {
                "offload_dtype": "disk",
                "offload_device": "disk",
                "onload_dtype": "disk",
                "onload_device": "disk",
                "preparing_dtype": torch.bfloat16,
                "preparing_device": device,
                "computation_dtype": torch.bfloat16,
                "computation_device": device,
                "clear_parameters": True,
            }
        else:
            return {}
    
    def parse_quant_options(self, quant_options):
        quant_map = {}
        if quant_options is None:
            return quant_map
        for entry in quant_options.split(";"):
            if entry == "":
                continue
            model_string, sep, spec = entry.rpartition(":")
            if sep == "":
                raise ValueError(f"Failed to parse quant option: `{entry}`. Expected `<model_string>:<method>[/<exclude_modules>]`.")
            if spec == "":
                continue
            method, _, excludes = spec.partition("/")
            exclude_modules = excludes.split(",") if excludes != "" else None
            quant_config = QuantizeConfig(method=method, exclude_modules=exclude_modules)
            if not quant_config.backend.capabilities().get("is_differentiable", True):
                raise ValueError(f"Quantization method `{method}` is not differentiable, so it cannot be used for training (frozen quantized layers must pass gradients through to LoRA branches). Choose a method whose backend declares `is_differentiable=True`.")
            quant_map[self.normalize_quant_key(model_string)] = quant_config
        return quant_map

    def normalize_quant_key(self, model_string):
        if isinstance(model_string, str) and model_string.startswith("[") and model_string.endswith("]"):
            try:
                model_string = json.loads(model_string)
            except json.JSONDecodeError:
                raise ValueError(f"Failed to parse quant option model string: `{model_string}`. A multi-file model must be written as a JSON list of files, matching its `--model_paths` entry.")
        if isinstance(model_string, list):
            return tuple(model_string)
        return model_string

    def get_quant_config(self, quant_map, model_string):
        quant_config = quant_map.get(self.normalize_quant_key(model_string))
        if quant_config is None and len(quant_map) > 0:
            print(f"No quant option matches `{model_string}`. This model is loaded without quantization. Parsed quant options: {({key: config.method for key, config in quant_map.items()})}.")
        return quant_config

    def parse_model_configs(self, model_paths, model_id_with_origin_paths, fp8_models=None, offload_models=None, quant_options=None, device="cpu"):
        fp8_models = [] if fp8_models is None else fp8_models.split(",")
        offload_models = [] if offload_models is None else offload_models.split(",")
        quant_map = self.parse_quant_options(quant_options)
        model_configs = []
        if model_paths is not None:
            model_paths = json.loads(model_paths)
            for path in model_paths:
                vram_config = self.parse_vram_config(
                    fp8=path in fp8_models,
                    offload=path in offload_models,
                    device=device
                )
                model_configs.append(ModelConfig(path=path, quantize=self.get_quant_config(quant_map, path), **vram_config))
        if model_id_with_origin_paths is not None:
            model_id_with_origin_paths = model_id_with_origin_paths.split(",")
            for model_id_with_origin_path in model_id_with_origin_paths:
                vram_config = self.parse_vram_config(
                    fp8=model_id_with_origin_path in fp8_models,
                    offload=model_id_with_origin_path in offload_models,
                    device=device
                )
                config = self.parse_path_or_model_id(model_id_with_origin_path)
                model_configs.append(ModelConfig(model_id=config.model_id, origin_file_pattern=config.origin_file_pattern, quantize=self.get_quant_config(quant_map, model_id_with_origin_path), **vram_config))
        return model_configs
    

    def parse_path_or_model_id(self, model_id_with_origin_path, default_value=None):
        if model_id_with_origin_path is None:
            return default_value
        elif os.path.exists(model_id_with_origin_path):
            return ModelConfig(path=model_id_with_origin_path)
        else:
            if ":" not in model_id_with_origin_path:
                raise ValueError(f"Failed to parse model config: {model_id_with_origin_path}. This is neither a valid path nor in the format of `model_id/origin_file_pattern`.")
            split_id = model_id_with_origin_path.rfind(":")
            model_id = model_id_with_origin_path[:split_id]
            origin_file_pattern = model_id_with_origin_path[split_id + 1:]
            return ModelConfig(model_id=model_id, origin_file_pattern=origin_file_pattern)


    def auto_detect_lora_target_modules(
        self,
        model: torch.nn.Module,
        search_for_linear=False,
        linear_detector=lambda x: min(x.in_features, x.out_features) >= 512,
        block_list_detector=lambda x: isinstance(x, torch.nn.ModuleList) and len(x) > 1,
        name_prefix="",
    ):
        lora_target_modules = []
        if search_for_linear:
            for name, module in model.named_modules():
                module_name = name_prefix + ["", "."][name_prefix != ""] + name
                if isinstance(module, torch.nn.Linear) and linear_detector(module):
                    lora_target_modules.append(module_name)
        else:
            for name, module in model.named_children():
                module_name = name_prefix + ["", "."][name_prefix != ""] + name
                lora_target_modules += self.auto_detect_lora_target_modules(
                    module,
                    search_for_linear=block_list_detector(module),
                    linear_detector=linear_detector,
                    block_list_detector=block_list_detector,
                    name_prefix=module_name,
                )
        return lora_target_modules
    

    def parse_lora_target_modules(self, model, lora_target_modules):
        if lora_target_modules == "":
            print("No LoRA target modules specified. The framework will automatically search for them.")
            lora_target_modules = self.auto_detect_lora_target_modules(model)
            print(f"LoRA will be patched at {lora_target_modules}.")
        else:
            lora_target_modules = lora_target_modules.split(",")
        return lora_target_modules
    

    def load_training_template_model(self, pipe, path_or_model_id, use_gradient_checkpointing=False, use_gradient_checkpointing_offload=False):
        if path_or_model_id is None:
            return pipe
        model_config = self.parse_path_or_model_id(path_or_model_id)
        pipe.load_training_template_model(model_config)
        template_units = [
            GeneralUnit_TemplateProcessInputs(pipe.template_data_processor),
            GeneralUnit_TemplateForward(use_gradient_checkpointing, use_gradient_checkpointing_offload),
        ]
        pipe.units = template_units + pipe.units
        return pipe


    def switch_pipe_to_training_mode(
        self,
        pipe,
        trainable_models=None,
        lora_base_model=None, lora_target_modules="", lora_rank=32, lora_checkpoint=None,
        preset_lora_path=None, preset_lora_model=None,
        task="sft",
    ):
        # Scheduler
        pipe.scheduler.set_timesteps(1000, training=True)
        
        # Freeze untrainable models
        pipe.freeze_except([] if trainable_models is None else trainable_models.split(","))
        
        # Preset LoRA
        # `preset_lora_path` 支持逗号分隔的多个 LoRA 文件,按先后顺序依次融合到同一基座上(delta 累加)。
        # 例如同时融合 FL2VA 与 Ref2VA 两个 adapter 做混合训练。
        if preset_lora_path is not None:
            for preset_path in preset_lora_path.split(","):
                preset_path = preset_path.strip()
                if preset_path:
                    print(f"Fusing preset LoRA: {preset_path}")
                    pipe.load_lora(getattr(pipe, preset_lora_model), preset_path)
        
        # FP8
        # FP8 relies on a model-specific memory management scheme.
        # It is delegated to the subclass.
        
        # Add LoRA to the base models
        if lora_base_model is not None and not task.endswith(":data_process"):
            if lora_base_model != "" and ((not hasattr(pipe, lora_base_model)) or getattr(pipe, lora_base_model) is None):
                print(f"No {lora_base_model} models in the pipeline. We cannot patch LoRA on the model. If this occurs during the data processing stage, it is normal.")
                return
            model = pipe if lora_base_model == "" else getattr(pipe, lora_base_model)
            model = self.add_lora_to_model(
                model,
                target_modules=self.parse_lora_target_modules(model, lora_target_modules),
                lora_rank=lora_rank,
                upcast_dtype=pipe.torch_dtype,
            )
            if lora_checkpoint is not None:
                if isinstance(lora_checkpoint, str):
                    raw_lora = load_state_dict(lora_checkpoint)
                else:
                    raw_lora = lora_checkpoint
                lora_loader = pipe.lora_loader(torch_dtype=pipe.torch_dtype, device=pipe.device)
                lora = lora_loader.convert_state_dict(raw_lora)
                lora = self.mapping_lora_state_dict(lora)
                # convert_state_dict keeps only LoRA pairs. E8T4 checkpoints also store the
                # trained router, and dropping it would re-init the gate from seed 0.
                gate_items = {key: value for key, value in raw_lora.items()
                              if key.endswith("mlp.gate.weight") or key.endswith("mlp.router.weight")}
                lora.update(gate_items)
                missing_keys, unexpected_keys = model.load_state_dict(lora, strict=False)
                print(f"LoRA checkpoint loaded. Total {len(lora)} keys")
                if gate_items:
                    print(f"Router gates loaded from checkpoint: {len(gate_items)}")
                unexpected_gates = [key for key in unexpected_keys
                                    if key.endswith("mlp.gate.weight") or key.endswith("mlp.router.weight")]
                if unexpected_gates:
                    raise RuntimeError(f"Router gates in the checkpoint did not match the model: {unexpected_gates[:4]}")
                if len(unexpected_keys) > 0:
                    print(f"Warning, LoRA key mismatch! Unexpected keys in LoRA checkpoint: {unexpected_keys}")
                del missing_keys
            if lora_base_model != "":
                setattr(pipe, lora_base_model, model)


    def split_pipeline_units(
        self, task, pipe,
        trainable_models=None, lora_base_model=None,
        # TODO: set `remove_unnecessary_params` to `True` by default
        remove_unnecessary_params=False,
        # TODO: move `loss_required_params` to `loss.py`
        loss_required_params=("input_latents", "max_timestep_boundary", "min_timestep_boundary", "first_frame_latents", "video_latents", "audio_input_latents", "num_inference_steps", "cfg_scale"),
        force_remove_params_shared=tuple(),
        force_remove_params_posi=tuple(),
        force_remove_params_nega=tuple(),
    ):
        models_require_backward = []
        if trainable_models is not None:
            models_require_backward += trainable_models.split(",")
        if lora_base_model is not None:
            models_require_backward += [lora_base_model]
        if task.endswith(":data_process"):
            other_units, pipe.units = pipe.split_pipeline_units(models_require_backward)
            if remove_unnecessary_params:
                required_params = list(loss_required_params) + [i for i in inspect.signature(self.pipe.model_fn).parameters]
                for unit in other_units:
                    required_params.extend(unit.fetch_input_params())
                required_params = sorted(list(set(required_params)))
                pipe.units.append(GeneralUnit_RemoveCache(required_params, force_remove_params_shared, force_remove_params_posi, force_remove_params_nega))
        elif task.endswith(":train"):
            pipe.units, _ = pipe.split_pipeline_units(models_require_backward)
        return pipe
    
    def parse_extra_inputs(self, data, extra_inputs, inputs_shared):
        controlnet_keys_map = (
            ("blockwise_controlnet_", "blockwise_controlnet_inputs",),
            ("controlnet_", "controlnet_inputs"),
        )
        controlnet_inputs = {}
        for extra_input in extra_inputs:
            for prefix, name in controlnet_keys_map:
                if extra_input.startswith(prefix):
                    if name not in controlnet_inputs:
                        controlnet_inputs[name] = {}
                    controlnet_inputs[name][extra_input.replace(prefix, "")] = data[extra_input]
                    break
            else:
                inputs_shared[extra_input] = data[extra_input]
        for name, params in controlnet_inputs.items():
            inputs_shared[name] = [ControlNetInput(**params)]
        return inputs_shared
    
    def resume_from_checkpoint(self, path, remove_prefix_in_ckpt):
        if path is None:
            return
        state_dict = load_state_dict(path)
        if remove_prefix_in_ckpt is not None:
            state_dict = {remove_prefix_in_ckpt + i: state_dict[i] for i in state_dict}
        missing_keys, unexpected_keys = self.load_state_dict(state_dict, strict=False)
        if len(unexpected_keys) != 0:
            raise ValueError(f"Cannot load checkpoint: {path}. {len(unexpected_keys)} keys are unexpected.")
        print(f"Loaded checkpoint from {path}. {len(state_dict)} keys are available.")