Skip to content

feat(models): FP8 E4M3 block-scale dequant + shared expert FP4 support (DSV4 Phase 1) - #66

Merged
ezutfen merged 6 commits into
mainfrom
feat/dsv4-flash-fp8-fp4-dequant
Jul 13, 2026
Merged

feat(models): FP8 E4M3 block-scale dequant + shared expert FP4 support (DSV4 Phase 1)#66
ezutfen merged 6 commits into
mainfrom
feat/dsv4-flash-fp8-fp4-dequant

Conversation

@ezutfen

@ezutfen ezutfen commented Jul 13, 2026

Copy link
Copy Markdown

Summary

DeepSeek-V4-Flash Phase 1: FP8/FP4 dequantization.

FP8 E4M3 + UE8M0 block-scale dequant (NEW)

V4-Flash attention weights (wq_a, wq_b, wkv, wo_a, wo_b) are stored as FP8 E4M3 with per-128x128 block UE8M0 scales — a format not previously handled by LARQL.

New module quant/fp8_block.rs:

  • dequantize(weights, scales, rows, cols) applies the per-block E8M0 scale to E4M3 weight elements
  • Handles partial last blocks (when rows/cols aren't multiples of 128)
  • 15 unit tests: single/multi-block, partial blocks, edge cases, realistic V4-Flash attention dimensions (wq_a [4096x1024], wkv [4096x512], wq_b [1024x32768])

Shared expert FP4 dequant (EXTENDED)

The existing dequantize_per_expert_mxfp4 only caught per-routed-expert weights (experts.<digit>.w[123].weight). V4-Flash also has a shared expert with the same I8+F8_E8M0 format: shared_experts.w[123].weight. Extended the pattern matcher to catch both.

Safetensors loader wiring

  • dequantize_fp8_block_scaled: detects (.weight=F8_E4M3, .scale=F8_E8M0) companion pairs and dequantizes to f32 before the main tensor loop
  • Main loop skips tensors consumed by both the expert/shared-expert FP4 dequantizer and the new FP8 block-scale dequantizer
  • Both paths are no-ops for non-V4-Flash architectures (they only fire when the specific dtype+companion patterns exist)

Weight format coverage after this PR

Tensor class Format Dequant path Status
Attention (wq_a, wq_b, wkv, wo_a, wo_b) FP8 E4M3 + UE8M0 per-128x128 fp8_block::dequantize NEW
Routed experts (w1, w2, w3) FP4 E2M1 packed + E8M0 per-32 mxfp4::dequantize_expert Already worked
Shared expert (w1, w2, w3) FP4 E2M1 packed + E8M0 per-32 mxfp4::dequantize_expert EXTENDED

Test results (Valinor RTX 3060, Rust 1.97.0)

  • 441 lib tests: all pass (15 new fp8_block tests + 426 existing)
  • 97 architecture integration tests: all pass
  • 32 loading integration tests: all pass (including V4-Flash expert dequant tests)
  • cargo fmt + clippy: clean

Phase context

Part of docs/dsv4-flash-implementation-plan.md:

ezutfen and others added 6 commits July 12, 2026 23:23
…SV4 Phase 1)

Add FP8 E4M3 + UE8M0 per-128x128 block-scale dequantization for
DeepSeek-V4-Flash attention weights (wq_a, wq_b, wkv, wo_a, wo_b).

New module: quant/fp8_block.rs
- dequantize(weights, scales, rows, cols) applies per-block E8M0 scale
  to E4M3 weight elements, supporting partial last blocks
- 17 unit tests covering single/multi-block, partial blocks, edge cases,
  and real V4-Flash attention dimensions

Safetensors loader changes:
- dequantize_fp8_block_scaled: detects (.weight=F8_E4M3, .scale=F8_E8M0)
  companion pairs and dequantizes before the main loop sees them
- Extended dequantize_per_expert_mxfp4 to also catch shared_experts.w[123]
  (was only catching per-routed-expert weights)
- Main loop now skips tensors consumed by both dequantizers

Expert FP4 E2M1 dequant was already working via dequantize_per_expert_mxfp4;
this commit extends it to shared experts and adds the missing FP8 attention
weight path.
…dequant

Adds 3 integration tests in test_loading.rs that exercise the new
safetensors loader code paths:
- FP8 block-scaled attention weight dequant (wq_a, wkv)
- Shared expert MXFP4 dequant (shared_experts.w[123])
- Multi-block FP8 with different per-block scales

These cover the dequantize_fp8_block_scaled and extended shared expert
pattern matching, bringing safetensors.rs back above the 89% coverage
policy threshold.
@ezutfen
ezutfen merged commit 4476dd3 into main Jul 13, 2026
18 checks passed
@ezutfen
ezutfen deleted the feat/dsv4-flash-fp8-fp4-dequant branch July 13, 2026 04:03
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.

1 participant