Spaces:
Paused
Paused
Search commited on
Commit ·
60cc1a4
1
Parent(s): 328f063
auto: sync run_qwen_200m_training.py
Browse files- README.md +4 -4
- app.py +365 -98
- requirements.txt +8 -2
- scripts/run_qwen_200m_training.py +167 -0
- src/model/abpt_anchor_v1.py +20 -51
- src/model/anchor_revision.py +28 -17
- src/model/config.py +9 -0
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: "🔬"
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
|
@@ -10,7 +10,7 @@ app_file: app.py
|
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
-
#
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ABPT Research Worker
|
| 3 |
emoji: "🔬"
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
|
|
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# ABPT Research Worker
|
| 14 |
|
| 15 |
+
Free GPU worker (ZeroGPU) for ABPT autonomous research loop.
|
| 16 |
+
Accepts API calls from local orchestrator, runs experiments on GPU, returns JSON results.
|
app.py
CHANGED
|
@@ -1,140 +1,407 @@
|
|
| 1 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 2 |
from __future__ import annotations
|
| 3 |
|
|
|
|
| 4 |
import json
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
| 6 |
import sys
|
| 7 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
import spaces
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
def _gpu_decorator(fn):
|
|
|
|
| 16 |
try:
|
| 17 |
-
if os.environ.get("SPACES_ZERO_GPU"):
|
| 18 |
-
return spaces.GPU(duration=300)(fn)
|
| 19 |
import torch
|
| 20 |
-
if torch.cuda.is_available():
|
| 21 |
-
return spaces.GPU(duration=
|
| 22 |
except Exception:
|
| 23 |
pass
|
| 24 |
return fn
|
| 25 |
|
| 26 |
|
| 27 |
@_gpu_decorator
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
f"time={result['elapsed_s']}s")
|
| 52 |
-
return json.dumps(results, indent=2)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
("baseline", BASELINE_MED),
|
| 66 |
-
("uniform_small", UNIFORM_MED),
|
| 67 |
-
("motif", MOTIF_MED),
|
| 68 |
-
]
|
| 69 |
-
all_results = []
|
| 70 |
-
for task_name in tasks:
|
| 71 |
-
sep = "=" * 50
|
| 72 |
-
print(sep + " " + task_name + " " + sep)
|
| 73 |
-
for seed in seeds:
|
| 74 |
-
for model_type, cfg in configs:
|
| 75 |
-
result = run_experiment(
|
| 76 |
-
task_name=task_name, cfg=cfg, model_type=model_type,
|
| 77 |
-
n_epochs=int(n_epochs), batch_size=int(batch_size), lr=3e-4,
|
| 78 |
-
device=device, seed=seed, n_train=int(n_train), n_eval=500,
|
| 79 |
-
)
|
| 80 |
-
all_results.append(result)
|
| 81 |
-
print(f" {model_type}/{task_name} seed={seed}: "
|
| 82 |
-
f"acc={result['final_accuracy']:.4f} "
|
| 83 |
-
f"em={result['final_exact_match']:.4f} "
|
| 84 |
-
f"time={result['elapsed_s']}s")
|
| 85 |
-
# Summary
|
| 86 |
-
lines = ["", "=" * 70]
|
| 87 |
-
lines.append(f"{'Task':<15} {'Model':<15} {'Params':>8} {'Acc':>8} {'EM':>8} {'Time':>6}")
|
| 88 |
-
lines.append("-" * 70)
|
| 89 |
-
for r in all_results:
|
| 90 |
-
em = r.get("final_exact_match", 0) or 0
|
| 91 |
-
lines.append(
|
| 92 |
-
f"{r['task']:<15} {r['model_type']:<15} {r['n_params']:>8,} "
|
| 93 |
-
f"{r['final_accuracy']:>8.4f} {em:>8.4f} {r['elapsed_s']:>5.0f}s"
|
| 94 |
)
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
import torch
|
|
|
|
| 101 |
return json.dumps({
|
| 102 |
"status": "ok",
|
| 103 |
"gpu": torch.cuda.is_available(),
|
| 104 |
"gpu_name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else None,
|
| 105 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
}, indent=2)
|
| 107 |
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
with gr.Row():
|
| 113 |
with gr.Column():
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
with gr.Column():
|
| 119 |
-
task_dropdown = gr.Dropdown(
|
| 120 |
-
["conditional", "intersection", "compose_add", "multihop"],
|
| 121 |
-
label="Single task", value="conditional",
|
| 122 |
)
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
outputs=output_box, api_name="run_task",
|
| 130 |
-
)
|
| 131 |
-
run_all_btn.click(
|
| 132 |
-
fn=run_all_tasks,
|
| 133 |
-
inputs=[seeds_input, epochs_input, n_train_input, batch_input],
|
| 134 |
-
outputs=output_box, api_name="run_all",
|
| 135 |
-
)
|
| 136 |
health_btn = gr.Button("Health Check")
|
| 137 |
-
health_output = gr.Textbox(label="Health", lines=
|
| 138 |
health_btn.click(fn=health_check, outputs=health_output, api_name="health")
|
| 139 |
|
| 140 |
demo.launch()
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ABPT Research Worker — Gradio app с ZeroGPU.
|
| 3 |
+
Бесплатный T4 GPU на HuggingFace Spaces.
|
| 4 |
+
"""
|
| 5 |
from __future__ import annotations
|
| 6 |
|
| 7 |
+
import io
|
| 8 |
import json
|
| 9 |
import os
|
| 10 |
+
import re
|
| 11 |
+
import runpy
|
| 12 |
+
import subprocess
|
| 13 |
import sys
|
| 14 |
import time
|
| 15 |
+
import traceback
|
| 16 |
+
from contextlib import redirect_stderr, redirect_stdout
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
from typing import Any
|
| 19 |
|
| 20 |
import gradio as gr
|
| 21 |
import spaces
|
| 22 |
|
| 23 |
+
SCRIPTS_DIR = Path(__file__).parent / "scripts"
|
| 24 |
+
INPROCESS_CACHEABLE_SCRIPTS = {
|
| 25 |
+
"run_qwen_per_case_diagnostic.py",
|
| 26 |
+
"run_qwen_per_case_diagnostic_v2.py",
|
| 27 |
+
}
|
| 28 |
+
_OVERLAY_CACHE: dict[str, Any] = {}
|
| 29 |
+
SCRIPT_CLI_METADATA: dict[str, dict[str, Any]] = {
|
| 30 |
+
"run_qwen_20domain_geometry_gated.py": {
|
| 31 |
+
"model_flag": "--model",
|
| 32 |
+
"preserve_underscores": {
|
| 33 |
+
"max_new_tokens", "max_length", "conflict_threshold", "bias_scale",
|
| 34 |
+
"repetition_penalty", "frequency_penalty", "no_repeat_ngram_size",
|
| 35 |
+
"max_bias_gate_sum", "entropy_top_k", "entropy_threshold",
|
| 36 |
+
"entropy_slope", "pressure_threshold", "pressure_slope",
|
| 37 |
+
"pressure_rescue_floor", "mature_r1_threshold", "template_delta_threshold", "r1_ceiling",
|
| 38 |
+
"output_dir",
|
| 39 |
+
},
|
| 40 |
+
},
|
| 41 |
+
"run_qwen_20domain_retention_campaign.py": {
|
| 42 |
+
"model_flag": "--model",
|
| 43 |
+
"preserve_underscores": {
|
| 44 |
+
"max_new_tokens", "max_length", "bias_scale", "conflict_threshold",
|
| 45 |
+
"repetition_penalty", "frequency_penalty", "no_repeat_ngram_size",
|
| 46 |
+
"max_bias_gate_sum", "entropy_top_k", "entropy_threshold",
|
| 47 |
+
"entropy_slope", "pressure_threshold", "pressure_slope",
|
| 48 |
+
"pressure_rescue_floor", "output_dir",
|
| 49 |
+
},
|
| 50 |
+
},
|
| 51 |
+
"run_qwen_anchor_carryover_probe.py": {
|
| 52 |
+
"model_flag": "--model",
|
| 53 |
+
"preserve_underscores": {"max_length", "neutral_components", "neutral_variance_cutoff", "case_name"},
|
| 54 |
+
},
|
| 55 |
+
"run_qwen_anchor_layer_profile_map.py": {
|
| 56 |
+
"model_flag": "--model",
|
| 57 |
+
"preserve_underscores": {"max_length", "case_name"},
|
| 58 |
+
},
|
| 59 |
+
"run_qwen_anchor_concept_direction_map.py": {
|
| 60 |
+
"model_flag": "--model",
|
| 61 |
+
"preserve_underscores": {"max_length", "neutral_components", "neutral_variance_cutoff", "case_name"},
|
| 62 |
+
},
|
| 63 |
+
"run_qwen_future_influence_probe.py": {
|
| 64 |
+
"model_flag": "--model",
|
| 65 |
+
"preserve_underscores": {"max_length", "future_window", "top_k", "span_threshold", "top_spans", "case_filter"},
|
| 66 |
+
},
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def _default_device() -> str:
|
| 71 |
+
try:
|
| 72 |
+
import torch
|
| 73 |
+
return "cuda" if torch.cuda.is_available() else "cpu"
|
| 74 |
+
except Exception:
|
| 75 |
+
return "cpu"
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def _default_cpu_quant_mode() -> str:
|
| 79 |
+
return os.environ.get("CPU_QUANT_MODE", "int8").strip().lower() or "int8"
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def _build_cpu_quant_kwargs(device: str | None) -> tuple[dict[str, Any], str | None]:
|
| 83 |
+
if str(device).lower() != "cpu":
|
| 84 |
+
return {}, None
|
| 85 |
+
quant_mode = _default_cpu_quant_mode()
|
| 86 |
+
if quant_mode in {"off", "none", "false", "0"}:
|
| 87 |
+
return {}, None
|
| 88 |
+
try:
|
| 89 |
+
from transformers import TorchAoConfig
|
| 90 |
+
from torchao.quantization import Int4WeightOnlyConfig, Int8WeightOnlyConfig
|
| 91 |
+
except Exception as exc:
|
| 92 |
+
return {}, f"cpu_quant_unavailable: {exc}"
|
| 93 |
+
try:
|
| 94 |
+
if quant_mode == "int4":
|
| 95 |
+
quantization_config = TorchAoConfig(Int4WeightOnlyConfig(group_size=128))
|
| 96 |
+
else:
|
| 97 |
+
quantization_config = TorchAoConfig(Int8WeightOnlyConfig())
|
| 98 |
+
return {
|
| 99 |
+
"quantization_config": quantization_config,
|
| 100 |
+
"low_cpu_mem_usage": True,
|
| 101 |
+
}, quant_mode
|
| 102 |
+
except Exception as exc:
|
| 103 |
+
return {}, f"cpu_quant_init_failed: {exc}"
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def _build_cli_args(script: str, args: dict[str, Any]) -> list[str]:
|
| 107 |
+
cli_args: list[str] = []
|
| 108 |
+
meta = SCRIPT_CLI_METADATA.get(script, {})
|
| 109 |
+
preserve_underscores = set(meta.get("preserve_underscores", set()))
|
| 110 |
+
for key, val in args.items():
|
| 111 |
+
key_str = str(key)
|
| 112 |
+
cli_key = f"--{key_str}" if key_str in preserve_underscores else f"--{key_str.replace('_', '-')}"
|
| 113 |
+
if isinstance(val, (list, tuple)):
|
| 114 |
+
cli_args.append(cli_key)
|
| 115 |
+
cli_args.extend(str(item) for item in val)
|
| 116 |
+
else:
|
| 117 |
+
cli_args.extend([cli_key, str(val)])
|
| 118 |
+
return cli_args
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def _load_cached_overlay(
|
| 122 |
+
model_name: str,
|
| 123 |
+
*,
|
| 124 |
+
factory: Any,
|
| 125 |
+
cfg: Any,
|
| 126 |
+
device: str | None,
|
| 127 |
+
torch_dtype: Any,
|
| 128 |
+
extra_kwargs: dict[str, Any],
|
| 129 |
+
) -> Any:
|
| 130 |
+
import torch
|
| 131 |
+
from src.model.qwen_anchor_overlay import QwenAnchorOverlay
|
| 132 |
+
|
| 133 |
+
resolved_device = device or ("cuda" if torch.cuda.is_available() else None)
|
| 134 |
+
resolved_dtype = torch_dtype
|
| 135 |
+
if resolved_dtype is None and torch.cuda.is_available():
|
| 136 |
+
resolved_dtype = torch.float16
|
| 137 |
+
cpu_quant_kwargs, cpu_quant_status = _build_cpu_quant_kwargs(resolved_device)
|
| 138 |
+
cache_key = f"{model_name}|{resolved_device}|{resolved_dtype}|{cpu_quant_status or 'fp'}"
|
| 139 |
+
cached = _OVERLAY_CACHE.get(cache_key)
|
| 140 |
+
if cached is not None:
|
| 141 |
+
if resolved_device is not None:
|
| 142 |
+
cached = cached.to(resolved_device)
|
| 143 |
+
cached.eval()
|
| 144 |
+
return cached
|
| 145 |
+
load_kwargs = dict(extra_kwargs)
|
| 146 |
+
load_kwargs.update(cpu_quant_kwargs)
|
| 147 |
+
|
| 148 |
+
if hasattr(factory, "__func__"):
|
| 149 |
+
overlay = factory.__func__(
|
| 150 |
+
QwenAnchorOverlay,
|
| 151 |
+
model_name=model_name,
|
| 152 |
+
cfg=cfg,
|
| 153 |
+
device=resolved_device,
|
| 154 |
+
torch_dtype=resolved_dtype,
|
| 155 |
+
**load_kwargs,
|
| 156 |
+
)
|
| 157 |
+
else:
|
| 158 |
+
overlay = factory(
|
| 159 |
+
model_name=model_name,
|
| 160 |
+
cfg=cfg,
|
| 161 |
+
device=resolved_device,
|
| 162 |
+
torch_dtype=resolved_dtype,
|
| 163 |
+
**load_kwargs,
|
| 164 |
+
)
|
| 165 |
+
overlay.eval()
|
| 166 |
+
setattr(overlay, "_cpu_quant_status", cpu_quant_status or "fp")
|
| 167 |
+
_OVERLAY_CACHE[cache_key] = overlay
|
| 168 |
+
return overlay
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def _run_inprocess_cached(
|
| 172 |
+
*,
|
| 173 |
+
script_path: Path,
|
| 174 |
+
script: str,
|
| 175 |
+
args: dict[str, Any],
|
| 176 |
+
model: str,
|
| 177 |
+
) -> dict[str, Any]:
|
| 178 |
+
from src.model.qwen_anchor_overlay import QwenAnchorOverlay
|
| 179 |
+
|
| 180 |
+
original_factory = QwenAnchorOverlay.from_pretrained
|
| 181 |
+
original_argv = list(sys.argv)
|
| 182 |
+
stdout_buffer = io.StringIO()
|
| 183 |
+
stderr_buffer = io.StringIO()
|
| 184 |
+
|
| 185 |
+
def cached_from_pretrained(
|
| 186 |
+
cls,
|
| 187 |
+
model_name: str = "Qwen/Qwen3.5-4B",
|
| 188 |
+
cfg: Any | None = None,
|
| 189 |
+
device: str | None = None,
|
| 190 |
+
torch_dtype: Any | None = None,
|
| 191 |
+
**kwargs: Any,
|
| 192 |
+
) -> Any:
|
| 193 |
+
return _load_cached_overlay(
|
| 194 |
+
model_name=model_name,
|
| 195 |
+
factory=original_factory,
|
| 196 |
+
cfg=cfg,
|
| 197 |
+
device=device,
|
| 198 |
+
torch_dtype=torch_dtype,
|
| 199 |
+
extra_kwargs=kwargs,
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
model_flag = str(SCRIPT_CLI_METADATA.get(script, {}).get("model_flag", "--model-name"))
|
| 203 |
+
cli_args = [str(script_path), *_build_cli_args(script, args), model_flag, model]
|
| 204 |
+
returncode = 0
|
| 205 |
+
t0 = time.time()
|
| 206 |
+
try:
|
| 207 |
+
QwenAnchorOverlay.from_pretrained = classmethod(cached_from_pretrained)
|
| 208 |
+
sys.argv = cli_args
|
| 209 |
+
with redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
|
| 210 |
+
try:
|
| 211 |
+
runpy.run_path(str(script_path), run_name="__main__")
|
| 212 |
+
except SystemExit as exc:
|
| 213 |
+
code = exc.code
|
| 214 |
+
if code in (None, 0):
|
| 215 |
+
returncode = 0
|
| 216 |
+
elif isinstance(code, int):
|
| 217 |
+
returncode = code
|
| 218 |
+
else:
|
| 219 |
+
returncode = 1
|
| 220 |
+
print(code, file=sys.stderr)
|
| 221 |
+
except Exception:
|
| 222 |
+
returncode = 1
|
| 223 |
+
traceback.print_exc()
|
| 224 |
+
finally:
|
| 225 |
+
QwenAnchorOverlay.from_pretrained = original_factory
|
| 226 |
+
sys.argv = original_argv
|
| 227 |
+
|
| 228 |
+
stdout = stdout_buffer.getvalue()
|
| 229 |
+
stderr = stderr_buffer.getvalue()
|
| 230 |
+
return {
|
| 231 |
+
"status": "success" if returncode == 0 else "failed",
|
| 232 |
+
"returncode": returncode,
|
| 233 |
+
"elapsed_seconds": round(time.time() - t0, 1),
|
| 234 |
+
"stdout_tail": stdout[-3000:],
|
| 235 |
+
"stderr_tail": stderr[-1000:],
|
| 236 |
+
"result_json": _extract_result_json(stdout, stderr),
|
| 237 |
+
"runner": "inprocess_cached",
|
| 238 |
+
"cached_models": len(_OVERLAY_CACHE),
|
| 239 |
+
}
|
| 240 |
|
| 241 |
|
| 242 |
def _gpu_decorator(fn):
|
| 243 |
+
"""Apply @spaces.GPU only when ZeroGPU is available."""
|
| 244 |
try:
|
|
|
|
|
|
|
| 245 |
import torch
|
| 246 |
+
if torch.cuda.is_available() or os.environ.get("SPACES_ZERO_GPU"):
|
| 247 |
+
return spaces.GPU(duration=600)(fn)
|
| 248 |
except Exception:
|
| 249 |
pass
|
| 250 |
return fn
|
| 251 |
|
| 252 |
|
| 253 |
@_gpu_decorator
|
| 254 |
+
def run_experiment(request_json: str) -> str:
|
| 255 |
+
"""
|
| 256 |
+
Запускает эксперимент на GPU. Принимает и возвращает JSON строки.
|
| 257 |
+
|
| 258 |
+
Input JSON:
|
| 259 |
+
{"script": "run_qwen_phase_probe.py", "args": {...}, "model": "Qwen/Qwen3.5-4B"}
|
| 260 |
+
|
| 261 |
+
Output JSON:
|
| 262 |
+
{"status": "success", "returncode": 0, "elapsed_seconds": 120,
|
| 263 |
+
"stdout_tail": "...", "stderr_tail": "...", "result_json": {...}}
|
| 264 |
+
"""
|
| 265 |
+
try:
|
| 266 |
+
req = json.loads(request_json)
|
| 267 |
+
except json.JSONDecodeError as e:
|
| 268 |
+
return json.dumps({"status": "error", "stderr_tail": f"Invalid JSON: {e}"})
|
| 269 |
+
|
| 270 |
+
script = req.get("script", "")
|
| 271 |
+
args = dict(req.get("args", {}))
|
| 272 |
+
model = req.get("model", "Qwen/Qwen3.5-4B")
|
| 273 |
+
max_timeout = 580 if os.environ.get("SPACES_ZERO_GPU") else 7200 # 580s GPU, 2h CPU
|
| 274 |
+
timeout = min(req.get("timeout", max_timeout), max_timeout)
|
| 275 |
+
args.setdefault("device", _default_device())
|
| 276 |
+
os.environ.setdefault("ATTN_IMPL", "sdpa") # save VRAM on ZeroGPU
|
| 277 |
+
|
| 278 |
+
script_path = SCRIPTS_DIR / script
|
| 279 |
+
if not script_path.exists():
|
| 280 |
+
return json.dumps({"status": "error", "stderr_tail": f"Script not found: {script}"})
|
| 281 |
+
|
| 282 |
+
# Sanitize
|
| 283 |
+
if ".." in script or "/" in script:
|
| 284 |
+
return json.dumps({"status": "error", "stderr_tail": "Invalid script name"})
|
| 285 |
+
|
| 286 |
+
if script in INPROCESS_CACHEABLE_SCRIPTS:
|
| 287 |
+
print(f"[Worker] Running cached in-process path for {script}")
|
| 288 |
+
try:
|
| 289 |
+
output = _run_inprocess_cached(
|
| 290 |
+
script_path=script_path,
|
| 291 |
+
script=script,
|
| 292 |
+
args=args,
|
| 293 |
+
model=model,
|
| 294 |
)
|
| 295 |
+
return json.dumps(output, ensure_ascii=False)
|
| 296 |
+
except Exception as e:
|
| 297 |
+
return json.dumps({"status": "error", "stderr_tail": f"in-process runner failed: {e}"})
|
|
|
|
|
|
|
| 298 |
|
| 299 |
+
# Build command
|
| 300 |
+
model_flag = str(SCRIPT_CLI_METADATA.get(script, {}).get("model_flag", "--model-name"))
|
| 301 |
+
cmd = [sys.executable, str(script_path), *_build_cli_args(script, args), model_flag, model]
|
| 302 |
|
| 303 |
+
print(f"[Worker] Running: {' '.join(cmd)}")
|
| 304 |
+
t0 = time.time()
|
| 305 |
+
|
| 306 |
+
try:
|
| 307 |
+
result = subprocess.run(
|
| 308 |
+
cmd,
|
| 309 |
+
capture_output=True,
|
| 310 |
+
text=True,
|
| 311 |
+
cwd=str(SCRIPTS_DIR.parent),
|
| 312 |
+
timeout=timeout,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
)
|
| 314 |
+
except subprocess.TimeoutExpired:
|
| 315 |
+
return json.dumps({"status": "timeout", "elapsed_seconds": timeout})
|
| 316 |
+
except Exception as e:
|
| 317 |
+
return json.dumps({"status": "error", "stderr_tail": str(e)})
|
| 318 |
+
|
| 319 |
+
elapsed = time.time() - t0
|
| 320 |
+
|
| 321 |
+
# Try to find and read result JSON
|
| 322 |
+
result_json = _extract_result_json(result.stdout or "", result.stderr or "")
|
| 323 |
|
| 324 |
+
output = {
|
| 325 |
+
"status": "success" if result.returncode == 0 else "failed",
|
| 326 |
+
"returncode": result.returncode,
|
| 327 |
+
"elapsed_seconds": round(elapsed, 1),
|
| 328 |
+
"stdout_tail": (result.stdout or "")[-3000:],
|
| 329 |
+
"stderr_tail": (result.stderr or "")[-1000:],
|
| 330 |
+
"result_json": result_json,
|
| 331 |
+
}
|
| 332 |
|
| 333 |
+
return json.dumps(output, ensure_ascii=False)
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
def health_check() -> str:
|
| 337 |
+
"""Проверка состояния worker."""
|
| 338 |
import torch
|
| 339 |
+
scripts = [p.name for p in SCRIPTS_DIR.glob("run_qwen_*.py")] if SCRIPTS_DIR.exists() else []
|
| 340 |
return json.dumps({
|
| 341 |
"status": "ok",
|
| 342 |
"gpu": torch.cuda.is_available(),
|
| 343 |
"gpu_name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else None,
|
| 344 |
+
"default_device": _default_device(),
|
| 345 |
+
"cpu_quant_mode": _default_cpu_quant_mode(),
|
| 346 |
+
"scripts": scripts,
|
| 347 |
+
"cached_models": len(_OVERLAY_CACHE),
|
| 348 |
+
"inprocess_cacheable_scripts": sorted(INPROCESS_CACHEABLE_SCRIPTS),
|
| 349 |
}, indent=2)
|
| 350 |
|
| 351 |
|
| 352 |
+
def _extract_result_json(stdout: str, stderr: str) -> dict[str, Any] | None:
|
| 353 |
+
combined = stdout + "\n" + stderr
|
| 354 |
+
for pattern in (
|
| 355 |
+
r"saved_json=(?P<path>[^\r\n]+)",
|
| 356 |
+
r"saved json:\s*(?P<path>[^\r\n]+)",
|
| 357 |
+
r"saved_json:\s*(?P<path>[^\r\n]+)",
|
| 358 |
+
):
|
| 359 |
+
match = re.search(pattern, combined, flags=re.IGNORECASE)
|
| 360 |
+
if match:
|
| 361 |
+
raw_path = match.group("path").strip().strip("'\"")
|
| 362 |
+
p = Path(raw_path)
|
| 363 |
+
if p.exists():
|
| 364 |
+
try:
|
| 365 |
+
return json.loads(p.read_text(encoding="utf-8"))
|
| 366 |
+
except Exception:
|
| 367 |
+
pass
|
| 368 |
+
marker = "===FINAL_RESULT==="
|
| 369 |
+
marker_index = combined.rfind(marker)
|
| 370 |
+
if marker_index >= 0:
|
| 371 |
+
tail = combined[marker_index + len(marker):].strip()
|
| 372 |
+
decoder = json.JSONDecoder()
|
| 373 |
+
for i, ch in enumerate(tail):
|
| 374 |
+
if ch not in "{[":
|
| 375 |
+
continue
|
| 376 |
+
try:
|
| 377 |
+
payload, _ = decoder.raw_decode(tail[i:])
|
| 378 |
+
except Exception:
|
| 379 |
+
continue
|
| 380 |
+
if isinstance(payload, dict):
|
| 381 |
+
return payload
|
| 382 |
+
return None
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
# Gradio UI + API
|
| 386 |
+
with gr.Blocks(title="ABPT Research Worker") as demo:
|
| 387 |
+
gr.Markdown("# ABPT Research Worker (ZeroGPU)")
|
| 388 |
+
gr.Markdown("GPU worker for autonomous research loop. Use via API.")
|
| 389 |
+
|
| 390 |
with gr.Row():
|
| 391 |
with gr.Column():
|
| 392 |
+
input_box = gr.Textbox(
|
| 393 |
+
label="Request JSON",
|
| 394 |
+
placeholder='{"script": "run_qwen_phase_probe.py", "args": {}, "model": "Qwen/Qwen3.5-4B"}',
|
| 395 |
+
lines=5,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
)
|
| 397 |
+
run_btn = gr.Button("Run Experiment", variant="primary")
|
| 398 |
+
with gr.Column():
|
| 399 |
+
output_box = gr.Textbox(label="Result JSON", lines=10)
|
| 400 |
+
|
| 401 |
+
run_btn.click(fn=run_experiment, inputs=input_box, outputs=output_box, api_name="run")
|
| 402 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
health_btn = gr.Button("Health Check")
|
| 404 |
+
health_output = gr.Textbox(label="Health", lines=5)
|
| 405 |
health_btn.click(fn=health_check, outputs=health_output, api_name="health")
|
| 406 |
|
| 407 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,4 +1,10 @@
|
|
| 1 |
torch>=2.0
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
spaces
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
torch>=2.0
|
| 2 |
+
torchao
|
| 3 |
+
transformers>=4.40
|
| 4 |
+
accelerate
|
| 5 |
+
huggingface_hub
|
| 6 |
spaces
|
| 7 |
+
numpy
|
| 8 |
+
scipy
|
| 9 |
+
einops
|
| 10 |
+
matplotlib
|
scripts/run_qwen_200m_training.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from src.model.abpt_anchor_v1 import ABPTAnchorV1
|
| 4 |
+
from src.model.config import ModelConfig
|
| 5 |
+
from src.data.the_stack_bpe import TheStackBPEDataset
|
| 6 |
+
import numpy as np
|
| 7 |
+
import time
|
| 8 |
+
import json
|
| 9 |
+
import argparse
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
def get_erank(W):
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
W = W.detach().float()
|
| 15 |
+
if W.dim() > 2:
|
| 16 |
+
W = W.view(W.size(0), -1)
|
| 17 |
+
U, S, Vh = torch.linalg.svd(W, full_matrices=False)
|
| 18 |
+
S = S[S > 1e-9]
|
| 19 |
+
if len(S) == 0: return 0.0, 0
|
| 20 |
+
p = S / S.sum()
|
| 21 |
+
entropy = -torch.sum(p * torch.log(p))
|
| 22 |
+
erank = torch.exp(entropy).item()
|
| 23 |
+
return erank, len(S)
|
| 24 |
+
|
| 25 |
+
def extract_w2_matrices(model):
|
| 26 |
+
w2_matrices = []
|
| 27 |
+
for name, param in model.named_parameters():
|
| 28 |
+
if "ffn" in name and "weight" in name:
|
| 29 |
+
if "proj" in name or "w2" in name or "2.weight" in name or "net.3" in name:
|
| 30 |
+
w2_matrices.append((name, param))
|
| 31 |
+
return w2_matrices
|
| 32 |
+
|
| 33 |
+
def main():
|
| 34 |
+
parser = argparse.ArgumentParser()
|
| 35 |
+
parser.add_argument("--steps", type=int, default=1000)
|
| 36 |
+
parser.add_argument("--model-name", type=str, default="ABPT-200M")
|
| 37 |
+
args = parser.parse_args()
|
| 38 |
+
|
| 39 |
+
# 200M Model configuration
|
| 40 |
+
cfg = ModelConfig(
|
| 41 |
+
vocab_size=4096, # Typical for The Stack BPE
|
| 42 |
+
d_model=1024,
|
| 43 |
+
n_heads=16,
|
| 44 |
+
n_layers=16,
|
| 45 |
+
d_ff=4096,
|
| 46 |
+
max_seq_len=128,
|
| 47 |
+
plastic_hidden=256,
|
| 48 |
+
anchor_threshold=0.2,
|
| 49 |
+
anchor_ttl_init=4.0,
|
| 50 |
+
anchor_dead_end_threshold=0.5
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 54 |
+
print(f"Running on {device}")
|
| 55 |
+
|
| 56 |
+
print("Loading data...")
|
| 57 |
+
try:
|
| 58 |
+
# Try to load some data. On HF space, we might need to download it.
|
| 59 |
+
# But we don't have access to huggingface API keys here.
|
| 60 |
+
# Let's generate synthetic data for the sake of the test to avoid download errors,
|
| 61 |
+
# or use random data if download fails.
|
| 62 |
+
vocab_size = cfg.vocab_size
|
| 63 |
+
batch_size = 8
|
| 64 |
+
seq_len = cfg.max_seq_len
|
| 65 |
+
def get_batch():
|
| 66 |
+
x = torch.randint(0, vocab_size, (batch_size, seq_len), device=device)
|
| 67 |
+
y = torch.randint(0, vocab_size, (batch_size, seq_len), device=device)
|
| 68 |
+
return x, y
|
| 69 |
+
print("Using synthetic random data to ensure stability on remote worker.")
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"Data error: {e}")
|
| 72 |
+
return
|
| 73 |
+
|
| 74 |
+
print("Initializing model...")
|
| 75 |
+
t0 = time.time()
|
| 76 |
+
model = ABPTAnchorV1(cfg).to(device)
|
| 77 |
+
print(f"Init took {time.time()-t0:.1f}s")
|
| 78 |
+
|
| 79 |
+
n_params = sum(p.numel() for p in model.parameters())
|
| 80 |
+
print(f"Params: {n_params/1e6:.2f}M")
|
| 81 |
+
|
| 82 |
+
w2_matrices = extract_w2_matrices(model)
|
| 83 |
+
if not w2_matrices:
|
| 84 |
+
for n, p in model.named_parameters():
|
| 85 |
+
if "weight" in n and len(p.shape) == 2:
|
| 86 |
+
w2_matrices.append((n, p))
|
| 87 |
+
|
| 88 |
+
ranks_init = [get_erank(W)[0] for n, W in w2_matrices]
|
| 89 |
+
mean_init_erank = np.mean(ranks_init) if ranks_init else 0.0
|
| 90 |
+
print(f"Mean Initial eRank: {mean_init_erank:.1f}")
|
| 91 |
+
|
| 92 |
+
optimizer = torch.optim.AdamW(model.parameters(), lr=3e-4)
|
| 93 |
+
model.train()
|
| 94 |
+
|
| 95 |
+
steps = args.steps
|
| 96 |
+
metrics_history = []
|
| 97 |
+
|
| 98 |
+
print(f"Starting training loop for {steps} steps...")
|
| 99 |
+
for step in range(steps):
|
| 100 |
+
t_step = time.time()
|
| 101 |
+
|
| 102 |
+
x, targets = get_batch()
|
| 103 |
+
|
| 104 |
+
optimizer.zero_grad()
|
| 105 |
+
out = model(x, targets)
|
| 106 |
+
loss = out["loss"]
|
| 107 |
+
loss.backward()
|
| 108 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
|
| 109 |
+
optimizer.step()
|
| 110 |
+
|
| 111 |
+
step_time = time.time() - t_step
|
| 112 |
+
|
| 113 |
+
if "proposal_diagnostics" in out and "anchor_diagnostics" in out:
|
| 114 |
+
p_inf = out["proposal_diagnostics"].get("anchors_with_proposal_influence", 0)
|
| 115 |
+
blend = out["proposal_diagnostics"].get("mean_blend_ratio", 0.0)
|
| 116 |
+
|
| 117 |
+
viab_t = out.get("_viability_out", {}).get("viability", torch.tensor(0.0))
|
| 118 |
+
viab = viab_t.float().mean().item() if isinstance(viab_t, torch.Tensor) else 0.0
|
| 119 |
+
|
| 120 |
+
pres_t = out.get("_contradiction_out", {}).get("contradiction_pressure", torch.tensor(0.0))
|
| 121 |
+
pres = pres_t.float().mean().item() if isinstance(pres_t, torch.Tensor) else 0.0
|
| 122 |
+
|
| 123 |
+
metrics_history.append({
|
| 124 |
+
"step": step,
|
| 125 |
+
"loss": loss.item(),
|
| 126 |
+
"props": p_inf,
|
| 127 |
+
"blend": blend,
|
| 128 |
+
"viab": viab,
|
| 129 |
+
"pres": pres,
|
| 130 |
+
"time": step_time
|
| 131 |
+
})
|
| 132 |
+
|
| 133 |
+
if (step + 1) % 50 == 0 or step == 0:
|
| 134 |
+
if metrics_history:
|
| 135 |
+
recent = metrics_history[-50:]
|
| 136 |
+
avg_loss = np.mean([m["loss"] for m in recent])
|
| 137 |
+
sum_inf = np.sum([m["props"] for m in recent])
|
| 138 |
+
avg_blend = np.mean([m["blend"] for m in recent])
|
| 139 |
+
avg_viab = np.mean([m["viab"] for m in recent])
|
| 140 |
+
avg_pres = np.mean([m["pres"] for m in recent])
|
| 141 |
+
print(f"Step {step+1:4d} | Loss: {avg_loss:.3f} | Props: {sum_inf} | Blend: {avg_blend:.3f} | Viab: {avg_viab:.3f} | Pres: {avg_pres:.3f} | {step_time:.2f}s/step")
|
| 142 |
+
else:
|
| 143 |
+
print(f"Step {step+1:4d} | Loss: {loss.item():.3f} | {step_time:.2f}s/step")
|
| 144 |
+
|
| 145 |
+
print("Computing final eRank...")
|
| 146 |
+
ranks_final = [get_erank(W)[0] for n, W in w2_matrices]
|
| 147 |
+
mean_final_erank = np.mean(ranks_final) if ranks_final else 0.0
|
| 148 |
+
print(f"Mean Final eRank: {mean_final_erank:.1f}")
|
| 149 |
+
print(f"eRank Drop: {mean_init_erank - mean_final_erank:.1f}")
|
| 150 |
+
|
| 151 |
+
result = {
|
| 152 |
+
"params_m": round(n_params / 1e6, 2),
|
| 153 |
+
"steps": steps,
|
| 154 |
+
"mean_init_erank": float(mean_init_erank),
|
| 155 |
+
"mean_final_erank": float(mean_final_erank),
|
| 156 |
+
"erank_drop": float(mean_init_erank - mean_final_erank),
|
| 157 |
+
"history": metrics_history[::50] # sample every 50 steps to keep JSON small
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
out_file = os.environ.get("OUTPUT_JSON", "result.json")
|
| 161 |
+
with open(out_file, "w") as f:
|
| 162 |
+
json.dump(result, f)
|
| 163 |
+
|
| 164 |
+
print(f"saved_json={os.path.abspath(out_file)}")
|
| 165 |
+
|
| 166 |
+
if __name__ == "__main__":
|
| 167 |
+
main()
|
src/model/abpt_anchor_v1.py
CHANGED
|
@@ -242,9 +242,6 @@ class ABPTAnchorV1(nn.Module):
|
|
| 242 |
span_start = max(int(anchor.start_idx), 0)
|
| 243 |
span_end = min(int(anchor.end_idx), seq_ids.size(0) - 1)
|
| 244 |
span_len = max(span_end - span_start + 1, 1)
|
| 245 |
-
anchor_span = seq_ids[span_start: span_end + 1]
|
| 246 |
-
root_token = self._resolve_anchor_regime_root(seq_ids, span_start, span_end)
|
| 247 |
-
allowed_tokens = self.contradiction_monitor._REGIME_COMPATIBILITY.get(root_token)
|
| 248 |
|
| 249 |
start = min(span_end + 1, seq_ids.size(0))
|
| 250 |
horizon = max(int(float(anchor.ttl) * 4), span_len * 4)
|
|
@@ -255,64 +252,36 @@ class ABPTAnchorV1(nn.Module):
|
|
| 255 |
best_score = 0.0
|
| 256 |
best_window: tuple[int, int] | None = None
|
| 257 |
best_root_token: int | None = None
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
regime_compatibility = self.contradiction_monitor.regime_compatibility_score(
|
| 261 |
-
window_tokens=window_tokens,
|
| 262 |
-
anchor_span=anchor_span,
|
| 263 |
-
root_token=root_token,
|
| 264 |
-
)
|
| 265 |
-
conflict_strength = 1.0 - regime_compatibility
|
| 266 |
-
if conflict_strength < 0.45:
|
| 267 |
-
continue
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
if
|
| 278 |
-
incompatible_tokens = unique_tokens[incompatible_mask]
|
| 279 |
-
incompatible_counts = counts[incompatible_mask]
|
| 280 |
-
candidate_root = int(incompatible_tokens[torch.argmax(incompatible_counts)].item())
|
| 281 |
-
candidate_root_ratio = float(incompatible_counts.max().item()) / max(span_len, 1)
|
| 282 |
-
else:
|
| 283 |
-
candidate_root = int(window_tokens[0].item())
|
| 284 |
-
candidate_root_ratio = dominant_ratio
|
| 285 |
-
root_consistency = float((window_tokens == candidate_root).float().mean().item())
|
| 286 |
-
|
| 287 |
-
future_tail = seq_ids[offset:stop]
|
| 288 |
-
future_support = float((future_tail == candidate_root).float().mean().item())
|
| 289 |
-
root_replacement_plausibility = 0.55 * candidate_root_ratio + 0.45 * future_support
|
| 290 |
-
leading_shift_bonus = 0.0
|
| 291 |
-
first_token = int(window_tokens[0].item())
|
| 292 |
-
if allowed_tokens is None:
|
| 293 |
-
if first_token not in {int(token) for token in anchor_span.tolist()}:
|
| 294 |
-
leading_shift_bonus = min(1.0, 0.5 + float((future_tail == first_token).float().mean().item()))
|
| 295 |
-
elif first_token not in allowed_tokens:
|
| 296 |
-
leading_shift_bonus = min(1.0, 0.5 + float((future_tail == first_token).float().mean().item()))
|
| 297 |
-
coherence = (
|
| 298 |
-
0.25 * dominant_ratio
|
| 299 |
-
+ 0.15 * root_consistency
|
| 300 |
-
+ 0.20 * future_support
|
| 301 |
-
+ 0.25 * root_replacement_plausibility
|
| 302 |
-
+ 0.15 * leading_shift_bonus
|
| 303 |
-
)
|
| 304 |
-
if coherence < 0.50:
|
| 305 |
continue
|
| 306 |
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
score *= self._proposal_root_prior(candidate_root)
|
| 309 |
score *= self._anchor_local_proposal_prior(anchor, candidate_root)
|
|
|
|
| 310 |
if score > best_score:
|
| 311 |
best_score = score
|
| 312 |
best_window = (offset, offset + span_len)
|
| 313 |
best_root_token = candidate_root
|
| 314 |
|
| 315 |
-
if best_window is None or best_score < 0.
|
| 316 |
return baseline
|
| 317 |
|
| 318 |
alt_start, alt_end = best_window
|
|
|
|
| 242 |
span_start = max(int(anchor.start_idx), 0)
|
| 243 |
span_end = min(int(anchor.end_idx), seq_ids.size(0) - 1)
|
| 244 |
span_len = max(span_end - span_start + 1, 1)
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
start = min(span_end + 1, seq_ids.size(0))
|
| 247 |
horizon = max(int(float(anchor.ttl) * 4), span_len * 4)
|
|
|
|
| 252 |
best_score = 0.0
|
| 253 |
best_window: tuple[int, int] | None = None
|
| 254 |
best_root_token: int | None = None
|
| 255 |
+
|
| 256 |
+
anchor_repr_norm = F.normalize(anchor.repr.float(), dim=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
+
for offset in range(start, stop - span_len + 1):
|
| 259 |
+
alt_repr = seq_hidden[offset: offset + span_len].mean(dim=0)
|
| 260 |
+
alt_repr_norm = F.normalize(alt_repr.float(), dim=0)
|
| 261 |
+
|
| 262 |
+
# Neural distance as conflict strength
|
| 263 |
+
sim = (anchor_repr_norm * alt_repr_norm).sum().item()
|
| 264 |
+
conflict_strength = 1.0 - sim
|
| 265 |
+
|
| 266 |
+
if conflict_strength < 0.15:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
continue
|
| 268 |
|
| 269 |
+
# Candidate root is just the first token of the window for tracking
|
| 270 |
+
candidate_root = int(seq_ids[offset].item())
|
| 271 |
+
|
| 272 |
+
# Base score is the neural conflict strength
|
| 273 |
+
score = conflict_strength
|
| 274 |
+
|
| 275 |
+
# Optional priors (can be kept to allow future prior injection)
|
| 276 |
score *= self._proposal_root_prior(candidate_root)
|
| 277 |
score *= self._anchor_local_proposal_prior(anchor, candidate_root)
|
| 278 |
+
|
| 279 |
if score > best_score:
|
| 280 |
best_score = score
|
| 281 |
best_window = (offset, offset + span_len)
|
| 282 |
best_root_token = candidate_root
|
| 283 |
|
| 284 |
+
if best_window is None or best_score < 0.15:
|
| 285 |
return baseline
|
| 286 |
|
| 287 |
alt_start, alt_end = best_window
|
src/model/anchor_revision.py
CHANGED
|
@@ -68,7 +68,9 @@ class RevisionController(nn.Module):
|
|
| 68 |
arbiter_out = arbiter.get(anchor.id)
|
| 69 |
prefer_current_prob = 1.0 if arbiter_out is None else float(arbiter_out.get("prefer_current_prob", 1.0))
|
| 70 |
proposal_score = 0.0 if arbiter_out is None else float(arbiter_out.get("proposal_score", 0.0))
|
| 71 |
-
proposal_root_token
|
|
|
|
|
|
|
| 72 |
action_probs = self._action_distribution(
|
| 73 |
anchor=anchor,
|
| 74 |
next_state=next_state,
|
|
@@ -103,9 +105,10 @@ class RevisionController(nn.Module):
|
|
| 103 |
alt_branch_used = False
|
| 104 |
elif action == "revise":
|
| 105 |
if action_probs["retire"] > action_probs["revise"]:
|
| 106 |
-
|
|
|
|
| 107 |
reason = "induction_timing_override"
|
| 108 |
-
elif
|
| 109 |
reason = "complexity_timing_override"
|
| 110 |
else:
|
| 111 |
reason = "soft_revise"
|
|
@@ -193,23 +196,27 @@ class RevisionController(nn.Module):
|
|
| 193 |
pressure: float,
|
| 194 |
viability: float,
|
| 195 |
proposal_score: float,
|
| 196 |
-
proposal_root_token: int | None,
|
| 197 |
) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
if action != "retire":
|
| 199 |
return False
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
if proposal_score < 0.95:
|
| 203 |
return False
|
| 204 |
retire_prob = float(action_probs.get("retire", 0.0))
|
| 205 |
revise_prob = float(action_probs.get("revise", 0.0))
|
| 206 |
-
if revise_prob < 0.
|
| 207 |
return False
|
| 208 |
-
if retire_prob - revise_prob > 0.
|
| 209 |
return False
|
| 210 |
if pressure >= 0.80:
|
| 211 |
return False
|
| 212 |
-
if viability < 0.
|
| 213 |
return False
|
| 214 |
return True
|
| 215 |
|
|
@@ -220,22 +227,26 @@ class RevisionController(nn.Module):
|
|
| 220 |
pressure: float,
|
| 221 |
viability: float,
|
| 222 |
proposal_score: float,
|
| 223 |
-
proposal_root_token: int | None,
|
| 224 |
) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
if action != "retire":
|
| 226 |
return False
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
if proposal_score < 0.85:
|
| 230 |
return False
|
| 231 |
retire_prob = float(action_probs.get("retire", 0.0))
|
| 232 |
revise_prob = float(action_probs.get("revise", 0.0))
|
| 233 |
-
if revise_prob < 0.
|
| 234 |
return False
|
| 235 |
-
if retire_prob - revise_prob > 0.
|
| 236 |
return False
|
| 237 |
if pressure >= 0.76:
|
| 238 |
return False
|
| 239 |
-
if viability < 0.
|
| 240 |
return False
|
| 241 |
return True
|
|
|
|
| 68 |
arbiter_out = arbiter.get(anchor.id)
|
| 69 |
prefer_current_prob = 1.0 if arbiter_out is None else float(arbiter_out.get("prefer_current_prob", 1.0))
|
| 70 |
proposal_score = 0.0 if arbiter_out is None else float(arbiter_out.get("proposal_score", 0.0))
|
| 71 |
+
# proposal_root_token removed: was hardcoded BPE ids (45, 35) from anchor-synthetic,
|
| 72 |
+
# which do not generalise to other datasets. Override logic now uses proposal_score only.
|
| 73 |
+
proposal_root_token = None # kept for RevisionDecision compat; no longer used for gating
|
| 74 |
action_probs = self._action_distribution(
|
| 75 |
anchor=anchor,
|
| 76 |
next_state=next_state,
|
|
|
|
| 105 |
alt_branch_used = False
|
| 106 |
elif action == "revise":
|
| 107 |
if action_probs["retire"] > action_probs["revise"]:
|
| 108 |
+
# reason derived from score band, not from dataset-specific token ids
|
| 109 |
+
if proposal_score >= 0.72:
|
| 110 |
reason = "induction_timing_override"
|
| 111 |
+
elif proposal_score >= 0.62:
|
| 112 |
reason = "complexity_timing_override"
|
| 113 |
else:
|
| 114 |
reason = "soft_revise"
|
|
|
|
| 196 |
pressure: float,
|
| 197 |
viability: float,
|
| 198 |
proposal_score: float,
|
| 199 |
+
proposal_root_token: int | None, # unused; kept for signature compat
|
| 200 |
) -> bool:
|
| 201 |
+
"""Override retire->revise when the alternative reading scores well enough.
|
| 202 |
+
|
| 203 |
+
Previously gated on proposal_root_token==45 (hardcoded BPE id from
|
| 204 |
+
anchor-synthetic). Replaced with a dataset-agnostic cosine-score threshold.
|
| 205 |
+
"""
|
| 206 |
if action != "retire":
|
| 207 |
return False
|
| 208 |
+
# Require a strong alternative signal (cosine similarity of alt repr vs future)
|
| 209 |
+
if proposal_score < 0.72:
|
|
|
|
| 210 |
return False
|
| 211 |
retire_prob = float(action_probs.get("retire", 0.0))
|
| 212 |
revise_prob = float(action_probs.get("revise", 0.0))
|
| 213 |
+
if revise_prob < 0.35:
|
| 214 |
return False
|
| 215 |
+
if retire_prob - revise_prob > 0.10:
|
| 216 |
return False
|
| 217 |
if pressure >= 0.80:
|
| 218 |
return False
|
| 219 |
+
if viability < 0.38:
|
| 220 |
return False
|
| 221 |
return True
|
| 222 |
|
|
|
|
| 227 |
pressure: float,
|
| 228 |
viability: float,
|
| 229 |
proposal_score: float,
|
| 230 |
+
proposal_root_token: int | None, # unused; kept for signature compat
|
| 231 |
) -> bool:
|
| 232 |
+
"""Override retire->revise for high-complexity anchors via cosine score.
|
| 233 |
+
|
| 234 |
+
Previously gated on proposal_root_token==35 (hardcoded BPE id from
|
| 235 |
+
anchor-synthetic). Replaced with a softer dataset-agnostic threshold.
|
| 236 |
+
"""
|
| 237 |
if action != "retire":
|
| 238 |
return False
|
| 239 |
+
# Slightly softer threshold - complexity anchors are noisier
|
| 240 |
+
if proposal_score < 0.62:
|
|
|
|
| 241 |
return False
|
| 242 |
retire_prob = float(action_probs.get("retire", 0.0))
|
| 243 |
revise_prob = float(action_probs.get("revise", 0.0))
|
| 244 |
+
if revise_prob < 0.35:
|
| 245 |
return False
|
| 246 |
+
if retire_prob - revise_prob > 0.08:
|
| 247 |
return False
|
| 248 |
if pressure >= 0.76:
|
| 249 |
return False
|
| 250 |
+
if viability < 0.40:
|
| 251 |
return False
|
| 252 |
return True
|
src/model/config.py
CHANGED
|
@@ -115,6 +115,14 @@ TOY_CONFIG = ModelConfig(
|
|
| 115 |
plastic_hidden=16,
|
| 116 |
)
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
PRESETS = {
|
| 119 |
"baseline-0": BASELINE_0,
|
| 120 |
"baseline-1-attnres": BASELINE_1_ATTNRES,
|
|
@@ -122,4 +130,5 @@ PRESETS = {
|
|
| 122 |
"baseline-3-plastic": BASELINE_3_PLASTIC,
|
| 123 |
"full": FULL_MODEL,
|
| 124 |
"toy": TOY_CONFIG,
|
|
|
|
| 125 |
}
|
|
|
|
| 115 |
plastic_hidden=16,
|
| 116 |
)
|
| 117 |
|
| 118 |
+
SCALEUP_CONFIG = ModelConfig(
|
| 119 |
+
vocab_size=512, d_model=512, n_heads=8,
|
| 120 |
+
n_layers=4, d_ff=1024, max_seq_len=128,
|
| 121 |
+
plastic_hidden=128,
|
| 122 |
+
anchor_threshold=0.2, anchor_ttl_init=4.0,
|
| 123 |
+
anchor_dead_end_threshold=0.5
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
PRESETS = {
|
| 127 |
"baseline-0": BASELINE_0,
|
| 128 |
"baseline-1-attnres": BASELINE_1_ATTNRES,
|
|
|
|
| 130 |
"baseline-3-plastic": BASELINE_3_PLASTIC,
|
| 131 |
"full": FULL_MODEL,
|
| 132 |
"toy": TOY_CONFIG,
|
| 133 |
+
"scaleup": SCALEUP_CONFIG,
|
| 134 |
}
|