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

Function save_model_hook

diffusers/examples/amused/train_amused.py:512–542  ·  view source on GitHub ↗
(models, weights, output_dir)

Source from the content-addressed store, hash-verified

510 )
511
512 def save_model_hook(models, weights, output_dir):
513 if accelerator.is_main_process:
514 transformer_lora_layers_to_save = None
515 text_encoder_lora_layers_to_save = None
516
517 for model_ in models:
518 if isinstance(model_, type(accelerator.unwrap_model(model))):
519 if args.use_lora:
520 transformer_lora_layers_to_save = get_peft_model_state_dict(model_)
521 else:
522 model_.save_pretrained(os.path.join(output_dir, "transformer"))
523 elif isinstance(model_, type(accelerator.unwrap_model(text_encoder))):
524 if args.text_encoder_use_lora:
525 text_encoder_lora_layers_to_save = get_peft_model_state_dict(model_)
526 else:
527 model_.save_pretrained(os.path.join(output_dir, "text_encoder"))
528 else:
529 raise ValueError(f"unexpected save model: {model_.__class__}")
530
531 # make sure to pop weight so that corresponding model is not saved again
532 weights.pop()
533
534 if transformer_lora_layers_to_save is not None or text_encoder_lora_layers_to_save is not None:
535 AmusedLoraLoaderMixin.save_lora_weights(
536 output_dir,
537 transformer_lora_layers=transformer_lora_layers_to_save,
538 text_encoder_lora_layers=text_encoder_lora_layers_to_save,
539 )
540
541 if args.use_ema:
542 ema.save_pretrained(os.path.join(output_dir, "ema_model"))
543
544 def load_model_hook(models, input_dir):
545 transformer = None

Callers

nothing calls this directly

Calls 3

save_pretrainedMethod · 0.45
popMethod · 0.45
save_lora_weightsMethod · 0.45

Tested by

no test coverage detected