Skip to content

[WIP] DeepSeek-V3.2 model support#3800

Draft
sdmyzlp wants to merge 1 commit into
pytorch:mainfrom
sdmyzlp:br_dsv32_draft
Draft

[WIP] DeepSeek-V3.2 model support#3800
sdmyzlp wants to merge 1 commit into
pytorch:mainfrom
sdmyzlp:br_dsv32_draft

Conversation

@sdmyzlp

@sdmyzlp sdmyzlp commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Updated in 7.14:

Moved code into deepseek_v3, and with first attempt for indexer loss.


Adds the DeepSeek-V3.2 (DeepSeek-V3.2-Exp) transformer model with Lightning Indexer + DeepSeek Sparse Attention (DSA). Based on DeepSeek-V3 modeling in torchtitan, and this PR depends on #3787.

Indexer — Lightning Indexer for sparse token selection

  • Projects query (via wq_b from the MLA latent query), and key (via wk) into a low-rank index space.
  • Applies a LayerNorm (k_norm), non-interleaved RoPE (rotate-half, using CosSinRoPE with yarn-scaled frequencies), and a Hadamard rotation to both index query and key.
  • Produces per-token index weights (weights_proj).
  • Static select() computes \sum_h \mathrm{relu}(q_h\cdot k) \cdot w_h, masks with the causal+document attention mask, and returns a per-token boolean selection via top-k.

DSAFlexAttention — fused sparse inner attention

  • Extends FlexAttention. Calls Indexer.select to get the per-token selection, composes it with the document mask into a combined BlockMask, then delegates to standard flex_attention with that sparse mask.
  • Static build_dsa_block_mask builds the combined BlockMask.

Attention — V3 MLA + Indexer + DSA

  • Extends DeepSeek-V3's Attention. In addition to the MLA q/k/v projections, it runs the Indexer on (x, qr) and passes the index tensors into DSAFlexAttention.

Sharding

  • Indexer weights: wq_b / weights_proj colwise S(0)@TP; wk / k_norm / rope cache Replicate@TP.
  • Inner attention: all positional inputs are head-allgathered to Replicate@TP (q/k/v are gathered for the indexer KL loss; idx_* are gathered for select()'s reduce_sum over index heads). The out_src is Replicate@TP (truthful, since the inputs are replicated), and out_dst reshards to S(2)@TP to feed the rowwise wo.

TODO

  • Indexer loss support — add the KL-loss between indexer scores and full attention scores (requires per-head attention score output and a head-dim allgather, which the current in_dst = Replicate@TP on q/k/v already prepares for).

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 26, 2026
@pytorch-bot

pytorch-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/8gpu

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@tianyu-l

Copy link
Copy Markdown
Contributor

Thanks for the proposal. I wonder what's your thought on supporting DeepSeek V4 #3634 vs. V3.2

@sdmyzlp

sdmyzlp commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

@tianyu-l Thanks for the quick response. For context: we've been maintaining V3.2 and V4 on our internal version of torchtitan, and would like to polish and contribute them upstream.

We're also actively improving the V4 PR (#3634); these are parallel efforts. Currently DeepSeek-V3.2 is put in a separate folder. Would you prefer merging it into the existing DeepSeek-V3 directory instead, since it's more of a variant of the original V3, to keep the model directory cleaner? FMPV, V3.2 is simpler, while also being able to exercise new features like indexer loss and sparse attentions.

We'd appreciate your thoughts on any potential concerns, and are willing to iterate based on the project's direction.

config.model_spec = model_registry("debugmodel")
config.parallelism = ParallelismConfig(
tensor_parallel_degree=2,
spmd_backend="full_dtensor",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are migrating from full_dtensor to spmd_types -- would you be interested in help migrate this model?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will investigate this.

Comment on lines +137 to +139
deepseekv32_configs = {
"debugmodel": _debugmodel,
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is debugmodel only, can we add real configs?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would benefit from a README.md listing out the distributed training techniques supported and verified, including parallelisms, per-layer compilation, activation checkpointing, etc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please report numerics compared with HF native impl. See https://github.com/pytorch/torchtitan/tree/main/scripts/checkpoint_conversion for more details. We don't have to land the custom scripts, but good to convince ourselves.

@sdmyzlp sdmyzlp Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for the pointer, I briefly tested with fp32 against DeepSeek-V3.2 HF, confirmed bitwise-exact logits given CosSinRoPE is modified per #3787. Will share a more comprehensive result later.

@sdmyzlp sdmyzlp marked this pull request as draft June 28, 2026 04:14
@sdmyzlp sdmyzlp changed the title [RFC and WIP] DeepSeek-V3.2 model support [WIP] DeepSeek-V3.2 model support Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants