|
| 1 | +""" |
| 2 | +A script to convert Stable Diffusion 3.5 ControlNet checkpoints to the Diffusers format. |
| 3 | +
|
| 4 | +Example: |
| 5 | + Convert a SD3.5 ControlNet checkpoint to Diffusers format using local file: |
| 6 | + ```bash |
| 7 | + python scripts/convert_sd3_controlnet_to_diffusers.py \ |
| 8 | + --checkpoint_path "path/to/local/sd3.5_large_controlnet_canny.safetensors" \ |
| 9 | + --output_path "output/sd35-controlnet-canny" \ |
| 10 | + --dtype "fp16" # optional, defaults to fp32 |
| 11 | + ``` |
| 12 | +
|
| 13 | + Or download and convert from HuggingFace repository: |
| 14 | + ```bash |
| 15 | + python scripts/convert_sd3_controlnet_to_diffusers.py \ |
| 16 | + --original_state_dict_repo_id "stabilityai/stable-diffusion-3.5-controlnets" \ |
| 17 | + --filename "sd3.5_large_controlnet_canny.safetensors" \ |
| 18 | + --output_path "/raid/yiyi/sd35-controlnet-canny-diffusers" \ |
| 19 | + --dtype "fp32" # optional, defaults to fp32 |
| 20 | + ``` |
| 21 | +
|
| 22 | +Note: |
| 23 | + The script supports the following ControlNet types from SD3.5: |
| 24 | + - Canny edge detection |
| 25 | + - Depth estimation |
| 26 | + - Blur detection |
| 27 | +
|
| 28 | + The checkpoint files can be downloaded from: |
| 29 | + https://huggingface.co/stabilityai/stable-diffusion-3.5-controlnets |
| 30 | +""" |
| 31 | + |
| 32 | +import argparse |
| 33 | + |
| 34 | +import safetensors.torch |
| 35 | +import torch |
| 36 | +from huggingface_hub import hf_hub_download |
| 37 | + |
| 38 | +from diffusers import SD3ControlNetModel |
| 39 | + |
| 40 | + |
| 41 | +parser = argparse.ArgumentParser() |
| 42 | +parser.add_argument("--checkpoint_path", type=str, default=None, help="Path to local checkpoint file") |
| 43 | +parser.add_argument( |
| 44 | + "--original_state_dict_repo_id", type=str, default=None, help="HuggingFace repo ID containing the checkpoint" |
| 45 | +) |
| 46 | +parser.add_argument("--filename", type=str, default=None, help="Filename of the checkpoint in the HF repo") |
| 47 | +parser.add_argument("--output_path", type=str, required=True, help="Path to save the converted model") |
| 48 | +parser.add_argument( |
| 49 | + "--dtype", type=str, default="fp32", help="Data type for the converted model (fp16, bf16, or fp32)" |
| 50 | +) |
| 51 | + |
| 52 | +args = parser.parse_args() |
| 53 | + |
| 54 | + |
| 55 | +def load_original_checkpoint(args): |
| 56 | + if args.original_state_dict_repo_id is not None: |
| 57 | + if args.filename is None: |
| 58 | + raise ValueError("When using `original_state_dict_repo_id`, `filename` must also be specified") |
| 59 | + print(f"Downloading checkpoint from {args.original_state_dict_repo_id}/{args.filename}") |
| 60 | + ckpt_path = hf_hub_download(repo_id=args.original_state_dict_repo_id, filename=args.filename) |
| 61 | + elif args.checkpoint_path is not None: |
| 62 | + print(f"Loading checkpoint from local path: {args.checkpoint_path}") |
| 63 | + ckpt_path = args.checkpoint_path |
| 64 | + else: |
| 65 | + raise ValueError("Please provide either `original_state_dict_repo_id` or a local `checkpoint_path`") |
| 66 | + |
| 67 | + original_state_dict = safetensors.torch.load_file(ckpt_path) |
| 68 | + return original_state_dict |
| 69 | + |
| 70 | + |
| 71 | +def convert_sd3_controlnet_checkpoint_to_diffusers(original_state_dict): |
| 72 | + converted_state_dict = {} |
| 73 | + |
| 74 | + # Direct mappings for controlnet blocks |
| 75 | + for i in range(19): # 19 controlnet blocks |
| 76 | + converted_state_dict[f"controlnet_blocks.{i}.weight"] = original_state_dict[f"controlnet_blocks.{i}.weight"] |
| 77 | + converted_state_dict[f"controlnet_blocks.{i}.bias"] = original_state_dict[f"controlnet_blocks.{i}.bias"] |
| 78 | + |
| 79 | + # Positional embeddings |
| 80 | + converted_state_dict["pos_embed_input.proj.weight"] = original_state_dict["pos_embed_input.proj.weight"] |
| 81 | + converted_state_dict["pos_embed_input.proj.bias"] = original_state_dict["pos_embed_input.proj.bias"] |
| 82 | + |
| 83 | + # Time and text embeddings |
| 84 | + time_text_mappings = { |
| 85 | + "time_text_embed.timestep_embedder.linear_1.weight": "time_text_embed.timestep_embedder.linear_1.weight", |
| 86 | + "time_text_embed.timestep_embedder.linear_1.bias": "time_text_embed.timestep_embedder.linear_1.bias", |
| 87 | + "time_text_embed.timestep_embedder.linear_2.weight": "time_text_embed.timestep_embedder.linear_2.weight", |
| 88 | + "time_text_embed.timestep_embedder.linear_2.bias": "time_text_embed.timestep_embedder.linear_2.bias", |
| 89 | + "time_text_embed.text_embedder.linear_1.weight": "time_text_embed.text_embedder.linear_1.weight", |
| 90 | + "time_text_embed.text_embedder.linear_1.bias": "time_text_embed.text_embedder.linear_1.bias", |
| 91 | + "time_text_embed.text_embedder.linear_2.weight": "time_text_embed.text_embedder.linear_2.weight", |
| 92 | + "time_text_embed.text_embedder.linear_2.bias": "time_text_embed.text_embedder.linear_2.bias", |
| 93 | + } |
| 94 | + |
| 95 | + for new_key, old_key in time_text_mappings.items(): |
| 96 | + if old_key in original_state_dict: |
| 97 | + converted_state_dict[new_key] = original_state_dict[old_key] |
| 98 | + |
| 99 | + # Transformer blocks |
| 100 | + for i in range(19): |
| 101 | + # Split QKV into separate Q, K, V |
| 102 | + qkv_weight = original_state_dict[f"transformer_blocks.{i}.attn.qkv.weight"] |
| 103 | + qkv_bias = original_state_dict[f"transformer_blocks.{i}.attn.qkv.bias"] |
| 104 | + q, k, v = torch.chunk(qkv_weight, 3, dim=0) |
| 105 | + q_bias, k_bias, v_bias = torch.chunk(qkv_bias, 3, dim=0) |
| 106 | + |
| 107 | + block_mappings = { |
| 108 | + f"transformer_blocks.{i}.attn.to_q.weight": q, |
| 109 | + f"transformer_blocks.{i}.attn.to_q.bias": q_bias, |
| 110 | + f"transformer_blocks.{i}.attn.to_k.weight": k, |
| 111 | + f"transformer_blocks.{i}.attn.to_k.bias": k_bias, |
| 112 | + f"transformer_blocks.{i}.attn.to_v.weight": v, |
| 113 | + f"transformer_blocks.{i}.attn.to_v.bias": v_bias, |
| 114 | + # Output projections |
| 115 | + f"transformer_blocks.{i}.attn.to_out.0.weight": original_state_dict[ |
| 116 | + f"transformer_blocks.{i}.attn.proj.weight" |
| 117 | + ], |
| 118 | + f"transformer_blocks.{i}.attn.to_out.0.bias": original_state_dict[ |
| 119 | + f"transformer_blocks.{i}.attn.proj.bias" |
| 120 | + ], |
| 121 | + # Feed forward |
| 122 | + f"transformer_blocks.{i}.ff.net.0.proj.weight": original_state_dict[ |
| 123 | + f"transformer_blocks.{i}.mlp.fc1.weight" |
| 124 | + ], |
| 125 | + f"transformer_blocks.{i}.ff.net.0.proj.bias": original_state_dict[f"transformer_blocks.{i}.mlp.fc1.bias"], |
| 126 | + f"transformer_blocks.{i}.ff.net.2.weight": original_state_dict[f"transformer_blocks.{i}.mlp.fc2.weight"], |
| 127 | + f"transformer_blocks.{i}.ff.net.2.bias": original_state_dict[f"transformer_blocks.{i}.mlp.fc2.bias"], |
| 128 | + # Norms |
| 129 | + f"transformer_blocks.{i}.norm1.linear.weight": original_state_dict[ |
| 130 | + f"transformer_blocks.{i}.adaLN_modulation.1.weight" |
| 131 | + ], |
| 132 | + f"transformer_blocks.{i}.norm1.linear.bias": original_state_dict[ |
| 133 | + f"transformer_blocks.{i}.adaLN_modulation.1.bias" |
| 134 | + ], |
| 135 | + } |
| 136 | + converted_state_dict.update(block_mappings) |
| 137 | + |
| 138 | + return converted_state_dict |
| 139 | + |
| 140 | + |
| 141 | +def main(args): |
| 142 | + original_ckpt = load_original_checkpoint(args) |
| 143 | + original_dtype = next(iter(original_ckpt.values())).dtype |
| 144 | + |
| 145 | + # Initialize dtype with fp32 as default |
| 146 | + if args.dtype == "fp16": |
| 147 | + dtype = torch.float16 |
| 148 | + elif args.dtype == "bf16": |
| 149 | + dtype = torch.bfloat16 |
| 150 | + elif args.dtype == "fp32": |
| 151 | + dtype = torch.float32 |
| 152 | + else: |
| 153 | + raise ValueError(f"Unsupported dtype: {args.dtype}. Must be one of: fp16, bf16, fp32") |
| 154 | + |
| 155 | + if dtype != original_dtype: |
| 156 | + print( |
| 157 | + f"Converting checkpoint from {original_dtype} to {dtype}. This can lead to unexpected results, proceed with caution." |
| 158 | + ) |
| 159 | + |
| 160 | + converted_controlnet_state_dict = convert_sd3_controlnet_checkpoint_to_diffusers(original_ckpt) |
| 161 | + |
| 162 | + controlnet = SD3ControlNetModel( |
| 163 | + patch_size=2, |
| 164 | + in_channels=16, |
| 165 | + num_layers=19, |
| 166 | + attention_head_dim=64, |
| 167 | + num_attention_heads=38, |
| 168 | + joint_attention_dim=None, |
| 169 | + caption_projection_dim=2048, |
| 170 | + pooled_projection_dim=2048, |
| 171 | + out_channels=16, |
| 172 | + pos_embed_max_size=None, |
| 173 | + pos_embed_type=None, |
| 174 | + use_pos_embed=False, |
| 175 | + force_zeros_for_pooled_projection=False, |
| 176 | + ) |
| 177 | + |
| 178 | + controlnet.load_state_dict(converted_controlnet_state_dict, strict=True) |
| 179 | + |
| 180 | + print(f"Saving SD3 ControlNet in Diffusers format in {args.output_path}.") |
| 181 | + controlnet.to(dtype).save_pretrained(args.output_path) |
| 182 | + |
| 183 | + |
| 184 | +if __name__ == "__main__": |
| 185 | + main(args) |
0 commit comments