InternLM2 模型卡梳理:20 万字长上下文与全维度能力提升

梳理 InternLM2 系列模型的特性:在 20 万字长输入中几乎完美实现长文大海捞针、在 LongBench 与 L-Eval 等长文任务上领先,并给出 Model Zoo、性能表现、依赖安装与半精度及量化加载的使用示例。

📘Commercial Application | 🤗HuggingFace | 📜Technical Report

简介

InternLM2 系列模型在本仓库正式发布,具有如下特性:

  • 有效支持20万字超长上下文:模型在 20 万字长输入中几乎完美地实现长文“大海捞针”,而且在 LongBench 和 L-Eval 等长文任务中的表现也达到开源模型中的领先水平。 可以通过 LMDeploy 尝试20万字超长上下文推理。
  • 综合性能全面提升:各能力维度相比上一代模型全面进步,在推理、数学、代码、对话体验、指令遵循和创意写作等方面的能力提升尤为显著,综合性能达到同量级开源模型的领先水平,在重点能力评测上 InternLM2-Chat-20B 能比肩甚至超越 ChatGPT (GPT-3.5)。
  • 代码解释器与数据分析:在配合代码解释器(code-interpreter)的条件下,InternLM2-Chat-20B 在 GSM8K 和 MATH 上可以达到和 GPT-4 相仿的水平。基于在数理和工具方面强大的基础能力,InternLM2-Chat 提供了实用的数据分析能力。
  • 工具调用能力整体升级:基于更强和更具有泛化性的指令理解、工具筛选与结果反思等能力,新版模型可以更可靠地支持复杂智能体的搭建,支持对工具进行有效的多轮调用,完成较复杂的任务。可以查看更多样例

    更新

[2024.03.26] 我们发布了 InternLM2 的技术报告。 可以点击 arXiv链接 来了解更多细节。

[2024.01.31] 我们发布了 InternLM2-1.8B,以及相关的对话模型。该模型在保持领先性能的情况下,提供了更低廉的部署方案。

[2024.01.23] 我们发布了 InternLM2-Math-7B 和 InternLM2-Math-20B 以及相关的对话模型。InternLM-Math以较小的尺寸超过了ChatGPT的表现。可以点击InternLM-Math进行下载,并了解详情。

[2024.01.17] 我们发布了 InternLM2-7B 和 InternLM2-20B 以及相关的对话模型,InternLM2 在数理、代码、对话、创作等各方面能力都获得了长足进步,综合性能达到开源模型的领先水平。可以点击下面的模型库进行下载或者查看模型文档来了解更多细节.

[2023.12.13] 我们更新了 InternLM-7B-Chat 和 InternLM-20B-Chat 模型权重。通过改进微调数据和训练策略,新版对话模型生成的回复质量更高、语言风格更加多元。

[2023.09.20] InternLM-20B 已发布,包括基础版和对话版。

Model Zoo

模型说明:

在此次发布中,InternLM2 包含两种模型规格:7B 和 20B。7B 为轻量级的研究和应用提供了一个轻便但性能不俗的模型,20B 模型的综合性能更为强劲,可以有效支持更加复杂的实用场景。每个规格不同模型关系如下图所示:

  1. InternLM2-Base:高质量和具有很强可塑性的模型基座,是模型进行深度领域适配的高质量起点。
  2. InternLM2:进一步在大规模无标签数据上进行预训练,并结合特定领域的增强语料库进行训练,在评测中成绩优异,同时保持了很好的通用语言能力,是我们推荐的在大部分应用中考虑选用的优秀基座。
  3. InternLM2-Chat-SFT: 基于 InternLM2-Base 模型进行了有监督微调,是 InternLM2-Chat 模型的中间版本。我们将它们开源以助力社区在对齐方面的研究。
  4. InternLM2-Chat: 在 InternLM2-Chat-SFT 的基础上进行了 online RLHF 以进一步对齐. InternLM2-Chat 面向对话交互进行了优化,具有较好的指令遵循、共情聊天和调用工具等的能力,是我们推荐直接用于下游应用的模型。

局限性: 尽管在训练过程中我们非常注重模型的安全性,尽力促使模型输出符合伦理和法律要求的文本,但受限于模型大小以及概率生成范式,模型可能会产生各种不符合预期的输出,例如回复内容包含偏见、歧视等有害内容,请勿传播这些内容。由于传播不良信息导致的任何后果,本项目不承担责任。

补充说明: 上表中的 HF 表示对应模型为 HuggingFace 平台提供的 transformers 框架格式;Origin 则表示对应模型为我们 InternLM 团队的 InternEvo 框架格式。

性能

客观评测

DatasetBaichuan2-7B-ChatMistral-7B-Instruct-v0.2Qwen-7B-ChatInternLM2-Chat-7BChatGLM3-6BBaichuan2-13B-ChatMixtral-8x7B-Instruct-v0.1Qwen-14B-ChatInternLM2-Chat-20B
MMLU50.159.257.163.758.056.670.366.766.5
CMMLU53.442.057.963.057.854.850.668.165.1
AGIEval35.334.539.747.244.240.041.746.550.3
C-Eval53.942.459.860.859.156.354.071.563.0
TrivialQA37.635.046.150.838.140.357.754.553.9
NaturalQuestions12.88.118.624.114.012.722.522.925.9
C378.566.984.491.579.384.482.191.593.5
CMRC8.15.614.663.843.227.85.313.050.4
WinoGrande49.950.854.265.861.750.960.955.774.8
BBH35.946.545.561.256.042.557.355.868.3
GSM-8K32.448.344.170.753.856.071.757.779.6
Math5.78.612.023.020.44.322.527.631.9
HumanEval17.735.436.059.852.419.537.840.967.1
MBPP37.725.733.951.455.640.940.930.065.8
  • MBPP性能使用的是MBPP(Sanitized)版本数据集

主观评测

  • 我们评测了InternLM2-Chat在AlpacaEval 2.0 上的性能,结果表明InternLM2-Chat在AlpacaEval上已经超过了 Claude 2, GPT-4(0613) 和 Gemini Pro.

    Model NameWin RateLength
    GPT-4 Turbo50.00%2049
    GPT-423.58%1365
    GPT-4 031422.07%1371
    Mistral Medium21.86%1500
    XwinLM 70b V0.121.81%1775
    InternLM2 Chat 20B21.75%2373
    Mixtral 8x7B v0.118.26%1465
    Claude 217.19%1069
    Gemini Pro16.85%1315
    GPT-4 061315.76%1140
    Claude 2.115.73%1096
  • 性能数据截止2024-01-17

    依赖

  • Python >= 3.8

  • PyTorch >= 1.12.0 (推荐 2.0.0 和更高版本)

  • Transformers >= 4.34

    使用案例

接下来我们展示使用 TransformersModelScopeWeb demo 进行推理。 对话模型采用了 chatml 格式 来支持通用对话和智能体应用。 为了保障更好的使用效果,在用 TransformersModelScope 进行推理前,请确保安装的 transformers 库版本满足以下要求:

1
transformers >= 4.34

通过 Transformers 加载

通过以下的代码从 Transformers 加载 InternLM2-7B-Chat 模型 (可修改模型名称替换不同的模型)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("internlm/internlm2-chat-7b", trust_remote_code=True)
# 设置`torch_dtype=torch.float16`来将模型精度指定为torch.float16,否则可能会因为您的硬件原因造成显存不足的问题。
model = AutoModelForCausalLM.from_pretrained("internlm/internlm2-chat-7b", device_map="auto",trust_remote_code=True, torch_dtype=torch.float16)
# (可选) 如果在低资源设备上,可以通过bitsandbytes加载4-bit或8-bit量化的模型,进一步节省GPU显存.
# 4-bit 量化的 InternLM 7B 大约会消耗 8GB 显存.
# pip install -U bitsandbytes
# 8-bit: model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, load_in_8bit=True)
# 4-bit: model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, load_in_4bit=True)
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
print(response)
# 模型输出:你好!有什么我可以帮助你的吗?
response, history = model.chat(tokenizer, "请提供三个管理时间的建议。", history=history)
print(response)

通过 ModelScope 加载

通过以下的代码从 ModelScope 加载 InternLM 模型 (可修改模型名称替换不同的模型)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import torch
from modelscope import snapshot_download, AutoTokenizer, AutoModelForCausalLM
model_dir = snapshot_download('Shanghai_AI_Laboratory/internlm2-chat-7b')
tokenizer = AutoTokenizer.from_pretrained(model_dir, device_map="auto", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, torch_dtype=torch.float16)
# (可选) 如果在低资源设备上,可以通过bitsandbytes加载4-bit或8-bit量化的模型,进一步节省GPU显存.
# 4-bit 量化的 InternLM 7B 大约会消耗 8GB 显存.
# pip install -U bitsandbytes
# 8-bit: model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, load_in_8bit=True)
# 4-bit: model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, load_in_4bit=True)
model = model.eval()
response, history = model.chat(tokenizer, "hello", history=[])
print(response)
response, history = model.chat(tokenizer, "please provide three suggestions about time management", history=history)
print(response)

基于 InternLM 高性能部署

我们使用 LMDeploy 完成 InternLM 的一键部署。

通过 pip install lmdeploy>=0.2.1 安装 LMDeploy 之后,只需 4 行代码,就可以实现离线批处理:

1
2
3
4
from lmdeploy import pipeline
pipe = pipeline("internlm/internlm2-chat-7b")
response = pipe(["Hi, pls intro yourself", "Shanghai is"])
print(response)

20万字超长上下文推理

激活 LMDeploy 的 Dynamic NTK 能力,可以轻松把 internlm2-chat-7b 外推到 200K 上下文

1
2
3
4
5
6
7
from lmdeploy import pipeline, GenerationConfig, TurbomindEngineConfig

backend_config = TurbomindEngineConfig(rope_scaling_factor=2.0, session_len=160000)
pipe = pipeline('internlm/internlm2-chat-7b', backend_config=backend_config)
prompt = 'Use a long prompt to replace this sentence'
response = pipe(prompt)
print(response)

微调 InternLM

XTuner 是一个高效、灵活、全能的轻量化大模型微调工具库。

亮点

  1. 支持大语言模型 LLM、多模态图文模型 VLM 的预训练及轻量级微调。XTuner 支持在 8GB 显存下微调 7B 模型,同时也支持多节点跨设备微调更大尺度模型(70B+)。
  2. 支持 QLoRALoRA、全量参数微调等多种微调算法,支撑用户根据具体需求作出最优选择。
  3. 兼容 DeepSpeed 🚀,轻松应用各种 ZeRO 训练优化策略。
  4. 训练所得模型可无缝接入部署工具库 LMDeploy、大规模评测工具库 OpenCompassVLMEvalKit

安装

  • 借助 conda 准备虚拟环境
  • 安装集成 DeepSpeed 版本的 XTuner

微调

  • 步骤 0,准备配置文件。XTuner 提供多个开箱即用的配置文件,用户可以通过下列命令查看所有 InternLM2 的预置配置文件:
  • 步骤 1,开始微调。
  • 步骤 2,将保存的 PTH 模型(如果使用的DeepSpeed,则将会是一个文件夹)转换为 HuggingFace 模型:

对话

XTuner 提供与大模型对话的工具。

1
xtuner chat ${NAME_OR_PATH_TO_LLM} [optional arguments]

例如:

与 InternLM2-Chat-7B, oasst1 adapter 对话:

1
xtuner chat internlm/internlm2-chat-7b --adapter xtuner/internlm2-chat-7b-qlora-oasst1 --prompt-template internlm2_chat

与 LLaVA-InternLM2-7B 对话:

1
xtuner chat internlm/internlm2-chat-7b --visual-encoder openai/clip-vit-large-patch14-336 --llava xtuner/llava-internlm2-7b --prompt-template internlm2_chat --image $IMAGE_PATH
本文结束 感谢您的阅读