Skip to content

Fix keep_in_fp32_modules not working for tied weights in load_and_quantize_model - #4043

Merged
SunMarc merged 2 commits into
huggingface:mainfrom
jiqing-feng:slow
May 19, 2026
Merged

SunMarc merged 2 commits into
huggingface:mainfrom
jiqing-feng:slow

Conversation

@jiqing-feng

Copy link
Copy Markdown
Contributor

Problem

MixedInt8LoaddedModelTest::test_fp32_8bit_conversion and Bnb4BitTestLoadedModel::test_fp32_4bit_conversion fail with:

AssertionError: assert torch.float16 == torch.float32

When quantizing an already-loaded model (not on meta device), modules specified in keep_in_fp32_modules (e.g. lm_head) are not correctly kept in fp32.

Root Cause

In BLOOM, lm_head.weight is tied to transformer.word_embeddings.weight (same Parameter object). model.named_parameters() deduplicates by parameter id and yields each parameter only once. Since transformer.word_embeddings.weight is encountered first in DFS traversal, it is the only one yielded — and the check "lm_head" in "transformer.word_embeddings.weight" is False, so the parameter stays in fp16. lm_head.weight is never iterated over.

Fix

Add a second pass using model.named_modules() to explicitly convert parameters of keep_in_fp32_modules to fp32. This is not affected by named_parameters() deduplication since we access the module's parameters directly.

Signed-off-by: jiqing-feng <jiqing.feng@intel.com>
@jiqing-feng

Copy link
Copy Markdown
Contributor Author

Hi @SunMarc . Would you please review this PR? Thanks!

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

Thanks !

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

@SunMarc
SunMarc merged commit e1d39a3 into huggingface:main May 19, 2026
18 of 25 checks passed
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