|
| 1 | +import argparse |
| 2 | +import os |
| 3 | + |
| 4 | +import torch |
| 5 | +from safetensors.torch import load_file |
| 6 | +from transformers import AutoModel, AutoTokenizer |
| 7 | + |
| 8 | +from diffusers import AutoencoderKL, FlowMatchEulerDiscreteScheduler, LuminaNextDiT2DModel, LuminaText2ImgPipeline |
| 9 | + |
| 10 | + |
| 11 | +def main(args): |
| 12 | + # checkpoint from https://huggingface.co/Alpha-VLLM/Lumina-Next-SFT or https://huggingface.co/Alpha-VLLM/Lumina-Next-T2I |
| 13 | + all_sd = load_file(args.origin_ckpt_path, device="cpu") |
| 14 | + converted_state_dict = {} |
| 15 | + # pad token |
| 16 | + converted_state_dict["pad_token"] = all_sd["pad_token"] |
| 17 | + |
| 18 | + # patch embed |
| 19 | + converted_state_dict["patch_embedder.weight"] = all_sd["x_embedder.weight"] |
| 20 | + converted_state_dict["patch_embedder.bias"] = all_sd["x_embedder.bias"] |
| 21 | + |
| 22 | + # time and caption embed |
| 23 | + converted_state_dict["time_caption_embed.timestep_embedder.linear_1.weight"] = all_sd["t_embedder.mlp.0.weight"] |
| 24 | + converted_state_dict["time_caption_embed.timestep_embedder.linear_1.bias"] = all_sd["t_embedder.mlp.0.bias"] |
| 25 | + converted_state_dict["time_caption_embed.timestep_embedder.linear_2.weight"] = all_sd["t_embedder.mlp.2.weight"] |
| 26 | + converted_state_dict["time_caption_embed.timestep_embedder.linear_2.bias"] = all_sd["t_embedder.mlp.2.bias"] |
| 27 | + converted_state_dict["time_caption_embed.caption_embedder.0.weight"] = all_sd["cap_embedder.0.weight"] |
| 28 | + converted_state_dict["time_caption_embed.caption_embedder.0.bias"] = all_sd["cap_embedder.0.bias"] |
| 29 | + converted_state_dict["time_caption_embed.caption_embedder.1.weight"] = all_sd["cap_embedder.1.weight"] |
| 30 | + converted_state_dict["time_caption_embed.caption_embedder.1.bias"] = all_sd["cap_embedder.1.bias"] |
| 31 | + |
| 32 | + for i in range(24): |
| 33 | + # adaln |
| 34 | + converted_state_dict[f"layers.{i}.gate"] = all_sd[f"layers.{i}.attention.gate"] |
| 35 | + converted_state_dict[f"layers.{i}.adaLN_modulation.1.weight"] = all_sd[f"layers.{i}.adaLN_modulation.1.weight"] |
| 36 | + converted_state_dict[f"layers.{i}.adaLN_modulation.1.bias"] = all_sd[f"layers.{i}.adaLN_modulation.1.bias"] |
| 37 | + |
| 38 | + # qkv |
| 39 | + converted_state_dict[f"layers.{i}.attn1.to_q.weight"] = all_sd[f"layers.{i}.attention.wq.weight"] |
| 40 | + converted_state_dict[f"layers.{i}.attn1.to_k.weight"] = all_sd[f"layers.{i}.attention.wk.weight"] |
| 41 | + converted_state_dict[f"layers.{i}.attn1.to_v.weight"] = all_sd[f"layers.{i}.attention.wv.weight"] |
| 42 | + |
| 43 | + # cap |
| 44 | + converted_state_dict[f"layers.{i}.attn2.to_q.weight"] = all_sd[f"layers.{i}.attention.wq.weight"] |
| 45 | + converted_state_dict[f"layers.{i}.attn2.to_k.weight"] = all_sd[f"layers.{i}.attention.wk_y.weight"] |
| 46 | + converted_state_dict[f"layers.{i}.attn2.to_v.weight"] = all_sd[f"layers.{i}.attention.wv_y.weight"] |
| 47 | + |
| 48 | + # output |
| 49 | + converted_state_dict[f"layers.{i}.attn2.to_out.0.weight"] = all_sd[f"layers.{i}.attention.wo.weight"] |
| 50 | + |
| 51 | + # attention |
| 52 | + # qk norm |
| 53 | + converted_state_dict[f"layers.{i}.attn1.norm_q.weight"] = all_sd[f"layers.{i}.attention.q_norm.weight"] |
| 54 | + converted_state_dict[f"layers.{i}.attn1.norm_q.bias"] = all_sd[f"layers.{i}.attention.q_norm.bias"] |
| 55 | + |
| 56 | + converted_state_dict[f"layers.{i}.attn1.norm_k.weight"] = all_sd[f"layers.{i}.attention.k_norm.weight"] |
| 57 | + converted_state_dict[f"layers.{i}.attn1.norm_k.bias"] = all_sd[f"layers.{i}.attention.k_norm.bias"] |
| 58 | + |
| 59 | + converted_state_dict[f"layers.{i}.attn2.norm_q.weight"] = all_sd[f"layers.{i}.attention.q_norm.weight"] |
| 60 | + converted_state_dict[f"layers.{i}.attn2.norm_q.bias"] = all_sd[f"layers.{i}.attention.q_norm.bias"] |
| 61 | + |
| 62 | + converted_state_dict[f"layers.{i}.attn2.norm_k.weight"] = all_sd[f"layers.{i}.attention.ky_norm.weight"] |
| 63 | + converted_state_dict[f"layers.{i}.attn2.norm_k.bias"] = all_sd[f"layers.{i}.attention.ky_norm.bias"] |
| 64 | + |
| 65 | + # attention norm |
| 66 | + converted_state_dict[f"layers.{i}.attn_norm1.weight"] = all_sd[f"layers.{i}.attention_norm1.weight"] |
| 67 | + converted_state_dict[f"layers.{i}.attn_norm2.weight"] = all_sd[f"layers.{i}.attention_norm2.weight"] |
| 68 | + converted_state_dict[f"layers.{i}.norm1_context.weight"] = all_sd[f"layers.{i}.attention_y_norm.weight"] |
| 69 | + |
| 70 | + # feed forward |
| 71 | + converted_state_dict[f"layers.{i}.feed_forward.linear_1.weight"] = all_sd[f"layers.{i}.feed_forward.w1.weight"] |
| 72 | + converted_state_dict[f"layers.{i}.feed_forward.linear_2.weight"] = all_sd[f"layers.{i}.feed_forward.w2.weight"] |
| 73 | + converted_state_dict[f"layers.{i}.feed_forward.linear_3.weight"] = all_sd[f"layers.{i}.feed_forward.w3.weight"] |
| 74 | + |
| 75 | + # feed forward norm |
| 76 | + converted_state_dict[f"layers.{i}.ffn_norm1.weight"] = all_sd[f"layers.{i}.ffn_norm1.weight"] |
| 77 | + converted_state_dict[f"layers.{i}.ffn_norm2.weight"] = all_sd[f"layers.{i}.ffn_norm2.weight"] |
| 78 | + |
| 79 | + # final layer |
| 80 | + converted_state_dict["final_layer.linear.weight"] = all_sd["final_layer.linear.weight"] |
| 81 | + converted_state_dict["final_layer.linear.bias"] = all_sd["final_layer.linear.bias"] |
| 82 | + |
| 83 | + converted_state_dict["final_layer.adaLN_modulation.1.weight"] = all_sd["final_layer.adaLN_modulation.1.weight"] |
| 84 | + converted_state_dict["final_layer.adaLN_modulation.1.bias"] = all_sd["final_layer.adaLN_modulation.1.bias"] |
| 85 | + |
| 86 | + # Lumina-Next-SFT 2B |
| 87 | + transformer = LuminaNextDiT2DModel( |
| 88 | + sample_size=128, |
| 89 | + patch_size=2, |
| 90 | + in_channels=4, |
| 91 | + hidden_size=2304, |
| 92 | + num_layers=24, |
| 93 | + num_attention_heads=32, |
| 94 | + num_kv_heads=8, |
| 95 | + multiple_of=256, |
| 96 | + ffn_dim_multiplier=None, |
| 97 | + norm_eps=1e-5, |
| 98 | + learn_sigma=True, |
| 99 | + qk_norm=True, |
| 100 | + cross_attention_dim=2048, |
| 101 | + scaling_factor=1.0, |
| 102 | + ) |
| 103 | + transformer.load_state_dict(converted_state_dict, strict=True) |
| 104 | + |
| 105 | + num_model_params = sum(p.numel() for p in transformer.parameters()) |
| 106 | + print(f"Total number of transformer parameters: {num_model_params}") |
| 107 | + |
| 108 | + if args.only_transformer: |
| 109 | + transformer.save_pretrained(os.path.join(args.dump_path, "transformer")) |
| 110 | + else: |
| 111 | + scheduler = FlowMatchEulerDiscreteScheduler() |
| 112 | + |
| 113 | + vae = AutoencoderKL.from_pretrained("stabilityai/sdxl-vae", torch_dtype=torch.float32) |
| 114 | + |
| 115 | + tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b") |
| 116 | + text_encoder = AutoModel.from_pretrained("google/gemma-2b") |
| 117 | + |
| 118 | + pipeline = LuminaText2ImgPipeline( |
| 119 | + tokenizer=tokenizer, text_encoder=text_encoder, transformer=transformer, vae=vae, scheduler=scheduler |
| 120 | + ) |
| 121 | + pipeline.save_pretrained(args.dump_path) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + parser = argparse.ArgumentParser() |
| 126 | + |
| 127 | + parser.add_argument( |
| 128 | + "--origin_ckpt_path", default=None, type=str, required=False, help="Path to the checkpoint to convert." |
| 129 | + ) |
| 130 | + parser.add_argument( |
| 131 | + "--image_size", |
| 132 | + default=1024, |
| 133 | + type=int, |
| 134 | + choices=[256, 512, 1024], |
| 135 | + required=False, |
| 136 | + help="Image size of pretrained model, either 512 or 1024.", |
| 137 | + ) |
| 138 | + parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output pipeline.") |
| 139 | + parser.add_argument("--only_transformer", default=True, type=bool, required=True) |
| 140 | + |
| 141 | + args = parser.parse_args() |
| 142 | + main(args) |
0 commit comments