hf_mirrors/Qwen2.5-VL-3B-Instruct-W8A8
Model Introduction
Files & versions
Analysis

模型转换步骤

原始模型:https://modelscope.cn/models/Qwen/Qwen2.5-VL-3B-Instruct

modelslim版本:master

量化步骤:

  • 修改 https://gitcode.com/Ascend/msit/blob/master/msmodelslim/example/Qwen2.5-VL/quant_qwen2_5vl.py 最后一行代码为
    # 7.保存权重
    calibrator.save(args.save_directory, save_type=["ascendV1"], part_file_size=args.part_file_size)
  • 执行量化命令:https://gitcode.com/Ascend/msit/tree/master/msmodelslim/example/Qwen2.5-VL#11-qwen25-vl-w8a8%E9%87%8F%E5%8C%96-%E5%BC%82%E5%B8%B8%E5%80%BC%E6%8A%91%E5%88%B6%E7%AE%97%E6%B3%95%E4%BD%BF%E7%94%A8m2
  • 需要把原始模型中除了模型权重的其他文件拷贝到量化模型的目录

运行示例脚本:

from transformers import AutoProcessor
from vllm import LLM, SamplingParams
from qwen_vl_utils import process_vision_info

MODEL_PATH = "/home/models/Qwen2.5-VL-3B-Instruct-W8A8"

llm = LLM(
    model=MODEL_PATH,
    max_model_len=16384,
    quantization="ascend",
    limit_mm_per_prompt={"image": 10},
)

sampling_params = SamplingParams(
    max_tokens=512
)

image_messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": "https://modelscope.oss-cn-beijing.aliyuncs.com/resource/qwen.png",
                "min_pixels": 224 * 224,
                "max_pixels": 1280 * 28 * 28,
            },
            {"type": "text", "text": "Please provide a detailed description of this image"},
        ],
    },
]

messages = image_messages

processor = AutoProcessor.from_pretrained(MODEL_PATH)
prompt = processor.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
)

image_inputs, _, _ = process_vision_info(messages, return_video_kwargs=True)

mm_data = {}
if image_inputs is not None:
    mm_data["image"] = image_inputs

llm_inputs = {
    "prompt": prompt,
    "multi_modal_data": mm_data,
}

outputs = llm.generate([llm_inputs], sampling_params=sampling_params)
generated_text = outputs[0].outputs[0].text

print(generated_text)