MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / load_model_hook

Function load_model_hook

diffusers/examples/cogvideo/train_cogvideox_lora.py:1150–1182  ·  view source on GitHub ↗
(models, input_dir)

Source from the content-addressed store, hash-verified

1148 )
1149
1150 def load_model_hook(models, input_dir):
1151 transformer_ = None
1152
1153 while len(models) > 0:
1154 model = models.pop()
1155
1156 if isinstance(model, type(unwrap_model(transformer))):
1157 transformer_ = model
1158 else:
1159 raise ValueError(f"Unexpected save model: {model.__class__}")
1160
1161 lora_state_dict = CogVideoXPipeline.lora_state_dict(input_dir)
1162
1163 transformer_state_dict = {
1164 f'{k.replace("transformer.", "")}': v for k, v in lora_state_dict.items() if k.startswith("transformer.")
1165 }
1166 transformer_state_dict = convert_unet_state_dict_to_peft(transformer_state_dict)
1167 incompatible_keys = set_peft_model_state_dict(transformer_, transformer_state_dict, adapter_name="default")
1168 if incompatible_keys is not None:
1169 # check only for unexpected keys
1170 unexpected_keys = getattr(incompatible_keys, "unexpected_keys", None)
1171 if unexpected_keys:
1172 logger.warning(
1173 f"Loading adapter weights from state_dict led to unexpected keys not found in the model: "
1174 f" {unexpected_keys}. "
1175 )
1176
1177 # Make sure the trainable params are in float32. This is again needed since the base models
1178 # are in `weight_dtype`. More details:
1179 # https://github.com/huggingface/diffusers/pull/6514#discussion_r1449796804
1180 if args.mixed_precision == "fp16":
1181 # only upcast trainable parameters (LoRA) into fp32
1182 cast_training_params([transformer_])
1183
1184 accelerator.register_save_state_pre_hook(save_model_hook)
1185 accelerator.register_load_state_pre_hook(load_model_hook)

Callers

nothing calls this directly

Calls 5

cast_training_paramsFunction · 0.90
unwrap_modelFunction · 0.70
popMethod · 0.45
lora_state_dictMethod · 0.45

Tested by

no test coverage detected