Qwen2是Qwen大型语言模型的新系列

7B

224次引用 更新于2个月前

2个月前

0f939085f8d6 · 8.1GB

model
qwen2
·
7.62B
·
Q8_0
params
{"stop":["<|im_start|>","<|im_end|>"]}
template
{{ if .System }}<|im_start|>system {{ .System }}<|im_end|>{{ end }}<|im_start|>user {{ .Prompt }}<|im_end|> <|im_start|>assistant
license
Tongyi Qianwen RESEARCH LICENSE AGREEMENT Tongyi Qianwen Release Date: November 30, 2023 By clicking to agree or by using or distributing any portion or element of the Tongyi Qianwen Materials, you will be deemed to have recognized and accepted the content of this Agreement, which is effective immediately. 1. Definitions a. This Tongyi Qianwen RESEARCH LICENSE AGREEMENT (this "Agreement") shall mean the terms and conditions for use, reproduction, distribution and modification of the Materials as defined by this Agreement. b. "We"(or "Us") shall mean Alibaba Cloud. c. "You" (or "Your") shall mean a natural person or legal entity exercising the rights granted by this Agreement and/or using the Materials for any purpose and in any field of use. d. "Third Parties" shall mean individuals or legal entities that are not under common control with Us or You. e. "Tongyi Qianwen" shall mean the large language models, and software and algorithms, consisting of trained model weights, parameters (including optimizer states), machine-learning model code, inference-enabling code, training-enabling code, fine-tuning enabling code and other elements of the foregoing distributed by Us. f. "Materials" shall mean, collectively, Alibaba Cloud's proprietary Tongyi Qianwen and Documentation (and any portion thereof) made available under this Agreement. g. "Source" form shall mean the preferred form for making modifications, including but not limited to model source code, documentation source, and configuration files. h. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. i. "Non-Commercial" shall mean for research or evaluation purposes only. 2. Grant of Rights a. You are granted a non-exclusive, worldwide, non-transferable and royalty-free limited license under Alibaba Cloud's intellectual property or other rights owned by Us embodied in the Materials to use, reproduce, distribute, copy, create derivative works of, and make modifications to the Materials FOR NON-COMMERCIAL PURPOSES ONLY. b. If you are commercially using the Materials, You shall request a license from Us. 3. Redistribution You may reproduce and distribute copies of the Materials or derivative works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: a. You shall give any other recipients of the Materials or derivative works a copy of this Agreement; b. You shall cause any modified files to carry prominent notices stating that You changed the files; c. You shall retain in all copies of the Materials that You distribute the following attribution notices within a "Notice" text file distributed as a part of such copies: "Tongyi Qianwen is licensed under the Tongyi Qianwen RESEARCH LICENSE AGREEMENT, Copyright (c) Alibaba Cloud. All Rights Reserved."; and d. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such derivative works as a whole, provided Your use, reproduction, and distribution of the work otherwise complies with the terms and conditions of this Agreement. 4. Rules of use a. The Materials may be subject to export controls or restrictions in China, the United States or other countries or regions. You shall comply with applicable laws and regulations in your use of the Materials. b. You can not use the Materials or any output therefrom to improve any other large language model (excluding Tongyi Qianwen or derivative works thereof). 5. Intellectual Property a. We retain ownership of all intellectual property rights in and to the Materials and derivatives made by or for Us. Conditioned upon compliance with the terms and conditions of this Agreement, with respect to any derivative works and modifications of the Materials that are made by you, you are and will be the owner of such derivative works and modifications. b. No trademark license is granted to use the trade names, trademarks, service marks, or product names of Us, except as required to fulfill notice requirements under this Agreement or as required for reasonable and customary use in describing and redistributing the Materials. c. If you commence a lawsuit or other proceedings (including a cross-claim or counterclaim in a lawsuit) against Us or any entity alleging that the Materials or any output therefrom, or any part of the foregoing, infringe any intellectual property or other right owned or licensable by you, then all licences granted to you under this Agreement shall terminate as of the date such lawsuit or other proceeding is commenced or brought. 6. Disclaimer of Warranty and Limitation of Liability a. We are not obligated to support, update, provide training for, or develop any further version of the Tongyi Qianwen Materials or to grant any license thereto. b. THE MATERIALS ARE PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED WARRANTY OF ANY KIND INCLUDING WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT, OR FITNESS FOR A PARTICULAR PURPOSE. WE MAKE NO WARRANTY AND ASSUME NO RESPONSIBILITY FOR THE SAFETY OR STABILITY OF THE MATERIALS AND ANY OUTPUT THEREFROM. c. IN NO EVENT SHALL WE BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE MATERIALS OR ANY OUTPUT OF IT, NO MATTER HOW IT’S CAUSED. d. You will defend, indemnify and hold harmless Us from and against any claim by any third party arising out of or related to your use or distribution of the Materials. 7. Survival and Termination. a. The term of this Agreement shall commence upon your acceptance of this Agreement or access to the Materials and will continue in full force and effect until terminated in accordance with the terms and conditions herein. b. We may terminate this Agreement if you breach any of the terms or conditions of this Agreement. Upon termination of this Agreement, you must delete and cease use of the Materials. Sections 6 and 8 shall survive the termination of this Agreement. 8. Governing Law and Jurisdiction. a. This Agreement and any dispute arising out of or relating to it will be governed by the laws of China, without regard to conflict of law principles, and the UN Convention on Contracts for the International Sale of Goods does not apply to this Agreement. b. The People's Courts in Hangzhou City shall have exclusive jurisdiction over any dispute arising out of this Agreement. 9. Other Terms and Conditions. a. Any arrangements, understandings, or agreements regarding the Material not stated herein are separate from and independent of the terms and conditions of this Agreement. You shall request a seperate license from Us, if You use the Materials in ways not expressly agreed to in this Agreement. b. We shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed.

说明文件

Qwen2-7B-Instruct

简介

Qwen2是Qwen大型语言模型的新系列。对于Qwen2,我们发布了从0.5亿到72亿参数的一系列基础语言模型和指令微调语言模型,包括混合专家模型。此仓库包含指令微调的7亿参数Qwen2模型。

与先前发布的Qwen1.5等其他最先进的开源语言模型相比,Qwen2在包括语言理解、语言生成、多语言能力、编码、数学、推理等多个基准测试中,普遍超过了大多数开源模型,并且与专有模型展示了竞争力。

Qwen2-7B-Instruct支持的最大上下文长度为131,072个token,可处理大量输入。请参阅本节获取如何部署Qwen2以处理长文本的详细说明。

有关更多详细信息,请参阅我们的博客GitHub文档

模型详情

Qwen2 是一个包含不同尺寸解码语言模型的语言模型系列。对于每个尺寸,我们发布了基础语言模型和同步聊天模型。它基于 Transformer 架构,使用 SwiGLU 激活,注意 QKV 偏置,分组查询注意力等功能。此外,我们还拥有一个适合多种自然语言和代码的改进型分词器。

训练细节

我们使用大量数据进行模型的预训练,并通过监督微调和直接偏好优化对模型进行后续训练。

要求

Qwen2 的代码已经包含在最新的 Hugging Face Transformer 中,我们建议您安装 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。您可以按照以下步骤启用长上下文功能

  1. 安装 vLLM:您可以通过以下命令安装 vLLM。
pip install "vllm>=0.4.3"

或者您可以从 源代码 安装 vLLM。

  1. 配置模型设置:下载模型权重后,通过以下代码片段修改 config.json 文件

        {
            "architectures": [
                "Qwen2ForCausalLM"
            ],
            // ...
            "vocab_size": 152064,
    
    
            // adding the following snippets
            "rope_scaling": {
                "factor": 4.0,
                "original_max_position_embeddings": 32768,
                "type": "yarn"
            }
        }
    

    此代码片段使 YARN 支持更长的上下文。

  2. 模型部署:利用 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}
}