convert_image_encoder_to_gguf.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import argparse
  2. import os
  3. import json
  4. import re
  5. import torch
  6. import numpy as np
  7. from gguf import *
  8. from transformers import CLIPModel, CLIPProcessor, CLIPVisionModel, SiglipVisionModel
  9. TEXT = "clip.text"
  10. VISION = "clip.vision"
  11. def k(raw_key: str, arch: str) -> str:
  12. return raw_key.format(arch=arch)
  13. def should_skip_tensor(name: str, has_text: bool, has_vision: bool, has_llava: bool) -> bool:
  14. if name in (
  15. "logit_scale",
  16. "text_model.embeddings.position_ids",
  17. "vision_model.embeddings.position_ids",
  18. ):
  19. return True
  20. if has_llava and name in ["visual_projection.weight", "vision_model.post_layernorm.weight", "vision_model.post_layernorm.bias"]:
  21. return True
  22. if name.startswith("v") and not has_vision:
  23. return True
  24. if name.startswith("t") and not has_text:
  25. return True
  26. return False
  27. def get_tensor_name(name: str) -> str:
  28. # Standardize the transformers llava next keys for
  29. # image newline / mm projector with the classes in haotian-liu LLaVA
  30. if name == "image_newline":
  31. return "model.image_newline"
  32. if name.startswith("multi_modal_projector"):
  33. name = name.replace("multi_modal_projector", "mm")
  34. if "linear_1" in name:
  35. name = name.replace("linear_1", "0")
  36. if "linear_2" in name:
  37. name = name.replace("linear_2", "2")
  38. return name
  39. if "projection" in name:
  40. return name
  41. if "mm_projector" in name:
  42. name = name.replace("model.mm_projector", "mm")
  43. name = re.sub(r'mm\.mlp\.mlp', 'mm.model.mlp', name, count=1)
  44. name = re.sub(r'mm\.peg\.peg', 'mm.model.peg', name, count=1)
  45. return name
  46. return name.replace("text_model", "t").replace("vision_model", "v").replace("encoder.layers", "blk").replace("embeddings.", "").replace("_proj", "").replace("self_attn.", "attn_").replace("layer_norm", "ln").replace("layernorm", "ln").replace("mlp.fc1", "ffn_down").replace("mlp.fc2", "ffn_up").replace("embedding", "embd").replace("final", "post").replace("layrnorm", "ln")
  47. def bytes_to_unicode():
  48. """
  49. Returns list of utf-8 byte and a corresponding list of unicode strings.
  50. The reversible bpe codes work on unicode strings.
  51. This means you need a large # of unicode characters in your vocab if you want to avoid UNKs.
  52. When you're at something like a 10B token dataset you end up needing around 5K for decent coverage.
  53. This is a significant percentage of your normal, say, 32K bpe vocab.
  54. To avoid that, we want lookup tables between utf-8 bytes and unicode strings.
  55. And avoids mapping to whitespace/control characters the bpe code barfs on.
  56. """
  57. bs = (
  58. list(range(ord("!"), ord("~") + 1))
  59. + list(range(ord("¡"), ord("¬") + 1))
  60. + list(range(ord("®"), ord("ÿ") + 1))
  61. )
  62. cs = bs[:]
  63. n = 0
  64. for b in range(2**8):
  65. if b not in bs:
  66. bs.append(b)
  67. cs.append(2**8 + n)
  68. n += 1
  69. cs = [chr(n) for n in cs]
  70. return dict(zip(bs, cs))
  71. ap = argparse.ArgumentParser()
  72. ap.add_argument("-m", "--model-dir", help="Path to model directory cloned from HF Hub", required=True)
  73. ap.add_argument("--use-f32", action="store_true", default=False, help="Use f32 instead of f16")
  74. ap.add_argument("--text-only", action="store_true", required=False,
  75. help="Save a text-only model. It can't be used to encode images")
  76. ap.add_argument("--vision-only", action="store_true", required=False,
  77. help="Save a vision-only model. It can't be used to encode texts")
  78. ap.add_argument("--clip-model-is-vision", action="store_true", required=False,
  79. help="The clip model is a pure vision model (ShareGPT4V vision extract for example)")
  80. # Selectable visual encoders that are compatible with this script
  81. encoder_group = ap.add_mutually_exclusive_group()
  82. encoder_group.add_argument("--clip-model-is-openclip", action="store_true", required=False,
  83. help="The clip model is from openclip (for ViT-SO400M type))")
  84. encoder_group.add_argument("--clip-model-is-siglip", action="store_true", required=False,
  85. help="the visual encoder is Siglip.")
  86. ap.add_argument("--llava-projector", help="Path to llava.projector file. If specified, save an image encoder for LLaVA models.")
  87. ap.add_argument("--projector-type", help="Type of projector. Possible values: mlp, ldp, ldpv2", choices=["mlp", "ldp", "ldpv2"], default="mlp")
  88. ap.add_argument("-o", "--output-dir", help="Directory to save GGUF files. Default is the original model directory", default=None)
  89. # Example --image_mean 0.48145466 0.4578275 0.40821073 --image_std 0.26862954 0.26130258 0.27577711
  90. # Example --image_mean 0.5 0.5 0.5 --image_std 0.5 0.5 0.5
  91. default_image_mean = [0.48145466, 0.4578275, 0.40821073]
  92. default_image_std = [0.26862954, 0.26130258, 0.27577711]
  93. ap.add_argument('--image-mean', type=float, nargs='+', help='Mean of the images for normalization (overrides processor) ', default=None)
  94. ap.add_argument('--image-std', type=float, nargs='+', help='Standard deviation of the images for normalization (overrides processor)', default=None)
  95. # with proper
  96. args = ap.parse_args()
  97. if args.text_only and args.vision_only:
  98. print("--text-only and --image-only arguments cannot be specified at the same time.")
  99. exit(1)
  100. if args.use_f32:
  101. print("WARNING: Weights for the convolution op is always saved in f16, as the convolution op in GGML does not support 32-bit kernel weights yet.")
  102. # output in the same directory as the model if output_dir is None
  103. dir_model = args.model_dir
  104. if (
  105. args.clip_model_is_vision or
  106. not os.path.exists(dir_model + "/vocab.json") or
  107. args.clip_model_is_openclip or
  108. args.clip_model_is_siglip
  109. ):
  110. vocab = None
  111. tokens = None
  112. else:
  113. with open(dir_model + "/vocab.json", "r", encoding="utf-8") as f:
  114. vocab = json.load(f)
  115. tokens = [key for key in vocab]
  116. with open(dir_model + "/config.json", "r", encoding="utf-8") as f:
  117. config = json.load(f)
  118. if args.clip_model_is_vision:
  119. v_hparams = config
  120. t_hparams = None
  121. else:
  122. v_hparams = config["vision_config"]
  123. t_hparams = config["text_config"]
  124. # possible data types
  125. # ftype == 0 -> float32
  126. # ftype == 1 -> float16
  127. #
  128. # map from ftype to string
  129. ftype_str = ["f32", "f16"]
  130. ftype = 1
  131. if args.use_f32:
  132. ftype = 0
  133. if args.clip_model_is_siglip:
  134. model = SiglipVisionModel.from_pretrained(dir_model)
  135. processor = None
  136. elif args.clip_model_is_vision or args.clip_model_is_openclip:
  137. model = CLIPVisionModel.from_pretrained(dir_model)
  138. processor = None
  139. else:
  140. model = CLIPModel.from_pretrained(dir_model)
  141. processor = CLIPProcessor.from_pretrained(dir_model)
  142. fname_middle = None
  143. has_text_encoder = True
  144. has_vision_encoder = True
  145. has_llava_projector = False
  146. if args.text_only:
  147. fname_middle = "text-"
  148. has_vision_encoder = False
  149. elif args.llava_projector is not None:
  150. fname_middle = "mmproj-"
  151. has_text_encoder = False
  152. has_llava_projector = True
  153. elif args.vision_only:
  154. fname_middle = "vision-"
  155. has_text_encoder = False
  156. else:
  157. fname_middle = ""
  158. output_dir = args.output_dir if args.output_dir is not None else dir_model
  159. os.makedirs(output_dir, exist_ok=True)
  160. output_prefix = os.path.basename(output_dir).replace("ggml_", "")
  161. fname_out = os.path.join(output_dir, f"{fname_middle}model-{ftype_str[ftype]}.gguf")
  162. fout = GGUFWriter(path=fname_out, arch="clip")
  163. fout.add_bool("clip.has_text_encoder", has_text_encoder)
  164. fout.add_bool("clip.has_vision_encoder", has_vision_encoder)
  165. fout.add_bool("clip.has_llava_projector", has_llava_projector)
  166. fout.add_file_type(ftype)
  167. model_name = config["_name_or_path"] if "_name_or_path" in config else os.path.basename(dir_model)
  168. fout.add_name(model_name)
  169. if args.text_only:
  170. fout.add_description("text-only CLIP model")
  171. elif args.vision_only and not has_llava_projector:
  172. fout.add_description("vision-only CLIP model")
  173. elif has_llava_projector:
  174. fout.add_description("image encoder for LLaVA")
  175. # add projector type
  176. fout.add_string("clip.projector_type", args.projector_type)
  177. else:
  178. fout.add_description("two-tower CLIP model")
  179. if has_text_encoder:
  180. assert t_hparams is not None
  181. assert tokens is not None
  182. if args.clip_model_is_siglip:
  183. text_projection_dim = 0
  184. else:
  185. text_projection_dim = t_hparams.get("projection_dim", config["projection_dim"])
  186. # text_model hparams
  187. fout.add_uint32(k(KEY_CONTEXT_LENGTH, TEXT), t_hparams["max_position_embeddings"])
  188. fout.add_uint32(k(KEY_EMBEDDING_LENGTH, TEXT), t_hparams["hidden_size"])
  189. fout.add_uint32(k(KEY_FEED_FORWARD_LENGTH, TEXT), t_hparams["intermediate_size"])
  190. fout.add_uint32("clip.text.projection_dim", text_projection_dim)
  191. fout.add_uint32(k(KEY_ATTENTION_HEAD_COUNT, TEXT), t_hparams["num_attention_heads"])
  192. fout.add_float32(k(KEY_ATTENTION_LAYERNORM_EPS, TEXT), t_hparams["layer_norm_eps"])
  193. fout.add_uint32(k(KEY_BLOCK_COUNT, TEXT), t_hparams["num_hidden_layers"])
  194. fout.add_token_list(tokens)
  195. def get_non_negative_vision_feature_layers(v_hparams):
  196. """
  197. Determine the vision feature layer(s) for the llava model, which are indices into the
  198. hidden states of the visual encoder. Note that the hidden states array generally takes the
  199. form:
  200. [<emb input>, <output of enc block 0>, ... <output of enc block num_hidden_layers>]
  201. so feature indices should be offset as n+1 to get the output of encoder block n.
  202. We convert all vision feature layers to non-negative so that -1 can be used in
  203. the model as an unset value. If no vision feature layer is found, we leave it unset.
  204. """
  205. num_hidden_layers = v_hparams["num_hidden_layers"]
  206. to_non_negative = lambda layer_idx: layer_idx if layer_idx >= 0 else num_hidden_layers + layer_idx + 1
  207. feature_layers_key = None
  208. # Key used for llava models in transformers
  209. if "vision_feature_layer" in config:
  210. feature_layers_key = "vision_feature_layer"
  211. # Key used for llava models in the original format
  212. elif "mm_vision_select_layer" in config:
  213. feature_layers_key = "mm_vision_select_layer"
  214. if feature_layers_key is not None:
  215. feature_layers = config[feature_layers_key]
  216. if isinstance(feature_layers, int):
  217. feature_layers = [feature_layers]
  218. return [to_non_negative(feature_layer) for feature_layer in feature_layers]
  219. # Determine if we have explicitly specified vision feature layers in our config
  220. feature_layers = get_non_negative_vision_feature_layers(v_hparams)
  221. if has_vision_encoder:
  222. # Siglip does not have a visual projector; set projection dim to 0
  223. if args.clip_model_is_siglip:
  224. visual_projection_dim = 0
  225. else:
  226. visual_projection_dim = v_hparams.get("projection_dim", config["projection_dim"])
  227. # set vision_model hparams
  228. fout.add_uint32("clip.vision.image_size", v_hparams["image_size"])
  229. fout.add_uint32("clip.vision.patch_size", v_hparams["patch_size"])
  230. fout.add_uint32(k(KEY_EMBEDDING_LENGTH, VISION), v_hparams["hidden_size"])
  231. fout.add_uint32(k(KEY_FEED_FORWARD_LENGTH, VISION), v_hparams["intermediate_size"])
  232. fout.add_uint32("clip.vision.projection_dim", visual_projection_dim)
  233. fout.add_uint32(k(KEY_ATTENTION_HEAD_COUNT, VISION), v_hparams["num_attention_heads"])
  234. fout.add_float32(k(KEY_ATTENTION_LAYERNORM_EPS, VISION), v_hparams["layer_norm_eps"])
  235. if feature_layers:
  236. block_count = max(feature_layers)
  237. else:
  238. block_count = v_hparams["num_hidden_layers"] - 1 if has_llava_projector else v_hparams["num_hidden_layers"]
  239. fout.add_uint32(k(KEY_BLOCK_COUNT, VISION), block_count)
  240. # /**
  241. # "image_grid_pinpoints": [
  242. # [
  243. # 336,
  244. # 672
  245. # ],
  246. # [
  247. # 672,
  248. # 336
  249. # ],
  250. # [
  251. # 672,
  252. # 672
  253. # ],
  254. # [
  255. # 1008,
  256. # 336
  257. # ],
  258. # [
  259. # 336,
  260. # 1008
  261. # ]
  262. # ],
  263. # Flattened:
  264. # [
  265. # 336, 672,
  266. # 672, 336,
  267. # 672, 672,
  268. # 1008, 336,
  269. # 336, 1008
  270. # ]
  271. # *
  272. # */
  273. if "image_grid_pinpoints" in v_hparams:
  274. # flatten it
  275. image_grid_pinpoints = []
  276. for pinpoint in v_hparams["image_grid_pinpoints"]:
  277. for p in pinpoint:
  278. image_grid_pinpoints.append(p)
  279. fout.add_array("clip.vision.image_grid_pinpoints", image_grid_pinpoints)
  280. if "image_crop_resolution" in v_hparams:
  281. fout.add_uint32("clip.vision.image_crop_resolution", v_hparams["image_crop_resolution"])
  282. if "image_aspect_ratio" in v_hparams:
  283. fout.add_string("clip.vision.image_aspect_ratio", v_hparams["image_aspect_ratio"])
  284. if "image_split_resolution" in v_hparams:
  285. fout.add_uint32("clip.vision.image_split_resolution", v_hparams["image_split_resolution"])
  286. if "mm_patch_merge_type" in v_hparams:
  287. fout.add_string("clip.vision.mm_patch_merge_type", v_hparams["mm_patch_merge_type"])
  288. if "mm_projector_type" in v_hparams:
  289. fout.add_string("clip.vision.mm_projector_type", v_hparams["mm_projector_type"])
  290. if feature_layers:
  291. fout.add_array("clip.vision.feature_layer", feature_layers)
  292. if processor is not None:
  293. image_mean = processor.image_processor.image_mean if args.image_mean is None or args.image_mean == default_image_mean else args.image_mean # pyright: ignore[reportAttributeAccessIssue]
  294. image_std = processor.image_processor.image_std if args.image_std is None or args.image_std == default_image_std else args.image_std # pyright: ignore[reportAttributeAccessIssue]
  295. else:
  296. image_mean = args.image_mean if args.image_mean is not None else default_image_mean
  297. image_std = args.image_std if args.image_std is not None else default_image_std
  298. fout.add_array("clip.vision.image_mean", image_mean)
  299. fout.add_array("clip.vision.image_std", image_std)
  300. use_gelu = v_hparams["hidden_act"] == "gelu"
  301. fout.add_bool("clip.use_gelu", use_gelu)
  302. if has_llava_projector:
  303. # By default, we drop the last layer for llava projector
  304. # models unless we have explicitly set vision feature layers
  305. if feature_layers is None:
  306. model.vision_model.encoder.layers.pop(-1)
  307. else:
  308. model.vision_model.encoder.layers = model.vision_model.encoder.layers[:max(feature_layers)]
  309. projector = torch.load(args.llava_projector)
  310. for name, data in projector.items():
  311. name = get_tensor_name(name)
  312. # pw and dw conv ndim==4
  313. if data.ndim == 2 or data.ndim == 4:
  314. data = data.squeeze().numpy().astype(np.float16)
  315. else:
  316. data = data.squeeze().numpy().astype(np.float32)
  317. fout.add_tensor(name, data)
  318. print("Projector tensors added\n")
  319. state_dict = model.state_dict()
  320. for name, data in state_dict.items():
  321. if should_skip_tensor(name, has_text_encoder, has_vision_encoder, has_llava_projector):
  322. # we don't need this
  323. print(f"skipping parameter: {name}")
  324. continue
  325. name = get_tensor_name(name)
  326. data = data.squeeze().numpy()
  327. n_dims = len(data.shape)
  328. # ftype == 0 -> float32, ftype == 1 -> float16
  329. ftype_cur = 0
  330. if n_dims == 4:
  331. print(f"tensor {name} is always saved in f16")
  332. data = data.astype(np.float16)
  333. ftype_cur = 1
  334. elif ftype == 1:
  335. if name[-7:] == ".weight" and n_dims == 2:
  336. print(" Converting to float16")
  337. data = data.astype(np.float16)
  338. ftype_cur = 1
  339. else:
  340. print(" Converting to float32")
  341. data = data.astype(np.float32)
  342. ftype_cur = 0
  343. else:
  344. if data.dtype != np.float32:
  345. print(" Converting to float32")
  346. data = data.astype(np.float32)
  347. ftype_cur = 0
  348. print(f"{name} - {ftype_str[ftype_cur]} - shape = {data.shape}")
  349. fout.add_tensor(name, data)
  350. fout.write_header_to_file()
  351. fout.write_kv_data_to_file()
  352. fout.write_tensors_to_file()
  353. fout.close()
  354. print("Done. Output file: " + fname_out)