Skip to content

Match a skip-list entry against the modules that contain a layer - #51718

Open
THuynh-91 wants to merge 1 commit into
huggingface:mainfrom
THuynh-91:fix-skip-list-inner-modules
Open

THuynh-91 wants to merge 1 commit into
huggingface:mainfrom
THuynh-91:fix-skip-list-inner-modules

Conversation

@THuynh-91

Copy link
Copy Markdown

What does this PR do?

A skip-list entry that names a module stopped covering the layers inside it in v5.0. #42711 moved bitsandbytes onto should_convert_module, which matches an entry from the start of the name, as a regex, or as a suffix of the layer's own name. v4 also skipped a layer when key + "." appeared anywhere in its name, so merger covered model.visual.merger.mlp.0. This looks unintended: #42711 (and #42776 for AWQ) describe the move as a simplification, and the v5 migration guide's quantization section doesn't list it.

Pre-quantized checkpoints rely on that. Unsloth's vision-language repos list ["lm_head", "multi_modal_projector", "merger", "modality_projection", ...] (newer ones add "visual" and "vision_tower") and store those modules unquantized. On main they are built as Linear4bit and generation fails with a bare AssertionError from bitsandbytes. unslothai/unsloth-zoo#491 patches should_convert_module at import, so this only shows up with plain Transformers. Qwen's own AWQ releases rely on it too: the seven official Qwen2-VL / Qwen2.5-VL AWQ checkpoints (about 1.75M downloads a month together; Qwen/Qwen2.5-VL-3B-Instruct-AWQ alone about 878k) set modules_to_not_convert: ["visual"] and store the vision tower unquantized. On main, Qwen/Qwen2.5-VL-3B-Instruct-AWQ fails to load (NotImplementedError: ... out_features: 3420 must be divisible by [8]), and Qwen/Qwen2-VL-2B-Instruct-AWQ loads with its 130 stored vision weights unused and the vision layers left uninitialized, so it answers "What color is the left half?" about a red/blue image with "The left half of the image is white." With this PR both build the vision tower unquantized and answer "Red", as on 4.57.6 (checked with gptqmodel 7.14's torch AWQ backend; the other five have the same list and vision tower but were not loaded). The Jamba docs example (llm_int8_skip_modules=["mamba"], model_doc/jamba.md) hits the same rule: on main it quantizes all 112 Mamba linears of the default config.

import numpy as np
import torch
from PIL import Image
from transformers import AutoModelForImageTextToText, AutoProcessor

repo = "unsloth/Qwen2-VL-2B-Instruct-bnb-4bit"  # llm_int8_skip_modules: ["lm_head", "multi_modal_projector", "merger", "modality_projection"]
model = AutoModelForImageTextToText.from_pretrained(repo, device_map="cuda:0")
print(type(model.model.visual.merger.mlp[0]).__name__)  # 4.57.6: Linear; main: Linear4bit

processor = AutoProcessor.from_pretrained(repo)
image = Image.fromarray((np.indices((224, 224)).sum(0) % 255).astype("uint8")).convert("RGB")
messages = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "Describe the image in one sentence."}]}]
prompt = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
inputs = processor(text=[prompt], images=[image], return_tensors="pt").to("cuda:0")
print(processor.decode(model.generate(**inputs, max_new_tokens=12, do_sample=False)[0, inputs["input_ids"].shape[1]:]))
  • 4.57.6: Linear, then a description of the image.
  • 5.25.1 and main (ce585bc): Linear4bit, then AssertionError in fix_4bit_weight_quant_state_from_module (bitsandbytes/nn/modules.py:498).
  • This PR: Linear, then a description.

Fix. The suffix rule also checks the modules that contain the layer (f"{key}." in full_name, v4's check). #49771's boundaries don't change: mlp.gate doesn't match mlp.gate_proj, layers.1 doesn't match layers.10, and merger doesn't match deepstack_merger_list. The skip-modules docs page describes the new suffix rule.

Behaviour change. An entry now covers every layer inside a module whose name ends with it, for every method that uses should_convert_module. Like v4, it doesn't need a . before it (merger also covers patch_merger). With the default skip-lists, only GIT changes among the module-name methods: its head is named output, so 12 *.output.dense layers are skipped, as in v4. torchao matches parameter names, so _keep_in_fp32_modules leaf entries such as T5's wo take effect there again. This is separate from #49774, which matches skip-lists against checkpoint names: that would also cover the Qwen AWQ visual entries (stored as visual.*), but not entries naming an inner module (merger, stored as visual.merger.*; Jamba's mamba) or checkpoints saved with v5 names (Qwen3-VL, Gemma 4).

Checked against what checkpoints store

For the 100 most-downloaded repos named *bnb-4bit* (99 of them Unsloth's; 98 readable), I compared, layer by layer, which Linear layers each rule converts with which ones the checkpoint stores in 4-bit (<layer>.weight.absmax present). The comparison used the safetensors headers and a meta-device model from each config, with the repo's skip-list plus _keep_in_fp32_modules.

rule layers converted wrongly repos
main 2,017 18
this PR 190 4
v4.57.6 (key + "." in name) 190 4
this PR with a . required before the entry 205 6

No layer is right on main and wrong with this PR. The 18 include Unsloth's Gemma 4 (E2B, E4B, 31B), Qwen3-VL (4B, 8B), Qwen2.5-VL (3B, 7B), Qwen2-VL and Ministral 3 repos. Of the 4 left, 3 are Gemma 3 / MedGemma repos whose skip-lists use the pre-v5 names (language_model.model.layers.N.mlp); that is a separate problem (skip-list entries aren't renamed along with the checkpoint keys). The fourth, unsloth/gemma-4-E2B-unsloth-bnb-4bit, was quantized while model.language_model.layers.1 still matched layers 10 to 19, before #49771. Layers whose names don't line up between checkpoint and model weren't counted (some Gemma 3 and LLaVA vision towers).

The same comparison on the 23 most-downloaded fine-grained FP8 checkpoints (stored dtype per layer) shows no decision changed.

Tests. New cases in tests/quantization/test_quantizers_utils.py fail on main and pass here. tests/quantization CPU suites (bnb, finegrained_fp8, mxfp4, torchao, autoawq) give the same results on main and this branch; the failures on both need an accelerator, Triton or compile, which this machine lacks. ruff check, ruff format --check (0.14.10) and the noisy-comment checker pass on the changed files. On GPU (RTX 3060 Ti, bitsandbytes 0.50.2), unsloth/Qwen2-VL-2B-Instruct-bnb-4bit and unsloth/Qwen3-VL-4B-Instruct-unsloth-bnb-4bit load with the same layer types as 4.57.6 and generate.

Found and fixed with Claude Code (Claude Opus 5.5). The reproducer, the survey and the tests were run on my machine.

Who can review?

Quantization: @SunMarc (cc @IlyasMoutawwakil, #49771/#49774)

A skip-list entry that names a module stopped covering the layers inside it in v5.0, when
huggingface#42711 moved bitsandbytes onto should_convert_module. Pre-quantized checkpoints rely on it:
Unsloth's vision-language repos list "merger", "visual" or "vision_tower" and store those
modules unquantized, so on main they are built as Linear4bit and generation fails; the Jamba
docs example llm_int8_skip_modules=["mamba"] quantizes every Mamba layer. The suffix rule now
also checks the modules containing a layer (v4's `key + "." in name`).

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.

1 participant