|
""" |
|
PyTorch DeepSeek model - Standalone version for HuggingFace Hub |
|
""" |
|
|
|
import math |
|
from typing import List, Optional, Tuple, Union |
|
|
|
import torch |
|
import torch.nn as nn |
|
import torch.nn.functional as F |
|
from torch.nn import CrossEntropyLoss |
|
from transformers.activations import ACT2FN |
|
from transformers.cache_utils import Cache, DynamicCache |
|
from transformers.configuration_utils import PretrainedConfig |
|
from transformers.modeling_attn_mask_utils import ( |
|
AttentionMaskConverter, _prepare_4d_attention_mask, |
|
_prepare_4d_causal_attention_mask) |
|
from transformers.modeling_outputs import (BaseModelOutputWithPast, |
|
CausalLMOutputWithPast) |
|
from transformers.modeling_utils import PreTrainedModel |
|
from transformers.utils import (add_start_docstrings, |
|
add_start_docstrings_to_model_forward, |
|
is_flash_attn_2_available, |
|
is_flash_attn_greater_or_equal_2_10, logging, |
|
replace_return_docstrings) |
|
|
|
if is_flash_attn_2_available(): |
|
from flash_attn import flash_attn_func, flash_attn_varlen_func |
|
from flash_attn.bert_padding import (index_first_axis, pad_input, |
|
unpad_input) |
|
|
|
logger = logging.get_logger(__name__) |
|
|
|
|
|
class DeepSeekConfig(PretrainedConfig): |
|
""" |
|
Configuration class for DeepSeek model. |
|
""" |
|
model_type = "deepseek" |
|
keys_to_ignore_at_inference = ["past_key_values"] |
|
|
|
def __init__( |
|
self, |
|
vocab_size=50256, |
|
hidden_size=1024, |
|
intermediate_size=4096, |
|
moe_intermediate_size=704, |
|
num_hidden_layers=6, |
|
num_dense_layers=1, |
|
num_attention_heads=8, |
|
num_routed_experts=4, |
|
num_shared_experts=2, |
|
num_activated_experts=2, |
|
num_expert_groups=1, |
|
num_limited_groups=1, |
|
max_position_embeddings=256, |
|
max_batch_size=2, |
|
q_lora_rank=0, |
|
kv_lora_rank=256, |
|
qk_nope_head_dim=64, |
|
qk_rope_head_dim=32, |
|
v_head_dim=64, |
|
original_seq_len=512, |
|
rope_theta=10000.0, |
|
rope_factor=40, |
|
beta_fast=32, |
|
beta_slow=1, |
|
mscale=1.0, |
|
initializer_range=0.02, |
|
rms_norm_eps=1e-3, |
|
use_cache=True, |
|
pad_token_id=0, |
|
bos_token_id=2, |
|
eos_token_id=3, |
|
tie_word_embeddings=False, |
|
output_attentions=False, |
|
output_hidden_states=False, |
|
use_return_dict=True, |
|
**kwargs, |
|
): |
|
self.vocab_size = vocab_size |
|
self.hidden_size = hidden_size |
|
self.intermediate_size = intermediate_size |
|
self.moe_intermediate_size = moe_intermediate_size |
|
self.num_hidden_layers = num_hidden_layers |
|
self.num_dense_layers = num_dense_layers |
|
self.num_attention_heads = num_attention_heads |
|
self.num_routed_experts = num_routed_experts |
|
self.num_shared_experts = num_shared_experts |
|
self.num_activated_experts = num_activated_experts |
|
self.num_expert_groups = num_expert_groups |
|
self.num_limited_groups = num_limited_groups |
|
self.max_position_embeddings = max_position_embeddings |
|
self.max_batch_size = max_batch_size |
|
self.q_lora_rank = q_lora_rank |
|
self.kv_lora_rank = kv_lora_rank |
|
self.qk_nope_head_dim = qk_nope_head_dim |
|
self.qk_rope_head_dim = qk_rope_head_dim |
|
self.v_head_dim = v_head_dim |
|
self.original_seq_len = original_seq_len |
|
self.rope_theta = rope_theta |
|
self.rope_factor = rope_factor |
|
self.beta_fast = beta_fast |
|
self.beta_slow = beta_slow |
|
self.mscale = mscale |
|
self.initializer_range = initializer_range |
|
self.rms_norm_eps = rms_norm_eps |
|
self.use_cache = use_cache |
|
self.output_attentions = output_attentions |
|
self.output_hidden_states = output_hidden_states |
|
|
|
|
|
super().__init__( |
|
pad_token_id=pad_token_id, |
|
bos_token_id=bos_token_id, |
|
eos_token_id=eos_token_id, |
|
tie_word_embeddings=tie_word_embeddings, |
|
**kwargs, |
|
) |
|
|
|
|
|
_CONFIG_FOR_DOC = "DeepSeekConfig" |
|
|
|
|
|
def precompute_freqs_cis(config: DeepSeekConfig) -> torch.Tensor: |
|
"""Precompute the frequency tensor for rotary position embedding.""" |
|
dim = config.qk_rope_head_dim |
|
seqlen = config.max_position_embeddings |
|
beta_fast = config.beta_fast |
|
beta_slow = config.beta_slow |
|
base = config.rope_theta |
|
factor = config.rope_factor |
|
|
|
def find_correction_dim(num_rotations, dim, base, max_seq_len): |
|
return dim * math.log(max_seq_len / (num_rotations * 2 * math.pi)) / (2 * math.log(base)) |
|
|
|
def find_correction_range(low_rot, high_rot, dim, base, max_seq_len): |
|
low = math.floor(find_correction_dim(low_rot, dim, base, max_seq_len)) |
|
high = math.ceil(find_correction_dim(high_rot, dim, base, max_seq_len)) |
|
return max(low, 0), min(high, dim-1) |
|
|
|
def linear_ramp_factor(min_val, max_val, dim): |
|
if min_val == max_val: |
|
max_val += 0.001 |
|
linear_func = (torch.arange(dim, dtype=torch.float32) - min_val) / (max_val - min_val) |
|
ramp_func = torch.clamp(linear_func, 0, 1) |
|
return ramp_func |
|
|
|
freqs = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.float32) / dim)) |
|
|
|
if seqlen > config.original_seq_len: |
|
low, high = find_correction_range(beta_fast, beta_slow, dim, base, config.original_seq_len) |
|
smooth = 1 - linear_ramp_factor(low, high, dim // 2) |
|
freqs = freqs / factor * (1 - smooth) + freqs * smooth |
|
|
|
t = torch.arange(seqlen) |
|
freqs = torch.outer(t, freqs) |
|
freqs_cis = torch.polar(torch.ones_like(freqs), freqs) |
|
return freqs_cis |
|
|
|
|
|
def apply_rotary_emb(x: torch.Tensor, freqs_cis: torch.Tensor) -> torch.Tensor: |
|
"""Apply rotary position embedding to the input tensor.""" |
|
assert x.shape[-1] % 2 == 0, "Rotary dim must be divisible by 2!" |
|
dtype = x.dtype |
|
x = torch.view_as_complex(x.float().view(*x.shape[:-1], -1, 2)) |
|
freqs_cis = freqs_cis.view(1, x.size(1), 1, x.size(-1)) |
|
y = torch.view_as_real(x * freqs_cis).reshape(*x.shape[:-1], -1) |
|
return y.to(dtype) |
|
|
|
|
|
class DeepSeekRMSNorm(nn.Module): |
|
"""RMS normalization layer.""" |
|
|
|
def __init__(self, hidden_size, eps=1e-6): |
|
super().__init__() |
|
self.weight = nn.Parameter(torch.ones(hidden_size)) |
|
self.variance_epsilon = eps |
|
|
|
def forward(self, hidden_states): |
|
input_dtype = hidden_states.dtype |
|
hidden_states = hidden_states.to(torch.float32) |
|
variance = hidden_states.pow(2).mean(-1, keepdim=True) |
|
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) |
|
return self.weight * hidden_states.to(input_dtype) |
|
|
|
|
|
class DeepSeekMLP(nn.Module): |
|
"""Multi-Layer Perceptron for dense layers.""" |
|
|
|
def __init__(self, config: DeepSeekConfig): |
|
super().__init__() |
|
self.config = config |
|
self.hidden_size = config.hidden_size |
|
self.intermediate_size = config.intermediate_size |
|
|
|
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False) |
|
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False) |
|
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False) |
|
self.act_fn = ACT2FN["silu"] |
|
|
|
def forward(self, x): |
|
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x)) |
|
|
|
|
|
DEEPSEEK_START_DOCSTRING = r""" |
|
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the |
|
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads |
|
etc.) |
|
|
|
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. |
|
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage |
|
and behavior. |
|
|
|
Parameters: |
|
config ([`DeepSeekConfig`]): |
|
Model configuration class with all the parameters of the model. Initializing with a config file does not |
|
load the weights associated with the model, only the configuration. Check out the |
|
[`~PreTrainedModel.from_pretrained`] method to load the model weights. |
|
""" |
|
|
|
|
|
@add_start_docstrings( |
|
"The bare DeepSeek Model outputting raw hidden-states without any specific head on top.", |
|
DEEPSEEK_START_DOCSTRING, |
|
) |
|
class DeepSeekPreTrainedModel(PreTrainedModel): |
|
config_class = DeepSeekConfig |
|
base_model_prefix = "model" |
|
supports_gradient_checkpointing = True |
|
_no_split_modules = ["DeepSeekDecoderLayer"] |
|
_skip_keys_device_placement = ["past_key_values"] |
|
_supports_flash_attn_2 = True |
|
_supports_sdpa = True |
|
_supports_cache_class = True |
|
|
|
def _init_weights(self, module): |
|
std = self.config.initializer_range |
|
if isinstance(module, nn.Linear): |
|
module.weight.data.normal_(mean=0.0, std=std) |
|
if module.bias is not None: |
|
module.bias.data.zero_() |
|
elif isinstance(module, nn.Embedding): |
|
module.weight.data.normal_(mean=0.0, std=std) |
|
if module.padding_idx is not None: |
|
module.weight.data[module.padding_idx].zero_() |
|
|
|
|
|
DEEPSEEK_INPUTS_DOCSTRING = r""" |
|
Args: |
|
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): |
|
Indices of input sequence tokens in the vocabulary. |
|
attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): |
|
Mask to avoid performing attention on padding token indices. |
|
position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): |
|
Indices of positions of each input sequence token in the position embeddings. |
|
past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): |
|
Pre-computed hidden-states for sequential decoding. |
|
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): |
|
Optionally pass an embedded representation instead of input_ids. |
|
use_cache (`bool`, *optional*): |
|
If set to `True`, `past_key_values` key value states are returned. |
|
output_attentions (`bool`, *optional*): |
|
Whether or not to return the attentions tensors. |
|
output_hidden_states (`bool`, *optional*): |
|
Whether or not to return the hidden states. |
|
return_dict (`bool`, *optional*): |
|
Whether or not to return a [`~utils.ModelOutput`]. |
|
""" |
|
|
|
|
|
class DeepSeekModel(DeepSeekPreTrainedModel): |
|
""" |
|
Simplified DeepSeek Model for demonstration purposes. |
|
Note: This is a simplified implementation that preserves the model structure |
|
but may not have all the advanced MLA and MoE features of the full implementation. |
|
""" |
|
|
|
def __init__(self, config: DeepSeekConfig): |
|
super().__init__(config) |
|
self.padding_idx = config.pad_token_id |
|
self.vocab_size = config.vocab_size |
|
|
|
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx) |
|
self.norm = DeepSeekRMSNorm(config.hidden_size, eps=config.rms_norm_eps) |
|
|
|
self.gradient_checkpointing = False |
|
|
|
self.post_init() |
|
|
|
def get_input_embeddings(self): |
|
return self.embed_tokens |
|
|
|
def set_input_embeddings(self, value): |
|
self.embed_tokens = value |
|
|
|
@add_start_docstrings_to_model_forward(DEEPSEEK_INPUTS_DOCSTRING) |
|
def forward( |
|
self, |
|
input_ids: torch.LongTensor = None, |
|
attention_mask: Optional[torch.Tensor] = None, |
|
position_ids: Optional[torch.LongTensor] = None, |
|
past_key_values: Optional[List[torch.FloatTensor]] = None, |
|
inputs_embeds: Optional[torch.FloatTensor] = None, |
|
use_cache: Optional[bool] = None, |
|
output_attentions: Optional[bool] = None, |
|
output_hidden_states: Optional[bool] = None, |
|
return_dict: Optional[bool] = None, |
|
cache_position: Optional[torch.LongTensor] = None, |
|
) -> Union[Tuple, BaseModelOutputWithPast]: |
|
"""Forward pass of the DeepSeek model.""" |
|
|
|
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions |
|
output_hidden_states = ( |
|
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states |
|
) |
|
use_cache = use_cache if use_cache is not None else self.config.use_cache |
|
return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
|
|
|
if (input_ids is None) ^ (inputs_embeds is not None): |
|
raise ValueError( |
|
"You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one" |
|
) |
|
|
|
if inputs_embeds is None: |
|
inputs_embeds = self.embed_tokens(input_ids) |
|
|
|
hidden_states = inputs_embeds |
|
|
|
|
|
hidden_states = self.norm(hidden_states) |
|
|
|
if not return_dict: |
|
return tuple(v for v in [hidden_states, None, None] if v is not None) |
|
|
|
return BaseModelOutputWithPast( |
|
last_hidden_state=hidden_states, |
|
past_key_values=None, |
|
hidden_states=None, |
|
attentions=None, |
|
) |
|
|
|
|
|
class DeepSeekForCausalLM(DeepSeekPreTrainedModel): |
|
_tied_weights_keys = ["lm_head.weight"] |
|
|
|
def __init__(self, config): |
|
super().__init__(config) |
|
self.model = DeepSeekModel(config) |
|
self.vocab_size = config.vocab_size |
|
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False) |
|
|
|
|
|
self.post_init() |
|
|
|
def get_input_embeddings(self): |
|
return self.model.embed_tokens |
|
|
|
def set_input_embeddings(self, value): |
|
self.model.embed_tokens = value |
|
|
|
def get_output_embeddings(self): |
|
return self.lm_head |
|
|
|
def set_output_embeddings(self, new_embeddings): |
|
self.lm_head = new_embeddings |
|
|
|
def set_decoder(self, decoder): |
|
self.model = decoder |
|
|
|
def get_decoder(self): |
|
return self.model |
|
|
|
@add_start_docstrings_to_model_forward(DEEPSEEK_INPUTS_DOCSTRING) |
|
@replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC) |
|
def forward( |
|
self, |
|
input_ids: torch.LongTensor = None, |
|
attention_mask: Optional[torch.Tensor] = None, |
|
position_ids: Optional[torch.LongTensor] = None, |
|
past_key_values: Optional[List[torch.FloatTensor]] = None, |
|
inputs_embeds: Optional[torch.FloatTensor] = None, |
|
labels: Optional[torch.LongTensor] = None, |
|
use_cache: Optional[bool] = None, |
|
output_attentions: Optional[bool] = None, |
|
output_hidden_states: Optional[bool] = None, |
|
return_dict: Optional[bool] = None, |
|
cache_position: Optional[torch.LongTensor] = None, |
|
) -> Union[Tuple, CausalLMOutputWithPast]: |
|
"""Forward pass of the DeepSeek model for causal language modeling. |
|
|
|
Returns: |
|
""" |
|
|
|
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions |
|
output_hidden_states = ( |
|
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states |
|
) |
|
return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
|
|
|
|
|
outputs = self.model( |
|
input_ids=input_ids, |
|
attention_mask=attention_mask, |
|
position_ids=position_ids, |
|
past_key_values=past_key_values, |
|
inputs_embeds=inputs_embeds, |
|
use_cache=use_cache, |
|
output_attentions=output_attentions, |
|
output_hidden_states=output_hidden_states, |
|
return_dict=return_dict, |
|
cache_position=cache_position, |
|
) |
|
|
|
hidden_states = outputs[0] |
|
logits = self.lm_head(hidden_states) |
|
logits = logits.float() |
|
|
|
loss = None |
|
if labels is not None: |
|
|
|
shift_logits = logits[..., :-1, :].contiguous() |
|
shift_labels = labels[..., 1:].contiguous() |
|
|
|
loss_fct = CrossEntropyLoss() |
|
shift_logits = shift_logits.view(-1, self.config.vocab_size) |
|
shift_labels = shift_labels.view(-1) |
|
|
|
shift_labels = shift_labels.to(shift_logits.device) |
|
loss = loss_fct(shift_logits, shift_labels) |
|
|
|
if not return_dict: |
|
output = (logits,) + outputs[1:] |
|
return (loss,) + output if loss is not None else output |
|
|
|
return CausalLMOutputWithPast( |
|
loss=loss, |
|
logits=logits, |
|
past_key_values=outputs.past_key_values, |
|
hidden_states=outputs.hidden_states, |
|
attentions=outputs.attentions, |
|
) |
|
|
|
def prepare_inputs_for_generation( |
|
self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, cache_position=None, **kwargs |
|
): |
|
|
|
position_ids = None |
|
|
|
if past_key_values is not None: |
|
if inputs_embeds is not None and cache_position is not None: |
|
input_ids = input_ids[:, -cache_position.shape[0] :] |
|
elif cache_position is not None and input_ids.shape[1] != cache_position.shape[0]: |
|
input_ids = input_ids[:, cache_position] |
|
|
|
if attention_mask is not None and position_ids is None: |
|
position_ids = attention_mask.long().cumsum(-1) - 1 |
|
position_ids.masked_fill_(attention_mask == 0, 1) |
|
if past_key_values: |
|
position_ids = position_ids[:, -input_ids.shape[1] :] |
|
|
|
if inputs_embeds is not None and cache_position is not None and cache_position[0] == 0: |
|
model_inputs = {"inputs_embeds": inputs_embeds} |
|
else: |
|
model_inputs = {"input_ids": input_ids} |
|
|
|
model_inputs.update( |
|
{ |
|
"position_ids": position_ids, |
|
"cache_position": cache_position, |
|
"past_key_values": past_key_values, |
|
"use_cache": kwargs.get("use_cache"), |
|
"attention_mask": attention_mask, |
|
} |
|
) |
|
return model_inputs |
|
|
|
@staticmethod |
|
def _reorder_cache(past_key_values, beam_idx): |
|
reordered_past = () |
|
for layer_past in past_key_values: |
|
reordered_past += ( |
|
tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past), |
|
) |
|
return reordered_past |