1
0

glmedge-surgery.py 1.3 KB

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