-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
executable file
·354 lines (309 loc) · 14.7 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import copy
import glob
import os
import time
from collections import deque
import gym
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from a2c_ppo_acktr import algo, utils
from a2c_ppo_acktr.arguments import get_args
from a2c_ppo_acktr.envs import make_vec_envs, make_vec_envs_eval, make_vec_envs_fseval, get_num_test
from a2c_ppo_acktr.model import Policy
from a2c_ppo_acktr.storage import RolloutStorage
from evaluation import evaluate, evaluate_lm, evaluate_fs_lm
from utils import setup_roberta
from torch import multiprocessing as mp
class Normalizer:
_STATS_FNAME = "env_stats.pickle"
# https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
def __init__(self, in_size, num_process, device='cpu', dtype=torch.float):
device='cpu'
self.mean = torch.zeros((num_process, in_size), device=device, dtype=dtype)
self.std = torch.ones((num_process, in_size), device=device, dtype=dtype)
self.num_process = num_process
self.eps = 1e-12 if dtype == torch.double else 1e-5
self.device = device
self.count = self.eps + torch.zeros((num_process, in_size), device=device, dtype=dtype)
def update_stats(self, batch_data, batch_indices):
if isinstance(batch_data, np.ndarray):
batch_data = torch.from_numpy(batch_data).float().to(data.device)
batch_data = batch_data.to('cpu')
if isinstance(batch_indices, np.ndarray):
batch_indices = torch.from_numpy(batch_indices).to('cpu')
for i in range(self.num_process):
index = (batch_indices == i).nonzero()
data = torch.gather(batch_data, dim=0, index=index)
if data.shape[0] > 1:
batch_mean = data.mean(0, keepdim=True)
batch_var = data.var(0, keepdim=True)
batch_count = data.shape[0]
self.update_from_moments(batch_mean, batch_var, batch_count, i)
def update_from_moments(self, batch_mean, batch_var, batch_count, index):
delta = batch_mean - self.mean[[index]]
tot_count = self.count[[index]] + batch_count
new_mean = self.mean[[index]] + delta * batch_count / tot_count
m_a = torch.square(self.std[[index]]) * (self.count[[index]])
m_b = batch_var * (batch_count)
M2 = m_a + m_b + torch.square(delta) * self.count[[index]] * batch_count / (self.count[[index]] + batch_count)
new_var = M2 / (self.count[[index]] + batch_count)
new_count = batch_count + self.count[[index]]
self.mean[[index]] = new_mean
self.std[[index]] = torch.sqrt(new_var)
self.count[[index]] = new_count
def normalize(self, val, index):
if isinstance(val, np.ndarray):
val = torch.from_numpy(val).to(self.device)
std = torch.clamp(self.std, self.eps)
mean = self.mean[index]
std = std[index]
return (val - mean.to(val.device)) / std.to(val.device)
def denormalize(self, val):
if isinstance(val, np.ndarray):
val = torch.from_numpy(val).to(self.device)
std = torch.clamp(self.std, self.eps)
return std * val.to(val.device) + self.mean.to(val.device)
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
def main():
ctx = mp.get_context('spawn')
args = get_args()
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
if args.cuda and torch.cuda.is_available() and args.cuda_deterministic:
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
log_dir = os.path.expanduser(args.log_dir)
eval_log_dir = log_dir + "_eval"
utils.cleanup_log_dir(log_dir)
utils.cleanup_log_dir(eval_log_dir)
torch.set_num_threads(1)
device = torch.device("cuda:0" if args.cuda else "cpu")
# Construct params
params = {
'conditioned_on_correct_classes': True,
'api_num_log_prob': args.api_num_log_prob,
'approx': args.approx,
'bs': 1
}
params['model'] = args.models
params['dataset'] = args.datasets
params['seed'] = args.seed
params['num_shots'] = args.num_shots
params['expr_name'] = ""
params['subsample_test_set'] = args.subsample_test_set
params['env_name'] = args.env_name
params['verbalizer'] = args.verbalizer
params['rew_type'] = args.rew_type
params['example_pool_size'] = args.example_pool_size
params['use_knn'] = args.use_knn
params['sub_sample'] = args.sub_sample
params['num_actors'] = args.num_actors
params['entropy_coef'] = args.env_entropy_coef
params['random_init'] = args.random_init
if args.models == 'gpt2-xl':
obs_size = 1600
elif args.models == 'gpt2-large':
obs_size = 1280
elif args.models == 'gpt2-medium':
obs_size = 1024
elif args.models == 'roberta-large':
obs_size = 1024
elif args.models == 't5-large':
obs_size = 1024
elif args.models == 't5-11b':
obs_size = 1024
elif args.models == 't5-3b':
obs_size = 1024
else:
assert False
print('Experiment params ', params, flush=True)
print('Experiment arguments ', args, flush=True)
envs = make_vec_envs(params['seed'], params, args.max_steps, args.num_processes, args.gamma, obs_size, 0)
envs_fseval = make_vec_envs_fseval(params['seed'], params, args.max_steps, 16, args.gamma, obs_size, 0)
num_test_samples = get_num_test(params['seed'], params, args.max_steps, args.num_processes, args.gamma, obs_size, 0, 0%torch.cuda.device_count())
eval_envs = []
for i in range(params['num_actors']):
eval_env, num_test_samples = make_vec_envs_eval(params['seed'], params, args.max_steps, args.num_processes, args.gamma, obs_size, False, i, i%torch.cuda.device_count())
eval_envs.append(eval_env)
num_blocks = int(envs.observation_space.shape[0]/obs_size)
actor_critic = Policy(
envs.observation_space.shape,
envs.action_space,
args.use_attention,
'cuda',
num_blocks,
base_kwargs={'recurrent': args.recurrent_policy,
'hidden_size': 1024})
actor_critic.to(device)
hidden_dim = 256
if args.algo == 'ppo':
agent = algo.PPO(
actor_critic,
args.clip_param,
args.ppo_epoch,
args.num_mini_batch,
args.value_loss_coef,
args.entropy_coef,
lr=args.lr,
eps=args.eps,
max_grad_norm=args.max_grad_norm)
else:
assert False
rollouts = RolloutStorage(args.num_steps, args.num_processes,
envs.observation_space.shape, envs.action_space,
actor_critic.recurrent_hidden_state_size)
obs = envs.reset()
rollouts.obs[0].copy_(obs)
rollouts.to(device)
episode_rewards = deque(maxlen=10)
start = time.time()
num_updates = int(
args.num_env_steps) // args.num_steps // args.num_processes
all_obs = []
all_rews = []
all_indexs = []
if args.normalize_rew:
rew_normalizer = Normalizer(1, 16 * len(params['label_dict'].keys()))
else:
rew_normalizer = None
for j in range(num_updates):
if args.use_linear_lr_decay:
# decrease learning rate linearly
utils.update_linear_schedule(
agent.optimizer, j, num_updates,
agent.optimizer.lr if args.algo == "acktr" else args.lr)
for step in range(args.num_steps):
# Sample actions
with torch.no_grad():
value, action, action_log_prob, recurrent_hidden_states = actor_critic.act(
rollouts.obs[step], rollouts.recurrent_hidden_states[step],
rollouts.masks[step])
# Observation reward and next obs
subset_idxs = envs.venv.envs[0].subset_idxs
all_indexs.append(copy.deepcopy(subset_idxs))
obs, reward, done, infos = envs.step(action)
if args.normalize_obs:
actor_critic.base.normalizer.update_stats(obs)
all_rews.append(copy.deepcopy(reward))
all_obs.append(copy.deepcopy(obs))
for info in infos:
if 'episode' in info.keys():
episode_rewards.append(info['episode']['r'])
if done[0]:
episode_rewards.append(info['episode_r'])
# If done then clean the history of observations.
masks = torch.FloatTensor(
[[0.0] if done_ else [1.0] for done_ in done])
bad_masks = torch.FloatTensor(
[[0.0] if 'bad_transition' in info.keys() else [1.0]
for info in infos])
rollouts.insert(obs, recurrent_hidden_states, action,
action_log_prob, torch.Tensor(subset_idxs).unsqueeze(-1), value, reward, masks, bad_masks)
if args.normalize_obs:
all_obs = []
if args.normalize_rew:
rew_normalizer.update_stats(torch.cat(all_rews, dim=0), torch.from_numpy(np.concatenate(all_indexs, axis=0)))
if args.normalize_rew:
rollouts.update_rew(rew_normalizer)
if args.normalize_rew:
all_indexs = []
all_rews = []
with torch.no_grad():
next_value = actor_critic.get_value(
rollouts.obs[-1], rollouts.recurrent_hidden_states[-1],
rollouts.masks[-1]).detach()
rollouts.compute_returns(next_value, args.use_gae, args.gamma,
args.gae_lambda, rew_normalizer, args.use_proper_time_limits)
value_loss, action_loss, dist_entropy = agent.update(rollouts)
rollouts.after_update()
# save for every interval-th episode or for the last epoch
if (j % args.save_interval == 0
or j == num_updates - 1) and args.save_dir != "":
save_path = os.path.join(args.save_dir, args.algo)
try:
os.makedirs(save_path)
except OSError:
pass
torch.save([
actor_critic,
getattr(utils.get_vec_normalize(envs), 'obs_rms', None)
], os.path.join(save_path, args.env_name + ".pt"))
if j % args.log_interval == 0 and len(episode_rewards) > 1:
total_num_steps = (j + 1) * args.num_processes * args.num_steps
end = time.time()
print(
"Updates {}, num timesteps {}, FPS {} \n Last {} training episodes: mean/median reward {:.1f}/{:.1f}, min/max reward {:.1f}/{:.1f}"
.format(j, total_num_steps,
int(total_num_steps / (end - start)),
len(episode_rewards), np.mean(episode_rewards),
np.median(episode_rewards), np.min(episode_rewards),
np.max(episode_rewards), dist_entropy, value_loss,
action_loss), flush=True)
if (args.eval_interval is not None and j % args.eval_interval == 0):
# Validation set
evaluate_fs_lm(actor_critic, None, envs_fseval, args.seed,
16, 16, params, args, obs_size)
actor_critic.to('cpu')
results = ctx.Queue()
orig_results = ctx.Queue()
tp = ctx.Queue()
fp = ctx.Queue()
fn = ctx.Queue()
env_queue = ctx.Queue()
evaluate_processes = []
for i in range(params['num_actors']):
eval = ctx.Process(
target=evaluate_lm,
args=(i, actor_critic, None, eval_envs[i], args.seed,
args.num_processes, num_test_samples, orig_results, results, env_queue, params, args, obs_size, tp, fp, fn))
eval.start()
evaluate_processes.append(eval)
for eval in evaluate_processes:
eval.join()
results_list = []
orig_results_list = []
tp_list = []
fp_list = []
fn_list = []
for i in range(results.qsize()):
results_list.append(results.get())
orig_results_list.append(orig_results.get())
tp_list.append(tp.get())
fp_list.append(fp.get())
fn_list.append(fn.get())
print('Evaluation mean reward {:.5f}, original mean reward {:.5f}'.format(sum(results_list)/len(results_list), sum(orig_results_list)/len(orig_results_list)), flush=True)
precision = sum(tp_list) / (sum(tp_list) + sum(fp_list))
recall = sum(tp_list) / (sum(tp_list) + sum(fn_list))
f_score = 2 * precision * recall / (precision + recall)
print('Evaluation mean reward f score {:.5f}'.format(f_score), flush=True)
actor_critic.to('cuda:0')
if not args.load_ckpt and args.env_name != 'lmall':
file_path = 'checkpoints/'+str(args.models)+'_'+str(args.datasets)+'_'+str(args.seed)+'/'
isExist = os.path.exists(file_path)
if not isExist:
os.makedirs(file_path)
current_prompt_embedding_pool = []
add_current_prompt_embedding_pool = []
current_verbalizer_embedding_pool = []
add_current_verbalizer_embedding_pool = []
for eval_env in eval_envs:
current_prompt_embedding_pool.append(eval_env.envs[0].current_prompt_embedding_pool)
add_current_prompt_embedding_pool.append(eval_env.envs[0].add_current_prompt_embedding_pool)
current_verbalizer_embedding_pool.append(eval_env.envs[0].current_verbalizer_embedding_pool)
add_current_verbalizer_embedding_pool.append(eval_env.envs[0].add_current_verbalizer_embedding_pool)
current_prompt_embedding_pool = torch.cat(current_prompt_embedding_pool, dim=0)
add_current_prompt_embedding_pool = torch.cat(add_current_prompt_embedding_pool, dim=0)
current_verbalizer_embedding_pool = torch.cat(current_verbalizer_embedding_pool, dim=0)
add_current_verbalizer_embedding_pool = torch.cat(add_current_verbalizer_embedding_pool, dim=0)
torch.save(current_prompt_embedding_pool, file_path+'current_prompt_embedding_pool.pth')
torch.save(add_current_prompt_embedding_pool, file_path+'add_current_prompt_embedding_pool.pth')
torch.save(current_verbalizer_embedding_pool, file_path+'current_verbalizer_embedding_pool.pth')
torch.save(add_current_verbalizer_embedding_pool, file_path+'add_current_verbalizer_embedding_pool.pth')
if __name__ == "__main__":
torch.multiprocessing.set_start_method('spawn')# good solution !!!!
main()