Upload modeling_meteor.py with huggingface_hub
Browse files- modeling_meteor.py +116 -0
modeling_meteor.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
import math
|
| 6 |
+
|
| 7 |
+
class RMSNorm(nn.Module):
|
| 8 |
+
def __init__(self, dim, eps=1e-6):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.eps = eps
|
| 11 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 12 |
+
def forward(self, x):
|
| 13 |
+
norm = torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
| 14 |
+
return x * norm * self.weight
|
| 15 |
+
|
| 16 |
+
class RotaryEmbedding(nn.Module):
|
| 17 |
+
def __init__(self, dim, theta=1000000.0):
|
| 18 |
+
super().__init__()
|
| 19 |
+
self.dim = dim
|
| 20 |
+
self.theta = theta
|
| 21 |
+
inv_freq = 1.0 / (theta ** (torch.arange(0, dim, 2).float() / dim))
|
| 22 |
+
self.register_buffer("inv_freq", inv_freq)
|
| 23 |
+
def forward(self, seq_len, device):
|
| 24 |
+
t = torch.arange(seq_len, device=device).type_as(self.inv_freq)
|
| 25 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
| 26 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
| 27 |
+
return emb.cos(), emb.sin()
|
| 28 |
+
|
| 29 |
+
def rotate_half(x):
|
| 30 |
+
x1, x2 = x[..., :x.shape[-1]//2], x[..., x.shape[-1]//2:]
|
| 31 |
+
return torch.cat((-x2, x1), dim=-1)
|
| 32 |
+
|
| 33 |
+
def apply_rope(q, k, cos, sin):
|
| 34 |
+
q_cos = q * cos
|
| 35 |
+
q_sin = rotate_half(q) * sin
|
| 36 |
+
k_cos = k * cos
|
| 37 |
+
k_sin = rotate_half(k) * sin
|
| 38 |
+
return q_cos + q_sin, k_cos + k_sin
|
| 39 |
+
|
| 40 |
+
class GQAAttention(nn.Module):
|
| 41 |
+
def __init__(self, config):
|
| 42 |
+
super().__init__()
|
| 43 |
+
self.hidden_size = config["hidden_size"]
|
| 44 |
+
self.num_heads = config["num_attention_heads"]
|
| 45 |
+
self.num_kv_heads = config["num_key_value_heads"]
|
| 46 |
+
self.head_dim = self.hidden_size // self.num_heads
|
| 47 |
+
self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
|
| 48 |
+
self.k_proj = nn.Linear(self.hidden_size, self.num_kv_heads * self.head_dim, bias=False)
|
| 49 |
+
self.v_proj = nn.Linear(self.hidden_size, self.num_kv_heads * self.head_dim, bias=False)
|
| 50 |
+
self.o_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
|
| 51 |
+
self.rope = RotaryEmbedding(self.head_dim, config["rope_theta"])
|
| 52 |
+
|
| 53 |
+
def forward(self, x):
|
| 54 |
+
B, T, C = x.shape
|
| 55 |
+
q = self.q_proj(x).view(B, T, self.num_heads, self.head_dim).transpose(1,2)
|
| 56 |
+
k = self.k_proj(x).view(B, T, self.num_kv_heads, self.head_dim).transpose(1,2)
|
| 57 |
+
v = self.v_proj(x).view(B, T, self.num_kv_heads, self.head_dim).transpose(1,2)
|
| 58 |
+
# GQA repeat
|
| 59 |
+
if self.num_kv_heads != self.num_heads:
|
| 60 |
+
k = k.repeat_interleave(self.num_heads // self.num_kv_heads, dim=1)
|
| 61 |
+
v = v.repeat_interleave(self.num_heads // self.num_kv_heads, dim=1)
|
| 62 |
+
cos, sin = self.rope(T, x.device)
|
| 63 |
+
cos = cos[None, None, :, :]
|
| 64 |
+
sin = sin[None, None, :, :]
|
| 65 |
+
q, k = apply_rope(q, k, cos, sin)
|
| 66 |
+
attn = F.scaled_dot_product_attention(q, k, v, is_causal=True)
|
| 67 |
+
out = attn.transpose(1,2).contiguous().view(B, T, C)
|
| 68 |
+
return self.o_proj(out)
|
| 69 |
+
|
| 70 |
+
class SwiGLU(nn.Module):
|
| 71 |
+
def __init__(self, config):
|
| 72 |
+
super().__init__()
|
| 73 |
+
self.gate_proj = nn.Linear(config["hidden_size"], config["intermediate_size"], bias=False)
|
| 74 |
+
self.up_proj = nn.Linear(config["hidden_size"], config["intermediate_size"], bias=False)
|
| 75 |
+
self.down_proj = nn.Linear(config["intermediate_size"], config["hidden_size"], bias=False)
|
| 76 |
+
def forward(self, x):
|
| 77 |
+
return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))
|
| 78 |
+
|
| 79 |
+
class MeteorBlock(nn.Module):
|
| 80 |
+
def __init__(self, config):
|
| 81 |
+
super().__init__()
|
| 82 |
+
self.attn = GQAAttention(config)
|
| 83 |
+
self.mlp = SwiGLU(config)
|
| 84 |
+
self.norm1 = RMSNorm(config["hidden_size"])
|
| 85 |
+
self.norm2 = RMSNorm(config["hidden_size"])
|
| 86 |
+
def forward(self, x):
|
| 87 |
+
x = x + self.attn(self.norm1(x))
|
| 88 |
+
x = x + self.mlp(self.norm2(x))
|
| 89 |
+
return x
|
| 90 |
+
|
| 91 |
+
class MeteorMiniCodeForCausalLM(nn.Module):
|
| 92 |
+
def __init__(self, config):
|
| 93 |
+
super().__init__()
|
| 94 |
+
self.config = config
|
| 95 |
+
self.embed = nn.Embedding(config["vocab_size"], config["hidden_size"])
|
| 96 |
+
self.layers = nn.ModuleList([MeteorBlock(config) for _ in range(config["num_hidden_layers"])])
|
| 97 |
+
self.norm = RMSNorm(config["hidden_size"])
|
| 98 |
+
self.lm_head = nn.Linear(config["hidden_size"], config["vocab_size"], bias=False)
|
| 99 |
+
|
| 100 |
+
def forward(self, input_ids, labels=None):
|
| 101 |
+
x = self.embed(input_ids)
|
| 102 |
+
for layer in self.layers:
|
| 103 |
+
x = layer(x)
|
| 104 |
+
x = self.norm(x)
|
| 105 |
+
logits = self.lm_head(x)
|
| 106 |
+
loss = None
|
| 107 |
+
if labels is not None:
|
| 108 |
+
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), labels.view(-1))
|
| 109 |
+
return {"loss": loss, "logits": logits}
|
| 110 |
+
|
| 111 |
+
# Test 4B param count
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
import json
|
| 114 |
+
cfg = json.load(open("config.json"))
|
| 115 |
+
m = MeteorMiniCodeForCausalLM(cfg)
|
| 116 |
+
print(f"Meteor Mini Code: {sum(p.numel() for p in m.parameters())/1e9:.3f}B params")
|