Perdix-1.1B-Instruct / modeling_perdix.py
prismdata's picture
Perdix-1.1B-Instruct: SFT of Perdix-1.1B-Base
586d744 verified
Raw History Blame Contribute Delete
8.68 kB
"""Perdix: Differential Attention + PolyNorm ๊ธฐ๋ฐ˜ decoder-only LM (transformers ์—ฐ๋™์šฉ).
ํ•™์Šต ์ฝ”๋“œ(model.py)์˜ PerdixSLM๊ณผ ๋ชจ๋“ˆ ์ด๋ฆ„์ด ๊ฐ™์•„ state_dict๊ฐ€ ๊ทธ๋Œ€๋กœ ํ˜ธํ™˜๋œ๋‹ค.
KV ์บ์‹œ๋Š” ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜๋‹ค(์ƒ์„ฑ ์‹œ ๋งค ์Šคํ… ์ „์ฒด ์‹œํ€€์Šค๋ฅผ ๋‹ค์‹œ ๊ณ„์‚ฐ).
ํŒจ๋”ฉ ๋งˆ์Šคํฌ๋„ ์—†๋‹ค: attention_mask๋Š” ๋ฌด์‹œ๋˜๋ฉฐ causal ๋งˆ์Šคํฌ๋งŒ ์ ์šฉ๋œ๋‹ค.
"""
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import GenerationMixin, PreTrainedModel
from transformers.modeling_outputs import CausalLMOutput
from .configuration_perdix import PerdixConfig
class RMSNorm(nn.Module):
def __init__(self, dim, eps=1e-5):
super().__init__()
self.weight = nn.Parameter(torch.ones(dim))
self.eps = eps
def forward(self, x):
dtype = x.dtype
x = x.float()
x = x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
return (x * self.weight.float()).to(dtype)
class PolyNorm(nn.Module):
"""poly_norm(x) = w1*rms(x) + w2*rms(x^2) + w3*rms(x^3) + b"""
def __init__(self):
super().__init__()
self.weight = nn.Parameter(torch.full((3,), 1.0 / 3.0))
self.bias = nn.Parameter(torch.zeros(1))
@staticmethod
def _rms(x, eps=1e-6):
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + eps)
def forward(self, x):
xf = x.float()
out = (self.weight[0] * self._rms(xf)
+ self.weight[1] * self._rms(xf ** 2)
+ self.weight[2] * self._rms(xf ** 3)
+ self.bias)
return out.to(x.dtype)
def precompute_rope(head_dim, max_seq_len, theta):
inv_freq = 1.0 / (theta ** (torch.arange(0, head_dim, 2).float() / head_dim))
t = torch.arange(max_seq_len).float()
freqs = torch.outer(t, inv_freq)
return torch.cos(freqs), torch.sin(freqs)
def apply_rope(x, cos, sin):
# x: (B, H, T, D) -> ์ง/ํ™€ ์„ฑ๋ถ„ ํšŒ์ „
x1, x2 = x[..., 0::2], x[..., 1::2]
T = x.shape[-2]
cos, sin = cos[:T].to(x.dtype), sin[:T].to(x.dtype)
out = torch.empty_like(x)
out[..., 0::2] = x1 * cos - x2 * sin
out[..., 1::2] = x1 * sin + x2 * cos
return out
class DifferentialAttention(nn.Module):
"""๋‘ ๊ฐœ์˜ ์–ดํ…์…˜ ๋งต ์ฐจ์ด๋กœ ๋…ธ์ด์ฆˆ๋ฅผ ์ƒ์‡„ํ•˜๋Š” ์–ดํ…์…˜.
ํ‘œ์ค€ ์–ดํ…์…˜๊ณผ ๋™์ผํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜: head_dim์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ 
(Q1,K1), (Q2,K2) ๋‘ ์Œ์„ ๋งŒ๋“ค๊ณ  softmax ๋งต์„ lambda ๊ฐ€์ค‘์œผ๋กœ ๋บ€๋‹ค.
๊ฐ ํ•ญ์ด ํ‘œ์ค€ softmax(QK^T)V ๊ผด์ด๋ฏ€๋กœ SDPA(flash attention) 2ํšŒ๋กœ ๊ณ„์‚ฐ.
"""
def __init__(self, cfg, layer_idx: int):
super().__init__()
self.n_heads = cfg.n_heads
self.head_dim = cfg.dim // cfg.n_heads // 2 # ๋ฐ˜์œผ๋กœ ์ชผ๊ฐœ 2์Œ
self.wq = nn.Linear(cfg.dim, cfg.dim, bias=False)
self.wk = nn.Linear(cfg.dim, cfg.dim, bias=False)
self.wv = nn.Linear(cfg.dim, cfg.dim, bias=False)
self.wo = nn.Linear(cfg.dim, cfg.dim, bias=False)
# lambda ์žฌํŒŒ๋ผ๋ฏธํ„ฐํ™” (Differential Transformer eq.2)
self.lambda_init = 0.8 - 0.6 * math.exp(-0.3 * layer_idx)
d = self.head_dim
self.lambda_q1 = nn.Parameter(torch.randn(d) * 0.1)
self.lambda_k1 = nn.Parameter(torch.randn(d) * 0.1)
self.lambda_q2 = nn.Parameter(torch.randn(d) * 0.1)
self.lambda_k2 = nn.Parameter(torch.randn(d) * 0.1)
# ํ—ค๋“œ๋ณ„ RMSNorm (๋…ผ๋ฌธ์˜ GroupNorm ์—ญํ• )
self.subln = RMSNorm(2 * self.head_dim, eps=cfg.norm_eps)
def forward(self, x, cos, sin):
B, T, C = x.shape
H, D = self.n_heads, self.head_dim
# (B, T, C) -> (B, 2H, T, D): ํ—ค๋“œ๋‹น (Q1,Q2), (K1,K2) ์Œ
q = self.wq(x).view(B, T, 2 * H, D).transpose(1, 2)
k = self.wk(x).view(B, T, 2 * H, D).transpose(1, 2)
# V(head_dim 2D)๋ฅผ D์งœ๋ฆฌ ๋‘ ํ—ค๋“œ๋กœ ํŽผ์นจ โ€” Q/K/V head_dim์„ ๋งž์ถฐ์•ผ
# flash attention ์ปค๋„ ์ž๊ฒฉ์ด ๋˜๊ณ (math ํด๋ฐฑ ๋ฐฉ์ง€), ๊ฒฐ๊ณผ๋Š” ์ˆ˜ํ•™์ ์œผ๋กœ ๋™์ผ
v = self.wv(x).view(B, T, 2 * H, D).transpose(1, 2)
q = apply_rope(q, cos, sin)
k = apply_rope(k, cos, sin)
q1, q2 = q[:, 0::2], q[:, 1::2] # ๊ฐ (B, H, T, D)
k1, k2 = k[:, 0::2], k[:, 1::2]
# ๊ฐ™์€ ์–ดํ…์…˜ ๋งต์„ V์˜ ๋‘ ๋ฐ˜์ชฝ์— ์ ์šฉํ•˜๋„๋ก ํ—ค๋“œ ์ถ•์œผ๋กœ ๋ณต์ œ
q1r = q1.repeat_interleave(2, dim=1) # (B, 2H, T, D)
k1r = k1.repeat_interleave(2, dim=1)
q2r = q2.repeat_interleave(2, dim=1)
k2r = k2.repeat_interleave(2, dim=1)
a1 = F.scaled_dot_product_attention(q1r, k1r, v, is_causal=True)
a2 = F.scaled_dot_product_attention(q2r, k2r, v, is_causal=True)
# (B, 2H, T, D) -> (B, H, T, 2D) ๋ณต์›
a1 = a1.view(B, H, 2, T, D).permute(0, 1, 3, 2, 4).reshape(B, H, T, 2 * D)
a2 = a2.view(B, H, 2, T, D).permute(0, 1, 3, 2, 4).reshape(B, H, T, 2 * D)
lam1 = torch.exp((self.lambda_q1 * self.lambda_k1).sum().float())
lam2 = torch.exp((self.lambda_q2 * self.lambda_k2).sum().float())
lam = (lam1 - lam2 + self.lambda_init).to(x.dtype)
attn = a1 - lam * a2 # (B, H, T, 2D)
attn = self.subln(attn) * (1.0 - self.lambda_init)
attn = attn.transpose(1, 2).reshape(B, T, C)
return self.wo(attn)
class FeedForward(nn.Module):
"""Linear -> PolyNorm -> Linear (Motif ๊ทธ๋ฆผ 1์˜ ๋น„๊ฒŒ์ดํŠธ FFN)"""
def __init__(self, cfg):
super().__init__()
self.up = nn.Linear(cfg.dim, cfg.ffn_dim, bias=False)
self.act = PolyNorm()
self.down = nn.Linear(cfg.ffn_dim, cfg.dim, bias=False)
def forward(self, x):
return self.down(self.act(self.up(x)))
class Block(nn.Module):
def __init__(self, cfg, layer_idx: int):
super().__init__()
self.attn_norm = RMSNorm(cfg.dim, cfg.norm_eps)
self.attn = DifferentialAttention(cfg, layer_idx)
self.ffn_norm = RMSNorm(cfg.dim, cfg.norm_eps)
self.ffn = FeedForward(cfg)
def forward(self, x, cos, sin):
x = x + self.attn(self.attn_norm(x), cos, sin)
x = x + self.ffn(self.ffn_norm(x))
return x
class PerdixForCausalLM(PreTrainedModel, GenerationMixin):
config_class = PerdixConfig
base_model_prefix = ""
# transformers 5.x๋Š” {๋ฌถ์ด๋Š” ํ‚ค: ์›๋ณธ ํ‚ค} dict, 4.x๋Š” ํ‚ค ๋ชฉ๋ก์„ ๊ธฐ๋Œ€ํ•œ๋‹ค. dict๋Š” ์–‘์ชฝ ๋‹ค ๋™์ž‘.
_tied_weights_keys = {"lm_head.weight": "tok_emb.weight"}
_supports_cache_class = False
def __init__(self, config: PerdixConfig):
super().__init__(config)
self.tok_emb = nn.Embedding(config.vocab_size, config.dim)
self.blocks = nn.ModuleList(
[Block(config, i) for i in range(config.n_layers)])
self.final_norm = RMSNorm(config.dim, config.norm_eps)
self.lm_head = nn.Linear(config.dim, config.vocab_size, bias=False)
# RoPE ํ‘œ๋Š” buffer๋กœ ๋‘์ง€ ์•Š๊ณ  ์ฒซ forward์—์„œ ๋งŒ๋“ ๋‹ค. transformers 5.x๋Š” ๋ชจ๋ธ์„ meta ์žฅ์น˜์—์„œ
# ๋งŒ๋“  ๋’ค ๊ฐ€์ค‘์น˜๋งŒ ์ฑ„์šฐ๋ฏ€๋กœ, ์ €์žฅ๋˜์ง€ ์•Š๋Š”(non-persistent) buffer๋Š” ๋‚ด์šฉ์ด ๋‚ ์•„๊ฐ„๋‹ค.
self._rope = None
self.post_init()
def _init_weights(self, m):
if isinstance(m, (nn.Linear, nn.Embedding)):
nn.init.normal_(m.weight, mean=0.0, std=self.config.init_std)
def get_input_embeddings(self):
return self.tok_emb
def set_input_embeddings(self, value):
self.tok_emb = value
def get_output_embeddings(self):
return self.lm_head
def set_output_embeddings(self, value):
self.lm_head = value
def forward(self, input_ids=None, attention_mask=None, labels=None, **kwargs):
x = self.tok_emb(input_ids)
if self._rope is None or self._rope[0].device != x.device:
head_dim = self.config.dim // self.config.n_heads // 2
cos, sin = precompute_rope(head_dim, self.config.max_seq_len, self.config.rope_theta)
self._rope = (cos.to(x.device), sin.to(x.device))
for blk in self.blocks:
x = blk(x, *self._rope)
logits = self.lm_head(self.final_norm(x))
loss = None
if labels is not None:
loss = F.cross_entropy(
logits[:, :-1].float().reshape(-1, logits.size(-1)),
labels[:, 1:].reshape(-1), ignore_index=-100)
return CausalLMOutput(loss=loss, logits=logits)
def prepare_inputs_for_generation(self, input_ids, **kwargs):
# ์บ์‹œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋งค ์Šคํ… ์ตœ๊ทผ max_seq_len ํ† ํฐ ์ „์ฒด๋ฅผ ๋‹ค์‹œ ๋„ฃ๋Š”๋‹ค
return {"input_ids": input_ids[:, -self.config.max_seq_len:]}