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 the size of int and bool type when computing module size #2411

Merged
merged 1 commit into from
Feb 2, 2024

Conversation

notsyncing
Copy link
Contributor

@notsyncing notsyncing commented Feb 2, 2024

What does this PR do?

Hello, I'm trying using accelerate to offload a large model (https://huggingface.co/TheBloke/WizardCoder-33B-V1.1-GPTQ) to CPU, with following code (requires #2383 if using Intel GPU, and huggingface/transformers#28755):

import datetime

import torch
import intel_extension_for_pytorch

import accelerate
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

model_id = "/mnt/external2/LLMs/WizardCoder-33B-V1.1-GPTQ"
tokenizer = AutoTokenizer.from_pretrained(model_id)

pipe = pipeline("text-generation", model=model_id, model_kwargs={"torch_dtype": torch.bfloat16, "device_map": "auto", "max_memory": {0: "16GB", "cpu": "128GB"}, "offload_buffers": True})
print(str(pipe.model.hf_device_map))

print(str(datetime.datetime.now()) + " Generating...")
results = pipe("public void helloWorld() {")

print(str(datetime.datetime.now()) + " Output:")
print(results)

I have one Intel Arc A770 16GB GPU in my machine, but the code above always OOM on the GPU.

After some digging, I found that in the utils/modeling.py:

def compute_module_sizes(
    # ... (other code) ...
            size = tensor.numel() * min(dtype_size, dtype_byte_size(tensor.dtype))  # here, dtype_size = 2, dtype_byte_size = 4

This will get wrong size 2 for that model TheBloke/WizardCoder-33B-V1.1-GPTQ, because it contains int32 weights, which size should be 4, so the layer size has been under-estimated, causing an OOM on real load.

The int32 weight won't be converted because of these lines in utils/modeling.py:

def set_module_tensor_to_device(
  # ... (other code) ...
        elif not str(value.dtype).startswith(("torch.uint", "torch.int", "torch.bool")):  # This line will ignore int32 values
            value = value.to(dtype)

So I added a check in compute_module_sizes, matching the set_module_tensor_to_device.

With this PR, the layer containing int32 values can get correct size, and my code can finish correctly without OOM.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

  * According to the code in set_module_tensor_to_device, uint, int and bool type
  won't be converted, so let's keep its original size, or the module size will be
  under-estimated.
Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Great catch ! Thanks for fixing this !

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@SunMarc SunMarc merged commit 68f5472 into huggingface:main Feb 2, 2024
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants