File size: 5,471 Bytes
23274c4 |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import random
# Set manual seed for reproducibility
def set_random_seed(seed=42):
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
# Ensures deterministic behavior
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Call the seed function
set_random_seed()
ELEMENT_LENGTH = 16
D_MODEL = 64
MAX_LEN = 129
N_LAYERS = 12
N_HEADS = 12
D_FF = D_MODEL * 4
D_K = D_MODEL // N_HEADS
D_V = D_MODEL // N_HEADS
DROPOUT = 0.1
class LayerNormalization(nn.Module):
def __init__(self, d_model: int, eps: float = 1e-6) -> None:
super().__init__()
self.eps = eps
self.alpha = nn.Parameter(torch.ones(d_model))
self.bias = nn.Parameter(torch.zeros(d_model))
def forward(self, x):
mean = x.mean(dim=-1, keepdim=True)
std = x.std(dim=-1, keepdim=True)
return self.alpha * (x - mean) / (std + self.eps) + self.bias
class Embedding(nn.Module):
def __init__(self, element_length, d_model, max_len):
super().__init__()
self.element_length = element_length
self.d_model = d_model
self.proj = nn.Linear(element_length, d_model)
self.pos_embed = nn.Embedding(max_len, d_model)
self.norm = LayerNormalization(d_model)
def forward(self, x):
seq_len = x.size(1)
pos = torch.arange(seq_len, dtype=torch.long, device=x.device)
pos = pos.unsqueeze(0).expand_as(x[:, :, 0])
tok_emb = self.proj(x.float()) # Ensure consistency in floating-point precision
embedding = tok_emb + self.pos_embed(pos)
return self.norm(embedding)
class ScaledDotProductAttention(nn.Module):
def __init__(self):
super().__init__()
def forward(self, Q, K, V):
scores = torch.matmul(Q, K.transpose(-1, -2)) / np.sqrt(D_K)
attn = F.softmax(scores, dim=-1)
context = torch.matmul(attn, V)
return context, attn
class MultiHeadAttention(nn.Module):
def __init__(self):
super().__init__()
self.W_Q = nn.Linear(D_MODEL, D_K * N_HEADS)
self.W_K = nn.Linear(D_MODEL, D_K * N_HEADS)
self.W_V = nn.Linear(D_MODEL, D_V * N_HEADS)
self.linear = nn.Linear(N_HEADS * D_V, D_MODEL)
self.norm = LayerNormalization(D_MODEL)
self.dropout = nn.Dropout(DROPOUT)
def forward(self, Q, K, V):
residual, batch_size = Q, Q.size(0)
q_s = self.W_Q(Q).view(batch_size, -1, N_HEADS, D_K).transpose(1, 2)
k_s = self.W_K(K).view(batch_size, -1, N_HEADS, D_K).transpose(1, 2)
v_s = self.W_V(V).view(batch_size, -1, N_HEADS, D_V).transpose(1, 2)
context, attn = ScaledDotProductAttention()(q_s, k_s, v_s)
output = context.transpose(1, 2).contiguous().view(batch_size, -1, N_HEADS * D_V)
output = self.linear(output)
return residual + self.dropout(output), attn
class PoswiseFeedForwardNet(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(D_MODEL, D_FF)
self.fc2 = nn.Linear(D_FF, D_MODEL)
self.dropout = nn.Dropout(DROPOUT)
self.norm = LayerNormalization(D_MODEL)
def forward(self, x):
output = self.fc2(self.dropout(F.relu(self.fc1(x))))
return x + self.dropout(output)
class EncoderLayer(nn.Module):
def __init__(self):
super().__init__()
self.enc_self_attn = MultiHeadAttention()
self.pos_ffn = PoswiseFeedForwardNet()
self.norm = LayerNormalization(D_MODEL)
def forward(self, enc_inputs):
attn_outputs, attn = self.enc_self_attn(enc_inputs, enc_inputs, enc_inputs)
attn_outputs = self.norm(attn_outputs)
enc_outputs = self.pos_ffn(attn_outputs)
return enc_outputs, attn
class LWM(torch.nn.Module):
def __init__(self, element_length=16, d_model=64, max_len=129, n_layers=12):
super().__init__()
self.embedding = Embedding(element_length, d_model, max_len)
self.layers = nn.ModuleList([EncoderLayer() for _ in range(n_layers)])
self.linear = nn.Linear(d_model, d_model)
self.norm = LayerNormalization(d_model)
embed_weight = self.embedding.proj.weight
d_model, n_dim = embed_weight.size()
self.decoder = nn.Linear(d_model, n_dim, bias=False)
self.decoder.weight = nn.Parameter(embed_weight.transpose(0, 1))
self.decoder_bias = nn.Parameter(torch.zeros(n_dim))
@classmethod
def from_pretrained(cls, ckpt_name='model_weights.pth', device='cuda', use_auth_token=None):
# Define model
model = cls().to(device)
# Load model weights
ckpt_path = ckpt_name
model.load_state_dict(torch.load(ckpt_path, map_location=device))
print(f"Model loaded successfully from {ckpt_path} to {device}")
return model
def forward(self, input_ids, masked_pos):
output = self.embedding(input_ids)
for layer in self.layers:
output, _ = layer(output)
masked_pos = masked_pos.long()[:, :, None].expand(-1, -1, output.size(-1))
h_masked = torch.gather(output, 1, masked_pos)
h_masked = self.norm(F.relu(self.linear(h_masked)))
logits_lm = self.decoder(h_masked) + self.decoder_bias
return logits_lm, output
|