File size: 4,773 Bytes
aaf22a7 | 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 | # ---------- mkd_core.py ----------
# (์ฃผ์ํ์ฌ์ ์ผ์ด๋ | MKD)
# MKDCore Transformer โ Optimized for 10GB GPU
# Author: MKD AI Systems
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
import torch
from torch import nn
from tokenizers import Tokenizer
# ============================================================
# CONFIGURATION
# ============================================================
SEQ_LEN = 1024 # Context window (tokens)
D_MODEL = 1024 # Hidden dimension
N_HEADS = 16 # Attention heads
N_LAYERS = 12 # Transformer depth
FF_DIM = D_MODEL * 4 # Feed-forward expansion
DROPOUT = 0.1 # Regularization rate
# ============================================================
# ๐ค TOKENIZER WRAPPER
# ============================================================
class MyTokenizer:
"""
Wrapper for your trained BPE tokenizer (prod_tokenizer.json)
Adds <|im_start|> and <|im_end|> for chat/sequence training.
"""
def __init__(self, json_path: str):
self.tok = Tokenizer.from_file(json_path)
def encode(self, text: str, add_special: bool = True) -> list[int]:
if add_special:
text = "<|im_start|>" + text + "<|im_end|>"
return self.tok.encode(text).ids
def decode(self, ids: list[int]) -> str:
return self.tok.decode(ids, skip_special_tokens=True)
@property
def vocab_size(self) -> int:
return self.tok.get_vocab_size()
# ============================================================
# MODEL COMPONENTS
# ============================================================
class FeedForward(nn.Module):
"""Feed-Forward Network (FFN) with GELU activation"""
def __init__(self, dim: int):
super().__init__()
self.net = nn.Sequential(
nn.Linear(dim, FF_DIM),
nn.GELU(),
nn.Linear(FF_DIM, dim),
nn.Dropout(DROPOUT)
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.net(x)
class MultiHeadAttention(nn.Module):
"""Scaled Dot-Product Multi-Head Attention"""
def __init__(self, dim: int, heads: int):
super().__init__()
self.heads = heads
self.scale = (dim // heads) ** -0.5
self.qkv = nn.Linear(dim, dim * 3)
self.proj = nn.Linear(dim, dim)
self.dropout = nn.Dropout(DROPOUT)
def forward(self, x: torch.Tensor) -> torch.Tensor:
B, T, C = x.shape
qkv = self.qkv(x).chunk(3, dim=-1)
q, k, v = [t.view(B, T, self.heads, C // self.heads).transpose(1, 2) for t in qkv]
attn = (q @ k.transpose(-2, -1)) * self.scale
attn = attn.softmax(dim=-1)
attn = self.dropout(attn)
out = (attn @ v).transpose(1, 2).contiguous().view(B, T, C)
return self.proj(out)
class Block(nn.Module):
"""Transformer block: Attention + FeedForward"""
def __init__(self, dim: int, heads: int):
super().__init__()
self.norm1 = nn.LayerNorm(dim)
self.attn = MultiHeadAttention(dim, heads)
self.norm2 = nn.LayerNorm(dim)
self.ff = FeedForward(dim)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = x + self.attn(self.norm1(x))
x = x + self.ff(self.norm2(x))
return x
# ============================================================
# ๐ง MKDCore TRANSFORMER MODEL
# ============================================================
class MKDCore(nn.Module):
"""MKDCore Transformer Language Model"""
def __init__(self, vocab_size: int):
super().__init__()
self.token_emb = nn.Embedding(vocab_size, D_MODEL)
self.pos_emb = nn.Embedding(SEQ_LEN, D_MODEL)
self.drop = nn.Dropout(DROPOUT)
self.layers = nn.ModuleList([Block(D_MODEL, N_HEADS) for _ in range(N_LAYERS)])
self.norm = nn.LayerNorm(D_MODEL)
self.head = nn.Linear(D_MODEL, vocab_size, bias=False)
def forward(self, x: torch.Tensor) -> torch.Tensor:
B, T = x.shape
pos = torch.arange(0, T, device=x.device).unsqueeze(0)
x = self.token_emb(x) + self.pos_emb(pos)
x = self.drop(x)
for layer in self.layers:
x = layer(x)
x = self.norm(x)
logits = self.head(x)
return logits
# ============================================================
# โ
TEST (Run only if executed directly)
# ============================================================
if __name__ == "__main__":
print("๐ Testing MKDCore model...")
vocab_size = 32000
model = MKDCore(vocab_size=vocab_size)
dummy = torch.randint(0, vocab_size, (2, 64)) # batch=2, seq=64
out = model(dummy)
print(f"โ
Output shape: {out.shape}") # [2, 64, 32000]
|