返回

解决4-bit量化VLM模型加载问题: Transformers教程

python

加载4-bit量化VLM模型:问题与方案

模型加载警告和Tokenizers访问错误

使用 Transformers 库加载 Hugging Face 上的 4-bit 量化视觉语言模型 (VLM) 时,经常会出现警告和错误。具体来说,当尝试从 "ThetaCursed/Ovis1.6-Gemma2-9B-bnb-4bit" 加载模型时,会遇到量化配置相关的警告和 AttributeError 。根本原因在于模型的加载方式以及模型本身是否包含文本和视觉的tokenizer。

问题主要体现在:

  1. 配置项警告 : 使用 BitsAndBytesConfig 中的某些参数,特别是 _load_in_4bit_load_in_8bit, 和 quant_method,在 Transformers 的新版本中可能被视为未使用的 kwargs 参数。
  2. tokenizer 不存在 : 模型结构与标准模型有所不同,某些模型没有内置文本或视觉 tokenizer 接口。

解决方案

下面列出针对上述问题的两种方案,分别解决警告信息和 AttributeError 问题。

方案一:优化量化配置,减少警告

某些参数被标记为未使用,是由于库的迭代更新造成的。只需要调整为与库版本兼容的参数。可以移除那些已被弃用或无效的键。同时检查 BitsAndBytesConfig 中的其他配置,并使用正确的方式加载模型。
核心修改方案:使用 load_in_4bit=True 加载4-bit量化模型。 bnb_4bit_compute_dtype 需要保持数据类型与硬件加速相兼容,例如 torch.bfloat16

代码示例:

import torch
from transformers import AutoModelForCausalLM, BitsAndBytesConfig

# 调整量化配置
kwargs = {
    "quantization_config": BitsAndBytesConfig(
        load_in_4bit=True,
        bnb_4bit_compute_dtype=torch.bfloat16, # 选择合适的数据类型
    )
}


model = AutoModelForCausalLM.from_pretrained(
    "ThetaCursed/Ovis1.6-Gemma2-9B-bnb-4bit",
    trust_remote_code=True, # 信任远程代码,根据需要设定
    **kwargs
).cuda()

操作步骤:

  1. 引入所需的库 torchtransformers
  2. 构建BitsAndBytesConfig 配置,只设置必要的参数, 例如load_in_4bitbnb_4bit_compute_dtype
  3. 使用 AutoModelForCausalLM.from_pretrained 从 Hugging Face 加载模型,并传入量化配置和 trust_remote_code=True 参数。如果该模型来自一个不信任的第三方,务必谨慎使用, 并检查它的源代码。

通过调整量化配置,减少了库中的警告。但AttributeError依然存在。继续看下面的方案二。

方案二:使用兼容的方式加载和调用tokenizer

引发 AttributeError 的原因通常是,AutoModelForCausalLM 加载的模型并非总提供 get_text_tokenizer()get_visual_tokenizer() 方法。部分 VLM 模型,特别是经过特定训练或者采用特殊架构的模型, 可能并未以传统的方式暴露文本和视觉 tokenizer。

针对这种情况,应当尝试检查模型的文档或者结构。直接加载相关 tokenizer 而非调用模型的方法可能是更为有效的方式。

代码示例:

from transformers import AutoProcessor
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
import torch

# 调整量化配置
kwargs = {
    "quantization_config": BitsAndBytesConfig(
        load_in_4bit=True,
        bnb_4bit_compute_dtype=torch.bfloat16, # 选择合适的数据类型
    )
}
# 加载模型
model = AutoModelForCausalLM.from_pretrained(
    "ThetaCursed/Ovis1.6-Gemma2-9B-bnb-4bit",
    trust_remote_code=True, # 信任远程代码,根据需要设定
    **kwargs
).cuda()

# 使用AutoProcessor加载 tokenizer
try:
    processor = AutoProcessor.from_pretrained("google/gemma-9b-it") # 使用对应tokenizer,也可以通过tokenizer_name传入模型的tokenizer
    if hasattr(processor,'tokenizer'):
        text_tokenizer = processor.tokenizer  # 处理文本
        print("文本tokenizer加载成功")
    if hasattr(processor, 'image_processor'):
        visual_tokenizer = processor.image_processor   #处理图像
        print("视觉tokenizer加载成功")

except Exception as e:
    print(f"加载tokenizer发生错误: {e}")

操作步骤:

  1. 使用 AutoModelForCausalLM 加载 4-bit 量化模型,参数同方案一。
  2. 使用 AutoProcessor,从对应模型路径下载,加载文本 tokenizer 和图像 processor 。
  3. 验证 processor 对象是否存在 tokenizerimage_processor 属性,如果有,将其分别赋给 text_tokenizervisual_tokenizer
  4. 使用 try-except 代码块,捕捉潜在的 tokenizer 加载错误。

说明:

使用 AutoProcessor 加载对应预训练模型tokenizer,可应对没有使用模型get_text_tokenizer()get_visual_tokenizer() 接口的情况。根据模型的具体架构选择合适的tokenizer路径,并且检查该模型的说明文档或相关示例,可以帮助确定是否确实需要独立的图像和文本处理器。如果模型本身不含独立的视觉 tokenizer, 只需要文本 tokenizer 就可。
请仔细评估从哪里加载 tokenizer,确认来源的安全性。

以上提供的两种方案旨在解决常见的4-bit量化VLM模型加载问题。正确使用Transformers库及理解模型架构,可以帮助更顺利的部署和使用这些模型。