|
import torch |
|
import torch.nn as nn |
|
import torch.nn.functional as F |
|
from torch.nn import SiLU |
|
import yaml |
|
|
|
|
|
def _init_weights(module, std=0.041666666666666664): |
|
if isinstance(module, nn.Linear): |
|
module.weight.data.normal_(mean=0.0, std=std) |
|
elif isinstance(module, nn.Embedding): |
|
module.weight.data.normal_(mean=0.0, std=std) |
|
|
|
class RotaryPositionalEmbedding(nn.Module): |
|
""" |
|
# https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/modeling_llama.py#L240 |
|
Rotary Positional Embedding (RoPE) for transformers Implemntation derived from https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/modeling_llama.py |
|
""" |
|
def __init__(self, dim: int, theta: float = 10000.0): |
|
super().__init__() |
|
self.dim = dim |
|
self.theta = theta |
|
|
|
def forward(self, x: torch.Tensor, seq_len: int) -> torch.Tensor: |
|
""" |
|
Apply rotary positional embedding to the input tensor. |
|
|
|
Args: |
|
x (torch.Tensor): Input tensor of shape [B, T, H, D] or [B, T, D] |
|
seq_len (int): Sequence length. |
|
|
|
Returns: |
|
torch.Tensor: Output tensor with rotary positional embeddings applied. |
|
""" |
|
|
|
if len(x.shape) == 3: |
|
B, T, D = x.shape |
|
is_4d = False |
|
else: |
|
B, T, H, D = x.shape |
|
is_4d = True |
|
|
|
|
|
if not is_4d and D % 2 != 0: |
|
raise ValueError(f"Feature dimension {D} must be divisible by 2 for RoPE") |
|
|
|
|
|
position = torch.arange(T, dtype=torch.float32, device=x.device).unsqueeze(-1) |
|
|
|
|
|
if is_4d: |
|
|
|
freqs = torch.exp( |
|
torch.arange(0, D, 2, dtype=torch.float32, device=x.device) * |
|
-(torch.log(torch.tensor(self.theta)) / D) |
|
) |
|
else: |
|
|
|
freqs = torch.exp( |
|
torch.arange(0, D, 2, dtype=torch.float32, device=x.device) * |
|
-(torch.log(torch.tensor(self.theta)) / D) |
|
) |
|
|
|
|
|
sinusoid = position * freqs |
|
sin = torch.sin(sinusoid) |
|
cos = torch.cos(sinusoid) |
|
|
|
|
|
if is_4d: |
|
sin = sin.unsqueeze(0).unsqueeze(2) |
|
cos = cos.unsqueeze(0).unsqueeze(2) |
|
else: |
|
sin = sin.unsqueeze(0) |
|
cos = cos.unsqueeze(0) |
|
|
|
|
|
x_rotated = x.clone() |
|
|
|
if is_4d: |
|
x_rotated[..., 0::2] = x[..., 0::2] * cos - x[..., 1::2] * sin |
|
x_rotated[..., 1::2] = x[..., 1::2] * cos + x[..., 0::2] * sin |
|
else: |
|
x_rotated[..., 0::2] = x[..., 0::2] * cos - x[..., 1::2] * sin |
|
x_rotated[..., 1::2] = x[..., 1::2] * cos + x[..., 0::2] * sin |
|
|
|
return x_rotated |
|
|
|
class MultiHeadLatentAttention(nn.Module): |
|
def __init__(self, config): |
|
super().__init__() |
|
self.config = config |
|
self.num_attention_heads = self.config['num_attention_heads'] |
|
self.hidden_size = self.config['hidden_size'] |
|
|
|
if self.hidden_size % self.num_attention_heads != 0: |
|
raise ValueError( |
|
f"hidden_size ({self.hidden_size}) must be divisible by num_attention_heads ({self.num_attention_heads})" |
|
) |
|
self.head_dim = self.hidden_size // self.num_attention_heads |
|
self.latent_dim = self.hidden_size // self.config['compression_ratio'] |
|
|
|
|
|
|
|
self.kv_proj_D = nn.Linear(self.hidden_size, self.latent_dim, bias=False) |
|
|
|
self.q_proj_D = nn.Linear(self.hidden_size, self.latent_dim, bias=False) |
|
|
|
|
|
self.k_proj_U = nn.Linear(self.latent_dim, self.hidden_size//2, bias=False) |
|
|
|
self.v_proj_U = nn.Linear(self.latent_dim, self.hidden_size, bias=False) |
|
|
|
self.q_proj_U = nn.Linear(self.latent_dim, self.hidden_size//2, bias=False) |
|
|
|
|
|
self.rope_k = nn.Linear(self.hidden_size, self.hidden_size//2, bias=False) |
|
self.rope_q = nn.Linear(self.latent_dim, self.hidden_size//2, bias=False) |
|
|
|
self.o_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False) |
|
|
|
self.rotary_emb = RotaryPositionalEmbedding(self.hidden_size//2, self.config['rope_theta']) |
|
|
|
def forward(self, x, attn_mask=None): |
|
B, T, C = x.size() |
|
|
|
kv_d = self.kv_proj_D(x) |
|
|
|
q_d = self.q_proj_D(x) |
|
|
|
k_proj_2 = self.k_proj_U(kv_d) |
|
q_proj_2 = self.q_proj_U(q_d) |
|
v = self.v_proj_U(kv_d) |
|
|
|
|
|
k_rope_2 = self.rope_k(x) |
|
q_rope_2 = self.rope_q(q_d) |
|
|
|
|
|
k_rope_2 = self.rotary_emb(k_rope_2, T) |
|
q_rope_2 = self.rotary_emb(q_rope_2, T) |
|
|
|
|
|
k_proj_2 = k_proj_2.view(B, T, self.num_attention_heads, self.head_dim//2) |
|
k_rope_2 = k_rope_2.view(B, T, self.num_attention_heads, self.head_dim//2) |
|
q_proj_2 = q_proj_2.view(B, T, self.num_attention_heads, self.head_dim//2) |
|
q_rope_2 = q_rope_2.view(B, T, self.num_attention_heads, self.head_dim//2) |
|
|
|
|
|
k = torch.cat((k_proj_2, k_rope_2), dim=-1) |
|
q = torch.cat((q_proj_2, q_rope_2), dim=-1) |
|
v = v.view(B, T, self.num_attention_heads, self.head_dim) |
|
|
|
|
|
k = k.transpose(1, 2) |
|
q = q.transpose(1, 2) |
|
v = v.transpose(1, 2) |
|
|
|
|
|
attn_out = F.scaled_dot_product_attention(q, k, v, |
|
dropout_p=0.0, |
|
is_causal=True, |
|
attn_mask=attn_mask) |
|
attn_out = attn_out.transpose(1, 2).contiguous().view(B, T, C) |
|
return self.o_proj(attn_out) |
|
|
|
class DeepSeekExpertLayer(nn.Module): |
|
def __init__(self, hidden_size, intermediate_size): |
|
super().__init__() |
|
self.hidden_size = hidden_size |
|
self.intermediate_size = intermediate_size |
|
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False) |
|
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False) |
|
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False) |
|
self.act_fn = SiLU() |
|
|
|
def forward(self, x): |
|
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x)) |
|
|
|
class DeepSeekMOE(nn.Module): |
|
""" |
|
A Mixture of Experts (MoE) layer that routes input through a set of expert layers. |
|
|
|
This class implements a mixture of experts mechanism where a subset of experts is selected |
|
for each input token based on learned routing logits. The output is a combination of the |
|
shared experts and the routed experts, allowing for efficient computation and increased |
|
model capacity. |
|
|
|
Attributes: |
|
hidden_size (int): The size of the hidden layer. |
|
intermediate_size (int): The size of the intermediate layer. |
|
num_experts (int): Total number of experts available. |
|
num_shared_experts (int): Number of shared experts that are used for all inputs. |
|
top_k (int): The number of top experts to route each input to. |
|
shared_experts (nn.ModuleList): List of shared expert layers. |
|
routed_experts (nn.ModuleList): List of routed expert layers. |
|
routing_fn (nn.Linear): Linear layer for computing routing logits. |
|
routing_bias (nn.Parameter): Bias for the routing logits. |
|
|
|
Methods: |
|
forward(x): Forward pass through the MoE layer, routing input through selected experts. |
|
""" |
|
def __init__(self, hidden_size, intermediate_size, num_experts, num_shared_experts, top_k): |
|
super().__init__() |
|
self.hidden_size = hidden_size |
|
self.intermediate_size = intermediate_size |
|
self.num_experts = num_experts |
|
self.num_shared_experts = num_shared_experts |
|
self.top_k = top_k |
|
self.num_routed_experts = num_experts - num_shared_experts |
|
self.shared_experts = nn.ModuleList( |
|
[DeepSeekExpertLayer(self.hidden_size, self.intermediate_size) for _ in range(self.num_shared_experts)] |
|
) |
|
self.routed_experts = nn.ModuleList( |
|
[DeepSeekExpertLayer(self.hidden_size, self.intermediate_size) for _ in range(self.num_routed_experts)] |
|
) |
|
|
|
|
|
self.routing_fn = nn.Linear(self.hidden_size, self.num_routed_experts, bias=False) |
|
self.routing_bias = nn.Parameter(torch.zeros(self.num_routed_experts)) |
|
def forward(self, x): |
|
B, T, C = x.size() |
|
shared_out = sum(expert(x) for expert in self.shared_experts) |
|
if self.num_shared_experts>1: |
|
shared_out = shared_out/self.num_shared_experts |
|
|
|
routing_logits = self.routing_fn(x) + self.routing_bias |
|
|
|
routing_probs = torch.sigmoid(routing_logits) |
|
scores, indices = torch.topk(routing_probs, self.top_k, dim=-1) |
|
|
|
scores = scores/torch.sum(scores, dim=-1, keepdim=True) |
|
|
|
|
|
combined_output = torch.zeros_like(x) |
|
|
|
|
|
expert_load = torch.zeros(self.num_routed_experts, device=x.device) |
|
for i in range(self.top_k): |
|
expert_idx = indices[:, :, i] |
|
|
|
expert_scores = scores[...,i:i+1] |
|
|
|
for j in range(self.num_routed_experts): |
|
mask = (expert_idx == j) |
|
if mask.any(): |
|
|
|
expert_load[j] += mask.sum().float() / (B * T * self.top_k) |
|
|
|
expert_input = x[mask] |
|
expert_output = self.routed_experts[j](expert_input) |
|
combined_output[mask] += expert_scores[mask] * expert_output |
|
final_output = shared_out + combined_output |
|
router_z_loss = self.update_bias_terms(expert_load) |
|
return final_output, router_z_loss |
|
|
|
def update_bias_terms(self, expert_load, router_z_loss_coef=0.001): |
|
|
|
|
|
target_load = 1.0 / self.num_routed_experts |
|
|
|
|
|
load_diff = expert_load - target_load |
|
|
|
|
|
|
|
update_rate = 0.1 * torch.abs(load_diff) |
|
|
|
|
|
|
|
self.routing_bias.data -= update_rate * load_diff |
|
|
|
|
|
|
|
|
|
router_z_loss = router_z_loss_coef * torch.mean(torch.log(torch.sum( |
|
torch.exp(self.routing_fn.weight), dim=-1))) |
|
|
|
return router_z_loss |
|
|
|
def update_bias_terms_old(self, expert_load, ): |
|
|
|
target_load = 1/self.num_experts |
|
load_diff = expert_load - target_load |
|
|
|
update_rate = 0.1 * torch.abs(load_diff) |
|
|
|
self.routing_bias = self.routing_bias - update_rate * load_diff |
|
|
|
|
|
|
|
|
|
|
|
|
|
class LlamaMLP(nn.Module): |
|
""" |
|
(mlp): LlamaMLP( |
|
(moe): DeepSeekMOE( |
|
(shared_experts): ModuleList( |
|
(0): DeepSeekExpertLayer( |
|
(gate_proj): Linear(in_features=576, out_features=1536, bias=False) |
|
(up_proj): Linear(in_features=576, out_features=1536, bias=False) |
|
(down_proj): Linear(in_features=1536, out_features=576, bias=False) |
|
(act_fn): SiLU() |
|
) |
|
) |
|
(routed_experts): ModuleList( |
|
(0-2): 3 x DeepSeekExpertLayer( |
|
(gate_proj): Linear(in_features=576, out_features=1536, bias=False) |
|
(up_proj): Linear(in_features=576, out_features=1536, bias=False) |
|
(down_proj): Linear(in_features=1536, out_features=576, bias=False) |
|
(act_fn): SiLU() |
|
) |
|
) |
|
(routing_fn): Linear(in_features=576, out_features=3, bias=False) |
|
) |
|
) |
|
""" |
|
def __init__(self, config): |
|
super().__init__() |
|
self.config = config |
|
self.moe = DeepSeekMOE(hidden_size=config['hidden_size'], |
|
intermediate_size=config['intermediate_size'], |
|
num_experts=config['num_experts'], |
|
num_shared_experts= config['num_shared_experts'], |
|
top_k=config['top_k']) |
|
|
|
|
|
|
|
|
|
def forward(self, x): |
|
output, router_z_loss = self.moe(x) |
|
return output, router_z_loss |
|
|
|
|
|
|
|
|
|
|
|
class LlamaRMSNorm(nn.Module): |
|
""" |
|
(norm): LlamaRMSNorm((576,), eps=1e-05) |
|
# RMSNorm Formula: |
|
# RMS(x) = sqrt((1 / d) * sum(x_i^2 for i in range(d))) |
|
# x_normalized = x / RMS(x) |
|
# output = gamma * x_normalized |
|
|
|
""" |
|
def __init__(self, config): |
|
super().__init__() |
|
self.config = config |
|
self.eps = self.config['rms_norm_eps'] |
|
self.weight = nn.Parameter(torch.ones(self.config['hidden_size'])) |
|
def forward(self, x): |
|
rms = torch.rsqrt(torch.mean(x ** 2, dim=-1, keepdim=True) + self.eps) |
|
return self.weight *rms * x |
|
|
|
class LlamaDecoderLayer(nn.Module): |
|
def __init__(self, config): |
|
super().__init__() |
|
self.config = config |
|
self.self_attn = MultiHeadLatentAttention(self.config) |
|
self.input_layernorm = LlamaRMSNorm(self.config) |
|
self.mlp = LlamaMLP(self.config) |
|
self.post_attention_layernorm = LlamaRMSNorm(self.config) |
|
|
|
def forward(self, x): |
|
residual = x |
|
x = self.input_layernorm(x) |
|
x = self.self_attn(x) |
|
x = x + residual |
|
residual = x |
|
x = self.post_attention_layernorm(x) |
|
x, router_z_loss = self.mlp(x) |
|
x = x + residual |
|
return x, router_z_loss |
|
|
|
class DeepSeekV3Model(nn.Module): |
|
def __init__(self, config): |
|
super().__init__() |
|
self.init_method = config['init_method'] |
|
self.config = config['model_config'] |
|
self.embed_tokens = nn.Embedding(self.config['vocab_size'], self.config['hidden_size']) |
|
self.rotary_emb = RotaryPositionalEmbedding(self.config['hidden_size'], self.config['rope_theta']) |
|
self.layers = nn.ModuleList([LlamaDecoderLayer(self.config) for _ in range(self.config['num_hidden_layers'])]) |
|
self.norm = LlamaRMSNorm(self.config) |
|
self.lm_head = nn.Linear(self.config['hidden_size'], self.config['vocab_size'], bias=False) |
|
|
|
if self.config['tie_word_embeddings']: |
|
self.lm_head.weight = self.embed_tokens.weight |
|
|
|
self.apply(lambda m: _init_weights(m, self.init_method['std'])) |
|
|
|
def forward(self, x, y=None): |
|
x = self.embed_tokens(x) |
|
total_router_z_loss = 0.0 |
|
for layer in self.layers: |
|
x, router_z_loss = layer(x) |
|
total_router_z_loss += router_z_loss |
|
x = self.norm(x) |
|
logits = self.lm_head(x) |
|
logits = logits.view(-1, logits.size(-1)) |
|
if y is not None: |
|
y = y.view(-1) |
|
ce_loss = torch.nn.functional.cross_entropy(logits, y) |
|
|
|
loss = ce_loss + total_router_z_loss |
|
return logits, loss |
|
else: |
|
return logits, None |
|
|
|
|
|
def generate(self, idx, max_new_tokens, context_length, temperature=1.0, top_k=None, eos_token=None, device=None): |
|
model = self.to(device) |
|
idx = idx.to(device) |
|
model.eval() |
|
for _ in range(max_new_tokens): |
|
idx_cond = idx[:, -context_length:] |
|
with torch.no_grad(): |
|
logits, _ = model(idx_cond) |
|
logits = logits.view(idx_cond.shape[0], -1, model.config['vocab_size']) |
|
|
|
|
|
logits = logits[:, -1, :] |
|
|
|
if top_k is not None: |
|
|
|
top_logits, top_pos = torch.topk(logits, top_k) |
|
min_logit = top_logits[:, -1].unsqueeze(-1) |
|
logits = torch.where(logits < min_logit, |
|
torch.tensor(float('-inf')).to(logits.device), |
|
logits) |
|
|
|
|
|
if temperature > 0.0: |
|
logits /= temperature |
|
probs = torch.softmax(logits, dim=-1) |
|
idx_next = torch.multinomial(probs, num_samples=1) |
|
else: |
|
idx_next = torch.argmax(logits, dim=-1, keepdim=True) |
|
|
|
if idx_next.item() == eos_token: |
|
break |
|
|
|
idx = torch.cat((idx, idx_next), dim=1) |
|
model.train() |
|
return idx |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|