Buckets:
| # 中文源码导读(2026-09-15)|阅读顺序 050|本地工程及方法装配 | |
| # 来源:官方同路径无此文件。 | |
| # 作用:将解析后的配置转换为 DiscoverConfig,选择 ours 环境、加载 AC1 root,并明确关闭历史实验开关。 | |
| # 对照基准:test-time-training/discover@6c40e82;完整路线见 docs/CODE_READING_ORDER_ZH.md。 | |
| """torchrun entry point. Algorithm and evaluator live in their original modules.""" | |
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| import os | |
| from pathlib import Path | |
| from threading import Lock | |
| ROOT = Path(__file__).resolve().parents[2] | |
| # 【本地工程及方法装配】environment_for | |
| # 选择三个 ours 环境;AC1 打包入口校验历史实际入树 root 的长度、分数和源码哈希后加载。 | |
| # 本地父值选项只允许 AC1;fresh_baseline 与 admitted root 的启动语义不同。 | |
| def environment_for(config): | |
| task = config["task"] | |
| if config.get("inherit_parent_values", False) and task not in {"ac1", "ac2"}: | |
| raise ValueError( | |
| "Parent-value inheritance is only supported for AC1 and AC2" | |
| ) | |
| if task == "denoising": | |
| from examples.denoising.single_anchor_persistent_function_graph_env import SingleAnchorPersistentFunctionGraphDenoisingEnv | |
| return SingleAnchorPersistentFunctionGraphDenoisingEnv | |
| if task == "erdos": | |
| from examples.erdos_min_overlap.single_anchor_persistent_function_graph_env import SingleAnchorPFGErdosEnv | |
| return SingleAnchorPFGErdosEnv | |
| if task in {"cp26", "cp32"}: | |
| method = config.get("method", "single-anchor") | |
| if method == "baseline": | |
| from examples.circle_packing.env import CirclePackingEnv | |
| if config["root"] != "fresh_baseline": | |
| raise ValueError( | |
| f"{task.upper()} baseline requires root: fresh_baseline" | |
| ) | |
| return CirclePackingEnv | |
| if task == "cp32": | |
| raise ValueError("CP32 currently supports only method: baseline") | |
| if method == "modular-root": | |
| from examples.circle_packing.single_anchor_modular_root_env import ( | |
| ModularRootSingleAnchorPFGCircleEnv, | |
| ) | |
| if config["root"] != "fresh_baseline": | |
| raise ValueError("CP26 modular root requires root: fresh_baseline") | |
| return ModularRootSingleAnchorPFGCircleEnv | |
| if method == "no-rl-reflection": | |
| import numpy as np | |
| from examples.circle_packing.env import check_packing_correctness | |
| from examples.circle_packing.single_anchor_no_rl_reflection_env import ( | |
| NoRLSingleAnchorReflectionCircleEnv, | |
| ) | |
| from examples.circle_packing.single_anchor_persistent_function_graph import ( | |
| BOOTSTRAP_PHASE, | |
| ) | |
| from examples.circle_packing.single_anchor_persistent_function_graph_env import ( | |
| SingleAnchorPFGCircleState, | |
| decode_packing_artifact, | |
| promote_bootstrap_winner, | |
| ) | |
| artifact_path = Path(config["root_artifact"]) | |
| artifact = json.loads(artifact_path.read_text(encoding="utf-8")) | |
| if artifact.get("task") != "cp26" or int(artifact.get("num_circles", 0)) != 26: | |
| raise ValueError("imported no-RL artifact is not CP26") | |
| program = str(artifact["winner_program_source"]) | |
| if hashlib.sha256(program.encode()).hexdigest() != artifact["winner_program_sha256"]: | |
| raise ValueError("imported CP26 winner program hash mismatch") | |
| construction = list(artifact["winner_construction"]) | |
| centers, radii = decode_packing_artifact(construction, 26) | |
| centers_array = np.asarray(centers, dtype=np.float64) | |
| radii_array = np.asarray(radii, dtype=np.float64) | |
| if not check_packing_correctness(centers_array, radii_array, 26): | |
| raise ValueError("imported CP26 winner packing is invalid") | |
| score = float(np.sum(radii_array)) | |
| if abs(score - float(artifact["winner_value"])) >= 1e-12: | |
| raise ValueError("imported CP26 winner score does not match packing") | |
| reflection = str(artifact.get("step0_reflection") or "").strip() | |
| if not reflection: | |
| raise ValueError("imported CP26 artifact has no Step-0 reflection") | |
| reflection_summary = dict(artifact.get("step0_reflection_summary") or {}) | |
| winner = SingleAnchorPFGCircleState( | |
| id=str(artifact["winner_id"]), | |
| timestep=0, | |
| construction=construction, | |
| code=program, | |
| value=score, | |
| terminal_value=score, | |
| observation="", | |
| phase=BOOTSTRAP_PHASE, | |
| num_circles=26, | |
| graph_source=program, | |
| patch_history=[program], | |
| ) | |
| valid_candidates = int(artifact.get("valid_candidates", 0)) | |
| class AdmittedBaselineStep0NoRLCircleEnv( | |
| NoRLSingleAnchorReflectionCircleEnv | |
| ): | |
| initial_batch_offset = 1 | |
| initial_step_reflection = reflection | |
| initial_step_reflection_summary = reflection_summary | |
| def create_initial_state(cls, problem_type): | |
| if str(problem_type) != "26": | |
| raise ValueError( | |
| "AdmittedBaselineStep0NoRLCircleEnv only supports CP26" | |
| ) | |
| return promote_bootstrap_winner( | |
| winner, | |
| copies=1, | |
| valid_candidates=valid_candidates, | |
| )[0] | |
| return AdmittedBaselineStep0NoRLCircleEnv | |
| from examples.circle_packing.single_anchor_persistent_function_graph_env import ( | |
| SingleAnchorPFGCircleEnv, | |
| ) | |
| if config["root"] != "fresh_baseline": | |
| raise ValueError("CP26 requires root: fresh_baseline") | |
| return SingleAnchorPFGCircleEnv | |
| if task == "ac2": | |
| from examples.ac_inequalities.ac2_single_anchor_persistent_function_graph_env import ( | |
| ParentValueSingleAnchorPFGAC2Env, | |
| SingleAnchorPFGAC2Env, | |
| ) | |
| if config["root"] != "fresh_baseline": | |
| raise ValueError("AC2 currently requires root: fresh_baseline") | |
| return ( | |
| ParentValueSingleAnchorPFGAC2Env | |
| if config.get("inherit_parent_values", False) | |
| else SingleAnchorPFGAC2Env | |
| ) | |
| from examples.ac_inequalities.ac1_single_anchor_persistent_function_graph_env import ( | |
| BaselineFrontierSingleAnchorPFGAC1Env, | |
| SingleAnchorPFGAC1Env, ParentValueSingleAnchorPFGAC1Env, | |
| SingleAnchorPFGAC1State, promote_bootstrap_winner, | |
| ) | |
| from examples.ac_inequalities.ac1_single_anchor_persistent_function_graph import BOOTSTRAP_PHASE | |
| from examples.ac_inequalities.env import evaluate_sequence_ac1 | |
| parent_mode = bool(config.get("inherit_parent_values", False)) | |
| base_env = ParentValueSingleAnchorPFGAC1Env if parent_mode else SingleAnchorPFGAC1Env | |
| if config["root"] == "fresh_baseline": | |
| return base_env | |
| artifact_path = ROOT / config["root_artifact"] | |
| artifact = json.loads(artifact_path.read_text()) | |
| if config["root"] == "admitted_api_frontier": | |
| if parent_mode: | |
| raise ValueError("The admitted API frontier is the no-P AC1 design") | |
| entries = list(artifact.get("parents") or []) | |
| expected = int(config["groups_per_batch"]) | |
| if len(entries) != expected: | |
| raise ValueError( | |
| f"AC1 frontier has {len(entries)} parents, expected {expected}" | |
| ) | |
| roots = [] | |
| for entry in entries: | |
| values = entry["construction"] | |
| if not 1000 <= len(values) <= 100000: | |
| raise ValueError("AC1 frontier parent length is outside [1000, 100000]") | |
| score = float(evaluate_sequence_ac1(values)) | |
| if abs(score - float(entry["raw_score"])) >= 1e-12: | |
| raise ValueError("AC1 frontier parent score does not match its array") | |
| program = entry["program"] | |
| if hashlib.sha256(program.encode()).hexdigest() != entry["program_sha256"]: | |
| raise ValueError("AC1 frontier parent program hash mismatch") | |
| winner = SingleAnchorPFGAC1State( | |
| id=entry["state_id"], timestep=0, construction=values, | |
| code=program, value=-score, terminal_value=-score, | |
| observation=entry.get("observation", ""), phase=BOOTSTRAP_PHASE, | |
| graph_source=program, patch_history=[program], | |
| source_values=entry["source_values"], inherit_parent_values=False, | |
| ) | |
| roots.append( | |
| promote_bootstrap_winner(winner, copies=1, valid_candidates=0)[0] | |
| ) | |
| class AdmittedBaselineFrontierAC1Env(BaselineFrontierSingleAnchorPFGAC1Env): | |
| _frontier_roots = tuple(root.to_dict() for root in roots) | |
| _frontier_index = 0 | |
| _frontier_lock = Lock() | |
| def create_initial_state(cls, problem_type): | |
| if problem_type != "ac1": | |
| raise ValueError("AdmittedBaselineFrontierAC1Env only supports AC1") | |
| with cls._frontier_lock: | |
| index = cls._frontier_index | |
| cls._frontier_index += 1 | |
| if index >= len(cls._frontier_roots): | |
| raise RuntimeError("AC1 admitted frontier was initialized more than once") | |
| return SingleAnchorPFGAC1State.from_dict( | |
| cls._frontier_roots[index] | |
| ) | |
| return AdmittedBaselineFrontierAC1Env | |
| values = artifact["construction"] | |
| assert 1000 <= len(values) <= 100000 | |
| score = float(evaluate_sequence_ac1(values)) | |
| assert abs(score - artifact["raw_score"]) < 1e-12 | |
| assert hashlib.sha256(artifact["program"].encode()).hexdigest() == artifact["program_sha256"] | |
| winner = SingleAnchorPFGAC1State( | |
| id=artifact["state_id"], timestep=0, construction=values, | |
| code=artifact["program"], value=-score, terminal_value=-score, | |
| observation="", phase=BOOTSTRAP_PHASE, graph_source=artifact["program"], | |
| patch_history=[artifact["program"]], source_values=artifact["source_values"], | |
| inherit_parent_values=parent_mode, | |
| ) | |
| class AdmittedRootAC1Env(base_env): | |
| def create_initial_state(cls, problem_type): | |
| return promote_bootstrap_winner(winner, copies=1, valid_candidates=0)[0] | |
| return AdmittedRootAC1Env | |
| # 【本地工程及方法装配】build_config | |
| # 本地正式配置的最终装配点:backend=local_gpu、PUCT、8×64 候选和指定任务环境。 | |
| # 显式关闭 trace/factorized/replay/后代回传等历史方案;Qwen 与 OSS 的模型执行配置在这里分流。 | |
| def build_config(c): | |
| from ttt_discover import DiscoverConfig | |
| qwen = c["model_name"] == "Qwen/Qwen3-8B" | |
| env_type = environment_for(c) | |
| # Same tree/credit switches as the aligned local run. | |
| # 【正式方法边界】以下开关关闭历史树/信用扩展;不能因为源码里保留这些分支就把它们算入当前方法。 | |
| for name in ("backup_descendant_values", "max_policy_survivors", "descendant_credit_replay", | |
| "bellman_advantage_relabel", "dedupe_by_semantics", "dedupe_semantics_within_parent"): | |
| setattr(env_type, name, False) | |
| return DiscoverConfig( | |
| env_type=env_type, | |
| problem_type=( | |
| "26" | |
| if c["task"] == "cp26" | |
| else "32" | |
| if c["task"] == "cp32" | |
| else c["task"] if c["task"] in {"ac1", "ac2"} else "" | |
| ), | |
| num_cpus_per_task=c["num_cpus_per_task"], eval_timeout=c["eval_timeout"], | |
| experiment_name=c["experiment_name"], wandb_project=None, | |
| backend="local_gpu", model_name=c["model_name"], local_model_path=c["model_path"], | |
| model_revision=c["model_revision"], local_files_only=True, | |
| renderer_name="qwen3" if qwen else "gpt_oss_high_reasoning", lora_rank=c["lora_rank"], lora_alpha=c["lora_alpha"], | |
| local_gpu_attn_implementation=c["attention"], local_gpu_dequantize_mxfp4=not qwen, | |
| local_gpu_streaming_mxfp4=False, local_gpu_context_window=c["max_model_len"], | |
| local_gpu_train_router=False, local_gpu_parallelism="data_parallel" if qwen else "tensor_parallel", | |
| local_gpu_required_world_size=len(c["train_gpus"]), local_gpu_gradient_checkpointing=True, | |
| local_gpu_position_chunk_size=64, local_gpu_checkpoint_root=str(Path(c["run_dir"]) / "checkpoints"), | |
| local_gpu_sampling_batch_size=4, local_gpu_sampling_cache_implementation="dynamic", | |
| local_gpu_sampling_expert_backend="eager", local_gpu_sampling_fused_logprobs=False, | |
| local_gpu_sampling_lora_dtype="bfloat16" if qwen else "float32", local_gpu_sampling_backend="vllm_http", | |
| local_gpu_vllm_base_urls=tuple(c["vllm_urls"]), local_gpu_vllm_base_model_name=c["model_name"], | |
| local_gpu_vllm_adapter_name="ttt-policy", local_gpu_vllm_timeout_seconds=3600, | |
| local_gpu_vllm_max_inflight_per_server=max(64, c["max_num_seqs"]) if qwen else 64, | |
| local_gpu_vllm_sleep_during_training=c.get("vllm_sleep_during_training", True), | |
| local_gpu_optimizer_state_offload=c.get("optimizer_state_offload", True), | |
| local_gpu_training_broadcast_chunk_size=c.get("training_broadcast_chunk_size", 1), | |
| local_gpu_frozen_weight_offload=c.get("frozen_weight_offload", False), | |
| local_gpu_seed=0, learning_rate=c["learning_rate"], num_epochs=c["logged_batches"], | |
| temperature=c["temperature"], kl_penalty_coef=c["kl_penalty_coef"], | |
| phase1_max_tokens=c["phase1_max_tokens"], groups_per_batch=c["groups_per_batch"], | |
| group_size=c["group_size"], parent_selection_policy="puct", save_every=1, | |
| load_checkpoint_path=None, trace_credit_enabled=False, trace_credit_state_value=False, | |
| trace_credit_weight=0.0, factorized_action_credit=False, breakthrough_replay_enabled=False, | |
| descendant_credit_enabled=False, bellman_relabel_enabled=False, hindsight_shortcut_enabled=False, | |
| ) | |
| # 【本地工程及方法装配】main | |
| # torchrun 启动此入口;读取 resolved_config.json,进入实验目录,调用公共 discover。 | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--resolved", type=Path, required=True) | |
| args = parser.parse_args() | |
| c = json.loads(args.resolved.read_text()) | |
| os.chdir(c["run_dir"]) | |
| from ttt_discover import discover | |
| discover(build_config(c)) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 15 kB
- Xet hash:
- c31b9dbf3978dae20440ae3a958ba5082c2182de88f899c09b94755d7612232d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.