File size: 2,636 Bytes
3b2d368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
import torch.nn.functional as F


class FMLP(nn.Module):
    def __init__(self, config):
        super().__init__()

        # Check if LoRA is enabled for ICL MLP
        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__()

        # Check if LoRA is enabled for Phi MLP
        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