Add the task screener: measure base vs trained instead of guessing
Browse files- screen_tasks.py +114 -0
screen_tasks.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020-2026 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
"""
|
| 16 |
+
Pick the before/after task by measuring it, not by guessing.
|
| 17 |
+
|
| 18 |
+
Runs opencode on the same candidate tasks with two models in turn, the base and the trained one,
|
| 19 |
+
scores each rollout with the task's held-out tests, and prints a table. The task to record is one
|
| 20 |
+
where the gap is clean: base fails, trained passes.
|
| 21 |
+
|
| 22 |
+
Nothing from the training path is involved: no trainer, no GRPO, no capture proxy. Just the agent,
|
| 23 |
+
the task, and the held-out tests. `capture_launcher.py` handles the trace separately.
|
| 24 |
+
|
| 25 |
+
idx seen? base trained gap
|
| 26 |
+
11 yes 1.00 1.00 no (trivial, both solve it)
|
| 27 |
+
34 no 0.00 1.00 CLEAN <- record this one
|
| 28 |
+
39 no 0.42 0.58 weak
|
| 29 |
+
|
| 30 |
+
`build_dataset(n_prompts, seed)` is deterministic, so indices are reproducible and `< 32` means the
|
| 31 |
+
training run saw it. A task the run never saw is the stronger story, but the black-box run trained on
|
| 32 |
+
32 prompts with no held-out eval, so an unseen task may simply not improve. That is what this
|
| 33 |
+
measures instead of assuming.
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
import argparse
|
| 37 |
+
import json
|
| 38 |
+
import statistics
|
| 39 |
+
|
| 40 |
+
from opencode_hf_sandbox import build_dataset, build_factory, opencode_agent_turns
|
| 41 |
+
from trl.experimental.async_grpo.openenv_harness import _messages_from_trace
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def parse_args():
|
| 45 |
+
p = argparse.ArgumentParser(description="Measure base vs trained on candidate tasks, one rollout each.")
|
| 46 |
+
p.add_argument("--base-model", default="Qwen/Qwen3-8B")
|
| 47 |
+
p.add_argument("--trained-model", default="sergiopaniego/Qwen3-8B-opencode-deepcoder-grpo")
|
| 48 |
+
p.add_argument("--sandbox-vllm-url", required=True)
|
| 49 |
+
p.add_argument("--serving", required=True, choices=("base", "trained"),
|
| 50 |
+
help="Which model the url in front of this process is currently serving.")
|
| 51 |
+
p.add_argument("--task-indices", default="11,34,39,25,33",
|
| 52 |
+
help="Comma separated indices into the built dataset.")
|
| 53 |
+
p.add_argument("--n-prompts", type=int, default=64)
|
| 54 |
+
p.add_argument("--seed", type=int, default=0)
|
| 55 |
+
p.add_argument("--rollouts", type=int, default=2, help="Rollouts per task, to see through sampling noise.")
|
| 56 |
+
p.add_argument("--sandbox-image", default="ghcr.io/huggingface/openenv-opencode-sandbox:latest")
|
| 57 |
+
p.add_argument("--sandbox-flavor", default="cpu-basic")
|
| 58 |
+
p.add_argument("--timeout-s", type=float, default=900.0)
|
| 59 |
+
p.add_argument("--out", default="screen_results.json")
|
| 60 |
+
return p.parse_args()
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def score_one(factory, prompt, seed, timeout_s):
|
| 64 |
+
"""One rollout: let the agent run its loop, then score the workspace with the held-out tests."""
|
| 65 |
+
session = factory.create(prompt, seed=seed, episode_id=None)
|
| 66 |
+
try:
|
| 67 |
+
try:
|
| 68 |
+
session.wait_for_completion(timeout_s=timeout_s)
|
| 69 |
+
except TimeoutError:
|
| 70 |
+
pass
|
| 71 |
+
trace = session.fetch_proxy_trace()
|
| 72 |
+
entries = opencode_agent_turns(trace)
|
| 73 |
+
verify = session.verify(_messages_from_trace(entries))
|
| 74 |
+
return (float(verify.env_reward) if verify.env_reward is not None else None, len(entries))
|
| 75 |
+
finally:
|
| 76 |
+
try:
|
| 77 |
+
session.close()
|
| 78 |
+
except Exception:
|
| 79 |
+
pass
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def main():
|
| 83 |
+
args = parse_args()
|
| 84 |
+
model = args.base_model if args.serving == "base" else args.trained_model
|
| 85 |
+
rows, tests_by_id = build_dataset(n_prompts=args.n_prompts, seed=args.seed)
|
| 86 |
+
factory = build_factory(args.sandbox_vllm_url, model, tests_by_id, args.sandbox_image, args.sandbox_flavor)
|
| 87 |
+
indices = [int(x) for x in args.task_indices.split(",") if x.strip()]
|
| 88 |
+
|
| 89 |
+
out = {"serving": args.serving, "model": model, "tasks": {}}
|
| 90 |
+
for idx in indices:
|
| 91 |
+
if not 0 <= idx < len(rows):
|
| 92 |
+
print(f"[skip] index {idx} outside 0..{len(rows) - 1}", flush=True)
|
| 93 |
+
continue
|
| 94 |
+
prompt = rows[idx]["prompt"]
|
| 95 |
+
scores, turns = [], []
|
| 96 |
+
for r in range(args.rollouts):
|
| 97 |
+
score, n_turns = score_one(factory, prompt, seed=args.seed + r, timeout_s=args.timeout_s)
|
| 98 |
+
scores.append(score)
|
| 99 |
+
turns.append(n_turns)
|
| 100 |
+
print(f"[{args.serving}] idx={idx} rollout={r} score={score} turns={n_turns}", flush=True)
|
| 101 |
+
clean = [s for s in scores if s is not None]
|
| 102 |
+
out["tasks"][str(idx)] = {
|
| 103 |
+
"seen_in_training": idx < 32,
|
| 104 |
+
"scores": scores,
|
| 105 |
+
"mean": statistics.mean(clean) if clean else None,
|
| 106 |
+
"turns": turns,
|
| 107 |
+
}
|
| 108 |
+
print("[results] " + json.dumps(out), flush=True)
|
| 109 |
+
with open(args.out, "w") as fh:
|
| 110 |
+
json.dump(out, fh, indent=2)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
main()
|