[bitsandbytes]: support read bnb pre-quantized model (#5753)
Co-authored-by: Michael Goin <michael@neuralmagic.com>
This commit is contained in:
co-authored by
Michael Goin
parent
2f808e69ab
commit
87525fab92
@@ -702,8 +702,14 @@ class BitsAndBytesModelLoader(BaseModelLoader):
|
||||
|
||||
return hf_weights_files, matched_pattern == "*.safetensors"
|
||||
|
||||
def _hf_weight_iter(self, hf_weights_files, use_safetensors: bool):
|
||||
if use_safetensors:
|
||||
return safetensors_weights_iterator(hf_weights_files)
|
||||
else:
|
||||
return pt_weights_iterator(hf_weights_files)
|
||||
|
||||
def _get_quantized_weights_iterator(
|
||||
self, model_name_or_path: str, revision: Optional[str]
|
||||
self, model_name_or_path: str, revision: Optional[str], pre_quant: bool
|
||||
) -> Tuple[Generator[Tuple[str, torch.Tensor], None, None], Dict[str,
|
||||
Any]]:
|
||||
"""Get an iterator to the model weights with bitsandbytes quantization,
|
||||
@@ -712,6 +718,7 @@ class BitsAndBytesModelLoader(BaseModelLoader):
|
||||
# only load the bitsandbytes module when needed
|
||||
try:
|
||||
import bitsandbytes
|
||||
from bitsandbytes.functional import QuantState
|
||||
if bitsandbytes.__version__ < "0.42.0":
|
||||
raise ImportError("bitsandbytes version is wrong. Please "
|
||||
"install bitsandbytes>=0.42.0.")
|
||||
@@ -725,17 +732,63 @@ class BitsAndBytesModelLoader(BaseModelLoader):
|
||||
model_name_or_path, revision)
|
||||
|
||||
quant_state_dict = {}
|
||||
if use_safetensors:
|
||||
weight_iterator = safetensors_weights_iterator(hf_weights_files)
|
||||
else:
|
||||
weight_iterator = pt_weights_iterator(hf_weights_files)
|
||||
|
||||
def generator():
|
||||
def quantized_checkpoint() -> Generator:
|
||||
# First iterate over all quant state weights
|
||||
weight_iterator = self._hf_weight_iter(hf_weights_files,
|
||||
use_safetensors)
|
||||
temp_state_dict = {}
|
||||
for weight_name, weight_tensor in weight_iterator:
|
||||
if weight_name.endswith(".weight"):
|
||||
continue
|
||||
# TODO: only nf4 quantization is supported for now
|
||||
if weight_name.endswith(".quant_state.bitsandbytes__fp4"):
|
||||
raise NotImplementedError(
|
||||
"Only bitsandbytes_nf4 quantization"
|
||||
f"is supported for now. {weight_name} is fp4 quantized"
|
||||
)
|
||||
temp_state_dict[weight_name] = weight_tensor
|
||||
|
||||
# Closure to parse quant_state for each prequant weight
|
||||
def _parse_quant_state(param_name: str,
|
||||
temp_state_dict: Dict) -> QuantState:
|
||||
quant_state = {}
|
||||
for k in temp_state_dict:
|
||||
if param_name + "." in k:
|
||||
quant_state[k] = temp_state_dict[k]
|
||||
# bitsandbytes library requires
|
||||
# weight.quant_state.bitsandbytes__nf4 in CPU
|
||||
quant_state[param_name +
|
||||
".quant_state.bitsandbytes__nf4"] = quant_state[
|
||||
param_name +
|
||||
".quant_state.bitsandbytes__nf4"].cpu().data
|
||||
return QuantState.from_dict(quant_state, device="cuda")
|
||||
|
||||
# Second iterate over all prequant and normal weights
|
||||
# pre quantized weights would have a quant_state
|
||||
for weight_name, weight_tensor in self._hf_weight_iter(
|
||||
hf_weights_files, use_safetensors):
|
||||
# Filter out all weights whose suffix is not ".weight"
|
||||
if not weight_name.endswith(".weight"):
|
||||
continue
|
||||
if weight_name + ".quant_state.bitsandbytes__nf4" \
|
||||
in temp_state_dict:
|
||||
quant_state = _parse_quant_state(weight_name,
|
||||
temp_state_dict)
|
||||
weight_name = weight_name.replace(".weight", ".qweight")
|
||||
quant_state_dict[weight_name] = quant_state
|
||||
yield weight_name.replace(".weight",
|
||||
".qweight"), weight_tensor
|
||||
else:
|
||||
yield weight_name, weight_tensor
|
||||
|
||||
def generator() -> Generator:
|
||||
for weight_name, weight_tensor in self._hf_weight_iter(
|
||||
hf_weights_files, use_safetensors):
|
||||
if any(target_module in weight_name
|
||||
for target_module in self.target_modules):
|
||||
weight_name = weight_name.replace(".weight", ".qweight")
|
||||
# bitsandbytes requires data in GPU
|
||||
# bitsandbytes requires data in GPU
|
||||
loaded_weight = weight_tensor.cuda().data
|
||||
with set_default_torch_dtype(torch.float32):
|
||||
processed_weight, quant_state = quantize_4bit(
|
||||
@@ -749,6 +802,8 @@ class BitsAndBytesModelLoader(BaseModelLoader):
|
||||
|
||||
yield weight_name, processed_weight
|
||||
|
||||
if pre_quant:
|
||||
return quantized_checkpoint(), quant_state_dict
|
||||
return generator(), quant_state_dict
|
||||
|
||||
def _load_weights(self, model_config: ModelConfig,
|
||||
@@ -766,12 +821,21 @@ class BitsAndBytesModelLoader(BaseModelLoader):
|
||||
logger.info("Loading weights with BitsAndBytes quantization. "
|
||||
" May take a while ...")
|
||||
|
||||
qweight_iterator, quant_state_dict = (
|
||||
self._get_quantized_weights_iterator(model_config.model,
|
||||
model_config.revision))
|
||||
is_quantized_checkpoint = False
|
||||
quant_config = getattr(model_config.hf_config, "quantization_config",
|
||||
None)
|
||||
if quant_config is not None and quant_config.get(
|
||||
'quant_method') == "bitsandbytes":
|
||||
is_quantized_checkpoint = True
|
||||
|
||||
qweight_iterator, quant_state_dict = \
|
||||
self._get_quantized_weights_iterator(
|
||||
model_config.model, model_config.revision, is_quantized_checkpoint)
|
||||
|
||||
model.load_weights(qweight_iterator)
|
||||
|
||||
torch.cuda.empty_cache()
|
||||
|
||||
param_dict = dict(model.named_parameters())
|
||||
stacked_quant_state_dict: Dict[str, Dict[int, Any]] = {}
|
||||
for quant_param_name in quant_state_dict:
|
||||
@@ -809,9 +873,9 @@ class BitsAndBytesModelLoader(BaseModelLoader):
|
||||
f"pack_factor not set for parameter {param_name}.")
|
||||
|
||||
num_elements = [0] * len(quant_states)
|
||||
for seq, quant_state in enumerate(quant_states.items()):
|
||||
for seq, quant_state in quant_states.items():
|
||||
num_elements[seq] = math.prod(
|
||||
quant_state[1].shape) // pack_ratio
|
||||
quant_state.shape) // pack_ratio
|
||||
|
||||
offsets = np.concatenate(([0], np.cumsum(num_elements)))
|
||||
set_weight_attrs(param, {"bnb_shard_offsets": offsets})
|
||||
|
||||
Reference in New Issue
Block a user