Skip to content

Experts and TP: per-instance experts flags, load-time-only experts, scalars and per-expert scales in TP and EP - #49775

Open
IlyasMoutawwakil wants to merge 12 commits into
fg-split/2-quantizer-apifrom
fg-split/3-experts-tp
Open

IlyasMoutawwakil wants to merge 12 commits into
fg-split/2-quantizer-apifrom
fg-split/3-experts-tp

Conversation

@IlyasMoutawwakil

@IlyasMoutawwakil IlyasMoutawwakil commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What and why.

  • use_experts_implementation sets its layout flags as class attributes instead of writing them in __init__.
    A flag set by the class body or the instance wins. This lets one wrapper class serve the experts of many models
    and set the flags per instance. Writing them in __init__ would overwrite those values. First user:
    FineGrainedExperts (Fine-grained block-scaled quantization: the shared machinery #49776).
  • ExpertsInterface._load_time_only names the implementations chosen at load time only. Their weights take a
    layout the others cannot read. It holds deepgemm_megamoe.
  • set_experts_implementation checks that set instead of main's hard-coded "deepgemm_megamoe" string.
    get_correct_experts_implementation reads it to know the name. So modeling_utils no longer imports
    finegrained_fp8.
  • HfQuantizer.update_parallel_plan replicates every 0-dim parameter a plan covers, e.g. the per-tensor FP8
    activation_scale, since a scalar has no axis to shard. It moves there from the legacy FP8 quantizer, as Arthur
    asked on Finegrained kernels: unified quantization kernels api (separate quantizers) #49015, so every quantizer gets it. The fine-grained quantizer is the second user (Fine-grained block-scaled quantization: the shared machinery #49776).
  • DtensorShardOperation treats any list entry with fewer dims than its param as a per-expert slice. Main only did
    so for exactly one dim fewer, so per-expert 0-dim scales were kept on every rank. The quantized experts hit this
    under expert parallelism (Fine-grained block-scaled quantization: the shared machinery #49776).
  • The legacy FP8 quantizer keeps main's Mega MoE plan rewrite, FP8Experts._impl_tp_layer_overrides.
  • test_megamoe_takes_over_the_experts_ep_rule already fails on main: it calls a removed update_tp_plan. It is
    fixed here because it tests the quantizer's update_parallel_plan, which this PR changes.

This PR keeps main's _hf_quantized_needs_local_tp and leaves tensor_parallel.py unchanged, so it does not overlap
with #49527 or #49679.

Depends on. #49774.

How tested. tests/integrations/test_moe.py, tests/tensor_parallel/test_tensor_parallel.py,
tests/utils/test_distributed_sharding_utils.py, and main's test_megamoe_takes_over_the_experts_ep_rule.

The comments and docstrings that say why each change exists are there for readability. Feel free to ask for any of them to go if it's self-evident.

🤖 Generated with Claude Code

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@IlyasMoutawwakil
IlyasMoutawwakil removed this pull request from stack #49781 October 20, 2026 13:41
@IlyasMoutawwakil IlyasMoutawwakil changed the title Experts and TP: per-instance experts flags, experts requirements, a public local-tensor marker, intra-expert styles Experts and TP: per-instance experts flags, experts requirements, a public local-tensor marker Oct 20, 2026
@IlyasMoutawwakil
IlyasMoutawwakil removed this pull request from stack #49788 October 20, 2026 16:25

@vasqu vasqu left a comment

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.

Let's wait on @3outeille PRs first no? Same for your EP stack then

@IlyasMoutawwakil IlyasMoutawwakil changed the title Experts and TP: per-instance experts flags, experts requirements, a public local-tensor marker Experts and TP: per-instance experts flags, load-time-only experts, scalars and per-expert scales in TP and EP Oct 21, 2026
IlyasMoutawwakil and others added 11 commits October 23, 2026 13:34
…perts requirements

- use_experts_implementation sets its layout flags as class attributes; a
  flag the class body or the instance sets wins, so one wrapper class serves
  experts of many models.
- ExpertsRequirements (switchable, parallel_styles) registered with an
  implementation's name in ExpertsInterface; set_experts_implementation
  consults it instead of special-casing deepgemm_megamoe, and checks the
  weight layout experts were loaded in (experts_weight_layout).
- modeling_utils lists implementation names through
  ALL_EXPERTS_FUNCTIONS.valid_keys() and no longer imports finegrained_fp8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- tp_uses_local_tensors: a public marker, read in one place, replacing
  _hf_quantized_needs_local_tp (torchao and legacy FP8 switched).
- moe_experts_colwise / moe_experts_packed_colwise / moe_experts_rowwise
  shard each expert of a stacked (E, out, in) param; MoEParamShard refuses to
  split experts with a post-expert norm.
- resolve_parallel_plans swaps in an experts implementation's parallel styles
  and replicates every 0-dim parameter a plan covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…anisms

The legacy FP8 quantizer replicated 0-dim params and swapped the Mega MoE styles itself.
`resolve_parallel_plans` does both, so its override and `FP8Experts._impl_tp_layer_overrides` go.
Make the plan helpers private and in place, inline the local-tensors marker check, and trim the comments.
Test the intra-expert placements, the plan resolution and a non-switchable implementation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rewrite the comments, docstrings and docs prose this PR adds in short, plain sentences.
Drop comments that restate the code and history or benchmark narration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…inery PR

Their first user is FineGrainedExperts, which the machinery PR adds: the
experts weight-layout check in set_experts_implementation, and the
moe_experts_colwise / packed_colwise / rowwise styles with the post-expert-norm
guard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lice

Main only did so for exactly one dim fewer, so per-expert 0-dim scales were kept on every rank. The
quantized experts of the fine-grained stack hit this under expert parallelism.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The rename of `_hf_quantized_needs_local_tp` had no user here and collides with #49679. Scalar replication
goes back from `resolve_parallel_plans` into `HfQuantizer.update_parallel_plan`, as Arthur asked on #49015,
so every quantizer gets it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: finegrained_fp8

IlyasMoutawwakil added a commit that referenced this pull request Nov 12, 2026
…the grouped GEMM

- NVFP4Experts registers parameters, which the EP styles shard along the expert axis; the plan gives the swizzled
  scales their weight's style. Tensor parallelism of NVFP4 layers still raises, unless they are kept whole.
- Both experts forwards skip the routes expert parallelism marks for other ranks.
- A per-expert slice can have fewer dims than its parameter, e.g. a 0-dim per-expert scale (as in #49775).
- generate switches grouped_mm to batched_mm while decoding: NVFP4 runs the grouped GEMM for it.
- Weight-only experts run eager whatever the experts class wrapping, which use_experts_implementation does in place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
IlyasMoutawwakil added a commit that referenced this pull request Nov 12, 2026
…weight-only, generate (#50670)

* Quantization plan: param_needs_quantization asks the param's quantizer

The plan inherited HfQuantizer's default (False), so the shape check reported every bitsandbytes weight mismatched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NVFP4: refuse tensor and expert parallelism again instead of replicating the layers

Replicating kept the packed weights whole on every rank, not just the scales.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ModelOpt schema: warn that W4A16 NVFP4 runs with FP4 activations and that KV cache quantization is dropped

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Quantization plan: drop the __class__ and __reduce__ overrides

Nothing checks a loaded model's quantizer class: the plan is the quantizer, a single one reached through it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NVFP4: weight-only (W4A16) checkpoints run their dequantized weights on full precision activations

ModelOpt's W4A16_NVFP4 loaded with the W4A4 config, which quantizes activations. NVFP4Config(weight_only=True) keeps the
weights packed and dequantizes them in each forward, in pure PyTorch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Quantization plan: drop __getattr__, GGUF's loading calls its quantizer explicitly

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NVFP4: expert parallelism for NVFP4 experts; batched_mm decodes with the grouped GEMM

- NVFP4Experts registers parameters, which the EP styles shard along the expert axis; the plan gives the swizzled
  scales their weight's style. Tensor parallelism of NVFP4 layers still raises, unless they are kept whole.
- Both experts forwards skip the routes expert parallelism marks for other ranks.
- A per-expert slice can have fewer dims than its parameter, e.g. a 0-dim per-expert scale (as in #49775).
- generate switches grouped_mm to batched_mm while decoding: NVFP4 runs the grouped GEMM for it.
- Weight-only experts run eager whatever the experts class wrapping, which use_experts_implementation does in place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NVFP4 EP test: run the W4A4 experts too, on the fake kernel, through both implementations

The fake grouped GEMM leaves rows past the last group NaN like the real one leaves them unwritten, so the test
catches a forward that doesn't zero other ranks' routes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NVFP4: refuse tensor parallelism from the distributed config alone

The plan inspection duplicated how the plans resolve. Tensor parallelism of NVFP4 layers isn't supported, so a
tensor-parallel run refuses NVFP4 linears, and NVFP4 experts without expert parallelism to shard them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Quantizers: fix the type checker's errors

- The FP8 and MXFP4 modules declare their local tensor-parallel forward as a class attribute instead of each instance.
- The plan's module swap no longer writes `source_cls`, which only bitsandbytes reads, on modules it swaps itself.
- NVFP4 reads `ep_size` like `tp_size`, from a distributed config that may be absent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NVFP4: load ModelOpt linears' static input_scale

A pre-quantized NVFP4Linear declared an empty input_scale placeholder, while ModelOpt saves a scalar,
so loading any W4A4 checkpoint with quantized linears (e.g. nvidia/Qwen3-30B-A3B-NVFP4's attention)
failed on a size mismatch. The placeholder now holds one value for pre-quantized loads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NVFP4: pass the global scales torch's grouped NVFP4 GEMM requires

F.scaled_grouped_mm has no NVFP4 implementation for block scales alone ("No gemm implementation was
found" on B200, torch 2.12 and 2.13): it takes [BlockWise1x16, TensorWise] per operand. The global
scales are ones, as they are applied to the output. The fake GEMM in the tests now refuses the old call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NVFP4: grouped experts forward with bf16 routing weights

The grouped forward scattered its rows into a float32 buffer without a cast: with a bf16 model's
bf16 routing weights the rows stay bf16 and index_put fails on the dtype mismatch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NVFP4: multi-device device_map for pre-quantized checkpoints

Only quantizing on the fly needs one device (it packs every weight on quantization_device). A
pre-quantized checkpoint now loads across several GPUs, each checked for Blackwell. On B200,
nvidia/Qwen3-30B-A3B-NVFP4 split over 2 GPUs matches the one-GPU load exactly (logits and greedy
tokens, eager and grouped_mm); the hub kernel runs on its inputs' device, not the current one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Address review: weight-only is a forward choice, docstrings, spacing

- NVFP4 modules always carry the swizzled scales, whose shape is plain arithmetic: weight-only modules hold the same
  tensors as W4A4 ones, the deserialization is the same, and only the forward differs.
- Only one NVFP4 dequantization, `dequantize_nvfp4`.
- The local tensor-parallel flag is set when the modules are swapped again, the type checker told to ignore it.
- Triple-quoted docstrings, the per-expert slice note next to its check, more blank lines.
- The NVFP4 EP test is parameterized and requires torch; the docs list multi-device loading of pre-quantized checkpoints.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

3 participants