bee-kake commited on
Commit
c5c0b58
1 Parent(s): f2d44c0

Upload gguf convert script

Browse files
nanollava_llm_convert.py ADDED
The diff for this file is too large to render. See raw diff
 
nanollava_mmproj_convert.py ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ from safetensors import safe_open
3
+ from transformers import AutoTokenizer
4
+ from pathlib import Path
5
+ import torch
6
+ import sys
7
+ import os
8
+
9
+ if 'NO_LOCAL_GGUF' not in os.environ:
10
+ print(str(Path(__file__).parent / 'gguf-py'))
11
+ sys.path.insert(1, str(Path(__file__).parent / 'gguf-py'))
12
+
13
+ from gguf import *
14
+
15
+
16
+ def k(raw_key: str, arch: str) -> str:
17
+ return raw_key.format(arch=arch)
18
+
19
+
20
+ parser = argparse.ArgumentParser()
21
+ parser.add_argument("--model", type=str, default="../nanoLLaVA")
22
+ parser.add_argument("--tokenizer", type=str, default="nanoLLaVA")
23
+ args = parser.parse_args()
24
+
25
+ tensors = safe_open(f'{args.model}/model.safetensors', framework="pt", device="cpu")
26
+
27
+ ### Vision encoder
28
+
29
+ ftype = 1 # fp16
30
+
31
+ fname_middle = "mmproj-"
32
+ has_text_encoder = False
33
+ has_llava_projector = True
34
+
35
+ fname_out = f"{args.model}/nanollava-mmproj-f16.gguf"
36
+ fout = GGUFWriter(fname_out, arch="clip")
37
+
38
+ fout.add_bool("clip.has_text_encoder", False)
39
+ fout.add_bool("clip.has_vision_encoder", True)
40
+ fout.add_bool("clip.has_llava_projector", True)
41
+ fout.add_file_type(ftype) # fp16
42
+
43
+ model_name = "qnguyen3/nanoLLaVA"
44
+ fout.add_name(model_name)
45
+ fout.add_description("image encoder for " + model_name)
46
+ fout.add_string("clip.projector_type", "mlp")
47
+
48
+ # vision model hparams
49
+ VISION = "clip.vision"
50
+ fout.add_uint32("clip.vision.image_size", 378)
51
+ fout.add_uint32("clip.vision.patch_size", 14)
52
+ fout.add_uint32(k(KEY_EMBEDDING_LENGTH, VISION), 1152)
53
+ fout.add_uint32(k(KEY_FEED_FORWARD_LENGTH, VISION), 4304)
54
+ fout.add_uint32("clip.vision.projection_dim", 2048)
55
+ fout.add_uint32(k(KEY_ATTENTION_HEAD_COUNT, VISION), 16)
56
+ fout.add_float32(k(KEY_ATTENTION_LAYERNORM_EPS, VISION), 1e-6)
57
+ fout.add_uint32(k(KEY_BLOCK_COUNT, VISION), 27 + 1)
58
+
59
+ fout.add_array("clip.vision.image_mean", [0.5, 0.5, 0.5])
60
+ fout.add_array("clip.vision.image_std", [0.5, 0.5, 0.5])
61
+ fout.add_bool("clip.use_gelu", True) # using regular GELU instead of quick
62
+
63
+ # vision projection
64
+ fout.add_tensor(
65
+ "mm.0.weight",
66
+ tensors.get_tensor("model.mm_projector.0.weight").to(
67
+ torch.float16
68
+ ).numpy().copy()
69
+ )
70
+ fout.add_tensor(
71
+ "mm.0.bias",
72
+ tensors.get_tensor("model.mm_projector.0.bias").to(torch.float32).numpy().copy(),
73
+ )
74
+ fout.add_tensor(
75
+ "mm.2.weight",
76
+ tensors.get_tensor("model.mm_projector.2.weight").to(
77
+ torch.float16
78
+ ).numpy().copy(),
79
+ )
80
+ fout.add_tensor(
81
+ "mm.2.bias",
82
+ tensors.get_tensor("model.mm_projector.2.bias").to(torch.float32).numpy().copy(),
83
+ )
84
+
85
+ # encoder (siglip)
86
+ fout.add_tensor(
87
+ "v.position_embd.weight",
88
+ tensors.get_tensor("model.vision_tower.vision_tower.vision_model.embeddings.position_embedding.weight").to(
89
+ torch.float16
90
+ ).numpy().copy(),
91
+ )
92
+ fout.add_tensor(
93
+ "v.patch_embd.weight",
94
+ tensors.get_tensor(
95
+ "model.vision_tower.vision_tower.vision_model.embeddings.patch_embedding.weight"
96
+ )
97
+ .reshape(1152, 3, 14, 14)
98
+ .to(torch.float16).numpy().copy(),
99
+ )
100
+ fout.add_tensor(
101
+ "v.patch_embd.bias",
102
+ tensors.get_tensor(
103
+ "model.vision_tower.vision_tower.vision_model.embeddings.patch_embedding.bias"
104
+ ).to(torch.float32).numpy().copy(),
105
+ )
106
+
107
+ fout.add_tensor(
108
+ "v.post_ln.weight",
109
+ tensors.get_tensor("model.vision_tower.vision_tower.vision_model.post_layernorm.weight").to(
110
+ torch.float32
111
+ ).numpy().copy(),
112
+ )
113
+ fout.add_tensor(
114
+ "v.post_ln.bias",
115
+ tensors.get_tensor("model.vision_tower.vision_tower.vision_model.post_layernorm.bias").to(
116
+ torch.float32
117
+ ).numpy().copy(),
118
+ )
119
+
120
+ def blk_tensor(i, name):
121
+ return tensors.get_tensor(
122
+ rf"model.vision_tower.vision_tower.vision_model.encoder.layers.{i}.{name}"
123
+ )
124
+
125
+ def add_tensor(blk_id, gguf_id=None):
126
+ if gguf_id is None:
127
+ gguf_id = blk_id
128
+
129
+ fout.add_tensor(f"v.blk.{gguf_id}.attn_q.weight", blk_tensor(blk_id, "self_attn.q_proj.weight").to(torch.float16).numpy().copy())
130
+ fout.add_tensor(f"v.blk.{gguf_id}.attn_q.bias", blk_tensor(blk_id, "self_attn.q_proj.bias").to(torch.float32).numpy().copy())
131
+ fout.add_tensor(f"v.blk.{gguf_id}.attn_k.weight", blk_tensor(blk_id, "self_attn.k_proj.weight").to(torch.float16).numpy().copy())
132
+ fout.add_tensor(f"v.blk.{gguf_id}.attn_k.bias", blk_tensor(blk_id, "self_attn.k_proj.bias").to(torch.float32).numpy().copy())
133
+ fout.add_tensor(f"v.blk.{gguf_id}.attn_v.weight", blk_tensor(blk_id, "self_attn.v_proj.weight").to(torch.float16).numpy().copy())
134
+ fout.add_tensor(f"v.blk.{gguf_id}.attn_v.bias", blk_tensor(blk_id, "self_attn.v_proj.bias").to(torch.float32).numpy().copy())
135
+
136
+ fout.add_tensor(
137
+ f"v.blk.{gguf_id}.attn_out.weight",
138
+ blk_tensor(blk_id, "self_attn.out_proj.weight").to(torch.float16).numpy().copy(),
139
+ )
140
+ fout.add_tensor(
141
+ f"v.blk.{gguf_id}.attn_out.bias",
142
+ blk_tensor(blk_id, "self_attn.out_proj.bias").to(torch.float32).numpy().copy(),
143
+ )
144
+
145
+ fout.add_tensor(
146
+ f"v.blk.{gguf_id}.ln1.weight",
147
+ blk_tensor(blk_id, "layer_norm1.weight").to(torch.float32).numpy().copy(),
148
+ )
149
+ fout.add_tensor(
150
+ f"v.blk.{gguf_id}.ln1.bias",
151
+ blk_tensor(blk_id, "layer_norm1.bias").to(torch.float32).numpy().copy(),
152
+ )
153
+
154
+ fout.add_tensor(
155
+ f"v.blk.{gguf_id}.ffn_down.weight",
156
+ blk_tensor(blk_id, "mlp.fc1.weight").to(torch.float16).numpy().copy(),
157
+ )
158
+ fout.add_tensor(
159
+ f"v.blk.{gguf_id}.ffn_down.bias",
160
+ blk_tensor(blk_id, "mlp.fc1.bias").to(torch.float32).numpy().copy(),
161
+ )
162
+ fout.add_tensor(
163
+ f"v.blk.{gguf_id}.ffn_up.weight",
164
+ blk_tensor(blk_id, "mlp.fc2.weight").to(torch.float16).numpy().copy(),
165
+ )
166
+ fout.add_tensor(
167
+ f"v.blk.{gguf_id}.ffn_up.bias",
168
+ blk_tensor(blk_id, "mlp.fc2.bias").to(torch.float32).numpy().copy(),
169
+ )
170
+
171
+ fout.add_tensor(
172
+ f"v.blk.{gguf_id}.ln2.weight",
173
+ blk_tensor(blk_id, "layer_norm2.weight").to(torch.float32).numpy().copy(),
174
+ )
175
+ fout.add_tensor(
176
+ f"v.blk.{gguf_id}.ln2.bias",
177
+ blk_tensor(blk_id, "layer_norm2.bias").to(torch.float32).numpy().copy(),
178
+ )
179
+
180
+ for i in range(27):
181
+ add_tensor(i)
182
+
183
+ # Duplicate the last block (llava-cli skips over this)
184
+ add_tensor(26, 27)
185
+
186
+ fout.write_header_to_file()
187
+ fout.write_kv_data_to_file()
188
+ fout.write_tensors_to_file()
189
+ fout.close()
190
+ print(f'successfully exported to {fname_out}')