|
| 1 | +#!/usr/bin/env python |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +from contextlib import nullcontext |
| 6 | + |
| 7 | +import torch |
| 8 | +from accelerate import init_empty_weights |
| 9 | + |
| 10 | +from diffusers import ( |
| 11 | + SanaControlNetModel, |
| 12 | +) |
| 13 | +from diffusers.models.modeling_utils import load_model_dict_into_meta |
| 14 | +from diffusers.utils.import_utils import is_accelerate_available |
| 15 | + |
| 16 | + |
| 17 | +CTX = init_empty_weights if is_accelerate_available else nullcontext |
| 18 | + |
| 19 | + |
| 20 | +def main(args): |
| 21 | + file_path = args.orig_ckpt_path |
| 22 | + |
| 23 | + all_state_dict = torch.load(file_path, weights_only=True) |
| 24 | + state_dict = all_state_dict.pop("state_dict") |
| 25 | + converted_state_dict = {} |
| 26 | + |
| 27 | + # Patch embeddings. |
| 28 | + converted_state_dict["patch_embed.proj.weight"] = state_dict.pop("x_embedder.proj.weight") |
| 29 | + converted_state_dict["patch_embed.proj.bias"] = state_dict.pop("x_embedder.proj.bias") |
| 30 | + |
| 31 | + # Caption projection. |
| 32 | + converted_state_dict["caption_projection.linear_1.weight"] = state_dict.pop("y_embedder.y_proj.fc1.weight") |
| 33 | + converted_state_dict["caption_projection.linear_1.bias"] = state_dict.pop("y_embedder.y_proj.fc1.bias") |
| 34 | + converted_state_dict["caption_projection.linear_2.weight"] = state_dict.pop("y_embedder.y_proj.fc2.weight") |
| 35 | + converted_state_dict["caption_projection.linear_2.bias"] = state_dict.pop("y_embedder.y_proj.fc2.bias") |
| 36 | + |
| 37 | + # AdaLN-single LN |
| 38 | + converted_state_dict["time_embed.emb.timestep_embedder.linear_1.weight"] = state_dict.pop( |
| 39 | + "t_embedder.mlp.0.weight" |
| 40 | + ) |
| 41 | + converted_state_dict["time_embed.emb.timestep_embedder.linear_1.bias"] = state_dict.pop("t_embedder.mlp.0.bias") |
| 42 | + converted_state_dict["time_embed.emb.timestep_embedder.linear_2.weight"] = state_dict.pop( |
| 43 | + "t_embedder.mlp.2.weight" |
| 44 | + ) |
| 45 | + converted_state_dict["time_embed.emb.timestep_embedder.linear_2.bias"] = state_dict.pop("t_embedder.mlp.2.bias") |
| 46 | + |
| 47 | + # Shared norm. |
| 48 | + converted_state_dict["time_embed.linear.weight"] = state_dict.pop("t_block.1.weight") |
| 49 | + converted_state_dict["time_embed.linear.bias"] = state_dict.pop("t_block.1.bias") |
| 50 | + |
| 51 | + # y norm |
| 52 | + converted_state_dict["caption_norm.weight"] = state_dict.pop("attention_y_norm.weight") |
| 53 | + |
| 54 | + # Positional embedding interpolation scale. |
| 55 | + interpolation_scale = {512: None, 1024: None, 2048: 1.0, 4096: 2.0} |
| 56 | + |
| 57 | + # ControlNet Input Projection. |
| 58 | + converted_state_dict["input_block.weight"] = state_dict.pop("controlnet.0.before_proj.weight") |
| 59 | + converted_state_dict["input_block.bias"] = state_dict.pop("controlnet.0.before_proj.bias") |
| 60 | + |
| 61 | + for depth in range(7): |
| 62 | + # Transformer blocks. |
| 63 | + converted_state_dict[f"transformer_blocks.{depth}.scale_shift_table"] = state_dict.pop( |
| 64 | + f"controlnet.{depth}.copied_block.scale_shift_table" |
| 65 | + ) |
| 66 | + |
| 67 | + # Linear Attention is all you need 🤘 |
| 68 | + # Self attention. |
| 69 | + q, k, v = torch.chunk(state_dict.pop(f"controlnet.{depth}.copied_block.attn.qkv.weight"), 3, dim=0) |
| 70 | + converted_state_dict[f"transformer_blocks.{depth}.attn1.to_q.weight"] = q |
| 71 | + converted_state_dict[f"transformer_blocks.{depth}.attn1.to_k.weight"] = k |
| 72 | + converted_state_dict[f"transformer_blocks.{depth}.attn1.to_v.weight"] = v |
| 73 | + # Projection. |
| 74 | + converted_state_dict[f"transformer_blocks.{depth}.attn1.to_out.0.weight"] = state_dict.pop( |
| 75 | + f"controlnet.{depth}.copied_block.attn.proj.weight" |
| 76 | + ) |
| 77 | + converted_state_dict[f"transformer_blocks.{depth}.attn1.to_out.0.bias"] = state_dict.pop( |
| 78 | + f"controlnet.{depth}.copied_block.attn.proj.bias" |
| 79 | + ) |
| 80 | + |
| 81 | + # Feed-forward. |
| 82 | + converted_state_dict[f"transformer_blocks.{depth}.ff.conv_inverted.weight"] = state_dict.pop( |
| 83 | + f"controlnet.{depth}.copied_block.mlp.inverted_conv.conv.weight" |
| 84 | + ) |
| 85 | + converted_state_dict[f"transformer_blocks.{depth}.ff.conv_inverted.bias"] = state_dict.pop( |
| 86 | + f"controlnet.{depth}.copied_block.mlp.inverted_conv.conv.bias" |
| 87 | + ) |
| 88 | + converted_state_dict[f"transformer_blocks.{depth}.ff.conv_depth.weight"] = state_dict.pop( |
| 89 | + f"controlnet.{depth}.copied_block.mlp.depth_conv.conv.weight" |
| 90 | + ) |
| 91 | + converted_state_dict[f"transformer_blocks.{depth}.ff.conv_depth.bias"] = state_dict.pop( |
| 92 | + f"controlnet.{depth}.copied_block.mlp.depth_conv.conv.bias" |
| 93 | + ) |
| 94 | + converted_state_dict[f"transformer_blocks.{depth}.ff.conv_point.weight"] = state_dict.pop( |
| 95 | + f"controlnet.{depth}.copied_block.mlp.point_conv.conv.weight" |
| 96 | + ) |
| 97 | + |
| 98 | + # Cross-attention. |
| 99 | + q = state_dict.pop(f"controlnet.{depth}.copied_block.cross_attn.q_linear.weight") |
| 100 | + q_bias = state_dict.pop(f"controlnet.{depth}.copied_block.cross_attn.q_linear.bias") |
| 101 | + k, v = torch.chunk(state_dict.pop(f"controlnet.{depth}.copied_block.cross_attn.kv_linear.weight"), 2, dim=0) |
| 102 | + k_bias, v_bias = torch.chunk( |
| 103 | + state_dict.pop(f"controlnet.{depth}.copied_block.cross_attn.kv_linear.bias"), 2, dim=0 |
| 104 | + ) |
| 105 | + |
| 106 | + converted_state_dict[f"transformer_blocks.{depth}.attn2.to_q.weight"] = q |
| 107 | + converted_state_dict[f"transformer_blocks.{depth}.attn2.to_q.bias"] = q_bias |
| 108 | + converted_state_dict[f"transformer_blocks.{depth}.attn2.to_k.weight"] = k |
| 109 | + converted_state_dict[f"transformer_blocks.{depth}.attn2.to_k.bias"] = k_bias |
| 110 | + converted_state_dict[f"transformer_blocks.{depth}.attn2.to_v.weight"] = v |
| 111 | + converted_state_dict[f"transformer_blocks.{depth}.attn2.to_v.bias"] = v_bias |
| 112 | + |
| 113 | + converted_state_dict[f"transformer_blocks.{depth}.attn2.to_out.0.weight"] = state_dict.pop( |
| 114 | + f"controlnet.{depth}.copied_block.cross_attn.proj.weight" |
| 115 | + ) |
| 116 | + converted_state_dict[f"transformer_blocks.{depth}.attn2.to_out.0.bias"] = state_dict.pop( |
| 117 | + f"controlnet.{depth}.copied_block.cross_attn.proj.bias" |
| 118 | + ) |
| 119 | + |
| 120 | + # ControlNet After Projection |
| 121 | + converted_state_dict[f"controlnet_blocks.{depth}.weight"] = state_dict.pop( |
| 122 | + f"controlnet.{depth}.after_proj.weight" |
| 123 | + ) |
| 124 | + converted_state_dict[f"controlnet_blocks.{depth}.bias"] = state_dict.pop(f"controlnet.{depth}.after_proj.bias") |
| 125 | + |
| 126 | + # ControlNet |
| 127 | + with CTX(): |
| 128 | + controlnet = SanaControlNetModel( |
| 129 | + num_attention_heads=model_kwargs[args.model_type]["num_attention_heads"], |
| 130 | + attention_head_dim=model_kwargs[args.model_type]["attention_head_dim"], |
| 131 | + num_layers=model_kwargs[args.model_type]["num_layers"], |
| 132 | + num_cross_attention_heads=model_kwargs[args.model_type]["num_cross_attention_heads"], |
| 133 | + cross_attention_head_dim=model_kwargs[args.model_type]["cross_attention_head_dim"], |
| 134 | + cross_attention_dim=model_kwargs[args.model_type]["cross_attention_dim"], |
| 135 | + caption_channels=2304, |
| 136 | + sample_size=args.image_size // 32, |
| 137 | + interpolation_scale=interpolation_scale[args.image_size], |
| 138 | + ) |
| 139 | + |
| 140 | + if is_accelerate_available(): |
| 141 | + load_model_dict_into_meta(controlnet, converted_state_dict) |
| 142 | + else: |
| 143 | + controlnet.load_state_dict(converted_state_dict, strict=True, assign=True) |
| 144 | + |
| 145 | + num_model_params = sum(p.numel() for p in controlnet.parameters()) |
| 146 | + print(f"Total number of controlnet parameters: {num_model_params}") |
| 147 | + |
| 148 | + controlnet = controlnet.to(weight_dtype) |
| 149 | + controlnet.load_state_dict(converted_state_dict, strict=True) |
| 150 | + |
| 151 | + print(f"Saving Sana ControlNet in Diffusers format in {args.dump_path}.") |
| 152 | + controlnet.save_pretrained(args.dump_path) |
| 153 | + |
| 154 | + |
| 155 | +DTYPE_MAPPING = { |
| 156 | + "fp32": torch.float32, |
| 157 | + "fp16": torch.float16, |
| 158 | + "bf16": torch.bfloat16, |
| 159 | +} |
| 160 | + |
| 161 | +VARIANT_MAPPING = { |
| 162 | + "fp32": None, |
| 163 | + "fp16": "fp16", |
| 164 | + "bf16": "bf16", |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + parser = argparse.ArgumentParser() |
| 170 | + |
| 171 | + parser.add_argument( |
| 172 | + "--orig_ckpt_path", default=None, type=str, required=True, help="Path to the checkpoint to convert." |
| 173 | + ) |
| 174 | + parser.add_argument( |
| 175 | + "--image_size", |
| 176 | + default=1024, |
| 177 | + type=int, |
| 178 | + choices=[512, 1024, 2048, 4096], |
| 179 | + required=False, |
| 180 | + help="Image size of pretrained model, 512, 1024, 2048 or 4096.", |
| 181 | + ) |
| 182 | + parser.add_argument( |
| 183 | + "--model_type", |
| 184 | + default="SanaMS_1600M_P1_ControlNet_D7", |
| 185 | + type=str, |
| 186 | + choices=["SanaMS_1600M_P1_ControlNet_D7", "SanaMS_600M_P1_ControlNet_D7"], |
| 187 | + ) |
| 188 | + parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output pipeline.") |
| 189 | + parser.add_argument("--dtype", default="fp16", type=str, choices=["fp32", "fp16", "bf16"], help="Weight dtype.") |
| 190 | + |
| 191 | + args = parser.parse_args() |
| 192 | + |
| 193 | + model_kwargs = { |
| 194 | + "SanaMS_1600M_P1_ControlNet_D7": { |
| 195 | + "num_attention_heads": 70, |
| 196 | + "attention_head_dim": 32, |
| 197 | + "num_cross_attention_heads": 20, |
| 198 | + "cross_attention_head_dim": 112, |
| 199 | + "cross_attention_dim": 2240, |
| 200 | + "num_layers": 7, |
| 201 | + }, |
| 202 | + "SanaMS_600M_P1_ControlNet_D7": { |
| 203 | + "num_attention_heads": 36, |
| 204 | + "attention_head_dim": 32, |
| 205 | + "num_cross_attention_heads": 16, |
| 206 | + "cross_attention_head_dim": 72, |
| 207 | + "cross_attention_dim": 1152, |
| 208 | + "num_layers": 7, |
| 209 | + }, |
| 210 | + } |
| 211 | + |
| 212 | + device = "cuda" if torch.cuda.is_available() else "cpu" |
| 213 | + weight_dtype = DTYPE_MAPPING[args.dtype] |
| 214 | + variant = VARIANT_MAPPING[args.dtype] |
| 215 | + |
| 216 | + main(args) |
0 commit comments