Construct the predictor model and move it to device.
(model_type, hidden_dim, num_experts, num_layers, hidden_size, device,
dropout=0.1)
| 338 | |
| 339 | |
| 340 | def make_model(model_type, hidden_dim, num_experts, num_layers, hidden_size, device, |
| 341 | dropout=0.1): |
| 342 | """Construct the predictor model and move it to device.""" |
| 343 | import torch.nn as nn |
| 344 | |
| 345 | class ExpertPredictor(nn.Module): |
| 346 | """Original 2-layer MLP, same-layer: h[l] -> experts[l].""" |
| 347 | def __init__(self): |
| 348 | super().__init__() |
| 349 | self.layer_emb = nn.Embedding(num_layers, 32) |
| 350 | self.net = nn.Sequential( |
| 351 | nn.Linear(hidden_dim + 32, hidden_size), |
| 352 | nn.ReLU(), |
| 353 | nn.Dropout(dropout), |
| 354 | nn.Linear(hidden_size, hidden_size), |
| 355 | nn.ReLU(), |
| 356 | nn.Dropout(dropout), |
| 357 | nn.Linear(hidden_size, num_experts), |
| 358 | ) |
| 359 | |
| 360 | def forward(self, x, layer_ids): |
| 361 | return self.net(torch.cat([x, self.layer_emb(layer_ids)], dim=-1)) |
| 362 | |
| 363 | class FateLinearPredictor(nn.Module): |
| 364 | """Single linear layer: h[l] -> experts[l] or experts[l+1] (cross).""" |
| 365 | def __init__(self): |
| 366 | super().__init__() |
| 367 | self.layer_emb = nn.Embedding(num_layers, 64) |
| 368 | self.proj = nn.Linear(hidden_dim + 64, num_experts) |
| 369 | |
| 370 | def forward(self, x, layer_ids): |
| 371 | return self.proj(torch.cat([x, self.layer_emb(layer_ids)], dim=-1)) |
| 372 | |
| 373 | class FateMLPPredictor(nn.Module): |
| 374 | """One-hidden-layer MLP (cross-layer ablation): h[l] -> experts[l+1].""" |
| 375 | def __init__(self): |
| 376 | super().__init__() |
| 377 | self.layer_emb = nn.Embedding(num_layers, 64) |
| 378 | self.net = nn.Sequential( |
| 379 | nn.Linear(hidden_dim + 64, hidden_size), |
| 380 | nn.ReLU(), |
| 381 | nn.Dropout(dropout), |
| 382 | nn.Linear(hidden_size, num_experts), |
| 383 | ) |
| 384 | |
| 385 | def forward(self, x, layer_ids): |
| 386 | return self.net(torch.cat([x, self.layer_emb(layer_ids)], dim=-1)) |
| 387 | |
| 388 | constructors = { |
| 389 | 'mlp': ExpertPredictor, |
| 390 | 'linear': FateLinearPredictor, |
| 391 | 'linear-cross': FateLinearPredictor, |
| 392 | 'mlp-cross': FateMLPPredictor, |
| 393 | } |
| 394 | if model_type not in constructors: |
| 395 | print(f"ERROR: unknown --model-type '{model_type}'") |
| 396 | sys.exit(1) |
| 397 | return constructors[model_type]().to(device) |
no outgoing calls
no test coverage detected