File size: 13,469 Bytes
dc15a10 | 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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | import json
import gzip
import torch
import pathlib
import requests
import traceback
import numpy as np
from torch import nn, Tensor
from torch.nn import functional as F
from torch.nn.utils.rnn import pack_padded_sequence, pad_sequence
from torch.distributions import Normal, Categorical
from typing import *
from functools import partial
from itertools import permutations
from libriichi.mjai import Bot
from libriichi.consts import obs_shape, oracle_obs_shape, ACTION_SPACE, GRP_SIZE
# ========== Online Server =========== #
OT_REQUEST_TIMEOUT = 2
ot_settings = {
"server": "http://example.com",
"online": False,
"api_key": "example_api_key",
}
is_online = False
def online_settings_init():
global ot_settings
# Check if the file exists
if (pathlib.Path(__file__).parent / 'ot_settings.json').exists():
with open(pathlib.Path(__file__).parent / 'ot_settings.json', 'r') as f:
ot_settings = json.load(f)
online_settings_init()
# ==================================== #
class ChannelAttention(nn.Module):
def __init__(self, channels, ratio=16, actv_builder=nn.ReLU, bias=True):
super().__init__()
self.shared_mlp = nn.Sequential(
nn.Linear(channels, channels // ratio, bias=bias),
actv_builder(),
nn.Linear(channels // ratio, channels, bias=bias),
)
if bias:
for mod in self.modules():
if isinstance(mod, nn.Linear):
nn.init.constant_(mod.bias, 0)
def forward(self, x: Tensor):
avg_out = self.shared_mlp(x.mean(-1))
max_out = self.shared_mlp(x.amax(-1))
weight = (avg_out + max_out).sigmoid()
x = weight.unsqueeze(-1) * x
return x
class ResBlock(nn.Module):
def __init__(
self,
channels,
*,
norm_builder = nn.Identity,
actv_builder = nn.ReLU,
pre_actv = False,
):
super().__init__()
self.pre_actv = pre_actv
if pre_actv:
self.res_unit = nn.Sequential(
norm_builder(),
actv_builder(),
nn.Conv1d(channels, channels, kernel_size=3, padding=1, bias=False),
norm_builder(),
actv_builder(),
nn.Conv1d(channels, channels, kernel_size=3, padding=1, bias=False),
)
else:
self.res_unit = nn.Sequential(
nn.Conv1d(channels, channels, kernel_size=3, padding=1, bias=False),
norm_builder(),
actv_builder(),
nn.Conv1d(channels, channels, kernel_size=3, padding=1, bias=False),
norm_builder(),
)
self.actv = actv_builder()
self.ca = ChannelAttention(channels, actv_builder=actv_builder, bias=True)
def forward(self, x):
out = self.res_unit(x)
out = self.ca(out)
out = out + x
if not self.pre_actv:
out = self.actv(out)
return out
class ResNet(nn.Module):
def __init__(
self,
in_channels,
conv_channels,
num_blocks,
*,
norm_builder = nn.Identity,
actv_builder = nn.ReLU,
pre_actv = False,
):
super().__init__()
blocks = []
for _ in range(num_blocks):
blocks.append(ResBlock(
conv_channels,
norm_builder = norm_builder,
actv_builder = actv_builder,
pre_actv = pre_actv,
))
layers = [nn.Conv1d(in_channels, conv_channels, kernel_size=3, padding=1, bias=False)]
if pre_actv:
layers += [*blocks, norm_builder(), actv_builder()]
else:
layers += [norm_builder(), actv_builder(), *blocks]
layers += [
nn.Conv1d(conv_channels, 32, kernel_size=3, padding=1),
actv_builder(),
nn.Flatten(),
nn.Linear(32 * 34, 1024),
]
self.net = nn.Sequential(*layers)
def forward(self, x):
return self.net(x)
class Brain(nn.Module):
def __init__(self, *, conv_channels, num_blocks, is_oracle=False, version=1):
super().__init__()
self.is_oracle = is_oracle
self.version = version
in_channels = obs_shape(version)[0]
if is_oracle:
in_channels += oracle_obs_shape(version)[0]
norm_builder = partial(nn.BatchNorm1d, conv_channels, momentum=0.01)
actv_builder = partial(nn.Mish, inplace=True)
pre_actv = True
match version:
case 1:
actv_builder = partial(nn.ReLU, inplace=True)
pre_actv = False
self.latent_net = nn.Sequential(
nn.Linear(1024, 512),
nn.ReLU(inplace=True),
)
self.mu_head = nn.Linear(512, 512)
self.logsig_head = nn.Linear(512, 512)
case 2:
pass
case 3 | 4:
norm_builder = partial(nn.BatchNorm1d, conv_channels, momentum=0.01, eps=1e-3)
case _:
raise ValueError(f'Unexpected version {self.version}')
self.encoder = ResNet(
in_channels = in_channels,
conv_channels = conv_channels,
num_blocks = num_blocks,
norm_builder = norm_builder,
actv_builder = actv_builder,
pre_actv = pre_actv,
)
self.actv = actv_builder()
# always use EMA or CMA when True
self._freeze_bn = False
def forward(self, obs: Tensor, invisible_obs: Optional[Tensor] = None) -> Union[Tuple[Tensor, Tensor], Tensor]:
if self.is_oracle:
assert invisible_obs is not None
obs = torch.cat((obs, invisible_obs), dim=1)
phi = self.encoder(obs)
match self.version:
case 1:
latent_out = self.latent_net(phi)
mu = self.mu_head(latent_out)
logsig = self.logsig_head(latent_out)
return mu, logsig
case 2 | 3 | 4:
return self.actv(phi)
case _:
raise ValueError(f'Unexpected version {self.version}')
def train(self, mode=True):
super().train(mode)
if self._freeze_bn:
for mod in self.modules():
if isinstance(mod, nn.BatchNorm1d):
mod.eval()
# I don't think this benefits
# module.requires_grad_(False)
return self
def reset_running_stats(self):
for mod in self.modules():
if isinstance(mod, nn.BatchNorm1d):
mod.reset_running_stats()
def freeze_bn(self, value: bool):
self._freeze_bn = value
return self.train(self.training)
class AuxNet(nn.Module):
def __init__(self, dims=None):
super().__init__()
self.dims = dims
self.net = nn.Linear(1024, sum(dims), bias=False)
def forward(self, x):
return self.net(x).split(self.dims, dim=-1)
class DQN(nn.Module):
def __init__(self, *, version=1):
super().__init__()
self.version = version
print(version)
match version:
case 1:
self.v_head = nn.Linear(512, 1)
self.a_head = nn.Linear(512, ACTION_SPACE)
case 2 | 3:
hidden_size = 512 if version == 2 else 256
self.v_head = nn.Sequential(
nn.Linear(1024, hidden_size),
nn.Mish(inplace=True),
nn.Linear(hidden_size, 1),
)
self.a_head = nn.Sequential(
nn.Linear(1024, hidden_size),
nn.Mish(inplace=True),
nn.Linear(hidden_size, ACTION_SPACE),
)
case 4:
self.net = nn.Linear(1024, 1 + ACTION_SPACE)
nn.init.constant_(self.net.bias, 0)
def forward(self, phi, mask):
if self.version == 4:
v, a = self.net(phi).split((1, ACTION_SPACE), dim=-1)
else:
v = self.v_head(phi)
a = self.a_head(phi)
a_sum = a.masked_fill(~mask, 0.).sum(-1, keepdim=True)
mask_sum = mask.sum(-1, keepdim=True)
a_mean = a_sum / mask_sum
q = (v + a - a_mean).masked_fill(~mask, -torch.inf)
return q
class MortalEngine:
def __init__(
self,
brain,
dqn,
is_oracle,
version,
device = None,
stochastic_latent = False,
enable_amp = False,
enable_quick_eval = True,
enable_rule_based_agari_guard = False,
name = 'NoName',
boltzmann_epsilon = 0,
boltzmann_temp = 1,
top_p = 1,
):
self.engine_type = 'mortal'
self.device = device or torch.device('cpu')
assert isinstance(self.device, torch.device)
self.brain = brain.to(self.device).eval()
self.dqn = dqn.to(self.device).eval()
self.is_oracle = is_oracle
self.version = version
self.stochastic_latent = stochastic_latent
self.enable_amp = enable_amp
self.enable_quick_eval = enable_quick_eval
self.enable_rule_based_agari_guard = enable_rule_based_agari_guard
self.name = name
self.boltzmann_epsilon = boltzmann_epsilon
self.boltzmann_temp = boltzmann_temp
self.top_p = top_p
def react_batch(self, obs, masks, invisible_obs):
# ========== Online Server =========== #
global ot_settings, is_online
try:
with (
torch.autocast(self.device.type, enabled=self.enable_amp),
torch.inference_mode(),
):
return self._react_batch(obs, masks, invisible_obs)
except Exception as ex:
raise Exception(f'{ex}\n{traceback.format_exc()}')
def _react_batch(self, obs, masks, invisible_obs):
obs = torch.as_tensor(np.stack(obs, axis=0), device=self.device)
masks = torch.as_tensor(np.stack(masks, axis=0), device=self.device)
invisible_obs = None
if self.is_oracle:
invisible_obs = torch.as_tensor(np.stack(invisible_obs, axis=0), device=self.device)
batch_size = obs.shape[0]
match self.version:
case 1:
mu, logsig = self.brain(obs, invisible_obs)
if self.stochastic_latent:
latent = Normal(mu, logsig.exp() + 1e-6).sample()
else:
latent = mu
q_out = self.dqn(latent, masks)
case 2 | 3 | 4:
phi = self.brain(obs)
q_out = self.dqn(phi, masks)
if self.boltzmann_epsilon > 0:
is_greedy = torch.full((batch_size,), 1-self.boltzmann_epsilon, device=self.device).bernoulli().to(torch.bool)
logits = (q_out / self.boltzmann_temp).masked_fill(~masks, -torch.inf)
sampled = sample_top_p(logits, self.top_p)
actions = torch.where(is_greedy, q_out.argmax(-1), sampled)
else:
is_greedy = torch.ones(batch_size, dtype=torch.bool, device=self.device)
actions = q_out.argmax(-1)
return actions.tolist(), q_out.tolist(), masks.tolist(), is_greedy.tolist()
def sample_top_p(logits, p):
if p >= 1:
return Categorical(logits=logits).sample()
if p <= 0:
return logits.argmax(-1)
probs = logits.softmax(-1)
probs_sort, probs_idx = probs.sort(-1, descending=True)
probs_sum = probs_sort.cumsum(-1)
mask = probs_sum - probs_sort > p
probs_sort[mask] = 0.
sampled = probs_idx.gather(-1, probs_sort.multinomial(1)).squeeze(-1)
return sampled
def load_model(seat: int) -> Bot:
# check if GPU is available
# device = torch.device('cpu')
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
# latest binary model
control_state_file = "./Elite4zMarch4th9407.pth"
# Get the path of control_state_file = current directory / control_state_file
control_state_file = pathlib.Path(__file__).parent / control_state_file
state = torch.load(control_state_file, map_location=device)
mortal = Brain(version=state['config']['control']['version'], conv_channels=state['config']['resnet']['conv_channels'], num_blocks=state['config']['resnet']['num_blocks']).eval()
dqn = DQN(version=state['config']['control']['version']).eval()
mortal.load_state_dict(state['mortal'])
dqn.load_state_dict(state['current_dqn'])
engine = MortalEngine(
mortal,
dqn,
is_oracle = False,
version = state['config']['control']['version'],
device = device,
enable_amp = False,
enable_quick_eval = False,
enable_rule_based_agari_guard = True,
name = 'mortal',
)
bot = Bot(engine, seat)
return bot
|