Download debug_gen.py from DevGwardo/minicpm5-toolcall-code: direct link, hf CLI and curl.
- Browser
- Download file 1.7 kB
-
https://huggingface.co/datasets/DevGwardo/minicpm5-toolcall-code/resolve/main/debug_gen.py
- Command line
-
hf download hf://datasets/DevGwardo/minicpm5-toolcall-code/debug_gen.py
-
curl -L -o debug_gen.py https://huggingface.co/datasets/DevGwardo/minicpm5-toolcall-code/resolve/main/debug_gen.py
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]) | |