llava-surgery.py 958 B

123456789101112131415161718192021222324252627282930
  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] for name in mm_tensors}
  15. torch.save(projector, f"{args.model}/llava.projector")
  16. # remove these tensors from the checkpoint and save it again
  17. for name in mm_tensors:
  18. del checkpoint[name]
  19. torch.save(checkpoint, path)
  20. print("Done!")
  21. print(f"Now you can convert {args.model} to a a regular LLaMA GGUF file.")
  22. print(f"Also, use {args.model}/llava.projector to prepare a llava-encoder.gguf file.")