Delta-Ultra-Mini / delta /model.py
Delta-FlareAI's picture
Upload folder using huggingface_hub
a006332 verified
Raw
History Blame Contribute Delete
13 kB
"""PyTorch implementation of the Delta Ultra Mini decoder-only Transformer."""
from __future__ import annotations
import json
import logging
import math
import os
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any
import torch
from torch import nn
from torch.nn import functional as F
logging.basicConfig(level=os.getenv("DELTA_LOG_LEVEL", "INFO").upper())
logger = logging.getLogger(__name__)
@dataclass(slots=True)
class DeltaConfig:
"""Configuration for Delta Ultra Mini.
Attributes:
vocab_size: Token vocabulary size.
d_model: Embedding and hidden size.
n_heads: Number of attention heads.
n_layers: Number of decoder blocks.
d_ff: Feed-forward hidden size.
max_seq_len: Maximum context length.
dropout: Dropout probability.
tie_embeddings: Whether output projection shares token embedding weight.
pad_token_id: Padding token id.
bos_token_id: Beginning-of-sequence token id.
eos_token_id: End-of-sequence token id.
"""
vocab_size: int = 32000
d_model: int = 512
n_heads: int = 8
n_layers: int = 8
d_ff: int = 2048
max_seq_len: int = 512
dropout: float = 0.1
tie_embeddings: bool = True
pad_token_id: int = 0
bos_token_id: int = 2
eos_token_id: int = 3
use_cache: bool = True
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "DeltaConfig":
"""Build a config from a dictionary."""
valid = {field for field in cls.__dataclass_fields__}
return cls(**{key: value for key, value in data.items() if key in valid})
@classmethod
def from_json(cls, path: str | Path) -> "DeltaConfig":
"""Load a config from a JSON file."""
with Path(path).open("r", encoding="utf-8") as handle:
return cls.from_dict(json.load(handle))
def to_dict(self) -> dict[str, Any]:
"""Serialize config to a dictionary."""
return asdict(self)
class RMSNorm(nn.Module):
"""Root Mean Square normalization without mean-centering."""
def __init__(self, dim: int, eps: float = 1e-6) -> None:
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Normalize the last dimension of x."""
normed = x * torch.rsqrt(x.pow(2).mean(dim=-1, keepdim=True) + self.eps)
return normed * self.weight
class RotaryEmbedding(nn.Module):
"""Rotary positional embedding cache for attention heads."""
def __init__(self, dim: int, max_seq_len: int = 512, base: float = 10000.0) -> None:
super().__init__()
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
positions = torch.arange(max_seq_len, dtype=torch.float)
freqs = torch.outer(positions, inv_freq)
emb = torch.cat((freqs, freqs), dim=-1)
self.register_buffer("cos_cached", emb.cos()[None, None, :, :], persistent=False)
self.register_buffer("sin_cached", emb.sin()[None, None, :, :], persistent=False)
def forward(self, seq_len: int, offset: int = 0) -> tuple[torch.Tensor, torch.Tensor]:
"""Return cosine and sine caches for a sequence span."""
end = offset + seq_len
return self.cos_cached[:, :, offset:end, :], self.sin_cached[:, :, offset:end, :]
def _rotate_half(x: torch.Tensor) -> torch.Tensor:
"""Rotate pairs of hidden dimensions for RoPE."""
x1, x2 = x.chunk(2, dim=-1)
return torch.cat((-x2, x1), dim=-1)
def apply_rotary(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
"""Apply rotary embedding to q or k tensors."""
return (x * cos) + (_rotate_half(x) * sin)
class CausalSelfAttention(nn.Module):
"""Multi-head causal self-attention with optional KV cache."""
def __init__(self, config: DeltaConfig) -> None:
super().__init__()
if config.d_model % config.n_heads != 0:
raise ValueError("d_model must be divisible by n_heads")
self.n_heads = config.n_heads
self.head_dim = config.d_model // config.n_heads
self.qkv_proj = nn.Linear(config.d_model, 3 * config.d_model, bias=False)
self.out_proj = nn.Linear(config.d_model, config.d_model, bias=False)
self.dropout = nn.Dropout(config.dropout)
self.rope = RotaryEmbedding(self.head_dim, config.max_seq_len)
mask = torch.tril(torch.ones(config.max_seq_len, config.max_seq_len, dtype=torch.bool))
self.register_buffer("causal_mask", mask, persistent=False)
def forward(
self,
x: torch.Tensor,
past_key_value: tuple[torch.Tensor, torch.Tensor] | None = None,
use_cache: bool = False,
) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor] | None]:
"""Run attention.
Args:
x: Input tensor of shape (batch, seq, hidden).
past_key_value: Optional cached key and value tensors.
use_cache: Whether to return a new cache.
Returns:
Attention output and optional key/value cache.
"""
batch_size, seq_len, hidden_size = x.shape
qkv = self.qkv_proj(x)
q, k, v = qkv.split(hidden_size, dim=-1)
q = q.view(batch_size, seq_len, self.n_heads, self.head_dim).transpose(1, 2)
k = k.view(batch_size, seq_len, self.n_heads, self.head_dim).transpose(1, 2)
v = v.view(batch_size, seq_len, self.n_heads, self.head_dim).transpose(1, 2)
past_len = 0 if past_key_value is None else past_key_value[0].size(2)
cos, sin = self.rope(seq_len, offset=past_len)
q = apply_rotary(q, cos.to(q.device, q.dtype), sin.to(q.device, q.dtype))
k = apply_rotary(k, cos.to(k.device, k.dtype), sin.to(k.device, k.dtype))
if past_key_value is not None:
past_k, past_v = past_key_value
k = torch.cat((past_k, k), dim=2)
v = torch.cat((past_v, v), dim=2)
present = (k, v) if use_cache else None
attn_scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.head_dim)
total_len = k.size(2)
if past_len == 0:
mask = self.causal_mask[:seq_len, :total_len]
attn_scores = attn_scores.masked_fill(~mask[None, None, :, :], torch.finfo(attn_scores.dtype).min)
attn_weights = F.softmax(attn_scores, dim=-1)
attn_weights = self.dropout(attn_weights)
y = torch.matmul(attn_weights, v)
y = y.transpose(1, 2).contiguous().view(batch_size, seq_len, hidden_size)
return self.out_proj(y), present
class SwiGLUFeedForward(nn.Module):
"""SwiGLU feed-forward network."""
def __init__(self, config: DeltaConfig) -> None:
super().__init__()
self.gate_proj = nn.Linear(config.d_model, config.d_ff, bias=False)
self.up_proj = nn.Linear(config.d_model, config.d_ff, bias=False)
self.down_proj = nn.Linear(config.d_ff, config.d_model, bias=False)
self.dropout = nn.Dropout(config.dropout)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Apply SwiGLU transformation."""
return self.down_proj(self.dropout(F.silu(self.gate_proj(x)) * self.up_proj(x)))
class DeltaDecoderBlock(nn.Module):
"""One Delta decoder block: RMSNorm, attention, RMSNorm, SwiGLU FFN."""
def __init__(self, config: DeltaConfig) -> None:
super().__init__()
self.attn_norm = RMSNorm(config.d_model)
self.attn = CausalSelfAttention(config)
self.ffn_norm = RMSNorm(config.d_model)
self.ffn = SwiGLUFeedForward(config)
def forward(
self,
x: torch.Tensor,
past_key_value: tuple[torch.Tensor, torch.Tensor] | None = None,
use_cache: bool = False,
) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor] | None]:
"""Run one decoder block."""
attn_out, present = self.attn(self.attn_norm(x), past_key_value=past_key_value, use_cache=use_cache)
x = x + attn_out
x = x + self.ffn(self.ffn_norm(x))
return x, present
class DeltaModel(nn.Module):
"""Delta Ultra Mini causal language model."""
def __init__(self, config: DeltaConfig | dict[str, Any] | None = None) -> None:
super().__init__()
self.config = DeltaConfig.from_dict(config) if isinstance(config, dict) else (config or DeltaConfig())
self.embed_tokens = nn.Embedding(self.config.vocab_size, self.config.d_model)
self.drop = nn.Dropout(self.config.dropout)
self.layers = nn.ModuleList(DeltaDecoderBlock(self.config) for _ in range(self.config.n_layers))
self.norm = RMSNorm(self.config.d_model)
self.lm_head = nn.Linear(self.config.d_model, self.config.vocab_size, bias=False)
if self.config.tie_embeddings:
self.lm_head.weight = self.embed_tokens.weight
self.apply(self._init_weights)
total_params = self.num_parameters()
logger.info("DeltaModel initialized with %s parameters", f"{total_params:,}")
print(f"DeltaModel parameters: {total_params:,}")
if not 48_000_000 <= total_params <= 55_000_000:
raise ValueError(f"Delta Ultra Mini must have 48M-55M parameters, got {total_params:,}")
def _init_weights(self, module: nn.Module) -> None:
"""Initialize weights with GPT-style normal initialization."""
if isinstance(module, nn.Linear):
nn.init.normal_(module.weight, mean=0.0, std=0.02)
if module.bias is not None:
nn.init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
nn.init.normal_(module.weight, mean=0.0, std=0.02)
def num_parameters(self, only_trainable: bool = True, exclude_embeddings: bool = False) -> int:
"""Return the number of model parameters.
Args:
only_trainable: Count only parameters with requires_grad.
exclude_embeddings: Exclude embedding parameters for Trainer FLOPs estimates.
"""
total = 0
for name, parameter in self.named_parameters():
if only_trainable and not parameter.requires_grad:
continue
if exclude_embeddings and "embed_tokens" in name:
continue
total += parameter.numel()
return total
def forward(
self,
input_ids: torch.Tensor,
labels: torch.Tensor | None = None,
past_key_values: list[tuple[torch.Tensor, torch.Tensor]] | None = None,
use_cache: bool = False,
**_: Any,
) -> dict[str, torch.Tensor | list[tuple[torch.Tensor, torch.Tensor]] | None]:
"""Run causal language modeling forward pass."""
if input_ids.size(1) > self.config.max_seq_len:
input_ids = input_ids[:, -self.config.max_seq_len :]
if labels is not None:
labels = labels[:, -self.config.max_seq_len :]
x = self.drop(self.embed_tokens(input_ids))
next_cache: list[tuple[torch.Tensor, torch.Tensor]] = []
for index, layer in enumerate(self.layers):
past = None if past_key_values is None else past_key_values[index]
x, present = layer(x, past_key_value=past, use_cache=use_cache)
if present is not None:
next_cache.append(present)
logits = self.lm_head(self.norm(x))
loss = None
if labels is not None:
shift_logits = logits[:, :-1, :].contiguous()
shift_labels = labels[:, 1:].contiguous()
loss = F.cross_entropy(
shift_logits.view(-1, shift_logits.size(-1)),
shift_labels.view(-1),
ignore_index=-100,
)
return {"loss": loss, "logits": logits, "past_key_values": next_cache if use_cache else None}
def save_checkpoint(
self,
path: str | Path,
optimizer: torch.optim.Optimizer | None = None,
scheduler: Any | None = None,
step: int = 0,
) -> None:
"""Save a full training checkpoint."""
checkpoint: dict[str, Any] = {
"model_state_dict": self.state_dict(),
"step": step,
"config": self.config.to_dict(),
}
if optimizer is not None:
checkpoint["optimizer_state_dict"] = optimizer.state_dict()
if scheduler is not None:
checkpoint["scheduler_state_dict"] = scheduler.state_dict()
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
torch.save(checkpoint, path)
@classmethod
def load_checkpoint(cls, path: str | Path, map_location: str | torch.device = "cpu") -> "DeltaModel":
"""Load a model from a checkpoint file."""
checkpoint = torch.load(path, map_location=map_location)
model = cls(DeltaConfig.from_dict(checkpoint["config"]))
model.load_state_dict(checkpoint["model_state_dict"])
return model