modify convert script of minicpmv
This commit is contained in:
parent
6da5130bdb
commit
77c580deeb
4 changed files with 1370 additions and 1 deletions
|
@ -166,6 +166,7 @@ elif args.minicpmv_projector is not None:
|
||||||
fname_middle = "mmproj-"
|
fname_middle = "mmproj-"
|
||||||
has_text_encoder = False
|
has_text_encoder = False
|
||||||
has_minicpmv_projector = True
|
has_minicpmv_projector = True
|
||||||
|
minicpmv_version = 3
|
||||||
elif args.vision_only:
|
elif args.vision_only:
|
||||||
fname_middle = "vision-"
|
fname_middle = "vision-"
|
||||||
has_text_encoder = False
|
has_text_encoder = False
|
||||||
|
@ -190,6 +191,7 @@ elif has_minicpmv_projector:
|
||||||
fout.add_description("image encoder for MiniCPM-V")
|
fout.add_description("image encoder for MiniCPM-V")
|
||||||
# add projector type
|
# add projector type
|
||||||
fout.add_string("clip.projector_type", "resampler")
|
fout.add_string("clip.projector_type", "resampler")
|
||||||
|
fout.add_int32("clip.minicpmv_version", minicpmv_version)
|
||||||
else:
|
else:
|
||||||
fout.add_description("two-tower CLIP model")
|
fout.add_description("two-tower CLIP model")
|
||||||
|
|
|
@ -41,7 +41,6 @@ config.auto_map = {
|
||||||
model.llm.save_pretrained(f"{args.model}/model")
|
model.llm.save_pretrained(f"{args.model}/model")
|
||||||
tok = AutoTokenizer.from_pretrained(args.model, trust_remote_code=True)
|
tok = AutoTokenizer.from_pretrained(args.model, trust_remote_code=True)
|
||||||
tok.save_pretrained(f"{args.model}/model")
|
tok.save_pretrained(f"{args.model}/model")
|
||||||
# os.system(f"cp {args.model}/modeling_minicpm.py {args.model}/MiniCPM_l3/modeling_minicpm.py")
|
|
||||||
|
|
||||||
print("Done!")
|
print("Done!")
|
||||||
print(f"Now you can convert {args.model} to a regular LLaMA GGUF file.")
|
print(f"Now you can convert {args.model} to a regular LLaMA GGUF file.")
|
File diff suppressed because it is too large
Load diff
47
examples/llava/minicpmv-convert/minicpmv2_6-surgery.py
Normal file
47
examples/llava/minicpmv-convert/minicpmv2_6-surgery.py
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
import argparse
|
||||||
|
import glob
|
||||||
|
import os
|
||||||
|
import torch
|
||||||
|
from transformers import AutoModel, AutoTokenizer
|
||||||
|
|
||||||
|
ap = argparse.ArgumentParser()
|
||||||
|
ap.add_argument("-m", "--model", help="Path to MiniCPM-V-2.6 model")
|
||||||
|
args = ap.parse_args()
|
||||||
|
|
||||||
|
# find the model part that includes the the multimodal projector weights
|
||||||
|
model = AutoModel.from_pretrained(args.model, trust_remote_code=True, local_files_only=True)
|
||||||
|
checkpoint = model.state_dict()
|
||||||
|
|
||||||
|
# get a list of mm tensor names
|
||||||
|
mm_tensors = [k for k, v in checkpoint.items() if k.startswith("resampler")]
|
||||||
|
|
||||||
|
# store these tensors in a new dictionary and torch.save them
|
||||||
|
projector = {name: checkpoint[name].float() for name in mm_tensors}
|
||||||
|
torch.save(projector, f"{args.model}/minicpmv.projector")
|
||||||
|
|
||||||
|
clip_tensors = [k for k, v in checkpoint.items() if k.startswith("vpm")]
|
||||||
|
if len(clip_tensors) > 0:
|
||||||
|
clip = {name.replace("vpm.", ""): checkpoint[name].float() for name in clip_tensors}
|
||||||
|
torch.save(clip, f"{args.model}/minicpmv.clip")
|
||||||
|
|
||||||
|
# added tokens should be removed to be able to convert Mistral models
|
||||||
|
if os.path.exists(f"{args.model}/added_tokens.json"):
|
||||||
|
with open(f"{args.model}/added_tokens.json", "w") as f:
|
||||||
|
f.write("{}\n")
|
||||||
|
|
||||||
|
config = model.llm.config
|
||||||
|
config._name_or_path = "openbmb/MiniCPM-V-2.6"
|
||||||
|
config.auto_map = {
|
||||||
|
"AutoConfig": "configuration_minicpm.MiniCPMConfig",
|
||||||
|
"AutoModel": "modeling_minicpm.MiniCPMModel",
|
||||||
|
"AutoModelForCausalLM": "modeling_minicpm.MiniCPMForCausalLM",
|
||||||
|
"AutoModelForSeq2SeqLM": "modeling_minicpm.MiniCPMForCausalLM",
|
||||||
|
"AutoModelForSequenceClassification": "modeling_minicpm.MiniCPMForSequenceClassification"
|
||||||
|
}
|
||||||
|
model.llm.save_pretrained(f"{args.model}/model")
|
||||||
|
tok = AutoTokenizer.from_pretrained(args.model, trust_remote_code=True)
|
||||||
|
tok.save_pretrained(f"{args.model}/model")
|
||||||
|
|
||||||
|
print("Done!")
|
||||||
|
print(f"Now you can convert {args.model} to a regular LLaMA GGUF file.")
|
||||||
|
print(f"Also, use {args.model}/minicpmv.projector to prepare a minicpmv-encoder.gguf file.")
|
Loading…
Add table
Add a link
Reference in a new issue