-
Notifications
You must be signed in to change notification settings - Fork 427
[DSV3] Adding deepseek-v3 model into torchtitan #1373
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2fe8ffa
remove CODEOWNERS
wwwjn 9ec5113
Implement Deepseek-V3 model skeleton (#1315)
wwwjn 70f09e5
[DSV3] Forward and backward pass for single GPU (#1320)
wwwjn 830e9f4
[DSV3] Adding 16B model training config, Enable FSDP and AC on DSV3-1…
wwwjn 72109b9
[DSV3] Apply TP on DSV3 (#1341)
wwwjn 55aef3a
Fix CI (#1366)
H-Huang e61d46a
rebase onto main branch
wwwjn b33933c
add README
wwwjn 3674ca8
restore unrelated files
wwwjn d7bceb0
update README
wwwjn 3997323
add 671B model config
wwwjn d882f49
address comments
wwwjn b787bf2
fix TP
wwwjn df96da9
lint
wwwjn 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,54 @@ | ||
# DeepSeek-V3 in TorchTitan | ||
|
||
DeepSeek-V3 is a Mixture-of-Experts (MoE) transformer model with Multi-head Latent Attention (MLA) architecture. | ||
|
||
## Setup | ||
|
||
### Download Tokenizer | ||
|
||
```bash | ||
# DeepSeek tokenizer (automatically downloads tokenizer.json and tokenizer_config.json) | ||
python scripts/download_tokenizer.py --repo_id deepseek-ai/DeepSeek-V3 | ||
``` | ||
|
||
## Training | ||
|
||
### Debug Training | ||
|
||
```bash | ||
# Quick debug run with small model | ||
CONFIG_FILE="./torchtitan/models/deepseek_v3/train_configs/debug_model.toml" ./run_train.sh | ||
``` | ||
|
||
### Full Model Training | ||
|
||
```bash | ||
# 16B parameter model: adapted from older 16B parameter model from https://huggingface.co/deepseek-ai/deepseek-moe-16b-base | ||
CONFIG_FILE="./torchtitan/models/deepseek_v3/train_configs/deepseek_v3_16b.toml" ./run_train.sh | ||
``` | ||
|
||
```bash | ||
# 671B parameter model | ||
CONFIG_FILE="./torchtitan/models/deepseek_v3/train_configs/deepseek_v3_671b.toml" ./run_train.sh | ||
``` | ||
|
||
|
||
## Supported Features | ||
- FSDP, HSDP | ||
- Activation checkpointing | ||
- Tensor Parallel (TP) | ||
- Expert Parallel (EP) | ||
|
||
|
||
## To be added | ||
- Modeling | ||
- Merge DeepSeek-V3 and Llama4 MoE common components | ||
- Attention Layer: need to pass softmax_scale to sdpa() to support scaling | ||
- Parallelism | ||
- Context Parallel support for DeepSeek-V3 | ||
- PP support for DeepSeek-V3 | ||
- torch.compile | ||
- Quantization | ||
- Testing | ||
- perfomance and loss converging tests | ||
- CI integration |
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,126 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
# | ||
# Copyright (c) Meta Platforms, Inc. All Rights Reserved. | ||
|
||
from torchtitan.components.loss import build_cross_entropy_loss | ||
from torchtitan.components.lr_scheduler import build_lr_schedulers | ||
from torchtitan.components.tokenizer import build_hf_tokenizer | ||
from torchtitan.datasets.hf_datasets import build_hf_dataloader | ||
from torchtitan.experiments.llama4.optimizer import build_llama4_optimizers | ||
|
||
from torchtitan.protocols.train_spec import register_train_spec, TrainSpec | ||
|
||
from .infra.parallelize import parallelize_deepseekv3 | ||
from .model.args import DeepSeekV3ModelArgs | ||
from .model.model import DeepSeekV3Model | ||
|
||
__all__ = [ | ||
"parallelize_deepseekv3", | ||
"DeepseekV3ModelArgs", | ||
"DeepseekV3Model", | ||
"deepseekv3_configs", | ||
] | ||
|
||
|
||
deepseekv3_configs = { | ||
"debugmodel": DeepSeekV3ModelArgs( | ||
vocab_size=102400, | ||
dim=256, | ||
inter_dim=1024, | ||
moe_inter_dim=256, | ||
n_layers=3, | ||
n_dense_layers=1, | ||
n_heads=16, | ||
n_routed_experts=8, | ||
n_shared_experts=2, | ||
n_activated_experts=3, | ||
route_scale=1.0, | ||
q_lora_rank=0, | ||
kv_lora_rank=512, | ||
qk_nope_head_dim=128, | ||
qk_rope_head_dim=64, | ||
v_head_dim=128, | ||
mscale=0.70, | ||
), | ||
"16B": DeepSeekV3ModelArgs( | ||
vocab_size=102400, | ||
dim=2048, | ||
inter_dim=10944, | ||
moe_inter_dim=1408, | ||
n_layers=27, | ||
n_dense_layers=1, | ||
n_heads=16, | ||
n_routed_experts=64, | ||
n_shared_experts=2, | ||
n_activated_experts=6, | ||
route_scale=1.0, | ||
q_lora_rank=0, | ||
kv_lora_rank=512, | ||
qk_nope_head_dim=128, | ||
qk_rope_head_dim=64, | ||
v_head_dim=128, | ||
mscale=0.70, | ||
), | ||
"236B": DeepSeekV3ModelArgs( | ||
vocab_size=102400, | ||
dim=5120, | ||
inter_dim=12288, | ||
moe_inter_dim=1536, | ||
n_layers=60, | ||
n_dense_layers=1, | ||
n_heads=128, | ||
n_routed_experts=160, | ||
n_shared_experts=2, | ||
n_activated_experts=6, | ||
n_expert_groups=8, | ||
n_limited_groups=3, | ||
route_scale=16.0, | ||
q_lora_rank=1536, | ||
kv_lora_rank=512, | ||
qk_nope_head_dim=128, | ||
qk_rope_head_dim=64, | ||
v_head_dim=128, | ||
), | ||
"671B": DeepSeekV3ModelArgs( | ||
vocab_size=129280, | ||
dim=7168, | ||
inter_dim=18432, | ||
moe_inter_dim=2048, | ||
n_layers=61, | ||
n_dense_layers=3, | ||
n_heads=128, | ||
n_routed_experts=256, | ||
n_shared_experts=1, | ||
n_activated_experts=8, | ||
n_expert_groups=8, | ||
n_limited_groups=4, | ||
route_scale=2.5, | ||
score_func="sigmoid", | ||
q_lora_rank=1536, | ||
kv_lora_rank=512, | ||
qk_nope_head_dim=128, | ||
qk_rope_head_dim=64, | ||
v_head_dim=128, | ||
dtype="fp8", | ||
), | ||
} | ||
|
||
|
||
register_train_spec( | ||
TrainSpec( | ||
name="deepseek_v3", | ||
cls=DeepSeekV3Model, | ||
config=deepseekv3_configs, | ||
parallelize_fn=parallelize_deepseekv3, | ||
pipelining_fn=None, | ||
build_optimizers_fn=build_llama4_optimizers, # use optimizer hooks to update expert weights | ||
build_lr_schedulers_fn=build_lr_schedulers, | ||
build_dataloader_fn=build_hf_dataloader, | ||
build_tokenizer_fn=build_hf_tokenizer, | ||
build_loss_fn=build_cross_entropy_loss, | ||
) | ||
) |
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.