Download debug_encode.py from DevGwardo/minicpm5-toolcall-code: direct link, hf CLI and curl.
- Browser
- Download file 1.27 kB
-
https://huggingface.co/datasets/DevGwardo/minicpm5-toolcall-code/resolve/08741fd8a3bf31a375f65a8a423fba83a985d811/debug_encode.py
- Command line
-
hf download hf://datasets/DevGwardo/minicpm5-toolcall-code@08741fd8a3bf31a375f65a8a423fba83a985d811/debug_encode.py
-
curl -L -o debug_encode.py https://huggingface.co/datasets/DevGwardo/minicpm5-toolcall-code/resolve/08741fd8a3bf31a375f65a8a423fba83a985d811/debug_encode.py
1.27 kB
| import json | |
| from transformers import AutoTokenizer | |
| MODEL_ID = "openbmb/MiniCPM5-1B" | |
| tok = AutoTokenizer.from_pretrained(MODEL_ID) | |
| def encode(ex, max_len=1600): | |
| msgs, tools = ex["messages"], ex["tools"] | |
| prompt_text = tok.apply_chat_template(msgs[:-1], tools=tools, tokenize=False, add_generation_prompt=True) | |
| full_text = tok.apply_chat_template(msgs, tools=tools, tokenize=False) | |
| prompt = tok(prompt_text, add_special_tokens=False)["input_ids"] | |
| full = tok(full_text, add_special_tokens=False)["input_ids"] | |
| if len(full) > max_len or len(full) <= len(prompt) + 1: | |
| return None | |
| n = 0 | |
| while n < min(len(prompt), len(full)) and prompt[n] == full[n]: | |
| n += 1 | |
| if n < len(prompt) - 4: | |
| return None | |
| labels = [-100] * n + full[n:] | |
| if all(l == -100 for l in labels): | |
| return None | |
| return {"input_ids": full, "labels": labels} | |
| rows = [json.loads(l) for l in open("data_native/train.jsonl", encoding="utf-8")][:100] | |
| ok = [e for e in (encode(r) for r in rows) if e] | |
| print("accepted", len(ok), "of", len(rows)) | |
| e = ok[0] | |
| print("len", len(e["input_ids"]), "supervised tokens", sum(1 for x in e["labels"] if x != -100)) | |
| print("decoded target:", repr(tok.decode([x for x in e["labels"] if x != -100])[:300])) | |