千问大模型部署笔记
目录
依赖项:
Instruct 和base 区别是什么
Qwen2.5-1.5B (Base)
🔹 Qwen2.5-0.5B-Instruct
Qwen2.5-VL
微调显存
Qwen2.5-VL-7B-Instruct 推理 模型15G, 8G显存不够
Qwen2.5-VL-3B-Instruct 推理 模型7G, 8G显存不够
千问7B模型推理代码:
依赖项:
pip install --upgrade huggingface_hub
pip install huggingface_hub[hf_xet]
pip install hf_xet
huggingface_hub login
Instruct 和base 区别是什么
Qwen2.5-1.5B (Base)
-
用途:预训练语言模型(类似 GPT-2/3),只是学会了预测下一个词。
-
特点:
-
需要明确提示(prompt engineering)才能得到比较好的回答。
-
没有专门对话能力,直接问问题可能会答非所问。
-
适合二次训练(如继续做指令微调、SFT、LoRA、RLHF 等)。
-
-
适合人群:研究者 / 想在基础模型上做 定制微调 的开发者。
🔹 Qwen2.5-0.5B-Instruct
-
用途:指令微调模型,阿里已经基于人类指令数据(SFT + RLHF/DPO)训练过。
-
特点:
-
开箱即用,直接问它问题(中文/英文)就能得到 对话式的回答。
-
适合问答、写代码、翻译、总结等任务。
-
容量小(0.5B 参数),运行非常轻量,但效果和知识覆盖面比大模型差一些。
-
-
适合人群:想要 轻量聊天/推理/测试环境,但不想自己再训练的开发者。
Qwen2.5-VL
Qwen2.5-VL-7B (INT4量化, 如GPTQ, AWQ) | ~6-8 GB | 8 GB | 在6-8GB显存上可运行,效果和速度相对INT8更好,是你的显存范围内的首选。 |
Qwen2.5-VL-7B (INT8量化) | ~8-10 GB | 10 GB | 可能需要接近8GB显存上限,或有溢出风险,需谨慎尝试。 |
Qwen2.5-VL-3B (原生或量化) | ~3-6 GB | 6 GB | 显存需求更低,但模型能力相对7B会有所减弱。 |
微调显存
-
若要进行全参数微调,你需要多张高端显卡(如A100 80GB) 和 DeepSpeed ZeRO 等分布式优化技术 。
-
若使用 LoRA 微调,单张24GB显存的显卡(如RTX 3090/4090) 是推荐的起点 。
-
若使用 QLoRA 微调,有望在单张24GB显存的消费级显卡上运行,但需要一些额外的优化和调试
Qwen2.5-VL-7B-Instruct 推理 模型15G, 8G显存不够
# Use a pipeline as a high-level helper
from transformers import pipelinepipe = pipeline("image-text-to-text", model="Qwen/Qwen2.5-VL-7B-Instruct")
messages = [{"role": "user","content": [{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"},{"type": "text", "text": "What animal is on the candy?"}]},
]
pipe(text=messages)
Qwen2.5-VL-3B-Instruct 推理 模型7G, 8G显存不够
这个可以跑
# Use a pipeline as a high-level helper
from transformers import pipelinepipe = pipeline("image-text-to-text", model="/home/lbg/data/models/Qwen2.5-VL-3B-Instruct")
messages = [{"role": "user","content": [{"type": "image", "url": "/home/lbg/project/seg/samurai/scripts/0904_1447_405.jpg"},{"type": "text", "text": "What animal is on the candy?"}]},
]
result = pipe(text=messages)
print(result)
千问7B模型推理代码:
from transformers import AutoTokenizer, AutoModelForCausalLMmodel_name = "Qwen/Qwen-7B" # 或者 "Qwen/Qwen-7B-Chat"tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name,device_map="auto", # 自动分配到GPUtrust_remote_code=True,load_in_8bit=True # 如果显存不够,可以用8bit量化
)prompt = "你好,请用一句话介绍你自己。"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=100)print(tokenizer.decode(outputs[0], skip_special_tokens=True))