Skip to content

[Feature] Support apply_liger_kernel_to_deepseek_v3 - #1424

Open
Yulong-Cauli wants to merge 1 commit into
linkedin:mainfrom
Yulong-Cauli:feat/deepseek-v3-support
Open

Yulong-Cauli wants to merge 1 commit into
linkedin:mainfrom
Yulong-Cauli:feat/deepseek-v3-support

Conversation

@Yulong-Cauli

Copy link
Copy Markdown
Contributor

Summary

Adds Liger Kernel support for DeepSeek-V3 (deepseek_v3 model type), closing the gap left open by #623.

  • model/deepseek_v3.py: new lce_forward. In training with labels, skip_logits defaults to True, so logits are never materialized (FLCE path via LigerForCausalLMLoss); otherwise logits are materialized and the HF-native self.loss_function is used. Returns LigerCausalLMOutputWithPast with token_accuracy / predicted_tokens.
  • monkey_patch.py: apply_liger_kernel_to_deepseek_v3 with class-level and instance-level patching, registered in MODEL_TYPE_TO_APPLY_LIGER_FN (AutoLigerKernelForCausalLM picks it up automatically).
    • RMSNorm: DeepseekV3RMSNormLigerRMSNorm, including attention q_a_layernorm / kv_a_layernorm.
    • SwiGLU: dense MLPs and shared experts (DeepseekV3MLP) → LigerQwen3MoeSwiGLUMLP on all supported versions. Routed experts: batched DeepseekV3Experts → fused LigerExperts on transformers v5+; per-expert patch on transformers v4.
    • RoPE: unsupported — DeepSeek-V3 uses interleaved partial RoPE, incompatible with liger_rotary_pos_emb. rope=True emits a warning_once and skips (no silent behavior change); RoPE is intentionally not listed in the README table.
  • Tests: instance monkey-patch test (asserts both v5 and v4 branch outcomes), 4 convergence tests (bf16/fp32 × FLCE/logits) driven by a mini deepseek_v3 config, revert_liger_kernel_to_deepseek_v3 util, README entry.

Details

  • The transformers v4 branch (per-expert patching) mirrors the existing mixtral / qwen3_moe patterns but could not be exercised locally (only transformers 5.16.1 is installed here); it is covered by the CI v4 matrix.
  • On transformers v5+, LigerExperts accepts DeepseekV3Config (num_local_experts aliases n_routed_experts), and its batched expert weights ((N, 2*I, H) / (N, H, I)) match the HF layout, so pretrained checkpoints load unchanged.

Testing Done

Check Status
Import PASS
Instance Patching PASS
Convergence bf16 FLCE PASS
Convergence bf16 with_logits PASS
Convergence fp32 FLCE PASS
Convergence fp32 with_logits PASS
Convergence multimodal SKIP (text-only model)
Checkstyle PASS

Exact commands run on this change:

  • pytest test/transformers/test_monkey_patch.py -k deepseek_v3 → 1 passed

  • pytest test/convergence/{bf16,fp32}/test_mini_models.py test/convergence/{bf16,fp32}/test_mini_models_with_logits.py -k deepseek_v3 → 4 passed

  • ruff check + ruff format --check clean on all changed files

  • Hardware Type: RTX 4060 Laptop GPU (8GB) — transformers 5.16.1, torch 2.11.0+cu128

  • run make test to ensure correctness

  • run make checkstyle to ensure code style

  • run make test-convergence to ensure convergence

Closes #623

Add Liger Kernel support for DeepSeek-V3 (deepseek_v3 model type),
closing the gap left open by issue linkedin#623:

- model/deepseek_v3.py: lce_forward with FusedLinearCrossEntropy
  (logits never materialized in training)
- monkey_patch.py: apply_liger_kernel_to_deepseek_v3 with RMSNorm,
  SwiGLU (dense MLP + shared experts, fused MoE routed experts on
  transformers v5+, per-expert patch on v4), and FLCE; RoPE is
  unsupported (interleaved partial RoPE) and warns + skips
- class-level and instance-level patching, MODEL_TYPE registration
- convergence tests (bf16/fp32, FLCE and logits paths), monkey-patch
  instance tests, revert util, README entry
@Yulong-Cauli
Yulong-Cauli force-pushed the feat/deepseek-v3-support branch from 32bec97 to 31c38e6 Compare September 13, 2026 06:55
@Yulong-Cauli

Copy link
Copy Markdown
Contributor Author

@arde171 @kolehma8 @BYHsu — friendly ping for a review when you get a chance.

The branch is now a single commit rebased onto latest main (95b01e9); no conflicts, head is 31c38e6.

Recap (closes #623): adds apply_liger_kernel_to_deepseek_v3

  • lce_forward built on FusedLinearCrossEntropy, so logits are never materialized during training
  • RMSNorm + SwiGLU patching for the dense MLP and shared experts; on transformers v5+ the MoE routed experts are patched via LigerExperts (per-expert patching on v4)
  • class-level and instance-level patching, MODEL_TYPE registration
  • interleaved partial RoPE is intentionally left unpatched (warns + skips)

Validation:

  • Kaggle Tesla T4 (SM 7.5, torch 2.10.0+cu128, transformers 5.0.0): deepseek_v3 3/3 passed — monkey-patch instance test, fp32 FLCE convergence, fp32 logits convergence. (The deepseek_v4 sibling cases skip on that env because transformers 5.0.0 doesn't ship the deepseek_v4 module yet.)
  • RTX 4060 Laptop (SM 8.9, transformers 5.16.1): 10/10 passed — deepseek_v3 + deepseek_v4 across bf16/fp32, FLCE and logits paths; the transformers v5 fused-MoE (LigerExperts) path is exercised in both environments.
  • Ruff checks are clean on all touched files.

Could a maintainer approve the workflow run when convenient? (Checks are pending approval for fork PRs.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[transformers] support DeepSeek V3

1 participant