Buckets:
| #!/usr/bin/env python3 | |
| """Evaluate one final 32K checkpoint on one MLRA PPL benchmark.""" | |
| from __future__ import annotations | |
| import argparse | |
| import copy | |
| import json | |
| import os | |
| import re | |
| import subprocess | |
| import sys | |
| from datetime import datetime | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parents[1] | |
| EVAL_ROOT = ROOT / "ppl-evals" | |
| if str(ROOT) not in sys.path: | |
| sys.path.insert(0, str(ROOT)) | |
| from utils.megatron_launcher import build_train_command, load_config, validate_config | |
| MODELS = { | |
| "mla": { | |
| "label": "MLA", | |
| "config": ROOT / "training/mla/longctx_32k/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/mla/longctx_32k/runs/train_32k/checkpoints", | |
| "extra": [ | |
| "--multi-latent-attention", "--kv-lora-rank", "128", | |
| "--qk-pos-emb-head-dim", "32", "--qk-head-dim", "64", | |
| "--v-head-dim", "64", "--rope-type", "rope", | |
| "--no-rope-fusion", "--spec", "training.mla.spec", "layer_spec", | |
| ], | |
| }, | |
| "kvpath_mla_v": { | |
| "label": "KVPath-MLA", | |
| "config": ROOT / "training/kvpath_mla_v/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_mla_v/longctx_32k/runs/train_32k/checkpoints", | |
| "extra": [ | |
| "--multi-latent-attention", "--kv-lora-rank", "384", | |
| "--qk-pos-emb-head-dim", "32", "--qk-head-dim", "64", | |
| "--v-head-dim", "64", "--rope-type", "rope", | |
| "--no-rope-fusion", "--attention-backend", "fused", | |
| "--spec", "training.kvpath_mla_v.spec", "layer_spec", | |
| ], | |
| }, | |
| "kvpath_delta_mla": { | |
| "label": "KVPath-Delta-MLA", | |
| "config": ROOT / "training/kvpath_delta_mla/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_delta_mla/runs/train_32k/checkpoints", | |
| "extra": [ | |
| "--multi-latent-attention", "--kv-lora-rank", "320", | |
| "--qk-pos-emb-head-dim", "32", "--qk-head-dim", "64", | |
| "--v-head-dim", "64", "--rope-type", "rope", | |
| "--no-rope-fusion", "--attention-backend", "fused", | |
| "--spec", "training.kvpath_delta_mla.spec", "layer_spec", | |
| ], | |
| }, | |
| "kvpath_mla_v_37_5": { | |
| "label": "KVPath-MLA-V-37.5", | |
| "config": ROOT / "training/kvpath_mla_v_37_5/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_mla_v_37_5/runs/train_32k/checkpoints", | |
| "extra": [ | |
| "--multi-latent-attention", "--kv-lora-rank", "256", | |
| "--qk-pos-emb-head-dim", "18", "--qk-head-dim", "62", | |
| "--v-head-dim", "62", "--rope-type", "rope", | |
| "--no-rope-fusion", "--attention-backend", "fused", | |
| "--spec", "training.kvpath_mla_v_37_5.spec", "layer_spec", | |
| ], | |
| }, | |
| "kvpath_mla_v_50": { | |
| "label": "KVPath-MLA-V-50", | |
| "config": ROOT / "training/kvpath_mla_v_50/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_mla_v_50/runs/train_32k/checkpoints", | |
| "extra": [ | |
| "--multi-latent-attention", "--kv-lora-rank", "256", | |
| "--qk-pos-emb-head-dim", "8", "--qk-head-dim", "72", | |
| "--v-head-dim", "72", "--rope-type", "rope", | |
| "--no-rope-fusion", "--attention-backend", "fused", | |
| "--spec", "training.kvpath_mla_v_50.spec", "layer_spec", | |
| ], | |
| }, | |
| "gqa": { | |
| "label": "GQA", | |
| "config": ROOT / "arxiv/gqa/gqa_ctx32k_stage1b_step0001908/config.toml", | |
| "checkpoint": ROOT / "arxiv/gqa/gqa_ctx32k_stage1b_step0001908/checkpoints", | |
| "extra": [], | |
| }, | |
| "roll16": { | |
| "label": "Rolling-r16 (no refresh)", | |
| "config": ROOT / "training/kvpath2_rolling_head64_roll16/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath2_rolling_head64_roll16/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath2_rolling_head64_roll16.entry_spec", "spec"], | |
| }, | |
| "roll32": { | |
| "label": "Rolling-r32 (no refresh)", | |
| "config": ROOT / "training/kvpath2_rolling_head64_roll32/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath2_rolling_head64_roll32/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath2_rolling_head64_roll32.entry_spec", "spec"], | |
| }, | |
| "roll16_refresh4": { | |
| "label": "Rolling-r16 (refresh every 4 layers)", | |
| "config": ROOT / "training/kvpath2_rolling_head64_roll16_base4_refresh/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath2_rolling_head64_roll16_base4_refresh/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath2_rolling_head64_roll16_base4_refresh.entry_spec", "spec"], | |
| }, | |
| "sharedproj_roll16_local128": { | |
| "label": "SharedProj-r16-local128", | |
| "config": ROOT / "training/kvpath2_rolling_head64_roll16_local128_sharedproj/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath2_rolling_head64_roll16_local128_sharedproj/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath2_rolling_head64_roll16_local128_sharedproj.entry_spec", "spec"], | |
| }, | |
| "sharedproj_roll16_local128_fixed": { | |
| "label": "SharedProj-r16-local128 (corrected backward)", | |
| "config": ROOT / "training/kvpath2_rolling_head64_roll16_local128_sharedproj/configs/fixed_train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath2_rolling_head64_roll16_local128_sharedproj/runs_fixed_20261024/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath2_rolling_head64_roll16_local128_sharedproj.entry_spec", "spec"], | |
| }, | |
| "sharedproj_roll32_local128": { | |
| "label": "SharedProj-r32-local128", | |
| "config": ROOT / "training/kvpath2_rolling_head64_roll32_local128_sharedproj/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath2_rolling_head64_roll32_local128_sharedproj/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath2_rolling_head64_roll32_local128_sharedproj.entry_spec", "spec"], | |
| }, | |
| "sharedproj_roll32_local128_fixed": { | |
| "label": "SharedProj-r32-local128 (corrected backward)", | |
| "config": ROOT / "training/kvpath2_rolling_head64_roll32_local128_sharedproj/configs/fixed_train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath2_rolling_head64_roll32_local128_sharedproj/runs_fixed_20261024/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath2_rolling_head64_roll32_local128_sharedproj.entry_spec", "spec"], | |
| }, | |
| "kvpath": { | |
| "label": "KVPath-Concat", | |
| "config": ROOT / "arxiv/kvpath/kvpath_ctx32k_stage1b_step0001908/config.toml", | |
| "checkpoint": ROOT / "arxiv/kvpath/kvpath_ctx32k_stage1b_step0001908/checkpoints", | |
| "extra": [], | |
| }, | |
| "kvpath_rolling": { | |
| "label": "KVPath-Rolling", | |
| "config": ROOT / "training/kvpath_rolling/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_rolling/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath_rolling.entry_spec", "spec"], | |
| }, | |
| "kvpath_rolling_fullrope": { | |
| "label": "KVPath-Rolling-FullRoPE", | |
| "config": ROOT / "training/kvpath_rolling_fullrope/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_rolling_fullrope/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath_rolling_fullrope.entry_spec", "spec"], | |
| }, | |
| "kvpath_64_16": { | |
| "label": "KVPath-64+16", | |
| "config": ROOT / "training/kvpath_64_16/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_64_16/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath_64_16.entry_spec", "spec"], | |
| }, | |
| "kvpath_96_24": { | |
| "label": "KVPath-96+24", | |
| "config": ROOT / "training/kvpath_96_24/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_96_24/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath_96_24.entry_spec", "spec"], | |
| }, | |
| "kvpath_96_24_alpha": { | |
| "label": "KVPath-96+24-alpha", | |
| "config": ROOT / "training/kvpath_96_24_alpha/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_96_24_alpha/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath_96_24_alpha.entry_spec", "spec"], | |
| }, | |
| "kvpath_80_20_local": { | |
| "label": "KVPath-80+20", | |
| "config": ROOT / "training/kvpath_80_20_local/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_80_20_local/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.kvpath_80_20_local.entry_spec", "spec"], | |
| }, | |
| "residual": { | |
| "label": "KVPath-Residual", | |
| "config": ROOT / "training/kvpath_delta/overall_delta/train_32k.toml", | |
| "checkpoint": ROOT / "training/kvpath_delta/overall_delta/train_32k/checkpoints", | |
| "extra": ["--no-rope-fusion"], | |
| }, | |
| "cla": { | |
| "label": "CLA", | |
| "config": ROOT / "arxiv/cla/cla_ctx32k_stage1b_step0002180/config.toml", | |
| "checkpoint": ROOT / "arxiv/cla/cla_ctx32k_stage1b_step0002180/checkpoints", | |
| "extra": ["--spec", "training.cla.cla.entry_spec", "spec"], | |
| }, | |
| "cla4_h64": { | |
| "label": "CLA4-h64", | |
| "config": ROOT / "training/cla4_h64/configs/train_32k.toml", | |
| "checkpoint": ROOT / "training/cla4_h64/runs/train_32k/checkpoints", | |
| "extra": ["--spec", "training.cla4_h64.entry_spec", "spec"], | |
| }, | |
| } | |
| MODELS = {k: v for k, v in MODELS.items() if k in ('gqa', 'roll16', 'sharedproj_roll16_local128', 'sharedproj_roll16_local128_fixed')} | |
| BENCHMARKS = { | |
| "c4": "C4", | |
| "fineweb_edu": "FineWeb-Edu", | |
| "wikipedia": "Wikipedia", | |
| "pg19": "PG-19", | |
| } | |
| # The four MLRA subsets evaluate exactly 9,994,240 input tokens. | |
| CONTEXTS = { | |
| "2k": {"seq_length": 2048, "batch_size": 8, "eval_iters": 610}, | |
| "8k": {"seq_length": 8192, "batch_size": 2, "eval_iters": 610}, | |
| "32k": {"seq_length": 32768, "batch_size": 1, "eval_iters": 305}, | |
| } | |
| PG19_CONTEXTS = { | |
| "2k": {"seq_length": 2048, "batch_size": 8, "eval_iters": 644}, | |
| "8k": {"seq_length": 8192, "batch_size": 2, "eval_iters": 644}, | |
| "32k": {"seq_length": 32768, "batch_size": 1, "eval_iters": 322}, | |
| } | |
| RESULT_RE = re.compile( | |
| r"validation loss.*?lm loss value:\s*([0-9.eE+-]+)\s*\|\s*" | |
| r"lm loss PPL:\s*([0-9.eE+-]+)" | |
| ) | |
| def runtime_env() -> dict[str, str]: | |
| env = os.environ.copy() | |
| python = Path(sys.executable) | |
| conda_prefix = python.parent.parent | |
| site_packages = conda_prefix / f"lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages" | |
| env["PATH"] = os.pathsep.join((str(python.parent), env.get("PATH", ""))) | |
| env["LD_LIBRARY_PATH"] = os.pathsep.join( | |
| part | |
| for part in ( | |
| str(conda_prefix / "lib"), | |
| str(site_packages / "nvidia/nccl/lib"), | |
| str(site_packages / "nvidia/cudnn/lib"), | |
| env.get("LD_LIBRARY_PATH"), | |
| ) | |
| if part | |
| ) | |
| env["PYTHONNOUSERSITE"] = "1" | |
| env.setdefault("CUDA_DEVICE_MAX_CONNECTIONS", "1") | |
| env["PYTHONPATH"] = os.pathsep.join( | |
| part for part in (str(ROOT), env.get("PYTHONPATH")) if part | |
| ) | |
| return env | |
| def run_one(model_name: str, benchmark: str, context_name: str, port: int) -> dict[str, object]: | |
| model_info = MODELS[model_name] | |
| context = (PG19_CONTEXTS if benchmark == "pg19" else CONTEXTS)[context_name] | |
| checkpoint = model_info["checkpoint"] | |
| config = copy.deepcopy(load_config(model_info["config"])) | |
| config["launcher"].update( | |
| { | |
| "nnodes": 1, | |
| "nproc_per_node": 1, | |
| "node_rank": 0, | |
| "master_addr": "127.0.0.1", | |
| "master_port": port, | |
| } | |
| ) | |
| config["paths"].update( | |
| { | |
| "output_dir": str(checkpoint), | |
| "train_data_prefix": ("ppl-evals/datasets/pg19_test" if benchmark == "pg19" else f"ppl-evals/datasets/{benchmark}_10m"), | |
| "valid_data_prefix": ( | |
| "ppl-evals/datasets/pg19_test" | |
| if benchmark == "pg19" | |
| else f"ppl-evals/datasets/{benchmark}_10m" | |
| ), | |
| } | |
| ) | |
| batch_size = int(context["batch_size"]) | |
| config["data"].update( | |
| { | |
| "seq_length": int(context["seq_length"]), | |
| "micro_batch_size": batch_size, | |
| "global_batch_size": batch_size, | |
| "num_workers": 2, | |
| "data_cache_path": ( | |
| f"ppl-evals/cache/pg19/{context_name}" | |
| if benchmark == "pg19" | |
| else f"ppl-evals/cache/mlra_10m/{benchmark}/{context_name}" | |
| ), | |
| } | |
| ) | |
| config["training"].update( | |
| {"eval_iters": int(context["eval_iters"]), "distributed_optimizer": False} | |
| ) | |
| optimizer = validate_config(config, None) | |
| command = build_train_command( | |
| config, | |
| optimizer, | |
| python_executable=sys.executable, | |
| load_dir=checkpoint, | |
| extra_megatron_args=[ | |
| "--skip-train", | |
| "--no-load-optim", | |
| "--no-load-rng", | |
| "--no-create-attention-mask-in-dataloader", | |
| *model_info["extra"], | |
| ], | |
| ) | |
| log_group = "pg19" if benchmark == "pg19" else "mlra_10m" | |
| log_dir = EVAL_ROOT / f"logs/{log_group}" / model_name / benchmark | |
| log_dir.mkdir(parents=True, exist_ok=True) | |
| log_path = log_dir / f"{context_name}.log" | |
| print( | |
| f"[{datetime.now().isoformat(timespec='seconds')}] {model_info['label']} / " | |
| f"{BENCHMARKS[benchmark]} / {context_name}: starting", | |
| flush=True, | |
| ) | |
| match = None | |
| with log_path.open("w") as log: | |
| process = subprocess.Popen( | |
| command, | |
| cwd=ROOT, | |
| env=runtime_env(), | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT, | |
| text=True, | |
| bufsize=1, | |
| ) | |
| assert process.stdout is not None | |
| for line in process.stdout: | |
| log.write(line) | |
| candidate = RESULT_RE.search(line) | |
| if candidate: | |
| match = candidate | |
| print(line.rstrip(), flush=True) | |
| elif any( | |
| marker in line | |
| for marker in ( | |
| "successfully loaded checkpoint", | |
| "Evaluating iter 1/", | |
| "Traceback (most recent call last)", | |
| "CUDA out of memory", | |
| ) | |
| ): | |
| print(line.rstrip(), flush=True) | |
| return_code = process.wait() | |
| if return_code: | |
| raise RuntimeError(f"evaluation failed; see {log_path}") | |
| if match is None: | |
| raise RuntimeError(f"no validation result found; see {log_path}") | |
| return { | |
| "context_length": int(context["seq_length"]), | |
| "tokens": int(context["seq_length"]) * batch_size * int(context["eval_iters"]), | |
| "loss": float(match.group(1)), | |
| "ppl": float(match.group(2)), | |
| "log": str(log_path.relative_to(ROOT)), | |
| } | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--model", required=True, choices=MODELS) | |
| parser.add_argument("--benchmark", required=True, choices=BENCHMARKS) | |
| parser.add_argument("--contexts", nargs="+", choices=CONTEXTS, default=list(CONTEXTS)) | |
| parser.add_argument("--port", type=int, required=True) | |
| parser.add_argument("--force", action="store_true") | |
| args = parser.parse_args() | |
| result_dir = EVAL_ROOT / ("results_pg19" if args.benchmark == "pg19" else "results_mlra_10m") | |
| result_dir.mkdir(parents=True, exist_ok=True) | |
| result_path = result_dir / f"{args.model}_{args.benchmark}.json" | |
| if result_path.exists(): | |
| results = json.loads(result_path.read_text()) | |
| else: | |
| results = { | |
| "model": args.model, | |
| "model_label": MODELS[args.model]["label"], | |
| "benchmark": args.benchmark, | |
| "benchmark_label": BENCHMARKS[args.benchmark], | |
| "checkpoint": str(MODELS[args.model]["checkpoint"].relative_to(ROOT)), | |
| "results": {}, | |
| } | |
| for offset, context_name in enumerate(args.contexts): | |
| if context_name in results["results"] and not args.force: | |
| print(f"{context_name}: already complete", flush=True) | |
| continue | |
| results["results"][context_name] = run_one( | |
| args.model, args.benchmark, context_name, args.port + offset | |
| ) | |
| result_path.write_text(json.dumps(results, indent=2, sort_keys=True) + "\n") | |
| row = results["results"][context_name] | |
| print(f"{context_name}: loss={row['loss']:.6f}, PPL={row['ppl']:.5f}", flush=True) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 16.6 kB
- Xet hash:
- 293321779f075d5c9537809a1b3584bb7d8329b462724c2f37320ab6174f4965
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.