minicpm5-toolcall-code / debug_gen.py
DevGwardo's picture
Add generation debug
af3fd52 verified
Raw History Blame Contribute Delete
1.7 kB
import json
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
BASE = "openbmb/MiniCPM5-1B"
ADAPTER = "DevGwardo/minicpm5-1b-toolcall"
tok = AutoTokenizer.from_pretrained(ADAPTER)
print("im_end id:", tok.convert_tokens_to_ids("<|im_end|>"))
print("eos id:", tok.eos_token_id, "pad:", tok.pad_token_id, "bos:", tok.bos_token_id)
print("vocab:", len(tok))
model = AutoModelForCausalLM.from_pretrained(BASE, dtype=torch.float32, use_kernels=False)
model = PeftModel.from_pretrained(model, ADAPTER)
model.eval()
# check the adapter actually carries signal
w = model.base_model.model.model.layers[0].self_attn.q_proj.lora_A.default.weight
print("lora_A norm:", float(w.float().norm()), "std:", float(w.float().std()))
rows = [json.loads(l) for l in open("/data/test.jsonl", encoding="utf-8")]
pos = [r for r in rows if r["messages"][-1].get("tool_calls")][:3]
for r in pos:
prompt = tok.apply_chat_template(
r["messages"][:-1], tools=r["tools"], tokenize=False, add_generation_prompt=True
)
print("\n=== PROMPT TAIL ===")
print(repr(prompt[-120:]))
enc = tok(prompt, return_tensors="pt", add_special_tokens=False)
with torch.no_grad():
out = model.generate(
**enc, max_new_tokens=192, do_sample=False,
eos_token_id=tok.convert_tokens_to_ids("<|im_end|>"), pad_token_id=tok.pad_token_id,
)
new = out[0][enc["input_ids"].shape[1]:]
print("generated tokens:", len(new), "ids head:", new[:10].tolist())
print("RAW:", repr(tok.decode(new, skip_special_tokens=True)[:600]))
print("EXPECTED:", json.dumps(r["messages"][-1]["tool_calls"], ensure_ascii=False)[:400])