跳到内容

量化多模态视觉-语言模型

sample image from MS COCO dataset

<|system|>
You are a helpful assistant.

<|user|>
Please describe the animal in this image

<|assistant|>
The animal in the image is a white kitten.
It has a fluffy coat and is resting on a white keyboard.
The kitten appears to be comfortable and relaxed, possibly enjoying the warmth of the keyboard.

此目录包含使用 GPTQ 量化技术量化各种视觉-语言模型的示例脚本。大多数示例不演示量化单独的视觉编码器参数(如果存在),因为压缩这些参数在性能-精度权衡方面带来的收益甚微。

压缩您自己的模型

要使用您自己的多模态模型,请从现有示例开始,并将 model_id 更改为匹配您自己的模型 stub。

model_id = "path/to/your/model"
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto")

自定义 GPTQModifier 参数

GPTQModifier 是负责执行模型权重量化的修饰符。有关使用不同权重方案进行量化的更多信息,请参阅 示例文件夹 中的 quantization_ 示例。

recipe = [
    GPTQModifier(
        targets="Linear",
        scheme="W4A16",
        sequential_targets=["MistralDecoderLayer"],
        ignore=["re:.*lm_head", "re:.*vision_tower.*", "re:.*multi_modal_projector.*"],
    ),
]

顺序目标 (Sequential Targets)

顺序目标是决定模型前向传播期间误差传播和激活卸载粒度的模块。这些通常是模型的“Transformer 块”,在 llm-compressor 中也称为“层”。

选择具有更高粒度的顺序目标(例如,“Linear”而不是“LlamaDecoderLayer”)将导致同时分配的 Hessian 更少,从而降低压缩的内存需求。这也可能提高模型的恢复精度,因为压缩误差以更高的粒度传播。然而,使用更高粒度的顺序目标也可能增加压缩时间,因为在卸载和加载激活上花费的时间更多。

添加您自己的 Smoothquant 映射

有关为您的数据集添加 Smoothquant 映射的指南,请参阅 SmoothQuant 指南

添加您自己的数据整理器 (Data Collator)

大多数示例使用通用的 data_collator,它可以正确地关联大多数多模态数据集的数据。如果您发现您的模型需要自定义数据整理(例如 pixtral 的情况),您可以修改此函数以反映这些模型特定的要求。

样本图像根据知识共享署名许可协议提供

https://creativecommons.org/licenses/by/4.0/legalcode

@article{cocodataset,
  author    = {Tsung{-}Yi Lin and Michael Maire and Serge J. Belongie and Lubomir D. Bourdev and Ross B. Girshick and James Hays and Pietro Perona and Deva Ramanan and Piotr Doll{'{a} }r and C. Lawrence Zitnick},
  title     = {Microsoft {COCO:} Common Objects in Context},
  journal   = {CoRR},
  volume    = {abs/1405.0312},
  year      = {2014},
  url       = {http://arxiv.org/abs/1405.0312},
  archivePrefix = {arXiv},
  eprint    = {1405.0312},
  timestamp = {Mon, 13 Aug 2018 16:48:13 +0200},
  biburl    = {https://dblp.org/rec/bib/journals/corr/LinMBHPRDZ14},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}