Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for itemsize => element_size() for torch backwards compat #30133

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Changes from 4 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: 8 additions & 5 deletions src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,12 +1160,15 @@ 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):
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()
if hasattr(param, "element_size"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply use:

quant_storage = self.hf_quantizer.quantization_config.bnb_4bit_quant_storage
num_bytes = quant_storage.element_size()

element_size seems present from 1.9.1: https://pytorch.org/docs/1.9.1/search.html?q=element_size&check_keywords=yes&area=default to latest: https://pytorch.org/docs/2.2/search.html?q=element_size&check_keywords=yes&area=default

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@hiyouga hiyouga Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.hf_quantizer.quantization_config.bnb_4bit_quant_storage is a torch.dtype instance while only the torch.tensor has element_size()

huggingface/peft#1635

num_bytes = param.element_size()
elif not hasattr(param, "quant_storage"):
num_bytes = 1
else:
num_bytes = param.quant_storage.itemsize
younesbelkada marked this conversation as resolved.
Show resolved Hide resolved
total_numel.append(
param.numel() * 2 * num_bytes
)
total_numel.append(param.numel() * 2 * nb_params)
else:
total_numel.append(param.numel())

Expand Down
Loading