Made model Batch ready

This commit is contained in:
Christian Risi 2025-10-07 16:37:20 +02:00
parent 109ad9f36b
commit fdece42462
4 changed files with 47 additions and 17 deletions

View File

@ -0,0 +1,19 @@
import torch
class DeToken(torch.nn.Module):
def __init__(self, embedding_size: int, vocabulary_size: int) -> None:
super().__init__()
self.__linear = torch.nn.Linear(embedding_size, vocabulary_size)
def forward(self, x: torch.Tensor) -> torch.Tensor:
# 1) Go from latent space to vocabularu space
x = self.__linear(x)
# 2) Go to logits
x = torch.softmax(x, 2)
return x

View File

@ -35,22 +35,28 @@ class Decoder(nn.Module):
) )
self.__layer_norm_3 = nn.LayerNorm(embedding_dimension) self.__layer_norm_3 = nn.LayerNorm(embedding_dimension)
def forward(
self,
def forward(self, x, k_x, v_x, padding_mask = None): #-> list[torch.Tensor]: # k_x = v_x . While x_q = x args: tuple[
torch.Tensor,
torch.Tensor,
torch.Tensor,
torch.Tensor
]
): # -> list[torch.Tensor]: # k_x = v_x . While x_q = x
# WARNING: args is needed to have sequential
x, k_x, v_x, padding_mask = args
# build of attention mask # build of attention mask
attention_mask = get_causal_attention_mask(x.size(1)) attention_mask = get_causal_attention_mask(x.size(1))
# 1) Masked Attention # 1) Masked Attention
MASKED_ATTENTION = self.__masked_attention( MASKED_ATTENTION = self.__masked_attention(
x, x, x, key_padding_mask=padding_mask, attn_mask=attention_mask x, x, x, key_padding_mask=padding_mask, attention_mask=attention_mask
) )
# 2) Dropout # 2) Dropout
DROPPED_MASKED_ATTENTION = self.__dropout( DROPPED_MASKED_ATTENTION = self.__dropout(MASKED_ATTENTION)
MASKED_ATTENTION
)
del MASKED_ATTENTION del MASKED_ATTENTION
# 3) Residual Connection # 3) Residual Connection
@ -61,7 +67,9 @@ class Decoder(nn.Module):
x = self.__layer_norm_1(x) x = self.__layer_norm_1(x)
# 5) Encoderdecoder (cross) attention # 5) Encoderdecoder (cross) attention
CROSS_ATTENTION = self.__cross_attention(x, k_x, v_x, key_padding_mask=padding_mask) CROSS_ATTENTION = self.__cross_attention(
x, k_x, v_x, key_padding_mask=padding_mask
)
# 6) Dropout # 6) Dropout
DROPPED_CROSS_ATTENTION = self.__dropout(CROSS_ATTENTION) DROPPED_CROSS_ATTENTION = self.__dropout(CROSS_ATTENTION)
@ -88,7 +96,7 @@ class Decoder(nn.Module):
# 12) Layer Normalization # 12) Layer Normalization
x = self.__layer_norm_3(x) x = self.__layer_norm_3(x)
return x, k_x, v_x, padding_mask return (x, k_x, v_x, padding_mask)
# use eval to disable dropout ecc # use eval to disable dropout ecc

View File

@ -1,3 +1,4 @@
import torch
import torch.nn as nn import torch.nn as nn
from Project_Model.Libs.Transformer.Classes.FeedForwardNetwork import FeedForwardNetwork from Project_Model.Libs.Transformer.Classes.FeedForwardNetwork import FeedForwardNetwork
from Project_Model.Libs.Transformer.Classes.TorchMultiHeadAttention import ( from Project_Model.Libs.Transformer.Classes.TorchMultiHeadAttention import (
@ -29,14 +30,17 @@ class Encoder(
embedding_dimension embedding_dimension
) # norm of second "Add and Normalize" ) # norm of second "Add and Normalize"
self.__dropout = nn.Dropout(0.1) # ... self.__dropout = nn.Dropout(0.1) # ...
pass
def forward(self, x, padding_mask = None):
def forward(self, args: tuple[torch.Tensor, torch.Tensor]):
# WARNING: args is needed to have sequential
x, padding_mask = args
# -> ATTENTION -> dropout -> add and normalize -> FF -> dropout -> add and normalize -> # -> ATTENTION -> dropout -> add and normalize -> FF -> dropout -> add and normalize ->
# Attention with Residual Connection [ input + self-attention] # Attention with Residual Connection [ input + self-attention]
# 1) Multi Head Attention # 1) Multi Head Attention
ATTENTION = self.__attention(x, x, x,key_padding_mask= padding_mask) ATTENTION = self.__attention(x, x, x, key_padding_mask=padding_mask)
# 2) Dropout # 2) Dropout
DROPPED_ATTENTION = self.__dropout(ATTENTION) DROPPED_ATTENTION = self.__dropout(ATTENTION)
@ -62,7 +66,7 @@ class Encoder(
# 8) Layer Normalization # 8) Layer Normalization
x = self.__layer_norm_2(x) x = self.__layer_norm_2(x)
return x,padding_mask return (x, padding_mask)
# use eval to disable dropout ecc # use eval to disable dropout ecc

View File

@ -8,17 +8,16 @@ class TorchMultiHeadAttention(nn.Module):
self, self,
embedding_dimension: int, embedding_dimension: int,
number_of_attention_heads: int, number_of_attention_heads: int,
dropout: float = 0.0, dropout: float = 0.0
): ):
super().__init__() super().__init__()
self.attention = nn.MultiheadAttention( self.attention = torch.nn.MultiheadAttention(
embedding_dimension, embedding_dimension,
number_of_attention_heads, num_heads=number_of_attention_heads,
dropout=dropout, dropout=dropout,
batch_first=True, batch_first=True,
) )
def forward( def forward(
self, self,
x_q: torch.Tensor, x_q: torch.Tensor,