import math import os from typing import Optional, Tuple, Union import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.checkpoint as cp from transformers.modeling_utils import PreTrainedModel from transformers.modeling_outputs import SequenceClassifierOutput try: from .configuration_sentimental import SentimentalConfig except Exception: try: from configuration_sentimental import SentimentalConfig except Exception: from sentimental_configuration import SentimentalConfig @torch.no_grad() def get_hadamard_matrix(d: int, dtype=torch.float32) -> torch.Tensor: eye = torch.eye(d, dtype=dtype) h = 1 out = eye.clone() while h < d: out = out.view(-1, 2, h) u = out[:, 0, :] v = out[:, 1, :] out = torch.cat((u + v, u - v), dim=-2) out = out.view(d, d) h *= 2 return (out * (1.0 / math.sqrt(d))).contiguous() class HadamardMLP(nn.Module): def __init__(self, config: SentimentalConfig): super().__init__() self.dim = config.hidden_size self.scale1 = nn.Parameter(torch.ones(self.dim)) self.scale2 = nn.Parameter(torch.ones(self.dim)) self.gate = nn.Parameter(torch.ones(self.dim)) self.bias = nn.Parameter(torch.zeros(self.dim)) hadamard_mat = get_hadamard_matrix(self.dim) self.register_buffer("hadamard_mat", hadamard_mat, persistent=False) def forward(self, x: torch.Tensor) -> torch.Tensor: mat = self.hadamard_mat.type_as(x) h = (x * self.scale1) @ mat g = F.silu(x * self.gate) out = ((h * g) @ mat) * self.scale2 + self.bias return out class SwiGLUMLP(nn.Module): def __init__(self, config: SentimentalConfig): super().__init__() self.gate_proj = nn.Linear(config.hidden_size, config.intermediate_size, bias=False) self.up_proj = nn.Linear(config.hidden_size, config.intermediate_size, bias=False) self.down_proj = nn.Linear(config.intermediate_size, config.hidden_size, bias=False) def forward(self, x: torch.Tensor) -> torch.Tensor: return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x)) class EngramMemory(nn.Module): def __init__(self, config: SentimentalConfig): super().__init__() self.dim = config.hidden_size self.num_entries = config.engram_entries self.n_gram_orders = config.engram_ngram_orders self.tables = nn.ModuleList([ nn.Embedding(self.num_entries, self.dim) for _ in self.n_gram_orders ]) self.gate_proj = nn.Linear(self.dim, self.dim * len(self.n_gram_orders), bias=False) self.out_proj = nn.Linear(self.dim * len(self.n_gram_orders), self.dim, bias=False) def _hash_ngram(self, tokens: torch.Tensor, order: int, table_idx: int) -> torch.Tensor: bsz, seqlen = tokens.shape padded = F.pad(tokens, (order - 1, 0), value=0) primes = (10007, 10009, 10037, 10039, 10061, 10067) p = primes[table_idx % len(primes)] if order == 2: return (padded[:, :seqlen] * p + padded[:, 1 : seqlen + 1]) % self.num_entries elif order == 3: h = (padded[:, :seqlen] * p + padded[:, 1 : seqlen + 1]) % self.num_entries return (h * p + padded[:, 2 : seqlen + 2]) % self.num_entries else: hash_val = torch.zeros((bsz, seqlen), dtype=torch.int64, device=tokens.device) for k in range(order): tok = padded[:, k : k + seqlen] hash_val = (hash_val * p + tok) % self.num_entries return hash_val def forward(self, x: torch.Tensor, tokens: torch.Tensor) -> torch.Tensor: mem_lookups = [self.tables[i](self._hash_ngram(tokens, order, i)) for i, order in enumerate(self.n_gram_orders)] concat_mem = torch.cat(mem_lookups, dim=-1) gate = torch.sigmoid(self.gate_proj(x)) return self.out_proj(concat_mem * gate) class RMSNorm(nn.Module): def __init__(self, dim: int, eps: float = 1e-5): super().__init__() self.eps = eps self.weight = nn.Parameter(torch.ones(dim)) def forward(self, x: torch.Tensor) -> torch.Tensor: norm = torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) return x * norm * self.weight class RotaryEmbedding(nn.Module): def __init__(self, dim: int, max_position_embeddings: int = 2048, base: float = 10000.0): super().__init__() self.dim = dim self.max_position_embeddings = max_position_embeddings self.base = base inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, dtype=torch.float32) / self.dim)) self.register_buffer("inv_freq", inv_freq, persistent=False) self._set_cos_sin_cache(max_position_embeddings) def _set_cos_sin_cache(self, seq_len: int, device=None, dtype=torch.float32): t = torch.arange(seq_len, device=device, dtype=torch.float32) inv_freq = self.inv_freq.to(device=device, dtype=torch.float32) freqs = torch.outer(t, inv_freq) emb = torch.cat((freqs, freqs), dim=-1) self.register_buffer("cos_cached", emb.cos().to(dtype=dtype), persistent=False) self.register_buffer("sin_cached", emb.sin().to(dtype=dtype), persistent=False) def forward(self, seq_len: int, device: torch.device, dtype: torch.dtype = torch.float32): if not hasattr(self, "cos_cached") or seq_len > self.cos_cached.shape[0] or self.cos_cached.device != device: self._set_cos_sin_cache(seq_len, device=device, dtype=dtype) return ( self.cos_cached[:seq_len].to(device=device, dtype=dtype), self.sin_cached[:seq_len].to(device=device, dtype=dtype), ) def rotate_half(x: torch.Tensor) -> torch.Tensor: x1 = x[..., : x.shape[-1] // 2] x2 = x[..., x.shape[-1] // 2 :] return torch.cat((-x2, x1), dim=-1) def apply_rotary_pos_emb(q: torch.Tensor, k: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor): q_embed = (q * cos) + (rotate_half(q) * sin) k_embed = (k * cos) + (rotate_half(k) * sin) return q_embed, k_embed class SentimentalAttention(nn.Module): def __init__(self, config: SentimentalConfig): super().__init__() self.dim = config.hidden_size self.n_heads = config.num_attention_heads self.n_kv_heads = config.num_key_value_heads self.head_dim = config.head_dim self.num_kv_groups = self.n_heads // self.n_kv_heads self.use_xsa = config.use_xsa self.use_per_head_gating = config.use_per_head_gating self.wq = nn.Linear(self.dim, self.n_heads * self.head_dim, bias=False) self.wk = nn.Linear(self.dim, self.n_kv_heads * self.head_dim, bias=False) self.wv = nn.Linear(self.dim, self.n_kv_heads * self.head_dim, bias=False) self.wo = nn.Linear(self.n_heads * self.head_dim, self.dim, bias=False) self.q_norm = RMSNorm(self.head_dim, eps=config.rms_norm_eps) self.k_norm = RMSNorm(self.head_dim, eps=config.rms_norm_eps) if self.use_per_head_gating: self.head_gate = nn.Linear(self.dim, self.n_heads, bias=True) nn.init.constant_(self.head_gate.bias, 1.0) nn.init.zeros_(self.head_gate.weight) def forward(self, x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor, attention_mask: Optional[torch.Tensor] = None) -> torch.Tensor: bsz, seqlen, _ = x.shape xq = self.wq(x).view(bsz, seqlen, self.n_heads, self.head_dim).transpose(1, 2) xk = self.wk(x).view(bsz, seqlen, self.n_kv_heads, self.head_dim).transpose(1, 2) xv = self.wv(x).view(bsz, seqlen, self.n_kv_heads, self.head_dim).transpose(1, 2) xq = self.q_norm(xq) xk = self.k_norm(xk) xq, xk = apply_rotary_pos_emb(xq, xk, cos, sin) if self.num_kv_groups > 1: xk = xk.repeat_interleave(self.num_kv_groups, dim=1) xv_expanded = xv.repeat_interleave(self.num_kv_groups, dim=1) else: xv_expanded = xv attn_mask = None if attention_mask is not None: attn_mask = attention_mask.unsqueeze(1).unsqueeze(2).bool() if attention_mask.dim() == 2 else attention_mask attn_out = F.scaled_dot_product_attention(xq, xk, xv_expanded, attn_mask=attn_mask, is_causal=False) if self.use_xsa: vn = F.normalize(xv_expanded, p=2, dim=-1, eps=1e-6) proj = (attn_out * vn).sum(dim=-1, keepdim=True) attn_out = attn_out - proj * vn if self.use_per_head_gating: gate = torch.sigmoid(self.head_gate(x)).transpose(1, 2).unsqueeze(-1) attn_out = attn_out * gate out = attn_out.transpose(1, 2).contiguous().view(bsz, seqlen, -1) return self.wo(out) class SentimentalBlock(nn.Module): def __init__(self, config: SentimentalConfig, layer_idx: int): super().__init__() self.dim = config.hidden_size self.layer_idx = layer_idx self.attn_norm = RMSNorm(self.dim, eps=config.rms_norm_eps) self.attn = SentimentalAttention(config) self.mlp_norm = RMSNorm(self.dim, eps=config.rms_norm_eps) if config.swiglu_interval == 0: self.use_swiglu = False elif config.swiglu_interval == 1: self.use_swiglu = True else: self.use_swiglu = ((layer_idx + 1) % config.swiglu_interval == 0) if self.use_swiglu: self.mlp = SwiGLUMLP(config) else: self.mlp = HadamardMLP(config) def forward(self, x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor, attention_mask: Optional[torch.Tensor] = None) -> torch.Tensor: x = x + self.attn(self.attn_norm(x), cos, sin, attention_mask=attention_mask) x = x + self.mlp(self.mlp_norm(x)) return x class SentimentalPreTrainedModel(PreTrainedModel): config_class = SentimentalConfig base_model_prefix = "model" supports_gradient_checkpointing = True _no_split_modules = ["SentimentalBlock"] def _init_weights(self, module): std = self.config.initializer_range if isinstance(module, (nn.Linear, nn.Embedding)): module.weight.data.normal_(mean=0.0, std=std) if hasattr(module, "bias") and module.bias is not None: module.bias.data.zero_() elif isinstance(module, RMSNorm): module.weight.data.fill_(1.0) class SentimentalModel(SentimentalPreTrainedModel): def __init__(self, config: SentimentalConfig): super().__init__(config) self.config = config self.gradient_checkpointing = False self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size) if config.use_engram: self.engram = EngramMemory(config) self.engram_norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) else: self.engram = None self.engram_norm = None self.layers = nn.ModuleList([ SentimentalBlock(config, layer_idx=i) for i in range(config.num_hidden_layers) ]) self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.rotary_emb = RotaryEmbedding(config.head_dim, config.max_position_embeddings, config.rope_theta) self.post_init() def forward(self, input_ids=None, attention_mask=None, position_ids=None, inputs_embeds=None, **kwargs): if input_ids is not None: bsz, seqlen = input_ids.shape h0 = self.embed_tokens(input_ids) tokens_for_engram = input_ids elif inputs_embeds is not None: bsz, seqlen, _ = inputs_embeds.shape h0 = inputs_embeds tokens_for_engram = torch.zeros((bsz, seqlen), dtype=torch.long, device=inputs_embeds.device) else: raise ValueError("You must specify either input_ids or inputs_embeds") if self.engram is not None: engram_out = self.engram(self.engram_norm(h0), tokens_for_engram) h0 = h0 + engram_out cos, sin = self.rotary_emb(seqlen, device=h0.device, dtype=h0.dtype) cos = cos.unsqueeze(0).unsqueeze(0) sin = sin.unsqueeze(0).unsqueeze(0) h = h0 for layer in self.layers: h = layer(h, cos, sin, attention_mask=attention_mask) out = self.norm(h) return out class SentimentalForSequenceClassification(SentimentalPreTrainedModel): def __init__(self, config: SentimentalConfig): super().__init__(config) self.num_labels = getattr(config, "num_labels", 3) self.model = SentimentalModel(config) self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False) self.post_init() def forward(self, input_ids=None, attention_mask=None, position_ids=None, inputs_embeds=None, labels=None, **kwargs): hidden_states = self.model(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, inputs_embeds=inputs_embeds) if attention_mask is not None: mask_expanded = attention_mask.unsqueeze(-1).expand_as(hidden_states).float() sum_embeddings = (hidden_states * mask_expanded).sum(dim=1) sum_mask = mask_expanded.sum(dim=1).clamp(min=1e-9) pooled_output = sum_embeddings / sum_mask else: pooled_output = hidden_states.mean(dim=1) logits = self.score(pooled_output).float() loss = None if labels is not None: loss = F.cross_entropy(logits.view(-1, self.num_labels), labels.view(-1)) return SequenceClassifierOutput(loss=loss, logits=logits) # Backwards compatibility alias SentimentalModelForSequenceClassification = SentimentalForSequenceClassification