@testquantize.py gives an error after we upgrade transformers from 4.56 to 4.57 . How to…
- User
@test_quantize.py gives an error after we upgrade transformers from 4.56 to 4.57 . How to fix it? --- Content from referenced files --- Content from @test_quantize.py: #!/usr/bin/env python3
Run test_model.py first
import os
import torch from transformers import BitsAndBytesConfig, Qwen3MoeModel, set_seed
from qwen3_moe_fused.modular_qwen3_moe_fused import Qwen3MoeFusedModel from qwen3_moe_fused.quantize.quantizer import patch_bnb_quantizer
os.environ["TRITON_PRINT_AUTOTUNING"] = "1"
def get_rtol_atol(actual, expect): actual = actual.float() expect = expect.float() diff = (actual - expect).abs() eps = torch.tensor(torch.finfo(actual.dtype).eps, device=actual.device, dtype=actual.dtype) rdiff = diff / torch.maximum(torch.maximum(actual.abs(), expect.abs()), eps) return ( f"mean_rtol={rdiff.mean().item():.3g} " f"max_rtol={rdiff.max().item():.3g} " f"mean_atol={diff.max().item():.3g} " f"max_atol={diff.max().item():.3g}" )
def main(): patch_bnb_quantizer()
codemodel_dir = "./pretrained/qwen-moe-tiny" model_quantized_dir = "./pretrained/qwen-moe-tiny-quantized" model_fused_dir = "./pretrained/qwen-moe-tiny-fused" model_fused_quantized_dir = "./pretrained/qwen-moe-tiny-fused-quantized" device = "cuda" dtype = torch.bfloat16 set_seed(42) vocab_size = 151936 batch_size = 7 seq_len = 13 bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_quant_type="nf4", bnb_4bit_use_double_quant=True, ) model_quantized = Qwen3MoeModel.from_pretrained( model_dir, device_map=device, torch_dtype=dtype, quantization_config=bnb_config ) model_quantized.save_pretrained(model_quantized_dir) model_fused_quantized = Qwen3MoeFusedModel.from_pretrained( model_fused_dir, device_map=device, torch_dtype=dtype, quantization_config=bnb_config ) model_fused_quantized.save_pretrained(model_fused_quantized_dir) model = Qwen3MoeModel.from_pretrained(model_dir, device_map=device) model_quantized = Qwen3MoeModel.from_pretrained(model_quantized_dir, device_map=device) model_fused_quantized = Qwen3MoeFusedModel.from_pretrained(model_fused_quantized_dir, device_map=device) input_ids = torch.randint(0, vocab_size, (batch_size, seq_len), device=device, dtype=torch.int32) hidden = model(input_ids=input_ids).last_hidden_state hidden_quantized = model_quantized(input_ids=input_ids).last_hidden_state hidden_fused_quantized = model_fused_quantized(input_ids=input_ids).last_hidden_state # print(hidden) # print(hidden_quantized) # print(hidden_fused_quantized) print(get_rtol_atol(hidden_quantized, hidden)) print(get_rtol_atol(hidden_fused_quantized, hidden)) print(get_rtol_atol(hidden_fused_quantized, hidden_quantized)) Assistant
I will run the test script to reproduce the error and investigate the codebase to identify the compatibility issue with the upgraded
transformerslibrary.Assistant
I'll read
qwen3_moe_fused/quantize/layer.pyto examine howfix_4bit_weight_quant_state_from_moduleis used withinMoeFusedLinear4bitand why the weight shape assertion might be failing.Assistant
I'll read
qwen3_moe_fused/modular_qwen3_moe_fused.pyto examine theMoeFusedLinearclass definition, specifically checking how the weights are initialized and what their shape is, as this might be causing the assertion failure inbitsandbytes.Assistant
I will read
C:\Python312\Lib\site-packages\bitsandbytes\nn\modules.pyto understand the context of theassert module.weight.shape[1] == 1failure infix_4bit_weight_quant_state_from_moduleand determine why theMoeFusedLinearweight shape is incompatible.- User
I've given you the permission to access C:\Python312\Lib\site-packages . Go on.
Assistant
I'll read
C:\Python312\Lib\site-packages\bitsandbytes\nn\modules.pyto investigate thefix_4bit_weight_quant_state_from_modulefunction and understand the cause of theAssertionError.Assistant
I'll read
C:\Python312\Lib\site-packages\bitsandbytes\nn\modules.pystarting at line 180 to examine theParams4bitclass and understand its quantization and reshaping behavior.Assistant
I'll search for the
Bnb4BitHfQuantizerclass in thetransformersdirectory withinsite-packagesto understand how it's implemented and how it might have changed.Assistant
I'll read
C:\Python312\Lib\site-packages\transformers\quantizers\quantizer_bnb_4bit.pyto examine theBnb4BitHfQuantizerimplementation.Assistant
I'll add debug print statements to
qwen3_moe_fused/quantize/layer.pyto inspect the weight shape and type beforefix_4bit_weight_quant_state_from_moduleis called.Assistant
I'll uncomment the debug print statement in
C:\transformers-qwen3-moe-fused\qwen3_moe_fused\quantize\layer.py.Assistant
I'll run the
test_quantize.pyscript again to see the debug output.Assistant
I'll read
qwen3_moe_fused/quantize/quantizer.pyto prepare for patching theparam_needs_quantizationmethod inBnb4BitHfQuantizer.Assistant
I'll read
C:\Python312\Lib\site-packages\transformers\quantizers\quantizers_utils.pyto examine theget_module_from_namefunction.Assistant
I'll read
C:\Python312\Lib\site-packages\bitsandbytes\functional.pyto investigate thequantize_4bitfunction.Assistant
I'll search for usages of
fix_4bit_weight_quant_state_from_moduleinC:\Python312\Lib\site-packages\bitsandbytes\nn\modules.pyto understand when and how it's typically called.Assistant
I'll read
C:\Python312\Lib\site-packages\bitsandbytes\nn\modules.pyaround line 528 to examine theLinear4bit.forwardmethod.Assistant
I'll modify
qwen3_moe_fused/quantize/quantizer.pyto importget_module_from_name, define a patched_param_needs_quantizationmethod, and updatepatch_bnb_quantizerto apply this patch toBnb4BitHfQuantizer.Assistant
I'll add the
_param_needs_quantizationfunction and update thepatch_bnb_quantizerfunction inC:\transformers-qwen3-moe-fused\qwen3_moe_fused\quantize\quantizer.py.Assistant
I'll run
test_quantize.pyagain to verify if patchingparam_needs_quantizationcorrectly changes the weight type toParams4bit.Assistant
I'll remove the debug print statements from
C:\transformers-qwen3-moe-fused\qwen3_moe_fused\quantize\layer.py.Assistant
I'll run
test_quantize.pyone last time to ensure everything is functioning correctly and the output is clean.Assistant
The issue was caused by an upgrade to
transformers4.57, which introduced a newparam_needs_quantizationmethod inBnb4BitHfQuantizer. This method explicitly checks if a module is an instance ofbitsandbytes.nn.Linear4bit. SinceMoeFusedLinear4bitdoes not inherit fromLinear4bit, its parameters were being loaded as standardtorch.Tensorobjects instead ofbitsandbytes.nn.modules.Params4bit, resulting in missing quantization states and subsequentAssertionErrorduring the forward pass.I fixed this by patching
Bnb4BitHfQuantizer.param_needs_quantizationto recognizeMoeFusedLinear4bitmodules.Modified files:
qwen3_moe_fused/quantize/quantizer.py: Added patching forparam_needs_quantization.qwen3_moe_fused/quantize/layer.py: Verified and cleaned up.
You can now run
test_quantize.pywithout errors.