-
Notifications
You must be signed in to change notification settings - Fork 34.3k
Add DeepSeek V4 #45643
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
Add DeepSeek V4 #45643
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
26c62d0
Add DeepSeek V4 (modular)
ArthurZucker 8b3c91a
Split V4 HCA / CSA caches and compressors into independent classes
ArthurZucker b4b3a20
Fix tests_generate / tests_tensor_parallel CI failures
ArthurZucker 6baa653
Address PR review feedback batch (comments 2-24)
ArthurZucker 838cc0c
Address PR review feedback batch (comments 25-42)
ArthurZucker f18a6b8
Fix Fp8Dequantize.reverse_op to actually re-quantize on save
ArthurZucker 921a8dc
Address Arthur's review batch + revisit two of vasqu's comments
ArthurZucker 6d82332
Apply suggestions from code review
ArthurZucker 4ee8e47
Move DeepseekV4Config out of modular + simplify __post_init__
ArthurZucker ea29922
Fix V4 TP failures: dynamic num_key_value_groups + FP8-safe GroupedLi…
ArthurZucker 092dcd6
Revert GroupedLinear F.linear loop, keep bmm
ArthurZucker def7199
Fix V4 GroupedLinear comment with real V4-Flash / V4-Pro config values
ArthurZucker 631151d
up
ArthurZucker fa0369f
up
ArthurZucker 5f711e0
small cleanup
ArthurZucker b5998a0
repo fixes
ArthurZucker dcbe66b
nits
ArthurZucker aecf319
more nits
ArthurZucker 4fc49a0
nits, small thing left to do
ArthurZucker 02356fe
update
ArthurZucker 8124d3e
update DeepseekV4HCACache
ArthurZucker 45c4323
more update
ArthurZucker a48b51b
nits
ArthurZucker 46bc564
update
ArthurZucker 240647a
Merge branch 'add-deepseek-v4' of github.com:huggingface/transformers…
ArthurZucker 5eb7bec
Merge branch 'add-deepseek-v4' of github.com:huggingface/transformers…
ArthurZucker 8f807c1
update
ArthurZucker 0c62b58
nits
ArthurZucker e32e9a8
update
ArthurZucker 5b125c6
fixes
ArthurZucker 07aca9f
nits
ArthurZucker 913adcc
Merge branch 'main' of https://github.com/huggingface/transformers in…
ArthurZucker 7b5b54a
Fix CI: \N>1 backrefs in conversion mapping + drop irrelevant drift
ArthurZucker 7feb6a1
Drop TP plan from V4 attention path; keep EP-only on experts
ArthurZucker d515ec5
Address vasqu review batch + skip quantized-TP test
ArthurZucker 65e564f
Drop model. prefix from V4 conversion mapping + tester / parity cleanup
ArthurZucker a021e84
Silence Unrecognized rope_parameters keys warning
ArthurZucker 0e35cfd
Disable flex_attention on V4; guard tensor-only mask pad
ArthurZucker a55ffee
Switch V4 to EP-only plan (gpt-oss style)
ArthurZucker d86d46d
Fix V4 yarn rope validation under nested rope_parameters
ArthurZucker ff720b3
Trim V4 config-attribute allowlist from 24 → 6
ArthurZucker c946087
Inline V4 config legacy-kwarg + resolve_* helpers into __post_init__
ArthurZucker 62a75c0
Disable FlashAttention on V4: head_dim=512 exceeds the 256 cap
ArthurZucker 08e4cf8
update
ArthurZucker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <!--Copyright 2026 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 contains specific syntax for our doc-builder (similar to MDX) that may not be | ||
| rendered properly in your Markdown viewer. | ||
|
|
||
| --> | ||
| *This model was released on {release_date} and added to Hugging Face Transformers on 2026-05-02.* | ||
|
|
||
| # DeepSeek-V4 | ||
|
|
||
| [DeepSeek-V4](https://huggingface.co/deepseek-ai) is the next-generation MoE language model from DeepSeek | ||
| ([paper](https://huggingface.co/deepseek-ai/DeepSeek-V4-Flash/blob/main/DeepSeek_V4.pdf)). The architecture replaces | ||
| DeepSeek-V3's Multi-head Latent Attention (MLA) with a hybrid local + long-range design, swaps residual connections | ||
| for Manifold-Constrained Hyper-Connections (mHC), and bootstraps the first few MoE layers with a static | ||
| token-id → expert-id hash table. | ||
|
|
||
| This implementation covers `DeepSeek-V4-Flash`, `DeepSeek-V4-Pro`, and their `-Base` pretrained siblings. All four | ||
| share the same architecture; they differ only in width / depth / expert count and weights. | ||
|
|
||
| ## Architecture (paper §2) | ||
|
|
||
| ### Hybrid attention (§2.3) | ||
|
|
||
| Each decoder block is one of three attention types, dispatched by `config.layer_types[i]`: | ||
|
|
||
| * **Sliding-window full attention** (`"sliding_attention"`): only the local window of `sliding_window` tokens, no | ||
| long-range branch. Matches V3's "Full Attention" style for the bootstrap layers. | ||
| * **Compressed Sparse Attention** (`"compressed_sparse_attention"`, **CSA** — paper §2.3.1): a low-compression | ||
| pool (`compress_rate_csa`, default `m=4`) with overlapping windows, plus a **Lightning Indexer** (eqs. 13–17) | ||
| that scores queries against the pool and gathers the top `index_topk` blocks per query before they reach core | ||
| attention. | ||
| * **Heavily Compressed Attention** (`"heavily_compressed_attention"`, **HCA** — paper §2.3.2): a high-compression | ||
| pool (`compress_rate_hca`, default `m'=128`) with non-overlapping windows. No indexer — every pooled entry | ||
| contributes to attention. | ||
|
ArthurZucker marked this conversation as resolved.
|
||
|
|
||
| All three types share the same backbone: | ||
|
|
||
| * **Shared K=V Multi-Query Attention**: `num_key_value_heads = 1`; `kv_proj` produces a single KV head and the same | ||
| tensor is read as both key and value. | ||
| * **Partial RoPE** (interleaved-pair, paper §2.3.3 "Partial Rotary Positional Embedding") on the trailing | ||
| `qk_rope_head_dim = head_dim * partial_rotary_factor` channels of each head. The same rotation is applied with | ||
| position `-i` to the attention output's rope slice (eq. 26) so the contribution of each KV entry stays a function | ||
| of the *relative* distance to the query. | ||
| * **Per-head learnable attention sink** (eq. 27). | ||
| * **Grouped low-rank output projection** (§2.3.1 "Grouped Output Projection"): `o_groups` head-groups → `o_lora_rank` | ||
| per group → `hidden_size`, computed by [`DeepseekV4GroupedLinear`] (`o_a_proj`) followed by `o_b_proj`. Cuts the | ||
| per-token cost of the wide attention output without losing expressivity. | ||
| * **Shared sliding-window K=V branch** of size `sliding_window` ("Additional Branch of Sliding Window Attention", | ||
| §2.3.1) preserves local fine-grained dependencies; the long-range compressor's output is concatenated with this | ||
| branch's KVs before core attention. | ||
|
|
||
| ### Manifold-Constrained Hyper-Connections (§2.2) | ||
|
|
||
| Residual connections are replaced by mHC (Xie et al., 2026): `hc_mult` parallel residual streams kept in shape | ||
| `[B, S, hc_mult, D]` throughout each block. Two [`DeepseekV4HyperConnection`] modules — `attn_hc` and `ffn_hc` — mix | ||
| streams in and out around the attention / MLP sublayers via a `(pre, post, comb)` triplet. The `comb` matrix is a | ||
| doubly-stochastic projection produced by `hc_sinkhorn_iters` Sinkhorn–Knopp iterations on the manifold, making | ||
| signal propagation non-expansive across deep stacks. A final [`DeepseekV4HyperHead`] collapses the `hc_mult` | ||
| streams down to a single sequence before the model norm. | ||
|
|
||
| ### MoE schedule (§2.1) | ||
|
|
||
| Routing is configured per layer by `config.mlp_layer_types`, with values from `{"hash_moe", "moe"}`: | ||
|
|
||
| * `"hash_moe"`: expert indices come from a frozen `tid2eid[input_ids]` lookup populated from the V4 checkpoint. | ||
| The learned gate `weight` still produces the per-expert scores that weight the selected experts; only | ||
| *which-experts* is static. Used for the first few bootstrap layers (default 3, override via legacy | ||
| `num_hash_layers`). | ||
| * `"moe"`: standard top-k routed MoE. The expert affinity uses **Sqrt(Softplus(·))** instead of V3's Sigmoid | ||
| ("we change the activation function that computes the affinity scores from Sigmoid(·) into Sqrt(Softplus(·))", | ||
| paper §2.1), and V3's `n_group` / `topk_group` constraint is dropped. The auxiliary-loss-free strategy | ||
| (DeepSeek's `noaux_tc`) is preserved via the `e_score_correction_bias` buffer that biases the top-k argmax | ||
| without flowing gradients. | ||
|
|
||
| Routed experts use a **clamped SwiGLU** (`gate.clamp(max=swiglu_limit)`, `up.clamp(min=-swiglu_limit, max=swiglu_limit)`, | ||
| then `act_fn(gate) * up`) on top of the standard Mixtral `[num_experts, 2 * moe_intermediate_size, hidden_size]` | ||
| expert weight layout. A single shared expert (a plain SwiGLU MLP at `moe_intermediate_size` width) runs in parallel | ||
| on every token. | ||
|
|
||
| ### Cache layers | ||
|
|
||
| Each non-sliding attention block needs to thread compressor / indexer state across forward calls. V4 ships two | ||
| cache layer types that auto-register with `LAYER_TYPE_CACHE_MAPPING`: | ||
|
|
||
| * `DeepseekV4HCACache`: sliding-window K=V + HCA compressor buffer / pool / count (no overlap, no indexer). | ||
| * `DeepseekV4CSACache`: sliding-window K=V + CSA compressor (with overlap state) + parallel indexer | ||
| buffer / pool / count / overlap at `index_head_dim`. | ||
|
|
||
| `DynamicCache(config=…)` builds the right cache layer per `config.layer_types[i]`. | ||
|
|
||
| ## DeepseekV4Config | ||
|
|
||
| [[autodoc]] DeepseekV4Config | ||
|
|
||
| ## DeepseekV4Model | ||
|
|
||
| [[autodoc]] DeepseekV4Model | ||
| - forward | ||
|
|
||
| ## DeepseekV4ForCausalLM | ||
|
|
||
| [[autodoc]] DeepseekV4ForCausalLM | ||
| - forward | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.