From f569172fc283fb5139751a0fefa81873faf87fa5 Mon Sep 17 00:00:00 2001 From: Younes Belkada <49240599+younesbelkada@users.noreply.github.com> Date: Wed, 10 Apr 2024 18:12:43 +0200 Subject: [PATCH] FIX / bnb: fix torch compatiblity issue with `itemize` (#30162) * fix torch compatiblity issues * fix * Update src/transformers/modeling_utils.py --- src/transformers/modeling_utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index 9f223338391cf..8a107694a1c36 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -1159,9 +1159,12 @@ def num_parameters(self, only_trainable: bool = False, exclude_embeddings: bool # For 4bit models, we need to multiply the number of parameters by 2 as half of the parameters are # used for the 4bit quantization (uint8 tensors are stored) if is_loaded_in_4bit and isinstance(param, bnb.nn.Params4bit): - total_numel.append( - param.numel() * 2 * self.hf_quantizer.quantization_config.bnb_4bit_quant_storage.itemsize + quant_storage = self.hf_quantizer.quantization_config.bnb_4bit_quant_storage + # For compatibility with older PT version - see: https://github.com/huggingface/peft/pull/1635 + nb_params = ( + quant_storage.itemsize if hasattr(quant_storage, "itemsize") else quant_storage.element_size() ) + total_numel.append(param.numel() * 2 * nb_params) else: total_numel.append(param.numel())