|
| 1 | +import torch, os, json |
| 2 | +from diffsynth import load_state_dict |
| 3 | +from diffsynth.pipelines.qwen_image import QwenImagePipeline, ModelConfig |
| 4 | +from diffsynth.pipelines.flux_image_new import ControlNetInput |
| 5 | +from diffsynth.trainers.utils import DiffusionTrainingModule, ModelLogger, launch_data_process_task, qwen_image_parser |
| 6 | +from diffsynth.trainers.unified_dataset import UnifiedDataset |
| 7 | +os.environ["TOKENIZERS_PARALLELISM"] = "false" |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +class QwenImageTrainingModule(DiffusionTrainingModule): |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + model_paths=None, model_id_with_origin_paths=None, |
| 15 | + tokenizer_path=None, processor_path=None, |
| 16 | + trainable_models=None, |
| 17 | + lora_base_model=None, lora_target_modules="", lora_rank=32, lora_checkpoint=None, |
| 18 | + use_gradient_checkpointing=True, |
| 19 | + use_gradient_checkpointing_offload=False, |
| 20 | + extra_inputs=None, |
| 21 | + enable_fp8_training=False, |
| 22 | + ): |
| 23 | + super().__init__() |
| 24 | + # Load models |
| 25 | + offload_dtype = torch.float8_e4m3fn if enable_fp8_training else None |
| 26 | + model_configs = [] |
| 27 | + if model_paths is not None: |
| 28 | + model_paths = json.loads(model_paths) |
| 29 | + model_configs += [ModelConfig(path=path, offload_dtype=offload_dtype) for path in model_paths] |
| 30 | + if model_id_with_origin_paths is not None: |
| 31 | + model_id_with_origin_paths = model_id_with_origin_paths.split(",") |
| 32 | + model_configs += [ModelConfig(model_id=i.split(":")[0], origin_file_pattern=i.split(":")[1], offload_dtype=offload_dtype) for i in model_id_with_origin_paths] |
| 33 | + |
| 34 | + tokenizer_config = ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="tokenizer/") if tokenizer_path is None else ModelConfig(tokenizer_path) |
| 35 | + processor_config = ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/") if processor_path is None else ModelConfig(processor_path) |
| 36 | + self.pipe = QwenImagePipeline.from_pretrained(torch_dtype=torch.bfloat16, device="cpu", model_configs=model_configs, tokenizer_config=tokenizer_config, processor_config=processor_config) |
| 37 | + |
| 38 | + # Enable FP8 |
| 39 | + if enable_fp8_training: |
| 40 | + self.pipe._enable_fp8_lora_training(torch.float8_e4m3fn) |
| 41 | + |
| 42 | + # Reset training scheduler (do it in each training step) |
| 43 | + self.pipe.scheduler.set_timesteps(1000, training=True) |
| 44 | + |
| 45 | + # Freeze untrainable models |
| 46 | + self.pipe.freeze_except([] if trainable_models is None else trainable_models.split(",")) |
| 47 | + |
| 48 | + # Add LoRA to the base models |
| 49 | + if lora_base_model is not None: |
| 50 | + model = self.add_lora_to_model( |
| 51 | + getattr(self.pipe, lora_base_model), |
| 52 | + target_modules=lora_target_modules.split(","), |
| 53 | + lora_rank=lora_rank, |
| 54 | + upcast_dtype=self.pipe.torch_dtype, |
| 55 | + ) |
| 56 | + if lora_checkpoint is not None: |
| 57 | + state_dict = load_state_dict(lora_checkpoint) |
| 58 | + state_dict = self.mapping_lora_state_dict(state_dict) |
| 59 | + load_result = model.load_state_dict(state_dict, strict=False) |
| 60 | + print(f"LoRA checkpoint loaded: {lora_checkpoint}, total {len(state_dict)} keys") |
| 61 | + if len(load_result[1]) > 0: |
| 62 | + print(f"Warning, LoRA key mismatch! Unexpected keys in LoRA checkpoint: {load_result[1]}") |
| 63 | + setattr(self.pipe, lora_base_model, model) |
| 64 | + |
| 65 | + # Store other configs |
| 66 | + self.use_gradient_checkpointing = use_gradient_checkpointing |
| 67 | + self.use_gradient_checkpointing_offload = use_gradient_checkpointing_offload |
| 68 | + self.extra_inputs = extra_inputs.split(",") if extra_inputs is not None else [] |
| 69 | + |
| 70 | + |
| 71 | + def forward_preprocess(self, data): |
| 72 | + # CFG-sensitive parameters |
| 73 | + inputs_posi = {"prompt": data["prompt"]} |
| 74 | + inputs_nega = {"negative_prompt": ""} |
| 75 | + |
| 76 | + # CFG-unsensitive parameters |
| 77 | + inputs_shared = { |
| 78 | + # Assume you are using this pipeline for inference, |
| 79 | + # please fill in the input parameters. |
| 80 | + "input_image": data["image"], |
| 81 | + "height": data["image"].size[1], |
| 82 | + "width": data["image"].size[0], |
| 83 | + # Please do not modify the following parameters |
| 84 | + # unless you clearly know what this will cause. |
| 85 | + "cfg_scale": 1, |
| 86 | + "rand_device": self.pipe.device, |
| 87 | + "use_gradient_checkpointing": self.use_gradient_checkpointing, |
| 88 | + "use_gradient_checkpointing_offload": self.use_gradient_checkpointing_offload, |
| 89 | + "edit_image_auto_resize": True, |
| 90 | + } |
| 91 | + |
| 92 | + # Extra inputs |
| 93 | + controlnet_input, blockwise_controlnet_input = {}, {} |
| 94 | + for extra_input in self.extra_inputs: |
| 95 | + if extra_input.startswith("blockwise_controlnet_"): |
| 96 | + blockwise_controlnet_input[extra_input.replace("blockwise_controlnet_", "")] = data[extra_input] |
| 97 | + elif extra_input.startswith("controlnet_"): |
| 98 | + controlnet_input[extra_input.replace("controlnet_", "")] = data[extra_input] |
| 99 | + else: |
| 100 | + inputs_shared[extra_input] = data[extra_input] |
| 101 | + if len(controlnet_input) > 0: |
| 102 | + inputs_shared["controlnet_inputs"] = [ControlNetInput(**controlnet_input)] |
| 103 | + if len(blockwise_controlnet_input) > 0: |
| 104 | + inputs_shared["blockwise_controlnet_inputs"] = [ControlNetInput(**blockwise_controlnet_input)] |
| 105 | + |
| 106 | + # Pipeline units will automatically process the input parameters. |
| 107 | + for unit in self.pipe.units: |
| 108 | + inputs_shared, inputs_posi, inputs_nega = self.pipe.unit_runner(unit, self.pipe, inputs_shared, inputs_posi, inputs_nega) |
| 109 | + return {**inputs_shared, **inputs_posi} |
| 110 | + |
| 111 | + |
| 112 | + def forward(self, data, inputs=None): |
| 113 | + if inputs is None: inputs = self.forward_preprocess(data) |
| 114 | + return inputs |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + parser = qwen_image_parser() |
| 120 | + args = parser.parse_args() |
| 121 | + dataset = UnifiedDataset( |
| 122 | + base_path=args.dataset_base_path, |
| 123 | + metadata_path=args.dataset_metadata_path, |
| 124 | + repeat=1, # Set repeat = 1 |
| 125 | + data_file_keys=args.data_file_keys.split(","), |
| 126 | + main_data_operator=UnifiedDataset.default_image_operator( |
| 127 | + base_path=args.dataset_base_path, |
| 128 | + max_pixels=args.max_pixels, |
| 129 | + height=args.height, |
| 130 | + width=args.width, |
| 131 | + height_division_factor=16, |
| 132 | + width_division_factor=16, |
| 133 | + ) |
| 134 | + ) |
| 135 | + model = QwenImageTrainingModule( |
| 136 | + model_paths=args.model_paths, |
| 137 | + model_id_with_origin_paths=args.model_id_with_origin_paths, |
| 138 | + tokenizer_path=args.tokenizer_path, |
| 139 | + processor_path=args.processor_path, |
| 140 | + trainable_models=args.trainable_models, |
| 141 | + lora_base_model=args.lora_base_model, |
| 142 | + lora_target_modules=args.lora_target_modules, |
| 143 | + lora_rank=args.lora_rank, |
| 144 | + lora_checkpoint=args.lora_checkpoint, |
| 145 | + use_gradient_checkpointing=args.use_gradient_checkpointing, |
| 146 | + use_gradient_checkpointing_offload=args.use_gradient_checkpointing_offload, |
| 147 | + extra_inputs=args.extra_inputs, |
| 148 | + enable_fp8_training=args.enable_fp8_training, |
| 149 | + ) |
| 150 | + model_logger = ModelLogger(args.output_path, remove_prefix_in_ckpt=args.remove_prefix_in_ckpt) |
| 151 | + launch_data_process_task( |
| 152 | + dataset, model, model_logger, |
| 153 | + num_workers=args.dataset_num_workers, |
| 154 | + ) |
0 commit comments