Upload modeling_aixk.py with huggingface_hub
Browse files- modeling_aixk.py +65 -0
modeling_aixk.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
class AIXKNovelAttention(nn.Module):
|
| 6 |
+
def __init__(self, config):
|
| 7 |
+
super().__init__()
|
| 8 |
+
self.hidden_size = config.hidden_size
|
| 9 |
+
self.num_heads = config.num_attention_heads
|
| 10 |
+
self.head_dim = self.hidden_size // self.num_heads
|
| 11 |
+
self.q_proj = nn.Linear(self.hidden_size, self.hidden_size)
|
| 12 |
+
self.k_proj = nn.Linear(self.hidden_size, self.hidden_size)
|
| 13 |
+
self.v_proj = nn.Linear(self.hidden_size, self.hidden_size)
|
| 14 |
+
self.local_context = nn.Conv1d(self.hidden_size, self.hidden_size, kernel_size=3, padding=1, groups=self.hidden_size)
|
| 15 |
+
self.out_proj = nn.Linear(self.hidden_size, self.hidden_size)
|
| 16 |
+
|
| 17 |
+
def forward(self, hidden_states, attention_mask=None):
|
| 18 |
+
batch_size, seq_len, _ = hidden_states.size()
|
| 19 |
+
local_feat = self.local_context(hidden_states.transpose(1, 2)).transpose(1, 2)
|
| 20 |
+
hidden_states = hidden_states + local_feat
|
| 21 |
+
q = self.q_proj(hidden_states).view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
|
| 22 |
+
k = self.k_proj(hidden_states).view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
|
| 23 |
+
v = self.v_proj(hidden_states).view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
|
| 24 |
+
attn_weights = torch.matmul(q, k.transpose(-1, -2)) / math.sqrt(self.head_dim)
|
| 25 |
+
if attention_mask is not None:
|
| 26 |
+
attn_weights = attn_weights + attention_mask
|
| 27 |
+
attn_probs = nn.functional.softmax(attn_weights, dim=-1)
|
| 28 |
+
attn_output = torch.matmul(attn_probs, v)
|
| 29 |
+
return self.out_proj(attn_output.transpose(1, 2).contiguous().view(batch_size, seq_len, self.hidden_size))
|
| 30 |
+
|
| 31 |
+
class SentenceSegmentationLayer(nn.Module):
|
| 32 |
+
def __init__(self, config):
|
| 33 |
+
super().__init__()
|
| 34 |
+
self.boundary_detector = nn.Sequential(nn.Linear(config.hidden_size, config.hidden_size), nn.Tanh(), nn.Linear(config.hidden_size, 1))
|
| 35 |
+
self.gate = nn.Sigmoid()
|
| 36 |
+
def forward(self, hidden_states):
|
| 37 |
+
return hidden_states * self.gate(self.boundary_detector(hidden_states))
|
| 38 |
+
|
| 39 |
+
class AIXKCustomModelFixed(nn.Module):
|
| 40 |
+
def __init__(self, config):
|
| 41 |
+
super().__init__()
|
| 42 |
+
self.config = config
|
| 43 |
+
self.embeddings = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 44 |
+
self.layers = nn.ModuleList([
|
| 45 |
+
nn.ModuleDict({
|
| 46 |
+
'attn': AIXKNovelAttention(config),
|
| 47 |
+
'seg': SentenceSegmentationLayer(config),
|
| 48 |
+
'mlp': nn.Sequential(nn.Linear(config.hidden_size, config.intermediate_size), nn.GELU(), nn.Linear(config.intermediate_size, config.hidden_size)),
|
| 49 |
+
'norm1': nn.LayerNorm(config.hidden_size),
|
| 50 |
+
'norm2': nn.LayerNorm(config.hidden_size)
|
| 51 |
+
}) for _ in range(config.num_hidden_layers)
|
| 52 |
+
])
|
| 53 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 54 |
+
|
| 55 |
+
def forward(self, input_ids, attention_mask=None):
|
| 56 |
+
x = self.embeddings(input_ids)
|
| 57 |
+
if attention_mask is None:
|
| 58 |
+
seq_len = input_ids.shape[1]
|
| 59 |
+
attention_mask = torch.triu(torch.ones(seq_len, seq_len, device=input_ids.device), diagonal=1).bool()
|
| 60 |
+
attention_mask = attention_mask.masked_fill(attention_mask, float('-inf'))
|
| 61 |
+
for layer in self.layers:
|
| 62 |
+
x = x + layer['attn'](layer['norm1'](x), attention_mask)
|
| 63 |
+
x = layer['seg'](x)
|
| 64 |
+
x = x + layer['mlp'](layer['norm2'](x))
|
| 65 |
+
return self.lm_head(x)
|