Skip to content

Weight loading: conversion ops declare many-to-many support, and four small loader fixes - #49773

Merged
ydshieh merged 1 commit into
mainfrom
fg-split/1-weight-loading
Nov 6, 2026
Merged

ydshieh merged 1 commit into
mainfrom
fg-split/1-weight-loading

Conversation

@IlyasMoutawwakil

@IlyasMoutawwakil IlyasMoutawwakil commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What.

  • ConversionOps.supports_many_to_many replaces the hard-coded tuple of Ernie 4.5 VL ops in WeightConverter.
    Core no longer imports an op to allow it. The Ernie ops set the flag here. The first op outside core is
    FineGrainedWeightGlobals in NVFP4 on the fine-grained machinery #49779.
  • SplitModulelist squeezes only the split dim. A size-1 dim of the tensor itself stays. Any save that splits
    stacked experts back loses that dim otherwise, e.g. an expert's block scales with a single column.
  • WeightConverter.copy keeps force_cpu. Qwen4-Exp's ngram_embedding converter sets force_cpu=True on main,
    because the concatenated embedding is about 95 GiB. reverse_transform goes through copy, so the save path
    dropped it. The quantizer also copies converters for scale companions in Quantizer API: checkpoint renames, skip-lists in checkpoint names, dequantized saves #49774.
  • On-the-fly quantization: a full-precision weight that is quantized on load is no longer cast to the empty param's
    dtype first. Casting bf16 into int8 storage zeroes it, and casting into float8 rounds it twice. FP8 in FP8 on the fine-grained machinery (Fp8Config) #49777
    rounded twice, and MXFP4's int8-packed weights in 🚨 MXFP4 on the fine-grained machinery, and DeepSeek-V4's FP8 + MXFP4 plan #49778 came out zero.
    Pre-quantized loads are unchanged. Every tensor is still cast as before, including the on-disk scale containers,
    and get_new_dtype still runs.

Depends on. #49786.

How tested. New tests in tests/utils/test_core_model_loading.py. test_qkv_chunk_rope_permute_with_fp8_quantization already
fails on main; #49777 replaces it.

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 force-pushed the fg-split/1-weight-loading branch 2 times, most recently from 6765030 to 89032a4 Compare October 20, 2026 13:33
@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: minimax_m3_vl, muse_spark

@IlyasMoutawwakil
IlyasMoutawwakil removed this pull request from stack #49781 October 20, 2026 13:41
@IlyasMoutawwakil
IlyasMoutawwakil changed the base branch from fg-split/fix-reverse-escaping to fg-split/fix-muse-spark-moe-plan October 20, 2026 13:42
@IlyasMoutawwakil IlyasMoutawwakil changed the title Weight loading: many-to-many ops declare themselves, plus four small loader fixes Weight loading: conversion ops declare many-to-many support, and five small loader fixes Oct 20, 2026
@IlyasMoutawwakil
IlyasMoutawwakil force-pushed the fg-split/1-weight-loading branch from 89032a4 to b837014 Compare October 20, 2026 16:04

@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.

Mainly concerned about that 1 main block for conversions so rather let Arthur/Cyril check that out

Comment thread src/transformers/core_model_loading.py Outdated
Comment on lines +1860 to +1880
converted_key, source_pattern = rename_source_key(original_key, [], converters, reverse=True)
if source_pattern is not None:
new_converter = deepcopy(pattern_to_converter[source_pattern])
# each target key gets its own converter instance
mapping = conversion_mapping.setdefault(renamed_key, new_converter)
# Rename after conversion, so the converter still sees the model's names.
# Each target key gets its own converter instance.
if converted_key not in conversion_mapping:
conversion_mapping[converted_key] = deepcopy(pattern_to_converter[source_pattern])
mapping = conversion_mapping[converted_key]
mapping.add_tensor(converted_key, original_key, source_pattern, tensor)
else:
renamed_key, _ = rename_source_key(original_key, renamings, [], reverse=True)
mapping = conversion_mapping.setdefault(renamed_key, WeightRenaming(original_key, renamed_key))
source_pattern = original_key

mapping.add_tensor(renamed_key, original_key, source_pattern, tensor)
mapping.add_tensor(renamed_key, original_key, original_key, tensor)

new_state_dict = {}
for first_param_name, reversed_converter in conversion_mapping.items():
for layer_name, reversed_transform in conversion_mapping.items():
# Apply the reverse converter
realized_value = reversed_converter.convert(first_param_name, model=model, config=model.config)
realized_value = reversed_transform.convert(layer_name, model=model, config=model.config)
for target_name, param in realized_value.items():
param = param[0] if isinstance(param, list) else param
if isinstance(reversed_transform, WeightConverter):
target_name, _ = rename_source_key(target_name, renamings, [], reverse=True)

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.

imo this is something more well suited for @ArthurZucker or @Cyrilvallez to check tbh

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah fair, cc @ArthurZucker @Cyrilvallez. during save, renamings were applied before converters, so a converter whose target gets renamed never matched and the key didn't round trip. now the converter works with model names and renamings go on its outputs, there's a round trip test for it

Comment thread src/transformers/core_model_loading.py Outdated
for original_key, tensor in state_dict:
# Rename the key according to all renaming pattern and optional weight converter patterns
renamed_key, source_pattern = rename_source_key(original_key, renamings, converters, reverse=True)
converted_key, source_pattern = rename_source_key(original_key, [], converters, reverse=True)

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.

it does look a bit weird skipping / passing empty lists around

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah it's ugly, will split it into two explicit steps

if not self.operations:
raise ValueError("WeightConverter requires at least one operation.")

def copy(self, source_patterns, target_patterns, **kwargs) -> WeightConverter:

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.

where did you encounter issues here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

qwen4-exp, its ngram_embedding converter has force_cpu=True bcz the cat is ~95GiB. when the embedding is quantized the quantizer copies the converter for the scale, and copy was dropping force_cpu so the cat ran on gpu. reverse() goes through copy too

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

True for copy, however reverse does not care about force_cpu anyway

@IlyasMoutawwakil IlyasMoutawwakil Oct 23, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

right ! updating the comment now, hope it doesn't interfere with your review 🙏

@IlyasMoutawwakil
IlyasMoutawwakil removed this pull request from stack #49788 October 20, 2026 16:25
@IlyasMoutawwakil
IlyasMoutawwakil force-pushed the fg-split/1-weight-loading branch from b837014 to fcd32ed Compare October 20, 2026 16:25
@IlyasMoutawwakil
IlyasMoutawwakil changed the base branch from fg-split/fix-muse-spark-moe-plan to fg-split/fix-minimax-m3-vl-save-mapping October 20, 2026 16:25
@IlyasMoutawwakil
IlyasMoutawwakil force-pushed the fg-split/1-weight-loading branch 2 times, most recently from 805c40b to bd1f71f Compare October 21, 2026 07:24
@IlyasMoutawwakil
IlyasMoutawwakil force-pushed the fg-split/1-weight-loading branch from bd1f71f to 7f0af1d Compare October 21, 2026 07:36
@IlyasMoutawwakil
IlyasMoutawwakil marked this pull request as ready for review October 21, 2026 07:52
@IlyasMoutawwakil IlyasMoutawwakil changed the title Weight loading: conversion ops declare many-to-many support, and five small loader fixes Weight loading: conversion ops declare many-to-many support, and four small loader fixes Oct 21, 2026
@IlyasMoutawwakil
IlyasMoutawwakil force-pushed the fg-split/1-weight-loading branch from 7f0af1d to 9ba7111 Compare October 21, 2026 07:52
@IlyasMoutawwakil
IlyasMoutawwakil removed this pull request from stack #49798 October 21, 2026 08:59

@Cyrilvallez Cyrilvallez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice! Overall agreed with those changes. However, I think the changes for reverse loading are non-trivial, so I opened #49928 instead (where I added you as co-author) instead, to be able to isolate the change and make it a bit simpler. If it's okay with you, we can proceed to merge that one PR, and then update this one to contain only the other smaller changes

@Cyrilvallez

Copy link
Copy Markdown
Member

Merged the linked PR @IlyasMoutawwakil, we can rebase this onto main

Comment thread src/transformers/conversion_mapping.py Resolved
@IlyasMoutawwakil
IlyasMoutawwakil force-pushed the fg-split/1-weight-loading branch from 5ecaeff to b2f8616 Compare October 27, 2026 10:47
Base automatically changed from fg-split/fix-minimax-m3-vl-save-mapping to main October 27, 2026 11:26
… small loader fixes

Rebased onto the merged reverse-escaping fix (#49772) and the rebased
MiniMax-M3-VL save-mapping PR. The reverse loading path stays main's (#49928);
this keeps the PR's other loader fixes and their tests.

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

@Cyrilvallez Cyrilvallez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM thanks!

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

This PR might need docs.

This adds a new public opt-in flag, ConversionOps.supports_many_to_many (default False), which is now the only way for a custom op outside core to be used in a many-to-many WeightConverter — previously this was a hard-coded internal tuple. The weightconverter page says many-to-many works "only with operations that explicitly support it" but never names the attribute, so custom-op authors can't discover how to enable it.

Pages that might need an update:

  • docs/source/en/weightconverter.md

Posted automatically after approval. Docs live in docs/source/en/, and updating them turns the docs-check check green. The check doesn't block merging.

@Cyrilvallez

Copy link
Copy Markdown
Member

Ok merging this one to unblock #50301 as well!

@Cyrilvallez
Cyrilvallez added this pull request to the merge queue Nov 6, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 6, 2026
@ydshieh

ydshieh commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

it's good, the fsdp job failure is not from this PR. merge now

@ydshieh
ydshieh merged commit cea2686 into main Nov 6, 2026
123 of 124 checks passed
@ydshieh
ydshieh deleted the fg-split/1-weight-loading branch November 6, 2026 15:18
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