Skip to content

[BUG] When set v2=True in quantize_config: TypeError: NativeProcessor.preprocess() got an unexpected keyword argument 'fail_safe' #1843

@CHNtentes

Description

@CHNtentes

Here's my code:

from gptqmodel import GPTQModel, QuantizeConfig, BACKEND
from transformers import AutoTokenizer, AutoProcessor
import json
import base64
from PIL import Image
import io
import os

def format_qwen2_vl_dataset(base64, user, assistant):
    return [
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "image": f"data:image;base64,{base64}"
                },
                {"type": "text", "text": user}
            ]
        },
        {"role": "assistant", "content": assistant}
    ]

def format_qwen2_dataset(user, assistant):
    return [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": user
                }
            ]
        },
        {"role": "assistant", "content": assistant}
    ]

def resize_and_to_base64(image_path, target_size=(448, 896)):
    try:
        # 打开图片
        with Image.open(image_path) as img:
            # 调整图片尺寸
            resized_img = img.resize(target_size)

            # 创建字节流对象,用于存储调整后的图片数据
            img_byte_arr = io.BytesIO()

            # 保存调整后的图片到字节流(保持原格式)
            # 如果需要固定格式,可将format参数改为'JPEG'或'PNG'等
            resized_img.save(img_byte_arr, format=img.format)

            # 获取字节流中的二进制数据
            binary_data = img_byte_arr.getvalue()

            # 转换为Base64编码并返回字符串形式
            return base64.b64encode(binary_data).decode('utf-8')

    except FileNotFoundError:
        return f"错误:未找到图片文件 {image_path}"
    except Exception as e:
        return f"处理图片时出错:{str(e)}"

def prepare_dataset_vlm(format_func, n_sample: int = 20) -> list[list[dict]]:
    from datasets import load_dataset, load_from_disk

    dataset = load_dataset(
        "json", data_files="/nvme/sh/shanghai-2/ltg/EST-VQA_modified.json", split="train"
    )
    dataset = dataset.shuffle(seed=42)
    dataset = dataset.select(range(n_sample))
    return [
        format_func(resize_and_to_base64(f'/nvme/sh/shanghai-2/ltg/EST_VQA/{sample["image"]}'), sample["conversations"][0]["value"], sample["conversations"][1]["value"])
        for sample in dataset
    ]

def prepare_dataset_llm(format_func, n_sample: int = 20) -> list[list[dict]]:
    from datasets import load_dataset, load_from_disk

    dataset = load_dataset(
        "json", data_files="/nvme/sh/shanghai-2/ltg/distill_r1_110k_modified.json", split="train"
    )
    dataset = dataset.shuffle(seed=42)
    dataset = dataset.select(range(n_sample))
    return [
        format_func(sample["input"], sample["content"])
        for sample in dataset
    ]

calibration_dataset_vlm = prepare_dataset_vlm(format_qwen2_vl_dataset, n_sample=256)
calibration_dataset_llm = prepare_dataset_llm(format_qwen2_dataset, n_sample=768)

quantize_config = QuantizeConfig(
    bits=4,  # quantize model to 4-bit
    group_size=32,  # it is recommended to set the value to 128
    desc_act=False,
    sym=True,
    lm_head=False,
    damp_percent=0.01,
    damp_auto_increment=0.0025,
    static_groups=False,
    true_sequential=True,
    v2=True,
)

modelpath = "/nvme/sh/shanghai-2/ltg/Qwen2.5-VL-3B-Instruct_0.5"

model = GPTQModel.load(
    modelpath,
    quantize_config=quantize_config,
    trust_remote_code=True
)

calibration_dataset = calibration_dataset_vlm + calibration_dataset_llm
print(calibration_dataset[0])
print(calibration_dataset[-1])

model.quantize(calibration_dataset, batch_size=2, backend=BACKEND.AUTO)
model.save(f"{modelpath}_GPTQ-nomse-v2-4.0.0")

When I used v4.0.0 everything worked well. After upgrading to v4.2.5, it gave me this error:

Traceback (most recent call last):
  File "/nvme/sh/shanghai-2/ltg/GPTQModel.py", line 117, in <module>
    model.quantize(calibration_dataset, batch_size=2, backend=BACKEND.AUTO)
  File "/root/miniconda3/lib/python3.10/site-packages/gptqmodel/models/base.py", line 493, in quantize
    return module_looper.loop(
  File "/root/miniconda3/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
  File "/root/miniconda3/lib/python3.10/site-packages/gptqmodel/looper/module_looper.py", line 301, in loop
    processor.preprocess(subset[name], buffered_fwd=buffered_fwd, fail_safe=fail_safe)
TypeError: NativeProcessor.preprocess() got an unexpected keyword argument 'fail_safe'

If I remove v2=True from quantize_config then there's no error.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions