KBaba7/llama.cpp
0
1import argparse2import os3import torch4from transformers import AutoModel, AutoTokenizer5 6ap = argparse.ArgumentParser()7ap.add_argument("-m", "--model", help="Path to MiniCPM-V model")8args = ap.parse_args()9 10# find the model part that includes the the multimodal projector weights11model = AutoModel.from_pretrained(args.model, trust_remote_code=True, local_files_only=True, torch_dtype=torch.bfloat16)12checkpoint = model.state_dict()13 14# get a list of mm tensor names15mm_tensors = [k for k, v in checkpoint.items() if k.startswith("resampler")]16 17# store these tensors in a new dictionary and torch.save them18projector = {name: checkpoint[name].float() for name in mm_tensors}19torch.save(projector, f"{args.model}/minicpmv.projector")20 21clip_tensors = [k for k, v in checkpoint.items() if k.startswith("vpm")]22if len(clip_tensors) > 0:23 clip = {name.replace("vpm.", ""): checkpoint[name].float() for name in clip_tensors}24 torch.save(clip, f"{args.model}/minicpmv.clip")25 26 # added tokens should be removed to be able to convert Mistral models27 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 31config = model.llm.config32config.auto_map = {33 "AutoConfig": "configuration_minicpm.MiniCPMConfig",34 "AutoModel": "modeling_minicpm.MiniCPMModel",35 "AutoModelForCausalLM": "modeling_minicpm.MiniCPMForCausalLM",36 "AutoModelForSeq2SeqLM": "modeling_minicpm.MiniCPMForCausalLM",37 "AutoModelForSequenceClassification": "modeling_minicpm.MiniCPMForSequenceClassification"38}39model.llm.save_pretrained(f"{args.model}/model")40tok = AutoTokenizer.from_pretrained(args.model, trust_remote_code=True)41tok.save_pretrained(f"{args.model}/model")42 43print("Done!")44print(f"Now you can convert {args.model} to a regular LLaMA GGUF file.")45print(f"Also, use {args.model}/minicpmv.projector to prepare a minicpmv-encoder.gguf file.")46 