CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
modular_olmo.py174 linesDownload Raw Back to olmo
1from typing import Callable, Optional2 3import torch4import torch.nn as nn5import torch.nn.functional as F6 7from ...cache_utils import Cache8from ...modeling_utils import ALL_ATTENTION_FUNCTIONS9from ...utils import logging10from ...utils.deprecation import deprecate_kwarg11from ..llama.modeling_llama import (12    LlamaAttention,13    LlamaDecoderLayer,14    LlamaForCausalLM,15    LlamaMLP,16    LlamaModel,17    LlamaRotaryEmbedding,18    eager_attention_forward,19    rotate_half,20)21from .configuration_olmo import OlmoConfig22 23 24logger = logging.get_logger(__name__)25 26 27class OlmoLayerNorm(nn.Module):28    """LayerNorm but with no learnable weight or bias."""29 30    def __init__(self, hidden_size: int) -> None:31        super().__init__()32        self.normalized_shape = (hidden_size,)33 34    def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:35        orig_dtype = hidden_states.dtype36        return F.layer_norm(hidden_states.to(dtype=torch.float32), self.normalized_shape, None, None, eps=1e-5).to(37            orig_dtype38        )39 40 41class OlmoMLP(LlamaMLP):42    def __init__(self, config):43        super().__init__(config)44        self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)45        self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)46        self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)47 48 49def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):50    """Applies Rotary Position Embedding to the query and key tensors.51 52    Args:53        q (`torch.Tensor`): The query tensor.54        k (`torch.Tensor`): The key tensor.55        cos (`torch.Tensor`): The cosine part of the rotary embedding.56        sin (`torch.Tensor`): The sine part of the rotary embedding.57        position_ids (`torch.Tensor`, *optional*):58            Deprecated and unused.59        unsqueeze_dim (`int`, *optional*, defaults to 1):60            The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and61            sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note62            that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and63            k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes64            cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have65            the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.66    Returns:67        `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.68    """69    q_type, k_type = q.dtype, k.dtype70    cos = cos.unsqueeze(unsqueeze_dim)71    sin = sin.unsqueeze(unsqueeze_dim)72    q_embed = (q * cos) + (rotate_half(q) * sin)73    k_embed = (k * cos) + (rotate_half(k) * sin)74    return q_embed.to(q_type), k_embed.to(k_type)75 76 77class OlmoAttention(LlamaAttention):78    @deprecate_kwarg("past_key_value", new_name="past_key_values", version="4.58")79    def forward(80        self,81        hidden_states: torch.Tensor,82        position_embeddings: tuple[torch.Tensor, torch.Tensor],83        attention_mask: Optional[torch.Tensor],84        past_key_values: Optional[Cache] = None,85        cache_position: Optional[torch.LongTensor] = None,86        **kwargs,87    ) -> tuple[torch.Tensor, Optional[torch.Tensor]]:88        input_shape = hidden_states.shape[:-1]89        hidden_shape = (*input_shape, -1, self.head_dim)90 91        query_states = self.q_proj(hidden_states)92        key_states = self.k_proj(hidden_states)93        value_states = self.v_proj(hidden_states)94 95        if self.config.clip_qkv is not None:96            query_states.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)97            key_states.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)98            value_states.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)99 100        query_states = query_states.view(hidden_shape).transpose(1, 2)101        key_states = key_states.view(hidden_shape).transpose(1, 2)102        value_states = value_states.view(hidden_shape).transpose(1, 2)103 104        cos, sin = position_embeddings105        query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)106 107        if past_key_values is not None:108            # sin and cos are specific to RoPE models; cache_position needed for the static cache109            cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}110            key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx, cache_kwargs)111 112        attention_interface: Callable = eager_attention_forward113        if self.config._attn_implementation != "eager":114            attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]115 116        attn_output, attn_weights = attention_interface(117            self,118            query_states,119            key_states,120            value_states,121            attention_mask,122            dropout=0.0 if not self.training else self.attention_dropout,123            scaling=self.scaling,124            **kwargs,125        )126 127        attn_output = attn_output.reshape(*input_shape, -1).contiguous()128        attn_output = self.o_proj(attn_output)129        return attn_output, attn_weights130 131 132class OlmoDecoderLayer(LlamaDecoderLayer):133    def __init__(self, config: OlmoConfig, layer_idx: int):134        super().__init__(config, layer_idx)135        self.input_layernorm = OlmoLayerNorm(config.hidden_size)136        self.post_attention_layernorm = OlmoLayerNorm(config.hidden_size)137        self.self_attn = OlmoAttention(config=config, layer_idx=layer_idx)138 139 140# This is identical to LlamaRotaryEmbedding except the output cos and sin are returned141# as float32 rather than the input type.142class OlmoRotaryEmbedding(LlamaRotaryEmbedding):143    def forward(self, x, position_ids):144        inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1).to(x.device)145        position_ids_expanded = position_ids[:, None, :].float()146 147        device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu"148        with torch.autocast(device_type=device_type, enabled=False):  # Force float32149            freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)150            emb = torch.cat((freqs, freqs), dim=-1)151            cos = emb.cos() * self.attention_scaling152            sin = emb.sin() * self.attention_scaling153            return cos, sin154 155 156class OlmoModel(LlamaModel):157    def __init__(self, config: OlmoConfig):158        super().__init__(config)159        self.layers = nn.ModuleList(160            [OlmoDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]161        )162        self.norm = OlmoLayerNorm(config.hidden_size)163 164 165class OlmoForCausalLM(LlamaForCausalLM):166    pass167 168 169__all__ = [170    "OlmoForCausalLM",171    "OlmoModel",172    "OlmoPreTrainedModel",  # noqa: F822173]174 
Aluode/PerceptionLabPortable · CoolFace