-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrl_loop.py
More file actions
228 lines (168 loc) · 6.19 KB
/
rl_loop.py
File metadata and controls
228 lines (168 loc) · 6.19 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
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
import gc
import numpy as np
import torch
import torch.nn.functional
import torch.optim as optim
import tqdm
import wandb
from fastchessenv import CBoards, RandomChessEnv
from torch.distributions import Categorical
from stonefish.config import expose_modules
from stonefish.convert import board_to_lczero_tensor
from stonefish.eval.agent_loader import load_model_from_config
from stonefish.utils import RolloutTensor
def _pad_mask(mask):
# Size of the tensor minus the size of actual valid moves
size = 5700 - mask.shape[1]
batch = mask.shape[0]
pad = np.zeros((batch, size))
return np.concatenate([mask, pad], axis=1)
def _state_to_tensor(states):
states = states.flatten()
boards = CBoards.from_array(states).to_board()
boards = [board_to_lczero_tensor(b) for b in boards]
boards_tensor = torch.Tensor(np.stack(boards))
return boards_tensor
class FakeEnv:
def __init__(self, n):
self.envs = [RandomChessEnv(1, invert=True) for _ in range(n)]
def step(self, actions):
out = []
for action, env in zip(actions, self.envs, strict=False):
out.append(env.step(action.reshape(-1, 1)))
out = zip(*out, strict=False)
return map(np.concatenate, out)
def reset(self):
out = []
for env in self.envs:
out.append(env.reset())
out = zip(*out, strict=False)
return map(np.concatenate, out)
def generate_rollout_batch(env, next_boards_tensor, mask, model, num_steps=16):
tensor = RolloutTensor.empty()
device = next_boards_tensor.device
for _ in tqdm.tqdm(range(num_steps)):
boards_tensor = next_boards_tensor
mask = _pad_mask(mask)
mask_tensor = torch.Tensor(mask).to(device)
with torch.no_grad():
logits = model.forward(boards_tensor, None)
legal_logits = logits * mask_tensor + (1 - mask_tensor) * -1e10
probs = torch.nn.functional.softmax(legal_logits, dim=1)
actions = Categorical(probs).sample()
next_states, mask, rewards, done = env.step(actions.detach().cpu().numpy())
next_boards_tensor = _state_to_tensor(next_states).to(device)
tensor = tensor.add(
boards_tensor,
actions,
next_boards_tensor,
torch.Tensor(rewards).to(device),
torch.Tensor(done).to(device),
mask_tensor,
)
return tensor, next_boards_tensor, mask
def main():
# Initialize wandb
wandb.init(
project="stonefish-rl",
config={
"lr": 1e-4,
"num_envs": 32,
"num_steps": 128,
"gamma": 0.99,
"device": "mps" if torch.backends.mps.is_available() else "cpu",
},
)
model = load_model_from_config("configs/train_convnet_big.yml", "model_3.pth")
# Use MPS if available (Mac), otherwise CPU
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
print(f"Using device: {device}")
model.model = model.model.to(device)
# Commented out for local testing
# model.model = DistributedDataParallel(model.model)
opt = optim.Adam(model.model.parameters(), lr=1e-4)
env = FakeEnv(48)
states, mask = env.reset()
next_boards_tensor = _state_to_tensor(states).to(device)
for step in range(10000):
tensor, next_boards_tensor, mask = generate_rollout_batch(
env, next_boards_tensor, mask, model.model, num_steps=128
)
# Commented out for local testing
# dist.barrier()
tensor.reward[tensor.done & (tensor.reward == 0)] = -1.0
games_completed = tensor.done.sum().item()
avg_reward = (
tensor.reward[tensor.done].mean().item() if games_completed > 0 else 0.0
)
print(
f"Step {step}: {games_completed} games completed, avg reward: {avg_reward:.3f}"
)
tensor.decay_(0.99)
opt.zero_grad()
(
flat_state,
flat_next_state,
flat_action,
flat_reward,
flat_done,
flat_mask,
) = tensor.get_data()
completed_mask = (flat_reward != 0).flatten()
flat_state = flat_state[completed_mask]
flat_action = flat_action[completed_mask]
flat_reward = flat_reward[completed_mask]
flat_mask = flat_mask[completed_mask]
if len(flat_state) == 0:
print("No completed games, skipping update")
continue
logits = model.model(flat_state, None)
# Apply mask to logits
legal_logits = logits * flat_mask + (1 - flat_mask) * -1e10
log_probs = torch.log_softmax(legal_logits, dim=1)
sel_log_probs = log_probs.gather(1, flat_action).squeeze(1)
policy_loss = -1.0 * torch.mean(flat_reward * sel_log_probs)
policy_loss.backward()
opt.step()
# Log to wandb
wandb.log(
{
"step": step,
"games_completed": games_completed,
"avg_reward": avg_reward,
"policy_loss": policy_loss.item(),
"completed_samples": len(flat_state),
"wins": (tensor.reward[tensor.done] == 1).sum(),
"losses": (tensor.reward[tensor.done] == -1).sum(),
"avg_log_prob": sel_log_probs.mean().item(),
}
)
# Save checkpoint every 100 steps
if step % 100 == 0:
checkpoint = {
"step": step,
"model_state_dict": model.model.state_dict(),
"optimizer_state_dict": opt.state_dict(),
"avg_reward": avg_reward,
}
torch.save(checkpoint, f"rl_checkpoint_{step}.pth")
print(f"Saved checkpoint at step {step}")
# Explicitly delete tensors to free memory
del (
tensor,
flat_state,
flat_action,
flat_reward,
flat_mask,
logits,
log_probs,
sel_log_probs,
)
gc.collect()
torch.mps.empty_cache()
if __name__ == "__main__":
expose_modules()
# Commented out for local testing
# setup_distributed()
main()
# cleanup_distributed()