faz2_train_shards seq_len kirpma + notebook
Browse files
code/kod/SmartCore_V1_Colab_Faz2.ipynb
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"# SmartCore V1 — Faz 2 Colab Smoke (GPU)\n",
|
| 8 |
+
"\n",
|
| 9 |
+
"180M Mamba-3 (SISO) + GQA 5:1 hibrit modelin **smoke testi**: HF'deki pre-tokenize veriden\n",
|
| 10 |
+
"(`kdirgul/smartcore-v1-data`) streaming + model + WSD eğitim döngüsü, GPU'da.\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"**Önkoşul:** Colab **Pro+ / A100** (Runtime > Change runtime type > A100). HF token (private repo erişimi).\n",
|
| 13 |
+
"\n",
|
| 14 |
+
"**Smoke amacı:** veri akışı + model + loop + GPU uçtan uca çalışıyor mu (loss ~10.8'den düşüyor, NaN/OOM yok).\n",
|
| 15 |
+
"**NOT:** Bu smoke saf-PyTorch minimal modeli kullanır (her yerde çalışır, yavaş). **Tam-hız Faz 3** için\n",
|
| 16 |
+
"`mamba-og` fork (Triton SISO kernel) entegre edilecek — ayrı adım."
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"metadata": {},
|
| 22 |
+
"execution_count": null,
|
| 23 |
+
"outputs": [],
|
| 24 |
+
"source": [
|
| 25 |
+
"!nvidia-smi -L\n",
|
| 26 |
+
"!pip -q install einops sentencepiece datasets huggingface_hub pyarrow"
|
| 27 |
+
]
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"cell_type": "markdown",
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"source": [
|
| 33 |
+
"## 1) HF login (private repo erişimi)"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"cell_type": "code",
|
| 38 |
+
"metadata": {},
|
| 39 |
+
"execution_count": null,
|
| 40 |
+
"outputs": [],
|
| 41 |
+
"source": [
|
| 42 |
+
"from huggingface_hub import login, HfFolder\n",
|
| 43 |
+
"login() # token'ı buraya yapıştır"
|
| 44 |
+
]
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"cell_type": "markdown",
|
| 48 |
+
"metadata": {},
|
| 49 |
+
"source": [
|
| 50 |
+
"## 2) Kodu HF'den indir"
|
| 51 |
+
]
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"cell_type": "code",
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"execution_count": null,
|
| 57 |
+
"outputs": [],
|
| 58 |
+
"source": [
|
| 59 |
+
"import os, sys\n",
|
| 60 |
+
"from huggingface_hub import snapshot_download\n",
|
| 61 |
+
"p = snapshot_download(\"kdirgul/smartcore-v1\", allow_patterns=[\"code/**\"], repo_type=\"model\")\n",
|
| 62 |
+
"CODE = os.path.join(p, \"code\")\n",
|
| 63 |
+
"os.environ[\"HF_TOKEN\"] = HfFolder.get_token()\n",
|
| 64 |
+
"print(\"kod dizini:\", CODE)\n",
|
| 65 |
+
"print(os.listdir(os.path.join(CODE, \"kod\"))[:5])"
|
| 66 |
+
]
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"cell_type": "markdown",
|
| 70 |
+
"metadata": {},
|
| 71 |
+
"source": [
|
| 72 |
+
"## 3) Smoke (küçük config — hızlı doğrulama)\n",
|
| 73 |
+
"Pipeline'ı kanıtlar. Tam 180M (d_model=768 n_layer=20 seq=2048) da çalışır ama minimal modelle yavaştır."
|
| 74 |
+
]
|
| 75 |
+
},
|
| 76 |
+
{
|
| 77 |
+
"cell_type": "code",
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"execution_count": null,
|
| 80 |
+
"outputs": [],
|
| 81 |
+
"source": [
|
| 82 |
+
"import subprocess\n",
|
| 83 |
+
"cmd = [\"python\",\"kod/faz2_train_shards.py\",\"--data\",\"kdirgul/smartcore-v1-data\",\"--hf\",\n",
|
| 84 |
+
" \"--d_model\",\"512\",\"--n_layer\",\"12\",\"--seq_len\",\"1024\",\n",
|
| 85 |
+
" \"--micro_batch\",\"8\",\"--steps\",\"40\",\"--device\",\"cuda\"]\n",
|
| 86 |
+
"subprocess.run(cmd, cwd=CODE, env={**os.environ})"
|
| 87 |
+
]
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"cell_type": "markdown",
|
| 91 |
+
"metadata": {},
|
| 92 |
+
"source": [
|
| 93 |
+
"## Başarı kriterleri\n",
|
| 94 |
+
"- loss ~10.8'den (=ln 48000) düşüyor (40 adımda hafif düşüş normal — gerçek veri).\n",
|
| 95 |
+
"- grad_norm stabil (clip=1.0 ile sıçramalar kontrol altında).\n",
|
| 96 |
+
"- NaN/OOM yok.\n",
|
| 97 |
+
"- A100'de throughput makul.\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"## Sıradaki: Faz 3 (tam pretraining)\n",
|
| 100 |
+
"1. **Fork/Triton entegrasyonu:** minimal model yerine `mamba-og` (Triton SISO kernel) → ~10-50× hız.\n",
|
| 101 |
+
" `referans_kod/model.py` (Mamba3LM) + GQA katmanları (`mamba_ssm.modules.mha`) + bf16.\n",
|
| 102 |
+
"2. Tam config: d_model=768, n_layer=20, seq=2048, batch ~0.5M token (grad accum), WSD, 12B token.\n",
|
| 103 |
+
"3. Checkpoint + async HF push + `--resume` (Colab session). `referans_kod/train_180m.py` harness temel alınır.\n",
|
| 104 |
+
"4. Faz 4 decay + Faz 5 eval (TR panel + EN panel)."
|
| 105 |
+
]
|
| 106 |
+
}
|
| 107 |
+
],
|
| 108 |
+
"metadata": {
|
| 109 |
+
"kernelspec": {
|
| 110 |
+
"display_name": "Python 3",
|
| 111 |
+
"name": "python3"
|
| 112 |
+
},
|
| 113 |
+
"language_info": {
|
| 114 |
+
"name": "python"
|
| 115 |
+
},
|
| 116 |
+
"accelerator": "GPU",
|
| 117 |
+
"colab": {
|
| 118 |
+
"provenance": []
|
| 119 |
+
}
|
| 120 |
+
},
|
| 121 |
+
"nbformat": 4,
|
| 122 |
+
"nbformat_minor": 0
|
| 123 |
+
}
|
code/kod/faz2_train_shards.py
CHANGED
|
@@ -28,8 +28,9 @@ def wsd_lr(step, total, peak, floor, warmup_frac=0.02, decay_frac=0.25):
|
|
| 28 |
|
| 29 |
class ShardStream:
|
| 30 |
"""Pre-shard parquet okuyucu (yerel dizin veya HF repo), oranlı karışım."""
|
| 31 |
-
def __init__(self, root, weights, hf=False, token=None):
|
| 32 |
self.names = list(weights); self.w = [weights[n] for n in self.names]
|
|
|
|
| 33 |
self.iters = {n: self._src(root, n, hf, token) for n in self.names}
|
| 34 |
|
| 35 |
def _src(self, root, name, hf, token):
|
|
@@ -48,7 +49,8 @@ class ShardStream:
|
|
| 48 |
yield row.as_py()
|
| 49 |
|
| 50 |
def batch(self, bsz):
|
| 51 |
-
rows = [next(self.iters[random.choices(self.names, weights=self.w, k=1)[0]])
|
|
|
|
| 52 |
return torch.tensor(rows, dtype=torch.long)
|
| 53 |
|
| 54 |
|
|
@@ -83,7 +85,7 @@ def main():
|
|
| 83 |
|
| 84 |
tok = os.environ.get("HF_TOKEN")
|
| 85 |
print(f"veri: {args.data} ({'HF stream' if args.hf else 'yerel parquet'}) | mixture {MIX}")
|
| 86 |
-
stream = ShardStream(args.data, MIX, hf=args.hf, token=tok)
|
| 87 |
|
| 88 |
decay = [p for p in model.parameters() if p.ndim >= 2]
|
| 89 |
nod = [p for p in model.parameters() if p.ndim < 2]
|
|
|
|
| 28 |
|
| 29 |
class ShardStream:
|
| 30 |
"""Pre-shard parquet okuyucu (yerel dizin veya HF repo), oranlı karışım."""
|
| 31 |
+
def __init__(self, root, weights, seq_len, hf=False, token=None):
|
| 32 |
self.names = list(weights); self.w = [weights[n] for n in self.names]
|
| 33 |
+
self.seq_len = seq_len # satırlar 2048; daha kısa smoke için kırp
|
| 34 |
self.iters = {n: self._src(root, n, hf, token) for n in self.names}
|
| 35 |
|
| 36 |
def _src(self, root, name, hf, token):
|
|
|
|
| 49 |
yield row.as_py()
|
| 50 |
|
| 51 |
def batch(self, bsz):
|
| 52 |
+
rows = [next(self.iters[random.choices(self.names, weights=self.w, k=1)[0]])[:self.seq_len]
|
| 53 |
+
for _ in range(bsz)]
|
| 54 |
return torch.tensor(rows, dtype=torch.long)
|
| 55 |
|
| 56 |
|
|
|
|
| 85 |
|
| 86 |
tok = os.environ.get("HF_TOKEN")
|
| 87 |
print(f"veri: {args.data} ({'HF stream' if args.hf else 'yerel parquet'}) | mixture {MIX}")
|
| 88 |
+
stream = ShardStream(args.data, MIX, args.seq_len, hf=args.hf, token=tok)
|
| 89 |
|
| 90 |
decay = [p for p in model.parameters() if p.ndim >= 2]
|
| 91 |
nod = [p for p in model.parameters() if p.ndim < 2]
|