guldo-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- guldo/__init__.py +0 -0
- guldo/_src/COMPILER.md +329 -0
- guldo/_src/DEPLOY.md +200 -0
- guldo/_src/LAUNCH.md +85 -0
- guldo/_src/PROTOCOL.md +294 -0
- guldo/_src/RULES.md +25 -0
- guldo/_src/SUBMIT.md +170 -0
- guldo/_src/__init__.py +0 -0
- guldo/_src/attest.py +257 -0
- guldo/_src/build_site.py +958 -0
- guldo/_src/bundle_templates/check.py +1382 -0
- guldo/_src/bundle_templates/qcheck.py +253 -0
- guldo/_src/bundle_templates/qscore.py +402 -0
- guldo/_src/bundle_templates/score.py +649 -0
- guldo/_src/certificate.py +828 -0
- guldo/_src/certs_site.py +108 -0
- guldo/_src/claims.py +343 -0
- guldo/_src/compile_domain.py +1312 -0
- guldo/_src/compile_quality.py +286 -0
- guldo/_src/count_runner.py +248 -0
- guldo/_src/counted.py +274 -0
- guldo/_src/declared.py +215 -0
- guldo/_src/deploy/Caddyfile +48 -0
- guldo/_src/deploy/ledger-backup.service +9 -0
- guldo/_src/deploy/ledger-backup.sh +73 -0
- guldo/_src/deploy/ledger-backup.timer +10 -0
- guldo/_src/deploy/ledger-export.service +12 -0
- guldo/_src/deploy/ledger-export.timer +9 -0
- guldo/_src/deploy/ledger-web.service +38 -0
- guldo/_src/deploy/pin_cpu.sh +34 -0
- guldo/_src/deploy/provision_eval.sh +128 -0
- guldo/_src/deploy_smoke.py +148 -0
- guldo/_src/dlog_experiment.py +165 -0
- guldo/_src/domains/aidecode/compiled.json +586 -0
- guldo/_src/domains/aidecode/decl.py +134 -0
- guldo/_src/domains/aidecode/reference.py +73 -0
- guldo/_src/domains/aiinfer/compiled.json +722 -0
- guldo/_src/domains/aiinfer/decl.py +91 -0
- guldo/_src/domains/aiinfer/reference.py +42 -0
- guldo/_src/domains/dlog/compiled.json +248 -0
- guldo/_src/domains/dlog/decl.py +41 -0
- guldo/_src/domains/dlog/reference.py +10 -0
- guldo/_src/domains/maxcut/compiled.json +291 -0
- guldo/_src/domains/maxcut/decl.py +46 -0
- guldo/_src/domains/maxcut/reference.py +16 -0
- guldo/_src/drift_check.py +89 -0
- guldo/_src/export_bundle.py +750 -0
- guldo/_src/extrapolate.py +326 -0
- guldo/_src/families.py +53 -0
- guldo/_src/gen_cheat_table.py +36 -0
- guldo/_src/gpu_meter.py +634 -0
- guldo/_src/gpu_runner.py +63 -0
- guldo/_src/harness.py +273 -0
- guldo/_src/instances.py +86 -0
- guldo/_src/instances_factor.py +40 -0
- guldo/_src/instances_mlwe.py +69 -0
- guldo/_src/judge.py +278 -0
- guldo/_src/limits_plan.py +158 -0
- guldo/_src/official.py +189 -0
- guldo/_src/ops.py +281 -0
- guldo/_src/portal.py +313 -0
- guldo/_src/portal_core.py +57 -0
- guldo/_src/power.py +174 -0
- guldo/_src/prereg.py +173 -0
- guldo/_src/public_portal.py +500 -0
- guldo/_src/quality_core.py +126 -0
- guldo/_src/recheck.py +236 -0
- guldo/_src/remeasure.py +88 -0
- guldo/_src/report_html.py +493 -0
- guldo/_src/results/.keep +0 -0
- guldo/_src/rounds.py +846 -0
- guldo/_src/runner.py +52 -0
- guldo/_src/sandbox/Dockerfile +11 -0
- guldo/_src/sandbox/Dockerfile.v2 +19 -0
- guldo/_src/sandbox/README.md +40 -0
- guldo/_src/sandbox/linux_selftest.py +117 -0
- guldo/_src/sandbox/meter.py +89 -0
- guldo/_src/sandbox/meter_core.py +28 -0
- guldo/_src/site_assets.py +176 -0
- guldo/_src/site_live_check.py +79 -0
- guldo/_src/solvers/aidecode_flops_kv.py +73 -0
- guldo/_src/solvers/aidecode_flops_lossy.py +73 -0
- guldo/_src/solvers/aidecode_flops_ref.py +64 -0
- guldo/_src/solvers/aidecode_kv.py +76 -0
- guldo/_src/solvers/aidecode_kv_f32.py +77 -0
- guldo/_src/solvers/aidecode_lossy.py +76 -0
- guldo/_src/solvers/aidecode_repeat.py +5 -0
- guldo/_src/solvers/aiinfer_batched.py +40 -0
- guldo/_src/solvers/aiinfer_f32.py +41 -0
- guldo/_src/solvers/aiinfer_gpu_batched.py +42 -0
- guldo/_src/solvers/aiinfer_gpu_ref.py +45 -0
- guldo/_src/solvers/aiinfer_lossy.py +42 -0
- guldo/_src/solvers/baseline_bkz.py +14 -0
- guldo/_src/solvers/cheat_lookup.py +24 -0
- guldo/_src/solvers/cheat_table.json +1 -0
- guldo/_src/solvers/cheat_table_v1_stale.json +1 -0
- guldo/_src/solvers/daemon_cpu_hider.py +17 -0
- guldo/_src/solvers/dlog_bsgs.py +19 -0
- guldo/_src/solvers/dlog_stride4.py +18 -0
- guldo/_src/solvers/egg_base.py +11 -0
- guldo/_src/solvers/egg_eightaxis.py +68 -0
- guldo/_src/solvers/egg_template.py +11 -0
- guldo/_src/solvers/factor_brent.py +34 -0
- guldo/_src/solvers/factor_floyd.py +19 -0
- guldo/_src/solvers/factor_trial_fast_py.py +21 -0
- guldo/_src/solvers/factor_trial_np.py +21 -0
- guldo/_src/solvers/factor_trial_py.py +12 -0
- guldo/_src/solvers/fixed_overhead_ref.py +21 -0
- guldo/_src/solvers/fork_bomb.py +18 -0
- guldo/_src/solvers/gpu_noop.py +6 -0
- guldo/_src/solvers/hostile_probe.py +36 -0
- guldo/_src/solvers/kill_meter.py +27 -0
- guldo/_src/solvers/memory_bomb.py +10 -0
- guldo/_src/solvers/noop.py +5 -0
- guldo/_src/solvers/orphan_cpu_hider.py +16 -0
- guldo/_src/solvers/planted_peek.py +23 -0
- guldo/_src/solvers/sleeping_daemon_hang.py +12 -0
- guldo/_src/solvers/small_n_trick.py +22 -0
- guldo/_src/solvers/starter_template.py +21 -0
- guldo/_src/solvers/stdout_flood.py +9 -0
- guldo/_src/solvers/tmp_fill.py +10 -0
- guldo/_src/solvers/tuned_bkz.py +25 -0
- guldo/_src/static_check.py +214 -0
- guldo/_src/submit_check.py +166 -0
- guldo/_src/svp.py +889 -0
- guldo/_src/tensorops.py +304 -0
- guldo/_src/tflops.py +453 -0
- guldo/_src/tflops_runner.py +134 -0
- guldo/cli.py +60 -0
- guldo_cli-0.1.0.dist-info/METADATA +30 -0
- guldo_cli-0.1.0.dist-info/RECORD +134 -0
- guldo_cli-0.1.0.dist-info/WHEEL +5 -0
- guldo_cli-0.1.0.dist-info/entry_points.txt +2 -0
- guldo_cli-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"""Compiler for QUALITY challenges (see COMPILER.md, "Quality challenges"). A declaration that defines `quality(inst, ans) -> float in [0, 1]` is a quality challenge:
|
|
2
|
+
every solver gets the same CPU budget per instance and is scored on how good its answer is, not how fast it is.
|
|
3
|
+
|
|
4
|
+
decl.py (extra, all optional except quality)
|
|
5
|
+
quality(inst, ans) called ONLY on answers verify() accepted; 0.0 = worst valid answer, 1.0 = best known/possible. Pure and deterministic.
|
|
6
|
+
QUALITY_BUDGET CPU seconds per instance for everyone, the reference included (default 30)
|
|
7
|
+
QUALITY_MIN_GAIN the smallest mean gain (in quality points, 0..1) worth certifying (default 0.02); power is computed at this threshold
|
|
8
|
+
|
|
9
|
+
The compiler validates quality(), measures the reference's quality over the size range on the public profiling seed, keeps the sizes where the reference leaves headroom,
|
|
10
|
+
chooses the instances per size from a power simulation, and writes compiled.json with kind "quality". A quality domain is deliberately NOT registered as a speed family:
|
|
11
|
+
the judge, harness and portal would score it by speed. Its product is the challenge bundle: `compile_domain.py export NAME`.
|
|
12
|
+
|
|
13
|
+
What this compiler does not know: whether quality() measures what you care about, or whether the reference is a strong baseline (say so in REFERENCE_NOTE).
|
|
14
|
+
"""
|
|
15
|
+
import json
|
|
16
|
+
import math
|
|
17
|
+
import os
|
|
18
|
+
import random
|
|
19
|
+
import statistics
|
|
20
|
+
import time
|
|
21
|
+
|
|
22
|
+
import compile_domain as CD
|
|
23
|
+
import declared
|
|
24
|
+
import harness
|
|
25
|
+
import quality_core as Q
|
|
26
|
+
|
|
27
|
+
KIND = "quality"
|
|
28
|
+
DEFAULT_BUDGET = 30
|
|
29
|
+
DEFAULT_MIN_GAIN = 0.02
|
|
30
|
+
NS = (12, 24, 48, 96, 192)
|
|
31
|
+
MAX_SIZES = 5
|
|
32
|
+
HEADROOM_MEAN = 0.95 # a size where the reference averages above this leaves too little room to show a gain
|
|
33
|
+
NOISE_FLOOR, NOISE_CAP = 0.02, 0.25
|
|
34
|
+
NOISE_SHARE = 1.0 # ASSUMED: a solver's per-instance gain varies as much as the reference's own quality does (one real pair on maxcut: gain sd 0.036 vs reference sd 0.02-0.04;
|
|
35
|
+
# qscore.py measures the real value and re-simulates)
|
|
36
|
+
SEEDS = 3
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def is_quality(mod):
|
|
40
|
+
return callable(getattr(mod, "quality", None))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def settings(mod):
|
|
44
|
+
"""-> (budget seconds, min_gain). Raises CD.CompileError on a malformed value."""
|
|
45
|
+
budget, gain = getattr(mod, "QUALITY_BUDGET", DEFAULT_BUDGET), getattr(mod, "QUALITY_MIN_GAIN", DEFAULT_MIN_GAIN)
|
|
46
|
+
num = lambda v: isinstance(v, (int, float)) and not isinstance(v, bool) and math.isfinite(v) # noqa: E731
|
|
47
|
+
if not (num(budget) and 1 <= budget <= 600):
|
|
48
|
+
raise CD.CompileError("QUALITY_BUDGET must be a number of CPU seconds from 1 to 600")
|
|
49
|
+
if not (num(gain) and 0 < gain <= 0.5):
|
|
50
|
+
raise CD.CompileError("QUALITY_MIN_GAIN must be a number in (0, 0.5]: the smallest mean quality gain worth certifying, in quality points")
|
|
51
|
+
return float(budget), float(gain)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def score_answer(mod, inst, ans):
|
|
55
|
+
"""The quality of a verified answer, checked to be a finite number in [0, 1]. The scorer treats a violation as the declaration's bug, never as a score."""
|
|
56
|
+
q = mod.quality(inst, ans)
|
|
57
|
+
if isinstance(q, bool) or not isinstance(q, (int, float)) or not math.isfinite(q) or not 0.0 <= q <= 1.0:
|
|
58
|
+
raise CD.CompileError(f"quality() returned {q!r} (type {type(q).__name__}); it must return a finite number in [0, 1]")
|
|
59
|
+
return float(q)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def measure(mod, ref, master, size, idx, budget):
|
|
63
|
+
"""Run the reference once on one instance under the CPU budget -> {"status", "quality", "cpu"}. An unanswered or invalid instance scores 0."""
|
|
64
|
+
derive, verify = declared.make_spec(mod)
|
|
65
|
+
inst = derive(master, size, idx)
|
|
66
|
+
rc, out, err, cpu = harness._exec_local(ref, json.dumps({"args": inst["args"]}), budget)
|
|
67
|
+
import signal
|
|
68
|
+
if rc == -signal.SIGXCPU or cpu > budget or rc == -signal.SIGKILL:
|
|
69
|
+
return {"status": "timeout", "quality": 0.0, "cpu": min(cpu, budget)}
|
|
70
|
+
if rc != 0 or not out.strip():
|
|
71
|
+
return {"status": "crash", "quality": 0.0, "cpu": cpu, "err": err[-300:]}
|
|
72
|
+
try:
|
|
73
|
+
ans = json.loads(out.strip().splitlines()[-1])
|
|
74
|
+
ok = bool(verify(inst, ans))
|
|
75
|
+
except Exception:
|
|
76
|
+
return {"status": "wrong", "quality": 0.0, "cpu": cpu}
|
|
77
|
+
if not ok:
|
|
78
|
+
return {"status": "failed" if ans is None else "wrong", "quality": 0.0, "cpu": cpu}
|
|
79
|
+
return {"status": "solved", "quality": score_answer(mod, inst, ans), "cpu": cpu}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# ------------------------------------------------------------------------------------------------------------------------ validation
|
|
83
|
+
def quality_validation(mod, dirpath):
|
|
84
|
+
"""The checks specific to quality(): its settings, range and determinism on planted witnesses, and that it is not a constant."""
|
|
85
|
+
out = []
|
|
86
|
+
try:
|
|
87
|
+
budget, gain = settings(mod)
|
|
88
|
+
except CD.CompileError as e:
|
|
89
|
+
return [CD.R("quality settings", "fail", str(e))]
|
|
90
|
+
out.append(CD.R("quality settings", "pass", f"budget {budget:g} s per instance, certification threshold {gain:g} quality points"))
|
|
91
|
+
derive, verify = declared.make_spec(mod)
|
|
92
|
+
lo, hi = mod.SIZE_RANGE
|
|
93
|
+
step = getattr(mod, "SIZE_STEP", 1)
|
|
94
|
+
sizes = sorted({lo, lo + ((hi - lo) // (2 * step)) * step, hi})
|
|
95
|
+
master = b"svp-compiler-validate-A"
|
|
96
|
+
seen = []
|
|
97
|
+
try:
|
|
98
|
+
for s in sizes:
|
|
99
|
+
for i in range(3):
|
|
100
|
+
x = derive(master, s, i)
|
|
101
|
+
w = CD._witness(mod, x)
|
|
102
|
+
if not verify(x, w):
|
|
103
|
+
raise CD.CompileError(f"verify() rejected the planted witness at size {s}: quality() is only defined on verified answers")
|
|
104
|
+
q1, q2 = score_answer(mod, x, w), score_answer(mod, x, w)
|
|
105
|
+
if q1 != q2:
|
|
106
|
+
raise CD.CompileError(f"quality() gave {q1} then {q2} for the same answer at size {s}: it must be deterministic")
|
|
107
|
+
seen.append(q1)
|
|
108
|
+
out.append(CD.R("quality range", "pass", f"finite, in [0, 1] and deterministic on {len(seen)} planted witnesses (range {min(seen):.3f}..{max(seen):.3f})"))
|
|
109
|
+
except CD.CompileError as e:
|
|
110
|
+
out.append(CD.R("quality range", "fail", str(e)))
|
|
111
|
+
except Exception as e: # the author's code can raise anything
|
|
112
|
+
out.append(CD.R("quality range", "fail", f"quality() raised {type(e).__name__}: {e}"))
|
|
113
|
+
return out
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
# ------------------------------------------------------------------------------------------------------------------------ profile + plan
|
|
117
|
+
def candidate_sizes(mod, limit=8):
|
|
118
|
+
lo, hi = mod.SIZE_RANGE
|
|
119
|
+
step = getattr(mod, "SIZE_STEP", 1)
|
|
120
|
+
allsz = list(range(lo, hi + 1, step))
|
|
121
|
+
if len(allsz) <= limit:
|
|
122
|
+
return allsz
|
|
123
|
+
return sorted({allsz[round(i * (len(allsz) - 1) / (limit - 1))] for i in range(limit)})
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def profile(mod, ref, budget, k, log=CD._log):
|
|
127
|
+
"""{size: {"q": [...], "status": [...], "cpu": [...]}} for the reference on the public profiling seed."""
|
|
128
|
+
pts = {}
|
|
129
|
+
for s in candidate_sizes(mod):
|
|
130
|
+
recs = [measure(mod, ref, CD.PROFILE_SEED, s, i, budget) for i in range(k)]
|
|
131
|
+
pts[s] = {"q": [r["quality"] for r in recs], "status": [r["status"] for r in recs], "cpu": [r["cpu"] for r in recs]}
|
|
132
|
+
crashed = [r for r in recs if r["status"] == "crash"]
|
|
133
|
+
if crashed:
|
|
134
|
+
raise CD.CompileError(f"the reference crashed at size {s}: {crashed[0].get('err', '')[-200:]}")
|
|
135
|
+
log(f"profile: size {s}: reference quality mean {statistics.fmean(pts[s]['q']):.3f} (n={k}, "
|
|
136
|
+
f"{sum(x == 'timeout' for x in pts[s]['status'])} timeouts, median cpu {statistics.median(pts[s]['cpu']):.2f}s)")
|
|
137
|
+
return pts
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def choose_sizes(pts, notes):
|
|
141
|
+
"""The sizes with headroom, thinned to at most MAX_SIZES evenly (always keeping the largest)."""
|
|
142
|
+
usable = [s for s in sorted(pts) if statistics.fmean(pts[s]["q"]) <= HEADROOM_MEAN]
|
|
143
|
+
if not usable:
|
|
144
|
+
raise CD.CompileError(f"the reference averages above {HEADROOM_MEAN} quality at every size in SIZE_RANGE: no headroom to show a gain. Raise SIZE_RANGE's upper end or weaken the "
|
|
145
|
+
"reference's claim on the problem (a saturated baseline makes every solver tie)")
|
|
146
|
+
dropped = [s for s in sorted(pts) if s not in usable]
|
|
147
|
+
if dropped:
|
|
148
|
+
notes.append(f"sizes {dropped} dropped: the reference is already above {HEADROOM_MEAN} quality there")
|
|
149
|
+
if len(usable) > MAX_SIZES:
|
|
150
|
+
usable = sorted({usable[round(i * (len(usable) - 1) / (MAX_SIZES - 1))] for i in range(MAX_SIZES)})
|
|
151
|
+
return usable
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def profile_for_power(pts, sizes):
|
|
155
|
+
return {"sizes": sizes, "mean": {str(s): statistics.fmean(pts[s]["q"]) for s in sizes},
|
|
156
|
+
"sd": {str(s): statistics.pstdev(pts[s]["q"]) if len(pts[s]["q"]) > 1 else 0.0 for s in sizes}}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def plan_power(prof, gain, target_power, trials, boot, log=CD._log):
|
|
160
|
+
"""Smallest n (from NS) whose all-seeds power at a true gain of 2 x threshold reaches target_power, then the power curve at that n."""
|
|
161
|
+
sd = statistics.fmean(prof["sd"].values())
|
|
162
|
+
noise = min(max(NOISE_SHARE * sd, NOISE_FLOOR), NOISE_CAP)
|
|
163
|
+
target = round(2 * gain, 4)
|
|
164
|
+
rng = random.Random(0)
|
|
165
|
+
tried, chosen = {}, NS[-1]
|
|
166
|
+
for n in NS:
|
|
167
|
+
p = Q.power_at(prof, n, target, noise, gain, SEEDS, trials, boot, rng)
|
|
168
|
+
tried[n] = round(p["all_seeds"], 3)
|
|
169
|
+
log(f"power: n={n}: P(all {SEEDS} seeds >= {gain:g} | true gain {target:g}) = {p['all_seeds']:.2f}")
|
|
170
|
+
chosen = n
|
|
171
|
+
if p["all_seeds"] >= target_power:
|
|
172
|
+
break
|
|
173
|
+
effects = Q.effects_for(gain)
|
|
174
|
+
table = Q.power_table(prof, chosen, noise, gain, SEEDS, trials, boot, effects, seed=1)
|
|
175
|
+
lo, hi = Q.certifiable_range(table, target_power)
|
|
176
|
+
notes = []
|
|
177
|
+
if tried[chosen] < target_power:
|
|
178
|
+
notes.append(f"even n={chosen} (the largest allowed) certifies a gain of {target:g} only {100 * tried[chosen]:.0f}% of the time")
|
|
179
|
+
if table[0]["single"] > 0.10:
|
|
180
|
+
notes.append(f"FALSE-CREDIT RATE {100 * table[0]['single']:.0f}% at zero gain (> 10%): the lower bound is not holding on this ladder")
|
|
181
|
+
return {"n": chosen, "noise": round(noise, 4), "noise_assumed": True, "target_effect": target, "target_power": target_power, "trials": trials, "boot": boot,
|
|
182
|
+
"threshold": gain, "tried": {str(k): v for k, v in tried.items()}, "min_certifiable": lo, "max_certifiable": hi,
|
|
183
|
+
"curve": {str(e): {"single": round(v["single"], 3), "all_seeds": round(v["all_seeds"], 3)} for e, v in table.items()}, "notes": notes}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def selftest(prof, n, gain, noise, boot=60):
|
|
187
|
+
"""Controls on the estimator, with ladders drawn from the compiled profile (the bundle's own selftest covers the declaration and the pipeline):
|
|
188
|
+
zero gain must credit 0; a solver that never answers must credit 0; a known gain of 3 x threshold must be credited at least the threshold."""
|
|
189
|
+
rng = random.Random(7)
|
|
190
|
+
base, same_ = Q.synth_ladders(prof, n, 0.0, 0.0, rng)
|
|
191
|
+
zero = Q.seed_credit(base, {d: dict(v) for d, v in base.items()}, rng, boot)["credit_quality"]
|
|
192
|
+
none = Q.seed_credit(base, {d: {i: 0.0 for i in v} for d, v in base.items()}, rng, boot)["credit_quality"]
|
|
193
|
+
b2, s2 = Q.synth_ladders(prof, n, 3 * gain, 0.0, rng)
|
|
194
|
+
known = Q.seed_credit(b2, s2, rng, boot)["credit_quality"]
|
|
195
|
+
controls = [CD.R("zero gain", "pass" if zero == 0 else "fail", f"reference vs itself credits {zero:.4f} (must be 0)"),
|
|
196
|
+
CD.R("never answers", "pass" if none == 0 else "fail", f"a solver with no valid answer credits {none:.4f} (must be 0)"),
|
|
197
|
+
CD.R("known gain", "pass" if known >= gain else "fail", f"a true gain of {3 * gain:g} is credited {known:.4f} (must reach {gain:g})")]
|
|
198
|
+
return {"status": CD.worst(controls) if CD.worst(controls) != "warn" else "pass", "controls": controls,
|
|
199
|
+
"kind": "estimator controls on synthetic ladders from the profile; the exported bundle's `check.py selftest` covers the declaration itself"}
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def verdict(compiled):
|
|
203
|
+
name, qc = compiled["name"], compiled["quality"]
|
|
204
|
+
pw = qc["power"]
|
|
205
|
+
st = (compiled.get("selftest") or {}).get("status", "pending")
|
|
206
|
+
notes = list(qc.get("notes", [])) + list(pw["notes"])
|
|
207
|
+
mce, top = pw["min_certifiable"], pw["max_certifiable"]
|
|
208
|
+
if st == "fail" or mce is None:
|
|
209
|
+
level = "NOT READY"
|
|
210
|
+
elif st in ("skipped", "pending"):
|
|
211
|
+
level = "DRAFT (no self-test)"
|
|
212
|
+
elif notes:
|
|
213
|
+
level = "READY, WITH NOTES (read them)"
|
|
214
|
+
else:
|
|
215
|
+
level = "READY"
|
|
216
|
+
cost = qc["cpu_hours"]
|
|
217
|
+
lines = [f"{name}: {level} [quality challenge]",
|
|
218
|
+
f" the challenge : sizes {compiled['dims']}, {qc['budget']:g} CPU s per instance, {compiled['official']['n']} instances per size; one submission costs up to {cost['worst_case']} CPU-hours",
|
|
219
|
+
" it can certify: " + (f"mean quality gains from about {mce:g} to about {top:g} (on the 0-1 quality scale) over the reference (threshold {pw['threshold']:g}, {SEEDS} seeds, "
|
|
220
|
+
f"{int(100 * pw['target_power'])}% power)" if mce is not None else "NOTHING on the grid tried: the ladder is too weak (see the notes)"),
|
|
221
|
+
f" noise assumed : {pw['noise']:g} quality points per instance (an ASSUMPTION; `score.py` measures the real value and re-simulates)",
|
|
222
|
+
f" self-test : {st}"] + [f" note : {x}" for x in notes]
|
|
223
|
+
nxt = [f"python3 compile_domain.py export {name} # the challenge bundle: checker, scorer, power analysis"]
|
|
224
|
+
if st in ("skipped", "pending"):
|
|
225
|
+
nxt.insert(0, f"python3 compile_domain.py compile {name} # the full compile with the self-test")
|
|
226
|
+
return level, lines + [" next:"] + [f" {x}" for x in nxt]
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
# ------------------------------------------------------------------------------------------------------------------------ the compile
|
|
230
|
+
def compile_dir(dirpath, *, quick=False, k=None, target_power=0.8, trials=None, boot=None, skip_selftest=False, log=CD._log):
|
|
231
|
+
dirpath = os.path.abspath(dirpath)
|
|
232
|
+
mod, ref, res = CD.parse(dirpath)
|
|
233
|
+
log("parse:")
|
|
234
|
+
CD._print(res)
|
|
235
|
+
if CD.worst(res) == "fail":
|
|
236
|
+
raise CD.CompileError("the declaration did not parse")
|
|
237
|
+
if not is_quality(mod):
|
|
238
|
+
raise CD.CompileError("this declaration has no quality(inst, ans) function: it is a speed challenge (use compile_domain.py compile)")
|
|
239
|
+
k = k or (3 if quick else 8)
|
|
240
|
+
trials = trials or (8 if quick else 30)
|
|
241
|
+
boot = boot or (30 if quick else 60)
|
|
242
|
+
quality_checks = quality_validation(mod, dirpath)
|
|
243
|
+
budget, gain = settings(mod) if CD.worst(quality_checks) != "fail" else (DEFAULT_BUDGET, DEFAULT_MIN_GAIN)
|
|
244
|
+
log("validate:")
|
|
245
|
+
val = CD.validate(mod, dirpath, ref, quality=mod.quality) + quality_checks
|
|
246
|
+
CD._print(val)
|
|
247
|
+
if CD.worst(val) == "fail":
|
|
248
|
+
raise CD.CompileError("validation failed; nothing was compiled. Fix the FAIL items above.")
|
|
249
|
+
with CD.provisional(mod):
|
|
250
|
+
pts = profile(mod, ref, budget, k, log)
|
|
251
|
+
notes = []
|
|
252
|
+
sizes = choose_sizes(pts, notes)
|
|
253
|
+
allq = [q for s in pts for q in pts[s]["q"]]
|
|
254
|
+
if len(set(allq)) == 1:
|
|
255
|
+
raise CD.CompileError(f"the reference scores exactly {allq[0]} on every instance at every size: a constant measures nothing")
|
|
256
|
+
prof = profile_for_power(pts, sizes)
|
|
257
|
+
pw = plan_power(prof, gain, target_power, trials, boot, log)
|
|
258
|
+
n = pw["n"]
|
|
259
|
+
hist = {s: statistics.median(pts[s]["cpu"]) for s in sizes}
|
|
260
|
+
cost = {"worst_case": round(len(sizes) * n * budget * 2 * SEEDS / 3600.0, 2),
|
|
261
|
+
"reference_expected": round(sum(statistics.fmean(pts[s]["cpu"]) for s in sizes) * n * SEEDS / 3600.0, 2)}
|
|
262
|
+
practice = [s for s in sorted(pts) if all(x == "solved" for x in pts[s]["status"]) and statistics.median(pts[s]["cpu"]) <= 1.0][:2] or [sizes[0]]
|
|
263
|
+
if any(x == "timeout" for s in sizes for x in pts[s]["status"]):
|
|
264
|
+
notes.append("the reference times out on some instances: they count 0 for the baseline, so ANY valid answer there is a gain (intended? say so in REFERENCE_NOTE)")
|
|
265
|
+
compiled = {
|
|
266
|
+
"format": declared.FORMAT, "kind": KIND, "name": mod.NAME, "version": mod.VERSION, "compiled_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
|
267
|
+
"decl_sha256": declared.sha256_file(os.path.join(dirpath, declared.DECL)), "reference_sha256": declared.sha256_file(ref),
|
|
268
|
+
"compiler_sha256": declared.sha256_file(os.path.abspath(CD.__file__)), "quality_compiler_sha256": declared.sha256_file(os.path.abspath(__file__)),
|
|
269
|
+
"reference_note": (getattr(mod, "REFERENCE_NOTE", "") or "").strip(), "size_range": list(mod.SIZE_RANGE), "size_step": getattr(mod, "SIZE_STEP", 1),
|
|
270
|
+
"official": {"n": n, "budget": budget}, "dims": sizes, "practice_dims": practice,
|
|
271
|
+
"quality": {"budget": budget, "min_gain": gain, "power": pw, "cpu_hours": cost, "notes": notes, "headroom_mean_cap": HEADROOM_MEAN,
|
|
272
|
+
"profile": {"seed": CD.PROFILE_SEED.decode(), "k": k, "median_cpu": {str(s): round(v, 3) for s, v in hist.items()},
|
|
273
|
+
"sizes": {str(s): {"mean": round(statistics.fmean(pts[s]["q"]), 4), "sd": round(prof["sd"].get(str(s), statistics.pstdev(pts[s]["q"])), 4),
|
|
274
|
+
"timeouts": sum(x == "timeout" for x in pts[s]["status"]), "q": [round(q, 4) for q in pts[s]["q"]]} for s in sorted(pts)}},
|
|
275
|
+
"simulation": prof},
|
|
276
|
+
"validation": res + val, "selftest": {"status": "skipped"} if skip_selftest or quick else {"status": "pending"}}
|
|
277
|
+
if compiled["selftest"]["status"] == "pending":
|
|
278
|
+
compiled["selftest"] = selftest(prof, n, gain, pw["noise"], boot)
|
|
279
|
+
CD.write_compiled(dirpath, compiled)
|
|
280
|
+
log(f"compiled: sizes {sizes}, n={n}, budget {budget:g} s, certifiable gain {pw['min_certifiable']}..{pw['max_certifiable']} points, worst case {cost['worst_case']} CPU-h per submission")
|
|
281
|
+
for note in notes + pw["notes"]:
|
|
282
|
+
log(" NOTE " + note)
|
|
283
|
+
if compiled["selftest"].get("controls"):
|
|
284
|
+
log("selftest:")
|
|
285
|
+
CD._print(compiled["selftest"]["controls"])
|
|
286
|
+
return compiled
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"""Run `solve(*args)` of one solver file under EXACT Python bytecode-instruction counting, and report the count. Needs Python 3.12+ (sys.monitoring).
|
|
2
|
+
|
|
3
|
+
This is the child process of counted.py; it is not meant to be run by hand. Protocol:
|
|
4
|
+
argv[1] the solver file (a top-level `solve`)
|
|
5
|
+
stdin {"args": [...], "limit": N} N = the most instructions the solver may execute
|
|
6
|
+
stdout the LAST line is {"status": "ok" | "limit" | "error", "steps": int, "ans": ..., "err": "..."}
|
|
7
|
+
|
|
8
|
+
The solver runs in a restricted subset of Python (see check_source below): pure computation, no sys, files, frames, getattr/type/eval/open, dunder attributes, and
|
|
9
|
+
imports only from a few pure modules, enforced here before and while it runs.
|
|
10
|
+
|
|
11
|
+
What is counted: every bytecode instruction executed from the start of the solver's module body through the end of `solve`, in the solver's own code and in any
|
|
12
|
+
pure-Python code it calls: top-level precomputation is charged too (otherwise a solver could build a lookup table at import time for free, or spin there for ever).
|
|
13
|
+
Validation and compilation happen before counting starts. What is NOT counted: the counter's own callback, and anything that happens
|
|
14
|
+
INSIDE a C function: `pow(a, b, m)` on huge integers, `sorted`, `sum`, numpy and fpylll each cost ONE instruction however long they run. That is the
|
|
15
|
+
honest limit of this cost model (see COMPILER.md): it measures how much Python a solver executes, which is exactly reproducible, not how long it takes.
|
|
16
|
+
|
|
17
|
+
Determinism: the cyclic garbage collector is disabled while counting (its timing would otherwise depend on allocation history), the parent fixes
|
|
18
|
+
PYTHONHASHSEED, and nothing about the machine (speed, load, other processes) enters the count. For a solver that is itself deterministic, the same
|
|
19
|
+
program on the same input under the SAME Python version and build gives the same number every time. That is a claim to CHECK, not to assume: a solver
|
|
20
|
+
whose behaviour depends on memory addresses (ordering by id(), say), on the clock or on randomness will count differently between runs, and so will a
|
|
21
|
+
different Python version. counted.py selfcheck / `practice --count` run an instance twice and refuse to certify if the counts differ.
|
|
22
|
+
"""
|
|
23
|
+
import ast
|
|
24
|
+
import builtins
|
|
25
|
+
import gc
|
|
26
|
+
import json
|
|
27
|
+
import os
|
|
28
|
+
import sys
|
|
29
|
+
import types
|
|
30
|
+
|
|
31
|
+
TOOL_ID = 3 # sys.monitoring tool ids 0-2 and 5 are taken by the debugger, coverage, profiler and optimizer: 3 and 4 are free
|
|
32
|
+
|
|
33
|
+
# ------------------------------------------------------------------------------------------------------------- the restricted subset a counted solver is written in
|
|
34
|
+
# A counted solver is pure computation. It runs in the SAME process as the counter, so it must not be able to reach the counter, the runner's stack frame (which holds
|
|
35
|
+
# it), the answer channel (the real stdout), files or processes. That is enforced here, not by trust: (1) the source is checked against a whitelist (below), (2) it runs
|
|
36
|
+
# with a minimal `__builtins__` (no getattr, type, open, eval, exec, import machinery, sys), (3) `import` yields proxies that expose only named functions of a few pure
|
|
37
|
+
# modules (a real module would leak others: `fractions.sys`). It is a sandbox for CPython-level escapes, not for bugs in CPython or for work hidden in C calls
|
|
38
|
+
# (see the module docstring), and the whole of it is pinned by the counter hash: a different rule set is a different counter.
|
|
39
|
+
ALLOWED_MODULES = { # name -> the names it exposes (None: every public name; math and itertools are C modules and refer to no other module)
|
|
40
|
+
"math": None,
|
|
41
|
+
"itertools": None,
|
|
42
|
+
"heapq": ("heappush", "heappop", "heapify", "heapreplace", "heappushpop", "merge", "nlargest", "nsmallest"),
|
|
43
|
+
"bisect": ("bisect", "bisect_left", "bisect_right", "insort", "insort_left", "insort_right"),
|
|
44
|
+
"functools": ("reduce", "partial", "lru_cache", "cache", "cmp_to_key", "total_ordering"),
|
|
45
|
+
"collections": ("deque", "defaultdict", "Counter", "OrderedDict", "namedtuple"),
|
|
46
|
+
"fractions": ("Fraction",),
|
|
47
|
+
}
|
|
48
|
+
_BUILTIN_NAMES = ("abs all any bin bool bytearray bytes callable chr complex dict divmod enumerate filter float frozenset hash hex int isinstance issubclass iter len list map max min "
|
|
49
|
+
"next oct ord pow print range repr reversed round set slice sorted str sum tuple zip "
|
|
50
|
+
"Exception ArithmeticError AssertionError AttributeError ImportError IndexError KeyError LookupError MemoryError NameError NotImplementedError OverflowError "
|
|
51
|
+
"RecursionError RuntimeError StopIteration TypeError ValueError ZeroDivisionError NotImplemented").split()
|
|
52
|
+
_BANNED_NAMES = {"eval", "exec", "compile", "open", "getattr", "setattr", "delattr", "hasattr", "vars", "globals", "locals", "dir", "type", "object", "super", "input", "breakpoint",
|
|
53
|
+
"memoryview", "id", "help", "exit", "quit", "property", "staticmethod", "classmethod", "BaseException", "SystemExit", "KeyboardInterrupt", "GeneratorExit"}
|
|
54
|
+
_BANNED_ATTR_PREFIXES = ("_", "gi_", "cr_", "ag_", "f_", "tb_", "co_") # dunders, privates, and the frame / generator / traceback / code-object attributes that lead to a stack frame
|
|
55
|
+
_BANNED_ATTRS = {"format", "format_map", "mro"} # str.format reads attributes by string: "{0.__class__}"
|
|
56
|
+
_DUNDER_DEFS = {"__init__", "__repr__", "__str__", "__eq__", "__ne__", "__lt__", "__le__", "__gt__", "__ge__", "__hash__", "__len__", "__iter__", "__next__", "__getitem__",
|
|
57
|
+
"__setitem__", "__contains__", "__add__", "__sub__", "__mul__", "__neg__", "__bool__", "__call__"} # a solver may DEFINE these; it may not name them elsewhere
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _bad_attr(name):
|
|
61
|
+
return name.startswith(_BANNED_ATTR_PREFIXES) or name in _BANNED_ATTRS
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def check_source(text):
|
|
65
|
+
"""-> [problems] (empty: the source is inside the restricted subset). Static, on the syntax tree: no dunder or private names or attributes, no frame/generator/traceback
|
|
66
|
+
attributes, no str.format, no eval/exec/getattr/type/open/..., no async code, and imports only from ALLOWED_MODULES (and only their listed names)."""
|
|
67
|
+
try:
|
|
68
|
+
tree = ast.parse(text)
|
|
69
|
+
except (SyntaxError, ValueError, RecursionError, MemoryError) as e:
|
|
70
|
+
return [f"cannot be parsed ({type(e).__name__})"]
|
|
71
|
+
problems = []
|
|
72
|
+
|
|
73
|
+
def add(node, msg):
|
|
74
|
+
problems.append(f"line {getattr(node, 'lineno', '?')}: {msg}")
|
|
75
|
+
for node in ast.walk(tree):
|
|
76
|
+
if isinstance(node, ast.Name):
|
|
77
|
+
if node.id.startswith("__"):
|
|
78
|
+
add(node, f"the name {node.id} is not allowed")
|
|
79
|
+
elif node.id in _BANNED_NAMES:
|
|
80
|
+
add(node, f"the name {node.id} is not allowed")
|
|
81
|
+
elif isinstance(node, ast.Attribute):
|
|
82
|
+
if _bad_attr(node.attr):
|
|
83
|
+
add(node, f"the attribute .{node.attr} is not allowed")
|
|
84
|
+
elif isinstance(node, (ast.FunctionDef, ast.ClassDef)):
|
|
85
|
+
if node.name.startswith("__") and (isinstance(node, ast.ClassDef) or node.name not in _DUNDER_DEFS):
|
|
86
|
+
add(node, f"defining {node.name} is not allowed")
|
|
87
|
+
elif isinstance(node, (ast.Import, ast.ImportFrom)):
|
|
88
|
+
if isinstance(node, ast.ImportFrom) and (node.level or not node.module):
|
|
89
|
+
add(node, "relative imports are not allowed")
|
|
90
|
+
continue
|
|
91
|
+
mods = [a.name for a in node.names] if isinstance(node, ast.Import) else [node.module]
|
|
92
|
+
for m in mods:
|
|
93
|
+
if m not in ALLOWED_MODULES:
|
|
94
|
+
add(node, f"import of {m} is not allowed (allowed: {', '.join(sorted(ALLOWED_MODULES))})")
|
|
95
|
+
for a in node.names:
|
|
96
|
+
if a.asname and a.asname.startswith("__"):
|
|
97
|
+
add(node, f"importing as {a.asname} is not allowed")
|
|
98
|
+
if isinstance(node, ast.ImportFrom) and node.module in ALLOWED_MODULES and a.name != "*":
|
|
99
|
+
names = ALLOWED_MODULES[node.module]
|
|
100
|
+
if (names is not None and a.name not in names) or a.name.startswith("_"):
|
|
101
|
+
add(node, f"{a.name} is not available from {node.module}")
|
|
102
|
+
elif isinstance(node, ast.ExceptHandler):
|
|
103
|
+
if node.name and node.name.startswith("__"):
|
|
104
|
+
add(node, f"the name {node.name} is not allowed")
|
|
105
|
+
elif isinstance(node, (ast.Await, ast.AsyncFunctionDef, ast.AsyncFor, ast.AsyncWith)):
|
|
106
|
+
add(node, "async code is not allowed")
|
|
107
|
+
elif hasattr(ast, "MatchClass") and isinstance(node, ast.MatchClass): # `case C(__class__=x)` reads an attribute by name
|
|
108
|
+
for attr in node.kwd_attrs:
|
|
109
|
+
if _bad_attr(attr):
|
|
110
|
+
add(node, f"the attribute .{attr} is not allowed")
|
|
111
|
+
return problems[:20]
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _proxies():
|
|
115
|
+
out = {}
|
|
116
|
+
for name, exports in ALLOWED_MODULES.items():
|
|
117
|
+
mod = __import__(name)
|
|
118
|
+
names = [n for n in dir(mod) if not n.startswith("_")] if exports is None else [n for n in exports if hasattr(mod, n)]
|
|
119
|
+
ns = types.SimpleNamespace(**{n: getattr(mod, n) for n in names})
|
|
120
|
+
ns.__all__ = list(names)
|
|
121
|
+
out[name] = ns
|
|
122
|
+
return out
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _restricted_builtins(proxies):
|
|
126
|
+
safe = {n: getattr(builtins, n) for n in _BUILTIN_NAMES if hasattr(builtins, n)}
|
|
127
|
+
|
|
128
|
+
def guarded_import(name, globals=None, locals=None, fromlist=(), level=0):
|
|
129
|
+
if level != 0 or name not in proxies:
|
|
130
|
+
raise ImportError(f"import of {name} is not allowed in a counted solver")
|
|
131
|
+
return proxies[name]
|
|
132
|
+
safe["__import__"] = guarded_import
|
|
133
|
+
safe["__build_class__"] = builtins.__build_class__
|
|
134
|
+
return safe
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _prepare(path):
|
|
138
|
+
"""(code, namespace) for a solver file that passed check_source: compiled, with the restricted builtins, NOT yet executed (the caller executes it under the counter).
|
|
139
|
+
Raises on anything else."""
|
|
140
|
+
with open(path, "rb") as f:
|
|
141
|
+
text = f.read().decode("utf-8")
|
|
142
|
+
problems = check_source(text)
|
|
143
|
+
if problems:
|
|
144
|
+
raise ValueError("not allowed in a counted solver: " + "; ".join(problems[:5]))
|
|
145
|
+
return compile(text, path, "exec"), {"__builtins__": _restricted_builtins(_proxies()), "__name__": "svp_counted_solver"}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class _StepLimit(BaseException):
|
|
149
|
+
"""Raised from the counting callback when the budget is spent. A BaseException, so a solver's `except Exception` cannot swallow it."""
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _emit(obj):
|
|
153
|
+
sys.__stdout__.write("\n" + json.dumps(obj) + "\n")
|
|
154
|
+
sys.__stdout__.flush()
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def main():
|
|
158
|
+
if len(sys.argv) != 2:
|
|
159
|
+
_emit({"status": "error", "steps": 0, "err": "usage: count_runner.py SOLVER"})
|
|
160
|
+
return
|
|
161
|
+
try:
|
|
162
|
+
payload = json.loads(sys.stdin.read())
|
|
163
|
+
args, limit = payload["args"], int(payload["limit"])
|
|
164
|
+
except (ValueError, KeyError, TypeError) as e:
|
|
165
|
+
_emit({"status": "error", "steps": 0, "err": f"bad payload: {type(e).__name__}"})
|
|
166
|
+
return
|
|
167
|
+
if sys.version_info < (3, 12):
|
|
168
|
+
_emit({"status": "error", "steps": 0, "err": "step counting needs Python 3.12 or newer (sys.monitoring)"})
|
|
169
|
+
return
|
|
170
|
+
# running `python count_runner.py` puts THIS directory (the judge's) on sys.path: a solver could then `import instances` or `import families` and read the
|
|
171
|
+
# planted answers. Take it off before the solver loads. (This is not a sandbox: an unsandboxed solver can still open files. See counted.py.)
|
|
172
|
+
here = os.path.dirname(os.path.abspath(__file__))
|
|
173
|
+
sys.path[:] = [p for p in sys.path if p and os.path.abspath(p) != here]
|
|
174
|
+
# a solver that could reach sys.monitoring could switch its own counting off (set_events(3, 0)) and a re-run would reproduce the same meaningless count.
|
|
175
|
+
# Keep our reference and take the public ones away BEFORE the solver loads. This is a second line of defence: the restricted subset already gives the solver no way to
|
|
176
|
+
# name sys, a frame, gc or ctypes. The end of the run also checks that counting was never reconfigured.
|
|
177
|
+
mon = sys.monitoring
|
|
178
|
+
try:
|
|
179
|
+
delattr(sys, "monitoring")
|
|
180
|
+
except (AttributeError, TypeError):
|
|
181
|
+
pass
|
|
182
|
+
sys.modules.pop("sys.monitoring", None)
|
|
183
|
+
if sys.platform.startswith("linux"): # a solver that allocates without end must fail the same way instead of taking the host down. Linux only: on macOS a process's address space
|
|
184
|
+
try: # (shared cache included) can already exceed the limit, so setting it there would make EVERY allocation fail, not just the solver's
|
|
185
|
+
import resource
|
|
186
|
+
resource.setrlimit(resource.RLIMIT_AS, (2 << 30, 2 << 30))
|
|
187
|
+
except (ImportError, ValueError, OSError):
|
|
188
|
+
pass
|
|
189
|
+
try: # validated and compiled here, uncounted; the module body and solve() are executed and counted below
|
|
190
|
+
code, namespace = _prepare(sys.argv[1])
|
|
191
|
+
except BaseException as e:
|
|
192
|
+
_emit({"status": "error", "steps": 0, "err": f"could not load the solver: {type(e).__name__}: {str(e)[:200]}"})
|
|
193
|
+
return
|
|
194
|
+
count = [0]
|
|
195
|
+
tripped = [False]
|
|
196
|
+
|
|
197
|
+
def on_instruction(code, offset):
|
|
198
|
+
if tripped[0]:
|
|
199
|
+
return # the budget is already spent: let the cleanup code below run, uncounted
|
|
200
|
+
n = count[0] + 1
|
|
201
|
+
count[0] = n
|
|
202
|
+
if n > limit:
|
|
203
|
+
tripped[0] = True # raise ONCE. Raising on every later instruction would break the except/finally handlers too
|
|
204
|
+
raise _StepLimit()
|
|
205
|
+
|
|
206
|
+
status, ans, err = "ok", None, ""
|
|
207
|
+
real_stdout = sys.stdout
|
|
208
|
+
sys.stdout = sys.stderr # a solver's print() must not corrupt the answer channel
|
|
209
|
+
gc.collect()
|
|
210
|
+
gc.disable()
|
|
211
|
+
mon.use_tool_id(TOOL_ID, "svp-count")
|
|
212
|
+
mon.register_callback(TOOL_ID, mon.events.INSTRUCTION, on_instruction)
|
|
213
|
+
try:
|
|
214
|
+
mon.set_events(TOOL_ID, mon.events.INSTRUCTION)
|
|
215
|
+
exec(code, namespace)
|
|
216
|
+
ans = namespace["solve"](*args)
|
|
217
|
+
except _StepLimit:
|
|
218
|
+
status = "limit"
|
|
219
|
+
except BaseException as e:
|
|
220
|
+
status, err = "error", f"{type(e).__name__}: {str(e)[:200]}"
|
|
221
|
+
finally:
|
|
222
|
+
try:
|
|
223
|
+
intact = mon.get_events(TOOL_ID) == mon.events.INSTRUCTION
|
|
224
|
+
except ValueError: # the solver freed the tool id
|
|
225
|
+
intact = False
|
|
226
|
+
try:
|
|
227
|
+
mon.set_events(TOOL_ID, mon.events.NO_EVENTS)
|
|
228
|
+
mon.register_callback(TOOL_ID, mon.events.INSTRUCTION, None)
|
|
229
|
+
mon.free_tool_id(TOOL_ID)
|
|
230
|
+
except ValueError:
|
|
231
|
+
intact = False
|
|
232
|
+
gc.enable()
|
|
233
|
+
sys.stdout = real_stdout
|
|
234
|
+
steps = count[0]
|
|
235
|
+
if not intact and not tripped[0]:
|
|
236
|
+
status, ans, err = "error", None, "the solver reconfigured the instruction counter: its count means nothing"
|
|
237
|
+
if tripped[0]: # a solver with a bare `except:` could swallow the limit and finish: it still spent more than its budget
|
|
238
|
+
status, ans, err = "limit", None, ""
|
|
239
|
+
if status == "ok":
|
|
240
|
+
try:
|
|
241
|
+
json.dumps(ans)
|
|
242
|
+
except (TypeError, ValueError):
|
|
243
|
+
status, err, ans = "error", "the answer is not JSON-serialisable (an int, a list of ints or None is expected)", None
|
|
244
|
+
_emit({"status": status, "steps": steps, "ans": ans if status == "ok" else None, "err": err})
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
if __name__ == "__main__":
|
|
248
|
+
main()
|