forked from Gen-Verse/OpenClaw-RL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_data_source.py
More file actions
134 lines (113 loc) · 4.53 KB
/
Copy pathgui_data_source.py
File metadata and controls
134 lines (113 loc) · 4.53 KB
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
from __future__ import annotations
import copy
import json
import os
from pathlib import Path
import torch
from slime.rollout.data_source import DataSource
from slime.utils.types import Sample
def _pop_first(buffer: list[list[Sample]], num_samples: int) -> list[list[Sample]]:
num_to_pop = min(len(buffer), num_samples)
samples = buffer[:num_to_pop]
del buffer[:num_to_pop]
return samples
class GuiMetaDataSource(DataSource):
"""
Data source for GUI tasks from evaluation_examples meta files.
This bypasses jsonl prompt-data generation and directly loads:
- GUI_TEST_CONFIG_BASE_DIR
- GUI_TRAIN_META_PATH
"""
def __init__(self, args):
self.args = args
self.buffer: list[list[Sample]] = []
self.epoch_id = 0
self.sample_group_index = 0
self.sample_index = 0
self.sample_offset = 0
base_dir = Path(
os.getenv("GUI_TEST_CONFIG_BASE_DIR", str(Path(__file__).resolve().parent / "evaluation_examples"))
)
meta_path = Path(os.getenv("GUI_TRAIN_META_PATH", str(base_dir / "train_nochrome.json")))
with open(meta_path, "r", encoding="utf-8") as f:
meta = json.load(f)
tasks: list[dict] = []
for domain, example_ids in meta.items():
for example_id in example_ids:
cfg_path = base_dir / "examples" / str(domain) / f"{example_id}.json"
if not cfg_path.exists():
continue
with open(cfg_path, "r", encoding="utf-8") as cf:
task_cfg = json.load(cf)
tasks.append(
{
"domain": str(domain),
"example_id": str(example_id),
"instruction": task_cfg.get("instruction", ""),
"task_config": task_cfg,
}
)
if not tasks:
raise RuntimeError(f"No tasks loaded from {meta_path}")
self.tasks = tasks
def _make_prompt_samples(self, num_samples: int) -> list[Sample]:
out: list[Sample] = []
for _ in range(num_samples):
task = self.tasks[self.sample_offset % len(self.tasks)]
self.sample_offset += 1
if self.sample_offset % len(self.tasks) == 0:
self.epoch_id += 1
sample = Sample(
prompt=task["instruction"],
label="",
metadata={
"domain": task["domain"],
"example_id": task["example_id"],
"instruction": task["instruction"],
"task_config": task["task_config"],
},
)
out.append(sample)
return out
def get_samples(self, num_samples: int) -> list[list[Sample]]:
samples = _pop_first(self.buffer, num_samples)
num_samples -= len(samples)
if num_samples <= 0:
return samples
prompt_samples = self._make_prompt_samples(num_samples)
groups: list[list[Sample]] = []
for prompt_sample in prompt_samples:
group = []
for _ in range(self.args.n_samples_per_prompt):
s = copy.deepcopy(prompt_sample)
s.group_index = self.sample_group_index
s.index = self.sample_index
self.sample_index += 1
group.append(s)
self.sample_group_index += 1
groups.append(group)
return samples + groups
def add_samples(self, samples: list[list[Sample]]):
if samples:
self.buffer.extend(samples)
def save(self, rollout_id):
state = {
"epoch_id": self.epoch_id,
"sample_group_index": self.sample_group_index,
"sample_index": self.sample_index,
"sample_offset": self.sample_offset,
}
path = os.path.join(self.args.save, f"rollout/gui_meta_state_dict_{rollout_id}.pt")
os.makedirs(os.path.dirname(path), exist_ok=True)
torch.save(state, path)
def load(self, rollout_id=None):
if self.args.load is None:
return
path = os.path.join(self.args.load, f"rollout/gui_meta_state_dict_{rollout_id}.pt")
if not os.path.exists(path):
return
state = torch.load(path)
self.epoch_id = state.get("epoch_id", 0)
self.sample_group_index = state.get("sample_group_index", 0)
self.sample_index = state.get("sample_index", 0)
self.sample_offset = state.get("sample_offset", 0)