File size: 4,462 Bytes
56a0f31 | 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #!/usr/bin/env python3
# data_contract_check.py (v3) -- train-file "data contract" preflight (数据契约检查). CPU only, read only.
# Motivation: wave E (2026-10-30) trained ~2 h x 4 GPUs on 今译 rows whose user turn carried the task wrapper
# twice; no existing preflight looked at row CONTENT FORMAT. This check fails that file in seconds.
#
# The contract is LEARNED from a reference file (default stage2_v1 = the format the eval sets were built from):
# template markers T[task] = 【…】 markers present in >= 90% of reference rows of that task
# (content labels such as slip numbers 【98】【志2】【4+5】 are therefore never treated as wrappers).
# Checks, per row of the candidate file:
# C1 wrapper multiplicity: a template marker occurs more often than its max multiplicity in the reference.
# C2 template parity: the sequence of template markers is not one the reference uses for that task.
# C3 role shape: roles must be exactly system,user,assistant with a non-empty assistant turn.
# C0 unknown task: a system-prompt prefix that the reference never uses (reported; fails the contract).
# report-only: Han/Qin vs Chu cue counts, so "楚简" labels can be sanity-checked.
# History: v1/v2 counted every 【…】 marker and false-failed the reference itself on slip labels (fixed in v3).
# Usage: data_contract_check.py TRAIN.jsonl [REFERENCE.jsonl] -> exit 0 PASS / 1 FAIL
import collections, json, re, sys
MARK = re.compile(r"【[^】]{1,16}】")
HAN = re.compile(r"肩水|居延|金关|悬泉|敦煌|候长|燧长|隧长|啬夫|里耶|迁陵|甲渠|鄣卒|戍卒|候官|令史|亭长")
CHU = re.compile(r"楚|郢|[戠𢍰]|\{[^}]+\}")
TEMPLATE_FRAC = 0.90
def rows(p):
return [json.loads(l) for l in open(p, encoding="utf-8") if l.strip()]
def task_of(r):
s = [m["content"] for m in r["messages"] if m["role"] == "system"]
return s[0][:24] if s else "<no-system>"
def user_of(r):
u = [m["content"] for m in r["messages"] if m["role"] == "user"]
return u[0] if u else ""
def learn(R):
by_task = collections.defaultdict(list)
for r in R:
by_task[task_of(r)].append(MARK.findall(user_of(r)))
T, maxmult, skeletons = {}, {}, {}
for t, lists in by_task.items():
n = len(lists)
present = collections.Counter(m for ms in lists for m in set(ms))
T[t] = {m for m, c in present.items() if c / n >= TEMPLATE_FRAC}
maxmult[t] = {m: max(ms.count(m) for ms in lists) for m in T[t]}
skeletons[t] = {tuple(m for m in ms if m in T[t]) for ms in lists}
return T, maxmult, skeletons
def main():
train = sys.argv[1]
ref = sys.argv[2] if len(sys.argv) > 2 else "/root/autodl-tmp/llm_rag_20261023/data/stage2_v1.jsonl"
R, X = rows(ref), rows(train)
T, maxmult, skeletons = learn(R)
viol, examples = collections.Counter(), {}
per_task = collections.defaultdict(collections.Counter)
def hit(kind, t, i):
viol[kind] += 1; per_task[t][kind] += 1; examples.setdefault(kind, i)
for i, r in enumerate(X):
t, u = task_of(r), user_of(r)
per_task[t]["rows"] += 1
per_task[t]["han_cue"] += bool(HAN.search(u))
per_task[t]["chu_cue"] += bool(CHU.search(u))
if [m["role"] for m in r["messages"]] != ["system", "user", "assistant"] or not r["messages"][-1]["content"].strip():
hit("C3_role_shape", t, i)
if t not in T:
hit("C0_unknown_task", t, i); continue
ms = MARK.findall(u)
if any(ms.count(m) > maxmult[t][m] for m in T[t]):
hit("C1_wrapper_multiplicity", t, i)
if tuple(m for m in ms if m in T[t]) not in skeletons[t]:
hit("C2_template_parity", t, i)
print("train:", train, "rows", len(X), "| reference:", ref)
for t in sorted(T):
print(" template[%s] = %s" % (t[:14], sorted(T[t])))
for t, c in per_task.items():
print(" task %-26s rows=%5d C1=%5d C2=%5d C0=%4d han_cue=%4d chu_cue=%4d" % (
t, c["rows"], c["C1_wrapper_multiplicity"], c["C2_template_parity"], c["C0_unknown_task"],
c["han_cue"], c["chu_cue"]))
for k, i in examples.items():
print(" first %s at row %d: %s" % (k, i, json.dumps(user_of(X[i])[:110], ensure_ascii=False)))
ok = not viol
print("DATA_CONTRACT", "PASS" if ok else "FAIL", dict(viol))
sys.exit(0 if ok else 1)
if __name__ == "__main__":
main()
|