File size: 1,696 Bytes
af3fd52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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])