|
| 1 | +import argparse |
| 2 | +from contextlib import nullcontext |
| 3 | + |
| 4 | +import torch |
| 5 | +from accelerate import init_empty_weights |
| 6 | +from safetensors.torch import load_file |
| 7 | + |
| 8 | +# from transformers import T5EncoderModel, T5Tokenizer |
| 9 | +from diffusers import MochiTransformer3DModel |
| 10 | +from diffusers.utils.import_utils import is_accelerate_available |
| 11 | + |
| 12 | + |
| 13 | +CTX = init_empty_weights if is_accelerate_available else nullcontext |
| 14 | + |
| 15 | +TOKENIZER_MAX_LENGTH = 256 |
| 16 | + |
| 17 | +parser = argparse.ArgumentParser() |
| 18 | +parser.add_argument("--transformer_checkpoint_path", default=None, type=str) |
| 19 | +# parser.add_argument("--vae_checkpoint_path", default=None, type=str) |
| 20 | +parser.add_argument("--output_path", required=True, type=str) |
| 21 | +parser.add_argument("--push_to_hub", action="store_true", default=False, help="Whether to push to HF Hub after saving") |
| 22 | +parser.add_argument("--text_encoder_cache_dir", type=str, default=None, help="Path to text encoder cache directory") |
| 23 | +parser.add_argument("--dtype", type=str, default=None) |
| 24 | + |
| 25 | +args = parser.parse_args() |
| 26 | + |
| 27 | + |
| 28 | +# This is specific to `AdaLayerNormContinuous`: |
| 29 | +# Diffusers implementation split the linear projection into the scale, shift while Mochi split it into shift, scale |
| 30 | +def swap_scale_shift(weight, dim): |
| 31 | + shift, scale = weight.chunk(2, dim=0) |
| 32 | + new_weight = torch.cat([scale, shift], dim=0) |
| 33 | + return new_weight |
| 34 | + |
| 35 | + |
| 36 | +def convert_mochi_transformer_checkpoint_to_diffusers(ckpt_path): |
| 37 | + original_state_dict = load_file(ckpt_path, device="cpu") |
| 38 | + new_state_dict = {} |
| 39 | + |
| 40 | + # Convert patch_embed |
| 41 | + new_state_dict["patch_embed.proj.weight"] = original_state_dict.pop("x_embedder.proj.weight") |
| 42 | + new_state_dict["patch_embed.proj.bias"] = original_state_dict.pop("x_embedder.proj.bias") |
| 43 | + |
| 44 | + # Convert time_embed |
| 45 | + new_state_dict["time_embed.timestep_embedder.linear_1.weight"] = original_state_dict.pop("t_embedder.mlp.0.weight") |
| 46 | + new_state_dict["time_embed.timestep_embedder.linear_1.bias"] = original_state_dict.pop("t_embedder.mlp.0.bias") |
| 47 | + new_state_dict["time_embed.timestep_embedder.linear_2.weight"] = original_state_dict.pop("t_embedder.mlp.2.weight") |
| 48 | + new_state_dict["time_embed.timestep_embedder.linear_2.bias"] = original_state_dict.pop("t_embedder.mlp.2.bias") |
| 49 | + new_state_dict["time_embed.pooler.to_kv.weight"] = original_state_dict.pop("t5_y_embedder.to_kv.weight") |
| 50 | + new_state_dict["time_embed.pooler.to_kv.bias"] = original_state_dict.pop("t5_y_embedder.to_kv.bias") |
| 51 | + new_state_dict["time_embed.pooler.to_q.weight"] = original_state_dict.pop("t5_y_embedder.to_q.weight") |
| 52 | + new_state_dict["time_embed.pooler.to_q.bias"] = original_state_dict.pop("t5_y_embedder.to_q.bias") |
| 53 | + new_state_dict["time_embed.pooler.to_out.weight"] = original_state_dict.pop("t5_y_embedder.to_out.weight") |
| 54 | + new_state_dict["time_embed.pooler.to_out.bias"] = original_state_dict.pop("t5_y_embedder.to_out.bias") |
| 55 | + new_state_dict["time_embed.caption_proj.weight"] = original_state_dict.pop("t5_yproj.weight") |
| 56 | + new_state_dict["time_embed.caption_proj.bias"] = original_state_dict.pop("t5_yproj.bias") |
| 57 | + |
| 58 | + # Convert transformer blocks |
| 59 | + num_layers = 48 |
| 60 | + for i in range(num_layers): |
| 61 | + block_prefix = f"transformer_blocks.{i}." |
| 62 | + old_prefix = f"blocks.{i}." |
| 63 | + |
| 64 | + # norm1 |
| 65 | + new_state_dict[block_prefix + "norm1.linear.weight"] = original_state_dict.pop(old_prefix + "mod_x.weight") |
| 66 | + new_state_dict[block_prefix + "norm1.linear.bias"] = original_state_dict.pop(old_prefix + "mod_x.bias") |
| 67 | + if i < num_layers - 1: |
| 68 | + new_state_dict[block_prefix + "norm1_context.linear.weight"] = original_state_dict.pop( |
| 69 | + old_prefix + "mod_y.weight" |
| 70 | + ) |
| 71 | + new_state_dict[block_prefix + "norm1_context.linear.bias"] = original_state_dict.pop( |
| 72 | + old_prefix + "mod_y.bias" |
| 73 | + ) |
| 74 | + else: |
| 75 | + new_state_dict[block_prefix + "norm1_context.linear_1.weight"] = original_state_dict.pop( |
| 76 | + old_prefix + "mod_y.weight" |
| 77 | + ) |
| 78 | + new_state_dict[block_prefix + "norm1_context.linear_1.bias"] = original_state_dict.pop( |
| 79 | + old_prefix + "mod_y.bias" |
| 80 | + ) |
| 81 | + |
| 82 | + # Visual attention |
| 83 | + qkv_weight = original_state_dict.pop(old_prefix + "attn.qkv_x.weight") |
| 84 | + q, k, v = qkv_weight.chunk(3, dim=0) |
| 85 | + |
| 86 | + new_state_dict[block_prefix + "attn1.to_q.weight"] = q |
| 87 | + new_state_dict[block_prefix + "attn1.to_k.weight"] = k |
| 88 | + new_state_dict[block_prefix + "attn1.to_v.weight"] = v |
| 89 | + new_state_dict[block_prefix + "attn1.norm_q.weight"] = original_state_dict.pop( |
| 90 | + old_prefix + "attn.q_norm_x.weight" |
| 91 | + ) |
| 92 | + new_state_dict[block_prefix + "attn1.norm_k.weight"] = original_state_dict.pop( |
| 93 | + old_prefix + "attn.k_norm_x.weight" |
| 94 | + ) |
| 95 | + new_state_dict[block_prefix + "attn1.to_out.0.weight"] = original_state_dict.pop( |
| 96 | + old_prefix + "attn.proj_x.weight" |
| 97 | + ) |
| 98 | + new_state_dict[block_prefix + "attn1.to_out.0.bias"] = original_state_dict.pop(old_prefix + "attn.proj_x.bias") |
| 99 | + |
| 100 | + # Context attention |
| 101 | + qkv_weight = original_state_dict.pop(old_prefix + "attn.qkv_y.weight") |
| 102 | + q, k, v = qkv_weight.chunk(3, dim=0) |
| 103 | + |
| 104 | + new_state_dict[block_prefix + "attn1.add_q_proj.weight"] = q |
| 105 | + new_state_dict[block_prefix + "attn1.add_k_proj.weight"] = k |
| 106 | + new_state_dict[block_prefix + "attn1.add_v_proj.weight"] = v |
| 107 | + new_state_dict[block_prefix + "attn1.norm_added_q.weight"] = original_state_dict.pop( |
| 108 | + old_prefix + "attn.q_norm_y.weight" |
| 109 | + ) |
| 110 | + new_state_dict[block_prefix + "attn1.norm_added_k.weight"] = original_state_dict.pop( |
| 111 | + old_prefix + "attn.k_norm_y.weight" |
| 112 | + ) |
| 113 | + if i < num_layers - 1: |
| 114 | + new_state_dict[block_prefix + "attn1.to_add_out.weight"] = original_state_dict.pop( |
| 115 | + old_prefix + "attn.proj_y.weight" |
| 116 | + ) |
| 117 | + new_state_dict[block_prefix + "attn1.to_add_out.bias"] = original_state_dict.pop( |
| 118 | + old_prefix + "attn.proj_y.bias" |
| 119 | + ) |
| 120 | + |
| 121 | + # MLP |
| 122 | + new_state_dict[block_prefix + "ff.net.0.proj.weight"] = original_state_dict.pop(old_prefix + "mlp_x.w1.weight") |
| 123 | + new_state_dict[block_prefix + "ff.net.2.weight"] = original_state_dict.pop(old_prefix + "mlp_x.w2.weight") |
| 124 | + if i < num_layers - 1: |
| 125 | + new_state_dict[block_prefix + "ff_context.net.0.proj.weight"] = original_state_dict.pop( |
| 126 | + old_prefix + "mlp_y.w1.weight" |
| 127 | + ) |
| 128 | + new_state_dict[block_prefix + "ff_context.net.2.weight"] = original_state_dict.pop( |
| 129 | + old_prefix + "mlp_y.w2.weight" |
| 130 | + ) |
| 131 | + |
| 132 | + # Output layers |
| 133 | + new_state_dict["norm_out.linear.weight"] = swap_scale_shift(original_state_dict.pop("final_layer.mod.weight"), dim=0) |
| 134 | + new_state_dict["norm_out.linear.bias"] = swap_scale_shift(original_state_dict.pop("final_layer.mod.bias"), dim=0) |
| 135 | + new_state_dict["proj_out.weight"] = original_state_dict.pop("final_layer.linear.weight") |
| 136 | + new_state_dict["proj_out.bias"] = original_state_dict.pop("final_layer.linear.bias") |
| 137 | + |
| 138 | + new_state_dict["pos_frequencies"] = original_state_dict.pop("pos_frequencies") |
| 139 | + |
| 140 | + print("Remaining Keys:", original_state_dict.keys()) |
| 141 | + |
| 142 | + return new_state_dict |
| 143 | + |
| 144 | + |
| 145 | +# def convert_mochi_vae_checkpoint_to_diffusers(ckpt_path, vae_config): |
| 146 | +# original_state_dict = torch.load(ckpt_path, map_location="cpu")["state_dict"] |
| 147 | +# return convert_ldm_vae_checkpoint(original_state_dict, vae_config) |
| 148 | + |
| 149 | + |
| 150 | +def main(args): |
| 151 | + if args.dtype is None: |
| 152 | + dtype = None |
| 153 | + if args.dtype == "fp16": |
| 154 | + dtype = torch.float16 |
| 155 | + elif args.dtype == "bf16": |
| 156 | + dtype = torch.bfloat16 |
| 157 | + elif args.dtype == "fp32": |
| 158 | + dtype = torch.float32 |
| 159 | + else: |
| 160 | + raise ValueError(f"Unsupported dtype: {args.dtype}") |
| 161 | + |
| 162 | + transformer = None |
| 163 | + # vae = None |
| 164 | + |
| 165 | + if args.transformer_checkpoint_path is not None: |
| 166 | + converted_transformer_state_dict = convert_mochi_transformer_checkpoint_to_diffusers( |
| 167 | + args.transformer_checkpoint_path |
| 168 | + ) |
| 169 | + transformer = MochiTransformer3DModel() |
| 170 | + transformer.load_state_dict(converted_transformer_state_dict, strict=True) |
| 171 | + if dtype is not None: |
| 172 | + # Original checkpoint data type will be preserved |
| 173 | + transformer = transformer.to(dtype=dtype) |
| 174 | + |
| 175 | + # text_encoder_id = "google/t5-v1_1-xxl" |
| 176 | + # tokenizer = T5Tokenizer.from_pretrained(text_encoder_id, model_max_length=TOKENIZER_MAX_LENGTH) |
| 177 | + # text_encoder = T5EncoderModel.from_pretrained(text_encoder_id, cache_dir=args.text_encoder_cache_dir) |
| 178 | + |
| 179 | + # # Apparently, the conversion does not work anymore without this :shrug: |
| 180 | + # for param in text_encoder.parameters(): |
| 181 | + # param.data = param.data.contiguous() |
| 182 | + |
| 183 | + transformer.save_pretrained("/raid/aryan/mochi-diffusers", subfolder="transformer") |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + main(args) |
0 commit comments