-
Notifications
You must be signed in to change notification settings - Fork 319
/
lora_on_simple_mlp.py
60 lines (47 loc) · 1.94 KB
/
lora_on_simple_mlp.py
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
from functools import partial
import torch
import torch.nn as nn
from loguru import logger
from torch.nn.utils import parametrize
class LoRALayer(torch.nn.Module):
def __init__(self, in_dim: int, out_dim: int, rank: int, alpha: float, device: str):
super().__init__()
std_dev = 1 / torch.sqrt(torch.tensor(rank).float())
self.A = torch.nn.Parameter(torch.randn(in_dim, rank) * std_dev).to(device)
self.B = torch.nn.Parameter(torch.zeros(rank, out_dim)).to(device)
self.alpha = alpha
def forward(self, x):
return self.alpha * (x @ self.A @ self.B)
class MLP(nn.Module):
def __init__(self, input_size):
super(MLP, self).__init__()
self.layers = nn.Sequential(
nn.Linear(input_size, input_size // 2),
nn.ReLU(),
nn.Linear(input_size // 2, input_size // 4),
nn.Sigmoid()
)
def forward(self, x):
return self.layers(x)
class LinearWithLoRA(torch.nn.Module):
def __init__(self, linear, rank, alpha, device):
super().__init__()
self.linear = linear
self.lora = LoRALayer(
linear.in_features, linear.out_features, rank, alpha, device=device
)
def forward(self, x):
return self.linear(x) + self.lora(x)
device = "cuda" if torch.cuda.is_available() else "cpu"
assign_lora = partial(LinearWithLoRA, rank=5, alpha=0.5, device=device)
if __name__ == "__main__":
model = MLP(16).to(device)
logger.info(model.layers[0])
for num_layers in range(len(model.layers)):
if isinstance(model.layers[num_layers], nn.Linear):
model.layers[num_layers] = assign_lora(model.layers[num_layers])
model.layers[num_layers].linear.requires_grad = False
logger.info(model.layers[0])
logger.info(model)
logger.info(model(torch.randn(7, 16).to(device)))
torch.compile(model, fullgraph=True, mode="max-autotune")(torch.randn(7, 16).to(device))