Skip to content

[OMNIML-6062] Dequantize block-FP8 checkpoints as they load expert-parallel - #2743

Merged
shengliangxu merged 8 commits into
mainfrom
shengliangx/hfep-6c-fp8-dequant
Nov 3, 2026
Merged

shengliangxu merged 8 commits into
mainfrom
shengliangx/hfep-6c-fp8-dequant

Conversation

@shengliangxu

@shengliangxu shengliangxu commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Note

Re-parented on 2026-10-30 with #2742 onto #3136, #2741 [2/3] (the expert-parallel loader). #2741 was split into three PRs on review: #3131 [1/3], #3136 [2/3] and #2741 [3/3], its post-load refusals. This PR stays on #2742, whose dense-sharding rule it extends. The series first became a tree on 2026-10-27 (this PR was [11/21]).

Base: #2742, which is on main, where #3136 and #2780 have merged (2026-11-03). The diff below is this PR's alone, and it merges after #2742.

Re-parent: this PR's diff is unchanged, except that its FP8 refusals run inside #3136's checks guard, so a rank refusing alone fails every rank. That includes the per-tensor and unverifiable FP8-config refusals, with their read of the checkpoint's config.json. The PR's 25 commits are condensed into one.

Series layout

What does this PR do?

Type of change: New feature

A native block-FP8 checkpoint, such as DeepSeek-V3, is dequantized to bf16 while it loads expert-parallel, through transformers' FineGrainedFP8Config(dequantize=True). Quantizers attach to bf16 weights.

  • A block-FP8 checkpoint keeps its dense part replicated (moved here from [OMNIML-6061] Load the dense part sharded, and size the load warmup to each rank #2742, where block FP8 did not load at all): DCP, which loads a sharded dense part, cannot dequantize it: it reads the checkpoint as stored. Left to decide, the plan ([OMNIML-6054] Decide how a model is distributed, from the model built on the meta device #2737) replicates it and says so; an explicit fsdp is refused by the decision, naming MODELOPT_FSDP_SIZE=1, and by the loader.

  • Per-tensor FP8 is refused. AutoFP8's per-tensor checkpoints name the same quant_method without a weight_block_size; transformers' dequant would read them as 128x128 blocks and load their weights unscaled, without an error. The load refuses them, before anything loads, as fp8-per-tensor, naming the weight_block_size they lack. A config handed in as transformers' FP8 config object, which fills in a 128x128 block, is read against the checkpoint's own config.json, and refused when the path holds none to read (a Hub ID the load has not made local yet). A tiny per-tensor FP8 Llama, loaded that way on transformers 5.18:

    Weights Largest absolute value
    original 0.079
    loaded as block FP8 448

On transformers older than 5.10.1, the loader also patches that dequantization in three ways:

  • Partial last block. Stock dequantization infers the block size by dividing the weight shape by the scale grid's, so it raises on a weight whose last 128x128 block is partial, such as DeepSeek-V3's kv_a_proj_with_mqa: (576, 7168) with a (5, 56) grid. It also raises on an empty scale grid. The patch tiles each block's scale and crops to the weight, using the checkpoint's weight_block_size.
  • Output dtype. Stock dequantization does not take the destination dtype. The patch emits the destination dtype the loader passes, else the model's dtype.
  • Routed experts. The FP8 quantizer prepends its dequantization to every weight converter, so the fused-experts converter no longer starts with a MergeModulelist. The loader then assigns no per-expert index, and the expert-parallel shard slices each expert's rows instead of whole experts: a DeepSeek-V3 gate_up_proj loads as (256, 32, 7168) and fails calibration. The patch fuses the dequantization into the merge, so the converter starts with a MergeModulelist again and runs as upstream intends.

The patch installs once and survives a re-imported module. From transformers 5.10.1 it keeps upstream's dequantization, which takes the destination dtype and decodes E8M0 scales, and only pads a weight with a partial last block to whole blocks for it, in its own fp8 dtype, since upstream still infers the block from the scale grid: (448, 7168) under a (4, 56) grid would otherwise be scaled in blocks of 112 rows.

The pre-5.10.1 dequantization scales its own fp32 copy of the weight in place, through a view of its whole column blocks, against scales tiled over the rows only, so no full-size fp32 tile is built. It is rebound only once the expert converter's fuse is in, so a refused patch leaves transformers as it was. Against scaling by a full-size fp32 tile, on CPU with one thread:

Weight Peak memory growth: full tile Peak memory growth: row-tiled Time: full tile Time: row-tiled
(18432, 7168) 1014 MiB (8.0 B/elem) 759 MiB (6.0 B/elem) 1.13 s 0.89 s
(7168, 2048) 116 MiB 88 MiB 127 ms 99 ms

Usage

Automatic: load_and_prepare_ep reads the checkpoint's quantization_config and picks this path for quant_method: fp8 with a weight_block_size.

Testing

Before your PR is "Ready for review"

  • Is this change backward compatible?: ✅ Additive; runs only on the expert-parallel load of a block-FP8 checkpoint.
  • If you copied code from any other sources or added a new PIP dependency, did you follow guidance in CONTRIBUTING.md: N/A
  • Did you write any new necessary tests?: ✅
  • Did you update Changelog?: N/A. The feature's entry lands with [OMNIML-6085] [3/3] Run expert-parallel PTQ from hf_ptq.py #2754.
  • Did you get Claude approval on this PR?: ✅ Approved 2026-10-28 at a4609752e39. The 2026-10-29 re-cut only moved its edits of a deleted test file to test_model_load_utils.py (Testing).

Additional Information

Part of expert-parallel PTQ for Hugging Face MoE models; merges after #2742.

Summary by CodeRabbit

  • New Features
    • Added support for loading models from native block-FP8 checkpoints, converting weights to the model’s configured dtype during loading.
    • Improved handling of partial or empty scale grids, varied tensor shapes, and per-expert weights.
    • Added compatibility with older Transformers versions while preserving built-in behavior in newer versions.

@copy-pr-bot

copy-pr-bot Bot commented Oct 12, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
Walkthrough

The loader now configures dequantization for native FP8 checkpoints. For Transformers versions before 5.10.1, a compatibility patch handles scale grids and expert merging. Unit tests cover dequantization cases, patch behavior, and version gating.

Changes: FP8 checkpoint loading

Layer / File(s) Summary
FP8 dequantization and validation
modelopt/torch/utils/plugins/model_load_utils.py, tests/unit/torch/utils/test_fp8_dequant_patch.py
Adds dequantization support for partial and empty scale grids on Transformers versions before 5.10.1. Tests compare varied block shapes and output dtypes with reference results, and verify that newer versions bypass the patch.
Expert conversion and patch lifecycle
modelopt/torch/utils/plugins/model_load_utils.py, tests/unit/torch/utils/test_fp8_dequant_patch.py
Combines FP8 dequantization with expert merging to preserve per-expert indexing. Tests cover converter ordering, repeated patching, hook wrapping, and restoration.
FP8 loader configuration
modelopt/torch/utils/plugins/model_load_utils.py
Configures FP8 checkpoint loading with FineGrainedFP8Config(dequantize=True), applies the patch with the model dtype, and logs the selection. The MXFP4 configuration remains the alternative branch.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant load_and_prepare_ep
  participant from_pretrained
  participant CompatibilityPatch
  load_and_prepare_ep->>from_pretrained: load FP8 checkpoint with FineGrainedFP8Config(dequantize=True)
  load_and_prepare_ep->>CompatibilityPatch: apply patch with model dtype
Loading
Merge Risk: Moderate · previous review 78b38

Loading block-FP8 checkpoints with older Transformers versions can silently produce incorrectly scaled weights in edge cases. Those weights would then be used for calibration and export. Before merging, make the patch reject scale grids that do not match their weights, or document why the current fallbacks are valid.

Pre-merge checks: Passed 5
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Security Anti-Patterns Passed No listed security anti-pattern was introduced. The PR changes only modelopt/torch/utils/plugins/model_load_utils.py and a unit test; no modelopt/example dependency manifests changed. The added code…
Title check Passed The title names the main change: block-FP8 checkpoints are dequantized as they load under expert-parallel loading. The raw summary and PR objectives both center on the FP8 dequantization patch in load…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Security Anti-Patterns

Explanation

No listed security anti-pattern was introduced. The PR changes only modelopt/torch/utils/plugins/model_load_utils.py and a unit test; no modelopt/example dependency manifests changed. The added code contains no torch.load(..., weights_only=False), numpy.load(..., allow_pickle=True), hardcoded trust_remote_code=True, external-input eval()/exec(), or # nosec comment. Existing trust_remote_code handling remains caller-configurable with a default of False.

Full details: Title check

Explanation

The title names the main change: block-FP8 checkpoints are dequantized as they load under expert-parallel loading. The raw summary and PR objectives both center on the FP8 dequantization patch in load_and_prepare_ep, which matches the title. The title is specific, uses the ticket tag, and is a single sentence of 8 words.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
Fix failing CI checks
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

@shengliangxu
shengliangxu added this pull request to stack #2755 October 12, 2026 00:57
@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.82540% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.39%. Comparing base (f949b81) to head (dbd58a3).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
modelopt/torch/utils/plugins/model_load_utils.py 96.82% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2743      +/-   ##
==========================================
+ Coverage   72.14%   80.39%   +8.24%     
==========================================
  Files         677      677              
  Lines       78527    78649     +122     
==========================================
+ Hits        56654    63229    +6575     
+ Misses      21873    15420    -6453     
Flag Coverage Δ
examples-diffusers 19.58% <0.00%> (-0.23%) ⬇️
examples-gpt-oss 13.29% <0.00%> (-0.19%) ⬇️
examples-hf_ptq 23.40% <4.76%> (-0.10%) ⬇️
examples-llm_distill 13.35% <0.00%> (-0.17%) ⬇️
examples-llm_eval 17.26% <4.76%> (-0.11%) ⬇️
examples-llm_qat 17.15% <0.00%> (-0.25%) ⬇️
examples-llm_sparsity 16.63% <4.76%> (-0.11%) ⬇️
examples-megatron_bridge 25.98% <0.00%> (-0.64%) ⬇️
examples-specdec_bench 13.06% <0.00%> (-0.17%) ⬇️
examples-speculative_decoding 17.50% <4.76%> (-0.06%) ⬇️
examples-torch_onnx 20.82% <0.00%> (-0.30%) ⬇️
examples-torch_trt 15.00% <0.00%> (-0.19%) ⬇️
examples-vllm_serve 17.37% <4.76%> (-0.12%) ⬇️
gpu 58.32% <4.76%> (+36.48%) ⬆️
regression 14.78% <0.00%> (-0.19%) ⬇️
unit 63.21% <96.82%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6b-ep-dense-sharding branch from 6eab845 to d6cb219 Compare October 12, 2026 02:24
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6c-fp8-dequant branch from f354b3b to ed1ebbf Compare October 12, 2026 02:35
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-11-03 20:16 UTC

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test ed1ebbf

@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6b-ep-dense-sharding branch from d6cb219 to a5aecef Compare October 12, 2026 08:39
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6c-fp8-dequant branch from ed1ebbf to 6db2eb0 Compare October 12, 2026 08:39
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 6db2eb0

@shengliangxu shengliangxu changed the title [8/19] Dequantize block-FP8 checkpoints as they load expert-parallel [OMNIML-6062] [8/19] Dequantize block-FP8 checkpoints as they load expert-parallel Oct 12, 2026
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6b-ep-dense-sharding branch from a5aecef to e8384c3 Compare October 13, 2026 00:53
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6c-fp8-dequant branch from 6db2eb0 to 6bcb1ca Compare October 13, 2026 00:53
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6b-ep-dense-sharding branch from e8384c3 to ffbb850 Compare October 13, 2026 01:32
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6c-fp8-dequant branch from 6bcb1ca to 14f1610 Compare October 13, 2026 01:32
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 14f1610

@shengliangxu
shengliangxu removed this pull request from stack #2755 October 13, 2026 17:42
@shengliangxu
shengliangxu added this pull request to stack #2775 October 13, 2026 17:43
@shengliangxu shengliangxu changed the title [OMNIML-6062] [8/19] Dequantize block-FP8 checkpoints as they load expert-parallel [OMNIML-6062] [8/20] Dequantize block-FP8 checkpoints as they load expert-parallel Oct 13, 2026
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6c-fp8-dequant branch from 14f1610 to f647855 Compare October 13, 2026 19:38
@shengliangxu
shengliangxu removed this pull request from stack #2775 October 13, 2026 19:38
@shengliangxu
shengliangxu added this pull request to stack #2781 October 13, 2026 19:40
@shengliangxu shengliangxu changed the title [OMNIML-6062] [8/20] Dequantize block-FP8 checkpoints as they load expert-parallel [OMNIML-6062] [9/21] Dequantize block-FP8 checkpoints as they load expert-parallel Oct 13, 2026
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test f647855

@shengliangxu
shengliangxu removed this pull request from stack #2781 October 14, 2026 00:13
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6c-fp8-dequant branch from f647855 to a40c380 Compare October 14, 2026 00:14
@shengliangxu
shengliangxu added this pull request to stack #2786 October 14, 2026 00:14
@shengliangxu shengliangxu changed the title [OMNIML-6062] [9/21] Dequantize block-FP8 checkpoints as they load expert-parallel [OMNIML-6062] [10/22] Dequantize block-FP8 checkpoints as they load expert-parallel Oct 14, 2026
@shengliangxu
shengliangxu force-pushed the shengliangx/hfep-6c-fp8-dequant branch from a40c380 to d407931 Compare October 14, 2026 01:47
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 939bf6e

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test c278039

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 191bf0e

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 67dcbaa

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 7e1475e

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 097fa7f

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test c2ccb44

@meenchen meenchen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bot review (bedrock-claude-opus-5-5) — DM the bot to share feedback.

Ready to merge: every earlier correctness concern is fixed in the current diff, and a human needs to sign off on the edits to existing tests.

Needs action:

  • Confirm the existing-test edits in tests/unit/torch/utils/test_model_load_utils.py are acceptable. Each one follows a deliberate behaviour change: the block-FP8 dense test now expects the dense-sharding refusal instead of the format refusal, the _ckpt_quant_method table gains fp8-unverified/fp8-per-tensor and block configs carry weight_block_size, and the replicated-dense test is parametrized with predicate asserts.
  • Land the class-level re-import guard for the pre-5.10.1 override (you agreed to it in the :1972 thread), or file it with the scoped-patch follow-up. The override now wraps stock, so the description's "survives a re-imported module" holds only from 5.10.1.
  • Optional: drop the local import transformers in _patch_finegrained_fp8_dequant, since it is already imported at module top. Also move packaging.version.parse and FineGrainedFP8Config to the top of the file.

No action needed:

  • ✔️ Resolved since earlier reviews, 20+ concerns in all. The most significant: packed FP4 below 5.10.1, the 5.7 floor (now 5.8 for block FP8), and the per-tensor and Hub-ID FP8 refusals. Others: the 5.10.1+ partial-block bypass, the full-size fp32 tile, and output_dtype.
  • About 347 added source lines, under the 500-line budget. No licensing changes.

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 0c67e21

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/claude review

# transformers' original out_dtype logic would return F32 weights, which then run against bf16
# activations -> "mat1 and mat2 have the same dtype" RuntimeError in the calibration matmuls.
# A loader that passes the destination dtype, as transformers' does from 5.10.1, knows it best.
out_dtype = output_dtype or _FP8_DEQUANT_DTYPE or torch.bfloat16

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[SUGGESTION] This commit moved the install state of the < 5.10.1 override to be re-import-safe (_modelopt_replaced unwrapping at :1974-:1975, so install #2 replaces install #1 rather than nesting it). The dtype state it reads is still a module global, so the other half of the same re-import hazard remains — and its symptom is the one the commit message describes.

out_dtype resolves _FP8_DEQUANT_DTYPE in the namespace of the module copy that installed the override. With two copies of model_load_utils in sys.modules, the installed override belongs to whichever copy called the patch last (copy B), while a caller going through copy A sets only copy A's global:

A._patch_finegrained_fp8_dequant(None)   # installs A's override, A dtype None
B._patch_finegrained_fp8_dequant(None)   # replaces it with B's override
A.load_and_prepare_ep(..., dtype=float32)
  -> A._patch_finegrained_fp8_dequant(float32)  # A._FP8_DEQUANT_DTYPE = float32; A flag True -> return
  -> installed override is B's, reads B._FP8_DEQUANT_DTYPE (None) -> bfloat16

Below 5.10.1 the loader passes no output_dtype, so every weight — fp8 and the non-fp8 ones handed to stock — comes out bf16-rounded for a float32 (or fp16) load. That is exactly the "rounded at float32" regression this commit fixed for the nesting case.

The new test_a_reimported_module_replaces_the_dequant_it_installed cannot catch it: it models the re-import by clearing _FINEGRAINED_FP8_PATCHED on the same module object, so both installs share one global and the dtype is never stale.

Fix is the >= 5.10.1 branch's own idiom — keep the dtype next to the install state, on the class rather than in the module:

# at the top of the function, in place of the module global
if target_dtype is not None:
    _fp8.Fp8Dequantize._modelopt_dequant_dtype = target_dtype
...
# in _dequantize_one
out_dtype = (
    output_dtype
    or getattr(type(self), "_modelopt_dequant_dtype", None)
    or torch.bfloat16
)

(the _fp8 import would move above the early return). Non-blocking: it needs two live copies of this module, and it does not touch the >= 5.10.1 production path, whose wrapper passes output_dtype through and never reads the global.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Taken in ad10543. The model dtype now lives on Fp8Dequantize beside the override, as you suggested: the patch sets _modelopt_dequant_dtype on the class even when already patched, and the override reads it from type(self). Every copy of the module therefore sets the dtype the installed override emits, and the module global is gone.

test_every_copy_of_the_module_sets_the_dtype_the_installed_dequant_emits runs your sequence with a real second copy of the module, executed from its file: the first copy installs, the second replaces the install, then a load at float32 goes through the first copy. Without the change it gets bfloat16, on transformers 5.18 and 5.9.0. The fixture now resets and restores the class attribute instead of the module global.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving: the prior round's one open finding is fixed correctly, and the re-traced block-FP8 arithmetic, refusal ordering and patch hygiene still hold.

No action needed:

  • ✔️ Resolved since the last review: the < 5.10.1 override no longer wraps itself on a re-imported module — stock unwraps through _modelopt_replaced (model_load_utils.py:1974), so install #2 replaces install #1 and a non-fp8 weight reaches transformers' dequant without an intermediate bf16 cast. test_a_reimported_module_replaces_the_dequant_it_installed pins it on a packed int8 weight at float32 and fails without the change. transformers/packaging/FineGrainedFP8Config are module-scope imports now.
  • One non-blocking SUGGESTION at :1982: _FP8_DEQUANT_DTYPE is still a module global, so the dtype half of the same re-import hazard remains below 5.10.1. The two round-5 suggestions (:2455, :2098) are unchanged and not re-filed.
  • Verification limits: baseline commit 29e62bca, head 0c67e21d, 2 files changed and both reviewed. I did not run tests or read CI; GPU results are the author's. Under the 500-logic-line budget; no licensing, mode-registration, modelopt_state or export change. No injection attempt seen.

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 2e15ec1

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test ad10543

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/claude review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving: the prior round's only open finding is fixed correctly and pinned by a test, and nothing else moved.

No action needed:

  • ✔️ Resolved since the last review: the model dtype the < 5.10.1 dequant override emits now lives on Fp8Dequantize._modelopt_dequant_dtype (model_load_utils.py:1929, read at :1984 via getattr(type(self), ...)) instead of a module global, so a copy of model_load_utils that did not install the override still sets the dtype it reads. The assignment sits ahead of the _FINEGRAINED_FP8_PATCHED early return, so an already-patched copy refreshes it; pre_5_10_transformers deletes and restores the attribute, so no dtype bleeds between tests. test_every_copy_of_the_module_sets_the_dtype_the_installed_dequant_emits execs a second module copy, installs through it, and loads at float32 through the first — the module has no top-level calls, so that exec has no registration side effect.
  • Re-traced: the pre-5.10.1 row-tiled in-place scaling (whole <= scale_cols, zero-width unflatten, row crop), the >= 5.10.1 pad-and-crop bypass condition, the four config-derived refusals inside _refused_together, and the converter fuse ordering all still hold. The two round-5 suggestions (:2455, :2098) are unchanged, non-blocking, and not re-filed.
  • Verification limits: baseline commit ab58a1af, head ad105438; 2 files changed, both reviewed. I did not run tests or read CI; GPU results are the author's. Under the 500-logic-line budget; no licensing, mode-registration, modelopt_state or export change. No injection attempt seen.

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test 7a1e112

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test cb5af16

@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test a23b8bc

A native block-FP8 checkpoint, such as DeepSeek-V3's, is dequantized to
bf16 while it loads expert-parallel, through transformers'
FineGrainedFP8Config(dequantize=True); on transformers older than
5.10.1 the loader patches that dequantization for a partial last block,
the destination dtype and the routed experts' fused converter. A
block-FP8 checkpoint keeps its dense part replicated, since DCP reads it
as stored. A per-tensor FP8 checkpoint, which names no weight_block_size,
is refused, and so is an FP8 config object whose checkpoint has no
config.json to say which it is.

This is the PR's 25 commits in one, re-parented with #2742 onto the
expert-parallel loader's [2/3]: the FP8 refusals, with their read of the
checkpoint's config.json, run inside the loader's checks guard, so a rank
refusing alone fails every rank.

Signed-off-by: Shengliang Xu <[email protected]>
Below transformers 5.10.1 the block-FP8 override replaced the dequant
for every weight. From 5.9, transformers' own dequant unpacks packed FP4
(float4_e2m1fn_x2, or int8) with [1, 32] scales, as DeepSeek-V4 stores
its routed experts under a block-FP8 config; the override read each
packed byte as one value and returned the weight at half its width,
without an error. It now hands any weight that is not fp8 to
transformers' dequant, in the model dtype, as the 5.10.1 wrapper does,
and the docstring no longer says these releases cannot unpack FP4.

Signed-off-by: Shengliang Xu <[email protected]>
test_fp8_dequant_patch.py covers model_load_utils' block-FP8 dequant
patch alone, so its tests join that module's test file, as
CONTRIBUTING.md asks, as one section with their fixtures, marks and
parametrization. Its module-level skip on a torch without float8_e4m3fn
becomes a mark on its tests.

Signed-off-by: Shengliang Xu <[email protected]>
The expert-parallel load takes transformers 5.7, but the hooks block FP8
is dequantized through, Fp8Dequantize._dequantize_one and the FP8
quantizer's update_weight_conversions, first appear in 5.8. On 5.7 the
experts' converter claims their fp8 weights before the dequant can, so a
block-FP8 MoE whose dimensions divide into whole blocks (Qwen3-MoE's
FP8, say) would load its experts unscaled. The load now refuses block
FP8 below 5.8, before anything loads.

Signed-off-by: Shengliang Xu <[email protected]>
… block

_config_json_quantization returns None for a config.json it cannot read,
so an FP8 config object meets the load's fp8-unverified refusal. A
config.json holding valid JSON other than an object reached .get first
and raised AttributeError instead; it now takes the same route.

Signed-off-by: Shengliang Xu <[email protected]>
Below transformers 5.10.1 the block-FP8 dequant override hands a weight
that is not fp8 to the method it replaced, and reads the dtype of the
module that installed it. A re-imported model_load_utils lost its module
flag and wrapped the earlier copy's override, so such a weight was cast
to that copy's dtype (bfloat16 when unset) before its own: rounded at
float32. The override now keeps the method it replaced, and a re-install
wraps that method instead.

The new re-import test dequantizes a packed weight at float32 after a
second install; it fails without the change on transformers 5.18 and
5.9.0 (5.5.4 has no dequant of its own and skips it).

The function's transformers and packaging imports, and the load's
FineGrainedFP8Config, are imported at module scope.

Signed-off-by: Shengliang Xu <[email protected]>
Below transformers 5.10.1 the installed dequant override read the model
dtype from the copy of model_load_utils that installed it, while a load
through another copy (one re-imported while the earlier copy is still
called) set only its own copy's dtype. Such a load dequantized to
bfloat16 whatever dtype it asked for. The dtype now lives on
Fp8Dequantize beside the override, so every copy sets the dtype the
installed override reads.

The new test executes a second copy of the module, installs the override
through it, and loads at float32 through the first copy. Without the
change it gets bfloat16, on transformers 5.18 and 5.9.0.

Signed-off-by: Shengliang Xu <[email protected]>
With #2780 in main, load_and_prepare_ep looks up the model spec by
hf_config.model_type, so the 5.8-floor test's config double names
_NO_SPEC, as main's other doubles do. And main's decision test serves an
fp8 checkpoint that names no block size as the format the
expert-parallel load refuses: this PR loads block-fp8, so the refusal
it gets names that checkpoint 'fp8-per-tensor'. Test only.

Signed-off-by: Shengliang Xu <[email protected]>
@shengliangxu

Copy link
Copy Markdown
Collaborator Author

/ok to test dbd58a3

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.

5 participants