-
Notifications
You must be signed in to change notification settings - Fork 34.3k
Add deepseek 3.2 exp #41251
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 3.2 exp #41251
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
e715cfe
Add DeepSeek V3.2-Exp (DeepSeek Sparse Attention)
ArthurZucker 5f364a8
Indexer returns the additive index mask directly (drop unused top-k i…
ArthurZucker 8f1a76f
Make deepseek_v32 a lean DeepSeek-V3 + DSA model; glm_moe_dsa inherit…
ArthurZucker 4c2576e
Address review: matmul-only indexer, explicit attention forward, impo…
ArthurZucker e89da2a
Revert DynamicCache __iter__ / ddp churn for indexer keys
ArthurZucker e92bdcc
Docs: drop the GlmMoeDsa TIP and contributor line per review
ArthurZucker 2803e9c
Gate DSA mask materialization to eager/SDPA; indexer returns top-k in…
ArthurZucker 27056a3
Simplify deepseek_v32 checkpoint conversion to the qwen2_moe base
ArthurZucker 5d22edc
Fix CI: config-attributes check, indexer fp32 dtype + causality
ArthurZucker 0366c77
Make first_k_dense_replace configurable and fix tiny-config MoE test …
ArthurZucker 2499d54
more efficient rotary pos emb
ArthurZucker caa2bdf
manual cleanups
ArthurZucker b5ec169
up
ArthurZucker 15df809
updates
ArthurZucker c7d0049
nits
ArthurZucker b56feb3
nits
ArthurZucker 612e6f5
nit
ArthurZucker f700d25
nits
ArthurZucker 1a4be19
remove the overwrite with a post_init fix?
ArthurZucker fa90aaa
fix tests
ArthurZucker bc9190e
long context test
ArthurZucker 17414ce
Merge branch 'add-deepseek-exp' of github.com:huggingface/transformer…
ArthurZucker ef4e5e8
Tidy deepseek_v32 config comments
ArthurZucker 21cf984
Merge remote-tracking branch 'origin/main' into add-deepseek-exp
ArthurZucker c0e5f2d
add padded test and start styling
ArthurZucker 806e502
nit
ArthurZucker f1e4ebf
Merge branch 'add-deepseek-exp' of github.com:huggingface/transformer…
ArthurZucker bc3e588
Apply ruff format to deepseek_v32 / glm_moe_dsa modular files
ArthurZucker 7f09fcd
Merge branch 'add-deepseek-exp' of github.com:huggingface/transformer…
ArthurZucker 87c4504
Add publish/contribute dates to deepseek_v32 model card
ArthurZucker 42eec21
Merge branch 'main' into add-deepseek-exp
vasqu 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,100 @@ | ||
| <!--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. | ||
|
|
||
| --> | ||
| *This model was published in HF papers on 2025-12-02 and contributed to Hugging Face Transformers on 2026-06-10.* | ||
|
|
||
| <div style="float: right;"> | ||
| <div class="flex flex-wrap space-x-1"> | ||
| <img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white"> | ||
| <img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat"> | ||
| <img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white"> | ||
| <img alt="FP8" src="https://img.shields.io/badge/FP8-4d8a4d?style=flat"> | ||
| </div> | ||
| </div> | ||
|
|
||
| # DeepSeek-V3.2 | ||
|
|
||
| ## Overview | ||
|
|
||
| [DeepSeek-V3.2-Exp](https://huggingface.co/deepseek-ai/DeepSeek-V3.2-Exp) is an experimental release from DeepSeek-AI that introduces **DeepSeek Sparse Attention (DSA)**, a trainable, fine-grained sparse attention mechanism designed to improve training and inference efficiency in long-context scenarios. It is built directly on top of [DeepSeek-V3.1-Terminus](https://huggingface.co/deepseek-ai/DeepSeek-V3.1-Terminus): the model keeps the same 685B-parameter Mixture-of-Experts (MoE) backbone and Multi-head Latent Attention (MLA), and is obtained through continued training that adds the sparse-attention indexer while deliberately aligning the training distribution with V3.1-Terminus so the two models can be compared head-to-head. | ||
|
|
||
| The work was later extended in the [DeepSeek-V3.2 technical report](https://huggingface.co/papers/2512.02556), *DeepSeek-V3.2: Pushing the Frontier of Open Large Language Models*, which pairs DSA with a scalable reinforcement-learning framework and reports gold-medal level results on competition math (IMO) and competitive programming (IOI) benchmarks. | ||
|
|
||
| The abstract from the DeepSeek-V3.2-Exp release is the following: | ||
|
|
||
| *We introduce DeepSeek-V3.2-Exp, an experimental version of our model that incorporates DeepSeek Sparse Attention (DSA) to explore and validate optimizations for training and inference efficiency in long-context scenarios. DeepSeek Sparse Attention achieves fine-grained sparse attention for the first time with minimal impact on model output quality. Built upon DeepSeek-V3.1-Terminus, DeepSeek-V3.2-Exp delivers substantially improved efficiency in both training and inference, especially in long-context settings, while maintaining virtually identical benchmark performance.* | ||
|
|
||
| ### DeepSeek Sparse Attention (DSA) | ||
|
|
||
| DSA reduces the quadratic cost of attention over long sequences by attending only to a selected subset of past tokens. It has two components: | ||
|
|
||
| 1. **Lightning indexer.** A lightweight, low-head-count scoring module computes an *index score* between each query and every preceding key. In the reference implementation it runs in FP8 with a Hadamard (`rotate_activation`) transform; because the transform is orthogonal (`Hq·Hk = q·k`) and FP8 is only a precision optimization, the transformers port computes the same scores directly in bf16/fp32, keeping the indexer cheap relative to the main attention. | ||
| 2. **Fine-grained token selection.** For each query the indexer keeps the top-`index_topk` (2048 by default) tokens, and main MLA attention is then computed only over those tokens via an additive mask. This turns the per-query attention cost from `O(L)` to `O(index_topk)` for long sequences when using `flash_mla`, which is not supported yet 😉. | ||
|
|
||
| The indexer keeps its own small per-token key cache (single-head, `index_head_dim`) alongside the main K/V cache. In transformers this lives on a dedicated cache layer — [`DynamicIndexedLayer`] for growing caches and [`StaticIndexedLayer`] for static / `torch.compile` caches — and is updated through `past_key_values.update_indexer()`. | ||
|
|
||
| In DeepSeek-V3.2 **every layer runs its own indexer** — there is no cross-layer top-k sharing. | ||
|
|
||
| > [!NOTE] | ||
| > **The MLA query LoRA path (`q_lora_rank`) is required.** The indexer scores queries from the low-rank query latent `q_a_layernorm(q_a_proj(x))` (its `wq_b` projection is sized by `q_lora_rank`), so the model always uses the LoRA query path and `q_lora_rank` must be set — the released checkpoint uses `1536`. The optional non-LoRA `q_proj` path that [DeepSeek-V3](./deepseek_v3) exposes for `q_lora_rank=None` is **not supported** here: without the query latent there is nothing for the indexer to consume. | ||
|
|
||
| ## Usage examples | ||
|
|
||
| DeepSeek-V3.2-Exp is distributed as an FP8 checkpoint. The indexer projections are kept out of FP8 quantization, since the checkpoint stores them in bf16/fp32: | ||
|
|
||
| ```python | ||
| from transformers import FineGrainedFP8Config, AutoModelForCausalLM, AutoTokenizer | ||
| import torch | ||
|
|
||
| model_name = "deepseek-ai/DeepSeek-V3.2-Exp" | ||
| quantization_config = FineGrainedFP8Config( | ||
| modules_to_not_convert=["model.layers.*.mlp.gate.*", "*.self_attn.indexer.weights_proj.*"], | ||
| weight_block_size=(128, 128), | ||
| ) | ||
| model = AutoModelForCausalLM.from_pretrained( | ||
| model_name, | ||
| torch_dtype="auto", | ||
| device_map="auto", | ||
| quantization_config=quantization_config, | ||
| ) | ||
| tokenizer = AutoTokenizer.from_pretrained(model_name) | ||
|
|
||
| inputs = tokenizer("What are we having for dinner?", return_tensors="pt").to(model.device) | ||
| outputs = model.generate(**inputs, max_new_tokens=20) | ||
| print(tokenizer.decode(outputs[0], skip_special_tokens=True)) | ||
| ``` | ||
|
|
||
| The original code can be found [here](https://github.com/deepseek-ai/DeepSeek-V3.2-Exp). | ||
|
|
||
| ## DeepseekV32Config | ||
|
|
||
| [[autodoc]] DeepseekV32Config | ||
|
|
||
| ## DeepseekV32PreTrainedModel | ||
|
|
||
| [[autodoc]] DeepseekV32PreTrainedModel | ||
| - forward | ||
|
|
||
| ## DeepseekV32Model | ||
|
|
||
| [[autodoc]] DeepseekV32Model | ||
| - forward | ||
|
|
||
| ## DeepseekV32ForCausalLM | ||
|
|
||
| [[autodoc]] DeepseekV32ForCausalLM |
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
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.