zefancainlp's picture
download
raw
17.5 kB
# 中文源码导读(2026-09-15)|阅读顺序 047|本地工程及扩展
# 来源:官方同路径无此文件。
# 作用:解析命令和 YAML,锁定恢复配置,启动 vLLM 池和 torchrun;本地另接入 Qwen/六卡 profile。
# 对照基准:test-time-training/discover@6c40e82;完整路线见 docs/CODE_READING_ORDER_ZH.md。
"""Own only the sampler/trainer processes started for this run."""
from __future__ import annotations
import argparse
from datetime import datetime, timezone
import json
import os
from pathlib import Path
import signal
import socket
import subprocess
import sys
import tempfile
import time
import yaml
import psutil
from ttt_discover.runtime.topology import SIX_GPU_PROFILE, apply_six_gpu_profile, validate_topology
ROOT = Path(__file__).resolve().parents[2]
def write_json(path, value):
temporary = path.with_suffix(path.suffix + ".tmp")
temporary.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n")
temporary.replace(path)
# 【本地工程及扩展】resolve
# 解析新运行或恢复配置;保存实际使用的参数,防止恢复时悄悄切换方法、GPU 或预算。
def resolve(args):
path = args.run_dir.expanduser().resolve()
model_profile = getattr(args, "model_profile", None)
saved_model = None
if args.resume:
saved_model = json.loads((path / "resolved_config.json").read_text()).get("model_name")
if model_profile == "qwen3-8b" or saved_model == "Qwen/Qwen3-8B":
if model_profile == "gpt-oss-120b":
raise ValueError("Cannot resume a Qwen checkpoint as GPT-OSS")
from ttt_discover.runtime.qwen_profile import resolve_qwen
return resolve_qwen(args)
if args.task == "cp32":
raise ValueError(
"CP32 currently requires --model-profile qwen3-8b "
"--method baseline"
)
if any(getattr(args, key, None) is not None for key in (
"gpus", "memory_profile", "sampler_memory_fraction", "sampler_max_seqs", "sampler_token_budget", "base_port"
)):
raise ValueError("Qwen performance options require --model-profile qwen3-8b")
rollout_gpus = getattr(args, "rollout_gpus", None)
execution_profile = getattr(args, "execution_profile", None)
parent_mode = getattr(args, "inherit_parent_values", None)
if parent_mode is not None and args.task not in {"ac1", "ac2"}:
raise ValueError(
"--inherit-parent-values is only supported for AC1 and AC2"
)
requested_gpu_groups = None
if rollout_gpus is not None:
profiles = yaml.safe_load((ROOT / "configs/rollout_gpu_profiles.yaml").read_text())
if rollout_gpus not in profiles:
raise ValueError("rollout_gpus must be 6 or 8")
requested_gpu_groups = profiles[rollout_gpus]
if args.resume:
c = json.loads((path / "resolved_config.json").read_text())
if parent_mode is not None and parent_mode != c.get("inherit_parent_values", False):
raise ValueError("Resume preserves the saved parent-value mode; use a new run directory")
if execution_profile is not None and c.get("execution_profile") != execution_profile:
raise ValueError("Resume keeps the saved training topology; convert the checkpoint into a new run first")
if Path(c["run_dir"]).resolve() != path:
raise ValueError("Resume requires the original absolute output path; copied checkpoint indexes contain absolute paths")
if c["task"] != args.task or c["model_path"] != str(args.model_path.expanduser().resolve()):
raise ValueError("Resume task/model must match resolved_config.json")
if requested_gpu_groups is not None and c["sampler_gpu_groups"] != requested_gpu_groups:
raise ValueError("Resume preserves the saved rollout GPU topology. Use a new run directory to select another profile.")
if args.updates is not None:
if args.updates < c["updates"]:
raise ValueError("Cannot reduce the update target on resume")
c["updates"] = args.updates
else:
if path.exists() and any(path.iterdir()):
raise ValueError("Run directory is not empty. Use --resume for the same run.")
c = yaml.safe_load((ROOT / "configs/h100_8gpu.yaml").read_text())
c.update(yaml.safe_load((ROOT / f"configs/{args.task}.yaml").read_text()))
if args.task in {"ac1", "ac2"}:
c["inherit_parent_values"] = bool(parent_mode)
if requested_gpu_groups is not None:
c["sampler_gpu_groups"] = requested_gpu_groups
if execution_profile == SIX_GPU_PROFILE:
if rollout_gpus not in (None, 6):
raise ValueError("six-gpu-tp4 only supports six rollout GPUs")
apply_six_gpu_profile(c)
c.update(model_path=str(args.model_path.expanduser().resolve()), run_dir=str(path),
experiment_name=args.task + "-pfg", smoke=args.smoke,
created_at_utc=datetime.now(timezone.utc).isoformat())
if args.smoke:
# Only the rollout/update budget changes. Prompt, context, evaluator,
# optimizer, LoRA and GPU topology are identical to production.
c.update(groups_per_batch=1, group_size=8, updates=1)
if args.updates is not None:
c["updates"] = args.updates
if c["updates"] < 1:
raise ValueError("updates must be positive")
# CPU evaluation may run either in a private local Ray instance or in an
# externally managed multi-node Ray cluster. Keep this runtime choice in
# the resolved config so the actual evaluator topology is auditable.
requested_ray_address = getattr(args, "ray_address", None)
if requested_ray_address is not None:
c["ray_address"] = requested_ray_address
else:
c.setdefault("ray_address", os.environ.get("RAY_ADDRESS", "local"))
requested_ray_namespace = getattr(args, "ray_namespace", None)
if requested_ray_namespace is not None:
c["ray_namespace"] = requested_ray_namespace
else:
c.setdefault("ray_namespace", f"pfg-{path.name}")
c["logged_batches"] = c["updates"] + int(c["root"] == "fresh_baseline")
c["vllm_urls"] = [f'http://127.0.0.1:{c["base_port"] + i}' for i in range(len(c["sampler_gpu_groups"]))]
temp_root = getattr(args, "temp_root", None)
if temp_root is not None:
c["runtime_tmp_parent"] = str(temp_root.expanduser().resolve())
validate_topology(c)
return c
def stop_owned(process, timeout=60):
if process is None or process.poll() is not None:
return
os.killpg(process.pid, signal.SIGTERM)
try:
process.wait(timeout=timeout)
except subprocess.TimeoutExpired:
os.killpg(process.pid, signal.SIGKILL)
process.wait()
# 【本地工程及扩展】pool_command
# 构造采样服务启动命令;TP=2 副本数与训练 torchrun rank 数分别配置。
def pool_command(c):
qwen = c["model_name"] == "Qwen/Qwen3-8B"
sleep = c.get("vllm_sleep_during_training", True)
serve_bin = ROOT / ".venv-serve/bin/vllm"
command = [str(ROOT / ".venv-serve/bin/python"), str(ROOT / "ttt_discover/runtime/vllm_pool.py"),
"--model-path", c["model_path"], "--gpu-groups", ";".join(",".join(map(str, g)) for g in c["sampler_gpu_groups"]),
"--host", "127.0.0.1", "--base-port", str(c["base_port"]), "--served-model-name", c["model_name"],
"--gpu-memory-utilization", str(c["gpu_memory_utilization"]), "--max-model-len", str(c["max_model_len"]),
"--max-num-seqs", str(c["max_num_seqs"]), "--max-num-batched-tokens", str(c["max_num_batched_tokens"]),
"--max-lora-rank", str(c["lora_rank"]), "--startup-timeout", "1800", "--log-dir", str(Path(c["run_dir"]) / "vllm"),
"--vllm-bin", str(serve_bin), "--offline", "--prefix-caching"]
if qwen:
command += ["--model-family", "qwen3", "--lora-dtype", "bfloat16"]
else:
command.append("--fully-sharded-loras")
if sleep:
command += ["--enable-sleep-mode", "--sleep-after-ready"]
return command
# 【本地工程及扩展】main
# 先核对资源并启动采样池,再启动 torchrun;运行后验收 checkpoint 和真实更新。
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"task",
choices=["denoising", "erdos", "ac1", "ac2", "cp26", "cp32"],
)
parser.add_argument("--model-path", type=Path, required=True)
parser.add_argument("--run-dir", type=Path, required=True)
parser.add_argument("--smoke", action="store_true")
parser.add_argument("--rollout-gpus", type=int, choices=[6, 8],
help="Rollout GPU count: 6 uses GPUs 2-7; 8 uses GPUs 0-7, in TP=2 replicas. Training always uses all 8 GPUs. Resume keeps the saved topology.")
parser.add_argument("--resume", action="store_true")
parser.add_argument("--inherit-parent-values", action=argparse.BooleanOptionalAction,
default=None, help="AC1/AC2: keep fixed Step-0 Z and additionally supply selected-parent P. Resume keeps the saved mode.")
parser.add_argument("--probe-resume", action="store_true",
help="Isolated TP4 load/sampling/32K-backward/Adam/save test; never advances the real run")
parser.add_argument("--execution-profile", choices=[SIX_GPU_PROFILE],
help="Six GPUs total: sampling 2-7, TP=4 training 2,3,6,7; requires converted state for resume")
parser.add_argument("--updates", type=int)
parser.add_argument("--temp-root", type=Path,
help="Short existing directory with room for Ray/sandbox temporary files; saved for resume")
parser.add_argument("--ray-address",
help="External Ray head address for distributed CPU evaluation; default is a private local Ray instance")
parser.add_argument("--ray-namespace",
help="Ray namespace for this run; defaults to a name derived from run-dir")
parser.add_argument("--dry-run", action="store_true")
parser.add_argument("--model-profile", choices=["gpt-oss-120b", "qwen3-8b"])
parser.add_argument(
"--method",
choices=[
"single-anchor",
"baseline",
"no-rl-reflection",
"modular-root",
],
help=(
"Circle-packing/Qwen search method. CP32 currently requires "
"--method baseline."
),
)
parser.add_argument(
"--root-artifact",
type=Path,
help=(
"Validated imported root artifact. Required by CP26 "
"no-rl-reflection and preserved across resume."
),
)
parser.add_argument("--gpus", help="Qwen only: explicit physical GPU IDs; default 2,3,4,5,6,7")
parser.add_argument("--memory-profile", choices=["colocated", "phased"], help="Qwen only; default phased (92% sampler allocation)")
parser.add_argument("--sampler-memory-fraction", type=float, help="Qwen only; never 1.0")
parser.add_argument("--sampler-max-seqs", type=int, help="Qwen only: serving concurrency, not RL batch size")
parser.add_argument("--sampler-token-budget", type=int, help="Qwen only: tokens per serving scheduler iteration")
parser.add_argument("--base-port", type=int, help="Qwen only: default 8300")
args = parser.parse_args()
c = resolve(args)
if args.probe_resume and (not args.resume or c.get("execution_profile") != SIX_GPU_PROFILE):
raise ValueError("--probe-resume requires a converted six-gpu-tp4 run and --resume")
if args.dry_run:
print(json.dumps(c, indent=2)); return
from ttt_discover.runtime.preflight import check
check(c, require_idle=True)
for i in range(len(c["sampler_gpu_groups"])):
with socket.socket() as sock:
sock.bind(("127.0.0.1", c["base_port"] + i))
run_dir = Path(c["run_dir"])
if args.probe_resume:
run_dir = run_dir / "resume_probe"
run_dir.mkdir(parents=True, exist_ok=True)
write_json(run_dir / "resolved_config.json", c)
write_json(run_dir / "status.json", {"status": "starting", "started_at_utc": datetime.now(timezone.utc).isoformat()})
pool_cmd = pool_command(dict(c, run_dir=str(run_dir)))
train_cmd = [str(Path(sys.executable).with_name("torchrun")), "--standalone", f'--nproc-per-node={len(c["train_gpus"])}',
"--module", "ttt_discover.runtime.resume_probe" if args.probe_resume else "ttt_discover.runtime.train",
"--resolved", str(run_dir / "resolved_config.json")]
env = os.environ.copy()
env.pop("PYTHONPATH", None)
cpu_threads = str(c["num_cpus_per_task"])
env.update(PYTHONNOUSERSITE="1", PYTHONUNBUFFERED="1", TOKENIZERS_PARALLELISM="false",
HF_HOME=str(ROOT / ".cache/huggingface"), HF_HUB_OFFLINE="1", TRANSFORMERS_OFFLINE="1",
OPENPROBLEMS_CACHE_DIR=str(ROOT / ".cache/openproblems"), WANDB_MODE="disabled", TTT_WANDB_TABLES="0",
RAY_ADDRESS=str(c.get("ray_address", "local")),
RAY_NAMESPACE=str(c.get("ray_namespace", "pfg-" + str(os.getpid()))),
TTT_RECREATE_CPU_SCHEDULER="1",
TTT_SANDBOX_TRANSPORT="ray_payload", TTT_SANDBOX_MEMORY_GB="96",
TTT_RAY_RUNTIME_PYTHONPATH=str(ROOT),
OMP_NUM_THREADS=cpu_threads, MKL_NUM_THREADS=cpu_threads,
OPENBLAS_NUM_THREADS=cpu_threads, NUMEXPR_NUM_THREADS=cpu_threads,
CUDA_VISIBLE_DEVICES=",".join(map(str, c["train_gpus"])),
PYTORCH_CUDA_ALLOC_CONF="expandable_segments:True", LOCAL_GPU_NCCL_TIMEOUT_SECONDS="600",
LOCAL_GPU_CONTROL_TIMEOUT_SECONDS="86400", TORCH_NCCL_ASYNC_ERROR_HANDLING="1",
LOCAL_GPU_LOG_OPTIMIZER_STAGES="1")
# Ray Unix socket names have a strict length limit.
ray_tmp = tempfile.mkdtemp(prefix="pfg-", dir=c.get("runtime_tmp_parent"))
env.update(RAY_TMPDIR=ray_tmp, TMPDIR=ray_tmp)
pool = trainer = None
from ttt_discover.runtime.telemetry import GpuTelemetry
telemetry = GpuTelemetry(run_dir, validate_topology(c), c.get("gpu_telemetry_interval", 0))
def interrupt(signum, frame):
raise KeyboardInterrupt
signal.signal(signal.SIGTERM, interrupt)
signal.signal(signal.SIGINT, interrupt)
status = "failed"
try:
telemetry.start()
with (run_dir / "pool.log").open("ab") as pool_log, (run_dir / "launcher.log").open("ab") as train_log:
# A resume pool gets its own logfile offset; old ready events do not count.
offset = (run_dir / "pool.log").stat().st_size
pool_env = env.copy()
# Expandable segments are a trainer memory optimization. They are
# incompatible with the custom-all-reduce CUDA graph IPC pointers
# in this serving stack. Never pass trainer allocator flags here.
pool_env.pop("PYTORCH_CUDA_ALLOC_CONF", None)
pool_env.pop("PYTORCH_ALLOC_CONF", None)
pool_env["VLLM_CACHE_ROOT"] = str(ROOT / ".cache/vllm")
pool_env["TRITON_CACHE_DIR"] = str(ROOT / ".cache/triton")
pool = subprocess.Popen(pool_cmd, cwd=ROOT, env=pool_env, stdout=pool_log, stderr=subprocess.STDOUT, start_new_session=True)
write_json(run_dir / "processes.json", {"launcher": os.getpid(), "launcher_created": psutil.Process().create_time(), "pool": pool.pid, "trainer": None})
deadline = time.monotonic() + 1900
while True:
if pool.poll() is not None:
raise RuntimeError("Sampler pool exited; inspect pool.log and vllm/*.log")
with (run_dir / "pool.log").open("rb") as handle:
handle.seek(offset)
ready = b'"event": "vllm_pool_ready"' in handle.read()
if ready:
break
if time.monotonic() > deadline:
raise TimeoutError("Sampler startup timed out")
time.sleep(2)
trainer = subprocess.Popen(train_cmd, cwd=ROOT, env=env, stdout=train_log, stderr=subprocess.STDOUT, start_new_session=True)
write_json(run_dir / "status.json", {"status": "running", "started_at_utc": datetime.now(timezone.utc).isoformat()})
write_json(run_dir / "processes.json", {"launcher": os.getpid(), "launcher_created": psutil.Process().create_time(), "pool": pool.pid, "trainer": trainer.pid})
print(f"Training started. Logs: {run_dir / 'launcher.log'}", flush=True)
while trainer.poll() is None:
if pool.poll() is not None:
raise RuntimeError("Sampler pool failed during training")
time.sleep(2)
if trainer.returncode != 0:
raise RuntimeError(f"Trainer exited {trainer.returncode}; inspect launcher.log")
if args.probe_resume:
evidence = json.loads((run_dir / "probe_verification.json").read_text())
else:
from ttt_discover.runtime.verify import verify_run
evidence = verify_run(run_dir)
write_json(run_dir / "verification.json", evidence)
status = "complete"
print(json.dumps(evidence, indent=2), flush=True)
finally:
stop_owned(trainer)
stop_owned(pool, timeout=90)
telemetry.stop()
write_json(run_dir / "status.json", {"status": status, "finished_at_utc": datetime.now(timezone.utc).isoformat()})
if __name__ == "__main__":
main()

Xet Storage Details

Size:
17.5 kB
·
Xet hash:
487bd424ce05ed95ae4b282926f46137b750ec7859f38ce60a571f040c9986a2

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.