1
0

llava_surgery.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import argparse
  2. import glob
  3. import os
  4. import torch
  5. ap = argparse.ArgumentParser()
  6. ap.add_argument("-m", "--model", help="Path to LLaVA v1.5 model")
  7. args = ap.parse_args()
  8. # find the model part that includes the the multimodal projector weights
  9. path = sorted(glob.glob(f"{args.model}/pytorch_model*.bin"))[-1]
  10. checkpoint = torch.load(path)
  11. # get a list of mm tensor names
  12. mm_tensors = [k for k, v in checkpoint.items() if k.startswith("model.mm_projector")]
  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}/llava.projector")
  16. # BakLLaVA models contain CLIP tensors in it
  17. clip_tensors = [k for k, v in checkpoint.items() if k.startswith("model.vision_tower")]
  18. if len(clip_tensors) > 0:
  19. clip = {name.replace("vision_tower.vision_tower.", ""): checkpoint[name].float() for name in clip_tensors}
  20. torch.save(clip, f"{args.model}/llava.clip")
  21. # added tokens should be removed to be able to convert Mistral models
  22. if os.path.exists(f"{args.model}/added_tokens.json"):
  23. with open(f"{args.model}/added_tokens.json", "w") as f:
  24. f.write("{}\n")
  25. print("Done!")
  26. print(f"Now you can convert {args.model} to a regular LLaMA GGUF file.")
  27. print(f"Also, use {args.model}/llava.projector to prepare a llava-encoder.gguf file.")