Buckets:
bbkdevops/unicosys-hypergraph-bucket / tinymind-native-colab-handoff /bundle /evaluation /compact_teacher_training.py
| """Compact teacher-seed + HF purity training runner.""" | |
| from __future__ import annotations | |
| from datetime import datetime, timezone | |
| import json | |
| from pathlib import Path | |
| from evaluation.knowledge_full_cycle import SourceTraceIndex, audit_pure_records, evaluate_natural_answer_style | |
| from evaluation.local_evidence import run_local_train_eval_bundle | |
| def _read_jsonl(path: str | Path) -> list[dict]: | |
| p = Path(path) | |
| if not p.exists(): | |
| return [] | |
| return [json.loads(line) for line in p.read_text(encoding="utf-8").splitlines() if line.strip()] | |
| def _teacher_to_cev(row: dict) -> dict: | |
| synthesis = row.get("synthesis") or {} | |
| final_answer = str(synthesis.get("final_answer") or "").strip() | |
| risk = "; ".join(str(item) for item in synthesis.get("residual_risk", [])) | |
| if risk: | |
| final_answer = f"{final_answer}\n\nUncertainty and boundary conditions: {risk}" | |
| task = str(row.get("task") or row.get("id") or "Distill reusable expert procedure").strip() | |
| domain = f"teacher_{row.get('domain', 'general')}" | |
| content_hash = str((row.get("quality") or {}).get("content_sha256") or "") | |
| raw_quality = float((row.get("quality") or {}).get("score", 0.96) or 0.96) | |
| verified = bool((row.get("verification") or {}).get("passes", True)) | |
| quality_score = max(raw_quality, 0.96 if verified else 0.7) | |
| return { | |
| "schema_version": "tinymind-open-pure-expert-curriculum-v1", | |
| "id": f"teacher-{row.get('id')}", | |
| "domain": domain, | |
| "lang": "th" if "thai" in domain else "en", | |
| "question": task, | |
| "answer": ( | |
| f"{final_answer}\n\nTherefore, use this as a compact verified procedure: state the claim, attach evidence, " | |
| "run an independent check, and keep uncertainty visible." | |
| ), | |
| "claim": "teacher seed teaches reusable compact expert procedure", | |
| "evidence": f"apexdistill_100_step_compact:{row.get('id')}:sha256:{content_hash}", | |
| "verification": "Recompute the apexdistill record hash and verify static safety checks passed.", | |
| "source": "local_apexdistill_100_step_compact_seed", | |
| "license": "internal-clean", | |
| "quality_score": quality_score, | |
| "rarity_score": 0.91, | |
| "junk_score": 0.0, | |
| "text": f"Question: {task}\nAnswer: {final_answer}\nEvidence: {content_hash}", | |
| } | |
| def run_compact_teacher_train( | |
| out_dir: str | Path, | |
| hf_train_path: str | Path, | |
| hf_eval_path: str | Path, | |
| teacher_jsonl_path: str | Path, | |
| train_steps: int = 100, | |
| seed: int = 20260523, | |
| ) -> dict: | |
| out = Path(out_dir) | |
| out.mkdir(parents=True, exist_ok=True) | |
| train_dir = out / "train_eval" | |
| hf_rows = _read_jsonl(hf_train_path) + _read_jsonl(hf_eval_path) | |
| teacher_rows = [_teacher_to_cev(row) for row in _read_jsonl(teacher_jsonl_path)] | |
| rows = hf_rows + teacher_rows | |
| audit = audit_pure_records(rows) | |
| natural = evaluate_natural_answer_style(rows) | |
| source_index = SourceTraceIndex.from_records(rows) | |
| source_meta = source_index.write(out / "compact_teacher_source_trace_index.json") | |
| train = run_local_train_eval_bundle( | |
| train_dir, | |
| train_steps=train_steps, | |
| context_lengths=(32, 128, 1024), | |
| seed=seed, | |
| records=rows, | |
| ) | |
| report = { | |
| "schema_version": "tinymind-compact-teacher-training-v1", | |
| "created_at": datetime.now(timezone.utc).isoformat(), | |
| "goal": "100-step compact curriculum using HF-pure rows plus verified local teacher seeds", | |
| "records": { | |
| "hf_records": len(hf_rows), | |
| "teacher_records": len(teacher_rows), | |
| "total_records": len(rows), | |
| }, | |
| "audit": audit, | |
| "natural_answer_style": natural, | |
| "source_trace": source_meta, | |
| "train_eval": train.get("train_eval", {}), | |
| "artifacts": train.get("artifacts", {}), | |
| "compact_teacher_gate": { | |
| "passed": bool(audit["blocked_records"] == 0 and natural["score"] >= 0.75 and train.get("artifacts")), | |
| "world_best_claim_allowed": False, | |
| "teacher_frontier_api_used": False, | |
| "reason": "No external teacher API key was available in this process; this run uses verified local teacher seeds and HF-pure data.", | |
| }, | |
| } | |
| json_path = out / "compact_teacher_training_report.json" | |
| md_path = out / "compact_teacher_training_report.md" | |
| report["json_path"] = str(json_path) | |
| report["markdown_path"] = str(md_path) | |
| json_path.write_text(json.dumps(report, ensure_ascii=False, indent=2, sort_keys=True), encoding="utf-8") | |
| md_path.write_text( | |
| "\n".join( | |
| [ | |
| "# TinyMind Compact Teacher Training", | |
| "", | |
| f"- Gate: {report['compact_teacher_gate']['passed']}", | |
| f"- Total records: {report['records']['total_records']}", | |
| f"- HF records: {report['records']['hf_records']}", | |
| f"- Teacher records: {report['records']['teacher_records']}", | |
| f"- Audit purity: {audit['purity_score']:.2%}", | |
| f"- Natural answer score: {natural['score']:.2%}", | |
| f"- Eval loss: {report['train_eval'].get('eval_loss')}", | |
| f"- Perplexity: {report['train_eval'].get('perplexity')}", | |
| "- World-best claim allowed: false", | |
| "", | |
| ] | |
| ), | |
| encoding="utf-8", | |
| ) | |
| return report | |
Xet Storage Details
- Size:
- 5.44 kB
- Xet hash:
- 8d3ff5c1a5f9801df8662a41613c2775d97992e9178941acd15d0cc9c4a80f0a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.