-
Notifications
You must be signed in to change notification settings - Fork 2
/
exploit.py
201 lines (188 loc) · 7.05 KB
/
exploit.py
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import os
import wandb
from omegaconf import OmegaConf
from tqdm import tqdm
import logging
import subprocess
import openai
import pandas as pd
import hydra
import re
from zero_hero.core import (
gpt_call,
wrap_system_message,
wrap_user_message,
ZEROHERO_ROOT_DIR,
RewardNode,
SkillDatabase,
TaskDatabase,
)
from evolution.utils.extract_task_code import file_to_string
class Divider:
def __init__(self, model="gpt-3.5-turbo-0125", root_dir=None) -> None:
self.model = model
self.root_dir = root_dir if root_dir is not None else ZEROHERO_ROOT_DIR
self.prompt_dir = f"{self.root_dir}/evolution/utils/prompts"
self.initial_sys = file_to_string(f"{self.prompt_dir}/skill/initial_sys.txt")
self.initial_user = file_to_string(f"{self.prompt_dir}/skill/initial_user.txt")
def run(self, mission: str, skill_database: SkillDatabase):
messages = [
wrap_system_message(
self.initial_sys.format(skills=skill_database.render())
),
wrap_user_message(self.initial_user.format(task=mission)),
]
resp = gpt_call(messages=messages, model=self.model, n_samples=1, temperature=0)
subtasks_proposal = resp[0]["message"]["content"]
logging.info(subtasks_proposal)
subtasks_with_method = re.findall(r"\(\d+\)\.\s(.*)", subtasks_proposal)
subtasks, methods, variants = [], [], []
for st in subtasks_with_method:
match = re.search(r"<<<(\w+)>>>*(.*)", st)
method, subtask = (
match.group(1).lower(),
match.group(2).lstrip("Task").lstrip(":").strip(),
)
if "reuse" in method:
index = re.search(r"skill.*?(\d+).*", subtask).group(1)
variant = skill_database.get_variant(index=int(index) - 1)
else:
variant = ""
subtasks.append(subtask)
methods.append(method) # introduce, reuse
variants.append(variant)
df = pd.DataFrame(
{
"subtask": subtasks,
"method": methods,
"variants": variants,
"status": "todo",
}
)
return df
class Conquerer:
def __init__(self, subtasks, **learn_kwargs) -> None:
self.df = subtasks
self.env_name = "franka_table"
seed = 99
self.tdb = TaskDatabase(
env_name=self.env_name,
env_idx = f"E{seed:02d}"
)
self.learn_kwargs = learn_kwargs
self.reset()
def reset(self):
self.chain = {}
self.succ = False
self.subtask = None
self.homework = None
def run(self):
extended_command = [f"{k}={v}" for k, v in self.learn_kwargs.items()]
precedents = self.chain
df = self.df
subtasks = df["subtask"].values
homework = [""] * len(subtasks)
for i, row in tqdm(df.iterrows()):
subtask = row.subtask
method = row["method"]
logging.info(f"[{method}] On subtask: {subtask} ...")
if method == "introduce":
# learn and update
# precedents =','.join(row.precedents.split(',').strip())
command = [
# "python3",
"/data/xufeng/miniconda3/envs/zerohero/bin/python",
f"{ZEROHERO_ROOT_DIR}/learn.py",
f'task="{subtask}"',
"seed=99",
]
if len(precedents) > 0:
command.append(f'precedents="{precedents}"')
if len(extended_command) > 0:
command.extend(extended_command)
logging.info(f"Command to run:\n {command}")
sp = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out_str, _ = sp.communicate()
self.tdb.load()
subtask_homework = self.tdb.df.get_attr(attr="status", command=subtask)[0]
homework[i] = subtask_homework
if subtask_homework == "failed":
logging.info(f"Failed to learn! Break at {subtask}.")
self.succ = False
self.subtasks = subtasks
self.homework = homework
return
elif subtask_homework == "compromised":
logging.warning(f"Compromised at subtask: {subtask}")
else:
pass
variants = {self.tdb.df.get_attr(attr="variants", task=command): subtask}
elif method == "reuse":
variants = row["variants"]
else:
raise NotImplementedError
precedents = {**precedents, **variants}
self.last_reward_node_idx = precedents.pop(-1)
self.chain = precedents
self.succ = True
self.subtasks = subtasks
self.homework = homework
return
def play(self):
precedents = self.chain
subtasks, homework, succ = self.subtasks, self.homework, self.succ
idx = self.last_reward_node_idx
assert homework is not None and subtasks is not None
rnode = RewardNode(
idx=idx,
precedents=precedents,
**self.learn_kwargs,
).init()
playbacks = rnode.play(suffix="_all")
logging.info(
f"Playback for running reward node {idx} with precedents {precedents} is:\n{playbacks}"
)
v_idx, v_path = playbacks["reward_idx"], playbacks["video_path"]
wandb_video = {f"{v_idx}_all": wandb.Video(v_path, fps=30, format="mp4")}
wandb_info_table = {
"subtasks": wandb.Table(
columns=[i for i in range(len(subtasks))], data=[subtasks, homework]
),
"precedents": wandb.Table(
columns=[i for i in range(len(precedents))], data=precedents
),
}
log = {
"success": succ,
**wandb_info_table,
**wandb_video,
}
return log
@hydra.main(config_path="cfg", config_name="config", version_base="1.1")
def main(cfg):
openai.api_key = os.getenv("OPENAI_API_KEY")
my_cfg = OmegaConf.to_container(cfg, resolve=True, throw_on_missing=True)
logging.info(cfg)
env_name = cfg.env.env_name.lower()
env_idx = f"E{cfg.seed:02d}"
sdb = SkillDatabase(env_name=env_name, env_idx=env_idx)
# sdb.absorb(tdb)
divider = Divider(model=cfg.exploit.model)
mission = cfg.exploit.mission
subtasks_df = divider.run(mission, skill_database=sdb)
assert mission is not None and len(mission) > 0
conquerer = Conquerer(subtasks=subtasks_df, **cfg.exploit.learn)
conquerer.run()
play_log = conquerer.play()
if cfg.exploit.use_wandb:
wandbrun = wandb.init(
project=cfg.exploit.wandb_project,
config=my_cfg,
)
wandbrun.log(play_log)
if __name__ == "__main__":
main()