Add code and docs
Browse files- kaggle_distill.ipynb +128 -0
kaggle_distill.ipynb
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "50b7c415",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# AChess — Stockfish distillation on Kaggle CPU\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"This notebook builds a chess distillation dataset on a Kaggle CPU session:\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"- positions from weak Stockfish self-play (Skill 3–7, around SF5),\n",
|
| 13 |
+
"- labels from full-strength Stockfish 19 at depth 12,\n",
|
| 14 |
+
"- Parquet shards uploaded to `xLeverd/chess`.\n",
|
| 15 |
+
"\n",
|
| 16 |
+
"Add `HF_TOKEN` to Kaggle Secrets before running. The token needs write access\n",
|
| 17 |
+
"to the dataset repo."
|
| 18 |
+
]
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"cell_type": "code",
|
| 22 |
+
"execution_count": null,
|
| 23 |
+
"id": "cd57c5eb",
|
| 24 |
+
"metadata": {},
|
| 25 |
+
"outputs": [],
|
| 26 |
+
"source": [
|
| 27 |
+
"!pip -q install python-chess pyarrow huggingface_hub tqdm"
|
| 28 |
+
]
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"cell_type": "code",
|
| 32 |
+
"execution_count": null,
|
| 33 |
+
"id": "723ce789",
|
| 34 |
+
"metadata": {},
|
| 35 |
+
"outputs": [],
|
| 36 |
+
"source": [
|
| 37 |
+
"import os\n",
|
| 38 |
+
"try:\n",
|
| 39 |
+
" from kaggle_secrets import UserSecretsClient\n",
|
| 40 |
+
" os.environ[\"HF_TOKEN\"] = UserSecretsClient().get_secret(\"HF_TOKEN\")\n",
|
| 41 |
+
"except Exception as exc:\n",
|
| 42 |
+
" print(\"Kaggle secret not found:\", exc)\n",
|
| 43 |
+
"\n",
|
| 44 |
+
"!wget -q https://github.com/official-stockfish/Stockfish/releases/download/sf_19/stockfish-linux-x86-64-universal.tar.gz -O /kaggle/working/sf.tar.gz\n",
|
| 45 |
+
"!mkdir -p /kaggle/working/sf && tar -xzf /kaggle/working/sf.tar.gz -C /kaggle/working/sf\n",
|
| 46 |
+
"import glob\n",
|
| 47 |
+
"cands = [p for p in glob.glob('/kaggle/working/sf/**/stockfish*', recursive=True)\n",
|
| 48 |
+
" if os.path.isfile(p) and os.access(p, os.X_OK)]\n",
|
| 49 |
+
"print(cands[:5])\n",
|
| 50 |
+
"assert cands, \"Stockfish binary not found\"\n",
|
| 51 |
+
"STOCKFISH = cands[0]\n",
|
| 52 |
+
"print(\"using\", STOCKFISH)"
|
| 53 |
+
]
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"cell_type": "code",
|
| 57 |
+
"execution_count": null,
|
| 58 |
+
"id": "8eb5b9fa",
|
| 59 |
+
"metadata": {},
|
| 60 |
+
"outputs": [],
|
| 61 |
+
"source": [
|
| 62 |
+
"from huggingface_hub import hf_hub_download\n",
|
| 63 |
+
"script = hf_hub_download(\"xLeverd/chess\", \"distill.py\", repo_type=\"dataset\")\n",
|
| 64 |
+
"print(script)"
|
| 65 |
+
]
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"cell_type": "code",
|
| 69 |
+
"execution_count": null,
|
| 70 |
+
"id": "fb6cccb2",
|
| 71 |
+
"metadata": {},
|
| 72 |
+
"outputs": [],
|
| 73 |
+
"source": [
|
| 74 |
+
"# 4 vCPU on Kaggle CPU. 100k rows at depth 12 is roughly 10–20 min.\n",
|
| 75 |
+
"# Raise --target if the session has time; the script is resumable by row count.\n",
|
| 76 |
+
"!python {script} --target 100000 --workers 4 --depth 12 --chunk 200 --shard-size 25000 --out /kaggle/working/data --stockfish {STOCKFISH}"
|
| 77 |
+
]
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"cell_type": "code",
|
| 81 |
+
"execution_count": null,
|
| 82 |
+
"id": "8cfac94f",
|
| 83 |
+
"metadata": {},
|
| 84 |
+
"outputs": [],
|
| 85 |
+
"source": [
|
| 86 |
+
"import glob, pandas as pd\n",
|
| 87 |
+
"files = sorted(glob.glob('/kaggle/working/data/*.parquet'))\n",
|
| 88 |
+
"df = pd.concat([pd.read_parquet(f) for f in files], ignore_index=True)\n",
|
| 89 |
+
"print(len(df), df.columns.tolist())\n",
|
| 90 |
+
"print(df.head(3).to_string())\n",
|
| 91 |
+
"print(df.wdl.value_counts().to_dict())"
|
| 92 |
+
]
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
"cell_type": "code",
|
| 96 |
+
"execution_count": null,
|
| 97 |
+
"id": "de63c180",
|
| 98 |
+
"metadata": {},
|
| 99 |
+
"outputs": [],
|
| 100 |
+
"source": [
|
| 101 |
+
"from huggingface_hub import HfApi\n",
|
| 102 |
+
"api = HfApi(token=os.environ[\"HF_TOKEN\"])\n",
|
| 103 |
+
"api.create_repo(\"xLeverd/chess\", repo_type=\"dataset\", exist_ok=True)\n",
|
| 104 |
+
"api.upload_folder(\n",
|
| 105 |
+
" repo_id=\"xLeverd/chess\",\n",
|
| 106 |
+
" repo_type=\"dataset\",\n",
|
| 107 |
+
" folder_path=\"/kaggle/working/data\",\n",
|
| 108 |
+
" path_in_repo=\"data\",\n",
|
| 109 |
+
" commit_message=\"Add Kaggle CPU Stockfish distillation shards\",\n",
|
| 110 |
+
")\n",
|
| 111 |
+
"print(\"https://huggingface.co/datasets/xLeverd/chess\")"
|
| 112 |
+
]
|
| 113 |
+
}
|
| 114 |
+
],
|
| 115 |
+
"metadata": {
|
| 116 |
+
"kernelspec": {
|
| 117 |
+
"display_name": "Python 3",
|
| 118 |
+
"language": "python",
|
| 119 |
+
"name": "python3"
|
| 120 |
+
},
|
| 121 |
+
"language_info": {
|
| 122 |
+
"name": "python",
|
| 123 |
+
"version": "3.10"
|
| 124 |
+
}
|
| 125 |
+
},
|
| 126 |
+
"nbformat": 4,
|
| 127 |
+
"nbformat_minor": 5
|
| 128 |
+
}
|