本地部署千文2多模态大模型Qwen2-VL-7B-Instruct-GPTQ-Int4

本地部署千文2多模态大模型Qwen2-VL-7B-Instruct-GPTQ-Int4

第一部分 准备环境
1、创建环境
conda create --name qwen2vl python=3.9
2、激活环境
conda activate qwen2vl
3、安装modelscope(下载方式为魔塔社区命令行下载)
pip install modelscope
第二部 下载模型
1、下载命令
modelscope download --model qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4 --cache_dir /home/ubuntu/ChatPro
第三部分 编写python代码加载模型并推理
1、编写代码

from PIL import Image
import requests
import torch
from torchvision import io
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor


model_dir = "qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4"
model = Qwen2VLForConditionalGeneration.from_pretrained(
    model_dir, torch_dtype="auto", device_map="auto"
)
processor = AutoProcessor.from_pretrained(model_dir)
# Image
url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
image = Image.open(requests.get(url, stream=True).raw)

conversation = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
            },
            {"type": "text", "text": "Describe this image."},
        ],
    }
]


# Preprocess the inputs
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
inputs = processor(
    text=[text_prompt], images=[image], padding=True, return_tensors="pt"
)
inputs = inputs.to("cuda")

# Inference: Generation of the output
output_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids = [
    output_ids[len(input_ids) :]
    for input_ids, output_ids in zip(inputs.input_ids, output_ids)
]
output_text = processor.batch_decode(
    generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
)
print(output_text)

2、安装代码运行环境
pip install opencv-python
pip install uvicorn
pip install fastapi
pip install git+https://github.com/huggingface/transformers
pip install qwen-vl-utils
pip install torchvision
pip install python-multipart
pip install ‘accelerate>=0.26.0’
pip install optimum(安装这个模块的时候会卸载transformers,所以如果是先安装的transformers按完这个需要再次安装transformers,或者后安装transformers)
pip install git+https://github.com/huggingface/transformers
pip install auto-gptq

第四部分 可能出现的问题(有待补充)
1、报错信息:The checkpoint you are trying to load has model type starcoder2 but Transformers does not recognize this architecture. This could be because of an issue with the checkpoint, or because your version of Transformers is out of date.
解决办法:卸载了之前的transformer重新安装transformer(记得重新安装的时候一定要看官方文档推荐你怎么安或者推荐安装哪个版本,这一点很重要很重要很重要!!!,transformer的版本装不对,会进行不下去)
2、报错信息:ModuleNotFoundError: No module named ‘optimum’
解决办法:pip install optimum
3、报错信息:importlib.metadata.PackageNotFoundError: No package metadata was found for auto-gptq
解决办法:pip install auto-gptq
4、报错信息:torch.cuda.OutOfMemoryError: CUDA out of memory
解决办法:看你的显卡内存够不够

附:Qwen2-VL-7B-Instruct-GPTQ-Int4官方文档地址

### Qwen2-7B-Instruct 模型概述 Qwen2-VL-7B-Instruct 是一款多模态预训练模型,专为理解和生成自然语言而设计。该模型基于Transformer架构,在大规模语料库上进行了预训练,并针对特定指令进行了微调,使其能够更好地理解并执行各种任务[^1]。 #### 主要特点 - **参数量**:拥有约70亿个参数,提供了强大的表达能力和泛化性能。 - **视觉与文本融合能力**:不仅擅长处理纯文本输入,还支持图像描述、问答等多种跨模态应用场景。 - **指令跟随优化**:经过精心调整后的指令解析机制使得模型可以更加精准地响应用户的指示。 ### 使用指南 为了方便开发者快速集成和部署此模型,官方提供了一系列工具和支持资源: #### 安装依赖项 首先需要安装必要的Python包来加载和运行这个大型语言模型: ```bash pip install transformers torch accelerate safetensors ``` #### 加载模型实例 通过Hugging Face Transformers库可以直接获取到最新版本的Qwen2-VL-7B-Instruct模型及其对应的分词器: ```python from transformers import AutoModelForCausalLM, AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-VL-7B-Instruct") model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True).half().cuda() ``` #### 构建推理请求 构建一个简单的对话交互流程,向模型发送提示信息以获得回应: ```python prompt = "解释一下什么是气候变化?" inputs = tokenizer(prompt, return_tensors="pt").to('cuda') output = model.generate(**inputs, max_new_tokens=50) response = tokenizer.decode(output.cpu()[0], skip_special_tokens=True) print(response) ``` 上述代码片段展示了如何利用Qwen2-VL-7B-Instruct来进行基本的文字交流。对于更复杂的场景,则可以根据实际需求进一步定制化开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值