Skip to content

write dequantization scripts for DeepSeek V4 FP4/FP8 weights#3873

Open
snehalv2002 wants to merge 1 commit into
mainfrom
snehalv-dsv4-dequantize
Open

write dequantization scripts for DeepSeek V4 FP4/FP8 weights#3873
snehalv2002 wants to merge 1 commit into
mainfrom
snehalv-dsv4-dequantize

Conversation

@snehalv2002
Copy link
Copy Markdown
Collaborator

@snehalv2002 snehalv2002 commented May 11, 2026

Description

Adds scripts for dequantizing the DeepSeek V4 weights to bf16. DeepSeek gives us: MoE weights in INT8 (actually two FP4s stored in 1 byte because torch doesn't have an FP4 dtype) and attention weights in F8_E4M3. All scaling factors are in F8_E8M0. We heavily reference the huggingface dequantization script here.

FIXES: b/510020740

Tests

The test script runs inference on deepseek v4 flash through the transformers library, loading weights from the original deepseek checkpoints and our script dequantized checkpoints, then comparing kl-div, token output, etc. Test results can be seen here.

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@snehalv2002 snehalv2002 force-pushed the snehalv-dsv4-dequantize branch 2 times, most recently from 9647437 to b8b85f2 Compare May 12, 2026 18:10
@snehalv2002 snehalv2002 force-pushed the snehalv-dsv4-dequantize branch from b8b85f2 to 8f8ce91 Compare May 12, 2026 22:44
Comment on lines +183 to +186
parser.add_argument("--input-path", "--input-fp8-hf-path", type=str, required=True,
help="Path to DeepSeek FP8/FP4 Hugging Face folder")
parser.add_argument("--output-path", "--output-bf16-hf-path", type=str, required=True,
help="Directory to save output BF16 weights")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: We can probably have a check to make sure that the input and output paths are not the same ( to prevent override ).

)


def weight_dequant_cpu(x: torch.Tensor, s: torch.Tensor, block_size: int = 128) -> torch.Tensor:
Copy link
Copy Markdown
Collaborator

@parambole parambole May 13, 2026

Choose a reason for hiding this comment

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

Rename it to dequantize_fp8 ?

Comment on lines +34 to +35
for i in range(0, M, block_size):
for j in range(0, N, block_size):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: Can we vectorize this operation ?

Copy link
Copy Markdown
Collaborator

@parambole parambole left a comment

Choose a reason for hiding this comment

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

Thank you for adding this script. I have left a few comments PTAL.

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.

2 participants