模型简介
模型特点
模型能力
使用案例
🚀 神经聊天-7B-v3-1模型
神经聊天-7B-v3-1是一款基于英特尔Gaudi 2处理器微调的70亿参数大语言模型。它在开源数据集上进行训练,性能表现出色,能为多种语言相关任务提供支持。
🚀 快速开始
本模型是基于英特尔Gaudi 2处理器,在mistralai/Mistral-7B-v0.1基础上,于开源数据集Open-Orca/SlimOrca上微调得到的70亿参数大语言模型。该模型使用Direct Performance Optimization (DPO)方法,结合Intel/orca_dpo_pairs进行对齐。更多信息请参考Medium文章The Practice of Supervised Fine-tuning and Direct Preference Optimization on Intel Gaudi2。
Photo by Google DeepMind on Unsplash
✨ 主要特性
模型信息
属性 | 详情 |
---|---|
模型作者 - 公司 | Intel。NeuralChat团队,成员来自DCAI/AISE/AIPT。核心团队成员:吕考考、吕亮、王畅、张文心、任旭辉和沈浩昊。 |
日期 | 2023年10月 |
版本 | v3 - 1 |
模型类型 | 70亿参数大语言模型 |
论文或其他资源 | Medium博客 |
许可证 | Apache 2.0 |
问题或评论 | 社区板块 和 Intel DevHub Discord |
预期用途
预期用途 | 描述 |
---|---|
主要预期用途 | 可以使用微调后的模型进行多个与语言相关的任务。查看大语言模型排行榜,了解该模型的表现。 |
主要预期用户 | 任何进行与语言相关任务推理的人。 |
非预期用途 | 在大多数情况下,该模型需要针对特定任务进行微调。该模型不应被用于故意为人们创造敌对或疏离的环境。 |
📦 安装指南
训练超参数
训练期间使用了以下超参数:
- 学习率:1e - 04
- 训练批次大小:1
- 评估批次大小:2
- 随机种子:42
- 分布式类型:multi - HPU
- 设备数量:8
- 梯度累积步数:8
- 总训练批次大小:64
- 总评估批次大小:8
- 优化器:Adam,beta=(0.9, 0.999),epsilon = 1e - 08
- 学习率调度器类型:cosine
- 学习率调度器热身比例:0.03
- 训练轮数:2.0
复现模型
以下是复现模型的示例代码:GitHub示例代码。以下是复现构建模型的文档:
git clone https://github.com/intel/intel-extension-for-transformers.git
cd intel-extension-for-transformers
docker build --no-cache ./ --target hpu --build-arg REPO=https://github.com/intel/intel-extension-for-transformers.git --build-arg ITREX_VER=main -f ./intel_extension_for_transformers/neural_chat/docker/Dockerfile -t chatbot_finetuning:latest
docker run -it --runtime=habana -e HABANA_VISIBLE_DEVICES=all -e OMPI_MCA_btl_vader_single_copy_mechanism=none --cap-add=sys_nice --net=host --ipc=host chatbot_finetuning:latest
# 进入docker容器后
cd examples/finetuning/finetune_neuralchat_v3
我们选择最新的预训练模型mistralai/Mistral-7B-v0.1和开源数据集Open-Orca/SlimOrca进行实验。
以下脚本使用deepspeed zero2在8张Gaudi2卡上启动训练。在finetune_neuralchat_v3.py
中,Gaudi2的默认设置为use_habana=True, use_lazy_mode=True, device="hpu"
。如果要在NVIDIA GPU上运行,可以将其设置为use_habana=False, use_lazy_mode=False, device="auto"
。
deepspeed --include localhost:0,1,2,3,4,5,6,7 \
--master_port 29501 \
finetune_neuralchat_v3.py
合并LoRA权重:
python apply_lora.py \
--base-model-path mistralai/Mistral-7B-v0.1 \
--lora-model-path finetuned_model/ \
--output-path finetuned_model_lora
💻 使用示例
基础用法
FP32推理(使用Transformers)
import transformers
model_name = 'Intel/neural-chat-7b-v3-1'
model = transformers.AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
def generate_response(system_input, user_input):
# 使用提供的模板格式化输入
prompt = f"### System:\n{system_input}\n### User:\n{user_input}\n### Assistant:\n"
# 对提示进行分词和编码
inputs = tokenizer.encode(prompt, return_tensors="pt", add_special_tokens=False)
# 生成响应
outputs = model.generate(inputs, max_length=1000, num_return_sequences=1)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# 仅提取助手的响应
return response.split("### Assistant:\n")[-1]
# 示例用法
system_input = "You are a math expert assistant. Your mission is to help users understand and solve various math problems. You should provide step-by-step solutions, explain reasonings and give the correct answer."
user_input = "calculate 100 + 520 + 60"
response = generate_response(system_input, user_input)
print(response)
# 预期响应
"""
To calculate the sum of 100, 520, and 60, we will follow these steps:
1. Add the first two numbers: 100 + 520
2. Add the result from step 1 to the third number: (100 + 520) + 60
Step 1: Add 100 and 520
100 + 520 = 620
Step 2: Add the result from step 1 to the third number (60)
(620) + 60 = 680
So, the sum of 100, 520, and 60 is 680.
"""
BF16推理(使用Intel Extension for Transformers和Intel Extension for Pytorch)
from transformers import AutoTokenizer, TextStreamer
import torch
from intel_extension_for_transformers.transformers import AutoModelForCausalLM
import intel_extension_for_pytorch as ipex
model_name = "Intel/neural-chat-7b-v3-1"
prompt = "Once upon a time, there existed a little girl,"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
inputs = tokenizer(prompt, return_tensors="pt").input_ids
streamer = TextStreamer(tokenizer)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)
model = ipex.optimize(model.eval(), dtype=torch.bfloat16, inplace=True, level="O1", auto_kernel_selection=True)
outputs = model.generate(inputs, streamer=streamer, max_new_tokens=300)
INT4推理(使用Transformers和Intel Extension for Transformers)
from transformers import AutoTokenizer, TextStreamer
from intel_extension_for_transformers.transformers import AutoModelForCausalLM, WeightOnlyQuantConfig
model_name = "Intel/neural-chat-7b-v3-1"
# 对于int8,应设置weight_dtype="int8"
config = WeightOnlyQuantConfig(compute_dtype="bf16", weight_dtype="int4")
prompt = "Once upon a time, there existed a little girl,"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
inputs = tokenizer(prompt, return_tensors="pt").input_ids
streamer = TextStreamer(tokenizer)
model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=config)
outputs = model.generate(inputs, streamer=streamer, max_new_tokens=300)
高级用法
测试模型量化能力
以下代码块可用于确定PyTorch模型是否适合量化。需要注意的是,Intel Extension for PyTorch使用的optimum ipex处于预发布阶段,需要进一步测试。
要安装依赖项,应首先安装Intel Extensions for PyTorch,然后使用pip安装以下每个依赖项:
- torch
- optimum.intel
- optimum[ipex]
- transformers
Intel Extension for PyTorch方法:
在这种情况下,我们正在测试neural-chat-7b-v3-1是否可以量化,此测试方法展示了模型大小的变化,例如: 当基础类型指定为torch.bfloat16,但同时指定load_in_4bit = True,这会导致仅对权重进行量化,我们从模型测试中看到如下输出:
- model_quantize_internal: 模型大小 = 27625.02 MB
- model_quantize_internal: 量化后大小 = 4330.80 MB
此代码应在Python脚本中运行,例如ipex_test.py,如下所示:
import torch
import os
from transformers import AutoTokenizer
from intel_extension_for_transformers.transformers import AutoModelForCausalLM, pipeline
model_name = "Intel/neural-chat-7b-v3-1"
prompt = "Once upon a time, there existed a little girl,"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
inputs = tokenizer(prompt, return_tensors="pt").input_ids
result = {torch.bfloat16:"failed"}
typ = torch.bfloat16
try:
model = AutoModelForCausalLM.from_pretrained(model_name, load_in_4bit=True, torch_dtype = typ)
outputs = model.generate(inputs, max_new_tokens=20)
result[typ] = f"passed, {os.stat(model.bin_file).st_size}"
except:
result[typ] = "failed"
print("\n\nResults of quantizing: ")
# 确定是否量化
with open(r"output.log", 'r') as fp:
for l_no, line in enumerate(fp):
# 搜索字符串
if 'model_quantize_internal' in line:
print(line)
print("\n\nExecution results ")
for k,v in result.items():
print(k,v)
print("\n\nModel Output: ")
tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
从bash终端按以下方式运行代码:
python ipex_test.py 2>&1 | tee output.log
整个输出将捕获在output.log中,但会进行总结,同时还会包含模型输出,指示量化是否通过以及给定提示的模型输出。
📚 详细文档
影响因素
因素 | 描述 |
---|---|
数据集 | 有关数据集和注释的更多详细信息可以在Open-Orca/SlimOrca和相关论文https://arxiv.org/abs/2306.02707中找到。 |
输入影响 | 模型的性能可能会因输入而异。在这种情况下,提供的提示会极大地改变语言模型的预测。 |
训练环境 | 该模型在英特尔Gaudi 2处理器(8张卡)上进行训练。 |
硬件和软件影响 | 在其他硬件和软件上部署模型会改变模型性能。模型评估因素来自Hugging Face大语言模型排行榜:ARC、HellaSwag、MMLU、TruthfulQA、Winogrande、GSM8K和DROP(见下面的定量分析)。 |
评估指标
指标 | 描述 |
---|---|
模型性能评估 | 根据大语言模型排行榜上的指标,将该模型的性能与其他大语言模型进行评估。这些指标已成为大语言模型性能的标准。 |
决策阈值 | 未使用决策阈值。 |
不确定性和可变性处理方法 | - |
训练和评估数据
数据类型 | 描述 |
---|---|
数据集 | 训练数据来自Open-Orca/SlimOrca。没有来自GSM8k测试集的污染,因为这不是Open-Orca/SlimOrca数据集的一部分。 |
动机 | - |
预处理 | - |
🔧 技术细节
定量分析
该模型已提交到大语言模型排行榜。详细提交信息可在此处找到:https://huggingface.co/datasets/open-llm-leaderboard/details_Intel__neural-chat-7b-v3-1。以下指标显示该模型的性能相对于Mistral-7B-v0.1和neural-chat-7b-v3有显著提升。
模型 | 平均得分 ⬆️ | ARC (25-shot) ⬆️ | HellaSwag (10-shot) ⬆️ | MMLU (5-shot) ⬆️ | TruthfulQA (MC) (0-shot) ⬆️ | Winogrande (5-shot) | GSM8K (5-shot) | DROP (3-shot) |
---|---|---|---|---|---|---|---|---|
mistralai/Mistral-7B-v0.1 | 50.32 | 59.58 | 83.31 | 64.16 | 42.15 | 78.37 | 18.12 | 6.14 |
Intel/neural-chat-7b-v3 | 57.31 | 67.15 | 83.29 | 62.26 | 58.77 | 78.06 | 1.21 | 50.43 |
Intel/neural-chat-7b-v3-1 | 59.06 | 66.21 | 83.64 | 62.37 | 59.65 | 78.14 | 19.56 | 43.84 |
📄 许可证
本模型使用Apache 2.0许可证。
⚠️ 重要提示
神经聊天-7B-v3-1可能会产生事实错误的输出,不应依赖它来产生事实准确的信息。由于预训练模型和微调数据集的局限性,该模型有可能生成淫秽、有偏见或其他冒犯性的输出。因此,在部署神经聊天-7B-v3-1的任何应用程序之前,开发人员应进行安全测试。
💡 使用建议
用户(直接用户和下游用户)应了解该模型的风险、偏差和局限性。以下是一些有用的链接,可了解更多关于英特尔AI软件的信息:
免责声明
本模型的许可证不构成法律建议。我们不对使用此模型的第三方的行为负责。在将此模型用于商业目的之前,请咨询律师。



