Repository navigation
Expand file tree
/
Copy pathDecoder.py
More file actions
52 lines (37 loc) · 1.68 KB
/
Copy pathDecoder.py
File metadata and controls
52 lines (37 loc) · 1.68 KB
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
import torch
import torch.nn as nn
import lightning as L
from transformers.models.ctrl.modeling_ctrl import positional_encoding
from position_Encoding import PositionEncoding
from attention import attention
class DecoderonlyTransformer(L.LightningModule):
def __init__(self, num_tokens,d_vector, max_len):
super().__init__()
self.we=nn.Embedding(num_embeddings=num_tokens,embedding_dim=d_vector)
self.pe=PositionEncoding(d_length=d_vector,max_length_of_sentence=max_len)
self.attention=attention(d_vector=d_vector)
self.fc = nn.Linear(d_vector, num_tokens) # vocab_size
self.loss=nn.CrossEntropyLoss()
def forward(self,toke_ids):
word_embedings=self.we(toke_ids)
positional_encoding=self.pe(word_embedings)
seq_len = toke_ids.size(1)
mask = torch.tril(torch.ones(seq_len, seq_len)).to(toke_ids.device)
mask = mask == 0 # Convert to boolean mask (True = masked)
attention=self.attention(positional_encoding,
positional_encoding,
positional_encoding,
mask=mask)
residual_connection_values=positional_encoding + attention
fc_layer=self.fc(residual_connection_values)
return fc_layer
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=0.001)
def training_step(self, batch, batch_idx):
x, y = batch
logits = self.forward(x)
logits = logits.view(-1, logits.size(-1)) # Flatten for cross-entropy
y = y.view(-1)
loss = self.loss(logits, y)
self.log("train_loss", loss)
return loss