Skip to content

[WIP] add deepseek-v3 #35926

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

Merged
merged 101 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
b926c3d
init commit
ArthurZucker Sep 9, 2024
c62c5b7
Merge branch 'main' of github.com:huggingface/transformers into updat…
ArthurZucker Sep 9, 2024
5b85023
style
ArthurZucker Sep 9, 2024
3b76bda
take comments into account
ArthurZucker Sep 9, 2024
704767e
add deepseekv3 modeling
bzantium Jan 28, 2025
737ee3a
Merge branch 'main' into feature/#35425
bzantium Jan 28, 2025
fc3a4c7
Merge branch 'main' of https://github.com/bzantium/transformers into …
bzantium Jan 28, 2025
244e793
remove redundant code
bzantium Jan 28, 2025
0968df5
Merge branch 'feature/#35425' of https://github.com/bzantium/transfor…
bzantium Jan 28, 2025
4fb2a80
apply make style
bzantium Jan 28, 2025
6b002e5
apply fix-copies
bzantium Jan 28, 2025
4ec1e88
make format
bzantium Jan 28, 2025
114ab84
add init files
bzantium Jan 28, 2025
779f8d2
rename deepseekv3 into deepseek_v3 based on its model_type
bzantium Jan 28, 2025
22623a3
rename deepseekv3 into deepseek_v3 based on its model_type
bzantium Jan 28, 2025
78b19b0
deepseek-v3 not deepseek_v3
bzantium Jan 28, 2025
eb0e3a4
set model_type as deepseek_v3
bzantium Jan 28, 2025
57088cc
use default docs
bzantium Jan 28, 2025
0ef561b
apply make
bzantium Jan 28, 2025
9a75a56
fill type and docstring
bzantium Jan 28, 2025
cdf83e4
add rope_config_validation
bzantium Jan 29, 2025
51990b9
use custom DeepseekV3MLP
bzantium Jan 29, 2025
f4f0ebd
hold code only for checkpoints congifuration; remove redundant
bzantium Jan 30, 2025
4b72b30
revise rope yarn for DeepSeek variation
bzantium Jan 30, 2025
96562c4
Merge branch 'main' into feature/#35425
bzantium Jan 30, 2025
6792cb5
rename DeepSeek-V3
bzantium Jan 30, 2025
3bf3b32
some refactoring
ArthurZucker Jan 31, 2025
24bc8b2
revise load_hook to work properly; make moe func trainable; use llama…
bzantium Jan 31, 2025
5c0cd91
fix attention forward
bzantium Jan 31, 2025
8e994dd
use -1 for not-changing dim when to use exapnd
bzantium Feb 1, 2025
7405a95
refactor DeepseekV3TopkRouter
bzantium Feb 1, 2025
ea3c922
use reshape_for_rope instead of load_hook; revise attention forward f…
bzantium Feb 3, 2025
c813268
register pre_hook and hook both
bzantium Feb 3, 2025
4ab2f9e
make style
bzantium Feb 3, 2025
c5429ec
use n_shared_experts
bzantium Feb 10, 2025
4df42f0
Update src/transformers/models/deepseek_v3/configuration_deepseek_v3.py
bzantium Feb 14, 2025
e0a49ac
Merge branch 'main' of github.com:huggingface/transformers into featu…
Feb 14, 2025
dfd9abc
Merge branch 'feature/#35425' of github.com:bzantium/transformers int…
Feb 14, 2025
ba21b7c
add test file
bzantium Feb 15, 2025
2270173
Merge branch 'feature/#35425' of https://github.com/bzantium/transfor…
bzantium Feb 15, 2025
b5f420b
update modeling_file according to modular file
bzantium Feb 15, 2025
6bd75a9
make style
bzantium Feb 15, 2025
6ccbc66
add mapping for DeepseekV3ForSequenceClassification
bzantium Feb 15, 2025
a1c6274
remove aux_loss_alpha
bzantium Feb 15, 2025
a80462b
add deepseek_v3 for perf
bzantium Feb 15, 2025
dd78f48
add deepseek_v3
bzantium Feb 15, 2025
54481ef
rename test as deepseekv3
bzantium Feb 15, 2025
e0f1c2d
use tiny-deepseek-v3
bzantium Feb 15, 2025
23fb756
Merge branch 'main' into feature/#35425
bzantium Feb 15, 2025
5214741
remove DeepseekV3ForSequenceClassification
bzantium Feb 15, 2025
67f1f0c
cache before padding
bzantium Feb 15, 2025
f264f80
remote output_router_logits
bzantium Feb 18, 2025
d4c6a1b
Revert "remote output_router_logits"
bzantium Feb 18, 2025
c7c8d76
remove output_router_logits
bzantium Feb 18, 2025
0b5ff07
Merge branch 'main' into feature/#35425
bzantium Feb 18, 2025
ba6f7d4
make e_score_correction_bias as buffer
bzantium Feb 18, 2025
d7931b3
skip tests not compatible
bzantium Feb 18, 2025
92bd99c
make style
bzantium Feb 18, 2025
7d81efe
make e_score_correction_bias as buffer
bzantium Feb 18, 2025
b33fdb5
use rope_interleave instead of load_hook
bzantium Feb 19, 2025
7f859f8
skip tests not compatible with MLA
bzantium Feb 19, 2025
397ecf3
add doc for rope_interleave
bzantium Feb 19, 2025
2628438
fix typo
bzantium Feb 19, 2025
af3d328
remove torch.no_grad for selecting topk
bzantium Feb 19, 2025
f0357f9
Merge branch 'main' of github.com:huggingface/transformers into featu…
ArthurZucker Mar 24, 2025
14e7d4e
fix post merge issue
ArthurZucker Mar 24, 2025
5c85490
Merge branch 'main' of github.com:huggingface/transformers into updat…
ArthurZucker Mar 24, 2025
1d8516d
mrege with main and simplify
ArthurZucker Mar 24, 2025
9b4f433
nits
ArthurZucker Mar 24, 2025
abffdfe
final
ArthurZucker Mar 24, 2025
6c7eaa5
small fixes
ArthurZucker Mar 24, 2025
71d47f4
Merge branch 'main' of github.com:huggingface/transformers into updat…
ArthurZucker Mar 24, 2025
9e4965a
fix
ArthurZucker Mar 24, 2025
6bb8802
support TP better
ArthurZucker Mar 25, 2025
426d941
stash
ArthurZucker Mar 25, 2025
d4d60c3
Merge branch 'update-from-pretrained' of github.com:huggingface/trans…
ArthurZucker Mar 25, 2025
f0a8389
changes currently requires
ArthurZucker Mar 25, 2025
4b8a857
remove synch
ArthurZucker Mar 25, 2025
eedbf59
more fixes for TP
ArthurZucker Mar 25, 2025
409f341
temp fix for TP : some attention layers's FP8 scales are too small + …
ArthurZucker Mar 26, 2025
3fb9bea
updates to have generation work!
ArthurZucker Mar 27, 2025
7350a5d
push most of the changes
ArthurZucker Mar 28, 2025
a50c351
reorder functions + call for contributions!
ArthurZucker Mar 28, 2025
24557c3
update readme
ArthurZucker Mar 28, 2025
d7da38b
nits
ArthurZucker Mar 28, 2025
186e32b
update
ArthurZucker Mar 28, 2025
c198b4b
Merge branch 'main' of github.com:huggingface/transformers into featu…
ArthurZucker Mar 28, 2025
ee33cf7
ruff was updated on main
ArthurZucker Mar 28, 2025
f2bb6f9
merge with main and fix copies
ArthurZucker Mar 28, 2025
8cefd1c
revert unrelated changes
ArthurZucker Mar 28, 2025
a8fff20
route all tokens to all experts when testing to avoid no gradient iddues
ArthurZucker Mar 28, 2025
13019a7
finish fixing all tests
ArthurZucker Mar 28, 2025
9b310a1
fixup
ArthurZucker Mar 28, 2025
e3628a3
nit
ArthurZucker Mar 28, 2025
9eb38e6
clean config
ArthurZucker Mar 28, 2025
8cb959b
last readme changes
ArthurZucker Mar 28, 2025
a55630b
nit
ArthurZucker Mar 28, 2025
bce2073
do cnit
ArthurZucker Mar 28, 2025
a1f1f3f
typo
ArthurZucker Mar 28, 2025
d2ae072
last nit
ArthurZucker Mar 28, 2025
372efd6
one more one more
ArthurZucker Mar 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Flax), PyTorch, and/or TensorFlow.
| [DeBERTa](model_doc/deberta) | ✅ | ✅ | ❌ |
| [DeBERTa-v2](model_doc/deberta-v2) | ✅ | ✅ | ❌ |
| [Decision Transformer](model_doc/decision_transformer) | ✅ | ❌ | ❌ |
| [Deepseek-V3](model_doc/deepseek_v3) | ✅ | ❌ | ❌ |
| [Deformable DETR](model_doc/deformable_detr) | ✅ | ❌ | ❌ |
| [DeiT](model_doc/deit) | ✅ | ✅ | ❌ |
| [DePlot](model_doc/deplot) | ✅ | ❌ | ❌ |
Expand Down
46 changes: 46 additions & 0 deletions docs/source/en/model_doc/deepseek_v3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!--Copyright 2025 The HuggingFace 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.

⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

-->

# DeepSeek-V3

## Overview

The DeepSeek-V3 model was proposed in [DeepSeek-V3 Technical Report](https://arxiv.org/abs/2412.19437) by DeepSeek-AI Team.

The abstract from the paper is the following:
We present DeepSeek-V3, a strong Mixture-of-Experts (MoE) language model with 671B total parameters with 37B activated for each token. To achieve efficient inference and cost-effective training, DeepSeek-V3 adopts Multi-head Latent Attention (MLA) and DeepSeekMoE architectures, which were thoroughly validated in DeepSeek-V2. Furthermore, DeepSeek-V3 pioneers an auxiliary-loss-free strategy for load balancing and sets a multi-token prediction training objective for stronger performance. We pre-train DeepSeek-V3 on 14.8 trillion diverse and high-quality tokens, followed by Supervised Fine-Tuning and Reinforcement Learning stages to fully harness its capabilities. Comprehensive evaluations reveal that DeepSeek-V3 outperforms other open-source models and achieves performance comparable to leading closed-source models. Despite its excellent performance, DeepSeek-V3 requires only 2.788M H800 GPU hours for its full training. In addition, its training process is remarkably stable. Throughout the entire training process, we did not experience any irrecoverable loss spikes or perform any rollbacks. The model checkpoints are available at https://github.com/deepseek-ai/DeepSeek-V3.

### Usage tips
The model uses Multi-head Latent Attention (MLA) and DeepSeekMoE architectures for efficient inference and cost-effective training. It employs an auxiliary-loss-free strategy for load balancing and multi-token prediction training objective. The model can be used for various language tasks after being pre-trained on 14.8 trillion tokens and going through Supervised Fine-Tuning and Reinforcement Learning stages.

## DeepseekV3Config

[[autodoc]] DeepseekV3Config

## DeepseekV3Model

[[autodoc]] DeepseekV3Model
- forward

## DeepseekV3ForCausalLM

[[autodoc]] DeepseekV3ForCausalLM
- forward

## DeepseekV3ForSequenceClassification

[[autodoc]] DeepseekV3ForSequenceClassification
- forward
18 changes: 18 additions & 0 deletions src/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
],
"models.deberta_v2": ["DebertaV2Config"],
"models.decision_transformer": ["DecisionTransformerConfig"],
"models.deepseek_v3": ["DeepseekV3Config"],
"models.deformable_detr": ["DeformableDetrConfig"],
"models.deit": ["DeiTConfig"],
"models.deprecated": [],
Expand Down Expand Up @@ -1956,6 +1957,14 @@
"DecisionTransformerPreTrainedModel",
]
)
_import_structure["models.deepseek_v3"].extend(
[
"DeepseekV3ForCausalLM",
"DeepseekV3ForSequenceClassification",
"DeepseekV3Model",
"DeepseekV3PreTrainedModel",
]
)
_import_structure["models.deformable_detr"].extend(
[
"DeformableDetrForObjectDetection",
Expand Down Expand Up @@ -5395,6 +5404,9 @@
from .models.decision_transformer import (
DecisionTransformerConfig,
)
from .models.deepseek_v3 import (
DeepseekV3Config,
)
from .models.deformable_detr import (
DeformableDetrConfig,
)
Expand Down Expand Up @@ -6966,6 +6978,12 @@
DecisionTransformerModel,
DecisionTransformerPreTrainedModel,
)
from .models.deepseek_v3 import (
DeepseekV3ForCausalLM,
DeepseekV3ForSequenceClassification,
DeepseekV3Model,
DeepseekV3PreTrainedModel,
)
from .models.deformable_detr import (
DeformableDetrForObjectDetection,
DeformableDetrModel,
Expand Down
1 change: 1 addition & 0 deletions src/transformers/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
deberta,
deberta_v2,
decision_transformer,
deepseek_v3,
deformable_detr,
deit,
deprecated,
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/auto/configuration_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
("deberta", "DebertaConfig"),
("deberta-v2", "DebertaV2Config"),
("decision_transformer", "DecisionTransformerConfig"),
("deepseek_v3", "DeepseekV3Config"),
("deformable_detr", "DeformableDetrConfig"),
("deit", "DeiTConfig"),
("depth_anything", "DepthAnythingConfig"),
Expand Down Expand Up @@ -406,6 +407,7 @@
("deberta", "DeBERTa"),
("deberta-v2", "DeBERTa-v2"),
("decision_transformer", "Decision Transformer"),
("deepseek_v3", "Deepseek-V3"),
("deformable_detr", "Deformable DETR"),
("deit", "DeiT"),
("deplot", "DePlot"),
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/auto/modeling_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
("deberta", "DebertaModel"),
("deberta-v2", "DebertaV2Model"),
("decision_transformer", "DecisionTransformerModel"),
("deepseek_v3", "DeepseekV3Model"),
("deformable_detr", "DeformableDetrModel"),
("deit", "DeiTModel"),
("deta", "DetaModel"),
Expand Down Expand Up @@ -501,6 +502,7 @@
("ctrl", "CTRLLMHeadModel"),
("data2vec-text", "Data2VecTextForCausalLM"),
("dbrx", "DbrxForCausalLM"),
("deepseek_v3", "DeepseekV3ForCausalLM"),
("diffllama", "DiffLlamaForCausalLM"),
("electra", "ElectraForCausalLM"),
("emu3", "Emu3ForCausalLM"),
Expand Down
7 changes: 7 additions & 0 deletions src/transformers/models/auto/tokenization_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@
"DebertaV2TokenizerFast" if is_tokenizers_available() else None,
),
),
(
"deepseek_v3",
(
"LlamaTokenizer" if is_sentencepiece_available() else None,
"LlamaTokenizerFast" if is_tokenizers_available() else None,
),
),
(
"diffllama",
(
Expand Down
27 changes: 27 additions & 0 deletions src/transformers/models/deepseek_v3/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2025 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 _LazyModule
from ...utils.import_utils import define_import_structure


if TYPE_CHECKING:
from .configuration_deepseek_v3 import *
from .modeling_deepseek_v3 import *
else:
import sys

_file = globals()["__file__"]
sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__)
Loading