240831-Qwen2-VL-7B/2B部署测试

2024-09-01 02:44
文章标签 部署 测试 vl 7b 2b qwen2 240831

本文主要是介绍240831-Qwen2-VL-7B/2B部署测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

A. 运行效果

在这里插入图片描述

B. 配置部署

  • 如果可以执行下面就执行下面:
pip install git+https://github.com/huggingface/transformers accelerate
  • 否则分开执行
git clone https://github.com/huggingface/transformers
cd transformers
pip install . accelerate
  • 随后,执行
pip install qwen-vl-utils
pip install torchvision

C. 模型测试

C.1 测试代码与注意事项
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'  # 使用GPU 0 
# ⚠️ 注意事项1: 如果是混合显卡,且中有一块不支持Flash2-Attention,则需要在代码最开始的地方指定可用显卡from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info# default: Load the model on the available device(s)
model = Qwen2VLForConditionalGeneration.from_pretrained(# "/home/lgk/Downloads/Qwen2-VL-2B-Instruct", torch_dtype="auto", device_map = "auto""/home/lgk/Downloads/Qwen2-VL-2B-Instruct", torch_dtype="auto", device_map = "balanced_low_0"
)
# ⚠️ 注意事项2: 模型与输入需要选择与开头对应的设备,tokenizer没有要求,这里需要更改device_map = "balanced_low_0"# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
# model = Qwen2VLForConditionalGeneration.from_pretrained(
#     "/home/lgk/Downloads/Qwen2-VL-2B-Instruct",
#     torch_dtype=torch.bfloat16,
#     attn_implementation="flash_attention_2",
#     device_map="auto",
# )# default processer
processor = AutoProcessor.from_pretrained("/home/lgk/Downloads/Qwen2-VL-2B-Instruct")# The default range for the number of visual tokens per image in the model is 4-16384.
# You can set min_pixels and max_pixels according to your needs, such as a token range of 256-1280, to balance performance and cost.
# min_pixels = 256*28*28
# max_pixels = 1280*28*28
# processor = AutoProcessor.from_pretrained("/home/lgk/Downloads/Qwen2-VL-2B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)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."},],}
]# Preparation for inference
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(text=[text],images=image_inputs,videos=video_inputs,padding=True,return_tensors="pt",
)
inputs = inputs.to("cuda")# Inference: Generation of the output
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)
C.2 测试
  • mode=1
(qwen2-vl) (base) lgk@WIN-20240401VAM:~/Projects/transformers$ python -u "/home/lgk/Projects/transformers/test.py"
Loading checkpoint shards: 100%|███████████████████████████████████████████████████████| 2/2 [00:03<00:00,  1.58s/it]
['The image depicts a serene beach scene with a woman and her dog. The woman is sitting on the sand, wearing a plaid shirt and black pants, and appears to be smiling. She is holding up her hand in a high-five gesture towards the dog, which is also sitting on the sand. The dog has a harness on, and its front paws are raised in a playful manner. The background shows the ocean with gentle waves, and the sky is clear with a soft glow from the setting or rising sun, casting a warm light over the entire scene. The overall atmosphere is peaceful and joyful.']
  • mode=2
(qwen2-vl) (base) lgk@WIN-20240401VAM:~/Projects/transformers$ python test.py 
You are attempting to use Flash Attention 2.0 without specifying a torch dtype. This might lead to unexpected behaviour
Loading checkpoint shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:08<00:00,  4.03s/it]
['The image depicts a serene beach scene with a woman and her dog. The woman is sitting on the sand, wearing a plaid shirt and black pants, and appears to be smiling. She is holding up her hand in a high-five gesture towards the dog, which is also sitting on the sand. The dog has a harness on, and its front paws are raised in a playful manner. The background shows the ocean with gentle waves, and the sky is clear with a soft glow from the setting or rising sun, casting a warm light over the entire scene. The overall atmosphere is peaceful and joyful, capturing a moment of connection between the']

D. 问题分析

D.1 Flash-Attention版本选择

flash_attn-2.3.5+cu117torch2.0cxx11abiFALSE-cp310-cp310-linux_x86_64.whl与flash_attn-2.3.5+cu117torch2.0cxx11abiTrue-cp310-cp310-linux_x86_64.whl两个版本有什么区别?

这两个版本的 flash_attn 包的区别在于它们的编译选项,特别是关于 cxx11 ABI(Application Binary Interface)的设置,这影响到包的二进制兼容性。具体来说:

  1. cxx11abiFALSE

    • 编译时未启用 C++11 ABI(应用程序二进制接口)的选项。
    • 使用了旧的 ABI(即 cxx03 ABI),通常用于与旧版本的系统或编译器(例如 GCC 4.x 系列)兼容。
    • 对于那些需要与旧版本的 C++ 库兼容的系统或环境,这是合适的选择。
  2. cxx11abiTrue

    • 编译时启用了 C++11 ABI 选项。
    • 使用了新的 C++11 ABI,这通常是在较新的系统上运行的默认选择,兼容性较好,性能也有一定的提升。
    • 适用于较新的编译器(如 GCC 5 及以上)以及那些完全支持 C++11 标准的系统环境。
主要区别:
  • 二进制兼容性:启用 C++11 ABI 的版本(cxx11abiTrue)通常更现代化并且与新编译器及标准库更兼容。而未启用 C++11 ABI 的版本(cxx11abiFALSE)则是为了兼容旧的二进制接口。
  • 性能和特性:使用 cxx11abiTrue 可能会带来性能上的微小提升,因为它使用了较新的 ABI 优化。
选择哪个版本:
  • 如果你的环境中所有相关的 C++ 库和编译器都是较新的版本(支持 C++11 及以上),那么使用 cxx11abiTrue 版本是更合适的选择。
  • 如果你需要兼容老版本的系统或编译器,或者有其他旧的库依赖未启用 C++11 ABI,那么选择 cxx11abiFALSE

总之,选择时要考虑你系统的编译器版本、与其他库的兼容性以及性能需求。

D.2 如何判断

要判断你的环境中的所有相关 C++ 库和编译器是否支持 C++11 及以上版本,可以按照以下步骤进行检查:

1. 检查编译器版本

对于大多数系统,常用的 C++ 编译器是 GCC 或 Clang。以下是检查编译器版本的方法:

  • GCC (GNU Compiler Collection):

    gcc --version
    

    如果版本是 5.1 或更高,则默认支持 C++11 ABI。

  • Clang:

    clang --version
    

    Clang 3.3 及以上版本支持 C++11,但 Clang 3.5 及以上版本默认启用 C++11 ABI。

2. 检查编译器的默认 ABI 设置

检查你的编译器是否默认启用了 C++11 ABI,可以通过编译一个简单的测试程序并查看 ABI 信息:

  • 编译测试程序:

    创建一个简单的 C++ 文件(如 abi_check.cpp):

    #include <iostream>int main() {std::cout << "__GLIBCXX_USE_CXX11_ABI = " << __GLIBCXX_USE_CXX11_ABI << std::endl;return 0;
    }
    
  • 编译并运行:

    g++ abi_check.cpp -o abi_check
    ./abi_check
    

    输出结果 __GLIBCXX_USE_CXX11_ABI = 1 表示启用了 C++11 ABI,而 0 表示未启用。

3. 检查系统中已安装的 C++ 库

有些 C++ 库可能也需要支持 C++11 ABI。检查已安装的库是否与 C++11 ABI 兼容:

  • 查看已安装库的版本:可以使用包管理器(如 apt, yum, dnf 等)查看安装的 C++ 库的版本。

    例如,查看 libstdc++ 版本:

    apt list --installed | grep libstdc++
    
  • 查看符号信息:对于已安装的库,使用 nmobjdump 查看符号信息,确保符号表中的符号与 C++11 ABI 兼容。

4. 检查构建工具链配置

如果你的项目使用 CMake、Makefile 或其他构建系统:

  • CMake: 确保 CMAKE_CXX_STANDARD 设置为 11 或更高:

    set(CMAKE_CXX_STANDARD 11)
    
  • Makefile: 在编译选项中加入 -std=c++11 或更高的标准:

    CXXFLAGS = -std=c++11
    
总结

通过以上步骤,你可以确认你的编译器、库和构建工具链是否默认支持并启用了 C++11 及以上的 ABI。如果所有检查结果都表明支持 C++11,那么你可以安全地使用 cxx11abiTrue 版本的包。

E. 参考文献

  • 但是CUDA_VISIBLE_DEVICES只能在代码最开始的时候设置,中间改是没用的。
  • 【⚠️ 大模型运行漫长的开始】 关于多GPU使用 device_map_device map-CSDN博客
  • from_pretrained加载本地模型文件 - 知乎
  • ⚠️ flash-attn安装报错 - 知乎
  • ⚠️ 微调Qwen2-VL 最佳实践 — swift 2.4.0.dev0 文档
  • vllm-project/vllm: A high-throughput and memory-efficient inference and serving engine for LLMs
  • QwenLM/Qwen2-VL: Qwen2-VL is the multimodal large language model series developed by Qwen team, Alibaba Cloud.
  • Dao-AILab/flash-attention: Fast and memory-efficient exact attention
  • ⚠️ 下载编译好的Flash-attention, True/False版本可以测试下
  • ValueError: Flash Attention 2.0 only supports torch.float16 and torch.bfloat16 dtypes. You passed torch.float32, this might lead to unexpected behaviour. · Issue #28052 · huggingface/transformers
    在这里插入图片描述
pip install flash_attn-2.6.3+cu123torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl --no-build-isolation

这篇关于240831-Qwen2-VL-7B/2B部署测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125718

相关文章

如何在Ubuntu 24.04上部署Zabbix 7.0对服务器进行监控

《如何在Ubuntu24.04上部署Zabbix7.0对服务器进行监控》在Ubuntu24.04上部署Zabbix7.0监控阿里云ECS服务器,需配置MariaDB数据库、开放10050/1005... 目录软硬件信息部署步骤步骤 1:安装并配置mariadb步骤 2:安装Zabbix 7.0 Server

使用Python进行GRPC和Dubbo协议的高级测试

《使用Python进行GRPC和Dubbo协议的高级测试》GRPC(GoogleRemoteProcedureCall)是一种高性能、开源的远程过程调用(RPC)框架,Dubbo是一种高性能的分布式服... 目录01 GRPC测试安装gRPC编写.proto文件实现服务02 Dubbo测试1. 安装Dubb

Python的端到端测试框架SeleniumBase使用解读

《Python的端到端测试框架SeleniumBase使用解读》:本文主要介绍Python的端到端测试框架SeleniumBase使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录SeleniumBase详细介绍及用法指南什么是 SeleniumBase?SeleniumBase

python多线程并发测试过程

《python多线程并发测试过程》:本文主要介绍python多线程并发测试过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、并发与并行?二、同步与异步的概念?三、线程与进程的区别?需求1:多线程执行不同任务需求2:多线程执行相同任务总结一、并发与并行?1、

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

ubuntu如何部署Dify以及安装Docker? Dify安装部署指南

《ubuntu如何部署Dify以及安装Docker?Dify安装部署指南》Dify是一个开源的大模型应用开发平台,允许用户快速构建和部署基于大语言模型的应用,ubuntu如何部署Dify呢?详细请... Dify是个不错的开源LLM应用开发平台,提供从 Agent 构建到 AI workflow 编排、RA

ubuntu16.04如何部署dify? 在Linux上安装部署Dify的技巧

《ubuntu16.04如何部署dify?在Linux上安装部署Dify的技巧》随着云计算和容器技术的快速发展,Docker已经成为现代软件开发和部署的重要工具之一,Dify作为一款优秀的云原生应用... Dify 是一个基于 docker 的工作流管理工具,旨在简化机器学习和数据科学领域的多步骤工作流。它

Nginx部署React项目时重定向循环问题的解决方案

《Nginx部署React项目时重定向循环问题的解决方案》Nginx在处理React项目请求时出现重定向循环,通常是由于`try_files`配置错误或`root`路径配置不当导致的,本文给大家详细介... 目录问题原因1. try_files 配置错误2. root 路径错误解决方法1. 检查 try_f

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小