| import torch |
| import torch.nn as nn |
| import torch.nn.functional as F |
|
|
|
|
| class FMLP(nn.Module): |
| def __init__(self, config): |
| super().__init__() |
|
|
| |
| use_lora = getattr(config, 'use_lora_icl_mlp', False) |
| |
| if use_lora: |
| from .lora import LoRALinear |
| lora_rank = getattr(config, 'lora_rank', 8) |
| lora_alpha = getattr(config, 'lora_alpha', 16) |
| lora_dropout = getattr(config, 'lora_dropout', 0.0) |
| |
| self.fc_1 = LoRALinear( |
| config.embed_dim_f, config.mlp_dim_f, bias=True, |
| lora_rank=lora_rank, lora_alpha=lora_alpha, lora_dropout=lora_dropout |
| ) |
| self.fc_2 = LoRALinear( |
| config.mlp_dim_f, config.embed_dim_f, bias=True, |
| lora_rank=lora_rank, lora_alpha=lora_alpha, lora_dropout=lora_dropout |
| ) |
| else: |
| self.fc_1 = nn.Linear(config.embed_dim_f, config.mlp_dim_f, bias=True) |
| self.fc_2 = nn.Linear(config.mlp_dim_f, config.embed_dim_f, bias=True) |
| |
| self.activation = nn.GELU() |
| self.dropout = nn.Dropout(0.1) |
|
|
| def forward(self, x): |
| x = self.fc_1(x) |
| x = self.activation(x) |
| x = self.dropout(x) |
| x = self.fc_2(x) |
| return x |
|
|
|
|
| class PhiMLP(nn.Module): |
| def __init__(self, config): |
| super().__init__() |
|
|
| |
| use_lora = getattr(config, 'use_lora_phi_mlp', False) |
| |
| if use_lora: |
| from .lora import LoRALinear |
| lora_rank = getattr(config, 'lora_rank', 8) |
| lora_alpha = getattr(config, 'lora_alpha', 16) |
| lora_dropout = getattr(config, 'lora_dropout', 0.0) |
| |
| self.fc_1 = LoRALinear( |
| config.embed_dim_phi, config.mlp_dim_phi, bias=True, |
| lora_rank=lora_rank, lora_alpha=lora_alpha, lora_dropout=lora_dropout |
| ) |
| self.fc_2 = LoRALinear( |
| config.mlp_dim_phi, config.embed_dim_phi, bias=True, |
| lora_rank=lora_rank, lora_alpha=lora_alpha, lora_dropout=lora_dropout |
| ) |
| else: |
| self.fc_1 = nn.Linear(config.embed_dim_phi, config.mlp_dim_phi, bias=True) |
| self.fc_2 = nn.Linear(config.mlp_dim_phi, config.embed_dim_phi, bias=True) |
| |
| self.activation = nn.GELU() |
| self.dropout = nn.Dropout(0.1) |
|
|
| def forward(self, x): |
| x = self.fc_1(x) |
| x = self.activation(x) |
| x = self.dropout(x) |
| x = self.fc_2(x) |
| return x |