-
Notifications
You must be signed in to change notification settings - Fork 6.7k
small tweaks for parsing thibaudz controlnet checkpoints #3657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -339,41 +339,46 @@ def create_ldm_bert_config(original_config): | |
| return config | ||
|
|
||
|
|
||
| def convert_ldm_unet_checkpoint(checkpoint, config, path=None, extract_ema=False, controlnet=False): | ||
| def convert_ldm_unet_checkpoint( | ||
| checkpoint, config, path=None, extract_ema=False, controlnet=False, skip_extract_state_dict=False | ||
| ): | ||
| """ | ||
| Takes a state dict and a config, and returns a converted checkpoint. | ||
| """ | ||
|
|
||
| # extract state_dict for UNet | ||
| unet_state_dict = {} | ||
| keys = list(checkpoint.keys()) | ||
|
|
||
| if controlnet: | ||
| unet_key = "control_model." | ||
| if skip_extract_state_dict: | ||
| unet_state_dict = checkpoint | ||
|
Comment on lines
+349
to
+350
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this allows us to skip the initial key trimming for when the root level checkpoint doesn't need to be trimmed. If we don't manually skip it, the |
||
| else: | ||
| unet_key = "model.diffusion_model." | ||
|
|
||
| # at least a 100 parameters have to start with `model_ema` in order for the checkpoint to be EMA | ||
| if sum(k.startswith("model_ema") for k in keys) > 100 and extract_ema: | ||
| print(f"Checkpoint {path} has both EMA and non-EMA weights.") | ||
| print( | ||
| "In this conversion only the EMA weights are extracted. If you want to instead extract the non-EMA" | ||
| " weights (useful to continue fine-tuning), please make sure to remove the `--extract_ema` flag." | ||
| ) | ||
| for key in keys: | ||
| if key.startswith("model.diffusion_model"): | ||
| flat_ema_key = "model_ema." + "".join(key.split(".")[1:]) | ||
| unet_state_dict[key.replace(unet_key, "")] = checkpoint.pop(flat_ema_key) | ||
| else: | ||
| if sum(k.startswith("model_ema") for k in keys) > 100: | ||
| # extract state_dict for UNet | ||
| unet_state_dict = {} | ||
| keys = list(checkpoint.keys()) | ||
|
|
||
| if controlnet: | ||
| unet_key = "control_model." | ||
| else: | ||
| unet_key = "model.diffusion_model." | ||
|
|
||
| # at least a 100 parameters have to start with `model_ema` in order for the checkpoint to be EMA | ||
| if sum(k.startswith("model_ema") for k in keys) > 100 and extract_ema: | ||
| print(f"Checkpoint {path} has both EMA and non-EMA weights.") | ||
| print( | ||
| "In this conversion only the non-EMA weights are extracted. If you want to instead extract the EMA" | ||
| " weights (usually better for inference), please make sure to add the `--extract_ema` flag." | ||
| "In this conversion only the EMA weights are extracted. If you want to instead extract the non-EMA" | ||
| " weights (useful to continue fine-tuning), please make sure to remove the `--extract_ema` flag." | ||
| ) | ||
| for key in keys: | ||
| if key.startswith("model.diffusion_model"): | ||
| flat_ema_key = "model_ema." + "".join(key.split(".")[1:]) | ||
| unet_state_dict[key.replace(unet_key, "")] = checkpoint.pop(flat_ema_key) | ||
| else: | ||
| if sum(k.startswith("model_ema") for k in keys) > 100: | ||
| print( | ||
| "In this conversion only the non-EMA weights are extracted. If you want to instead extract the EMA" | ||
| " weights (usually better for inference), please make sure to add the `--extract_ema` flag." | ||
| ) | ||
|
|
||
| for key in keys: | ||
| if key.startswith(unet_key): | ||
| unet_state_dict[key.replace(unet_key, "")] = checkpoint.pop(key) | ||
| for key in keys: | ||
| if key.startswith(unet_key): | ||
| unet_state_dict[key.replace(unet_key, "")] = checkpoint.pop(key) | ||
|
|
||
| new_checkpoint = {} | ||
|
|
||
|
|
@@ -956,17 +961,42 @@ def stable_unclip_image_noising_components( | |
|
|
||
|
|
||
| def convert_controlnet_checkpoint( | ||
| checkpoint, original_config, checkpoint_path, image_size, upcast_attention, extract_ema | ||
| checkpoint, | ||
| original_config, | ||
| checkpoint_path, | ||
| image_size, | ||
| upcast_attention, | ||
| extract_ema, | ||
| use_linear_projection=None, | ||
| cross_attention_dim=None, | ||
| ): | ||
| ctrlnet_config = create_unet_diffusers_config(original_config, image_size=image_size, controlnet=True) | ||
| ctrlnet_config["upcast_attention"] = upcast_attention | ||
|
|
||
| ctrlnet_config.pop("sample_size") | ||
|
|
||
| if use_linear_projection is not None: | ||
| ctrlnet_config["use_linear_projection"] = use_linear_projection | ||
|
|
||
| if cross_attention_dim is not None: | ||
| ctrlnet_config["cross_attention_dim"] = cross_attention_dim | ||
|
|
||
| controlnet_model = ControlNetModel(**ctrlnet_config) | ||
|
|
||
| # Some controlnet ckpt files are distributed independently from the rest of the | ||
| # model components i.e. https://huggingface.co/thibaud/controlnet-sd21/ | ||
| if "time_embed.0.weight" in checkpoint: | ||
| skip_extract_state_dict = True | ||
| else: | ||
| skip_extract_state_dict = False | ||
|
|
||
|
Comment on lines
+986
to
+992
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the controlnet case, we can manually perform the check if this is the root level state dict |
||
| converted_ctrl_checkpoint = convert_ldm_unet_checkpoint( | ||
| checkpoint, ctrlnet_config, path=checkpoint_path, extract_ema=extract_ema, controlnet=True | ||
| checkpoint, | ||
| ctrlnet_config, | ||
| path=checkpoint_path, | ||
| extract_ema=extract_ema, | ||
| controlnet=True, | ||
| skip_extract_state_dict=skip_extract_state_dict, | ||
| ) | ||
|
|
||
| controlnet_model.load_state_dict(converted_ctrl_checkpoint) | ||
|
|
@@ -1344,6 +1374,8 @@ def download_controlnet_from_original_ckpt( | |
| upcast_attention: Optional[bool] = None, | ||
| device: str = None, | ||
| from_safetensors: bool = False, | ||
| use_linear_projection: Optional[bool] = None, | ||
| cross_attention_dim: Optional[bool] = None, | ||
| ) -> DiffusionPipeline: | ||
| if not is_omegaconf_available(): | ||
| raise ValueError(BACKENDS_MAPPING["omegaconf"][1]) | ||
|
|
@@ -1381,7 +1413,14 @@ def download_controlnet_from_original_ckpt( | |
| raise ValueError("`control_stage_config` not present in original config") | ||
|
|
||
| controlnet_model = convert_controlnet_checkpoint( | ||
| checkpoint, original_config, checkpoint_path, image_size, upcast_attention, extract_ema | ||
| checkpoint, | ||
| original_config, | ||
| checkpoint_path, | ||
| image_size, | ||
| upcast_attention, | ||
| extract_ema, | ||
| use_linear_projection=use_linear_projection, | ||
| cross_attention_dim=cross_attention_dim, | ||
| ) | ||
|
|
||
| return controlnet_model | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are overrides for these two configs which aren't correctly set for the config file https://huggingface.co/thibaud/controlnet-sd21/blob/main/control_v11p_sd21_openpose.yaml