File size: 5,914 Bytes
6aeb377
 
 
 
 
 
 
 
 
 
 
1ce72ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aeb377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ce72ab
 
 
6aeb377
 
 
 
 
1ce72ab
 
 
 
 
 
 
 
 
 
 
 
 
 
6aeb377
 
 
 
 
 
 
 
 
 
 
 
 
1ce72ab
6aeb377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ce72ab
6aeb377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ce72ab
 
 
6aeb377
 
 
 
 
 
1ce72ab
6aeb377
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from __future__ import annotations

import argparse
import json
import shutil
import tempfile
from pathlib import Path

from huggingface_hub import HfApi


METADATA_DEFAULTS = {
    "agent_id": "",
    "arena_version": "",
    "dataset_version": "",
    "generator_version": "",
    "label_source": "",
    "phase": "",
    "prompt_version": "",
    "seed": 0,
    "split": "",
    "policy_reward": 0.0,
    "solver_joint_actions_explored": 0,
    "solver_optimal_count": 0,
    "solver_reward": 0.0,
    "generator_mode": "",
    "targeted_skill": "",
}


DATASET_CARD = """---
license: apache-2.0
task_categories:
- text-generation
language:
- en
configs:
- config_name: default
  data_files:
  - split: train_broadcast
    path: train_broadcast.jsonl
  - split: train_action
    path: train_action_common.jsonl
  - split: train_action_rare
    path: train_action_rare.jsonl
  - split: validation
    path: validation.jsonl
  - split: test
    path: test.jsonl
  - split: overfit
    path: overfit.jsonl
---

# Swarm Arena SFT v2

Solver-filtered warm-start data for the deterministic Swarm Arena 4v4
coordination environment. Each row contains system, user, and assistant messages
plus provenance metadata. Training broadcasts and actions are separate splits so
sampling can preserve a 60/40 phase mixture. Validation and test are never
reweighted.

The simulator, oracle, audit, frozen evaluation, and Prime-RL configs live in
<https://github.com/ChinmayK0607/blog-rl/tree/exp/swarm-arena-4b/experiments/swarm_arena>.

Optional provenance fields use typed zero/empty-string defaults so every JSONL
split has one stable Arrow schema. They do not affect prompts or targets.

Dataset content SHA-256:
`edad09bb301748621a0fab73ebf3de60d60abfd9f56c9afcc6ca02ffe12f3a80`.
"""


def normalize_row(row: dict) -> dict:
    row = dict(row)
    row["metadata"] = {**METADATA_DEFAULTS, **row["metadata"]}
    return row


def write_normalized_split(source: Path, destination: Path) -> None:
    with source.open(encoding="utf-8") as input_handle, destination.open(
        "w", encoding="utf-8"
    ) as output_handle:
        for line in input_handle:
            output_handle.write(json.dumps(normalize_row(json.loads(line)), sort_keys=True) + "\n")


def split_train(
    source: Path,
    broadcast_path: Path,
    common_action_path: Path,
    rare_action_path: Path,
) -> None:
    rare_types = {"WAIT", "SCAN", "TRANSFER"}
    with source.open(encoding="utf-8") as input_handle, broadcast_path.open(
        "w", encoding="utf-8"
    ) as broadcast_handle, common_action_path.open(
        "w", encoding="utf-8"
    ) as common_handle, rare_action_path.open("w", encoding="utf-8") as rare_handle:
        for line in input_handle:
            row = normalize_row(json.loads(line))
            if row["metadata"]["phase"] == "BROADCAST":
                destination = broadcast_handle
            else:
                user = json.loads(row["messages"][-2]["content"])
                action_id = json.loads(row["messages"][-1]["content"])["action_id"]
                action = next(item for item in user["legal_actions"] if item["id"] == action_id)
                destination = rare_handle if action["type"] in rare_types else common_handle
            destination.write(json.dumps(row, sort_keys=True) + "\n")


def make_overfit(source: Path, destination: Path, count: int = 256) -> None:
    rows = [json.loads(line) for line in source.read_text(encoding="utf-8").splitlines()]
    broadcasts = [row for row in rows if row["metadata"]["phase"] == "BROADCAST"][:154]
    actions = [row for row in rows if row["metadata"]["phase"] == "ACT"]
    rare_types = {"WAIT", "SCAN", "TRANSFER"}

    def is_rare(row: dict) -> bool:
        user = json.loads(row["messages"][-2]["content"])
        action_id = json.loads(row["messages"][-1]["content"])["action_id"]
        action = next(item for item in user["legal_actions"] if item["id"] == action_id)
        return action["type"] in rare_types

    rare = [row for row in actions if is_rare(row)][:24]
    common = [row for row in actions if not is_rare(row)][: count - 154 - len(rare)]
    with destination.open("w", encoding="utf-8") as handle:
        for row in broadcasts + rare + common:
            handle.write(json.dumps(normalize_row(row), sort_keys=True) + "\n")


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("source", type=Path)
    parser.add_argument("--repo-id", default="CK0607/swarm-arena-sft-v2")
    parser.add_argument("--experiment-root", type=Path)
    args = parser.parse_args()

    api = HfApi()
    api.create_repo(args.repo_id, repo_type="dataset", exist_ok=True)
    with tempfile.TemporaryDirectory(prefix="swarm-arena-hf-") as temporary:
        staging = Path(temporary)
        split_train(
            args.source / "train.jsonl",
            staging / "train_broadcast.jsonl",
            staging / "train_action_common.jsonl",
            staging / "train_action_rare.jsonl",
        )
        make_overfit(args.source / "train.jsonl", staging / "overfit.jsonl")
        for filename in ("validation.jsonl", "test.jsonl"):
            write_normalized_split(args.source / filename, staging / filename)
        for filename in ("manifest.json", "audit.json"):
            shutil.copy2(args.source / filename, staging / filename)
        (staging / "README.md").write_text(DATASET_CARD, encoding="utf-8")
        if args.experiment_root:
            shutil.copytree(
                args.experiment_root,
                staging / "code",
                ignore=shutil.ignore_patterns("__pycache__", "*.pyc", "data", ".venv", "uv.lock"),
            )
        api.upload_folder(
            repo_id=args.repo_id,
            repo_type="dataset",
            folder_path=staging,
            commit_message="Publish audited Swarm Arena SFT v2",
        )


if __name__ == "__main__":
    main()