zhaicunqi commited on
Commit
815be35
1 Parent(s): 1ac7913

Upload README_CN.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README_CN.md +549 -0
README_CN.md ADDED
@@ -0,0 +1,549 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - zh
5
+ - en
6
+ library_name: transformers
7
+ tags:
8
+ - qihoo360
9
+ - 奇虎360
10
+ - zhinao
11
+ - 360Zhinao
12
+ - pretrain
13
+ ---
14
+
15
+ <div align="center">
16
+ <h1>
17
+ 360智脑
18
+ </h1>
19
+ </div>
20
+ <div align="center">
21
+ 🤗 <a href="https://huggingface.co/qihoo360">Hugging Face</a>&nbsp&nbsp | &nbsp&nbsp
22
+ 🤖 <a href="https://www.modelscope.cn/profile/qihoo360">ModelScope</a>&nbsp&nbsp | &nbsp&nbsp
23
+ 💬 <a href="./assets/WeChat.png">WeChat (微信)</a>&nbsp&nbsp
24
+ </div>
25
+ <br>
26
+ <p align="center">
27
+ 欢迎访问360智脑官网<a href="https://ai.360.com"> https://ai.360.com </a>体验更多更强大的功能。
28
+ </p>
29
+
30
+ <br>
31
+
32
+ # 模型介绍
33
+ 🎉🎉🎉我们开源了360智脑大模型的系列工作,本次开源了以下模型:
34
+ - **360Zhinao-7B-Base**
35
+ - **360Zhinao-7B-Chat-4K**
36
+ - **360Zhinao-7B-Chat-32K**
37
+ - **360Zhinao-7B-Chat-360K**
38
+
39
+ 360智脑大模型特点如下:
40
+ - **基础模型**:采用 3.4 万亿 Tokens 的高质量语料库训练,以中文、英文、代码为主,在相关基准评测中,同尺寸有竞争力。
41
+ - **对话模型**:具有强大的对话能力,开放4K、32K、360K三种不同文本长度。据了解,360K(约50万字)是当前国产开源模型文本长度最长的。
42
+
43
+ <br>
44
+
45
+ # 更新信息
46
+ - [2024.04.10] 我们发布了360Zhinao-7B 1.0版本,同时开放Base模型和4K、32K、360K三种文本长度的Chat模型。
47
+
48
+ <br>
49
+
50
+ # 目录
51
+ - [下载地址](#下载地址)
52
+ - [模型评估](#模型评估)
53
+ - [快速开始](#快速开始)
54
+ - [模型推理](#模型推理)
55
+ - [模型微调](#模型微调)
56
+ - [许可证](#许可证)
57
+
58
+ <br>
59
+
60
+ # 下载地址
61
+ 本次发布版本和下载链接见下表:
62
+ | Size | Model | BF16 | Int4|
63
+ |:-:|-|:-:|:-:|
64
+ | 7B | 360Zhinao-7B-Base | <a href="https://www.modelscope.cn/models/qihoo360/360Zhinao-7B-Base/summary">🤖</a> <a href="https://huggingface.co/qihoo360/360Zhinao-7B-Base">🤗</a> | |
65
+ | 7B | 360Zhinao-7B-Chat-4K | <a href="https://www.modelscope.cn/models/qihoo360/360Zhinao-7B-Chat-4K/summary">🤖</a> <a href="https://huggingface.co/qihoo360/360Zhinao-7B-Chat-4K">🤗</a> | <a href="https://www.modelscope.cn/models/qihoo360/360Zhinao-7B-Chat-4K-Int4/summary">🤖</a> <a href="https://huggingface.co/qihoo360/360Zhinao-7B-Chat-4K-Int4">🤗</a> |
66
+ | 7B | 360Zhinao-7B-Chat-32K | <a href="https://www.modelscope.cn/models/qihoo360/360Zhinao-7B-Chat-32K/summary">🤖</a> <a href="https://huggingface.co/qihoo360/360Zhinao-7B-Chat-32K">🤗</a> | <a href="https://www.modelscope.cn/models/qihoo360/360Zhinao-7B-Chat-32K-Int4/summary">🤖</a> <a href="https://huggingface.co/qihoo360/360Zhinao-7B-Chat-32K-Int4">🤗</a> |
67
+ | 7B | 360Zhinao-7B-Chat-360K | <a href="https://www.modelscope.cn/models/qihoo360/360Zhinao-7B-Chat-360K/summary">🤖</a> <a href="https://huggingface.co/qihoo360/360Zhinao-7B-Chat-360K">🤗</a> | <a href="https://www.modelscope.cn/models/qihoo360/360Zhinao-7B-Chat-360K-Int4/summary">🤖</a> <a href="https://huggingface.co/qihoo360/360Zhinao-7B-Chat-360K-Int4">🤗</a> |
68
+
69
+ <br>
70
+
71
+ # 模型评估
72
+
73
+ ## 基础模型
74
+ 我们在OpenCompass的主流评测数据集上验证了我们的模型性能,包括C-Eval、AGIEval、MMLU、CMMLU、HellaSwag、MATH、GSM8K、HumanEval、MBPP、BBH、LAMBADA,考察的能力包括自然语言理解、知识、数学计算和推理、代码生成、逻辑推理等。
75
+
76
+
77
+ | <div style="width: 100pt">Model</div> | AVG | CEval | AGIEval | MMLU | CMMLU | HellaSwag | MATH | GSM8K | HumanEval | MBPP | BBH | LAMBADA |
78
+ |:----------------------|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|
79
+ | Baichuan2-7B | 41.49 | 56.3 | 34.6 | 54.7 | 57 | 67 | 5.4 | 24.6 | 17.7 | 24 | 41.8 | 73.3 |
80
+ | Baichuan-7B | 31.94 | 44.7 | 24.6 | 41.5 | 44.6 | 68.4 | 2.5 | 9.6 | 9.1 | 6.4 | 32.8 | 67.1 |
81
+ | ChatGLM3-6B | **58.67** | 67 | 47.4 | 62.8 | 66.5 | 76.5 | 19.2 | 61 | 44.5 | **57.2** | **66.2** | 77.1 |
82
+ | DeepSeek-7B | 39.8 | 45 | 24 | 49.3 | 46.8 | 73.4 | 4.2 | 18.3 | 25 | 36.4 | 42.8 | 72.6 |
83
+ | InternLM2-7B | 58.01 | 65.7 | 50.2 | 65.5 | 66.2 | 79.6 | 19.9 | **70.6** | 41.5 | 42.4 | 64.4 | 72.1 |
84
+ | InternLM-7B | 39.33 | 53.4 | 36.9 | 51 | 51.8 | 70.6 | 6.3 | 31.2 | 13.4 | 14 | 37 | 67 |
85
+ | LLaMA-2-7B | 33.27 | 32.5 | 21.8 | 46.8 | 31.8 | 74 | 3.3 | 16.7 | 12.8 | 14.8 | 38.2 | 73.3 |
86
+ | LLaMA-7B | 30.35 | 27.3 | 20.6 | 35.6 | 26.8 | 74.3 | 2.9 | 10 | 12.8 | 16.8 | 33.5 | 73.3 |
87
+ | Mistral-7B-v0.1 | 47.67 | 47.4 | 32.8 | 64.1 | 44.7 | 78.9 | 11.3 | 47.5 | 27.4 | 38.6 | 56.7 | 75 |
88
+ | MPT-7B | 30.06 | 23.5 | 21.3 | 27.5 | 25.9 | 75 | 2.9 | 9.1 | 17.1 | 22.8 | 35.6 | 70 |
89
+ | Qwen1.5-7B | 55.12 | 73.57 | **50.8** | 62.15 | 71.84 | 72.62 | **20.36** | 54.36 | **53.05** | 36.8 | 40.01 | 70.74 |
90
+ | Qwen-7B | 49.53 | 63.4 | 45.3 | 59.7 | 62.5 | 75 | 13.3 | 54.1 | 27.4 | 31.4 | 45.2 | 67.5 |
91
+ | XVERSE-7B | 34.27 | 61.1 | 39 | 58.4 | 60.8 | 73.7 | 2.2 | 11.7 | 4.9 | 10.2 | 31 | 24 |
92
+ | Yi-6B | 47.8 | 73 | 44.3 | 64 | **73.5** | 73.1 | 6.3 | 39.9 | 15.2 | 23.6 | 44.9 | 68 |
93
+ | **360Zhinao-7B** | 56.15 | **74.11** | 49.49 | **67.44** | 72.38 | **83.05** | 16.38 | 53.83 | 35.98 | 42.4 | 43.95 | **78.59** |
94
+
95
+ 以上结果,在官方[Opencompass](https://rank.opencompass.org.cn/leaderboard-llm)上可查询或可复现。
96
+
97
+ ## Chat模型
98
+
99
+ 我们采用两阶段的方式训练长文本模型.
100
+
101
+ **第一阶段**:我们增大RoPE base,将上下文长度扩展至32K训练:
102
+ - 首先,对基础模型进行了约5B tokens的32K窗口继续预训练。
103
+ - 接着,SFT阶段使用了多种形式和来源的长文本数据,包括高质量的人工标注32K长文本数据。
104
+
105
+ **第二阶段**:我们将上下文长度扩展至360K进行训练,使用数据如下:
106
+ - 少量高质量人工标注数据。
107
+ - 由于带有标注的超长文本数据的稀缺性,我们构造了多种形式的合成数据:
108
+ - 多文档问答:类似[Ziya-Reader](https://arxiv.org/abs/2311.09198),我们基于360自有数据构造了多种类型的多文档问答数据,同时将问答改为多轮,显著提升长文本的训练效率。
109
+ - 单文档问答:类似[LLama2 Long](https://arxiv.org/abs/2309.16039),我们构造了基于超长文本各个片段的多轮问答数据。
110
+
111
+ 我们在多种长度和多种任务的评测Benchmark上验证不同版本模型的性能。
112
+
113
+ - ### 360Zhinao-7B-Chat-32K模型长文本能力评测
114
+
115
+
116
+ 我们使用LongBench验证长文本效果。[LongBench](https://github.com/THUDM/LongBench)是第一个多任务、中英双语、针对大语言模型长文本理解能力的评测基准。LongBench由六大类、二十一个不同的任务组成,我们选择其中与中文长文本应用最密切相关的中文单文档问答、多文档问答、摘要、Few-shot等任务进行评测。
117
+
118
+ | Model | Avg | 单文档QA | 多文档QA | 摘要 | Few-shot学习 | 代码补全 |
119
+ | :------------------------ |:---------:|:--------:|:---------:|:---------:|:------------:|:---------:|
120
+ | GPT-3.5-Turbo-16k | 37.84 | 61.2 | 28.7 | 16 | 29.2 | 54.1 |
121
+ | ChatGLM2-6B-32k | 37.16 | 51.6 | 37.6 | 16.2 | 27.7 | 52.7 |
122
+ | ChatGLM3-6B-32k | 44.62 | **62.3** | 44.8 | 17.8 | 42 | 56.2 |
123
+ | InternLM2-Chat-7B | 42.20 | 56.65 | 29.15 | **17.99** | 43.5 | **63.72** |
124
+ | Qwen1.5-Chat-7B | 36.75 | 52.85 | 30.08 | 14.28 | 32 | 54.55 |
125
+ | Qwen1.5-Chat-14B | 39.80 | 60.39 | 27.99 | 14.77 | 37 | 58.87 |
126
+ | 360Zhinao-7B-Chat-32K | **45.18** | 57.18 | **48.06** | 15.03 | **44** | 61.64 |
127
+
128
+ - ### 360Zhinao-7B-Chat-360K“大海捞针”测试
129
+
130
+ 大海捞针测试([NeedleInAHaystack](https://github.com/gkamradt/LLMTest_NeedleInAHaystack))是将关键信息插入一段长文本的不同位置,再对该关键信息提问,从而测试大模型的长文本能力的一种方法。
131
+
132
+ 360Zhinao-7B-Chat-360K在中英文大海捞针中都能达到98%以上的准确率。
133
+
134
+ - 英文"大海捞针"(和[NeedleInAHaystack](https://github.com/gkamradt/LLMTest_NeedleInAHaystack)相同)
135
+
136
+ <p align="center">
137
+ <img src="assets/360Zhinao-7B-Chat-360K.en_score.png" width="600" />
138
+ <p>
139
+
140
+ **针**:The best thing to do in San Francisco is eat a sandwich and sit in Dolores Park on a sunny day.
141
+
142
+ **提问**:What is the best thing to do in San Francisco?
143
+
144
+
145
+ - 中文“大海捞针”
146
+
147
+ <p align="center">
148
+ <img src="assets/360Zhinao-7B-Chat-360K.zh_score.png" width="600" />
149
+ <p>
150
+
151
+ 我们仿照[SuperCLUE-200K测评基准](https://mp.weixin.qq.com/s/QgoRf2LB-7vc3vTFOHJkpw)构造了中文大海捞针:
152
+
153
+ **海**:长篇小说。
154
+
155
+ **针**:王莽是一名勤奋的店员,他每天凌晨就起床,赶在第一缕阳光照亮大地之���到达店铺,为即将开始的一天做准备。他清扫店铺,整理货架,为顾客提供方便。他对五金的种类和用途了如指掌,无论顾客需要什么,他总能准确地找到。\n然而,他的老板刘秀却总是对他吹毛求疵。刘秀是个挑剔的人,他总能在王莽的工作中找出一些小错误,然后以此为由扣他的工资。他对王莽的工作要求非常严格,甚至有些过分。即使王莽做得再好,刘秀也总能找出一些小问题,让王莽感到非常沮丧。\n王莽虽然对此感到不满,但他并没有放弃。他知道,只有通过自己的努力,才能获得更好的生活。他坚持每天早起,尽管他知道那天可能会再次被刘秀扣工资。他始终保持微笑,尽管他知道刘秀可能会再次对他挑剔。
156
+
157
+ **提问**:王莽在谁的手下工作?
158
+
159
+ <br>
160
+
161
+ # 快速开始
162
+ 简单的示例来说明如何利用🤖 ModelScope和🤗 Transformers快速使用360Zhinao-7B-Base和360Zhinao-7B-Chat
163
+
164
+ ## 依赖安装
165
+ - python 3.8 and above
166
+ - pytorch 2.0 and above
167
+ - transformers 4.37.2 and above
168
+ - CUDA 11.4 and above are recommended.
169
+
170
+ ```shell
171
+ pip install -r requirements.txt
172
+ ```
173
+ 我们推荐安装flash-attention(当前已支持flash attention 2)来提高你的运行效率以及降低显存占用。(flash-attention只是可选项,不安装也可正常运行该项目)
174
+
175
+ >flash-attn >= 2.3.6
176
+ ```shell
177
+ FLASH_ATTENTION_FORCE_BUILD=TRUE pip install flash-attn==2.3.6
178
+ ```
179
+
180
+
181
+ ## 🤗 Transformers
182
+ ### Base模型推理
183
+
184
+ 此代码演示使用transformers快速使用360Zhinao-7B-Base模型进行推理
185
+ ```python
186
+ from transformers import AutoTokenizer, AutoModelForCausalLM
187
+ from transformers.generation import GenerationConfig
188
+
189
+ MODEL_NAME_OR_PATH = "qihoo360/360Zhinao-7B-Base"
190
+
191
+ tokenizer = AutoTokenizer.from_pretrained(
192
+ MODEL_NAME_OR_PATH,
193
+ trust_remote_code=True)
194
+
195
+ model = AutoModelForCausalLM.from_pretrained(
196
+ MODEL_NAME_OR_PATH,
197
+ device_map="auto",
198
+ trust_remote_code=True)
199
+
200
+ generation_config = GenerationConfig.from_pretrained(
201
+ MODEL_NAME_OR_PATH,
202
+ trust_remote_code=True)
203
+
204
+ inputs = tokenizer('中国二十四节气\n1. 立春\n2. 雨水\n3. 惊蛰\n4. 春分\n5. 清明\n', return_tensors='pt')
205
+ inputs = inputs.to(model.device)
206
+
207
+ pred = model.generate(input_ids=inputs["input_ids"], generation_config=generation_config)
208
+ print("outputs:\n", tokenizer.decode(pred.cpu()[0], skip_special_tokens=True))
209
+ ```
210
+
211
+ ### Chat模型推理
212
+
213
+ 此代码演示使用transformers快速使用360Zhinao-7B-Chat-4K模型进行推理
214
+ ```python
215
+ from transformers import AutoTokenizer, AutoModelForCausalLM
216
+ from transformers.generation import GenerationConfig
217
+
218
+ MODEL_NAME_OR_PATH = "qihoo360/360Zhinao-7B-Chat-4K"
219
+
220
+ tokenizer = AutoTokenizer.from_pretrained(
221
+ MODEL_NAME_OR_PATH,
222
+ trust_remote_code=True)
223
+
224
+ model = AutoModelForCausalLM.from_pretrained(
225
+ MODEL_NAME_OR_PATH,
226
+ device_map="auto",
227
+ trust_remote_code=True)
228
+
229
+ generation_config = GenerationConfig.from_pretrained(
230
+ MODEL_NAME_OR_PATH,
231
+ trust_remote_code=True)
232
+
233
+ messages = []
234
+ #round-1
235
+ messages.append({"role": "user", "content": "介绍一下刘德华"})
236
+ response = model.chat(tokenizer=tokenizer, messages=messages, generation_config=generation_config)
237
+ messages.append({"role": "assistant", "content": response})
238
+ print(messages)
239
+
240
+ #round-2
241
+ messages.append({"role": "user", "content": "他有什么代表作?"})
242
+ response = model.chat(tokenizer=tokenizer, messages=messages, generation_config=generation_config)
243
+ messages.append({"role": "assistant", "content": response})
244
+ print(messages)
245
+ ```
246
+
247
+ ## 🤖 ModelScope
248
+ ### Base模型推理
249
+
250
+ 此代码演示使用ModelScope快速使用360Zhinao-7B-Base模型进行推理
251
+
252
+
253
+ ```python
254
+ from modelscope import AutoModelForCausalLM, AutoTokenizer
255
+ from modelscope import GenerationConfig
256
+
257
+ MODEL_NAME_OR_PATH = "qihoo360/360Zhinao-7B-Base"
258
+
259
+ tokenizer = AutoTokenizer.from_pretrained(
260
+ MODEL_NAME_OR_PATH,
261
+ trust_remote_code=True)
262
+
263
+ model = AutoModelForCausalLM.from_pretrained(
264
+ MODEL_NAME_OR_PATH,
265
+ device_map="auto",
266
+ trust_remote_code=True)
267
+
268
+ generation_config = GenerationConfig.from_pretrained(
269
+ MODEL_NAME_OR_PATH,
270
+ trust_remote_code=True)
271
+
272
+ inputs = tokenizer('中国二十四节气\n1. 立春\n2. 雨水\n3. 惊蛰\n4. 春分\n5. 清明\n', return_tensors='pt')
273
+ inputs = inputs.to(model.device)
274
+
275
+ pred = model.generate(input_ids=inputs["input_ids"], generation_config=generation_config)
276
+ print("outputs:\n", tokenizer.decode(pred.cpu()[0], skip_special_tokens=True))
277
+ ```
278
+
279
+ ### Chat模型推理
280
+
281
+ 此代码演示使用ModelScope快速使用360Zhinao-7B-Chat-4K模型进行推理
282
+ ```python
283
+ from modelscope import AutoModelForCausalLM, AutoTokenizer
284
+ from modelscope import GenerationConfig
285
+
286
+ MODEL_NAME_OR_PATH = "qihoo360/360Zhinao-7B-Chat-4K"
287
+
288
+ tokenizer = AutoTokenizer.from_pretrained(
289
+ MODEL_NAME_OR_PATH,
290
+ trust_remote_code=True)
291
+
292
+ model = AutoModelForCausalLM.from_pretrained(
293
+ MODEL_NAME_OR_PATH,
294
+ device_map="auto",
295
+ trust_remote_code=True)
296
+
297
+ generation_config = GenerationConfig.from_pretrained(
298
+ MODEL_NAME_OR_PATH,
299
+ trust_remote_code=True)
300
+
301
+ messages = []
302
+ #round-1
303
+ messages.append({"role": "user", "content": "介绍一下刘德华"})
304
+ response = model.chat(tokenizer=tokenizer, messages=messages, generation_config=generation_config)
305
+ messages.append({"role": "assistant", "content": response})
306
+ print(messages)
307
+
308
+ #round-2
309
+ messages.append({"role": "user", "content": "他有什么代表作?"})
310
+ response = model.chat(tokenizer=tokenizer, messages=messages, generation_config=generation_config)
311
+ messages.append({"role": "assistant", "content": response})
312
+ print(messages)
313
+ ```
314
+
315
+ ## 终端 Demo
316
+ 可使用终端交互实现快速体验
317
+ ```shell
318
+ python cli_demo.py
319
+ ```
320
+ <p align="center">
321
+ <img src="assets/cli_demo.gif" width="600" />
322
+ <p>
323
+
324
+ ## 网页 Demo
325
+ 也可使用网页交互实现快速体验
326
+ ```shell
327
+ streamlit run web_demo.py
328
+ ```
329
+ <p align="center">
330
+ <img src="assets/web_demo.gif" width="600" />
331
+ <p>
332
+
333
+ ## API Demo
334
+ 启动命令
335
+ ```shell
336
+ python openai_api.py
337
+ ```
338
+
339
+ 请求参数
340
+ ```shell
341
+ curl 'http://localhost:8360/v1/chat/completions' \
342
+ -H 'Content-Type: application/json' \
343
+ -d '{
344
+ "max_new_tokens": 200,
345
+ "do_sample": true,
346
+ "top_k": 0,
347
+ "top_p": 0.8,
348
+ "temperature": 1.0,
349
+ "repetition_penalty": 1.0,
350
+ "messages": [
351
+ {"role": "system", "content": "You are a helpful assistant."},
352
+ {"role": "user", "content": "你好"}
353
+ ]
354
+ }'
355
+ ```
356
+
357
+ <br>
358
+
359
+ # 模型推理
360
+ ## 模型量化
361
+ 我们提供了基于AutoGPTQ的量化方案,并开源了Int4量化模型。
362
+
363
+ ## 模型部署
364
+ ### vLLM安装环境
365
+ 如希望部署及加速推理,我们建议你使用 `vLLM==0.3.3`。
366
+
367
+ 如果你使用**CUDA 12.1和PyTorch 2.1**,可以直接使用以下命令安装vLLM。
368
+ ```shell
369
+ pip install vllm==0.3.3
370
+ ```
371
+
372
+ 否则请参考vLLM官方的[安装说明](https://docs.vllm.ai/en/latest/getting_started/installation.html)。
373
+
374
+ >安装完成后,还需要以下操作~
375
+ 1. 把vllm/zhinao.py文件复制到env环境对应的vllm/model_executor/models目录下。
376
+ 2. 把vllm/serving_chat.py文件复制到env环境对应的vllm/entrypoints/openai目录下。
377
+ 3. 然后在vllm/model_executor/models/\_\_init\_\_.py文件增加一行代码
378
+
379
+ ```shell
380
+ "ZhinaoForCausalLM": ("zhinao", "ZhinaoForCausalLM"),
381
+ ```
382
+
383
+ ### vLLM服务启动
384
+
385
+ 启动服务
386
+ ```shell
387
+ python -m vllm.entrypoints.openai.api_server \
388
+ --served-model-name 360Zhinao-7B-Chat-4K \
389
+ --model qihoo360/360Zhinao-7B-Chat-4K \
390
+ --trust-remote-code \
391
+ --tensor-parallel-size 1 \
392
+ --max-model-len 4096 \
393
+ --host 0.0.0.0 \
394
+ --port 8360
395
+ ```
396
+
397
+ 使用curl请求服务
398
+ ```shell
399
+ curl http://localhost:8360/v1/chat/completions \
400
+ -H "Content-Type: application/json" \
401
+ -d '{
402
+ "model": "360Zhinao-7B-Chat-4K",
403
+ "max_tokens": 200,
404
+ "top_k": -1,
405
+ "top_p": 0.8,
406
+ "temperature": 1.0,
407
+ "presence_penalty": 0.0,
408
+ "frequency_penalty": 0.0,
409
+ "messages": [
410
+ {"role": "system", "content": "You are a helpful assistant."},
411
+ {"role": "user", "content": "你好"}
412
+ ],
413
+ "stop": [
414
+ "<eod>",
415
+ "<|im_end|>",
416
+ "<|im_start|>"
417
+ ]
418
+ }'
419
+ ```
420
+ 使用python请求服务
421
+ ```python
422
+ from openai import OpenAI
423
+ # Set OpenAI's API key and API base to use vLLM's API server.
424
+ openai_api_key = "EMPTY"
425
+ openai_api_base = "http://localhost:8360/v1"
426
+
427
+ client = OpenAI(
428
+ api_key=openai_api_key,
429
+ base_url=openai_api_base,
430
+ )
431
+
432
+ chat_response = client.chat.completions.create(
433
+ model="360Zhinao-7B-Chat-4K",
434
+ messages=[
435
+ {"role": "system", "content": "You are a helpful assistant."},
436
+ {"role": "user", "content": "你好"},
437
+ ],
438
+ stop=[
439
+ "<eod>",
440
+ "<|im_end|>",
441
+ "<|im_start|>"
442
+ ],
443
+ presence_penalty=0.0,
444
+ frequency_penalty=0.0
445
+ )
446
+ print("Chat response:", chat_response)
447
+ ```
448
+
449
+ > 注意:如需要开启重复惩罚,建议使用 *presence_penalty* 和 *frequency_penalty* 参数。
450
+
451
+ <br>
452
+
453
+ # 模型微调
454
+ ## 训练数据
455
+
456
+ 我们提供了微调训练样例数据 data/test.json,该样例数据是从 [multiturn_chat_0.8M](https://huggingface.co/datasets/BelleGroup/multiturn_chat_0.8M) 采样出 1 万条,并且做了格式转换。
457
+
458
+ 数据格式:
459
+ ```json
460
+ [
461
+ {
462
+ "id": 1,
463
+ "conversations": [
464
+ {
465
+ "from": "system",
466
+ "value": "You are a helpful assistant."
467
+ },
468
+ {
469
+ "from": "user",
470
+ "value": "您好啊"
471
+ },
472
+ {
473
+ "from": "assistant",
474
+ "value": "你好!我今天能为您做些什么?有什么问题或需要帮助吗? 我在这里为您提供服务。"
475
+ }
476
+ ]
477
+ }
478
+ ]
479
+ ```
480
+
481
+ ## 微调训练
482
+ 训练脚本如下:
483
+ ```shell
484
+ set -x
485
+
486
+ HOSTFILE=hostfile
487
+ DS_CONFIG=./finetune/ds_config_zero2.json
488
+
489
+ # PARAMS
490
+ LR=5e-6
491
+ EPOCHS=3
492
+ MAX_LEN=4096
493
+ BATCH_SIZE=4
494
+ NUM_NODES=1
495
+ NUM_GPUS=8
496
+ MASTER_PORT=29500
497
+
498
+ IS_CONCAT=False # 是否数据拼接到��大长度(MAX_LEN)
499
+
500
+ DATA_PATH="./data/training_data_sample.json"
501
+ MODEL_PATH="qihoo360/360Zhinao-7B-Base"
502
+ OUTPUT_DIR="./outputs/"
503
+
504
+ deepspeed --hostfile ${HOSTFILE} \
505
+ --master_port ${MASTER_PORT} \
506
+ --num_nodes ${NUM_NODES} \
507
+ --num_gpus ${NUM_GPUS} \
508
+ finetune.py \
509
+ --report_to "tensorboard" \
510
+ --data_path ${DATA_PATH} \
511
+ --model_name_or_path ${MODEL_PATH} \
512
+ --output_dir ${OUTPUT_DIR} \
513
+ --model_max_length ${MAX_LEN} \
514
+ --num_train_epochs ${EPOCHS} \
515
+ --per_device_train_batch_size ${BATCH_SIZE} \
516
+ --gradient_accumulation_steps 1 \
517
+ --save_strategy steps \
518
+ --save_steps 200 \
519
+ --learning_rate ${LR} \
520
+ --lr_scheduler_type cosine \
521
+ --adam_beta1 0.9 \
522
+ --adam_beta2 0.95 \
523
+ --adam_epsilon 1e-8 \
524
+ --max_grad_norm 1.0 \
525
+ --weight_decay 0.1 \
526
+ --warmup_ratio 0.01 \
527
+ --gradient_checkpointing True \
528
+ --bf16 True \
529
+ --tf32 True \
530
+ --deepspeed ${DS_CONFIG} \
531
+ --is_concat ${IS_CONCAT} \
532
+ --logging_steps 1 \
533
+ --log_on_each_node False
534
+ ```
535
+ ```shell
536
+ bash finetune/ds_finetune.sh
537
+ ```
538
+ - 可通过配置hostfile,实现单机、多机训练。
539
+ - 可通过配置ds_config,实现zero2、zero3。
540
+ - 可通过配置fp16、bf16实现混合精度训练,建议使用bf16,与预训练模型保持一致。
541
+ - 可通过配置is_concat参数,控制训练数据是否拼接,当训练数据量级较大时,可通过拼接提升训练效率。
542
+
543
+ <br>
544
+
545
+ # 许可证
546
+
547
+ 本仓库源码遵循开源许可证Apache 2.0。
548
+
549
+ 360智脑开源模型支持商用,若需将本模型及衍生模型用于商业用途,请通过邮箱(g-zhinao-opensource@360.cn)联系进行申请, 具体许可协议请见[《360智脑开源模型许可证》](https://github.com/Qihoo360/360zhinao/blob/main/360%E6%99%BA%E8%84%91%E5%BC%80%E6%BA%90%E6%A8%A1%E5%9E%8B%E8%AE%B8%E5%8F%AF%E8%AF%81.txt)。