Sure! Below is a PyTorch implementation of a transformer architecture using pre-norm (RMSNorm), causal attention mask, custom scaled dot product attention for the multihead attention mechanism, and SwiGLU activation in the feedforward layer.
python Copy code
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
class RMSNorm(nn.Module):
def __init__(self, d_model, eps=1e-6):
super(RMSNorm, self).__init__()
self.eps = eps
self.scale = nn.Parameter(torch.ones(d_model))
def forward(self, x):
norm = x.norm(2, dim=-1, keepdim=True)
return self.scale * x / (norm + self.eps)
class SwiGLU(nn.Module):
def __init__(self, d_model, d_ff):
super(SwiGLU, self).__init__()
self.linear1 = nn.Linear(d_model, d_ff * 2)
self.linear2 = nn.Linear(d_ff, d_model)
def forward(self, x):
x_proj = self.linear1(x)
x1, x2 = x_proj.chunk(2, dim=-1)
x = x1 * F.silu(x2)
return self.linear2(x)
class CustomScaledDotProductAttention(nn.Module):
def __init__(self, d_k, dropout=0.1):
super(CustomScaledDotProductAttention, self).__init__()
self.sqrt_dk = math.sqrt(d_k)
self.dropout = nn.Dropout(dropout)
def forward(self, q, k, v, mask=None):
scores = torch.matmul(q, k.transpose(-2, -1)) / self.sqrt_dk
if mask is not None:
scores = scores.masked_fill(mask == 0, float('-inf'))
attn = self.dropout(F.softmax(scores, dim=-1))
output = torch.matmul(attn, v)
return output, attn
class MultiheadAttention(nn.Module):
def __init__(self, d_model, n_heads, dropout=0.1):
super(MultiheadAttention, self).__init__()
self.d_k = d_model // n_heads
self.n_heads = n_heads
self.q_linear = nn.Linear(d_model, d_model)
self.k_linear = nn.Linear(d_model, d_model)
self.v_linear = nn.Linear(d_model, d_model)
self.out_linear = nn.Linear(d_model, d_model)
self.attention = CustomScaledDotProductAttention(self.d_k, dropout)
def forward(self, q, k, v, mask=None):
bsz = q.size(0)
q = self.q_linear(q).view(bsz, -1, self.n_heads, self.d_k).transpose(1, 2)
k = self.k_linear(k).view(bsz, -1, self.n_heads, self.d_k).transpose(1, 2)
v = self.v_linear(v).view(bsz, -1, self.n_heads, self.d_k).transpose(1, 2)
if mask is not None:
mask = mask.unsqueeze(1)
attn_output, attn_weights = self.attention(q, k, v, mask)
attn_output = attn_output.transpose(1, 2).contiguous().view(bsz, -1, self.n_heads * self.d_k)
output = self.out_linear(attn_output)
return output, attn_weights
class TransformerBlock(nn.Module):
def __init__(self, d_model, n_heads, d_ff, dropout=0.1):
super(TransformerBlock, self).__init__()
self.n_heads = n_heads
self.norm1 = RMSNorm(d_model)
self.norm2 = RMSNorm(d_model)
self.attn = MultiheadAttention(d_model, n_heads, dropout)
self.ff = SwiGLU(d_model, d_ff)
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
def forward(self, x, mask=None):
x2 = self.norm1(x)
attn_output, _ = self.attn(x2, x2, x2, mask)
x = x + self.dropout1(attn_output)
x2 = self.norm2(x)
ff_output = self.ff(x2)
x = x + self.dropout2(ff_output)
return x
class Transformer(nn.Module):
def __init__(self, d_model, n_heads, n_layers, d_ff, vocab_size, max_len, dropout=0.1):
super(Transformer, self).__init__()
self.d_model = d_model
self.n_heads = n_heads
self.n_layers = n_layers
self.embedding = nn.Embedding(vocab_size, d_model)
self.pos_embedding = nn.Parameter(torch.zeros(1, max_len, d_model))
self.layers = nn.ModuleList([
TransformerBlock(d_model, n_heads, d_ff, dropout) for _ in range(n_layers)
])
self.fc_out = nn.Linear(d_model, vocab_size)
def forward(self, x, mask=None):
seq_len = x.size(1)
x = self.embedding(x) + self.pos_embedding[:, :seq_len, :]
for layer in self.layers:
x = layer(x, mask)
return self.fc_out(x)
def generate_mask(sz):
mask = torch.tril(torch.ones(sz, sz))
return mask.unsqueeze(0).unsqueeze(1) # For compatibility with multihead attention
# Example usage
d_model = 512
n_heads = 8
d_ff = 2048
n_layers = 6
vocab_size = 10000
max_len = 512
dropout = 0.1
model = Transformer(d_model, n_heads, n_layers, d_ff, vocab_size, max_len, dropout)
sample_input = torch.randint(0, vocab_size, (32, 100)) # Batch size 32, sequence length 100
causal_mask = generate_mask(100)
output = model(sample_input, causal_mask)
print(output.shape) # Should output (32, 100, vocab_size)
You can modify the parameters and use this as a baseline for further experimentation in your transformer model.