The_Bubbles_Tsuki 2025-07-05 11:23 采纳率: 0%
浏览 29

加载模型时遇到难以解决的问题

遇到的问题

加载Meta-Llama-3-8B-Instruct模型时出错,一直无法解决
ValueError: .to is not supported for 4-bit or 8-bit bitsandbytes models. Please use the model as it is, since the model has already been set to the correct devices and casted to the correct dtype.

操作环境、软件版本等信息

bitsandbytes 0.45.0
torch 2.1.2
torchvision 0.16.2+cu118

# bitsandbytes parameters
use_4bit = True  # Activate 4-bit precision base model loading
bnb_4bit_compute_dtype = "float16"  # Compute dtype for 4-bit base models
bnb_4bit_quant_type = "nf4"  # Quantization type (fp4 or nf4)
use_nested_quant = False  # Activate nested quantization for 4-bit base models (double quantization)

# Load base model
model = AutoModelForCausalLM.from_pretrained(
    pretrained_model_name_or_path=model_name,
    quantization_config=bnb_config,
    device_map="auto"
)

尝试禁用4位量化等等仍然是这个报错,不太明白为什么,求解答

  • 写回答

6条回答 默认 最新

  • 阿里嘎多学长 2025-07-05 11:23
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    加载模型时遇到难以解决的问题

    你遇到的问题是加载 Meta-Llama-3-8B-Instruct 模型时出错,错误信息是 ValueError: .to is not supported for 4-bit or 8-bit bits

    这个错误可能是由于模型的数据类型不兼容导致的。Meta-Llama-3-8B-Instruct 模型可能使用的是 16-bit 或 32-bit 的浮点数,而 PyTorch 的默认数据类型是 32-bit 的浮点数。

    解决方法是尝试将模型的数据类型转换为 PyTorch 的默认数据类型。可以使用以下代码:

    model = torch.load('meta-llama-3-8b-instruct.pth', map_location=torch.device('cuda'))
    model.to(torch.float32)
    

    这将将模型的数据类型转换为 32-bit 的浮点数。

    如果你使用的是 CPU 加载模型,可以将 map_location=torch.device('cuda') 改为 map_location=torch.device('cpu')

    如果你仍然遇到问题,可以尝试使用 torch.loadmap_location 参数来指定模型的数据类型,例如:

    model = torch.load('meta-llama-3-8b-instruct.pth', map_location=lambda storage, loc: storage.to('cuda'))
    

    这将将模型的数据类型转换为 CUDA 设备的默认数据类型。

    评论

报告相同问题?

问题事件

  • 创建了问题 7月5日