Skip to content

Commit a38d38d

Browse files
justinchubyCopilot
andcommitted
DeepSeek-V4 B2: block-FP8 / packed-FP4 weight-loading + expert-emission primitives
Real DeepSeek-V4-flash routed experts are FP4-packed in int8 (E2M1, block-32, UE8M0 micro-scales) and its projections are block-FP8 (E4M3 weight + 2D UE8M0 [128,128] block scales). QuantizationConfig.from_transformers treated quant_method='fp8' as ordinary per-tensor fp8 and returned None, so the model built bf16 and rejected the packed [2048,2048] expert against the logical [2048,4096] initializer with a confusing "Weight shape mismatch". Add a clean, breaking quantized-tensor descriptor + load contract (mobius.integrations._block_quant), classified by tensor properties (never by model name): - BlockQuantScheme: property parser over quantization_config + expert_dtype. - QuantizedTensorDescriptor: logical + packed shape, qtype, block geometry, scale name/dtype/shape/layout, byte counts, routed/shared role. - classify_tensor / validate_descriptor: distinguish ORDINARY, BLOCK_FP8, FP4_PACKED, UNSUPPORTED; logical-vs-packed shape, scale pairing, wrong/ missing/orphan scale all fail closed. No dequantization, no copy-to-float. - read_raw_tensor_bytes / LazyRawTensor: byte-exact, header-only, bounded lazy per-shard loading (one tensor resident). - stack_expert_bank / PackedExpertBank: byte-exact expert-major bank packing (reusable lowering primitive; ragged banks are a hard error). - runtime_representation_gap / plan_routed_expert_bank: the emission gate. nxrt's BlockFormat accepts only interleaved llama.cpp block_mxfp4 + iq*; it has no block-FP8 format and no planar-FP4 bank layout, so both families typed-reject with BlockQuantExportError naming the exact ABI gap instead of emitting an unrunnable node. No dense fallback. Route block-scaled fp8 / fp4-expert checkpoints through the typed blocker in QuantizationConfig.from_transformers (property-guarded: per-tensor fp8 without weight_block_size still returns None). This moves the real-checkpoint failure from a shape-mismatch to a precise, actionable BlockQuantExportError. Tests use the real quantization_config + a measured slice of the checkpoint index metadata plus tiny synthetic packed safetensors (real I8 / F8_E4M3 / F8_E8M0 dtypes): classification, logical-vs-packed validation, byte preservation, scale pairing (missing/duplicate/orphan/wrong), shared vs routed, bounded lazy load, byte-exact bank stacking, and the emission typed-reject. An opt-in suite exercises the mounted checkpoint headers directly. Co-authored-by: Copilot <[email protected]>
1 parent cec78d2 commit a38d38d

3 files changed

Lines changed: 1434 additions & 3 deletions

File tree

‎src/mobius/_configs/_quantization.py‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,43 @@ def from_transformers(cls, hf_config) -> QuantizationConfig | None:
7474
"onnxruntime_USE_FP4_QMOE=ON). Export the unquantized (bf16) "
7575
"checkpoint instead, or quantize the bf16 export via Olive."
7676
)
77+
# Block-scaled fp8 (E4M3 weight + 2D UE8M0 block scale) and packed-fp4
78+
# routed experts (I8-packed E2M1 nibbles + UE8M0 micro-scale) are a
79+
# mixed-precision layout this INT4/per-tensor path cannot load — the
80+
# packed [out, in/2] fp4 expert vs its logical [out, in] initializer
81+
# produces a confusing "Weight shape mismatch". Detect it by property
82+
# (not model name, not the ``quant_method`` string) and fail closed with
83+
# a typed, actionable blocker naming the real layout + the runtime ABI
84+
# gap. Checked before the ``quant_method == "none"`` early-return because
85+
# a checkpoint can advertise fp4 experts via top-level ``expert_dtype``
86+
# while leaving ``quant_method`` unset.
87+
from mobius.integrations._block_quant import BlockQuantScheme
88+
89+
scheme = BlockQuantScheme.from_quantization_config(
90+
qc, expert_dtype=getattr(hf_config, "expert_dtype", None)
91+
)
92+
if scheme is not None:
93+
from mobius.integrations._block_quant import BlockQuantExportError
94+
95+
raise BlockQuantExportError(
96+
"Block-scaled FP8 / packed-FP4 checkpoint is not loadable by the "
97+
"INT4/per-tensor quantization path. Detected "
98+
f"quant_method={scheme.quant_method!r}, "
99+
f"weight_block_size={list(scheme.weight_block_size) or None}, "
100+
f"expert_dtype={scheme.expert_dtype!r}: block-FP8 projections "
101+
"(E4M3 weight + 2D UE8M0 block scale) and/or FP4-packed routed "
102+
"experts (I8-packed E2M1 nibbles + UE8M0 micro-scale). Parse and "
103+
"validate these by property with mobius.integrations._block_quant "
104+
"(BlockQuantScheme / classify_tensor / QuantizedTensorDescriptor); "
105+
"the routed-expert emission gate (plan_routed_expert_bank) reports "
106+
"the exact onnx-genai nxrt ABI gap. Native export is blocked until "
107+
"the runtime gains a block-FP8 / planar-FP4 BlockFormat."
108+
)
77109
if method == "none":
78110
return None
79-
# FP8 per-tensor quantization (float8_e4m3fn + scalar scale)
80-
# is handled by dtype casting in _assign_weight(), not by
81-
# QuantizedLinear block quantization.
111+
# Per-tensor fp8 (float8_e4m3fn + a scalar scale) is handled by dtype
112+
# casting in _assign_weight(), so it returns None here. (Block-scaled
113+
# fp8 was already routed to the typed blocker above.)
82114
if method == "fp8":
83115
return None
84116
return cls(

0 commit comments

Comments
 (0)