Skip to content

PreTrainedModel._load_pretrained_model doesn't move non-persistent buffers to cpu with low_cpu_mem_usage #25555

Description

@shingjan

System Info

With env variable INIT_INCLUDE_BUFFERS=True in accelerate, big LLM models will be loaded in meta tensors first and use actual weights during inference. However, PreTrainedModel._load_pretrained_model doesn't init non-persistent buffer tensors with low_cpu_mem_usage and accelerate, leading to a ValueError: inv_freq is on the meta device, we need a value to put in on 0. while accelerate tries to add hook to module with named_module_tensors.

Who can help?

cc: @sgugger

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

My repro, after this PR from accelerate:

import torch
from transformers import (
    AutoModelForCausalLM,
)

import os
os.environ["ACCELERATE_INIT_INCLUDE_BUFFERS"] = "1"

with torch.no_grad():
    model_real = AutoModelForCausalLM.from_pretrained(
        "lmsys/vicuna-7b-v1.3", device_map='auto', torch_dtype=torch.float16, trust_remote_code=True
    )

stacktrace:

Traceback (most recent call last):
  File "repro/vicuna.py", line 34, in <module>
    model_real = AutoModelForCausalLM.from_pretrained(
  File "/lib/transformers/src/transformers/models/auto/auto_factory.py", line 511, in from_pretrained
    return model_class.from_pretrained(
  File "/lib/transformers/src/transformers/modeling_utils.py", line 2996, in from_pretrained
    dispatch_model(model, **kwargs)
  File "/lib/accelerate/src/accelerate/big_modeling.py", line 385, in dispatch_model
    attach_align_device_hook_on_blocks(
  File "/lib/accelerate/src/accelerate/hooks.py", line 536, in attach_align_device_hook_on_blocks
    attach_align_device_hook_on_blocks(
  File "/lib/accelerate/src/accelerate/hooks.py", line 536, in attach_align_device_hook_on_blocks
    attach_align_device_hook_on_blocks(
  File "/lib/accelerate/src/accelerate/hooks.py", line 536, in attach_align_device_hook_on_blocks
    attach_align_device_hook_on_blocks(
  File "/lib/accelerate/src/accelerate/hooks.py", line 506, in attach_align_device_hook_on_blocks
    add_hook_to_module(module, hook)
  File "/lib/accelerate/src/accelerate/hooks.py", line 155, in add_hook_to_module
    module = hook.init_hook(module)
  File "/lib/accelerate/src/accelerate/hooks.py", line 253, in init_hook
    set_module_tensor_to_device(module, name, self.execution_device)
  File "/lib/accelerate/src/accelerate/utils/modeling.py", line 277, in set_module_tensor_to_device
    raise ValueError(f"{tensor_name} is on the meta device, we need a `value` to put in on {device}.")
ValueError: inv_freq is on the meta device, we need a `value` to put in on 0.

Expected behavior

The above loading should work

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions