Upload 9 files
Browse files- config.json +21 -0
- config_minillama.py +33 -0
- merges.txt +0 -0
- model.safetensors +3 -0
- modeling_minillama.py +170 -0
- special_tokens_map.json +6 -0
- tokenizer.json +0 -0
- tokenizer_config.json +21 -0
- vocab.json +0 -0
config.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"MiniLlama"
|
| 4 |
+
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "config_minillama.MiniLlamaConfig",
|
| 7 |
+
"AutoModel": "modeling_minillama.MiniLlama"
|
| 8 |
+
},
|
| 9 |
+
"dim": 768,
|
| 10 |
+
"dropout": 0.1,
|
| 11 |
+
"dtype": "float32",
|
| 12 |
+
"max_seq_len": 1024,
|
| 13 |
+
"model_type": "mini-llama",
|
| 14 |
+
"multiple_of": 256,
|
| 15 |
+
"n_heads": 12,
|
| 16 |
+
"n_kv_heads": 12,
|
| 17 |
+
"n_layers": 24,
|
| 18 |
+
"norm_eps": 1e-05,
|
| 19 |
+
"transformers_version": "4.57.3",
|
| 20 |
+
"vocab_size": 50257
|
| 21 |
+
}
|
config_minillama.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoConfig, PretrainedConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class MiniLlamaConfig(PretrainedConfig):
|
| 5 |
+
model_type = "mini-llama"
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
*,
|
| 10 |
+
vocab_size: int = 32000,
|
| 11 |
+
dim: int = 512, # Embedding dimension
|
| 12 |
+
n_layers: int = 8, # Number of transformer blocks
|
| 13 |
+
n_heads: int = 8, # Attention heads
|
| 14 |
+
n_kv_heads: int = 8, # Key/Value heads (for Grouped Query Attention)
|
| 15 |
+
multiple_of: int = 256, # For SwiGLU hidden layer size
|
| 16 |
+
norm_eps: float = 1e-5,
|
| 17 |
+
max_seq_len: int = 1024,
|
| 18 |
+
dropout: float = 0.0,
|
| 19 |
+
**kwargs,
|
| 20 |
+
):
|
| 21 |
+
super().__init__(**kwargs)
|
| 22 |
+
self.vocab_size = vocab_size
|
| 23 |
+
self.dim = dim
|
| 24 |
+
self.n_layers = n_layers
|
| 25 |
+
self.n_heads = n_heads
|
| 26 |
+
self.n_kv_heads = n_kv_heads
|
| 27 |
+
self.multiple_of = multiple_of
|
| 28 |
+
self.norm_eps = norm_eps
|
| 29 |
+
self.max_seq_len = max_seq_len
|
| 30 |
+
self.dropout = dropout
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
MiniLlamaConfig.register_for_auto_class(AutoConfig)
|
merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3acc4452ce5fa76c069a81303eb73bbca5534bc2b2888ebd453d441943312968
|
| 3 |
+
size 988429216
|
modeling_minillama.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
# We use relative references here because this file is a training artifact
|
| 6 |
+
# Hard references will prevent it from being dynamically loaded correctly
|
| 7 |
+
# ideally it should only reference the config and nothing else in our project
|
| 8 |
+
from .config_minillama import MiniLlamaConfig
|
| 9 |
+
from transformers import AutoModel, PreTrainedModel
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def precompute_freqs_cis(dim: int, end: int, theta: float = 10000.0):
|
| 13 |
+
freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim))
|
| 14 |
+
t = torch.arange(end, device=freqs.device)
|
| 15 |
+
freqs = torch.outer(t, freqs).float()
|
| 16 |
+
freqs_cis = torch.polar(torch.ones_like(freqs), freqs) # complex64
|
| 17 |
+
return freqs_cis
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def apply_rotary_emb(xq, xk, freqs_cis):
|
| 21 |
+
# Reshape as complex numbers
|
| 22 |
+
xq_ = torch.view_as_complex(xq.float().reshape(*xq.shape[:-1], -1, 2))
|
| 23 |
+
xk_ = torch.view_as_complex(xk.float().reshape(*xk.shape[:-1], -1, 2))
|
| 24 |
+
|
| 25 |
+
# Align shapes for broadcasting
|
| 26 |
+
freqs_cis = freqs_cis.view(1, xq_.size(1), 1, xq_.size(-1))
|
| 27 |
+
|
| 28 |
+
# Rotate!
|
| 29 |
+
xq_out = torch.view_as_real(xq_ * freqs_cis).flatten(3)
|
| 30 |
+
xk_out = torch.view_as_real(xk_ * freqs_cis).flatten(3)
|
| 31 |
+
return xq_out.type_as(xq), xk_out.type_as(xk)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class RMSNorm(nn.Module):
|
| 35 |
+
def __init__(self, dim: int, eps: float = 1e-6):
|
| 36 |
+
super().__init__()
|
| 37 |
+
self.eps = eps
|
| 38 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 39 |
+
|
| 40 |
+
def _norm(self, x):
|
| 41 |
+
# rsqrt = reciprocal square root (1 / sqrt(x))
|
| 42 |
+
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
| 43 |
+
|
| 44 |
+
def forward(self, x):
|
| 45 |
+
return self._norm(x.float()).type_as(x) * self.weight
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class Attention(nn.Module):
|
| 49 |
+
def __init__(self, args: MiniLlamaConfig):
|
| 50 |
+
super().__init__()
|
| 51 |
+
self.n_heads = args.n_heads
|
| 52 |
+
self.n_kv_heads = args.n_kv_heads
|
| 53 |
+
if args.dim % args.n_heads != 0:
|
| 54 |
+
raise ValueError("Model dimension must be divisible by number of heads.")
|
| 55 |
+
head_dim = args.dim // args.n_heads
|
| 56 |
+
if head_dim % 2 != 0:
|
| 57 |
+
raise ValueError("Head dimension must be even to apply rotary embeddings.")
|
| 58 |
+
if args.n_kv_heads > args.n_heads:
|
| 59 |
+
raise ValueError("n_kv_heads must be less than or equal to n_heads.")
|
| 60 |
+
if args.n_heads % args.n_kv_heads != 0:
|
| 61 |
+
raise ValueError("n_heads must be divisible by n_kv_heads for GQA.")
|
| 62 |
+
self.head_dim = head_dim
|
| 63 |
+
|
| 64 |
+
self.wq = nn.Linear(args.dim, args.n_heads * self.head_dim, bias=False)
|
| 65 |
+
self.wk = nn.Linear(args.dim, self.n_kv_heads * self.head_dim, bias=False)
|
| 66 |
+
self.wv = nn.Linear(args.dim, self.n_kv_heads * self.head_dim, bias=False)
|
| 67 |
+
self.wo = nn.Linear(args.n_heads * self.head_dim, args.dim, bias=False)
|
| 68 |
+
|
| 69 |
+
def forward(self, x, freqs_cis, mask=None):
|
| 70 |
+
bsz, seqlen, _ = x.shape
|
| 71 |
+
xq, xk, xv = self.wq(x), self.wk(x), self.wv(x)
|
| 72 |
+
|
| 73 |
+
# Reshape for heads
|
| 74 |
+
xq = xq.view(bsz, seqlen, self.n_heads, self.head_dim)
|
| 75 |
+
xk = xk.view(bsz, seqlen, self.n_kv_heads, self.head_dim)
|
| 76 |
+
xv = xv.view(bsz, seqlen, self.n_kv_heads, self.head_dim)
|
| 77 |
+
|
| 78 |
+
# Apply RoPE
|
| 79 |
+
xq, xk = apply_rotary_emb(xq, xk, freqs_cis)
|
| 80 |
+
|
| 81 |
+
# GQA: Repeat keys/values if n_kv_heads < n_heads
|
| 82 |
+
if self.n_kv_heads < self.n_heads:
|
| 83 |
+
xk = torch.repeat_interleave(xk, self.n_heads // self.n_kv_heads, dim=2)
|
| 84 |
+
xv = torch.repeat_interleave(xv, self.n_heads // self.n_kv_heads, dim=2)
|
| 85 |
+
|
| 86 |
+
# Attention Calculation
|
| 87 |
+
xq, xk, xv = xq.transpose(1, 2), xk.transpose(1, 2), xv.transpose(1, 2)
|
| 88 |
+
scores = torch.matmul(xq, xk.transpose(2, 3)) / math.sqrt(self.head_dim)
|
| 89 |
+
if mask is not None:
|
| 90 |
+
scores = scores + mask
|
| 91 |
+
output = torch.matmul(F.softmax(scores.float(), dim=-1).type_as(xq), xv)
|
| 92 |
+
|
| 93 |
+
return self.wo(output.transpose(1, 2).contiguous().view(bsz, seqlen, -1))
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class FeedForward(nn.Module):
|
| 97 |
+
def __init__(self, dim, hidden_dim, multiple_of):
|
| 98 |
+
super().__init__()
|
| 99 |
+
hidden_dim = int(2 * hidden_dim / 3)
|
| 100 |
+
hidden_dim = multiple_of * ((hidden_dim + multiple_of - 1) // multiple_of)
|
| 101 |
+
|
| 102 |
+
self.w1 = nn.Linear(dim, hidden_dim, bias=False)
|
| 103 |
+
self.w2 = nn.Linear(hidden_dim, dim, bias=False)
|
| 104 |
+
self.w3 = nn.Linear(dim, hidden_dim, bias=False)
|
| 105 |
+
|
| 106 |
+
def forward(self, x):
|
| 107 |
+
return self.w2(F.silu(self.w1(x)) * self.w3(x))
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
class TransformerBlock(nn.Module):
|
| 111 |
+
def __init__(self, layer_id, args):
|
| 112 |
+
super().__init__()
|
| 113 |
+
self.layer_id = layer_id
|
| 114 |
+
self.attention = Attention(args)
|
| 115 |
+
self.feed_forward = FeedForward(args.dim, 4 * args.dim, args.multiple_of)
|
| 116 |
+
self.attention_norm = RMSNorm(args.dim, eps=args.norm_eps)
|
| 117 |
+
self.ffn_norm = RMSNorm(args.dim, eps=args.norm_eps)
|
| 118 |
+
self.dropout = nn.Dropout(args.dropout)
|
| 119 |
+
|
| 120 |
+
def forward(self, x, freqs_cis, mask):
|
| 121 |
+
h = x + self.dropout(self.attention(self.attention_norm(x), freqs_cis, mask))
|
| 122 |
+
out = h + self.dropout(self.feed_forward(self.ffn_norm(h)))
|
| 123 |
+
return out
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
class MiniLlama(PreTrainedModel):
|
| 127 |
+
config_class = MiniLlamaConfig
|
| 128 |
+
|
| 129 |
+
def __init__(self, config: MiniLlamaConfig):
|
| 130 |
+
super().__init__(config)
|
| 131 |
+
self.config = config
|
| 132 |
+
self.tok_embeddings = nn.Embedding(config.vocab_size, config.dim)
|
| 133 |
+
self.layers = nn.ModuleList(
|
| 134 |
+
[TransformerBlock(i, config) for i in range(config.n_layers)]
|
| 135 |
+
)
|
| 136 |
+
self.norm = RMSNorm(config.dim, eps=config.norm_eps)
|
| 137 |
+
self.output = nn.Linear(config.dim, config.vocab_size, bias=False)
|
| 138 |
+
|
| 139 |
+
# Precompute RoPE
|
| 140 |
+
self.freqs_cis = precompute_freqs_cis(
|
| 141 |
+
config.dim // config.n_heads, config.max_seq_len * 2
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
def forward(self, tokens, targets=None):
|
| 145 |
+
bsz, seqlen = tokens.shape
|
| 146 |
+
h = self.tok_embeddings(tokens)
|
| 147 |
+
freqs_cis = self.freqs_cis[:seqlen].to(h.device)
|
| 148 |
+
|
| 149 |
+
mask = None
|
| 150 |
+
if seqlen > 1:
|
| 151 |
+
mask = torch.full((seqlen, seqlen), float("-inf"), device=tokens.device)
|
| 152 |
+
# Causal mask
|
| 153 |
+
mask = torch.triu(mask, diagonal=1).unsqueeze(0).unsqueeze(0)
|
| 154 |
+
|
| 155 |
+
for layer in self.layers:
|
| 156 |
+
h = layer(h, freqs_cis, mask)
|
| 157 |
+
|
| 158 |
+
logits = self.output(self.norm(h))
|
| 159 |
+
|
| 160 |
+
if targets is not None:
|
| 161 |
+
# Add loss calculation to make post training easy.
|
| 162 |
+
# You don't have to do this
|
| 163 |
+
return logits, F.cross_entropy(
|
| 164 |
+
logits.view(-1, self.config.vocab_size),
|
| 165 |
+
targets.view(-1),
|
| 166 |
+
)
|
| 167 |
+
return logits, None
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
MiniLlama.register_for_auto_class(AutoModel)
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": "<|endoftext|>",
|
| 3 |
+
"eos_token": "<|endoftext|>",
|
| 4 |
+
"pad_token": "<|endoftext|>",
|
| 5 |
+
"unk_token": "<|endoftext|>"
|
| 6 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"added_tokens_decoder": {
|
| 4 |
+
"50256": {
|
| 5 |
+
"content": "<|endoftext|>",
|
| 6 |
+
"lstrip": false,
|
| 7 |
+
"normalized": true,
|
| 8 |
+
"rstrip": false,
|
| 9 |
+
"single_word": false,
|
| 10 |
+
"special": true
|
| 11 |
+
}
|
| 12 |
+
},
|
| 13 |
+
"bos_token": "<|endoftext|>",
|
| 14 |
+
"clean_up_tokenization_spaces": false,
|
| 15 |
+
"eos_token": "<|endoftext|>",
|
| 16 |
+
"extra_special_tokens": {},
|
| 17 |
+
"model_max_length": 1024,
|
| 18 |
+
"pad_token": "<|endoftext|>",
|
| 19 |
+
"tokenizer_class": "GPT2Tokenizer",
|
| 20 |
+
"unk_token": "<|endoftext|>"
|
| 21 |
+
}
|
vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|