一、多模态大模型下载
1. hf-mirror方法
1.1 环境配置
pip install -U huggingface_hub
export HF_ENDPOINT = https://hf-mirror.com
$env :HF_ENDPOINT = "https://hf-mirror.com"
unset HF_HUB_OFFLINE
export HF_HUB_OFFLINE = 0
1.2 模型下载
huggingface-cli download --resume-download Qwen/Qwen2.5-VL-72B-Instruct-AWQ --local-dir Qwen2.5-VL-72B-Instruct-AWQ
1.3 数据下载
huggingface-cli download --repo-type dataset --resume-download wikitext --local-dir wikitext
2. ModelScope方法
2.1 环境配置
pip install modelscope -i https://mirrors.aliyun.com/pypi/simple/
2.2 模型下载
2.2.1通过命令行下载
在高带宽的机器上运行,推荐使用ModelScope命令行工具下载模型。该方法支持断点续传和模型高速下载
modelscope download --model = "Qwen/Qwen2.5-VL-72B-Instruct-AWQ" --local_dir ./model-dir
2.2.2通过SDK下载
使用ModelScope Python SDK下载模型,该方法支持断点续传和模型高速下载
from modelscope import snapshot_download
model_dir = snapshot_download( "Qwen/Qwen2.5-VL-72B-Instruct-AWQ" )
model_dir = snapshot_download( 'qwen/Qwen/Qwen2.5-VL-72B-Instruct-AWQ' , cache_dir= './weights' )
2.2.3通过Git下载
由于模型都是通过Git存储,所以也可以在安装Git LFS后,通过git clone的方式在本地下载模型
apt-get install git-lfs
git lfs install
git clone https://www.modelscope.cn/Qwen/Qwen2.5-VL-72B-Instruct-AWQ.git
二、VLLM部署多模态模型
1.准备工作
1.1获取vLLM镜像
docker pull egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/vllm:0.8.2-pytorch2.6-cu124-20250328
1.2模型权重下载
# 下载modelscope格式的Qwen2.5-VL-7B-Instruct模型
git lfs clone https://www.modelscope.cn/Qwen/Qwen2.5-VL-7B-Instruct.git
# 下载modelscope格式的Qwen2.5-VL-72B-Instruct-AWQ模型
git lfs clone https://www.modelscope.cn/Qwen/Qwen2.5-VL-72B-Instruct-AWQ.git
2.使用 vLLM 部署和推理
2.1运行vLLM容器
docker run -d -t \
--net = host \
--gpus all \
--privileged \
--ipc = host \
--name vllm \
-v /root:/root \
< vLLM镜像地址>
docker run --gpus all --ipc = host --privileged --network = host -v /home:/home -v /longhorn_data:/longhorn_data --name vllm_qwen2.5_vl -it egs-registry.cn-hangzhou.cr.aliyuncs.com/egs/vllm:0.8.2-pytorch2.6-cu124-20250328 bash
pip install git+https://github.com/huggingface/transformers@f3f6c86582611976e72be054675e2bf0abb5f775
pip install accelerate
pip install qwen-vl-utils -i https://mirrors.aliyun.com/pypi/simple/
pip install qwen-vl-utils[ decord] -i https://mirrors.aliyun.com/pypi/simple/
2.2启动OpenAI API服务
启动vLLM的 OpenAI API 服务器,通过vLLM把本地大模型部署成 OpenAI API 服务
API 服务器会监听默认端口为8000,可通过 --port 参数设置服务端口;同时,可通过--host 参数设置服务地址
vLLM尝试使用 bfloat16 数据类型,但GPU(Tesla T4)不支持 bfloat16,主要支持 FP16、INT8、INT4 精度的矩阵运算
2.2.1部署Qwen2.5-VL-7B-Instruct
vllm serve Qwen/Qwen2.5-VL-7B-Instruct \
--port 8000 \
--host 0.0 .0.0 \
--dtype float16 \
--limit-mm-per-prompt image = 5 ,video= 5
vllm serve Qwen/Qwen2.5-VL-7B-Instruct \
--host 0.0 .0.0 \
--port 8000 \
--tensor-parallel-size 2 \
--dtype auto \
--gpu-memory-utilization 0.85 \
--limit-mm-per-prompt image = 5 ,video= 5 \
--max-num-batched-tokens 4096 \
--max-model-len 4096 \
--max-num-seqs 16 \
--enforce-eager \
--trust-remote-code \
vllm serve Qwen/Qwen2.5-VL-7B-Instruct \
--tensor-parallel-size 2 \
--gpu-memory-utilization 0.9 \
--dtype bfloat16 \
--limit-mm-per-prompt image = 8 ,video= 4 \
--max-num-batched-tokens 8192
vllm serve Qwen/Qwen2.5-VL-7B-Instruct \
--tensor-parallel-size 4 \
--quantization awq \
--gpu-memory-utilization 0.85 \
--dtype float16 \
--limit-mm-per-prompt image = 3 ,video= 2
CUDA_VISIBLE_DEVICES = 0,1 ,2,3 vllm serve Qwen/Qwen2.5-VL-7B-Instruct --port 8000 --host 0.0 .0.0 --dtype float16 --limit-mm-per-prompt image = 3 ,video= 2 --tensor-parallel-size 4 --gpu-memory-utilization 0.85 --max-num-seqs 16 --enforce-eager --trust-remote-code --max-model-len 4096
2.2.2部署Qwen2.5-VL-72B-Instruct-AWQ
AWQ 量化将模型显存需求降低至约 1/4(72B FP16 需 144GB → AWQ 后约 36GB)
PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True允许动态扩展 CUDA 显存段,避免碎片化问题
PYTORCH_CUDA_ALLOC_CONF = expandable_segments:True \
vllm serve Qwen/Qwen2.5-VL-72B-Instruct-AWQ \
--tensor-parallel-size 4 \
--gpu-memory-utilization 0.85 \
--quantization awq \
--dtype float16 \
--max-model-len 8192 \
--enforce-eager \
--trust-remote-code \
--host 0.0 .0.0 \
--port 8678 \
--api-key xxx
PYTORCH_CUDA_ALLOC_CONF = expandable_segments:True \
vllm serve Qwen/Qwen2.5-VL-72B-Instruct-AWQ \
--tensor-parallel-size 4 \
--gpu-memory-utilization 0.9 \
--max-model-len 16384 \
--limit-mm-per-prompt image = 10 ,video= 5
8卡 T4 GPU(16GB显存)部署Qwen2.5-VL-72B-Instruct-AWQ 的优化配置方案
PYTORCH_CUDA_ALLOC_CONF = expandable_segments:True \
vllm serve Qwen/Qwen2.5-VL-72B-Instruct-AWQ \
--tensor-parallel-size 8 \
--quantization awq \
--gpu-memory-utilization 0.75 \
--dtype float16 \
--max-model-len 4096 \
--kv-cache-dtype fp8 \
--enforce-eager \
--trust-remote-code \
--host 0.0 .0.0 \
--port 8678
vllm serve Qwen/Qwen2.5-VL-72B-Instruct-AWQ --port 8000 --host 0.0 .0.0 --dtype float16 --limit-mm-per-prompt image = 3 ,video= 2 --quantization awq --tensor-parallel-size 8 --gpu-memory-utilization 0.85 --max-num-seqs 16 --enforce-eager --trust-remote-code --max-model-len 8192
参数说明
–kv-cache-dtype fp8 将KV缓存从FP16转为FP8,显存需求降低50%(关键优化点)
–gpu-memory-utilization 0.75 T4显存带宽较低(320GB/s),需预留更多空间给数据传输
2.2.3CURL 命令验证服务
API 服务部署成功之后,可以通过 CURL 命令验证服务
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen2.5-VL-72B-Instruct-AWQ",
"messages": [
{"role": "system", "content": "你是个友善的AI助手。"},
{"role": "user", "content": [{"type": "text", "text": "请介绍下多模态大模型"}]}
]}'
2.2.4通过 Python 客户端调用API 访问服务
from openai import OpenAI
openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"
client = OpenAI(
api_key= openai_api_key,
base_url= openai_api_base,
)
chat_response = client. chat. completions. create(
model= "Qwen/Qwen2.5-VL-72B-Instruct-AWQ" ,
messages= [
{ "role" : "system" , "content" : "你是个友善的AI助手。" } ,
{ "role" : "user" , "content" : [ { "type" : "text" , "text" : "请介绍下多模态大模型" } ] } ,
] ,
temperature= 0.7 ,
top_p= 0.8 ,
max_tokens= 512 ,
extra_body= {
"repetition_penalty" : 1.05 ,
} ,
)
print ( "Chat response:" , chat_response)
3.使用预构建的docker镜像部署
3.1 运行docker容器
docker run --gpus all --ipc = host --privileged --network = host -v /home:/home --name qwen2.5_vl -it qwenllm/qwenvl:2.5-cu121 bash
三、性能监控与常见问题解决
1.实时监控命令
watch -n 1 "nvidia-smi --query-gpu=memory.used,memory.total --format=csv"
curl http://localhost:8000/metrics | grep "vllm:requests_processed_total"
while true ; do
nvidia-smi --query-gpu= index,name,memory.used,memory.total,utilization.gpu \
--format = csv -l 1 | tee -a gpu_stats.csv
echo "Throughput: $( curl -s http://localhost:8678/metrics | grep 'tokens_generated_per_second' ) "
done
2.OOM(显存不足)错误
现象:CUDA out of memory
降低 --gpu-memory-utilization (每次调整0.05)
增加 --tensor-parallel-size
添加 --quantization awq(需模型支持)
--gpu-memory-utilization 0.75 → 0.7 → 0.65
--max-model-len 4096 → 3072
--kv-cache-dtype fp8 → auto
3.多卡负载不均
nvitop -m full --colorful
CUDA_VISIBLE_DEVICES = 0,1 vllm serve.. . --tensor-parallel-size 2
4.多媒体处理超时
降低 --limit-mm-per-prompt 数值
添加视频预处理参数(如降帧率):
model. load_media_processor(
video_config= { "max_fps" : 5 , "resolution" : 480 }
)
四、大模型部署资源评估
1.显存计算
总显存需求 = 模型参数显存 + KV缓存显存 + 多模态特征显存 + 安全余量(10-20%)
1.1模型参数显存计算
模型类型
计算公式
示例(Qwen2.5-VL-7B)
非量化模型(FP16)
参数量 × 2字节
7B × 2 = 14GB
AWQ量化模型(4bit)
参数量 × 0.5字节
72B × 0.5 = 36GB
多卡张量并行
总显存 ÷ 并行数 × 1.2(通信开销)
72B-AWQ用8卡:36÷8×1.2≈5.4GB/卡
1.2KV缓存显存计算
KV_cache_per_token = 2 × num_layers × hidden_size × num_heads_kv × bytes_per_param
Qwen2.5-VL-7B示例 :
32层,hidden_size=4096,num_heads_kv=32
FP16时:2×32×4096×32×2B = 16MB/token
若batch_size=4,seq_len=4096 → 4×4096×16MB ≈ 256GB(需张量并行分摊)
1.3多模态特征显存
媒体类型
特征计算公式
示例(512×512图像)
图像
分块数 × 特征维度 × dtype字节数
9块(3×3)× 512维 × 2B = 9KB/图
视频
关键帧数 × 图像特征 × 音频特征
24帧 × 9KB + 1MB音频 ≈ 217KB/视频
2.典型模型部署评估
2.1Qwen2.5-VL-7B-Instruct(非量化)
# 单卡部署场景(A10 24GB)
总显存需求 =
模型参数:14GB
+ KV缓存:batch_size=2, seq_len=4096 → 2×4096×16MB ≈ 128MB
+ 多模态:同时处理4图2视频 → (4×9KB)+(2×217KB) ≈ 0.5MB
+ 安全余量:14GB×20% = 2.8GB
≈ **16.9GB** < 24GB(可行)
# 多模态极限场景(处理10图5视频):
多模态显存增至:(10×9KB)+(5×217KB) ≈ 1.2MB → 总需求≈17.1GB
2.2Qwen2.5-VL-72B-Instruct-AWQ
# 8卡T4部署(16GB/卡)
单卡需求 =
模型参数:36GB÷8×1.2 = 5.4GB
+ KV缓存(FP8):batch_size=4, seq_len=4096 → 4×4096×(16MB/2)÷8 = 16MB
+ 多模态:2图1视频 → (2×9KB)+(1×217KB) ≈ 0.2MB
+ 安全余量:5.4GB×15% = 0.8GB
≈ **6.4GB** < 12GB(16GB×0.75利用率)
# 长上下文场景(seq_len=8192):
KV缓存显存翻倍 → 单卡需求≈6.4+16MB≈6.4GB(仍可行)
3.硬件选型参考表
模型
推荐配置
处理能力
成本估算(AWS)
Qwen2.5-VL-7B
1×A10G (24GB)
实时处理:4图+2视频@3s/req
$1.008/hr(g5.xlarge)
Qwen2.5-VL-72B-AWQ
8×T4 (16GB) + 64vCPU
批量处理:16req/s@8图上限
$3.912/hr(g4dn.metal)
高并发生产环境
4×A100 80GB + NVLink
100+ req/s@混合媒体输入
$32.77/hr(p4d.24xlarge)
4.优化策略与参数调整
4.1显存压缩技术
- - dtype bfloat16 \
- - kv- cache- dtype fp8 \
- - swap- space 16G \
- - chunked- prefix- reserve- ratio 0.2
4.2多模态处理优化
def adaptive_tiling ( img_size, max_tiles= 9 ) :
base_tile = 512 if img_size> 1024 else 256
rows = cols = int ( ( img_size// base_tile) ** 0.5 )
return rows* cols
4.3负载均衡配置
export NCCL_ALGO= Ring
export NCCL_P2P_DISABLE= 1
export CUDA_DEVICE_MAX_CONNECTIONS= 1
5.模型部署评估
5.1大模型显存分析
python -m vllm.model_analyzer \
--model Qwen/Qwen2.5-VL-72B-Instruct-AWQ \
--quantization awq \
--tensor-parallel-size 8 \
--profile memory \
--output csv \
--max-num-batched-tokens 4096
--breakdown-by layer \
--verbose 2
CUDA_VISIBLE_DEVICES = 0,1 ,2,3,4,5,6,7 \
python -m vllm.model_analyzer \
--tensor-parallel-size 8 \
--profile memory
--dtypes float16 bfloat16
--batch-sizes 1 4 8 16 \
--concurrency 10 20
--plot-memory-usage \
--output-dir ./plots
#!/bin/bash
MODEL = "Qwen/Qwen2.5-VL-72B-Instruct-AWQ"
TP_SIZE = 8
python -m vllm.model_analyzer \
--model $MODEL \
--quantization awq \
--tensor-parallel-size $TP_SIZE \
--profile memory \
--output markdown \
> memory_report.md
5.2多模态压力测试
from locust import HttpUser, task
class MultimodalUser ( HttpUser) :
@task
def send_request ( self) :
media_files = [
{ "type" : "image" , "size" : "1024x1024" } ,
{ "type" : "video" , "duration" : 30 }
]
self. client. post( "/generate" , json= { "inputs" : media_files} )
5.3显存监控看板
拉取指标
Prometheus
Grafana
显存仪表盘
模型参数显存
KV缓存显存
多模态特征显存
系统预留显存
所有评论(0)