Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_activations.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_data/data-00000-of-00001.arrowAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_data/dataset_info.jsonAdded+19
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_data/state.jsonAdded+13
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_gradients.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_weights.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/config.jsonAdded+22
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/fabric_state/checkpoint.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/generation_config.jsonAdded+4
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_activations.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_data/data-00000-of-00001.arrowAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_data/dataset_info.jsonAdded+19
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_data/state.jsonAdded+13
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_gradients.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_weights.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/model.safetensorsAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/pico_decoder.pyAdded+871
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/special_tokens_map.jsonAdded+16
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/tokenizer.jsonAdded
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/tokenizer_config.jsonAdded+239
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/config.jsonAdded+22
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/fabric_state/checkpoint.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/generation_config.jsonAdded+4
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_activations.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_data/data-00000-of-00001.arrowAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_data/dataset_info.jsonAdded+19
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_data/state.jsonAdded+13
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_gradients.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_weights.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/model.safetensorsAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/pico_decoder.pyAdded+871
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/special_tokens_map.jsonAdded+16
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/tokenizer.jsonAdded
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/tokenizer_config.jsonAdded+239
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/config.jsonAdded+22
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/fabric_state/checkpoint.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/generation_config.jsonAdded+4
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_activations.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_data/data-00000-of-00001.arrowAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_data/dataset_info.jsonAdded+19
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_data/state.jsonAdded+13
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_gradients.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_weights.ptAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/model.safetensorsAdded+3
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/pico_decoder.pyAdded+871
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/special_tokens_map.jsonAdded+16
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/tokenizer.jsonAdded
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/tokenizer_config.jsonAdded+239
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11500/config.jsonAdded+22
- pico-decoder-tiny-dolma5M-v1/checkpoints/step_11500/fabric_state/checkpoint.ptAdded+3
123version https://git-lfs.github.com/spec/v1oid sha256:b7566791bcac384369fec016e7b22021f575d48c61df8aad9bfc0329883637desize 33819123version https://git-lfs.github.com/spec/v1oid sha256:908a522ebc31350e7397330dd99c9ad2253e2efdc1bee1ff76f9babef6078d28size 6640812345678910111213141516171819{ "citation": "", "description": "", "features": { "input_ids": { "feature": { "dtype": "int32", "_type": "Value" }, "_type": "Sequence" }, "text": { "dtype": "string", "_type": "Value" } }, "homepage": "", "license": ""}12345678910111213{ "_data_files": [ { "filename": "data-00000-of-00001.arrow" } ], "_fingerprint": "86e249409514e027", "_format_columns": null, "_format_kwargs": {}, "_format_type": null, "_output_all_columns": false, "_split": null}123version https://git-lfs.github.com/spec/v1oid sha256:12234bb37aa2ac9eb8693ce81a16a8d95e1988d7f2567b5f4b4f9695cf03d0f0size 2371527123version https://git-lfs.github.com/spec/v1oid sha256:13cdf88a0327e67430f61983d602f9ac270d81dcd369b564f5812dbb7bc5110csize 237144312345678910111213141516171819202122{ "activation_hidden_dim": 384, "architectures": [ "PicoDecoderHF" ], "attention_n_heads": 12, "attention_n_kv_heads": 4, "auto_map": { "AutoConfig": "pico_decoder.PicoDecoderHFConfig", "AutoModelForCausalLM": "pico_decoder.PicoDecoderHF" }, "batch_size": 1024, "d_model": 96, "max_seq_len": 2048, "model_type": "pico_decoder", "n_layers": 12, "norm_eps": 1e-06, "position_emb_theta": 10000.0, "torch_dtype": "float32", "transformers_version": "4.48.3", "vocab_size": 50304}123version https://git-lfs.github.com/spec/v1oid sha256:29699775f693a67c3f81ab2147fc97edfc8b34a8bfafea0cf23101754f202445size 1355431711234{ "transformers_version": "4.48.3", "vocab_size": 50304}123version https://git-lfs.github.com/spec/v1oid sha256:b75f5ff01ec4160cb28f29bda76444646f868580c2d16de75fd14ac71b777602size 33819123version https://git-lfs.github.com/spec/v1oid sha256:e764f67fb43f87a6ce2fc55e5f3b2e644ca59fc31a7018f0846433c719611c4csize 6780812345678910111213141516171819{ "citation": "", "description": "", "features": { "input_ids": { "feature": { "dtype": "int32", "_type": "Value" }, "_type": "Sequence" }, "text": { "dtype": "string", "_type": "Value" } }, "homepage": "", "license": ""}12345678910111213{ "_data_files": [ { "filename": "data-00000-of-00001.arrow" } ], "_fingerprint": "cd469f698bb3f53d", "_format_columns": null, "_format_kwargs": {}, "_format_type": null, "_output_all_columns": false, "_split": null}123version https://git-lfs.github.com/spec/v1oid sha256:03a02f217f29ca5da124e36279e4e083ed9f778b7d5732fccda3e2c466d3b57fsize 2371527123version https://git-lfs.github.com/spec/v1oid sha256:a0c9f73398020562f782ac9531cf80eacbe28407d92085f15649539c9132115dsize 2371443123version https://git-lfs.github.com/spec/v1oid sha256:eb5adc0f49c2650e0499ce8e35572cb659e32f90975218190bb2a59e01fda2e0size 45143592123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871"""Pico Decoder: A Lightweight Causal Transformer Language Model
Pico Decoder uses a simple LLAMA-style transformer architecture, written for clarity and educational purposes.
Everything is written with a modular design for easy modification and experimentation.
Key features:- RMSNorm for layer normalization- Rotary Positional Embeddings (RoPE)- Multi-head attention with KV-cache support- SwiGLU activation function- Residual connections throughout
- KV-cache for faster autoregressive generation
References: - RoPE: https://arxiv.org/abs/2104.09864 - SwiGLU: https://arxiv.org/abs/2002.05202 - LLAMA: https://arxiv.org/abs/2302.13971
Adapted from: - OLMO: https://github.com/allenai/OLMo - LLAMA: https://github.com/meta/llama"""
from dataclasses import asdictfrom typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union
import torchimport torch.nn as nnimport torch.nn.functional as Ffrom torch.nn.attention import SDPBackend, sdpa_kernelfrom transformers import GenerationMixin, PretrainedConfig, PreTrainedModelfrom transformers.generation import GenerationConfigfrom transformers.modeling_outputs import CausalLMOutput, CausalLMOutputWithPast
try: if TYPE_CHECKING: # We need to do this to avoid importing these when creating the HF-compatible models from src.config import ModelConfigexcept ImportError: pass
########################################################## Layer Normalization#########################################################
class RMSNorm(torch.nn.Module): """Root Mean Square Layer Normalization.
A variant of Layer Normalization that uses RMS statistics instead of mean/variance, resulting in improved stability and performance.
Args: config (Union[ModelConfig, PicoHFConfig]): Configuration object containing normalization parameters - config.norm_eps: Small constant for numerical stability - config.d_model: Model dimension for the weight parameter
References: https://arxiv.org/abs/1910.07467 """
def __init__(self, config: Union["ModelConfig", "PicoDecoderHFConfig"]): super().__init__() self.eps = config.norm_eps self.weight = nn.Parameter(torch.ones(config.d_model))
def _norm(self, x: torch.Tensor) -> torch.Tensor: """ Normalizes the input tensor by its RMS value. """ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies RMS normalization to the input tensor and scales it by the weight parameter. """ output = self._norm(x.float()).type_as(x) return output * self.weight
########################################################## Positional Embedding#########################################################
class RoPE(nn.Module): """Rotary Positional Embeddings (RoPE).
Implements position-dependent rotation of keys and queries in attention mechanism, allowing better modeling of relative positions in sequences. Uses complex number operations for efficient rotation.
Args: config (Union[ModelConfig, PicoHFConfig]): Model configuration containing: - config.position_emb_theta: Base for frequency computation - config.d_model: Model dimension - config.attention_n_heads: Number of attention heads - config.max_seq_len: Maximum sequence length
References: https://arxiv.org/abs/2104.09864 """
_freqs_cis_tensor: torch.Tensor | None = None
def __init__(self, config: Union["ModelConfig", "PicoDecoderHFConfig"]): super().__init__()
self.theta = config.position_emb_theta self.dim = config.d_model // config.attention_n_heads
max_seq_len = config.max_seq_len
# only gets set once, and then reused for all RoPE instances if RoPE._freqs_cis_tensor is None: RoPE._freqs_cis_tensor = self._setup_freqs_cis( max_seq_len, self.theta, self.dim )
# register _freqs_cis buffer # can be easily recomputed so persistent=False self.register_buffer("_freqs_cis", self._freqs_cis_tensor, persistent=False)
@classmethod def _setup_freqs_cis(cls, seq_len: int, theta: float, dim: int) -> torch.Tensor: """Setup Frequency Tensor for RoPE Embeddings
Initializes the complex frequency tensor that is used to compute the RoPE embeddings.
Note other implementations will use cos and sin directly, but using the complex number representation is (probably) more efficient:
e^(theta * i * t) = cos(theta * t) + i * sin(theta * t) [Euler's formula] """ _freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim)) positions = torch.arange(seq_len) freqs = torch.outer(positions, _freqs) return torch.polar(torch.ones_like(freqs), freqs) # complex64
def get_freqs_cis( self, input_shape: torch.Size, start_pos: int, end_pos: int ) -> torch.Tensor: """Reshape Frequency Tensor for RoPE Embeddings
Makes the frequency tensor broadcastable with the input tensor. """ _freqs_cis = self._freqs_cis[start_pos:end_pos] ndim = len(input_shape) assert 0 <= 1 < ndim assert _freqs_cis.shape == (input_shape[1], input_shape[-1])
# TODO: Check whether this is correct (might be able to remove this) shape = [d if i == 1 or i == ndim - 1 else 1 for i, d in enumerate(input_shape)] return _freqs_cis.view(*shape)
def forward( self, queries: torch.Tensor, keys: torch.Tensor, start_pos: int = 0, ) -> Tuple[torch.Tensor, torch.Tensor]: """Apply RoPE Embeddings to Queries and Keys
Applies the rotary positional embeddings to the input tensors via complex num multiplication
NOTE: The start_pos is used if we want to use the kv_cache in the attention mechanism. """ queries_ = torch.view_as_complex( queries.float().reshape(*queries.shape[:-1], -1, 2) ) keys_ = torch.view_as_complex(keys.float().reshape(*keys.shape[:-1], -1, 2))
input_shape = ( queries_.shape ) # same as keys: (batch_size, seq_len, n_heads, head_dim/2) freqs_start_pos = start_pos freqs_end_pos = freqs_start_pos + queries_.shape[1]
freqs_cis = self.get_freqs_cis(input_shape, freqs_start_pos, freqs_end_pos)
queries_rotated = torch.view_as_real(queries_ * freqs_cis).flatten(3) keys_rotated = torch.view_as_real(keys_ * freqs_cis).flatten(3) return queries_rotated.type_as(queries), keys_rotated.type_as(keys)
########################################################## Attention#########################################################
class Attention(nn.Module): """Multi-head Attention with Group Query Attention support.
Implements scaled dot-product attention and supports: - Grouped Query Attention (GQA) - Key-Value caching for efficient inference - RoPE integration
Args: config (Union[ModelConfig, PretrainedConfig]): Configuration containing: - config.attention_n_heads: Number of attention heads - config.attention_n_kv_heads: Number of key/value heads - config.d_model: Model dimension - config.batch_size: Maximum batch size - config.max_seq_len: Maximum sequence length
Shape: - Input: (batch_size, seq_len, d_model) - Output: (batch_size, seq_len, d_model) """
def __init__( self, config: Union["ModelConfig", "PicoDecoderHFConfig"], ): super().__init__()
self.n_heads = config.attention_n_heads self.n_kv_heads = config.attention_n_kv_heads
self.batch_size = config.batch_size self.max_seq_len = config.max_seq_len
d_model = config.d_model self.head_dim = d_model // self.n_heads
self.n_rep = self.n_heads // self.n_kv_heads
self.q_proj = nn.Linear(d_model, self.n_heads * self.head_dim, bias=False) self.k_proj = nn.Linear(d_model, self.n_kv_heads * self.head_dim, bias=False) self.v_proj = nn.Linear(d_model, self.n_kv_heads * self.head_dim, bias=False) self.o_proj = nn.Linear(self.n_heads * self.head_dim, d_model, bias=False)
self.rope = RoPE(config)
def forward( self, input: torch.Tensor, mask: Optional[torch.Tensor] = None, past_key_values: Optional[Tuple[torch.Tensor, ...]] = None, use_cache: bool = False, ) -> Tuple[torch.Tensor, Optional[Tuple[torch.Tensor, torch.Tensor]]]: """Forward pass for the attention mechanism.
Computes queries, keys, and values for the attention mechanism. Applies rotary positional embeddings to the queries and keys, and then computes attention scores and outputs.
For an introduction to the attention mechanism, see: https://arxiv.org/abs/1706.03762
A few things to note: - The past_key_values is used to implement the KV cache, which is used to speed up generation by caching the KV pairs from previous forward passes. This is useful when doing tasks that require generating multiple tokens conditioned on previous tokens (e.g. language modeling, text generation, etc.). The way the KV cache is implemented is that each layer has its own KV cache - this KV cache is implemented as a tuple. """ bsz, seq_len, _ = input.shape _queries, _keys, _values = ( self.q_proj(input), self.k_proj(input), self.v_proj(input), )
# Reshaping for multi-head attention queries = _queries.view(bsz, seq_len, self.n_heads, self.head_dim) keys = _keys.view(bsz, seq_len, self.n_kv_heads, self.head_dim) values = _values.view(bsz, seq_len, self.n_kv_heads, self.head_dim)
# The start position is used to apply the RoPE embeddings to only the new tokens # when using the kv_cache in the attention mechanism. # We want to start from the last position in the cache. start_pos = past_key_values[0].shape[1] if past_key_values is not None else 0
# apply rotary positional embeddings queries, keys = self.rope(queries, keys, start_pos)
if past_key_values is not None: keys = torch.cat([past_key_values[0], keys], dim=1) values = torch.cat([past_key_values[1], values], dim=1)
if use_cache: cached_keys = keys cached_values = values else: cached_keys = None cached_values = None
queries = queries.transpose(1, 2) keys = keys.transpose(1, 2) values = values.transpose(1, 2)
apply_gqa = self.n_rep > 1 if apply_gqa and queries.device.type == "mps": # NOTE: MPS does not support GQA in the SDPA kernel, but we can repeat the keys and values # outside of the kernel to get the same effect. # See: https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html keys = keys.repeat_interleave(self.n_rep, dim=-3) values = values.repeat_interleave(self.n_rep, dim=-3) apply_gqa = False
backends = [SDPBackend.CUDNN_ATTENTION, SDPBackend.MATH]
with sdpa_kernel(backends=backends): attn_output = F.scaled_dot_product_attention( queries.contiguous(), keys.contiguous(), values.contiguous(), attn_mask=mask.to(queries.dtype) if mask is not None else None, enable_gqa=apply_gqa, )
attn_output = attn_output.transpose(1, 2).contiguous().view(bsz, seq_len, -1) output = self.o_proj(attn_output)
return output, (cached_keys, cached_values)
########################################################## SwiGLU (Combines MLP and Activation)#########################################################
class SwiGLU(nn.Module): """SwiGLU Activation Function with Linear Projections.
Implements the SwiGLU activation function combined with linear transformations, serving as the feed-forward network in transformer blocks.
Args: config (Union[ModelConfig, PicoDecoderHFConfig]): Configuration containing: - config.d_model: Model dimension - config.activation_hidden_dim: Hidden dimension (typically 4 * d_model)
References: https://arxiv.org/abs/2002.05202 """
def __init__(self, config: Union["ModelConfig", "PicoDecoderHFConfig"]): super().__init__()
model_dim = config.d_model act_hidden_dim = config.activation_hidden_dim # usually 4 * d_model
self.w_0 = nn.Linear(model_dim, act_hidden_dim, bias=False) self.w_1 = nn.Linear(model_dim, act_hidden_dim, bias=False) self.w_2 = nn.Linear(act_hidden_dim, model_dim, bias=False)
def forward(self, x: torch.Tensor) -> torch.Tensor: return self.w_2(F.silu(self.w_0(x)) * self.w_1(x))
########################################################## PicoDecoderBlock#########################################################
class PicoDecoderBlock(nn.Module): """Single Transformer Block with Attention and Feed-forward layers.
Implements a standard transformer block with: - Multi-head attention with normalization and residual connection - SwiGLU feed-forward network with normalization and residual connection
Args: config (Union[ModelConfig, PicoDecoderHFConfig]): Model configuration; either a dataclass or a HuggingFace PicoDecoderHFConfig """
def __init__( self, config: Union["ModelConfig", "PicoDecoderHFConfig"], ): super().__init__()
self.attention = Attention(config) self.swiglu = SwiGLU(config) self.attention_norm = RMSNorm(config) self.swiglu_norm = RMSNorm(config)
def forward( self, input: torch.Tensor, mask: Optional[torch.Tensor] = None, past_key_values: Optional[Tuple[torch.Tensor]] = None, use_cache: bool = False, ) -> Tuple[torch.Tensor, Optional[Tuple[torch.Tensor, torch.Tensor]]]: attention_output, cached_key_values = self.attention( self.attention_norm(input), mask=mask, past_key_values=past_key_values, use_cache=use_cache, ) # NOTE: cached_key_values is None if use_cache is False
h = input + attention_output out = h + self.swiglu(self.swiglu_norm(h)) return out, cached_key_values
########################################################## Pico Decoder (Causal Transformer Model)#########################################################
class PicoDecoder(nn.Module): """ Pico Decoder: combines the embedding, causal decoder blocks, and output projection into a single autoregressive model.
For more information on the model, see the classes for the modules that make up the model. """
def __init__( self, model_config: Union["ModelConfig", "PicoDecoderHFConfig"], ): super().__init__() self.config = model_config
self.embedding_proj = nn.Embedding(self.config.vocab_size, self.config.d_model) self.layers = nn.ModuleList( [PicoDecoderBlock(self.config) for _ in range(self.config.n_layers)] ) self.output_norm = RMSNorm(self.config) self.de_embedding_proj = nn.Linear( self.config.d_model, self.config.vocab_size, bias=False )
def convert_to_hf_model(self) -> "PicoDecoderHF": """Convert the Lightning model to a HuggingFace model.""" # Create HF config without fabric-specific settings hf_config = PicoDecoderHFConfig.from_dataclass(self.config)
# Create new HF model hf_model = PicoDecoderHF(hf_config)
# Copy state dict, excluding fabric-specific keys hf_model.load_state_dict(self.state_dict(prefix="pico_decoder."))
return hf_model
def forward( self, input_ids: torch.Tensor, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, use_cache: bool = False, ) -> Tuple[torch.Tensor, Optional[Tuple[Tuple[torch.Tensor, torch.Tensor]]]]: """ This is the forward pass for the entire Pico model. It boils down to: - Embedding the input ids - Creating a causal mask - Processing through the pico layers - Projecting the output to logits
NOTE: One feature that might be confusing is the KV cache. The KV cache is used to speed up generation by caching the KV pairs from previous forward passes. This is useful when doing tasks that require generating multiple tokens conditioned on previous tokens (e.g. language modeling, text generation, etc.). The way the KV cache is implemented is that each layer has its own KV cache which is stored as a tuple. The whole model then stores a tuple of these KV caches (so a tuple of tuples). """
seq_len = input_ids.shape[-1] h = self.embedding_proj(input_ids)
# Calculate start position from past cached KV pairs. Remember that each layer has its # own KV Cache. So when we index past_key_values, we need to index into the KV pairs for the # correct layer and then for either the keys or values. start_pos = 0 if past_key_values is None else past_key_values[0][0].shape[1]
# Create causal mask for current sequence mask = None if seq_len > 1: mask = torch.full((seq_len, seq_len), float("-inf")) mask = torch.triu(mask, diagonal=1)
# If using KV cache, extend mask to cover cached sequence length if past_key_values is not None: # Add zeros for cached tokens (we can attend to all of them) mask = torch.hstack([torch.zeros((seq_len, start_pos)), mask])
mask = mask.to(h.device)
# NOTE: If we are using the cache, we need to store the cached KV pairs for each layer # in a tuple. Each layer will have its own cached KV pair which we aggregate in a tuple. cached_key_values = () if use_cache else None
# Process through transformer blocks for idx, layer in enumerate(self.layers): layer_past_key_values = ( past_key_values[idx] if past_key_values is not None else None )
h, layer_cached_key_values = layer( h, mask=mask, past_key_values=layer_past_key_values, use_cache=use_cache )
if use_cache: cached_key_values += (layer_cached_key_values,)
# Final norm and projection h = self.output_norm(h) logits = self.de_embedding_proj(h).float()
return logits, cached_key_values
########################################################## HuggingFace Wrapper for the Pico Decoder model.#########################################################
class PicoDecoderHFConfig(PretrainedConfig): """Config class for the Pico Decoder HuggingFace wrapper."""
model_type = "pico_decoder"
@classmethod def from_dict(cls, config_dict: Dict[str, Any], **kwargs) -> "PicoDecoderHFConfig": """ Initialize config from a dictionary. Note that no kwargs are passed to the constructor -- this is because with some kwargs special handling is required and can make this class brittle. """ pico_config = cls(**config_dict)
return_unused_kwargs = kwargs.pop("return_unused_kwargs", False) unused_kwargs = { key: value for key, value in kwargs.items() if not hasattr(pico_config, key) }
if return_unused_kwargs: return pico_config, unused_kwargs return pico_config
@classmethod def from_dataclass(cls, model_config: "ModelConfig"): """Initialise from our custom config dataclass.""" return cls.from_dict(asdict(model_config))
class PicoDecoderHF(PreTrainedModel, GenerationMixin): """ HuggingFace wrapper for the Pico model with generation support.
Many evaluation frameworks require a model be setup as a HuggingFace model, so we provide a simple wrapper that does just that. When we save checkpoints of the Pico model, we save both the normal Pico model as well as the model wrapped in this HuggingFace class.
This also lets you do cool things like:
`model = AutoModelForCausalLM.from_pretrained("path/to/checkpoint")` """
config_class = PicoDecoderHFConfig _no_split_modules = ["PicoBlock", "Attention", "SwiGLU", "RMSNorm"] main_input_name = "input_ids"
def __init__(self, config: PicoDecoderHFConfig): super().__init__(config) self.pico_decoder = PicoDecoder(config) # Initialize generation config with defaults self.generation_config = GenerationConfig() # Set some reasonable defaults for the model if hasattr(config, "max_position_embeddings"): self.generation_config.max_length = config.max_position_embeddings if hasattr(config, "vocab_size"): self.generation_config.vocab_size = config.vocab_size
def forward( self, input_ids: torch.Tensor, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, use_cache: bool = False, **kwargs, ) -> Union[CausalLMOutput, CausalLMOutputWithPast]: """HuggingFace forward pass wrapper.
Forwards pass for the HuggingFace version of the Pico Model. Basic wrapper around the Pico model's forward pass, and returns the output as a HuggingFace CausalLMOutput. """ logits, past_key_values = self.pico_decoder( input_ids, past_key_values, use_cache ) if use_cache: return CausalLMOutputWithPast( logits=logits, past_key_values=past_key_values, ) else: return CausalLMOutput( logits=logits, )
def prepare_inputs_for_generation( self, input_ids: torch.LongTensor, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.LongTensor] = None, **kwargs, ) -> Dict[str, Any]: """ Prepare inputs for generation.
Args: input_ids: Input token IDs past_key_values: Cached key-value pairs from previous forward passes attention_mask: Attention mask for the input **kwargs: Additional arguments
Returns: Dictionary containing prepared inputs """ # If we have past_key_values, we only need the last token if past_key_values is not None: input_ids = input_ids[:, -1:]
return { "input_ids": input_ids, "past_key_values": past_key_values, "use_cache": True, }
def get_input_embeddings(self): """Get the input embeddings layer.""" return self.pico_decoder.embedding_proj
def set_input_embeddings(self, value): """Set the input embeddings layer.""" self.pico_decoder.embedding_proj = value
def get_output_embeddings(self): """Get the output embeddings layer.""" return self.pico_decoder.de_embedding_proj
def set_output_embeddings(self, value): """Set the output embeddings layer.""" self.pico_decoder.de_embedding_proj = value
def get_lm_head(self): """Get the language model head.""" return self.pico_decoder.de_embedding_proj
def can_generate(self) -> bool: """Check if the model can generate text.""" return True
@property def is_encoder_decoder(self) -> bool: """Check if the model is an encoder-decoder model.""" return False
@property def can_use_cache(self) -> bool: """Check if the model can use KV cache.""" return True
def resize_token_embeddings( self, new_num_tokens: Optional[int] = None ) -> torch.nn.Embedding: """Resize token embeddings.""" old_embeddings = self.get_input_embeddings() if new_num_tokens is None: new_num_tokens = old_embeddings.num_embeddings
new_embeddings = torch.nn.Embedding( new_num_tokens, old_embeddings.embedding_dim ) new_embeddings.weight.data[: old_embeddings.num_embeddings] = ( old_embeddings.weight.data )
self.pico_decoder.embedding_proj = new_embeddings self.pico_decoder.de_embedding_proj = torch.nn.Linear( old_embeddings.embedding_dim, new_num_tokens, bias=False )
return new_embeddings
# Register for auto classesPicoDecoderHFConfig.register_for_auto_class()PicoDecoderHF.register_for_auto_class("AutoModel")PicoDecoderHF.register_for_auto_class("AutoModelForCausalLM")
########################################################## New PicoDecoderForCausalLM class for generation support#########################################################
class PicoDecoderForCausalLM(PreTrainedModel, GenerationMixin): """ PicoDecoderForCausalLM: A HuggingFace-compatible model that properly supports generation.
This class is designed to work with existing checkpoints and provides full generation support. It inherits from the right base classes that HuggingFace expects for text generation. """
config_class = PicoDecoderHFConfig _no_split_modules = ["PicoBlock", "Attention", "SwiGLU", "RMSNorm"] main_input_name = "input_ids"
def __init__(self, config: PicoDecoderHFConfig): super().__init__(config) self.pico_decoder = PicoDecoder(config) # Initialize generation config with defaults self.generation_config = GenerationConfig() # Set some reasonable defaults for the model if hasattr(config, "max_position_embeddings"): self.generation_config.max_length = config.max_position_embeddings if hasattr(config, "vocab_size"): self.generation_config.vocab_size = config.vocab_size
def forward( self, input_ids: torch.Tensor, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, use_cache: bool = False, **kwargs, ) -> Union[CausalLMOutput, CausalLMOutputWithPast]: """Forward pass for text generation.""" logits, past_key_values = self.pico_decoder( input_ids, past_key_values, use_cache ) if use_cache: return CausalLMOutputWithPast( logits=logits, past_key_values=past_key_values, ) else: return CausalLMOutput( logits=logits, )
def prepare_inputs_for_generation( self, input_ids: torch.LongTensor, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.LongTensor] = None, **kwargs, ) -> Dict[str, Any]: """Prepare inputs for generation.""" # If we have past_key_values, we only need the last token if past_key_values is not None: input_ids = input_ids[:, -1:]
return { "input_ids": input_ids, "past_key_values": past_key_values, "use_cache": True, }
def get_input_embeddings(self): """Get the input embeddings layer.""" return self.pico_decoder.embedding_proj
def set_input_embeddings(self, value): """Set the input embeddings layer.""" self.pico_decoder.embedding_proj = value
def get_output_embeddings(self): """Get the output embeddings layer.""" return self.pico_decoder.de_embedding_proj
def set_output_embeddings(self, value): """Set the output embeddings layer.""" self.pico_decoder.de_embedding_proj = value
def get_lm_head(self): """Get the language model head.""" return self.pico_decoder.de_embedding_proj
def can_generate(self) -> bool: """Check if the model can generate text.""" return True
@property def is_encoder_decoder(self) -> bool: """Check if the model is an encoder-decoder model.""" return False
@property def can_use_cache(self) -> bool: """Check if the model can use KV cache.""" return True
def resize_token_embeddings( self, new_num_tokens: Optional[int] = None ) -> torch.nn.Embedding: """Resize token embeddings.""" old_embeddings = self.get_input_embeddings() if new_num_tokens is None: new_num_tokens = old_embeddings.num_embeddings
new_embeddings = torch.nn.Embedding( new_num_tokens, old_embeddings.embedding_dim ) new_embeddings.weight.data[: old_embeddings.num_embeddings] = ( old_embeddings.weight.data )
self.pico_decoder.embedding_proj = new_embeddings self.pico_decoder.de_embedding_proj = torch.nn.Linear( old_embeddings.embedding_dim, new_num_tokens, bias=False )
return new_embeddings
@classmethod def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs): """ Load a pretrained model from a checkpoint.
This method handles loading from both the old PicoDecoderHF format and the new format. """ # First try to load with the new class try: return super().from_pretrained( pretrained_model_name_or_path, *model_args, **kwargs ) except Exception as e: print(f"Failed to load with new class: {e}") print("Attempting to load with legacy class and convert...")
# Try to load with the old class and convert try: from transformers import AutoModel
old_model = AutoModel.from_pretrained( pretrained_model_name_or_path, trust_remote_code=True, *model_args, **kwargs, )
# Create new model instance new_model = cls(old_model.config)
# Copy state dict new_model.load_state_dict(old_model.state_dict(), strict=False)
return new_model
except Exception as e2: print(f"Failed to convert from legacy format: {e2}") raise e
# Register the new classPicoDecoderForCausalLM.register_for_auto_class("AutoModelForCausalLM")12345678910111213141516{ "eos_token": { "content": "<|endoftext|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "pad_token": { "content": "<|padding|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239{ "add_bos_token": false, "add_eos_token": false, "add_prefix_space": false, "added_tokens_decoder": { "0": { "content": "|||IP_ADDRESS|||", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "1": { "content": "<|padding|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "50254": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50255": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50256": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50257": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50258": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50259": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50260": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50261": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50262": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50263": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50264": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50265": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50266": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50267": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50268": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50269": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50270": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50271": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50272": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50273": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50274": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50275": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50276": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50277": { "content": "|||EMAIL_ADDRESS|||", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50278": { "content": "|||PHONE_NUMBER|||", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50279": { "content": "<|endoftext|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true } }, "bos_token": null, "clean_up_tokenization_spaces": true, "eos_token": "<|endoftext|>", "extra_special_tokens": {}, "model_max_length": 1000000000000000019884624838656, "pad_token": "<|padding|>", "tokenizer_class": "GPTNeoXTokenizer", "unk_token": null}12345678910111213141516171819202122{ "activation_hidden_dim": 384, "architectures": [ "PicoDecoderHF" ], "attention_n_heads": 12, "attention_n_kv_heads": 4, "auto_map": { "AutoConfig": "pico_decoder.PicoDecoderHFConfig", "AutoModelForCausalLM": "pico_decoder.PicoDecoderHF" }, "batch_size": 1024, "d_model": 96, "max_seq_len": 2048, "model_type": "pico_decoder", "n_layers": 12, "norm_eps": 1e-06, "position_emb_theta": 10000.0, "torch_dtype": "float32", "transformers_version": "4.48.3", "vocab_size": 50304}123version https://git-lfs.github.com/spec/v1oid sha256:8beeafc3b4a063162786a1fe5fe143b00f4696cd9539fb86b6b6c1d972c89cacsize 1355431711234{ "transformers_version": "4.48.3", "vocab_size": 50304}123version https://git-lfs.github.com/spec/v1oid sha256:092ca881572335b376b7f9ae68ad90d234fdf4283cac25e14e4454cba6552b8bsize 33819123version https://git-lfs.github.com/spec/v1oid sha256:6cd607fbdf282a424f3afd9963551ad22545bee806ea23117e0381e633118b37size 7140012345678910111213141516171819{ "citation": "", "description": "", "features": { "input_ids": { "feature": { "dtype": "int32", "_type": "Value" }, "_type": "Sequence" }, "text": { "dtype": "string", "_type": "Value" } }, "homepage": "", "license": ""}12345678910111213{ "_data_files": [ { "filename": "data-00000-of-00001.arrow" } ], "_fingerprint": "a31f8d5143acaf4f", "_format_columns": null, "_format_kwargs": {}, "_format_type": null, "_output_all_columns": false, "_split": null}123version https://git-lfs.github.com/spec/v1oid sha256:104690ede085eaebf9c489211b0e30e3d6488c2e8d5e52deb3e84989486e922csize 2371527123version https://git-lfs.github.com/spec/v1oid sha256:10446cd79b3dfba18456ca4dd55a0b4a9af75696a1d6b0e86684aa461f9f4211size 2371443123version https://git-lfs.github.com/spec/v1oid sha256:c7b953d14db139c5d39088f2b09d89bb8cb4490453b88a2e3638a1b55c25e1basize 45143592123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871"""Pico Decoder: A Lightweight Causal Transformer Language Model
Pico Decoder uses a simple LLAMA-style transformer architecture, written for clarity and educational purposes.
Everything is written with a modular design for easy modification and experimentation.
Key features:- RMSNorm for layer normalization- Rotary Positional Embeddings (RoPE)- Multi-head attention with KV-cache support- SwiGLU activation function- Residual connections throughout
- KV-cache for faster autoregressive generation
References: - RoPE: https://arxiv.org/abs/2104.09864 - SwiGLU: https://arxiv.org/abs/2002.05202 - LLAMA: https://arxiv.org/abs/2302.13971
Adapted from: - OLMO: https://github.com/allenai/OLMo - LLAMA: https://github.com/meta/llama"""
from dataclasses import asdictfrom typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union
import torchimport torch.nn as nnimport torch.nn.functional as Ffrom torch.nn.attention import SDPBackend, sdpa_kernelfrom transformers import GenerationMixin, PretrainedConfig, PreTrainedModelfrom transformers.generation import GenerationConfigfrom transformers.modeling_outputs import CausalLMOutput, CausalLMOutputWithPast
try: if TYPE_CHECKING: # We need to do this to avoid importing these when creating the HF-compatible models from src.config import ModelConfigexcept ImportError: pass
########################################################## Layer Normalization#########################################################
class RMSNorm(torch.nn.Module): """Root Mean Square Layer Normalization.
A variant of Layer Normalization that uses RMS statistics instead of mean/variance, resulting in improved stability and performance.
Args: config (Union[ModelConfig, PicoHFConfig]): Configuration object containing normalization parameters - config.norm_eps: Small constant for numerical stability - config.d_model: Model dimension for the weight parameter
References: https://arxiv.org/abs/1910.07467 """
def __init__(self, config: Union["ModelConfig", "PicoDecoderHFConfig"]): super().__init__() self.eps = config.norm_eps self.weight = nn.Parameter(torch.ones(config.d_model))
def _norm(self, x: torch.Tensor) -> torch.Tensor: """ Normalizes the input tensor by its RMS value. """ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies RMS normalization to the input tensor and scales it by the weight parameter. """ output = self._norm(x.float()).type_as(x) return output * self.weight
########################################################## Positional Embedding#########################################################
class RoPE(nn.Module): """Rotary Positional Embeddings (RoPE).
Implements position-dependent rotation of keys and queries in attention mechanism, allowing better modeling of relative positions in sequences. Uses complex number operations for efficient rotation.
Args: config (Union[ModelConfig, PicoHFConfig]): Model configuration containing: - config.position_emb_theta: Base for frequency computation - config.d_model: Model dimension - config.attention_n_heads: Number of attention heads - config.max_seq_len: Maximum sequence length
References: https://arxiv.org/abs/2104.09864 """
_freqs_cis_tensor: torch.Tensor | None = None
def __init__(self, config: Union["ModelConfig", "PicoDecoderHFConfig"]): super().__init__()
self.theta = config.position_emb_theta self.dim = config.d_model // config.attention_n_heads
max_seq_len = config.max_seq_len
# only gets set once, and then reused for all RoPE instances if RoPE._freqs_cis_tensor is None: RoPE._freqs_cis_tensor = self._setup_freqs_cis( max_seq_len, self.theta, self.dim )
# register _freqs_cis buffer # can be easily recomputed so persistent=False self.register_buffer("_freqs_cis", self._freqs_cis_tensor, persistent=False)
@classmethod def _setup_freqs_cis(cls, seq_len: int, theta: float, dim: int) -> torch.Tensor: """Setup Frequency Tensor for RoPE Embeddings
Initializes the complex frequency tensor that is used to compute the RoPE embeddings.
Note other implementations will use cos and sin directly, but using the complex number representation is (probably) more efficient:
e^(theta * i * t) = cos(theta * t) + i * sin(theta * t) [Euler's formula] """ _freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim)) positions = torch.arange(seq_len) freqs = torch.outer(positions, _freqs) return torch.polar(torch.ones_like(freqs), freqs) # complex64
def get_freqs_cis( self, input_shape: torch.Size, start_pos: int, end_pos: int ) -> torch.Tensor: """Reshape Frequency Tensor for RoPE Embeddings
Makes the frequency tensor broadcastable with the input tensor. """ _freqs_cis = self._freqs_cis[start_pos:end_pos] ndim = len(input_shape) assert 0 <= 1 < ndim assert _freqs_cis.shape == (input_shape[1], input_shape[-1])
# TODO: Check whether this is correct (might be able to remove this) shape = [d if i == 1 or i == ndim - 1 else 1 for i, d in enumerate(input_shape)] return _freqs_cis.view(*shape)
def forward( self, queries: torch.Tensor, keys: torch.Tensor, start_pos: int = 0, ) -> Tuple[torch.Tensor, torch.Tensor]: """Apply RoPE Embeddings to Queries and Keys
Applies the rotary positional embeddings to the input tensors via complex num multiplication
NOTE: The start_pos is used if we want to use the kv_cache in the attention mechanism. """ queries_ = torch.view_as_complex( queries.float().reshape(*queries.shape[:-1], -1, 2) ) keys_ = torch.view_as_complex(keys.float().reshape(*keys.shape[:-1], -1, 2))
input_shape = ( queries_.shape ) # same as keys: (batch_size, seq_len, n_heads, head_dim/2) freqs_start_pos = start_pos freqs_end_pos = freqs_start_pos + queries_.shape[1]
freqs_cis = self.get_freqs_cis(input_shape, freqs_start_pos, freqs_end_pos)
queries_rotated = torch.view_as_real(queries_ * freqs_cis).flatten(3) keys_rotated = torch.view_as_real(keys_ * freqs_cis).flatten(3) return queries_rotated.type_as(queries), keys_rotated.type_as(keys)
########################################################## Attention#########################################################
class Attention(nn.Module): """Multi-head Attention with Group Query Attention support.
Implements scaled dot-product attention and supports: - Grouped Query Attention (GQA) - Key-Value caching for efficient inference - RoPE integration
Args: config (Union[ModelConfig, PretrainedConfig]): Configuration containing: - config.attention_n_heads: Number of attention heads - config.attention_n_kv_heads: Number of key/value heads - config.d_model: Model dimension - config.batch_size: Maximum batch size - config.max_seq_len: Maximum sequence length
Shape: - Input: (batch_size, seq_len, d_model) - Output: (batch_size, seq_len, d_model) """
def __init__( self, config: Union["ModelConfig", "PicoDecoderHFConfig"], ): super().__init__()
self.n_heads = config.attention_n_heads self.n_kv_heads = config.attention_n_kv_heads
self.batch_size = config.batch_size self.max_seq_len = config.max_seq_len
d_model = config.d_model self.head_dim = d_model // self.n_heads
self.n_rep = self.n_heads // self.n_kv_heads
self.q_proj = nn.Linear(d_model, self.n_heads * self.head_dim, bias=False) self.k_proj = nn.Linear(d_model, self.n_kv_heads * self.head_dim, bias=False) self.v_proj = nn.Linear(d_model, self.n_kv_heads * self.head_dim, bias=False) self.o_proj = nn.Linear(self.n_heads * self.head_dim, d_model, bias=False)
self.rope = RoPE(config)
def forward( self, input: torch.Tensor, mask: Optional[torch.Tensor] = None, past_key_values: Optional[Tuple[torch.Tensor, ...]] = None, use_cache: bool = False, ) -> Tuple[torch.Tensor, Optional[Tuple[torch.Tensor, torch.Tensor]]]: """Forward pass for the attention mechanism.
Computes queries, keys, and values for the attention mechanism. Applies rotary positional embeddings to the queries and keys, and then computes attention scores and outputs.
For an introduction to the attention mechanism, see: https://arxiv.org/abs/1706.03762
A few things to note: - The past_key_values is used to implement the KV cache, which is used to speed up generation by caching the KV pairs from previous forward passes. This is useful when doing tasks that require generating multiple tokens conditioned on previous tokens (e.g. language modeling, text generation, etc.). The way the KV cache is implemented is that each layer has its own KV cache - this KV cache is implemented as a tuple. """ bsz, seq_len, _ = input.shape _queries, _keys, _values = ( self.q_proj(input), self.k_proj(input), self.v_proj(input), )
# Reshaping for multi-head attention queries = _queries.view(bsz, seq_len, self.n_heads, self.head_dim) keys = _keys.view(bsz, seq_len, self.n_kv_heads, self.head_dim) values = _values.view(bsz, seq_len, self.n_kv_heads, self.head_dim)
# The start position is used to apply the RoPE embeddings to only the new tokens # when using the kv_cache in the attention mechanism. # We want to start from the last position in the cache. start_pos = past_key_values[0].shape[1] if past_key_values is not None else 0
# apply rotary positional embeddings queries, keys = self.rope(queries, keys, start_pos)
if past_key_values is not None: keys = torch.cat([past_key_values[0], keys], dim=1) values = torch.cat([past_key_values[1], values], dim=1)
if use_cache: cached_keys = keys cached_values = values else: cached_keys = None cached_values = None
queries = queries.transpose(1, 2) keys = keys.transpose(1, 2) values = values.transpose(1, 2)
apply_gqa = self.n_rep > 1 if apply_gqa and queries.device.type == "mps": # NOTE: MPS does not support GQA in the SDPA kernel, but we can repeat the keys and values # outside of the kernel to get the same effect. # See: https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html keys = keys.repeat_interleave(self.n_rep, dim=-3) values = values.repeat_interleave(self.n_rep, dim=-3) apply_gqa = False
backends = [SDPBackend.CUDNN_ATTENTION, SDPBackend.MATH]
with sdpa_kernel(backends=backends): attn_output = F.scaled_dot_product_attention( queries.contiguous(), keys.contiguous(), values.contiguous(), attn_mask=mask.to(queries.dtype) if mask is not None else None, enable_gqa=apply_gqa, )
attn_output = attn_output.transpose(1, 2).contiguous().view(bsz, seq_len, -1) output = self.o_proj(attn_output)
return output, (cached_keys, cached_values)
########################################################## SwiGLU (Combines MLP and Activation)#########################################################
class SwiGLU(nn.Module): """SwiGLU Activation Function with Linear Projections.
Implements the SwiGLU activation function combined with linear transformations, serving as the feed-forward network in transformer blocks.
Args: config (Union[ModelConfig, PicoDecoderHFConfig]): Configuration containing: - config.d_model: Model dimension - config.activation_hidden_dim: Hidden dimension (typically 4 * d_model)
References: https://arxiv.org/abs/2002.05202 """
def __init__(self, config: Union["ModelConfig", "PicoDecoderHFConfig"]): super().__init__()
model_dim = config.d_model act_hidden_dim = config.activation_hidden_dim # usually 4 * d_model
self.w_0 = nn.Linear(model_dim, act_hidden_dim, bias=False) self.w_1 = nn.Linear(model_dim, act_hidden_dim, bias=False) self.w_2 = nn.Linear(act_hidden_dim, model_dim, bias=False)
def forward(self, x: torch.Tensor) -> torch.Tensor: return self.w_2(F.silu(self.w_0(x)) * self.w_1(x))
########################################################## PicoDecoderBlock#########################################################
class PicoDecoderBlock(nn.Module): """Single Transformer Block with Attention and Feed-forward layers.
Implements a standard transformer block with: - Multi-head attention with normalization and residual connection - SwiGLU feed-forward network with normalization and residual connection
Args: config (Union[ModelConfig, PicoDecoderHFConfig]): Model configuration; either a dataclass or a HuggingFace PicoDecoderHFConfig """
def __init__( self, config: Union["ModelConfig", "PicoDecoderHFConfig"], ): super().__init__()
self.attention = Attention(config) self.swiglu = SwiGLU(config) self.attention_norm = RMSNorm(config) self.swiglu_norm = RMSNorm(config)
def forward( self, input: torch.Tensor, mask: Optional[torch.Tensor] = None, past_key_values: Optional[Tuple[torch.Tensor]] = None, use_cache: bool = False, ) -> Tuple[torch.Tensor, Optional[Tuple[torch.Tensor, torch.Tensor]]]: attention_output, cached_key_values = self.attention( self.attention_norm(input), mask=mask, past_key_values=past_key_values, use_cache=use_cache, ) # NOTE: cached_key_values is None if use_cache is False
h = input + attention_output out = h + self.swiglu(self.swiglu_norm(h)) return out, cached_key_values
########################################################## Pico Decoder (Causal Transformer Model)#########################################################
class PicoDecoder(nn.Module): """ Pico Decoder: combines the embedding, causal decoder blocks, and output projection into a single autoregressive model.
For more information on the model, see the classes for the modules that make up the model. """
def __init__( self, model_config: Union["ModelConfig", "PicoDecoderHFConfig"], ): super().__init__() self.config = model_config
self.embedding_proj = nn.Embedding(self.config.vocab_size, self.config.d_model) self.layers = nn.ModuleList( [PicoDecoderBlock(self.config) for _ in range(self.config.n_layers)] ) self.output_norm = RMSNorm(self.config) self.de_embedding_proj = nn.Linear( self.config.d_model, self.config.vocab_size, bias=False )
def convert_to_hf_model(self) -> "PicoDecoderHF": """Convert the Lightning model to a HuggingFace model.""" # Create HF config without fabric-specific settings hf_config = PicoDecoderHFConfig.from_dataclass(self.config)
# Create new HF model hf_model = PicoDecoderHF(hf_config)
# Copy state dict, excluding fabric-specific keys hf_model.load_state_dict(self.state_dict(prefix="pico_decoder."))
return hf_model
def forward( self, input_ids: torch.Tensor, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, use_cache: bool = False, ) -> Tuple[torch.Tensor, Optional[Tuple[Tuple[torch.Tensor, torch.Tensor]]]]: """ This is the forward pass for the entire Pico model. It boils down to: - Embedding the input ids - Creating a causal mask - Processing through the pico layers - Projecting the output to logits
NOTE: One feature that might be confusing is the KV cache. The KV cache is used to speed up generation by caching the KV pairs from previous forward passes. This is useful when doing tasks that require generating multiple tokens conditioned on previous tokens (e.g. language modeling, text generation, etc.). The way the KV cache is implemented is that each layer has its own KV cache which is stored as a tuple. The whole model then stores a tuple of these KV caches (so a tuple of tuples). """
seq_len = input_ids.shape[-1] h = self.embedding_proj(input_ids)
# Calculate start position from past cached KV pairs. Remember that each layer has its # own KV Cache. So when we index past_key_values, we need to index into the KV pairs for the # correct layer and then for either the keys or values. start_pos = 0 if past_key_values is None else past_key_values[0][0].shape[1]
# Create causal mask for current sequence mask = None if seq_len > 1: mask = torch.full((seq_len, seq_len), float("-inf")) mask = torch.triu(mask, diagonal=1)
# If using KV cache, extend mask to cover cached sequence length if past_key_values is not None: # Add zeros for cached tokens (we can attend to all of them) mask = torch.hstack([torch.zeros((seq_len, start_pos)), mask])
mask = mask.to(h.device)
# NOTE: If we are using the cache, we need to store the cached KV pairs for each layer # in a tuple. Each layer will have its own cached KV pair which we aggregate in a tuple. cached_key_values = () if use_cache else None
# Process through transformer blocks for idx, layer in enumerate(self.layers): layer_past_key_values = ( past_key_values[idx] if past_key_values is not None else None )
h, layer_cached_key_values = layer( h, mask=mask, past_key_values=layer_past_key_values, use_cache=use_cache )
if use_cache: cached_key_values += (layer_cached_key_values,)
# Final norm and projection h = self.output_norm(h) logits = self.de_embedding_proj(h).float()
return logits, cached_key_values
########################################################## HuggingFace Wrapper for the Pico Decoder model.#########################################################
class PicoDecoderHFConfig(PretrainedConfig): """Config class for the Pico Decoder HuggingFace wrapper."""
model_type = "pico_decoder"
@classmethod def from_dict(cls, config_dict: Dict[str, Any], **kwargs) -> "PicoDecoderHFConfig": """ Initialize config from a dictionary. Note that no kwargs are passed to the constructor -- this is because with some kwargs special handling is required and can make this class brittle. """ pico_config = cls(**config_dict)
return_unused_kwargs = kwargs.pop("return_unused_kwargs", False) unused_kwargs = { key: value for key, value in kwargs.items() if not hasattr(pico_config, key) }
if return_unused_kwargs: return pico_config, unused_kwargs return pico_config
@classmethod def from_dataclass(cls, model_config: "ModelConfig"): """Initialise from our custom config dataclass.""" return cls.from_dict(asdict(model_config))
class PicoDecoderHF(PreTrainedModel, GenerationMixin): """ HuggingFace wrapper for the Pico model with generation support.
Many evaluation frameworks require a model be setup as a HuggingFace model, so we provide a simple wrapper that does just that. When we save checkpoints of the Pico model, we save both the normal Pico model as well as the model wrapped in this HuggingFace class.
This also lets you do cool things like:
`model = AutoModelForCausalLM.from_pretrained("path/to/checkpoint")` """
config_class = PicoDecoderHFConfig _no_split_modules = ["PicoBlock", "Attention", "SwiGLU", "RMSNorm"] main_input_name = "input_ids"
def __init__(self, config: PicoDecoderHFConfig): super().__init__(config) self.pico_decoder = PicoDecoder(config) # Initialize generation config with defaults self.generation_config = GenerationConfig() # Set some reasonable defaults for the model if hasattr(config, "max_position_embeddings"): self.generation_config.max_length = config.max_position_embeddings if hasattr(config, "vocab_size"): self.generation_config.vocab_size = config.vocab_size
def forward( self, input_ids: torch.Tensor, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, use_cache: bool = False, **kwargs, ) -> Union[CausalLMOutput, CausalLMOutputWithPast]: """HuggingFace forward pass wrapper.
Forwards pass for the HuggingFace version of the Pico Model. Basic wrapper around the Pico model's forward pass, and returns the output as a HuggingFace CausalLMOutput. """ logits, past_key_values = self.pico_decoder( input_ids, past_key_values, use_cache ) if use_cache: return CausalLMOutputWithPast( logits=logits, past_key_values=past_key_values, ) else: return CausalLMOutput( logits=logits, )
def prepare_inputs_for_generation( self, input_ids: torch.LongTensor, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.LongTensor] = None, **kwargs, ) -> Dict[str, Any]: """ Prepare inputs for generation.
Args: input_ids: Input token IDs past_key_values: Cached key-value pairs from previous forward passes attention_mask: Attention mask for the input **kwargs: Additional arguments
Returns: Dictionary containing prepared inputs """ # If we have past_key_values, we only need the last token if past_key_values is not None: input_ids = input_ids[:, -1:]
return { "input_ids": input_ids, "past_key_values": past_key_values, "use_cache": True, }
def get_input_embeddings(self): """Get the input embeddings layer.""" return self.pico_decoder.embedding_proj
def set_input_embeddings(self, value): """Set the input embeddings layer.""" self.pico_decoder.embedding_proj = value
def get_output_embeddings(self): """Get the output embeddings layer.""" return self.pico_decoder.de_embedding_proj
def set_output_embeddings(self, value): """Set the output embeddings layer.""" self.pico_decoder.de_embedding_proj = value
def get_lm_head(self): """Get the language model head.""" return self.pico_decoder.de_embedding_proj
def can_generate(self) -> bool: """Check if the model can generate text.""" return True
@property def is_encoder_decoder(self) -> bool: """Check if the model is an encoder-decoder model.""" return False
@property def can_use_cache(self) -> bool: """Check if the model can use KV cache.""" return True
def resize_token_embeddings( self, new_num_tokens: Optional[int] = None ) -> torch.nn.Embedding: """Resize token embeddings.""" old_embeddings = self.get_input_embeddings() if new_num_tokens is None: new_num_tokens = old_embeddings.num_embeddings
new_embeddings = torch.nn.Embedding( new_num_tokens, old_embeddings.embedding_dim ) new_embeddings.weight.data[: old_embeddings.num_embeddings] = ( old_embeddings.weight.data )
self.pico_decoder.embedding_proj = new_embeddings self.pico_decoder.de_embedding_proj = torch.nn.Linear( old_embeddings.embedding_dim, new_num_tokens, bias=False )
return new_embeddings
# Register for auto classesPicoDecoderHFConfig.register_for_auto_class()PicoDecoderHF.register_for_auto_class("AutoModel")PicoDecoderHF.register_for_auto_class("AutoModelForCausalLM")
########################################################## New PicoDecoderForCausalLM class for generation support#########################################################
class PicoDecoderForCausalLM(PreTrainedModel, GenerationMixin): """ PicoDecoderForCausalLM: A HuggingFace-compatible model that properly supports generation.
This class is designed to work with existing checkpoints and provides full generation support. It inherits from the right base classes that HuggingFace expects for text generation. """
config_class = PicoDecoderHFConfig _no_split_modules = ["PicoBlock", "Attention", "SwiGLU", "RMSNorm"] main_input_name = "input_ids"
def __init__(self, config: PicoDecoderHFConfig): super().__init__(config) self.pico_decoder = PicoDecoder(config) # Initialize generation config with defaults self.generation_config = GenerationConfig() # Set some reasonable defaults for the model if hasattr(config, "max_position_embeddings"): self.generation_config.max_length = config.max_position_embeddings if hasattr(config, "vocab_size"): self.generation_config.vocab_size = config.vocab_size
def forward( self, input_ids: torch.Tensor, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, use_cache: bool = False, **kwargs, ) -> Union[CausalLMOutput, CausalLMOutputWithPast]: """Forward pass for text generation.""" logits, past_key_values = self.pico_decoder( input_ids, past_key_values, use_cache ) if use_cache: return CausalLMOutputWithPast( logits=logits, past_key_values=past_key_values, ) else: return CausalLMOutput( logits=logits, )
def prepare_inputs_for_generation( self, input_ids: torch.LongTensor, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.LongTensor] = None, **kwargs, ) -> Dict[str, Any]: """Prepare inputs for generation.""" # If we have past_key_values, we only need the last token if past_key_values is not None: input_ids = input_ids[:, -1:]
return { "input_ids": input_ids, "past_key_values": past_key_values, "use_cache": True, }
def get_input_embeddings(self): """Get the input embeddings layer.""" return self.pico_decoder.embedding_proj
def set_input_embeddings(self, value): """Set the input embeddings layer.""" self.pico_decoder.embedding_proj = value
def get_output_embeddings(self): """Get the output embeddings layer.""" return self.pico_decoder.de_embedding_proj
def set_output_embeddings(self, value): """Set the output embeddings layer.""" self.pico_decoder.de_embedding_proj = value
def get_lm_head(self): """Get the language model head.""" return self.pico_decoder.de_embedding_proj
def can_generate(self) -> bool: """Check if the model can generate text.""" return True
@property def is_encoder_decoder(self) -> bool: """Check if the model is an encoder-decoder model.""" return False
@property def can_use_cache(self) -> bool: """Check if the model can use KV cache.""" return True
def resize_token_embeddings( self, new_num_tokens: Optional[int] = None ) -> torch.nn.Embedding: """Resize token embeddings.""" old_embeddings = self.get_input_embeddings() if new_num_tokens is None: new_num_tokens = old_embeddings.num_embeddings
new_embeddings = torch.nn.Embedding( new_num_tokens, old_embeddings.embedding_dim ) new_embeddings.weight.data[: old_embeddings.num_embeddings] = ( old_embeddings.weight.data )
self.pico_decoder.embedding_proj = new_embeddings self.pico_decoder.de_embedding_proj = torch.nn.Linear( old_embeddings.embedding_dim, new_num_tokens, bias=False )
return new_embeddings
@classmethod def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs): """ Load a pretrained model from a checkpoint.
This method handles loading from both the old PicoDecoderHF format and the new format. """ # First try to load with the new class try: return super().from_pretrained( pretrained_model_name_or_path, *model_args, **kwargs ) except Exception as e: print(f"Failed to load with new class: {e}") print("Attempting to load with legacy class and convert...")
# Try to load with the old class and convert try: from transformers import AutoModel
old_model = AutoModel.from_pretrained( pretrained_model_name_or_path, trust_remote_code=True, *model_args, **kwargs, )
# Create new model instance new_model = cls(old_model.config)
# Copy state dict new_model.load_state_dict(old_model.state_dict(), strict=False)
return new_model
except Exception as e2: print(f"Failed to convert from legacy format: {e2}") raise e
# Register the new classPicoDecoderForCausalLM.register_for_auto_class("AutoModelForCausalLM")12345678910111213141516{ "eos_token": { "content": "<|endoftext|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "pad_token": { "content": "<|padding|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239{ "add_bos_token": false, "add_eos_token": false, "add_prefix_space": false, "added_tokens_decoder": { "0": { "content": "|||IP_ADDRESS|||", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "1": { "content": "<|padding|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "50254": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50255": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50256": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50257": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50258": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50259": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50260": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50261": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50262": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50263": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50264": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50265": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50266": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50267": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50268": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50269": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50270": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50271": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50272": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50273": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50274": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50275": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50276": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50277": { "content": "|||EMAIL_ADDRESS|||", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50278": { "content": "|||PHONE_NUMBER|||", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50279": { "content": "<|endoftext|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true } }, "bos_token": null, "clean_up_tokenization_spaces": true, "eos_token": "<|endoftext|>", "extra_special_tokens": {}, "model_max_length": 1000000000000000019884624838656, "pad_token": "<|padding|>", "tokenizer_class": "GPTNeoXTokenizer", "unk_token": null}12345678910111213141516171819202122{ "activation_hidden_dim": 384, "architectures": [ "PicoDecoderHF" ], "attention_n_heads": 12, "attention_n_kv_heads": 4, "auto_map": { "AutoConfig": "pico_decoder.PicoDecoderHFConfig", "AutoModelForCausalLM": "pico_decoder.PicoDecoderHF" }, "batch_size": 1024, "d_model": 96, "max_seq_len": 2048, "model_type": "pico_decoder", "n_layers": 12, "norm_eps": 1e-06, "position_emb_theta": 10000.0, "torch_dtype": "float32", "transformers_version": "4.48.3", "vocab_size": 50304}123version https://git-lfs.github.com/spec/v1oid sha256:b084a21c0c8762356e304a126647a27adb3f984f3c5e6193631b11adf420bad7size 1355431711234{ "transformers_version": "4.48.3", "vocab_size": 50304}123version https://git-lfs.github.com/spec/v1oid sha256:feaba51452c98158a43c7022a18b83cf2394f309d8545b40ff53bc0b928e5a37size 33819123version https://git-lfs.github.com/spec/v1oid sha256:5e3d7b427e161ca817db6e6cbef4a2e2db23531afb3f98f57b165f21c207d16esize 6800812345678910111213141516171819{ "citation": "", "description": "", "features": { "input_ids": { "feature": { "dtype": "int32", "_type": "Value" }, "_type": "Sequence" }, "text": { "dtype": "string", "_type": "Value" } }, "homepage": "", "license": ""}12345678910111213{ "_data_files": [ { "filename": "data-00000-of-00001.arrow" } ], "_fingerprint": "3e961088e632a0a0", "_format_columns": null, "_format_kwargs": {}, "_format_type": null, "_output_all_columns": false, "_split": null}123version https://git-lfs.github.com/spec/v1oid sha256:24b94747237eec4326676d783e9a3d96804542f277eb1c9840d7c585e6942a79size 2371527123version https://git-lfs.github.com/spec/v1oid sha256:496fe88e02c0eb71fd8cfd472b49c126f31dcf88d2b4cf406032b24a577761efsize 2371443123version https://git-lfs.github.com/spec/v1oid sha256:c2645d2c85fb25231404d0d3d7f1c2b8e1aefd8c964230a2ab3c8f76eea6cdd4size 45143592123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871"""Pico Decoder: A Lightweight Causal Transformer Language Model
Pico Decoder uses a simple LLAMA-style transformer architecture, written for clarity and educational purposes.
Everything is written with a modular design for easy modification and experimentation.
Key features:- RMSNorm for layer normalization- Rotary Positional Embeddings (RoPE)- Multi-head attention with KV-cache support- SwiGLU activation function- Residual connections throughout
- KV-cache for faster autoregressive generation
References: - RoPE: https://arxiv.org/abs/2104.09864 - SwiGLU: https://arxiv.org/abs/2002.05202 - LLAMA: https://arxiv.org/abs/2302.13971
Adapted from: - OLMO: https://github.com/allenai/OLMo - LLAMA: https://github.com/meta/llama"""
from dataclasses import asdictfrom typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union
import torchimport torch.nn as nnimport torch.nn.functional as Ffrom torch.nn.attention import SDPBackend, sdpa_kernelfrom transformers import GenerationMixin, PretrainedConfig, PreTrainedModelfrom transformers.generation import GenerationConfigfrom transformers.modeling_outputs import CausalLMOutput, CausalLMOutputWithPast
try: if TYPE_CHECKING: # We need to do this to avoid importing these when creating the HF-compatible models from src.config import ModelConfigexcept ImportError: pass
########################################################## Layer Normalization#########################################################
class RMSNorm(torch.nn.Module): """Root Mean Square Layer Normalization.
A variant of Layer Normalization that uses RMS statistics instead of mean/variance, resulting in improved stability and performance.
Args: config (Union[ModelConfig, PicoHFConfig]): Configuration object containing normalization parameters - config.norm_eps: Small constant for numerical stability - config.d_model: Model dimension for the weight parameter
References: https://arxiv.org/abs/1910.07467 """
def __init__(self, config: Union["ModelConfig", "PicoDecoderHFConfig"]): super().__init__() self.eps = config.norm_eps self.weight = nn.Parameter(torch.ones(config.d_model))
def _norm(self, x: torch.Tensor) -> torch.Tensor: """ Normalizes the input tensor by its RMS value. """ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies RMS normalization to the input tensor and scales it by the weight parameter. """ output = self._norm(x.float()).type_as(x) return output * self.weight
########################################################## Positional Embedding#########################################################
class RoPE(nn.Module): """Rotary Positional Embeddings (RoPE).
Implements position-dependent rotation of keys and queries in attention mechanism, allowing better modeling of relative positions in sequences. Uses complex number operations for efficient rotation.
Args: config (Union[ModelConfig, PicoHFConfig]): Model configuration containing: - config.position_emb_theta: Base for frequency computation - config.d_model: Model dimension - config.attention_n_heads: Number of attention heads - config.max_seq_len: Maximum sequence length
References: https://arxiv.org/abs/2104.09864 """
_freqs_cis_tensor: torch.Tensor | None = None
def __init__(self, config: Union["ModelConfig", "PicoDecoderHFConfig"]): super().__init__()
self.theta = config.position_emb_theta self.dim = config.d_model // config.attention_n_heads
max_seq_len = config.max_seq_len
# only gets set once, and then reused for all RoPE instances if RoPE._freqs_cis_tensor is None: RoPE._freqs_cis_tensor = self._setup_freqs_cis( max_seq_len, self.theta, self.dim )
# register _freqs_cis buffer # can be easily recomputed so persistent=False self.register_buffer("_freqs_cis", self._freqs_cis_tensor, persistent=False)
@classmethod def _setup_freqs_cis(cls, seq_len: int, theta: float, dim: int) -> torch.Tensor: """Setup Frequency Tensor for RoPE Embeddings
Initializes the complex frequency tensor that is used to compute the RoPE embeddings.
Note other implementations will use cos and sin directly, but using the complex number representation is (probably) more efficient:
e^(theta * i * t) = cos(theta * t) + i * sin(theta * t) [Euler's formula] """ _freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim)) positions = torch.arange(seq_len) freqs = torch.outer(positions, _freqs) return torch.polar(torch.ones_like(freqs), freqs) # complex64
def get_freqs_cis( self, input_shape: torch.Size, start_pos: int, end_pos: int ) -> torch.Tensor: """Reshape Frequency Tensor for RoPE Embeddings
Makes the frequency tensor broadcastable with the input tensor. """ _freqs_cis = self._freqs_cis[start_pos:end_pos] ndim = len(input_shape) assert 0 <= 1 < ndim assert _freqs_cis.shape == (input_shape[1], input_shape[-1])
# TODO: Check whether this is correct (might be able to remove this) shape = [d if i == 1 or i == ndim - 1 else 1 for i, d in enumerate(input_shape)] return _freqs_cis.view(*shape)
def forward( self, queries: torch.Tensor, keys: torch.Tensor, start_pos: int = 0, ) -> Tuple[torch.Tensor, torch.Tensor]: """Apply RoPE Embeddings to Queries and Keys
Applies the rotary positional embeddings to the input tensors via complex num multiplication
NOTE: The start_pos is used if we want to use the kv_cache in the attention mechanism. """ queries_ = torch.view_as_complex( queries.float().reshape(*queries.shape[:-1], -1, 2) ) keys_ = torch.view_as_complex(keys.float().reshape(*keys.shape[:-1], -1, 2))
input_shape = ( queries_.shape ) # same as keys: (batch_size, seq_len, n_heads, head_dim/2) freqs_start_pos = start_pos freqs_end_pos = freqs_start_pos + queries_.shape[1]
freqs_cis = self.get_freqs_cis(input_shape, freqs_start_pos, freqs_end_pos)
queries_rotated = torch.view_as_real(queries_ * freqs_cis).flatten(3) keys_rotated = torch.view_as_real(keys_ * freqs_cis).flatten(3) return queries_rotated.type_as(queries), keys_rotated.type_as(keys)
########################################################## Attention#########################################################
class Attention(nn.Module): """Multi-head Attention with Group Query Attention support.
Implements scaled dot-product attention and supports: - Grouped Query Attention (GQA) - Key-Value caching for efficient inference - RoPE integration
Args: config (Union[ModelConfig, PretrainedConfig]): Configuration containing: - config.attention_n_heads: Number of attention heads - config.attention_n_kv_heads: Number of key/value heads - config.d_model: Model dimension - config.batch_size: Maximum batch size - config.max_seq_len: Maximum sequence length
Shape: - Input: (batch_size, seq_len, d_model) - Output: (batch_size, seq_len, d_model) """
def __init__( self, config: Union["ModelConfig", "PicoDecoderHFConfig"], ): super().__init__()
self.n_heads = config.attention_n_heads self.n_kv_heads = config.attention_n_kv_heads
self.batch_size = config.batch_size self.max_seq_len = config.max_seq_len
d_model = config.d_model self.head_dim = d_model // self.n_heads
self.n_rep = self.n_heads // self.n_kv_heads
self.q_proj = nn.Linear(d_model, self.n_heads * self.head_dim, bias=False) self.k_proj = nn.Linear(d_model, self.n_kv_heads * self.head_dim, bias=False) self.v_proj = nn.Linear(d_model, self.n_kv_heads * self.head_dim, bias=False) self.o_proj = nn.Linear(self.n_heads * self.head_dim, d_model, bias=False)
self.rope = RoPE(config)
def forward( self, input: torch.Tensor, mask: Optional[torch.Tensor] = None, past_key_values: Optional[Tuple[torch.Tensor, ...]] = None, use_cache: bool = False, ) -> Tuple[torch.Tensor, Optional[Tuple[torch.Tensor, torch.Tensor]]]: """Forward pass for the attention mechanism.
Computes queries, keys, and values for the attention mechanism. Applies rotary positional embeddings to the queries and keys, and then computes attention scores and outputs.
For an introduction to the attention mechanism, see: https://arxiv.org/abs/1706.03762
A few things to note: - The past_key_values is used to implement the KV cache, which is used to speed up generation by caching the KV pairs from previous forward passes. This is useful when doing tasks that require generating multiple tokens conditioned on previous tokens (e.g. language modeling, text generation, etc.). The way the KV cache is implemented is that each layer has its own KV cache - this KV cache is implemented as a tuple. """ bsz, seq_len, _ = input.shape _queries, _keys, _values = ( self.q_proj(input), self.k_proj(input), self.v_proj(input), )
# Reshaping for multi-head attention queries = _queries.view(bsz, seq_len, self.n_heads, self.head_dim) keys = _keys.view(bsz, seq_len, self.n_kv_heads, self.head_dim) values = _values.view(bsz, seq_len, self.n_kv_heads, self.head_dim)
# The start position is used to apply the RoPE embeddings to only the new tokens # when using the kv_cache in the attention mechanism. # We want to start from the last position in the cache. start_pos = past_key_values[0].shape[1] if past_key_values is not None else 0
# apply rotary positional embeddings queries, keys = self.rope(queries, keys, start_pos)
if past_key_values is not None: keys = torch.cat([past_key_values[0], keys], dim=1) values = torch.cat([past_key_values[1], values], dim=1)
if use_cache: cached_keys = keys cached_values = values else: cached_keys = None cached_values = None
queries = queries.transpose(1, 2) keys = keys.transpose(1, 2) values = values.transpose(1, 2)
apply_gqa = self.n_rep > 1 if apply_gqa and queries.device.type == "mps": # NOTE: MPS does not support GQA in the SDPA kernel, but we can repeat the keys and values # outside of the kernel to get the same effect. # See: https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html keys = keys.repeat_interleave(self.n_rep, dim=-3) values = values.repeat_interleave(self.n_rep, dim=-3) apply_gqa = False
backends = [SDPBackend.CUDNN_ATTENTION, SDPBackend.MATH]
with sdpa_kernel(backends=backends): attn_output = F.scaled_dot_product_attention( queries.contiguous(), keys.contiguous(), values.contiguous(), attn_mask=mask.to(queries.dtype) if mask is not None else None, enable_gqa=apply_gqa, )
attn_output = attn_output.transpose(1, 2).contiguous().view(bsz, seq_len, -1) output = self.o_proj(attn_output)
return output, (cached_keys, cached_values)
########################################################## SwiGLU (Combines MLP and Activation)#########################################################
class SwiGLU(nn.Module): """SwiGLU Activation Function with Linear Projections.
Implements the SwiGLU activation function combined with linear transformations, serving as the feed-forward network in transformer blocks.
Args: config (Union[ModelConfig, PicoDecoderHFConfig]): Configuration containing: - config.d_model: Model dimension - config.activation_hidden_dim: Hidden dimension (typically 4 * d_model)
References: https://arxiv.org/abs/2002.05202 """
def __init__(self, config: Union["ModelConfig", "PicoDecoderHFConfig"]): super().__init__()
model_dim = config.d_model act_hidden_dim = config.activation_hidden_dim # usually 4 * d_model
self.w_0 = nn.Linear(model_dim, act_hidden_dim, bias=False) self.w_1 = nn.Linear(model_dim, act_hidden_dim, bias=False) self.w_2 = nn.Linear(act_hidden_dim, model_dim, bias=False)
def forward(self, x: torch.Tensor) -> torch.Tensor: return self.w_2(F.silu(self.w_0(x)) * self.w_1(x))
########################################################## PicoDecoderBlock#########################################################
class PicoDecoderBlock(nn.Module): """Single Transformer Block with Attention and Feed-forward layers.
Implements a standard transformer block with: - Multi-head attention with normalization and residual connection - SwiGLU feed-forward network with normalization and residual connection
Args: config (Union[ModelConfig, PicoDecoderHFConfig]): Model configuration; either a dataclass or a HuggingFace PicoDecoderHFConfig """
def __init__( self, config: Union["ModelConfig", "PicoDecoderHFConfig"], ): super().__init__()
self.attention = Attention(config) self.swiglu = SwiGLU(config) self.attention_norm = RMSNorm(config) self.swiglu_norm = RMSNorm(config)
def forward( self, input: torch.Tensor, mask: Optional[torch.Tensor] = None, past_key_values: Optional[Tuple[torch.Tensor]] = None, use_cache: bool = False, ) -> Tuple[torch.Tensor, Optional[Tuple[torch.Tensor, torch.Tensor]]]: attention_output, cached_key_values = self.attention( self.attention_norm(input), mask=mask, past_key_values=past_key_values, use_cache=use_cache, ) # NOTE: cached_key_values is None if use_cache is False
h = input + attention_output out = h + self.swiglu(self.swiglu_norm(h)) return out, cached_key_values
########################################################## Pico Decoder (Causal Transformer Model)#########################################################
class PicoDecoder(nn.Module): """ Pico Decoder: combines the embedding, causal decoder blocks, and output projection into a single autoregressive model.
For more information on the model, see the classes for the modules that make up the model. """
def __init__( self, model_config: Union["ModelConfig", "PicoDecoderHFConfig"], ): super().__init__() self.config = model_config
self.embedding_proj = nn.Embedding(self.config.vocab_size, self.config.d_model) self.layers = nn.ModuleList( [PicoDecoderBlock(self.config) for _ in range(self.config.n_layers)] ) self.output_norm = RMSNorm(self.config) self.de_embedding_proj = nn.Linear( self.config.d_model, self.config.vocab_size, bias=False )
def convert_to_hf_model(self) -> "PicoDecoderHF": """Convert the Lightning model to a HuggingFace model.""" # Create HF config without fabric-specific settings hf_config = PicoDecoderHFConfig.from_dataclass(self.config)
# Create new HF model hf_model = PicoDecoderHF(hf_config)
# Copy state dict, excluding fabric-specific keys hf_model.load_state_dict(self.state_dict(prefix="pico_decoder."))
return hf_model
def forward( self, input_ids: torch.Tensor, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, use_cache: bool = False, ) -> Tuple[torch.Tensor, Optional[Tuple[Tuple[torch.Tensor, torch.Tensor]]]]: """ This is the forward pass for the entire Pico model. It boils down to: - Embedding the input ids - Creating a causal mask - Processing through the pico layers - Projecting the output to logits
NOTE: One feature that might be confusing is the KV cache. The KV cache is used to speed up generation by caching the KV pairs from previous forward passes. This is useful when doing tasks that require generating multiple tokens conditioned on previous tokens (e.g. language modeling, text generation, etc.). The way the KV cache is implemented is that each layer has its own KV cache which is stored as a tuple. The whole model then stores a tuple of these KV caches (so a tuple of tuples). """
seq_len = input_ids.shape[-1] h = self.embedding_proj(input_ids)
# Calculate start position from past cached KV pairs. Remember that each layer has its # own KV Cache. So when we index past_key_values, we need to index into the KV pairs for the # correct layer and then for either the keys or values. start_pos = 0 if past_key_values is None else past_key_values[0][0].shape[1]
# Create causal mask for current sequence mask = None if seq_len > 1: mask = torch.full((seq_len, seq_len), float("-inf")) mask = torch.triu(mask, diagonal=1)
# If using KV cache, extend mask to cover cached sequence length if past_key_values is not None: # Add zeros for cached tokens (we can attend to all of them) mask = torch.hstack([torch.zeros((seq_len, start_pos)), mask])
mask = mask.to(h.device)
# NOTE: If we are using the cache, we need to store the cached KV pairs for each layer # in a tuple. Each layer will have its own cached KV pair which we aggregate in a tuple. cached_key_values = () if use_cache else None
# Process through transformer blocks for idx, layer in enumerate(self.layers): layer_past_key_values = ( past_key_values[idx] if past_key_values is not None else None )
h, layer_cached_key_values = layer( h, mask=mask, past_key_values=layer_past_key_values, use_cache=use_cache )
if use_cache: cached_key_values += (layer_cached_key_values,)
# Final norm and projection h = self.output_norm(h) logits = self.de_embedding_proj(h).float()
return logits, cached_key_values
########################################################## HuggingFace Wrapper for the Pico Decoder model.#########################################################
class PicoDecoderHFConfig(PretrainedConfig): """Config class for the Pico Decoder HuggingFace wrapper."""
model_type = "pico_decoder"
@classmethod def from_dict(cls, config_dict: Dict[str, Any], **kwargs) -> "PicoDecoderHFConfig": """ Initialize config from a dictionary. Note that no kwargs are passed to the constructor -- this is because with some kwargs special handling is required and can make this class brittle. """ pico_config = cls(**config_dict)
return_unused_kwargs = kwargs.pop("return_unused_kwargs", False) unused_kwargs = { key: value for key, value in kwargs.items() if not hasattr(pico_config, key) }
if return_unused_kwargs: return pico_config, unused_kwargs return pico_config
@classmethod def from_dataclass(cls, model_config: "ModelConfig"): """Initialise from our custom config dataclass.""" return cls.from_dict(asdict(model_config))
class PicoDecoderHF(PreTrainedModel, GenerationMixin): """ HuggingFace wrapper for the Pico model with generation support.
Many evaluation frameworks require a model be setup as a HuggingFace model, so we provide a simple wrapper that does just that. When we save checkpoints of the Pico model, we save both the normal Pico model as well as the model wrapped in this HuggingFace class.
This also lets you do cool things like:
`model = AutoModelForCausalLM.from_pretrained("path/to/checkpoint")` """
config_class = PicoDecoderHFConfig _no_split_modules = ["PicoBlock", "Attention", "SwiGLU", "RMSNorm"] main_input_name = "input_ids"
def __init__(self, config: PicoDecoderHFConfig): super().__init__(config) self.pico_decoder = PicoDecoder(config) # Initialize generation config with defaults self.generation_config = GenerationConfig() # Set some reasonable defaults for the model if hasattr(config, "max_position_embeddings"): self.generation_config.max_length = config.max_position_embeddings if hasattr(config, "vocab_size"): self.generation_config.vocab_size = config.vocab_size
def forward( self, input_ids: torch.Tensor, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, use_cache: bool = False, **kwargs, ) -> Union[CausalLMOutput, CausalLMOutputWithPast]: """HuggingFace forward pass wrapper.
Forwards pass for the HuggingFace version of the Pico Model. Basic wrapper around the Pico model's forward pass, and returns the output as a HuggingFace CausalLMOutput. """ logits, past_key_values = self.pico_decoder( input_ids, past_key_values, use_cache ) if use_cache: return CausalLMOutputWithPast( logits=logits, past_key_values=past_key_values, ) else: return CausalLMOutput( logits=logits, )
def prepare_inputs_for_generation( self, input_ids: torch.LongTensor, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.LongTensor] = None, **kwargs, ) -> Dict[str, Any]: """ Prepare inputs for generation.
Args: input_ids: Input token IDs past_key_values: Cached key-value pairs from previous forward passes attention_mask: Attention mask for the input **kwargs: Additional arguments
Returns: Dictionary containing prepared inputs """ # If we have past_key_values, we only need the last token if past_key_values is not None: input_ids = input_ids[:, -1:]
return { "input_ids": input_ids, "past_key_values": past_key_values, "use_cache": True, }
def get_input_embeddings(self): """Get the input embeddings layer.""" return self.pico_decoder.embedding_proj
def set_input_embeddings(self, value): """Set the input embeddings layer.""" self.pico_decoder.embedding_proj = value
def get_output_embeddings(self): """Get the output embeddings layer.""" return self.pico_decoder.de_embedding_proj
def set_output_embeddings(self, value): """Set the output embeddings layer.""" self.pico_decoder.de_embedding_proj = value
def get_lm_head(self): """Get the language model head.""" return self.pico_decoder.de_embedding_proj
def can_generate(self) -> bool: """Check if the model can generate text.""" return True
@property def is_encoder_decoder(self) -> bool: """Check if the model is an encoder-decoder model.""" return False
@property def can_use_cache(self) -> bool: """Check if the model can use KV cache.""" return True
def resize_token_embeddings( self, new_num_tokens: Optional[int] = None ) -> torch.nn.Embedding: """Resize token embeddings.""" old_embeddings = self.get_input_embeddings() if new_num_tokens is None: new_num_tokens = old_embeddings.num_embeddings
new_embeddings = torch.nn.Embedding( new_num_tokens, old_embeddings.embedding_dim ) new_embeddings.weight.data[: old_embeddings.num_embeddings] = ( old_embeddings.weight.data )
self.pico_decoder.embedding_proj = new_embeddings self.pico_decoder.de_embedding_proj = torch.nn.Linear( old_embeddings.embedding_dim, new_num_tokens, bias=False )
return new_embeddings
# Register for auto classesPicoDecoderHFConfig.register_for_auto_class()PicoDecoderHF.register_for_auto_class("AutoModel")PicoDecoderHF.register_for_auto_class("AutoModelForCausalLM")
########################################################## New PicoDecoderForCausalLM class for generation support#########################################################
class PicoDecoderForCausalLM(PreTrainedModel, GenerationMixin): """ PicoDecoderForCausalLM: A HuggingFace-compatible model that properly supports generation.
This class is designed to work with existing checkpoints and provides full generation support. It inherits from the right base classes that HuggingFace expects for text generation. """
config_class = PicoDecoderHFConfig _no_split_modules = ["PicoBlock", "Attention", "SwiGLU", "RMSNorm"] main_input_name = "input_ids"
def __init__(self, config: PicoDecoderHFConfig): super().__init__(config) self.pico_decoder = PicoDecoder(config) # Initialize generation config with defaults self.generation_config = GenerationConfig() # Set some reasonable defaults for the model if hasattr(config, "max_position_embeddings"): self.generation_config.max_length = config.max_position_embeddings if hasattr(config, "vocab_size"): self.generation_config.vocab_size = config.vocab_size
def forward( self, input_ids: torch.Tensor, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, use_cache: bool = False, **kwargs, ) -> Union[CausalLMOutput, CausalLMOutputWithPast]: """Forward pass for text generation.""" logits, past_key_values = self.pico_decoder( input_ids, past_key_values, use_cache ) if use_cache: return CausalLMOutputWithPast( logits=logits, past_key_values=past_key_values, ) else: return CausalLMOutput( logits=logits, )
def prepare_inputs_for_generation( self, input_ids: torch.LongTensor, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.LongTensor] = None, **kwargs, ) -> Dict[str, Any]: """Prepare inputs for generation.""" # If we have past_key_values, we only need the last token if past_key_values is not None: input_ids = input_ids[:, -1:]
return { "input_ids": input_ids, "past_key_values": past_key_values, "use_cache": True, }
def get_input_embeddings(self): """Get the input embeddings layer.""" return self.pico_decoder.embedding_proj
def set_input_embeddings(self, value): """Set the input embeddings layer.""" self.pico_decoder.embedding_proj = value
def get_output_embeddings(self): """Get the output embeddings layer.""" return self.pico_decoder.de_embedding_proj
def set_output_embeddings(self, value): """Set the output embeddings layer.""" self.pico_decoder.de_embedding_proj = value
def get_lm_head(self): """Get the language model head.""" return self.pico_decoder.de_embedding_proj
def can_generate(self) -> bool: """Check if the model can generate text.""" return True
@property def is_encoder_decoder(self) -> bool: """Check if the model is an encoder-decoder model.""" return False
@property def can_use_cache(self) -> bool: """Check if the model can use KV cache.""" return True
def resize_token_embeddings( self, new_num_tokens: Optional[int] = None ) -> torch.nn.Embedding: """Resize token embeddings.""" old_embeddings = self.get_input_embeddings() if new_num_tokens is None: new_num_tokens = old_embeddings.num_embeddings
new_embeddings = torch.nn.Embedding( new_num_tokens, old_embeddings.embedding_dim ) new_embeddings.weight.data[: old_embeddings.num_embeddings] = ( old_embeddings.weight.data )
self.pico_decoder.embedding_proj = new_embeddings self.pico_decoder.de_embedding_proj = torch.nn.Linear( old_embeddings.embedding_dim, new_num_tokens, bias=False )
return new_embeddings
@classmethod def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs): """ Load a pretrained model from a checkpoint.
This method handles loading from both the old PicoDecoderHF format and the new format. """ # First try to load with the new class try: return super().from_pretrained( pretrained_model_name_or_path, *model_args, **kwargs ) except Exception as e: print(f"Failed to load with new class: {e}") print("Attempting to load with legacy class and convert...")
# Try to load with the old class and convert try: from transformers import AutoModel
old_model = AutoModel.from_pretrained( pretrained_model_name_or_path, trust_remote_code=True, *model_args, **kwargs, )
# Create new model instance new_model = cls(old_model.config)
# Copy state dict new_model.load_state_dict(old_model.state_dict(), strict=False)
return new_model
except Exception as e2: print(f"Failed to convert from legacy format: {e2}") raise e
# Register the new classPicoDecoderForCausalLM.register_for_auto_class("AutoModelForCausalLM")12345678910111213141516{ "eos_token": { "content": "<|endoftext|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }, "pad_token": { "content": "<|padding|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239{ "add_bos_token": false, "add_eos_token": false, "add_prefix_space": false, "added_tokens_decoder": { "0": { "content": "|||IP_ADDRESS|||", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "1": { "content": "<|padding|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true }, "50254": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50255": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50256": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50257": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50258": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50259": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50260": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50261": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50262": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50263": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50264": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50265": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50266": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50267": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50268": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50269": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50270": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50271": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50272": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50273": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50274": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50275": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50276": { "content": " ", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50277": { "content": "|||EMAIL_ADDRESS|||", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50278": { "content": "|||PHONE_NUMBER|||", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": false }, "50279": { "content": "<|endoftext|>", "lstrip": false, "normalized": false, "rstrip": false, "single_word": false, "special": true } }, "bos_token": null, "clean_up_tokenization_spaces": true, "eos_token": "<|endoftext|>", "extra_special_tokens": {}, "model_max_length": 1000000000000000019884624838656, "pad_token": "<|padding|>", "tokenizer_class": "GPTNeoXTokenizer", "unk_token": null}12345678910111213141516171819202122{ "activation_hidden_dim": 384, "architectures": [ "PicoDecoderHF" ], "attention_n_heads": 12, "attention_n_kv_heads": 4, "auto_map": { "AutoConfig": "pico_decoder.PicoDecoderHFConfig", "AutoModelForCausalLM": "pico_decoder.PicoDecoderHF" }, "batch_size": 1024, "d_model": 96, "max_seq_len": 2048, "model_type": "pico_decoder", "n_layers": 12, "norm_eps": 1e-06, "position_emb_theta": 10000.0, "torch_dtype": "float32", "transformers_version": "4.48.3", "vocab_size": 50304}123version https://git-lfs.github.com/spec/v1oid sha256:ac810aee5896aca24e171163abb6c1e5d844af63ab89ca6ffa3594afdbdcc673size 135543171pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_activations.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_data/data-00000-of-00001.arrowAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_data/dataset_info.jsonAdded+19
pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_data/state.jsonAdded+13
pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_gradients.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_1000/learning_dynamics/train_weights.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/config.jsonAdded+22
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/fabric_state/checkpoint.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/generation_config.jsonAdded+4
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_activations.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_data/data-00000-of-00001.arrowAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_data/dataset_info.jsonAdded+19
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_data/state.jsonAdded+13
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_gradients.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/learning_dynamics/train_weights.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/model.safetensorsAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/pico_decoder.pyAdded+871
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/special_tokens_map.jsonAdded+16
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/tokenizer.jsonAdded
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10000/tokenizer_config.jsonAdded+239
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/config.jsonAdded+22
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/fabric_state/checkpoint.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/generation_config.jsonAdded+4
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_activations.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_data/data-00000-of-00001.arrowAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_data/dataset_info.jsonAdded+19
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_data/state.jsonAdded+13
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_gradients.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/learning_dynamics/train_weights.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/model.safetensorsAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/pico_decoder.pyAdded+871
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/special_tokens_map.jsonAdded+16
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/tokenizer.jsonAdded
pico-decoder-tiny-dolma5M-v1/checkpoints/step_10500/tokenizer_config.jsonAdded+239
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/config.jsonAdded+22
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/fabric_state/checkpoint.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/generation_config.jsonAdded+4
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_activations.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_data/data-00000-of-00001.arrowAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_data/dataset_info.jsonAdded+19
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_data/state.jsonAdded+13
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_gradients.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/learning_dynamics/train_weights.ptAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/model.safetensorsAdded+3
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/pico_decoder.pyAdded+871
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/special_tokens_map.jsonAdded+16
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/tokenizer.jsonAdded
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11000/tokenizer_config.jsonAdded+239
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11500/config.jsonAdded+22
pico-decoder-tiny-dolma5M-v1/checkpoints/step_11500/fabric_state/checkpoint.ptAdded+3