File size: 1,492 Bytes
53d5fc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Decode training targets of N random rows: only assistant turns (text / tool calls) + <|im_end|> may be targets."""
import json, random, sys
import numpy as np, pyarrow.parquet as pq
from tokenizers import Tokenizer
tok = Tokenizer.from_file(sys.argv[2])
t = pq.read_table(sys.argv[1]).to_pylist()
random.Random(0).shuffle(t)
bad, n_tool = 0, 0
for r in t[:50]:
    ids = np.frombuffer(r["input_ids"], dtype=np.uint16).tolist(); m = np.frombuffer(r["loss_mask"], dtype=np.uint8).tolist()
    spans, cur = [], []
    for i, (x, y) in enumerate(zip(ids, m)):
        if y: cur.append(x)
        elif cur: spans.append(cur); cur = []
    if cur: spans.append(cur)
    texts = [tok.decode(s, skip_special_tokens=False) for s in spans]
    full = tok.decode(ids, skip_special_tokens=False)
    ok = all(s.endswith("<|im_end|>") and "<|im_start|>" not in s and "<tool_response>" not in s for s in texts)
    # every assistant turn body must be covered: count assistant headers vs target spans (context-only turns allowed fewer)
    n_asst = full.count("<|im_start|>assistant\n")
    ok = ok and 1 <= len(spans) <= n_asst
    bad += not ok; n_tool += r["is_tool"]
    if not ok or r is t[0] or (r["is_tool"] and n_tool == 1):
        print("----", r["id"], "tool" if r["is_tool"] else "chat", "ok" if ok else "BAD", f"asst_turns={n_asst} target_spans={len(spans)}")
        for s in texts[:3]: print("   TARGET:", repr(s[:220]))
print(json.dumps({"checked": 50, "bad": bad, "tool_rows": n_tool}))