Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deepseek v2 support #31976

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
1,133 changes: 1,133 additions & 0 deletions src/transformers/models/deepseek_v2/modular_deepseek_v2.py

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions src/transformers/models/deepseek_v2_copy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Copyright 2024 DeepSeek-AI and the HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import (
OptionalDependencyNotAvailable,
_LazyModule,
is_flax_available,
is_sentencepiece_available,
is_tokenizers_available,
is_torch_available,
)


_import_structure = {
"configuration_deepseek_v2": ["DeepseekV2Config"],
}

try:
if not is_sentencepiece_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["tokenization_deepseek_v2"] = ["DeepseekV2Tokenizer"]

try:
if not is_tokenizers_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["tokenization_deepseek_v2_fast"] = ["DeepseekV2TokenizerFast"]

try:
if not is_torch_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["modeling_deepseek_v2"] = [
"DeepseekV2ForCausalLM",
"DeepseekV2Model",
"DeepseekV2PreTrainedModel",
"DeepseekV2ForSequenceClassification",
"DeepseekV2ForQuestionAnswering",
"DeepseekV2ForTokenClassification",
]

try:
if not is_flax_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["modeling_flax_deepseek_v2"] = [
"FlaxDeepseekV2ForCausalLM",
"FlaxDeepseekV2Model",
"FlaxDeepseekV2PreTrainedModel",
]


if TYPE_CHECKING:
from .configuration_deepseek_v2 import DeepseekV2Config

try:
if not is_sentencepiece_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
from .tokenization_deepseek_v2 import DeepseekV2Tokenizer

try:
if not is_tokenizers_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
from .tokenization_deepseek_v2_fast import DeepseekV2TokenizerFast

try:
if not is_torch_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
from .modeling_deepseek_v2 import (
DeepseekV2ForCausalLM,
DeepseekV2ForQuestionAnswering,
DeepseekV2ForSequenceClassification,
DeepseekV2ForTokenClassification,
DeepseekV2Model,
DeepseekV2PreTrainedModel,
)

try:
if not is_flax_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
from .modeling_flax_deepseek_v2 import (
FlaxDeepseekV2ForCausalLM,
FlaxDeepseekV2Model,
FlaxDeepseekV2PreTrainedModel,
)


else:
import sys

sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
32 changes: 32 additions & 0 deletions src/transformers/models/deepseek_v2_copy/comparison_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig

# Clear CUDA cache before starting
torch.cuda.empty_cache()

model_name = "deepseek-ai/DeepSeek-V2"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)

max_memory = {i: "15GB" for i in range(torch.cuda.device_count())}

model = AutoModelForCausalLM.from_pretrained(
model_name,
trust_remote_code=True,
low_cpu_mem_usage=True,
device_map="auto",
torch_dtype=torch.bfloat16,
max_memory=max_memory
)

model.generation_config = GenerationConfig.from_pretrained(model_name)
model.generation_config.pad_token_id = model.generation_config.eos_token_id

text = "An attention function can be described as"
inputs = tokenizer(text, return_tensors="pt")

inputs = inputs.to(model.device)

outputs = model.generate(**inputs, max_new_tokens=2)

result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(result)
58 changes: 58 additions & 0 deletions src/transformers/models/deepseek_v2_copy/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"architectures": [
"DeepseekV2ForCausalLM"
],
"attention_bias": false,
"attention_dropout": 0.0,
"auto_map": {
"AutoConfig": "configuration_deepseek.DeepseekV2Config",
"AutoModel": "modeling_deepseek.DeepseekV2Model",
"AutoModelForCausalLM": "modeling_deepseek.DeepseekV2ForCausalLM"
},
"aux_loss_alpha": 0.001,
"bos_token_id": 100000,
"eos_token_id": 100001,
"first_k_dense_replace": 1,
"hidden_act": "silu",
"hidden_size": 5120,
"initializer_range": 0.02,
"intermediate_size": 12288,
"kv_lora_rank": 512,
"max_position_embeddings": 163840,
"model_type": "deepseek_v2",
"moe_intermediate_size": 1536,
"moe_layer_freq": 1,
"n_group": 8,
"n_routed_experts": 160,
"n_shared_experts": 2,
"norm_topk_prob": false,
"num_attention_heads": 128,
"num_experts_per_tok": 6,
"num_hidden_layers": 60,
"num_key_value_heads": 128,
"pretraining_tp": 1,
"q_lora_rank": 1536,
"qk_nope_head_dim": 128,
"qk_rope_head_dim": 64,
"rms_norm_eps": 1e-06,
"rope_scaling": {
"beta_fast": 32,
"beta_slow": 1,
"factor": 40,
"mscale": 0.707,
"mscale_all_dim": 0.707,
"original_max_position_embeddings": 4096,
"type": "yarn"
},
"rope_theta": 10000,
"routed_scaling_factor": 16.0,
"seq_aux": true,
"tie_word_embeddings": false,
"topk_group": 3,
"topk_method": "group_limited_greedy",
"torch_dtype": "bfloat16",
"transformers_version": "4.39.3",
"use_cache": true,
"v_head_dim": 128,
"vocab_size": 102400
}
Loading
Loading