-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_RL_multi.py
executable file
·203 lines (157 loc) · 7.67 KB
/
train_RL_multi.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
import os
# os.environ["CUDA_VISIBLE_DEVICES"] ="1"
import sys
import torch
from torch.cuda.amp import autocast
from torch.utils.data import DataLoader
from torch.distributions import Categorical
from torch import optim
from tqdm import tqdm
import torch.multiprocessing as mp
import multiprocessing
from torch.nn.parallel import DistributedDataParallel as DDP
import torch.distributed as dist
from socket import socket
from functools import partial
from RL.utils import BertAgentCritic, PPOTrainer, LLMEnv_batch_version
from DocBuilder.Retriever_k_means import cluster_builder, doc_retriever
from DatasetLoader.collate_func import collate
from DatasetLoader.dataset import NQADataset
from DocBuilder.LexMAE import lex_retriever
from DocBuilder.utils import restore_batched_list, generate_mask, tensor_retuen_type
from LM.llama_reader import LLaMa_reader, EncTunedLM
from LM.Knowledge_encoder import KnowEncoder
import yaml
import peft
from time import time
from transformers import AutoTokenizer
import config
token = "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"
def setup(rank, world_size, port):
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = str(port)
# initialize the process group
dist.init_process_group("gloo", rank=rank, world_size=world_size)
def cleanup():
dist.destroy_process_group()
def worker_init(rank, world_size, port, x):
setup(rank, world_size, port)
def training(rank:int, world_size:int, port, total:int, env:LLMEnv_batch_version, agent:BertAgentCritic, memory):
import torch.distributed as dist
print(f"Running on rank {rank}.")
setup(rank, world_size, port)
env.to(rank)
ratio = 1.
agent = agent.to(rank)
agentDDP = DDP(agent, device_ids=[rank], find_unused_parameters=True)
Agent_optim = optim.AdamW([{"params": agentDDP.module.bert.parameters(), "lr": config.train_config.agent_lr* ratio},
{"params": agentDDP.module.value_head.parameters(), "lr": config.train_config.agent_head_lr* ratio, "weight_decay": 0.02},
{"params": agentDDP.module.action_head.parameters(), "lr": config.train_config.agent_head_lr * ratio, "weight_decay": 0.02}], betas=config.train_config.betas)
trainer = PPOTrainer(agentDDP, Agent_optim, update_epochs=max(4//world_size, 1), **config.ppo_config)
reduce = optim.lr_scheduler.PolynomialLR(Agent_optim, total_iters=int(total*1.2), power=1.5)
warmup = optim.lr_scheduler.LinearLR(Agent_optim, 1e-5, 1, total_iters=int(total*0.001))
scheduler = optim.lr_scheduler.SequentialLR(Agent_optim, [warmup, reduce], milestones=[warmup.total_iters])
ma_reward = 0
reward_file = open(f"reward_number_{rank}.log", "a")
print("Start training...")
trajectory = [[] for _ in range(env.batch_size)] # don't do this->[[]]*env_bs
done = [True]*env.batch_size
state = [None]*env.batch_size
save_time = time()
while True:
for i in range(env.batch_size):
if done[i]:
state[i] = env.reset(i) # Shape: string
done[i] = False
scheduler.step()
while not any(done):
with torch.no_grad():
action_logits, state_value = agent(state) # action_logits shape: (B, action_space_size), state_value shape: (B,)
action_logits, state_value = action_logits.cpu(), state_value.cpu()
action_dist = Categorical(logits=action_logits)
action = action_dist.sample() # Shape: (B,)
if torch.rand([1]) < 0.05:
action = torch.randint(env.action_space_size, [env.batch_size])
else:
action = action_dist.sample() # Shape: (B,)
next_state, reward, done, _ = env.step(action) # next_state shape: string, reward shape: scalar, done shape: scalar (boolean)
# print(action, reward, done)
action_logp = action_dist.log_prob(action)
for i in range(env.batch_size):
trajectory[i].append([state[i], action[i], action_logp[i], reward[i], done[i], state_value[i]]) # Shapes: (string, (1,), (1),(15), (15) scalar, scalar (boolean), (1, 1))
state = next_state
rewards = []
for i in range(env.batch_size):
if done[i]:
traj_reward = [trajectory[i][j][3] for j in range(env.steps[i])]
rewards.append(sum(traj_reward))
memory.extend(trajectory[i])
trajectory[i] = []
# print(env.cat_response(env.response_cache[i]))
for r in rewards:
reward_file.write(f"{r:.5f}\n")
if len(memory)>(512):
dist.barrier()
reward_file.flush()
data = trainer.inin_loader(memory)
loader = DataLoader(data, trainer.batch_size, True, collate_fn=trainer.f, pin_memory = True, num_workers=0, persistent_workers=False, drop_last=True)
trainer.update(memory, loader)
dist.barrier()
del loader
if rank==0:
memory[:] = []
if time() - save_time > 10*60:
save_time = time()
if rank == 0:
# Save Agent weight
torch.save(agentDDP.module.state_dict(), f"./save/TV_Agent_{rank}.pt")
def main():
'''Train agent with RAG enviroment. The code can run on multiple GPU to increase training speed.'''
world_size = torch.cuda.device_count()
# world_size = 1
total = 5000
env_bs = 32
print(torch.cuda.device_count())
print('Loading LLM')
LM = LLaMa_reader(config.LM_dir, "cpu", token=config.token, from_pretrained=True)
dtype = LM.dtype
num_dims = LM.model.config.hidden_size
print(f'Initialize KnowEnc with {dtype}...')
Encoder = KnowEncoder(dims=num_dims, **config.enc_config, dtype=dtype)
print(f'Initialize EncTunedLM...')
peft_configs = {'Enc': peft.AdaptionPromptConfig(adapter_layers=32, adapter_len=1)}
LM = EncTunedLM(LM, Enc=Encoder, configs=peft_configs, adapter_name='Enc')
LM.eval()
print(f'Loading EncTunedLM weight...')
LM.load_state_dict(torch.load("save/TV_EncLM_0.pt", map_location='cpu'))
print('Initialize retriever')
lex_MAE_retriver = lex_retriever()
lex_MAE_retriver.model.load_state_dict(torch.load('save/LEX_MAE_retriever904.pt', map_location='cpu')['enc_model_state_dict'], assign=False)
lex_MAE_retriver.eval()
print('Loading dataset...')
if True:
data_path = "data/TV_test.jsonl"
dataset = NQADataset(data_path=data_path, use_doc=True, use_short=True, use_long=False, num_samples = 200)
length = 128
collate_fn = collate(max_length=length, form="short")
else:
data_path = "data/NQ_train.jsonl"
dataset = NQADataset(data_path=data_path, use_doc=True, use_short=False, use_long=True, num_samples = None)
length = 256
collate_fn = collate(max_length=length, form = "long")
# data_path = 'data/cleandata_with_doc.jsonl'
# dataset = NQADataset(data_path=data_path, num_samples=18, use_doc=True)
env = LLMEnv_batch_version(dataset, LM, lex_MAE_retriver, 3, collate_fn, batch_size=env_bs)
print("Initialize Agent...")
agent = BertAgentCritic(config.agent_size_config, 3)
agent.load_state_dict(torch.load("save/TV_Agent_0.pt", map_location="cpu"))
memory = multiprocessing.Manager().list()
with socket() as s:
s.bind(('', 0))
port = s.getsockname()[1]
mp.spawn(training,
args=(world_size, port, total, env, agent, memory),
nprocs=world_size,
join=True)
if __name__ == "__main__":
main()