Qwen2是Qwen大型语言模型的新系列
224 Pulls 更新于2个月前
更新于2个月前
2个月前
8fdaa78a53fe · 4.9GB
说明文档
Qwen2-7B-Instruct
简介
Qwen2是Qwen大型语言模型的新系列。对于我们发布的Qwen2,我们从0.5到720亿参数覆盖各种基语言模型和指令微调语言模型,包括混合专家模型。本仓库包含指令微调的7B Qwen2模型。
与最先进的开源语言模型相比,包括之前发布的Qwen1.5,Qwen2在语言理解、语言生成、多语言能力、编码、数学、推理等一系列基准测试中,通常超越了大多数开源模型,并对私有模型表现出竞争力。
Qwen2-7B-Instruct支持最高131,072个token的上下文长度,能够处理大量输入。请参阅本节详细了解如何部署Qwen2处理长文本。
模型详情
Qwen2 是一个包含不同模型大小的解码器语言模型系列。对于每个大小,我们发布基本语言模型和对齐聊天模型。它是基于 Transformer 架构,具有 SwiGLU 激活,注意力 QKV 偏置,组查询注意力等功能。此外,我们还有一个改进的适应多种自然语言和代码的分词器。
训练细节
我们使用大量数据对模型进行了预训练,并进行了监督微调和直接偏好优化的后训练。
需求
Qwen2 代码已集成在最新的 Hugging Face transformers 中,我们建议您安装 transformers>=4.37.0
,否则可能会遇到以下错误
KeyError: 'qwen2'
快速入门
这里提供了一个使用 apply_chat_template
的代码片段,以展示如何加载分词器和模型以及如何生成内容。
from transformers import AutoModelForCausalLM, AutoTokenizer
device = "cuda" # the device to load the model onto
model = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen2-7B-Instruct",
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-7B-Instruct")
prompt = "Give me a short introduction to large language model."
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(device)
generated_ids = model.generate(
model_inputs.input_ids,
max_new_tokens=512
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
处理长文本
为了处理超过 32,768 令牌的扩展输入,我们使用了 YARN 技术,这是一种增强模型长距离外推的技术,以确保在长文本上获得最佳性能。
对于部署,我们建议使用 vLLM。您可以通过以下步骤启用长上下文功能
- 安装 vLLM:您可以通过运行以下命令来安装 vLLM。
pip install "vllm>=0.4.3"
或您可以从 源 安装 vLLM。
配置模型设置:下载模型权重后,通过以下片段修改
config.json
文件{ "architectures": [ "Qwen2ForCausalLM" ], // ... "vocab_size": 152064, // adding the following snippets "rope_scaling": { "factor": 4.0, "original_max_position_embeddings": 32768, "type": "yarn" } }
此片段使 YARN 支持更长的上下文。
模型部署:使用 vLLM 来部署您的模型。例如,您可以使用以下命令设置一个类似 openAI 的服务器
python -m vllm.entrypoints.openai.api_server --served-model-name Qwen2-7B-Instruct --model path/to/weights
然后您可以通过以下方式访问 Chat API
curl https://127.0.0.1:8000/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "Qwen2-7B-Instruct", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Your Long Input Here."} ] }'
有关 vLLM 的进一步使用说明,请参考我们的 Github。
注意:目前,vLLM 仅为静态 YARN 提供支持,这意味着缩放因子不受输入长度的影响,可能影响较短的文本性能。我们建议仅在需要处理长上下文时才添加 rope_scaling
配置。
评估
我们简要比较了 Qwen2-7B-Instruct 与类似规模的指令微调 LLM,包括 Qwen1.5-7B-Chat。结果如下所示
数据集 | Llama-3-8B-Instruct | Yi-1.5-9B-Chat | GLM-4-9B-Chat | Qwen1.5-7B-Chat | Qwen2-7B-Instruct |
---|---|---|---|---|---|
英语 | |||||
MMLU | 68.4 | 69.5 | 72.4 | 59.5 | 70.5 |
MMLU-Pro | 41.0 | - | - | 29.1 | 44.1 |
GPQA | 34.2 | - | - | 27.8 | 25.3 |
TheroemQA | 23.0 | - | - | 14.1 | 25.3 |
MT-Bench | 8.05 | 8.20 | 8.35 | 7.60 | 8.41 |
编码 | |||||
Humaneval | 62.2 | 66.5 | 71.8 | 46.3 | 79.9 |
MBPP | 67.9 | - | - | 48.9 | 67.2 |
MultiPL-E | 48.5 | - | - | 27.2 | 59.1 |
Evalplus | 60.9 | - | - | 44.8 | 70.3 |
LiveCodeBench | 17.3 | - | - | 6.0 | 26.6 |
数学 | |||||
GSM8K | 79.6 | 84.8 | 79.6 | 60.3 | 82.3 |
MATH | 30.0 | 47.7 | 50.6 | 23.2 | 49.6 |
中文 | |||||
C-Eval | 45.9 | - | 75.6 | 67.3 | 77.2 |
AlignBench | 6.20 | 6.90 | 7.01 | 6.20 | 7.21 |
引用
如果您觉得我们的工作有帮助,请随意引用。
@article{qwen2,
title={Qwen2 Technical Report},
year={2024}
}