| """Central config for the Armenian LLM TPU project. |
| |
| Everything tunable lives here so train_tpu.py / prepare_data.py / launch.py stay |
| in sync. Values are chosen for a SINGLE TPU v5e/v6e chip (16GB HBM) training from |
| scratch. |
| """ |
| from __future__ import annotations |
|
|
| from dataclasses import dataclass, asdict, field |
| import json |
| import os |
|
|
|
|
| def _default_compute_dtype() -> str: |
| """Compute dtype, chosen by backend at import time. |
| |
| TPU (v5e/v6e, Colab supervisor path) uses bfloat16 — that's the proven, |
| still-running config; leave it untouched. GPU (Kaggle T4x2) has NO bf16 |
| tensor cores (Turing sm_75), so bf16 there de-optimizes to fp32-upcast |
| paths. The Kaggle launcher sets COMPUTE_DTYPE=float16 to use T4's native |
| fp16 tensor cores instead. Env override keeps this file backend-safe: the |
| SAME config.py is pulled from HF CODE_REPO by BOTH the TPU supervisor and |
| the Kaggle kernel, so the split must be by env, never a hardcoded change. |
| """ |
| dt = os.environ.get("COMPUTE_DTYPE", "bfloat16").strip().lower() |
| if dt not in ("bfloat16", "float16", "float32"): |
| raise ValueError(f"COMPUTE_DTYPE must be bfloat16|float16|float32, got {dt!r}") |
| return dt |
|
|
|
|
| def _default_micro_batch() -> int: |
| """Per-device micro-batch, chosen by backend at import time. |
| |
| TPU v5e1 (16GB HBM, single device, no pmap) fits micro=8 -> 8*ga(4) = 32 |
| seq/step. Kaggle T4x2 runs DATA-PARALLEL (pmap) across 2 GPUs, so the same |
| micro=8 would be 8*ga(4)*2 = 64 seq/step AND each T4 tried to allocate an |
| ~8.85GB fp16 buffer -> RESOURCE_EXHAUSTED OOM (T4 has 16GB but XLA-GPU |
| overhead + NCCL clique + fp16 activations eat the headroom). Setting |
| MICRO_BATCH=4 on GPU halves the per-device activation footprint AND makes |
| 4*ga(4)*2dev = 32 seq/step = EXACTLY the TPU baseline, so the LR schedule |
| and the resumed checkpoint stay in sync (identical effective batch/math). |
| Env override keeps config.py backend-safe (same file pulled by both paths). |
| """ |
| mb = os.environ.get("MICRO_BATCH", "8").strip() |
| try: |
| v = int(mb) |
| except ValueError: |
| raise ValueError(f"MICRO_BATCH must be a positive int, got {mb!r}") |
| if v < 1: |
| raise ValueError(f"MICRO_BATCH must be >= 1, got {v}") |
| return v |
|
|
|
|
| |
| |
| |
| |
| HF_USER = "ArthurYeghinyan" |
|
|
| |
| DATA_REPO = f"{HF_USER}/armenian-llm-data" |
| CODE_REPO = f"{HF_USER}/armenian-llm-code" |
| |
| |
| |
| CKPT_REPO = os.environ.get( |
| "CKPT_REPO", f"{HF_USER}/armenian-llm-124m") |
|
|
| |
| |
| |
| |
| SOURCE_DATASET = "HuggingFaceFW/finewiki" |
| SOURCE_CONFIG = "hy" |
| SOURCE_SPLIT = "train" |
| |
| |
|
|
|
|
| @dataclass(frozen=True) |
| class TokenizerConfig: |
| vocab_size: int = 32000 |
| model_type: str = "unigram" |
| character_coverage: float = 0.9998 |
| |
| train_sample_rows: int = 400_000 |
| max_chars_per_row: int = 20_000 |
|
|
|
|
| @dataclass(frozen=True) |
| class ModelConfig: |
| |
| vocab_size: int = 32000 |
| n_layer: int = 12 |
| n_head: int = 12 |
| n_kv_head: int = 12 |
| n_embd: int = 768 |
| seq_len: int = 1024 |
| ffn_mult: float = 8 / 3 |
| rope_theta: float = 10000.0 |
| rms_eps: float = 1e-5 |
| dtype: str = field(default_factory=_default_compute_dtype) |
| tie_embeddings: bool = True |
|
|
| def ffn_hidden(self) -> int: |
| h = int(self.n_embd * self.ffn_mult) |
| return ((h + 255) // 256) * 256 |
|
|
|
|
| @dataclass(frozen=True) |
| class TrainConfig: |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| micro_batch: int = field(default_factory=_default_micro_batch) |
| grad_accum: int = 4 |
| max_steps: int = 60_000 |
| warmup_steps: int = 500 |
| lr: float = 6e-4 |
| min_lr: float = 6e-5 |
| weight_decay: float = 0.1 |
| beta1: float = 0.9 |
| beta2: float = 0.95 |
| grad_clip: float = 1.0 |
| |
| save_every: int = 1000 |
| eval_every: int = 1000 |
| eval_iters: int = 100 |
| log_every: int = 20 |
| seed: int = 1337 |
|
|
|
|
| TOKENIZER = TokenizerConfig() |
| MODEL = ModelConfig() |
| TRAIN = TrainConfig() |
|
|
|
|
| def summary() -> dict: |
| return { |
| "data_repo": DATA_REPO, |
| "code_repo": CODE_REPO, |
| "ckpt_repo": CKPT_REPO, |
| "source": f"{SOURCE_DATASET}:{SOURCE_CONFIG}", |
| "tokenizer": asdict(TOKENIZER), |
| "model": asdict(MODEL), |
| "train": asdict(TRAIN), |
| "ffn_hidden": MODEL.ffn_hidden(), |
| } |
|
|
|
|
| if __name__ == "__main__": |
| print(json.dumps(summary(), indent=2, ensure_ascii=False)) |
|
|