comfyanonymous commited on
Commit
e8b81d4
1 Parent(s): 0985cf7

Create convert.py

Browse files
Files changed (1) hide show
  1. convert.py +50 -0
convert.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import safetensors.torch
3
+ from transformers import T5Tokenizer, T5EncoderModel
4
+
5
+ #https://huggingface.co/Laxhar/Freeway_Animation_HunYuan_Demo/blob/main/freeway_demo_ema_model/mp_rank_00_model_states.pt
6
+ input_diffusion = "mp_rank_00_model_states.pt"
7
+
8
+ #https://huggingface.co/Tencent-Hunyuan/HunyuanDiT-v1.2/blob/main/t2i/clip_text_encoder/pytorch_model.bin
9
+ input_bert = "pytorch_model.bin"
10
+
11
+ #https://huggingface.co/stabilityai/sdxl-vae/blob/main/sdxl_vae.safetensors
12
+ # or
13
+ #https://huggingface.co/madebyollin/sdxl-vae-fp16-fix/blob/main/sdxl_vae.safetensors
14
+ input_vae = "sdxl_vae.safetensors"
15
+
16
+ output = "freeway_animation_demo_hunyuan_dit.safetensors"
17
+
18
+
19
+
20
+ mt5 = T5EncoderModel.from_pretrained("google/mt5-xl").cuda()
21
+ tokenizer = T5Tokenizer.from_pretrained("google/mt5-xl")
22
+
23
+ sp_model = torch.ByteTensor(list(tokenizer.sp_model.serialized_model_proto()))
24
+ t5_sd = mt5.state_dict()
25
+
26
+ out_sd = {}
27
+
28
+ out_sd["text_encoders.mt5xl.spiece_model"] = sp_model
29
+
30
+ for k in t5_sd:
31
+ out_sd["text_encoders.mt5xl.transformer.{}".format(k)] = t5_sd[k].half()
32
+
33
+ bert_sd = torch.load(input_bert, weights_only=True)
34
+ for k in bert_sd:
35
+ if not k.startswith("visual."):
36
+ out_sd["text_encoders.hydit_clip.transformer.{}".format(k)] = bert_sd[k].half()
37
+
38
+ del bert_sd, mt5, t5_sd
39
+
40
+ hydit = torch.load(input_diffusion, weights_only=False)['ema']
41
+ for k in hydit:
42
+ out_sd["model.{}".format(k)] = hydit[k].half()
43
+
44
+
45
+ vae_sd = safetensors.torch.load_file(input_vae)
46
+
47
+ for k in vae_sd:
48
+ out_sd["vae.{}".format(k)] = vae_sd[k].half()
49
+
50
+ safetensors.torch.save_file(out_sd, output)