1import argparse
2import os
3import torch
4from transformers import AutoModel
5
6ap = argparse.ArgumentParser()
7ap.add_argument("-m", "--model", help="Path to GLM model")
8args = ap.parse_args()
9
10# find the model part that includes the the multimodal projector weights
11model = AutoModel.from_pretrained(args.model, trust_remote_code=True, local_files_only=True)
12checkpoint = model.state_dict()
13
14# get a list of mm tensor names
15mm_tensors = [k for k, v in checkpoint.items() if k.startswith("vision.adapter.")]
16
17# store these tensors in a new dictionary and torch.save them
18projector = {name: checkpoint[name].float() for name in mm_tensors}
19torch.save(projector, f"{args.model}/glm.projector")
20
21clip_tensors = [k for k, v in checkpoint.items() if k.startswith("vision.vit.model.vision_model.")]
22if len(clip_tensors) > 0:
23 clip = {name.replace("vision.vit.model.", ""): checkpoint[name].float() for name in clip_tensors}
24 torch.save(clip, f"{args.model}/glm.clip")
25
26 # added tokens should be removed to be able to convert Mistral models
27 if os.path.exists(f"{args.model}/added_tokens.json"):
28 with open(f"{args.model}/added_tokens.json", "w") as f:
29 f.write("{}\n")
30
31print("Done!")
32print(f"Now you can convert {args.model} to a regular LLaMA GGUF file.")
33print(f"Also, use {args.model}glm.projector to prepare a glm-encoder.gguf file.")