Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/transformers/integrations/bitsandbytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def convert(
# update param name to get the weights instead of the quantized stats
module, _ = get_module_from_name(model, param_name)

# Fused MoE experts (3D parameter): keep the packed data, the dequantization is registered after loading
if not isinstance(module, bnb.nn.Linear4bit):
config = self.hf_quantizer.quantization_config
packed, quant_state = bnb.functional.quantize_4bit(
value,
compress_statistics=config.bnb_4bit_use_double_quant,
quant_type=config.bnb_4bit_quant_type,
quant_storage=config.bnb_4bit_quant_storage,
)
self.hf_quantizer.experts_quant_states[param_name] = quant_state
result[param_name] = packed
continue

# Support models using `Conv1D` in place of `nn.Linear` (e.g. openai-community/gpt2) by transposing the weight matrix prior to quantization.
# Since weights are saved in the correct "orientation", we skip transposing when loading.
if issubclass(module.source_cls, Conv1D):
Expand Down
23 changes: 21 additions & 2 deletions src/transformers/quantizers/quantizer_bnb_4bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import TYPE_CHECKING

from .base import HfQuantizer
from .quantizers_utils import get_module_from_name
from .quantizers_utils import get_module_from_name, should_convert_module


if TYPE_CHECKING:
Expand Down Expand Up @@ -84,7 +84,15 @@ def param_needs_quantization(self, model: "PreTrainedModel", param_name: str, **
import bitsandbytes as bnb

module, name = get_module_from_name(model, param_name)
return isinstance(module, bnb.nn.Linear4bit) and name != "bias"
if isinstance(module, bnb.nn.Linear4bit):
return name != "bias"
# Fused MoE experts store their weights as 3D parameters instead of `nn.Linear` layers
module_name = param_name.rpartition(".")[0]
return (
module_name.endswith(".experts")
and should_convert_module(module_name, self.modules_to_not_convert)
and getattr(module, name).ndim == 3
)

def adjust_max_memory(self, max_memory: dict[str, int | str]) -> dict[str, int | str]:
# need more space for buffers that are created during quantization
Expand Down Expand Up @@ -118,6 +126,8 @@ def _process_model_before_weight_loading(
):
from ..integrations import replace_with_bnb_linear

# Quantization states of the fused expert parameters quantized on load, by parameter name
self.experts_quant_states = {}
self.modules_to_not_convert = self.get_modules_to_not_convert(
model, self.quantization_config.llm_int8_skip_modules, model._keep_in_fp32_modules
)
Expand All @@ -135,6 +145,15 @@ def _process_model_before_weight_loading(
)

def _process_model_after_weight_loading(self, model: "PreTrainedModel", **kwargs):
from bitsandbytes.nn.parametrize import replace_parameter_4bit_prequantized

# The fused expert parameters hold the packed 4-bit data: dequantize them when they are accessed
for param_name, quant_state in self.experts_quant_states.items():
module, name = get_module_from_name(model, param_name)
replace_parameter_4bit_prequantized(
module, name, quant_state.as_dict(packed=True), getattr(module, name).device
)
self.experts_quant_states = {}
setattr(model, "is_loaded_in_4bit", True)
return model

Expand Down
Loading