convert_mmbert

This commit is contained in:
dingbin.wang 2026-10-21 17:30:25 +08:00
parent e94dbb638b
commit 322778f3dd

22
convert_mmbert.py Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env python3
"""convert_mmbert.py — mmBERT pytorch_model.bin → Laya DecisionModel 格式"""
import torch, os, shutil, sys
from safetensors.torch import save_file
base = sys.argv[1] if len(sys.argv) > 1 else "/tmp/base"
sd = torch.load(os.path.join(base, "pytorch_model.bin"), map_location="cpu", weights_only=True)
out = {}
dropped = 0
for k, v in sd.items():
if k.startswith("cls."):
dropped += 1
continue
out["encoder." + k] = v.to(torch.float32)
save_file(out, os.path.join(base, "model.safetensors"), metadata={"format": "pt"})
os.remove(os.path.join(base, "pytorch_model.bin"))
os.makedirs(os.path.join(base, "tokenizer"), exist_ok=True)
for fn in ["tokenizer.json", "tokenizer_config.json", "special_tokens_map.json"]:
src = os.path.join(base, fn)
if os.path.exists(src):
shutil.copy(src, os.path.join(base, "tokenizer", fn))
print(f"[convert] encoder键 {len(out)} 个, 丢弃mlm头 {dropped} 个")