运行环境基线
上游 README 给出的基线环境包括 Python 3.8+、PyTorch 1.12+、Transformers 4.32+ 和 CUDA 11.4+。
Flash Attention 不是必选项,但对于支持 fp16 或 bf16 的设备,README 明确建议安装它来提升效率并降低显存占用。
- Python 3.8 及以上
- PyTorch 1.12 及以上,推荐 2.0+
- Transformers 4.32 及以上
- 面向 GPU 的路径建议使用 CUDA 11.4+
快速上手流程
先安装基础依赖
如果你想尽量贴近上游路径,先执行 `pip install -r requirements.txt`。
只在硬件支持时再加 flash-attention
把 flash-attention 当作优化层,而不是前置条件,因为上游 README 明确说明不安装也能正常运行。
用 `trust_remote_code=True` 加载 chat 模型
官方 quickstart 直接通过公开模型仓库加载 tokenizer 和 chat 模型。
最小 Transformers 示例
上游 quickstart 把本地体验聚焦在直接调用 `model.chat()` 上。
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen-7B-Chat",
device_map="auto",
trust_remote_code=True
).eval()
response, history = model.chat(tokenizer, "你好", history=None)
print(response)