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

Function load_model_hook

diffusers/examples/dreambooth/train_dreambooth_sd3.py:1192–1223  ·  view source on GitHub ↗
(models, input_dir)

Source from the content-addressed store, hash-verified

1190 weights.pop()
1191
1192 def load_model_hook(models, input_dir):
1193 for _ in range(len(models)):
1194 # pop models so that they are not loaded again
1195 model = models.pop()
1196
1197 # load diffusers style into model
1198 if isinstance(unwrap_model(model), SD3Transformer2DModel):
1199 load_model = SD3Transformer2DModel.from_pretrained(input_dir, subfolder="transformer")
1200 model.register_to_config(**load_model.config)
1201
1202 model.load_state_dict(load_model.state_dict())
1203 elif isinstance(unwrap_model(model), (CLIPTextModelWithProjection, T5EncoderModel)):
1204 try:
1205 load_model = CLIPTextModelWithProjection.from_pretrained(input_dir, subfolder="text_encoder")
1206 model(**load_model.config)
1207 model.load_state_dict(load_model.state_dict())
1208 except Exception:
1209 try:
1210 load_model = CLIPTextModelWithProjection.from_pretrained(input_dir, subfolder="text_encoder_2")
1211 model(**load_model.config)
1212 model.load_state_dict(load_model.state_dict())
1213 except Exception:
1214 try:
1215 load_model = T5EncoderModel.from_pretrained(input_dir, subfolder="text_encoder_3")
1216 model(**load_model.config)
1217 model.load_state_dict(load_model.state_dict())
1218 except Exception:
1219 raise ValueError(f"Couldn't load the model of type: ({type(model)}).")
1220 else:
1221 raise ValueError(f"Unsupported model found: {type(model)=}")
1222
1223 del load_model
1224
1225 accelerator.register_save_state_pre_hook(save_model_hook)
1226 accelerator.register_load_state_pre_hook(load_model_hook)

Callers

nothing calls this directly

Calls 6

register_to_configMethod · 0.80
load_state_dictMethod · 0.80
unwrap_modelFunction · 0.70
popMethod · 0.45
from_pretrainedMethod · 0.45
state_dictMethod · 0.45

Tested by

no test coverage detected