-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprefix_model.py
32 lines (25 loc) · 1.07 KB
/
prefix_model.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
import torch
import torch.nn as nn
class PrefixTuning(nn.Module):
def __init__(self, pretrained_config, prompt_len=100, hidden_dim=512):
super().__init__()
# Config of Pre-Trained LM
self.pretrained_config = pretrained_config
# torch.tensor([0, 1, 2, .. , prefix_len-1])
self.pre_prompt = torch.arange(prompt_len)
# Embedding
self.embd = nn.Embedding(num_embeddings=prompt_len, embedding_dim=pretrained_config.d_model)
# Reparameterization
self.reparam = nn.Sequential(
nn.Linear(pretrained_config.d_model, hidden_dim),
nn.Tanh(),
nn.Linear(hidden_dim, pretrained_config.d_model)
)
def forward(self, batch_size, device):
# Shape: batch_size, prompt_len
prompt=self.pre_prompt.unsqueeze(0).expand(batch_size, -1).to(device)
# Shape: batch_size, prompt_len, d_model
prompt=self.embd(prompt)
# Shape: batch_size, prompt_len, d_model
prompt=self.reparam(prompt)
return prompt