|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""V4 1B-scale fwd + bwd smoke on our parquet samples. |
| 3 | +
|
| 4 | +Builds a V4 stack via UnifiedSuperblockV4 sized close to ~1B params, loads |
| 5 | +N batches from the GB10 clang_semantic parquet, runs fwd + bwd, measures |
| 6 | +throughput. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + ~/sources/nanochat/.venv/bin/python scripts/v4_1b_parquet_smoke.py \ |
| 10 | + --seq-len 512 --batch-size 1 --steps 3 --hidden-size 2048 \ |
| 11 | + --layers 12 |
| 12 | +
|
| 13 | +Output: JSON receipt under reports/v4/v4_1b_parquet_<timestamp>.json |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import argparse |
| 19 | +import json |
| 20 | +import statistics |
| 21 | +import sys |
| 22 | +import time |
| 23 | +from dataclasses import asdict, dataclass, field |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 27 | +sys.path.insert(0, str(REPO_ROOT)) |
| 28 | + |
| 29 | +import mlx.core as mx # noqa: E402 |
| 30 | +import mlx.nn as nn # noqa: E402 |
| 31 | + |
| 32 | +from cppmega_mlx.data.parquet_dataset import TokenParquetDataset # noqa: E402 |
| 33 | + |
| 34 | +from cppmega_v4.models.unified_superblock_v4 import UnifiedSuperblockV4 # noqa: E402 |
| 35 | +from cppmega_v4.run_template import BlockSpec, RunTemplate # noqa: E402 |
| 36 | + |
| 37 | + |
| 38 | +DEFAULT_PARQUET = ( |
| 39 | + REPO_ROOT / "data" / "parquet_samples" / "gb10" |
| 40 | + / "clang_semantic_4k_v10" / "val_00000.parquet" |
| 41 | +) |
| 42 | + |
| 43 | + |
| 44 | +def _make_template(hidden_size: int, layers: int, vocab_size: int) -> RunTemplate: |
| 45 | + """Build a V4 template close to ~1B params at hidden=2048, layers=12.""" |
| 46 | + return RunTemplate( |
| 47 | + name=f"v4_smoke_h{hidden_size}_L{layers}", |
| 48 | + hidden_size=hidden_size, |
| 49 | + vocab_size=vocab_size, |
| 50 | + blocks=[ |
| 51 | + # Engram boost first (cheap doc-aware n-gram). |
| 52 | + BlockSpec(kind="engram", repeat=1, params={ |
| 53 | + "num_ngram_layers": 2, "max_ngram_size": 4, |
| 54 | + "num_embed_table_per_ngram": 4, "embed_dim": 64, |
| 55 | + "embed_table_size": 1024, |
| 56 | + }), |
| 57 | + # NSA × layers/3. |
| 58 | + BlockSpec(kind="nsa", repeat=max(1, layers // 3), params={ |
| 59 | + "num_heads": max(1, hidden_size // 64), |
| 60 | + "head_dim": 64, |
| 61 | + "compress_block_size": 64, "select_topk": 16, |
| 62 | + "sliding_window": 256, |
| 63 | + }), |
| 64 | + # CSA+HCA × layers/3. |
| 65 | + BlockSpec(kind="csa_hca", repeat=max(1, layers // 3), params={ |
| 66 | + "num_heads": max(1, hidden_size // 64), |
| 67 | + "head_dim": 64, |
| 68 | + "m_csa": 4, "m_hca": 16, |
| 69 | + }), |
| 70 | + # MLP × remainder. |
| 71 | + BlockSpec(kind="mlp", repeat=max(1, layers - 2 * (layers // 3)), |
| 72 | + params={"intermediate_size": hidden_size * 4}), |
| 73 | + ], |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +@dataclass |
| 78 | +class StepStat: |
| 79 | + step: int |
| 80 | + fwd_ms: float |
| 81 | + bwd_ms: float |
| 82 | + total_ms: float |
| 83 | + loss: float |
| 84 | + |
| 85 | + |
| 86 | +@dataclass |
| 87 | +class RunReceipt: |
| 88 | + template_name: str |
| 89 | + hidden_size: int |
| 90 | + layers: int |
| 91 | + seq_len: int |
| 92 | + batch_size: int |
| 93 | + steps: int |
| 94 | + parquet_path: str |
| 95 | + fwd_median_ms: float |
| 96 | + bwd_median_ms: float |
| 97 | + total_median_ms: float |
| 98 | + tokens_per_sec: float |
| 99 | + per_step: list = field(default_factory=list) |
| 100 | + |
| 101 | + |
| 102 | +def _build_embed_and_head(vocab_size: int, hidden_size: int): |
| 103 | + embed = nn.Embedding(vocab_size, hidden_size) |
| 104 | + head = nn.Linear(hidden_size, vocab_size, bias=False) |
| 105 | + return embed, head |
| 106 | + |
| 107 | + |
| 108 | +def _cross_entropy_loss(logits: mx.array, targets: mx.array, mask: mx.array) -> mx.array: |
| 109 | + """Sum-of-per-token cross-entropy with mask, divided by mask.sum().""" |
| 110 | + logits_f32 = logits.astype(mx.float32) |
| 111 | + # log_softmax(x) = x - logsumexp(x) |
| 112 | + log_probs = logits_f32 - mx.logsumexp(logits_f32, axis=-1, keepdims=True) |
| 113 | + target_lp = mx.take_along_axis( |
| 114 | + log_probs, targets.astype(mx.int32)[..., None], axis=-1, |
| 115 | + )[..., 0] |
| 116 | + neg_lp = -target_lp * mask.astype(mx.float32) |
| 117 | + denom = mx.maximum(mask.astype(mx.float32).sum(), 1.0) |
| 118 | + return neg_lp.sum() / denom |
| 119 | + |
| 120 | + |
| 121 | +def main() -> int: |
| 122 | + parser = argparse.ArgumentParser() |
| 123 | + parser.add_argument("--parquet", type=Path, default=DEFAULT_PARQUET) |
| 124 | + parser.add_argument("--hidden-size", type=int, default=512) |
| 125 | + parser.add_argument("--layers", type=int, default=4) |
| 126 | + parser.add_argument("--vocab-size", type=int, default=32000) |
| 127 | + parser.add_argument("--seq-len", type=int, default=128) |
| 128 | + parser.add_argument("--batch-size", type=int, default=1) |
| 129 | + parser.add_argument("--steps", type=int, default=3) |
| 130 | + parser.add_argument("--out", type=Path, default=REPO_ROOT / "reports" / "v4") |
| 131 | + args = parser.parse_args() |
| 132 | + |
| 133 | + if not args.parquet.exists(): |
| 134 | + print(f"!! parquet not found: {args.parquet}", file=sys.stderr) |
| 135 | + return 1 |
| 136 | + |
| 137 | + template = _make_template(args.hidden_size, args.layers, args.vocab_size) |
| 138 | + sb = UnifiedSuperblockV4(template) |
| 139 | + embed, head = _build_embed_and_head(args.vocab_size, args.hidden_size) |
| 140 | + |
| 141 | + ds = TokenParquetDataset( |
| 142 | + str(args.parquet), seq_len=args.seq_len, batch_size=args.batch_size, |
| 143 | + token_key="token_ids", loop=True, |
| 144 | + ) |
| 145 | + print(f"== V4 1B smoke ==") |
| 146 | + print(f" template: {template.name} ({template.total_blocks()} blocks)") |
| 147 | + print(f" hidden_size: {args.hidden_size}, layers: {args.layers}") |
| 148 | + print(f" seq_len: {args.seq_len}, batch_size: {args.batch_size}, steps: {args.steps}") |
| 149 | + print(f" parquet: {args.parquet}") |
| 150 | + print(f" dataset: {ds.num_samples} samples → {ds.num_batches} batches") |
| 151 | + |
| 152 | + stats: list[StepStat] = [] |
| 153 | + iter_batches = ds.iter_batches() |
| 154 | + for step in range(args.steps): |
| 155 | + batch = next(iter_batches) |
| 156 | + # batch.inputs: [B, T] int32 tokens; batch.targets: [B, T]; batch.target_mask |
| 157 | + token_ids = mx.array(batch.inputs) |
| 158 | + target_ids = mx.array(batch.targets) |
| 159 | + target_mask = mx.array(batch.target_mask) |
| 160 | + # document_ids: structure_ids first column if present, else None. |
| 161 | + doc_ids = None |
| 162 | + |
| 163 | + def fwd_bwd_step(): |
| 164 | + # Build the full computation graph for one step. |
| 165 | + def loss_fn(tok_ids): |
| 166 | + hidden = embed(tok_ids) |
| 167 | + out = sb(tok_ids, hidden, document_ids=doc_ids) |
| 168 | + logits = head(out) |
| 169 | + return _cross_entropy_loss(logits, target_ids, target_mask) |
| 170 | + loss, grad = mx.value_and_grad(loss_fn)(token_ids) |
| 171 | + mx.eval(loss, grad) |
| 172 | + return loss, grad |
| 173 | + |
| 174 | + # Forward-only timing. |
| 175 | + t0 = time.perf_counter() |
| 176 | + hidden = embed(token_ids) |
| 177 | + out = sb(token_ids, hidden, document_ids=doc_ids) |
| 178 | + logits = head(out) |
| 179 | + loss_only = _cross_entropy_loss(logits, target_ids, target_mask) |
| 180 | + mx.eval(loss_only) |
| 181 | + fwd_ms = (time.perf_counter() - t0) * 1000 |
| 182 | + |
| 183 | + # Fwd+bwd timing. |
| 184 | + t1 = time.perf_counter() |
| 185 | + loss, grad = fwd_bwd_step() |
| 186 | + total_ms = (time.perf_counter() - t1) * 1000 |
| 187 | + bwd_ms = max(0.0, total_ms - fwd_ms) |
| 188 | + |
| 189 | + ss = StepStat( |
| 190 | + step=step, fwd_ms=fwd_ms, bwd_ms=bwd_ms, total_ms=total_ms, |
| 191 | + loss=float(loss.item()), |
| 192 | + ) |
| 193 | + stats.append(ss) |
| 194 | + print(f" step {step}: loss={ss.loss:.4f} fwd={fwd_ms:.1f}ms " |
| 195 | + f"total={total_ms:.1f}ms (bwd≈{bwd_ms:.1f}ms)") |
| 196 | + |
| 197 | + fwd_med = statistics.median(s.fwd_ms for s in stats) |
| 198 | + bwd_med = statistics.median(s.bwd_ms for s in stats) |
| 199 | + total_med = statistics.median(s.total_ms for s in stats) |
| 200 | + tok_per_step = args.batch_size * args.seq_len |
| 201 | + tok_per_sec = tok_per_step / (total_med / 1000) if total_med > 0 else 0.0 |
| 202 | + |
| 203 | + receipt = RunReceipt( |
| 204 | + template_name=template.name, |
| 205 | + hidden_size=args.hidden_size, layers=args.layers, |
| 206 | + seq_len=args.seq_len, batch_size=args.batch_size, steps=args.steps, |
| 207 | + parquet_path=str(args.parquet), |
| 208 | + fwd_median_ms=fwd_med, bwd_median_ms=bwd_med, total_median_ms=total_med, |
| 209 | + tokens_per_sec=tok_per_sec, |
| 210 | + per_step=[asdict(s) for s in stats], |
| 211 | + ) |
| 212 | + args.out.mkdir(parents=True, exist_ok=True) |
| 213 | + ts = int(time.time()) |
| 214 | + out_path = args.out / f"v4_parquet_smoke_h{args.hidden_size}_L{args.layers}_{ts}.json" |
| 215 | + out_path.write_text(json.dumps(asdict(receipt), indent=2)) |
| 216 | + print() |
| 217 | + print(f"== summary ==") |
| 218 | + print(f" fwd median: {fwd_med:8.1f} ms") |
| 219 | + print(f" bwd median: {bwd_med:8.1f} ms") |
| 220 | + print(f" total median: {total_med:8.1f} ms") |
| 221 | + print(f" tokens/sec: {tok_per_sec:8.1f}") |
| 222 | + print(f" receipt: {out_path}") |
| 223 | + return 0 |
| 224 | + |
| 225 | + |
| 226 | +if __name__ == "__main__": |
| 227 | + sys.exit(main()) |
0 commit comments