Skip to content

How to Quantize Qwen2.5-VL on image calibration data #1781

Closed

Description

@Zhiy-Zhang

The following code which I tested:

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
            },
            {"type": "text", "text": "Describe this image."},
        ],
    }
]



quantize_config = QuantizeConfig(
    bits=4,  # quantize model to 4-bit
    group_size=128,  # it is recommended to set the value to 128
)

# load un-quantized model, by default, the model will always be loaded into CPU memory
model = GPTQModel.load(pretrained_model_id, quantize_config)

# quantize model, the calibration_dataset should be list of dict whose keys can only be "input_ids" and "attention_mask"
model.quantize(messages)

# save quantized model
model.save(quantized_model_id)

Is the above implementation correct?

Activity

  1. Qubitium commented on Sep 10, 2025

    @Qubitium
    Collaborator

    @Zhiy-Zhang Please check our tests/models ci tests for per model sample usage.

  2. Zhiy-Zhang commented on Sep 10, 2025

    @Zhiy-Zhang
    Author

    @Qubitium
    Thank you for your reply. I plan to use my own dataset for quantization, but I'm not entirely clear about the specific workflow. I'm referring to this test file “ test_qwen2_5_vl.py”,
    After reading the source code:https://github.com/ModelCloud/GPTQModel/blob/main/tests/models/model_test.py#L205, I understand that following this workflow, can I achieve quantization by simply modifying this part to replace it with my own dataset?

  3. Qubitium commented on Sep 10, 2025

    @Qubitium
    Collaborator

    @Zhiy-Zhang You are correct. Follow the same workflow but tweak/inject your own dataset plus quant params and you are all good

  4. Zhiy-Zhang commented on Sep 11, 2025

    @Zhiy-Zhang
    Author

    @Qubitium Thank you for your reply.

  5. Zhiy-Zhang commented on Sep 11, 2025

    @Zhiy-Zhang
    Author

    @Qubitium I tried modifying the dataset, but I encountered the following error when running it. It seems that the device of grid_thw is CPU. Is this expected? My GPTQModel was compiled from the source code on the main branch, and the Transformers version is 4.56.1.

    Image
  6. Zhiy-Zhang commented on Sep 11, 2025

    @Zhiy-Zhang
    Author

    @Qubitium If the device of cu_seqlens is set to CUDA, quantization can proceed normally, but the following error occurs during execution.

    Image

    The device appears to be “device(type='cuda')”
    I'm not sure if there's an issue with my environment; could you please provide some troubleshooting suggestions?

  7. Qubitium commented on Sep 11, 2025

    @Qubitium
    Collaborator

    @Qubitium If the device of cu_seqlens is set to CUDA, quantization can proceed normally, but the following error occurs during execution.

    Image

    The device appears to be “device(type='cuda')”
    I'm not sure if there's an issue with my environment; could you please provide some troubleshooting suggestions?

    Please code to src and var usage of cu_seqlens.

  8. Zhiy-Zhang commented on Sep 11, 2025

    @Zhiy-Zhang
    Author

    @Qubitium
    I modified two parts to make the entire training process run smoothly.
    1、https://github.com/ModelCloud/GPTQModel/blob/main/gptqmodel/looper/module_looper.py#L125:

    Image 2、https://github.com/ModelCloud/GPTQModel/blob/main/gptqmodel/models/loader.py#L534 Image
  9. Qubitium commented on Sep 11, 2025

    @Qubitium
    Collaborator

    @LRL-ModelCloud check this. By default we should pick the best GPU, Cuda for this user in this call. Something happened to cause this regress?

  10. Zhiy-Zhang commented on Sep 11, 2025

    @Zhiy-Zhang
    Author

    @Qubitium I only used one data sample for quantization, and then used the same sample for testing, but found that the results overflowed. Is this expected?

  11. Qubitium commented on Sep 11, 2025

    @Qubitium
    Collaborator

    @Zhiy-Zhang Previous device errors should be fixed in pr #1783

  12. Qubitium commented on Sep 11, 2025

    @Qubitium
    Collaborator

    @Qubitium I only used one data sample for quantization, and then used the same sample for testing, but found that the results overflowed. Is this expected?

    What do you mean overflowed? Please provide stack traces for every exception especially NaN type errors

  13. Qubitium commented on Sep 11, 2025

    @Qubitium
    Collaborator

    @Qubitium

    Image

    my model:
    https://github.com/bytedance/UI-TARS

    That is not overflow..it is just bad output due to bad quantization likely due to you using one single input/calibration sample. Check the loss values during quant stage. The lower the better, usually.

  14. Qubitium commented on Sep 12, 2025

    @Qubitium
    Collaborator

    @Zhiy-Zhang I am going to close this issue and the new threads are unrelated to the original issue. Please open a new issue if you have any other questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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