Qwen2 是 Qwen 系列大型语言模型的全新版本。在 Qwen2 中,我们发布了一系列基础语言模型和指令微调语言模型,参数规模从 0.50 亿到 720 亿不等,其中还包括一个混合专家模型(Mixture-of-Experts)。本仓库包含的是经过指令微调的 0.50 亿参数 Qwen2 模型。
与当前最先进的开源语言模型(包括此前发布的 Qwen1.5)相比,Qwen2 在一系列针对语言理解、文本生成、多语言能力、代码编写、数学运算、逻辑推理等任务的基准测试中,普遍超越了大多数开源模型,并展现出与专有模型相竞争的实力。
Qwen2 是一个语言模型系列,包含不同参数规模的解码器语言模型。对于每种规模,我们都会发布基础语言模型和经过对齐的对话模型。该系列模型基于 Transformer 架构,采用了 SwiGLU 激活函数、注意力 QKV 偏置、分组查询注意力(group query attention)等技术。此外,我们还改进了分词器,使其能够更好地适应多种自然语言和代码。
我们使用海量数据对模型进行了预训练,并通过监督微调(supervised finetuning)和直接偏好优化(direct preference optimization)对模型进行了后续训练。
Qwen2 的代码已集成到最新版的 Hugging Face Transformers 库中,建议您安装 transformers>=4.37.0,否则可能会遇到以下错误:
KeyError: 'qwen2'这里提供一个使用apply_chat_template的代码片段,向您展示如何加载分词器和模型以及如何生成内容。
from openmind import AutoTokenizer, AutoModelForCausalLM, is_torch_npu_available
from openmind_hub import snapshot_download
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
help="Path to model",
default="jeffding/Qwen2-0.5B-Instruct-openmind",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
model_path = args.model_name_or_path
if is_torch_npu_available():
device = "npu"
else:
device = "cpu"
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_path)
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]
print(response)
if __name__ == "__main__":
main()我们对Qwen2-0.5B-Instruct与Qwen1.5-0.5B-Chat进行了简要对比,结果如下:
| 数据集 | Qwen1.5-0.5B-Chat | Qwen2-0.5B-Instruct | Qwen1.5-1.8B-Chat | Qwen2-1.5B-Instruct |
|---|---|---|---|---|
| MMLU | 35.0 | 37.9 | 43.7 | 52.4 |
| HumanEval | 9.1 | 17.1 | 25.0 | 37.8 |
| GSM8K | 11.3 | 40.1 | 35.3 | 61.6 |
| C-Eval | 37.2 | 45.2 | 55.3 | 63.8 |
| IFEval(Prompt Strict-Acc.) | 14.6 | 20.0 | 16.8 | 29.0 |
如果您觉得我们的工作对您有所帮助,欢迎引用。
@article{qwen2,
title={Qwen2 Technical Report},
year={2024}
}