caizhi1 commited on
Commit
8644884
·
verified ·
1 Parent(s): a5f475b

Add files using upload-large-folder tool

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
chat_format.py ADDED
@@ -0,0 +1,875 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''AntGLM Chat-model data format.
2
+
3
+ 格式化 AntGLM 以及各种开源模型的符号系统:
4
+ - 确定 Chat 模型依赖的文件数据结构协议
5
+ - 确定单轮/多轮的统一结构
6
+ - 确定 Chat 符号系统的协议, 包括角色定义、分隔符等
7
+ - 方便做开源模型依赖的 prompt 转换
8
+ - 支持工具、代码、推理等支持
9
+
10
+ 参考 FastChat Conversation 对象的设计思路.
11
+ Reference: https://github.com/lm-sys/FastChat/blob/main/fastchat/conversation.py
12
+ '''
13
+
14
+ import copy
15
+ import dataclasses
16
+ import logging
17
+ import re
18
+ import uuid
19
+ from copy import deepcopy
20
+ from enum import IntEnum, auto
21
+ from typing import Dict, List, Optional, Tuple
22
+
23
+ logger = logging.getLogger(__name__)
24
+
25
+
26
+ class PromptStyle(IntEnum):
27
+ '''Prompt styles.'''
28
+
29
+ # 原始 antglm format 格式, 单轮指令没有结构, 多轮 `第1轮\n用户: xx\n机器人: xx\n`
30
+ ANTGLM_RAW = auto()
31
+ # Chat format 格式, 单轮多轮统一为 chat format 格式
32
+ ANTGLM_CHAT = auto()
33
+ # 单轮指令没有结构, 只有多轮为 chat format 格式
34
+ ANTGLM_ONLY_MULTITURN_CHAT = auto()
35
+ # OpenAI ChatML 格式, 包括千问
36
+ CHATML = auto()
37
+ # LLAMA2 格式
38
+ LLAMA2 = auto()
39
+ # ChatGLM 1/2 格式
40
+ CHATGLM = auto()
41
+ # ChatGLM3 格式
42
+ CHATGLM3 = auto()
43
+ # 百川格式
44
+ BAICHUAN2 = auto()
45
+
46
+
47
+ @dataclasses.dataclass
48
+ class Chat:
49
+ '''Chat 数据符号结构, 格式化 AntGLM 以及各种开源模型的符号系统.
50
+
51
+ Examples:
52
+
53
+ ```python
54
+ >>> from antllm.data.chat_format import Chat
55
+
56
+ >>> ### 从 json 数据结构创建 chat 对象, 并且 format 结构使用 AntGLM 原始结构
57
+ >>> input_json = {
58
+ ... "messages": [
59
+ ... {"role": "HUMAN", "content": "讲一个笑话"},
60
+ ... {"role": "ASSISTANT", "content": "为什么猪不能上网?因为它们会被网上的“猪”骗!哈哈哈!"},
61
+ ... {"role": "HUMAN", "content": "不好笑,换个程序员的笑话"}
62
+ ... ],
63
+ ... }
64
+ >>> chat = Chat.from_json(input_json, name='antglm_raw')
65
+
66
+ >>> ### 根据 chat 对象创建大模型训练所需 pack 数据
67
+ >>> pack_data = chat.prompt_pack
68
+ >>> print(pack_data)
69
+
70
+ >>> ### 根据 chat 对象创建大模型训练所需 input, output 数据
71
+ >>> data = chat.prompt_inout
72
+ >>> print(data)
73
+
74
+ >>> ### 根据 chat 对象创建大模型预测用的 prompt
75
+ >>> prompt = chat.prompt_str
76
+ >>> print(prompt)
77
+
78
+ >>> ### 从大模型训练数据 {"input": "xx", "output": "xx"} 中创建 chat 对象
79
+ >>> data = {
80
+ ... 'input': (
81
+ ... '第1轮\n用户: 讲一个笑话\n机器人: 为什么猪不能上网?因为它们会被网上的“猪”骗!哈哈哈!\n'
82
+ ... '第2轮\n用户: 不好笑,换个程序员的笑话\n机器人:'
83
+ ... ),
84
+ ... 'output': ''
85
+ ... }
86
+ >>> chat = Chat.from_inout(data, name='antglm_raw')
87
+
88
+ >>> ### 从大模型 pack 训练数据创建 chat 对象列表
89
+ >>> pack_data = {
90
+ ... 'inputs': ['第1轮\n用户: 讲一个笑话\n机器人:', '第2轮\n用户: 不好笑,换个程序员的笑话\n机器人:', '第1轮\n用户: 写首诗\n机器人:'],
91
+ ... 'outputs': [
92
+ ... '为什么猪不能上网?因为它们会被网上的“猪”骗!哈哈哈!\n',
93
+ ... '为什么程序员总是喜欢使用黑色主题?因为他们喜欢“黑暗模式”(Dark Mode),这样他们就可以在晚上加班时更好地隐藏自己的错误!',
94
+ ... '']
95
+ ... }
96
+ >>> chats = Chat.from_pack(pack_data, name='antglm_raw')
97
+ >>> assert len(chats) == 2
98
+ >>> print(chats[0])
99
+ >>> print(chats[1])
100
+
101
+ >>> ### 显示总交互轮数 (以用户输出多少次为轮数个数)
102
+ >>> print(chat.turns_num)
103
+
104
+ >>> ### 根据 chat 对象创建 json 格式化输出
105
+ >>> data_json = chat.to_json()
106
+ >>> print(data_json)
107
+
108
+ >>> ### 增加轮次信息
109
+ >>> content = (
110
+ ... '为什么程序员总是喜欢使用黑色主题?'
111
+ ... '因为他们喜欢“黑暗模式”(Dark Mode),这样他们就可以在晚上加班时更好地隐藏自己的错误!'
112
+ ... )
113
+ >>> chat.append_message(chat.role_assistant, content)
114
+
115
+ >>> ### 将 chat 对象转成 OpenAI ChatCompletion 接口的入参
116
+ >>> openai_messages = chat.to_openai_api_messages()
117
+ >>> print(openai_messages)
118
+
119
+ >>> ### 复制一个 chat 对象
120
+ >>> chat_new = chat.copy()
121
+ ```
122
+ '''
123
+
124
+ # 数据结构名称
125
+ id: str = None
126
+
127
+ # format 支持: antglm_raw, antglm_chat, chatglm1, chatglm2, llama2, qwen, baichuan2
128
+ name: Optional[str] = None
129
+
130
+ # Prompt 风格
131
+ prompt_style: Optional[PromptStyle] = None
132
+
133
+ # System Template 和 message
134
+ system_template: str = '<role>SYSTEM</role>{}'
135
+ system_message: str = ''
136
+
137
+ # 角色定义
138
+ role_human: str = 'HUMAN'
139
+ role_assistant: str = 'ASSISTANT'
140
+ role_observation: str = 'OBSERVATION'
141
+ role_template: str = '<role>{}</role>'
142
+
143
+ # 每轮符号定义
144
+ turn_start: str = ''
145
+ human_end: str = ''
146
+ assistant_start: str = ''
147
+ assistant_end: str = ''
148
+ assistant_end_ids: Optional[List[int]] = None
149
+ general_role_end: str = ''
150
+
151
+ # agent 符号定义
152
+ tool_template = '<tool>{}</tool>'
153
+ code_template = '<code>{}</code>'
154
+ arithemetic_templte = '<arithemetic>{}</arithemetic>'
155
+ image_template = '<image>{}</image>'
156
+
157
+ # All messages. Each item is (role, message).
158
+ messages: List[Tuple[str, str]] = ()
159
+
160
+ # messages 中用于 few-shot messages
161
+ offset: int = 0
162
+
163
+ # 其他 meta data
164
+ source: Optional[str] = None
165
+ lang: Optional[str] = None
166
+ topic: Optional[str] = None
167
+
168
+ # 原始 json 数据
169
+ origin_json: Optional[dict] = None
170
+
171
+ @property
172
+ def support_names(self) -> Dict[str, str]:
173
+ '''支持的数据对象名称.'''
174
+ return {
175
+ 'antglm_raw': '原始 antglm format 格式, 单轮指令没有结构, 多轮 `第1轮\\n用户:xx\\n机器人xx\\n`',
176
+ 'antglm_chat': 'Chat format 格式, 单轮多轮统一为 chat format 格式',
177
+ 'chatglm1': 'chatglm1 format',
178
+ 'chatglm2': 'chatglm2 format',
179
+ 'llama2': 'llama2 format',
180
+ 'qwen': '千问 format',
181
+ 'baichuan2': '百川 2 format',
182
+ }
183
+
184
+ @classmethod
185
+ def from_json(
186
+ cls,
187
+ input: dict,
188
+ name: Optional[str] = None,
189
+ prompt_style: Optional[PromptStyle] = None,
190
+ ):
191
+ '''从文件数据结构到数据对象的转换.
192
+
193
+ Params:
194
+ name: `Optional[str]`, 符号系统名称
195
+ - format 支持: antglm_raw, antglm_chat, chatglm1, chatglm2, llama2, qwen, baichuan2
196
+ - 如果指定了 format name, 使用该 name 符号系统, 否则使用 input 中 `name` 字段
197
+
198
+ prompt_style: `Optional[PromptStyle]`, 指定 prompt 风格, 默认使用和 name 一致的风格
199
+
200
+ input: `dict`, 文件中的 json dict 对象, 协议为:
201
+ - 既支持 `messages` 字段, 也支持 `turns` 字段
202
+ {
203
+ "id": "xxx",
204
+ "name": "antglm",
205
+ "source": "xxx",
206
+ "lang": "xx",
207
+ "topic": "xx",
208
+ "system_template": "",
209
+ "system_message": "xx",
210
+ "messages": [
211
+ {
212
+ "role": "HUMAN",
213
+ "content": "Hi"
214
+ },
215
+ {
216
+ "role": "ASSISTANT",
217
+ "content": "Hello"
218
+ },
219
+ {
220
+ "role": "OBSERVATION",
221
+ "content": "xxx"
222
+ },
223
+ {
224
+ "role": "ASSISTANT",
225
+ "content": "xxx"
226
+ }
227
+ ],
228
+ "turns": [
229
+ {"HUMAN": "xxx", "OBSERVATION": "xx", "ASSISTANT": "xx"}
230
+ ]
231
+ }
232
+
233
+ Returns:
234
+ `Chat` 对象
235
+ '''
236
+ _id = input.get('id')
237
+ if name:
238
+ _name = name
239
+ else:
240
+ _name = input.get('name')
241
+ source = input.get('source')
242
+ lang = input.get('lang')
243
+ topic = input.get('topic')
244
+ kwargs = {}
245
+ if 'system_template' in input:
246
+ kwargs['system_template'] = input['system_template']
247
+ if 'system_message' in input:
248
+ kwargs['system_message'] = input['system_message']
249
+
250
+ # 转换成 Chat 对象
251
+ chat = cls(
252
+ id=_id,
253
+ name=_name,
254
+ prompt_style=prompt_style,
255
+ source=source,
256
+ lang=lang,
257
+ topic=topic,
258
+ origin_json=deepcopy(input),
259
+ **kwargs,
260
+ )
261
+ if 'messages' in input:
262
+ for msg in input['messages']:
263
+ if msg['role'] == 'HUMAN':
264
+ role = chat.role_human
265
+ elif msg['role'] == 'OBSERVATION':
266
+ role = chat.role_observation
267
+ elif msg['role'] == 'ASSISTANT':
268
+ role = chat.role_assistant
269
+ else:
270
+ raise ValueError(f'不支持数据集中的 role: {msg["role"]}')
271
+
272
+ chat.append_message(role, msg['content'])
273
+
274
+ elif 'turns' in input:
275
+ for turn in input['turns']:
276
+ if 'HUMAN' in turn:
277
+ content = turn['HUMAN']
278
+ chat.append_message(chat.role_human, content)
279
+ if 'OBSERVATION' in turn:
280
+ content = turn['OBSERVATION']
281
+ chat.append_message(chat.role_observation, content)
282
+ if 'ASSISTANT' in turn:
283
+ content = turn['ASSISTANT']
284
+ chat.append_message(chat.role_assistant, content)
285
+
286
+ return chat
287
+
288
+ @classmethod
289
+ def from_pack(
290
+ cls,
291
+ packs: Dict[str, List[str]],
292
+ name: str,
293
+ prompt_style: Optional[PromptStyle] = None,
294
+ ) -> list:
295
+ '''根据 pack 数据创建 Chat 对象.
296
+
297
+ Params:
298
+ packs: `dict`, pack 样本数据
299
+ {
300
+ 'inputs': ['xx', 'xx'],
301
+ 'outputs': ['xx', 'xx'],
302
+ }
303
+
304
+ name: `str`, 符号系统名称
305
+ prompt_style: `Optional[PromptStyle]`, 指定 prompt 风格, 默认使用和 name 一致的风格
306
+ '''
307
+ chat = cls(name=name, prompt_style=prompt_style)
308
+ packs = cls._format_packs(packs)
309
+
310
+ sys_pattern = re.compile(chat.system_template.format(r'(.*?)'), re.DOTALL)
311
+ turn_pattern = re.compile(chat.turn_start.format(r'(\d+)'), re.DOTALL)
312
+ human_pattern = re.compile(chat.role_template.format(chat.role_human).strip(), re.DOTALL)
313
+ observe_pattern = re.compile(chat.role_template.format(chat.role_observation).strip(), re.DOTALL)
314
+ assistant_pattern = re.compile(chat.role_template.format(chat.role_assistant).strip(), re.DOTALL)
315
+
316
+ chats = []
317
+ for input, output in zip(packs['input'], packs['output']):
318
+ # system message
319
+ sys_match = sys_pattern.search(input)
320
+ if sys_match and sys_match.group(0):
321
+ # system 指令只在首轮, 新增 chat 对象
322
+ if len(chat.messages) > 0:
323
+ chats.append(chat)
324
+ chat = cls(name=name, prompt_style=prompt_style)
325
+
326
+ input = input[sys_match.end() :]
327
+ chat.system_message = sys_match.group(1)
328
+
329
+ # turn start
330
+ turn_match = turn_pattern.search(input)
331
+ if turn_match and turn_match.group(0):
332
+ # 当出现下一个轮次开始信息, 新增 chat 对象
333
+ if name in ['antglm', 'antglm_raw', 'chatglm2']:
334
+ round_start = 1
335
+ else:
336
+ round_start = 0
337
+
338
+ if all(
339
+ [
340
+ len(turn_match.groups()) > 0,
341
+ int(turn_match.group(1)) == round_start,
342
+ len(chat.messages) > 0,
343
+ ]
344
+ ):
345
+ chats.append(chat)
346
+ chat = cls(name=name, prompt_style=prompt_style)
347
+
348
+ input = input[turn_match.end() :]
349
+
350
+ human_iter = human_pattern.finditer(input)
351
+ observe_iter = observe_pattern.finditer(input)
352
+ assistant_iter = assistant_pattern.finditer(input)
353
+ human_match = next(human_iter, None)
354
+ observe_match = next(observe_iter, None)
355
+ assistant_match = next(assistant_iter, None)
356
+
357
+ if not human_match and not observe_match:
358
+ # 无 role format
359
+ chat.append_message(chat.role_human, input)
360
+
361
+ while human_match or observe_match:
362
+ next_human_match = next(human_iter, None)
363
+ next_observe_match = next(observe_iter, None)
364
+ input = cls._append_human_observation(
365
+ chat,
366
+ input,
367
+ human_match=human_match,
368
+ next_human_match=next_human_match,
369
+ observe_match=observe_match,
370
+ next_observe_match=next_observe_match,
371
+ assistant_match=assistant_match,
372
+ )
373
+
374
+ human_match = next_human_match
375
+ observe_match = next_observe_match
376
+ next_human_match = next(human_iter, None)
377
+ next_observe_match = next(observe_iter, None)
378
+
379
+ if output:
380
+ chat.append_message(chat.role_assistant, output)
381
+
382
+ if chat.messages:
383
+ chats.append(chat)
384
+
385
+ return chats
386
+
387
+ @classmethod
388
+ def _append_human_observation(
389
+ cls,
390
+ chat,
391
+ input: str,
392
+ human_match: Optional[re.Match] = None,
393
+ next_human_match: Optional[re.Match] = None,
394
+ observe_match: Optional[re.Match] = None,
395
+ next_observe_match: Optional[re.Match] = None,
396
+ assistant_match: Optional[re.Match] = None,
397
+ ) -> str:
398
+ '''给 chat 对象增加 human/observation message.'''
399
+ if observe_match:
400
+ # observation 在 human 之后
401
+ if observe_match.span()[0] > observe_match.span()[0]:
402
+ human_str = input[observe_match.span()[1] : observe_match.span()[0]]
403
+ observe_str = input[observe_match.span()[1] : assistant_match.span()[0]]
404
+ chat.append_message(chat.role_human, human_str.strip())
405
+ input_end = observe_match.span()[1]
406
+ if observe_match.span()[0] < next_human_match.span()[0]:
407
+ chat.append_message(chat.role_observation, observe_str.strip())
408
+ input_end = assistant_match.span()[1]
409
+ else:
410
+ # observation 在 human 之前
411
+ human_str = input[observe_match.span()[1] : assistant_match.span()[0]]
412
+ observe_str = input[observe_match.span()[1] : observe_match.span()[0]]
413
+ chat.append_message(chat.role_observation, observe_str.strip())
414
+ input_end = observe_match.span()[1]
415
+ if observe_match.span()[0] < next_observe_match.span()[0]:
416
+ chat.append_message(chat.role_human, human_str.strip())
417
+ input_end = assistant_match.span()[1]
418
+ else:
419
+ if assistant_match:
420
+ human_str = input[human_match.span()[1] : assistant_match.span()[0]]
421
+ input_end = assistant_match.span()[1]
422
+ else:
423
+ human_str = input[human_match.span()[1] :]
424
+ input_end = len(input)
425
+ chat.append_message(chat.role_human, human_str.strip())
426
+
427
+ return input[input_end:]
428
+
429
+ @classmethod
430
+ def from_inout(
431
+ cls,
432
+ sample: Dict[str, str],
433
+ name: str,
434
+ prompt_style: Optional[PromptStyle] = None,
435
+ ):
436
+ '''根据单样本创建一个 Chat 对象.
437
+
438
+ Params:
439
+ sample: `Dict[str, str]`, input/output 数据样本
440
+ {
441
+ "input": "xxx",
442
+ "output": "xxx",
443
+ }
444
+
445
+ name: `str`, 符号系统名称
446
+ prompt_style: `Optional[PromptStyle]`, 指定 prompt 风格, 默认使用和 name 一致的风格
447
+ '''
448
+ chat = cls(name=name, prompt_style=prompt_style)
449
+ input = sample['input']
450
+ output = sample['output']
451
+
452
+ sys_pattern = re.compile(chat.system_template.format(r'(.*?)'), re.DOTALL)
453
+ turn_pattern = re.compile(chat.turn_start.format(r'(\d+)'), re.DOTALL)
454
+ human_pattern = re.compile(chat.role_template.format(chat.role_human).strip(), re.DOTALL)
455
+ observe_pattern = re.compile(chat.role_template.format(chat.role_observation).strip(), re.DOTALL)
456
+ assistant_pattern = re.compile(chat.role_template.format(chat.role_assistant).strip(), re.DOTALL)
457
+
458
+ # 去除轮次信息
459
+ input = turn_pattern.sub('', input)
460
+
461
+ # system message search
462
+ sys_match = sys_pattern.search(input)
463
+ if sys_match and sys_match.group(0):
464
+ input = input[sys_match.end() :]
465
+ chat.system_message = sys_match.group(1)
466
+
467
+ human_iter = human_pattern.finditer(input)
468
+ observe_iter = observe_pattern.finditer(input)
469
+ assistant_iter = assistant_pattern.finditer(input)
470
+ human_match = next(human_iter, None)
471
+ observe_match = next(observe_iter, None)
472
+ assistant_match = next(assistant_iter, None)
473
+ next_human_match = next(human_iter, None)
474
+ next_observe_match = next(observe_iter, None)
475
+
476
+ while any(
477
+ [
478
+ human_match,
479
+ observe_match,
480
+ assistant_match,
481
+ ]
482
+ ):
483
+
484
+ # human/observation 先后顺序可能不一样, 并且有可能有多个
485
+ # 判断 assitant 之前是否还有 human/observation
486
+ while any(
487
+ [
488
+ human_match and human_match.span()[0] < assistant_match.span()[0],
489
+ observe_match and observe_match.span()[0] < assistant_match.span()[0],
490
+ next_human_match and next_human_match.span()[0] < assistant_match.span()[0],
491
+ next_observe_match and next_observe_match.span()[0] < assistant_match.span()[0],
492
+ ]
493
+ ):
494
+ if not input:
495
+ break
496
+
497
+ cls._append_human_observation(
498
+ chat,
499
+ input,
500
+ human_match=human_match,
501
+ next_human_match=next_human_match,
502
+ observe_match=observe_match,
503
+ next_observe_match=next_observe_match,
504
+ assistant_match=assistant_match,
505
+ )
506
+
507
+ human_match = next_human_match
508
+ observe_match = next_observe_match
509
+ next_human_match = next(human_iter, None)
510
+ next_observe_match = next(observe_iter, None)
511
+
512
+ # assistant message
513
+ if assistant_match and assistant_match.span():
514
+ if observe_match:
515
+ if observe_match.span() and observe_match.span()[0] < human_match.span()[0]:
516
+ assistant_str = input[assistant_match.span()[1] : observe_match.span()[0]]
517
+ elif human_match:
518
+ if human_match.span():
519
+ assistant_str = input[assistant_match.span()[1] : human_match.span()[0]]
520
+ else:
521
+ assistant_str = input[assistant_match.span()[1] :]
522
+
523
+ if assistant_str:
524
+ chat.append_message(chat.role_assistant, assistant_str)
525
+
526
+ assistant_match = next(assistant_iter, None)
527
+
528
+ if output:
529
+ chat.append_message(chat.role_assistant, output)
530
+
531
+ return chat
532
+
533
+ def __hash__(self):
534
+ '''数据对象的 hash 函数.'''
535
+ return hash(self.id)
536
+
537
+ def __post_init__(self):
538
+ '''对象初始化后的处理, 处理包括:
539
+ - 根据数据对象名称, 支持转成其他开源数据对象的基本信息
540
+ '''
541
+ self.id = str(uuid.uuid4())
542
+ if not self.messages:
543
+ self.messages = []
544
+
545
+ if not self.name and not self.prompt_style:
546
+ logger.error('构造 Chat 对象至少包含以下一个入参: `name/prompt_style`.\n\n' '`name` 支持以下 format 名称:')
547
+ logger.error('\n'.join([f'{k}: {v}' for k, v in self.support_names.items()]))
548
+ logger.error('\n`prompt_style` 参考 antllm.data.chat_format.PromptStyle')
549
+ raise ValueError
550
+
551
+ if self.name == 'antglm':
552
+ # 默认 antglm 使用原始 antglm_raw - 第1轮\n用户: xx\n机器人: xx\n
553
+ self.name = 'antglm_raw'
554
+
555
+ if not self.name and self.prompt_style == PromptStyle.ANTGLM_CHAT:
556
+ logger.info(
557
+ 'Chat 对象入参没有 `name`, 默认使用 `ANTGLM_CHAT`, format:\n'
558
+ f'role_human: {self.role_human}\n'
559
+ f'role_assistant: {self.role_assistant}\n'
560
+ f'role_observation: {self.role_observation}\n'
561
+ f'role_template: {self.role_template}\n'
562
+ f'turn_start: {self.turn_start}\n'
563
+ f'human_end: {self.human_end}\n'
564
+ f'assistant_start: {self.assistant_start}\n'
565
+ f'assistant_end: {self.assistant_end}\n'
566
+ f'assistant_end_ids: {self.assistant_end_ids}\n'
567
+ f'general_role_end: {self.general_role_end}\n'
568
+ f'tool_template: {self.tool_template}\n'
569
+ f'code_template: {self.code_template}\n'
570
+ f'arithemetic_templte: {self.arithemetic_templte}\n'
571
+ f'image_template: {self.image_template}\n'
572
+ f'\n入参 `name` 支持: ``'
573
+ )
574
+ return
575
+
576
+ if self.name == 'antglm_raw' or self.prompt_style == PromptStyle.ANTGLM_RAW:
577
+ self.prompt_style = PromptStyle.ANTGLM_RAW
578
+ self.role_template = '{}'
579
+ self.role_human = '用户: '
580
+ self.role_assistant = '机器人: '
581
+ self.turn_start = '第{}轮\n'
582
+ self.general_role_end = '\n'
583
+
584
+ if self.name in ['chatglm1', 'chatglm2'] or self.prompt_style == PromptStyle.CHATGLM:
585
+ self.prompt_style = PromptStyle.CHATGLM
586
+ self.role_template = '{}'
587
+ self.role_human = '问:'
588
+ self.role_assistant = '答:'
589
+ self.turn_start = '[Round {}]\n'
590
+ if self.name == 'chatglm1':
591
+ self.general_role_end = '\n'
592
+ else:
593
+ self.general_role_end = '\n\n'
594
+
595
+ elif self.name == 'chatglm3' or self.prompt_style == PromptStyle.CHATGLM3:
596
+ self.prompt_style = PromptStyle.CHATGLM3
597
+ self.system_template = '<|system|>\n {}'
598
+ self.role_human = '<|user|>\n '
599
+ self.role_assistant = '<|assistant|>\n '
600
+ self.role_template = '{}'
601
+
602
+ elif self.name == 'llama2' or self.prompt_style == PromptStyle.LLAMA2:
603
+ self.prompt_style = PromptStyle.LLAMA2
604
+ self.role_template = '{}'
605
+ self.system_template = '[INST] <<SYS>>\n{}\n<</SYS>>\n\n'
606
+ self.role_human = '[INST] '
607
+ self.role_assistant = '[/INST] '
608
+ self.human_end = ' '
609
+ self.assistant_end = ' </s><s>'
610
+
611
+ elif self.name == 'qwen':
612
+ self.prompt_style = PromptStyle.CHATML
613
+ self.role_template = '{}'
614
+ self.system_template = '<|im_start|>system\n{}'
615
+ if not self.system_message:
616
+ self.system_message = 'You are a helpful assistant.'
617
+ self.role_human = '<|im_start|>user\n'
618
+ self.role_assistant = '<|im_start|>assistant\n'
619
+ self.general_role_end = '<|im_end|>\n'
620
+
621
+ elif self.name == 'baichuan':
622
+ self.prompt_style = PromptStyle.BAICHUAN2
623
+ self.role_template = '{}'
624
+ self.system_template = '{}'
625
+ self.role_human = '<token_id-195>'
626
+ self.role_assistant = '<token_id-196>'
627
+
628
+ if not self.system_template:
629
+ self.system_template = '{}'
630
+
631
+ def readable_messages(self) -> str:
632
+ '''将 messages 输出为人类可读的字符串, 方便分析数据.'''
633
+ pass
634
+
635
+ @property
636
+ def prompt_str(self) -> str:
637
+ '''将 Chat 对象转成 prompt str, 合并 human/assitant 输出为 format 字符串.'''
638
+ return f'{self.prompt_inout["input"]}{self.prompt_inout["output"]}'
639
+
640
+ @classmethod
641
+ def _format_packs(cls, packs: Dict[str, List[str]]) -> Dict[str, List[str]]:
642
+ '''格式化 pack 样本, 输出相同 pack inputs, outputs 个数.'''
643
+ _packs = copy.deepcopy(packs)
644
+ if len(_packs['input']) - 1 == len(_packs['output']):
645
+ _packs['output'].append('')
646
+
647
+ if len(_packs['input']) != len(_packs['output']):
648
+ print(packs)
649
+ raise ValueError(
650
+ '输入 input 和 output 数量不匹配, '
651
+ f'input num: {len(packs["input"])}, '
652
+ f'output num: {len(packs["output"])}'
653
+ )
654
+
655
+ return _packs
656
+
657
+ @property
658
+ def prompt_inout(self) -> Dict[str, str]:
659
+ '''将 Chat 对象转成 input prompt, output prompt 字符串.
660
+
661
+ Returns:
662
+ `Dict[str, str]`, 示例:
663
+ {
664
+ "input": "<role>SYSTEM</role>xxxx<role>HUMAN</role>你好<role>ASSISTANT</role>你好,有什么可以帮您?<role>ASSISTANT</role>", # noqa
665
+ "output": "你好,有什么可以帮您?"
666
+ }
667
+ '''
668
+ packs = self._format_packs(self.prompt_pack)
669
+
670
+ # 兼容逻辑
671
+ if self.prompt_style == PromptStyle.ANTGLM_RAW:
672
+ packs['input'] = [f'{item} ' for item in packs['input']]
673
+
674
+ prompt_input = ''.join([f'{x}{y}' for x, y in zip(packs['input'][:-1], packs['output'][:-1])])
675
+ prompt_input += packs['input'][-1]
676
+ prompt_output = packs['output'][-1]
677
+
678
+ # 兼容逻辑
679
+ if self.prompt_style == PromptStyle.ANTGLM_RAW:
680
+ prompt_input = prompt_input.strip()
681
+
682
+ return {
683
+ 'input': prompt_input,
684
+ 'output': prompt_output,
685
+ }
686
+
687
+ @property
688
+ def prompt_pack(self) -> Dict[str, List[str]]:
689
+ '''将数据对象转成 pack input prompt, output prompt 字符串列表.:
690
+
691
+ Returns:
692
+ `Dict[str, List[str]]`, 示例:
693
+
694
+ {
695
+ "input": [
696
+ "<role>SYSTEM</role>xxxx<role>HUMAN</role>你好<role>ASSISTANT</role>",
697
+ "<role>HUMAN</role>讲个笑话<role>ASSISTANT</role>",
698
+ "<role>OBSERVATION</role>{\"weather\": \"晴\"}<role>ASSISTANT</role>"
699
+ ],
700
+ "output": [
701
+ "你好,有什么可以帮您?",
702
+ "笑话 1",
703
+ "今天天气 xxx"
704
+ ]
705
+ }
706
+
707
+ '''
708
+ inputs = []
709
+ outputs = []
710
+
711
+ # 最开始 system 构造
712
+ system_prompt = ''
713
+ if self.system_message:
714
+ system_prompt = self.system_template.format(self.system_message)
715
+
716
+ if system_prompt:
717
+ ret = system_prompt + self.general_role_end
718
+ else:
719
+ ret = ''
720
+
721
+ # 有些 prompt style 单轮指令没有 format
722
+ if self.prompt_style in [
723
+ PromptStyle.ANTGLM_RAW,
724
+ PromptStyle.ANTGLM_ONLY_MULTITURN_CHAT,
725
+ ]:
726
+ if len(self.messages) <= 2:
727
+ output = ''
728
+ for role, message in self.messages:
729
+ if role == self.role_assistant:
730
+ output = message
731
+ else:
732
+ input = ret + message
733
+ return {
734
+ 'input': [input],
735
+ 'output': [output],
736
+ }
737
+
738
+ # 多轮对话
739
+ if self.name in ['antglm_raw', 'chatglm2']:
740
+ round_start = 1
741
+ else:
742
+ round_start = 0
743
+
744
+ for i, (role, message) in enumerate(self.messages):
745
+ # 轮次信息
746
+ if self.name in ['antglm_raw', 'chatglm1', 'chatglm2']:
747
+ if i % 2 == 0:
748
+ ret += self.turn_start.format(i // 2 + round_start)
749
+
750
+ # 角色 + 内容
751
+ role_end = self.general_role_end
752
+ if role == self.role_assistant and self.assistant_end:
753
+ role_end = self.assistant_end
754
+ elif self.human_end:
755
+ role_end = self.human_end
756
+
757
+ ret += self.role_template.format(role) + message + role_end
758
+
759
+ if role == self.role_assistant:
760
+ # output 只保留实际 assistant 内容
761
+ if not message:
762
+ outputs.append('')
763
+ else:
764
+ outputs.append(message + role_end)
765
+ # input 需要连接 assistant role
766
+ inputs[-1] += ret[: -len(message + role_end)]
767
+ elif all(
768
+ [
769
+ role == self.role_observation,
770
+ len(self.messages) > 1,
771
+ self.messages[i - 1][0] != self.role_assistant,
772
+ ]
773
+ ):
774
+ # observation 之前不是 assistant, 需要将 observation 和上一个 input 连接一起
775
+ continue
776
+ else:
777
+ inputs.append(ret)
778
+ ret = ''
779
+
780
+ # 最后一轮不是机器人回复, 需要拼接机器人 role, 用于模型生成
781
+ if i == len(self.messages) - 1 and role != self.role_assistant:
782
+ inputs[-1] += self.role_template.format(self.role_assistant).strip()
783
+
784
+ # 兼容逻辑, 去除 inputs 最后空格符号
785
+ if self.prompt_style == PromptStyle.ANTGLM_RAW:
786
+ inputs = [item.strip() for item in inputs]
787
+
788
+ return {
789
+ 'input': inputs,
790
+ 'output': outputs,
791
+ }
792
+
793
+ @property
794
+ def turns_num(self) -> int:
795
+ '''和机器人的交互轮数, 以用户输出多少次为轮数个数.'''
796
+ return sum([1 if msg[0] == self.role_human else 0 for msg in self.messages])
797
+
798
+ def to_json(self) -> dict:
799
+ '''输出 chat json dict 格式, 包含不同角色和机器人交互的每轮信息.
800
+
801
+ Returns
802
+ `List[dict]`, {
803
+ "id": "xx",
804
+ "messages": [
805
+ {"role": "HUMAN", "content": "xxx"}
806
+ ]
807
+ "turns": [
808
+ {"HUMAN": "xx", "OBSERVATION": "xx", "ASSISTANT": "xx"}
809
+ ]
810
+ }
811
+ '''
812
+ turns = []
813
+ messages = []
814
+ turn = {}
815
+ for msg in self.messages:
816
+ if msg[0] == self.role_assistant:
817
+ messages.append({'role': 'ASSISTANT', 'content': msg[1]})
818
+ turn['ASSISTANT'] = msg[1]
819
+ turns.append(turn)
820
+ turn = {}
821
+
822
+ if msg[0] == self.role_human:
823
+ messages.append({'role': 'HUMAN', 'content': msg[1]})
824
+ turn['HUMAN'] = msg[1]
825
+
826
+ if msg[0] == self.role_observation:
827
+ messages.append({'role': 'OBSERVATION', 'content': msg[1]})
828
+ turn['OBSERVATION'] = msg[1]
829
+
830
+ if self.messages[-1][0] == self.role_human:
831
+ messages.append({'role': 'ASSISTANT', 'content': ''})
832
+ turn['ASSISTANT'] = ''
833
+ turns.append(turn)
834
+
835
+ result = self.origin_json or {}
836
+ result.update(
837
+ {
838
+ 'id': self.id,
839
+ 'name': self.name,
840
+ 'source': self.source,
841
+ 'lang': self.lang,
842
+ 'topic': self.topic,
843
+ 'system_template': self.system_template,
844
+ 'system_message': self.system_message,
845
+ 'turns': turns,
846
+ 'messages': messages,
847
+ }
848
+ )
849
+
850
+ return result
851
+
852
+ def set_system_message(self, system_message: str):
853
+ '''Set the system message.'''
854
+ self.system_message = system_message
855
+
856
+ def append_message(self, role: str, message: str):
857
+ '''Append a new message.'''
858
+ if not message:
859
+ message = ''
860
+ self.messages.append([role, message])
861
+
862
+ def to_openai_api_messages(self) -> List[dict]:
863
+ '''Convert the conversation to OpenAI chat completion format.'''
864
+ ret = [{'role': 'system', 'content': self.system_message}]
865
+
866
+ for i, (_, msg) in enumerate(self.messages[self.offset :]):
867
+ if i % 2 == 0:
868
+ ret.append({'role': 'user', 'content': msg})
869
+ else:
870
+ if msg is not None:
871
+ ret.append({'role': 'assistant', 'content': msg})
872
+ return ret
873
+
874
+ def copy(self):
875
+ return copy.deepcopy(self)
chat_template.jinja ADDED
@@ -0,0 +1 @@
 
 
1
+ {% for message in messages %}{% set role = message['role'] | lower %}{% if role == 'user' %}{% set role = 'HUMAN' %}{% endif %}{% set role = role | upper %}{{ '<role>' + role + '</role>' + message['content'] }}{% endfor %}{% if add_generation_prompt %}{{ '<role>ASSISTANT</role><think>' }}{% endif %}
config.json ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BailingMoeLinearV2ForCausalLM"
4
+ ],
5
+ "attention_dropout": 0.0,
6
+ "auto_map": {
7
+ "AutoConfig": "configuration_bailing_moe_linear_v2.BailingMoeLinearV2Config",
8
+ "AutoModel": "modeling_bailing_moe_linear_v2.BailingMoeLinearV2Model",
9
+ "AutoModelForCausalLM": "modeling_bailing_moe_linear_v2.BailingMoeLinearV2ForCausalLM"
10
+ },
11
+ "embedding_dropout": 0.0,
12
+ "eos_token_id": 156892,
13
+ "first_k_dense_replace": 1,
14
+ "group_norm_size": 4,
15
+ "head_dim": 128,
16
+ "hidden_act": "silu",
17
+ "hidden_size": 4096,
18
+ "initializer_range": 0.02,
19
+ "intermediate_size": 9216,
20
+ "layer_group_size": 8,
21
+ "linear_silu": false,
22
+ "max_position_embeddings": 131072,
23
+ "max_window_layers": 20,
24
+ "model_type": "bailing_moe_linear",
25
+ "moe_intermediate_size": 1024,
26
+ "moe_router_enable_expert_bias": true,
27
+ "moe_shared_expert_intermediate_size": 1024,
28
+ "mtp_loss_scaling_factor": 0,
29
+ "n_group": 8,
30
+ "norm_topk_prob": true,
31
+ "num_attention_heads": 32,
32
+ "num_experts": 256,
33
+ "num_experts_per_tok": 8,
34
+ "num_hidden_layers": 32,
35
+ "num_key_value_heads": 4,
36
+ "num_nextn_predict_layers": 0,
37
+ "num_shared_experts": 1,
38
+ "output_dropout": 0.0,
39
+ "output_router_logits": false,
40
+ "pad_token_id": 156892,
41
+ "partial_rotary_factor": 0.5,
42
+ "quantization_config": {
43
+ "config_groups": {
44
+ "group_0": {
45
+ "format": "pack-quantized",
46
+ "input_activations": null,
47
+ "output_activations": null,
48
+ "targets": [
49
+ "Linear"
50
+ ],
51
+ "weights": {
52
+ "actorder": "weight",
53
+ "block_structure": null,
54
+ "dynamic": false,
55
+ "group_size": 128,
56
+ "num_bits": 4,
57
+ "observer": "minmax",
58
+ "observer_kwargs": {},
59
+ "strategy": "group",
60
+ "symmetric": true,
61
+ "type": "int"
62
+ }
63
+ }
64
+ },
65
+ "format": "pack-quantized",
66
+ "global_compression_ratio": null,
67
+ "ignore": [
68
+ "model.layers.0.attention.dense",
69
+ "model.layers.1.attention.dense",
70
+ "model.layers.2.attention.dense",
71
+ "model.layers.3.attention.dense",
72
+ "model.layers.4.attention.dense",
73
+ "model.layers.5.attention.dense",
74
+ "model.layers.6.attention.dense",
75
+ "model.layers.7.attention.dense",
76
+ "model.layers.8.attention.dense",
77
+ "model.layers.9.attention.dense",
78
+ "model.layers.10.attention.dense",
79
+ "model.layers.11.attention.dense",
80
+ "model.layers.12.attention.dense",
81
+ "model.layers.13.attention.dense",
82
+ "model.layers.14.attention.dense",
83
+ "model.layers.15.attention.dense",
84
+ "model.layers.16.attention.dense",
85
+ "model.layers.17.attention.dense",
86
+ "model.layers.18.attention.dense",
87
+ "model.layers.19.attention.dense",
88
+ "model.layers.20.attention.dense",
89
+ "model.layers.21.attention.dense",
90
+ "model.layers.22.attention.dense",
91
+ "model.layers.23.attention.dense",
92
+ "model.layers.24.attention.dense",
93
+ "model.layers.25.attention.dense",
94
+ "model.layers.26.attention.dense",
95
+ "model.layers.27.attention.dense",
96
+ "model.layers.28.attention.dense",
97
+ "model.layers.29.attention.dense",
98
+ "model.layers.30.attention.dense",
99
+ "model.layers.31.attention.dense",
100
+ "lm_head"
101
+ ],
102
+ "kv_cache_scheme": null,
103
+ "quant_method": "compressed-tensors",
104
+ "quantization_status": "compressed",
105
+ "sparsity_config": {},
106
+ "transform_config": {},
107
+ "version": "0.11.0"
108
+ },
109
+ "rms_norm_eps": 1e-06,
110
+ "rope_scaling": null,
111
+ "rope_theta": 600000,
112
+ "routed_scaling_factor": 2.5,
113
+ "router_dtype": "fp32",
114
+ "score_function": "sigmoid",
115
+ "tie_word_embeddings": false,
116
+ "topk_group": 4,
117
+ "torch_dtype": "bfloat16",
118
+ "transformers_version": "4.55.2",
119
+ "use_bias": false,
120
+ "use_cache": true,
121
+ "use_qk_norm": true,
122
+ "use_qkv_bias": false,
123
+ "use_rmsnorm": true,
124
+ "vocab_size": 157184
125
+ }
configuration_bailing_moe_linear_v2.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Bailing MoE V2 model configuration"""
2
+
3
+ from transformers.configuration_utils import PretrainedConfig
4
+
5
+
6
+ class BailingMoeLinearV2Config(PretrainedConfig):
7
+ model_type = "bailing_moe_linear"
8
+
9
+ def __init__(
10
+ self,
11
+ vocab_size=157184,
12
+ hidden_size=2048,
13
+ intermediate_size=5120,
14
+ num_hidden_layers=20,
15
+ num_attention_heads=16,
16
+ num_key_value_heads=4,
17
+ hidden_act="silu",
18
+ use_qkv_bias=False, # bailing only
19
+ use_bias=False, # bailing only
20
+ rms_norm_eps=1e-06,
21
+ tie_word_embeddings=False, # PretrainedConfig key, here change default value.
22
+ embedding_dropout=0.0,
23
+ attention_dropout=0.0,
24
+ output_dropout=0.0,
25
+ initializer_range=0.02,
26
+ max_position_embeddings=32768,
27
+ rope_theta=600000.0,
28
+ use_cache=True,
29
+ max_window_layers=20,
30
+ rope_scaling=None,
31
+ pad_token_id=156892,
32
+ eos_token_id=156892,
33
+ num_experts=256,
34
+ num_shared_experts=1,
35
+ num_experts_per_tok=8,
36
+ n_group=8,
37
+ topk_group=4,
38
+ moe_intermediate_size=512,
39
+ first_k_dense_replace=1,
40
+ head_dim=128,
41
+ output_router_logits=False,
42
+ use_qk_norm=True,
43
+ num_nextn_predict_layers=0,
44
+ mtp_loss_scaling_factor=0,
45
+ moe_router_enable_expert_bias=True,
46
+ routed_scaling_factor=1.0,
47
+ layer_group_size=1,
48
+ group_norm_size=1,
49
+ linear_silu=False,
50
+ **kwargs,
51
+ ):
52
+ self.num_hidden_layers = num_hidden_layers
53
+ self.vocab_size = vocab_size
54
+ self.hidden_size = hidden_size
55
+ self.intermediate_size = intermediate_size
56
+ self.num_attention_heads = num_attention_heads
57
+ self.num_key_value_heads = num_key_value_heads
58
+ self.hidden_act = hidden_act
59
+ self.use_qkv_bias = use_qkv_bias
60
+ self.use_bias = use_bias
61
+ self.rms_norm_eps = rms_norm_eps
62
+ self.embedding_dropout = embedding_dropout
63
+ self.attention_dropout = attention_dropout
64
+ self.output_dropout = output_dropout
65
+ self.num_nextn_predict_layers = num_nextn_predict_layers
66
+ self.mtp_loss_scaling_factor = mtp_loss_scaling_factor
67
+ self.initializer_range = initializer_range
68
+ self.max_position_embeddings = max_position_embeddings
69
+ self.rope_theta = rope_theta
70
+ self.use_cache = use_cache
71
+ self.max_window_layers = max_window_layers
72
+ self.head_dim = head_dim or self.hidden_size // self.num_attention_heads
73
+ self.rope_scaling = rope_scaling
74
+ self.use_qk_norm = use_qk_norm
75
+ self.moe_router_enable_expert_bias = moe_router_enable_expert_bias
76
+ self.routed_scaling_factor = routed_scaling_factor
77
+
78
+ # MoE configs
79
+ self.num_experts = num_experts
80
+ self.num_shared_experts = num_shared_experts
81
+ self.num_experts_per_tok = num_experts_per_tok
82
+ self.n_group = n_group
83
+ self.topk_group = topk_group
84
+ self.moe_intermediate_size = moe_intermediate_size
85
+ self.first_k_dense_replace = first_k_dense_replace
86
+ self.output_router_logits = output_router_logits
87
+
88
+ # Linear configs
89
+ self.layer_group_size = layer_group_size
90
+ self.group_norm_size = group_norm_size
91
+ self.linear_silu = linear_silu
92
+
93
+ super().__init__(pad_token_id=pad_token_id, eos_token_id=eos_token_id, tie_word_embeddings=tie_word_embeddings, **kwargs)
generation_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "eos_token_id": 156892,
4
+ "pad_token_id": 156892,
5
+ "transformers_version": "4.55.2"
6
+ }
model-00001-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:75aebe06c673bf7894bfc82148f6c06bc393ab72e25ad198979fb2ca3edb537d
3
+ size 4999517128
model-00002-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6d7e77f7301952f14afd5c409e61dc440a902dbee8f616684d04faacdc369a10
3
+ size 4998826464
model-00003-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:03ac7cbfdf56d9863486fd31deaa5dd23de27d04f55a2fc266654a41d8cd0746
3
+ size 4999885432
model-00004-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8160f67efca711502555a0e8a7c1b644f83ced76a9f86f9cc7563de38515b2bd
3
+ size 4999893824
model-00005-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9a503208f4ca5df698606db9ce8111591d284977bfa05bac22b7dbd0f876f407
3
+ size 4999896528
model-00006-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d178d4c06764adec8c0a241fcdae07d76ddd05d008193713523355214fea72c6
3
+ size 4999892128
model-00007-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bcbd4e19c993442ccdc6e12eb003a6becaac46686152c82935a70cacf732439b
3
+ size 4999896672
model-00008-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d65e4af7f90ad87ffa052a9ca9a32a970bab396a5f8bdabb9adbd33b71d32385
3
+ size 4999892384
model-00009-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4aaacbfbc3de52ed319bb21dcc84852412ff5663595fb3791c0325fb48beb9ab
3
+ size 4999896792
model-00010-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:14dc330663c849b185fbfb05dbcc8c7eddbf0c8abaaacd0c3bc9814042a75482
3
+ size 4998833392
model-00011-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:19717ed540bc6368259358d4458e709bf164fed71a17390b8a309b5533e32e2a
3
+ size 4999892176
model-00012-of-00012.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0a1413da426840e35ea7a57ee01da102db665b30882e31a963c0770211eb93f1
3
+ size 1488776768
model.safetensors.index.json ADDED
The diff for this file is too large to render. See raw diff
 
modeling_bailing_moe_linear_v2.py ADDED
@@ -0,0 +1,1758 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2025 Antgroup and The HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
5
+ # and OPT implementations in this library. It has been modified from its
6
+ # original forms to accommodate minor architectural differences compared
7
+ # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ """PyTorch BailingMoE model."""
21
+
22
+ import math
23
+ import warnings
24
+ from typing import List, Optional, Tuple, Union
25
+
26
+ import torch
27
+ import torch.nn.functional as F
28
+ from torch import nn
29
+
30
+ from transformers.activations import ACT2FN
31
+ from transformers.cache_utils import Cache, DynamicCache
32
+ from transformers.modeling_attn_mask_utils import (
33
+ AttentionMaskConverter,
34
+ _prepare_4d_attention_mask,
35
+ _prepare_4d_causal_attention_mask,
36
+ _prepare_4d_causal_attention_mask_for_sdpa,
37
+ )
38
+ from transformers.modeling_outputs import MoeModelOutputWithPast
39
+ from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update
40
+ from transformers.modeling_utils import PreTrainedModel
41
+ from transformers.pytorch_utils import ALL_LAYERNORM_LAYERS, is_torch_greater_or_equal_than_1_13
42
+ from transformers.utils import (
43
+ add_start_docstrings,
44
+ add_start_docstrings_to_model_forward,
45
+ is_flash_attn_2_available,
46
+ is_flash_attn_greater_or_equal_2_10,
47
+ logging,
48
+ replace_return_docstrings,
49
+ )
50
+ from transformers.utils.import_utils import is_torch_fx_available
51
+ from .configuration_bailing_moe_linear_v2 import BailingMoeLinearV2Config
52
+ from transformers.generation.utils import GenerationMixin
53
+ from dataclasses import dataclass
54
+ from transformers.utils import ModelOutput
55
+
56
+
57
+ if is_flash_attn_2_available():
58
+ from flash_attn import flash_attn_func, flash_attn_varlen_func
59
+ from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
60
+
61
+ from fla.ops.simple_gla.fused_recurrent import fused_recurrent_simple_gla
62
+ from fla.ops.simple_gla.chunk import chunk_simple_gla
63
+
64
+
65
+ # This makes `_prepare_4d_causal_attention_mask` a leaf function in the FX graph.
66
+ # It means that the function will not be traced through and simply appear as a node in the graph.
67
+ if is_torch_fx_available():
68
+ if not is_torch_greater_or_equal_than_1_13:
69
+ import torch.fx
70
+
71
+ _prepare_4d_causal_attention_mask = torch.fx.wrap(_prepare_4d_causal_attention_mask)
72
+
73
+
74
+ logger = logging.get_logger(__name__)
75
+
76
+ _CONFIG_FOR_DOC = "BailingMoeLinearV2Config"
77
+
78
+
79
+ def roll_tensor(tensor, shifts=-1, dims=-1, fill_value=0):
80
+ """Roll the tensor input along the given dimension(s).
81
+ Inserted elements are set to be 0.0.
82
+ """
83
+ rolled_tensor = torch.roll(tensor, shifts=shifts, dims=dims)
84
+ rolled_tensor.select(dims, shifts).fill_(fill_value)
85
+ return rolled_tensor, rolled_tensor.sum()
86
+
87
+
88
+ @dataclass
89
+ class MoEV2CausalLMOutputWithPast(ModelOutput):
90
+ """
91
+ Base class for causal language model (or autoregressive) outputs as well as Mixture of Expert's router hidden
92
+ states terms, to train a MoE model.
93
+ Args:
94
+ loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
95
+ Language modeling loss (for next-token prediction).
96
+ logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
97
+ Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
98
+ past_key_values (`Cache`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
99
+ It is a [`~cache_utils.Cache`] instance. For more details, see our [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache).
100
+ Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see
101
+ `past_key_values` input) to speed up sequential decoding.
102
+ hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
103
+ Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
104
+ one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
105
+ Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
106
+ attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
107
+ Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
108
+ sequence_length)`.
109
+ Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
110
+ heads.
111
+ z_loss (`torch.FloatTensor`, *optional*, returned when `labels` is provided):
112
+ z_loss for the sparse modules.
113
+ aux_loss (`torch.FloatTensor`, *optional*, returned when `labels` is provided):
114
+ aux_loss for the sparse modules.
115
+ router_logits (`tuple(torch.FloatTensor)`, *optional*, returned when `output_router_logits=True` is passed or when `config.add_router_probs=True`):
116
+ Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, sequence_length, num_experts)`.
117
+ Router logits of the encoder model, useful to compute the auxiliary loss and the z_loss for the sparse
118
+ modules.
119
+ """
120
+
121
+ loss: Optional[torch.FloatTensor] = None
122
+ logits: Optional[torch.FloatTensor] = None
123
+ past_key_values: Optional[Cache] = None
124
+ hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None
125
+ attentions: Optional[tuple[torch.FloatTensor, ...]] = None
126
+ z_loss: Optional[torch.FloatTensor] = None
127
+ aux_loss: Optional[torch.FloatTensor] = None
128
+ router_logits: Optional[tuple[torch.FloatTensor]] = None
129
+ mtp_loss: Optional[torch.FloatTensor] = None
130
+ mtp_logits: Optional[tuple[torch.FloatTensor, ...]] = None
131
+
132
+
133
+ class MoeV2ModelOutputWithPast(MoeModelOutputWithPast):
134
+
135
+ def __init__(self, mtp_hidden_states=None, **kwargs):
136
+ super().__init__(**kwargs)
137
+ self.mtp_hidden_states = mtp_hidden_states
138
+
139
+
140
+ def _get_unpad_data(attention_mask):
141
+ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
142
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
143
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
144
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
145
+ return (
146
+ indices,
147
+ cu_seqlens,
148
+ max_seqlen_in_batch,
149
+ )
150
+
151
+
152
+ def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
153
+ warnings.warn(
154
+ "Calling `transformers.models.BailingMoeV2.modeling_BailingMoeV2._prepare_4d_attention_mask` is deprecated and will be removed in v4.37. Use `transformers.modeling_attn_mask_utils._prepare_4d_attention_mask"
155
+ )
156
+ return _prepare_4d_attention_mask(mask=mask, dtype=dtype, tgt_len=tgt_len)
157
+
158
+
159
+ def _make_causal_mask(
160
+ input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
161
+ ):
162
+ warnings.warn(
163
+ "Calling `transformers.models.BailingMoeV2.modeling_BailingMoeV2._make_causal_mask` is deprecated and will be removed in v4.37. Use `transformers.models.BailingMoeV2.modeling_BailingMoeV2.AttentionMaskConverter._make_causal_mask"
164
+ )
165
+ return AttentionMaskConverter._make_causal_mask(
166
+ input_ids_shape=input_ids_shape, dtype=dtype, device=device, past_key_values_length=past_key_values_length
167
+ )
168
+
169
+
170
+ class BailingMoeV2RMSNorm(nn.Module):
171
+ def __init__(self, hidden_size, eps=1e-6):
172
+ """
173
+ BailingMoeV2RMSNorm is equivalent to T5LayerNorm
174
+ """
175
+ super().__init__()
176
+ self.weight = nn.Parameter(torch.ones(hidden_size))
177
+ self.variance_epsilon = eps
178
+
179
+ def forward(self, hidden_states):
180
+ input_dtype = hidden_states.dtype
181
+ hidden_states = hidden_states.to(torch.float32)
182
+ variance = hidden_states.pow(2).mean(-1, keepdim=True)
183
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
184
+ return self.weight * hidden_states.to(input_dtype)
185
+
186
+
187
+ class BailingMoeV2GroupRMSNorm(nn.Module):
188
+ def __init__(self, hidden_size, group_norm_size, eps=1e-6):
189
+ """
190
+ BailingMoeV2RMSNorm is equivalent to T5LayerNorm
191
+ """
192
+ super().__init__()
193
+ self.weight = nn.Parameter(torch.ones(hidden_size))
194
+ self.group_norm_size = group_norm_size
195
+ assert hidden_size % group_norm_size == 0, "hidden_size must be divisible by group_norm_size"
196
+ self.variance_epsilon = eps
197
+
198
+ def forward(self, hidden_states):
199
+ input_dtype = hidden_states.dtype
200
+ input_shape = hidden_states.size()
201
+ group_input_shape = input_shape[:-1] + (self.group_norm_size, input_shape[-1] // self.group_norm_size)
202
+ hidden_states = hidden_states.view(group_input_shape)
203
+ hidden_states = hidden_states.to(torch.float32)
204
+ variance = hidden_states.pow(2).mean(-1, keepdim=True)
205
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
206
+ return self.weight * hidden_states.to(input_dtype).view(input_shape)
207
+
208
+
209
+ ALL_LAYERNORM_LAYERS.append(BailingMoeV2RMSNorm)
210
+
211
+
212
+ class BailingMoeV2RotaryEmbedding(nn.Module):
213
+ def __init__(self, config: BailingMoeLinearV2Config, device=None):
214
+ super().__init__()
215
+ # BC: "rope_type" was originally "type"
216
+ if hasattr(config, "rope_scaling") and config.rope_scaling is not None:
217
+ self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type"))
218
+ else:
219
+ self.rope_type = "default"
220
+ self.max_seq_len_cached = config.max_position_embeddings
221
+ self.original_max_seq_len = config.max_position_embeddings
222
+
223
+ self.config = config
224
+ self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
225
+
226
+ inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device)
227
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
228
+ self.original_inv_freq = self.inv_freq
229
+
230
+ @torch.no_grad()
231
+ @dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)
232
+ def forward(self, x, position_ids):
233
+ inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1).to(x.device)
234
+ position_ids_expanded = position_ids[:, None, :].float()
235
+
236
+ device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu"
237
+ with torch.autocast(device_type=device_type, enabled=False): # Force float32
238
+ freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
239
+ emb = torch.cat((freqs, freqs), dim=-1)
240
+ cos = emb.cos() * self.attention_scaling
241
+ sin = emb.sin() * self.attention_scaling
242
+
243
+ return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
244
+
245
+
246
+ # Copied from transformers.models.llama.modeling_llama.rotate_half
247
+ def rotate_half(x):
248
+ """Rotates half the hidden dims of the input."""
249
+ x1 = x[..., : x.shape[-1] // 2]
250
+ x2 = x[..., x.shape[-1] // 2 :]
251
+ return torch.cat((-x2, x1), dim=-1)
252
+
253
+
254
+ # Copied from transformers.models.llama.modeling_llama.apply_rotary_pos_emb
255
+ def apply_rotary_pos_emb(q, k, cos, sin, unsqueeze_dim=1):
256
+ """Applies Rotary Position Embedding to the query and key tensors.
257
+ Args:
258
+ q (`torch.Tensor`): The query tensor.
259
+ k (`torch.Tensor`): The key tensor.
260
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
261
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
262
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
263
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
264
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
265
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
266
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
267
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
268
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
269
+ Returns:
270
+ `tuple(torch.Tensor)` comprising the query and key tensors rotated using the Rotary Position Embedding.
271
+ """
272
+ cos = cos.unsqueeze(unsqueeze_dim)
273
+ sin = sin.unsqueeze(unsqueeze_dim)
274
+
275
+ # Keep half or full tensor for later concatenation
276
+ rotary_dim = cos.shape[-1]
277
+ q_rot, q_pass = q[..., :rotary_dim], q[..., rotary_dim:]
278
+ k_rot, k_pass = k[..., :rotary_dim], k[..., rotary_dim:]
279
+
280
+ # Apply rotary embeddings on the first half or full tensor
281
+ q_embed = (q_rot * cos) + (rotate_half(q_rot) * sin)
282
+ k_embed = (k_rot * cos) + (rotate_half(k_rot) * sin)
283
+
284
+ # Concatenate back to full shape
285
+ q_embed = torch.cat([q_embed, q_pass], dim=-1)
286
+ k_embed = torch.cat([k_embed, k_pass], dim=-1)
287
+ return q_embed, k_embed
288
+
289
+
290
+ class BailingMoeV2MLP(nn.Module):
291
+ def __init__(self, config: BailingMoeLinearV2Config, intermediate_size: int):
292
+ super().__init__()
293
+ self.config = config
294
+ self.hidden_size = config.hidden_size
295
+ self.intermediate_size = intermediate_size
296
+
297
+ self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
298
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
299
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
300
+ self.act_fn = ACT2FN[config.hidden_act]
301
+
302
+ def forward(self, x):
303
+ return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
304
+
305
+
306
+ class BailingMoeV2Gate(nn.Module):
307
+ def __init__(self, config):
308
+ super().__init__()
309
+ self.config = config
310
+ self.top_k = config.num_experts_per_tok
311
+ self.num_experts = config.num_experts
312
+
313
+ self.n_group = config.n_group
314
+ self.topk_group = config.topk_group
315
+
316
+ # topk selection algorithm
317
+ self.gating_dim = config.hidden_size
318
+ self.weight = nn.Parameter(torch.empty((self.num_experts, self.gating_dim)))
319
+ self.routed_scaling_factor = config.routed_scaling_factor
320
+
321
+ self.register_buffer("expert_bias", torch.zeros((self.num_experts)))
322
+ self.reset_parameters()
323
+
324
+ def reset_parameters(self) -> None:
325
+ import torch.nn.init as init
326
+
327
+ init.kaiming_uniform_(self.weight, a=math.sqrt(5))
328
+
329
+ def group_limited_topk(
330
+ self,
331
+ scores: torch.Tensor,
332
+ ):
333
+ num_tokens, _ = scores.size()
334
+ # Organize the experts into groups
335
+ group_scores = scores.view(num_tokens, self.n_group, -1).topk(2, dim=-1)[0].sum(dim=-1)
336
+ group_idx = torch.topk(group_scores, k=self.topk_group, dim=-1, sorted=False)[1]
337
+ group_mask = torch.zeros_like(group_scores)
338
+ group_mask.scatter_(1, group_idx, 1)
339
+
340
+ # Mask the experts based on selection groups
341
+ score_mask = (
342
+ group_mask.unsqueeze(-1)
343
+ .expand(num_tokens, self.n_group, self.num_experts // self.n_group)
344
+ .reshape(num_tokens, -1)
345
+ )
346
+
347
+ masked_scores = scores.masked_fill(~score_mask.bool(), float('-inf'))
348
+ probs, top_indices = torch.topk(masked_scores, k=self.top_k, dim=-1)
349
+
350
+ return probs, top_indices
351
+
352
+ def forward(self, hidden_states):
353
+ # compute gating score
354
+ hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
355
+ logits = F.linear(hidden_states.type(torch.float32), self.weight.type(torch.float32))
356
+
357
+ scores = torch.sigmoid(logits.float()).type_as(logits)
358
+
359
+ scores_for_routing = scores + self.expert_bias
360
+ _, topk_idx = self.group_limited_topk(scores_for_routing)
361
+
362
+ scores = torch.gather(scores, dim=1, index=topk_idx).type_as(logits)
363
+
364
+ topk_weight = scores / (scores.sum(dim=-1, keepdim=True) + 1e-20) if self.top_k > 1 else scores
365
+ topk_weight = topk_weight * self.routed_scaling_factor
366
+
367
+ return topk_idx, topk_weight, logits
368
+
369
+
370
+ class BailingMoeV2SparseMoeBlock(nn.Module):
371
+ """
372
+ A mixed expert module containing shared experts.
373
+ """
374
+
375
+ def __init__(self, config: BailingMoeLinearV2Config):
376
+ super().__init__()
377
+ self.config = config
378
+ self.num_experts_per_tok = config.num_experts_per_tok
379
+ self._setup_experts()
380
+ self.gate = BailingMoeV2Gate(config)
381
+ if config.num_shared_experts is not None:
382
+ self.shared_experts = BailingMoeV2MLP(
383
+ config=config, intermediate_size=config.moe_intermediate_size * config.num_shared_experts
384
+ )
385
+
386
+ def _setup_experts(self):
387
+ self.experts = nn.ModuleList(
388
+ [
389
+ BailingMoeV2MLP(config=self.config, intermediate_size=self.config.moe_intermediate_size)
390
+ for _ in range(self.config.num_experts)
391
+ ]
392
+ )
393
+
394
+ def forward(self, hidden_states):
395
+ identity = hidden_states
396
+ bsz, seq_len, h = hidden_states.shape
397
+ topk_idx, topk_weight, router_logits = self.gate(hidden_states)
398
+ hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
399
+ flat_topk_idx = topk_idx.view(-1)
400
+ if self.training:
401
+ hidden_states = hidden_states.repeat_interleave(self.num_experts_per_tok, dim=0)
402
+ y = torch.empty_like(hidden_states)
403
+ for i, expert in enumerate(self.experts):
404
+ y[flat_topk_idx == i] = expert(hidden_states[flat_topk_idx == i])
405
+ y = (y.view(*topk_weight.shape, -1) * topk_weight.unsqueeze(-1)).sum(dim=1)
406
+ y = y.to(hidden_states.dtype).view(bsz, seq_len, h)
407
+ else:
408
+ y = self.moe_infer(hidden_states, topk_idx, topk_weight).view(bsz, seq_len, h)
409
+ if self.config.num_shared_experts is not None:
410
+ y = y + self.shared_experts(identity)
411
+ return y, (router_logits.view(bsz, seq_len, -1), topk_idx.view(bsz, seq_len, -1))
412
+
413
+ @torch.no_grad()
414
+ def moe_infer(self, x, topk_ids, topk_weight):
415
+ cnts = topk_ids.new_zeros((topk_ids.shape[0], len(self.experts)))
416
+ cnts.scatter_(1, topk_ids, 1)
417
+ tokens_per_expert = cnts.sum(dim=0)
418
+ idxs = topk_ids.view(-1).argsort()
419
+ sorted_tokens = x[idxs // topk_ids.shape[1]]
420
+ tokens_per_expert = tokens_per_expert.cpu().numpy()
421
+ outputs = []
422
+ start_idx = 0
423
+ for i, num_tokens in enumerate(tokens_per_expert):
424
+ end_idx = start_idx + num_tokens
425
+ if num_tokens == 0:
426
+ continue
427
+ expert = self.experts[i]
428
+ tokens_for_this_expert = sorted_tokens[start_idx:end_idx]
429
+ expert_out = expert(tokens_for_this_expert)
430
+ outputs.append(expert_out.to(x.device))
431
+ start_idx = end_idx
432
+
433
+ outs = torch.cat(outputs, dim=0) if len(outputs) else sorted_tokens.new_empty(0)
434
+ new_x = torch.empty_like(outs)
435
+ new_x[idxs] = outs
436
+ final_out = (
437
+ new_x.view(*topk_ids.shape, -1)
438
+ .type(topk_weight.dtype)
439
+ .mul_(topk_weight.unsqueeze(dim=-1))
440
+ .sum(dim=1)
441
+ .type(new_x.dtype)
442
+ )
443
+ return final_out
444
+
445
+
446
+ # Copied from transformers.models.llama.modeling_llama.repeat_kv
447
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int, head_first: bool = True) -> torch.Tensor:
448
+ """
449
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). If head_first is True, the hidden states go from (batch,
450
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
451
+ """
452
+ if n_rep == 1:
453
+ return hidden_states
454
+ if head_first:
455
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
456
+ hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
457
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
458
+ else:
459
+ batch, slen, num_key_value_heads, head_dim = hidden_states.shape
460
+ hidden_states = hidden_states[:, :, :, None, :].expand(batch, slen, num_key_value_heads, n_rep, head_dim)
461
+ return hidden_states.reshape(batch, slen, num_key_value_heads * n_rep, head_dim)
462
+
463
+
464
+ # Copied from transformers.models.llama.modeling_llama.LlamaAttention with Llama->BailingMoeV2
465
+ class BailingMoeV2Attention(nn.Module):
466
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
467
+
468
+ def __init__(self, config: BailingMoeLinearV2Config, layer_idx: Optional[int] = None):
469
+ super().__init__()
470
+ self.config = config
471
+ self.layer_idx = layer_idx
472
+ if layer_idx is None:
473
+ logger.warning_once(
474
+ f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
475
+ "to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
476
+ "when creating this class."
477
+ )
478
+
479
+ self.attention_dropout = config.attention_dropout
480
+ self.hidden_size = config.hidden_size
481
+ self.num_heads = config.num_attention_heads
482
+ self.head_dim = config.head_dim or self.hidden_size // self.num_heads
483
+ partial_rotary_factor = config.partial_rotary_factor if hasattr(config, "partial_rotary_factor") else 1.0
484
+ self.rope_dim = int(self.head_dim * partial_rotary_factor)
485
+ self.num_key_value_heads = config.num_key_value_heads
486
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
487
+ self.max_position_embeddings = config.max_position_embeddings
488
+ self.rope_theta = config.rope_theta
489
+ self.is_causal = True
490
+
491
+ self.query_key_value = nn.Linear(
492
+ self.hidden_size,
493
+ (self.num_heads + 2 * self.num_key_value_heads) * self.head_dim,
494
+ bias=config.use_qkv_bias,
495
+ )
496
+
497
+ if self.config.use_qk_norm:
498
+ self.query_layernorm = BailingMoeV2RMSNorm(self.head_dim, eps=config.rms_norm_eps)
499
+ self.key_layernorm = BailingMoeV2RMSNorm(self.head_dim, eps=config.rms_norm_eps)
500
+ self.dense = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.use_bias)
501
+
502
+ def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
503
+ return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
504
+
505
+ def forward(
506
+ self,
507
+ hidden_states: torch.Tensor,
508
+ attention_mask: Optional[torch.Tensor] = None,
509
+ position_ids: Optional[torch.LongTensor] = None,
510
+ past_key_value: Optional[Cache] = None,
511
+ output_attentions: bool = False,
512
+ use_cache: bool = False,
513
+ position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC
514
+ **kwargs,
515
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
516
+
517
+ bsz, q_len, _ = hidden_states.size()
518
+
519
+ qkv = self.query_key_value(hidden_states)
520
+ qkv = qkv.view(bsz, q_len, self.num_heads + 2 * self.num_key_value_heads, self.head_dim)
521
+
522
+ query_states, key_states, value_states = qkv.split(
523
+ [self.num_heads, self.num_key_value_heads, self.num_key_value_heads], dim=-2
524
+ )
525
+ query_states = query_states.transpose(1, 2)
526
+ key_states = key_states.transpose(1, 2)
527
+ value_states = value_states.transpose(1, 2)
528
+
529
+ if self.config.use_qk_norm:
530
+ query_states = self.query_layernorm(query_states)
531
+ key_states = self.key_layernorm(key_states)
532
+
533
+ cos, sin = position_embeddings
534
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
535
+
536
+ if past_key_value is not None:
537
+ if self.layer_idx is None:
538
+ raise ValueError(
539
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
540
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
541
+ "with a layer index."
542
+ )
543
+ cache_kwargs = {"sin": sin, "cos": cos}
544
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
545
+
546
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
547
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
548
+
549
+ attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
550
+
551
+ kv_seq_len = key_states.shape[-2]
552
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
553
+ raise ValueError(
554
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
555
+ f" {attn_weights.size()}"
556
+ )
557
+
558
+ if attention_mask is not None:
559
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
560
+ raise ValueError(
561
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
562
+ )
563
+ attn_weights = attn_weights + attention_mask
564
+
565
+ # upcast attention to fp32
566
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
567
+ attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training)
568
+ attn_output = torch.matmul(attn_weights, value_states)
569
+
570
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
571
+ raise ValueError(
572
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
573
+ f" {attn_output.size()}"
574
+ )
575
+
576
+ attn_output = attn_output.transpose(1, 2).contiguous()
577
+
578
+ attn_output = attn_output.reshape(bsz, q_len, -1)
579
+
580
+ attn_output = self.dense(attn_output)
581
+
582
+ if not output_attentions:
583
+ attn_weights = None
584
+
585
+ return attn_output, attn_weights, past_key_value
586
+
587
+
588
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2 with Llama->BailingMoeV2
589
+ class BailingMoeV2FlashAttention2(BailingMoeV2Attention):
590
+ """
591
+ BailingMoeV2 flash attention module. This module inherits from `BailingMoeV2Attention` as the weights of the module stays
592
+ untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
593
+ flash attention and deal with padding tokens in case the input contains any of them.
594
+ """
595
+
596
+ def __init__(self, *args, **kwargs):
597
+ super().__init__(*args, **kwargs)
598
+
599
+ # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
600
+ # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0.
601
+ # Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left).
602
+ self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
603
+
604
+ def forward(
605
+ self,
606
+ hidden_states: torch.Tensor,
607
+ attention_mask: Optional[torch.LongTensor] = None,
608
+ position_ids: Optional[torch.LongTensor] = None,
609
+ past_key_value: Optional[Cache] = None,
610
+ output_attentions: bool = False,
611
+ use_cache: bool = False,
612
+ position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC
613
+ **kwargs,
614
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
615
+ # BailingMoeV2FlashAttention2 attention does not support output_attentions
616
+ output_attentions = False
617
+
618
+ bsz, q_len, _ = hidden_states.size()
619
+
620
+ # Flash attention requires the input to have the shape
621
+ # batch_size x seq_length x head_dim x hidden_dim
622
+ # therefore we just need to keep the original shape
623
+
624
+ qkv = self.query_key_value(hidden_states)
625
+ qkv = qkv.view(bsz, q_len, self.num_heads + 2 * self.num_key_value_heads, self.head_dim)
626
+
627
+ query_states, key_states, value_states = qkv.split(
628
+ [self.num_heads, self.num_key_value_heads, self.num_key_value_heads], dim=-2
629
+ )
630
+ query_states = query_states.transpose(1, 2)
631
+ key_states = key_states.transpose(1, 2)
632
+ value_states = value_states.transpose(1, 2)
633
+
634
+ if self.config.use_qk_norm:
635
+ query_states = self.query_layernorm(query_states)
636
+ key_states = self.key_layernorm(key_states)
637
+
638
+ cos, sin = position_embeddings
639
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
640
+
641
+ if past_key_value is not None:
642
+ cache_kwargs = {"sin": sin, "cos": cos}
643
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
644
+
645
+ # TODO: These transpose are quite inefficient but Flash Attention requires the layout [batch_size, sequence_length, num_heads, head_dim]. We would need to refactor the KV cache
646
+ # to be able to avoid many of these transpose/reshape/view.
647
+ query_states = query_states.transpose(1, 2)
648
+ key_states = key_states.transpose(1, 2)
649
+ value_states = value_states.transpose(1, 2)
650
+
651
+ dropout_rate = self.attention_dropout if self.training else 0.0
652
+
653
+ # In PEFT, usually we cast the layer norms in float32 for training stability reasons
654
+ # therefore the input hidden states gets silently cast in float32. Hence, we need
655
+ # cast them back in the correct dtype just to be sure everything works as expected.
656
+ # This might slow down training & inference so it is recommended to not cast the LayerNorms
657
+ # in fp32. (BailingMoeV2RMSNorm handles it correctly)
658
+
659
+ input_dtype = query_states.dtype
660
+ if input_dtype == torch.float32:
661
+ # Handle the case where the model is quantized
662
+ if hasattr(self.config, "_pre_quantization_dtype"):
663
+ target_dtype = self.config._pre_quantization_dtype
664
+ elif torch.is_autocast_enabled():
665
+ target_dtype = torch.get_autocast_gpu_dtype()
666
+ else:
667
+ target_dtype = self.query_key_value.weight.dtype
668
+
669
+ logger.warning_once(
670
+ f"The input hidden states seems to be silently casted in float32, this might be related to"
671
+ f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
672
+ f" {target_dtype}."
673
+ )
674
+
675
+ query_states = query_states.to(target_dtype)
676
+ key_states = key_states.to(target_dtype)
677
+ value_states = value_states.to(target_dtype)
678
+
679
+ attn_output = self._flash_attention_forward(
680
+ query_states, key_states, value_states, attention_mask, q_len, dropout=dropout_rate
681
+ )
682
+
683
+ attn_output = attn_output.reshape(bsz, q_len, -1).contiguous()
684
+ attn_output = self.dense(attn_output)
685
+
686
+ if not output_attentions:
687
+ attn_weights = None
688
+
689
+ return attn_output, attn_weights, past_key_value
690
+
691
+ def _flash_attention_forward(
692
+ self, query_states, key_states, value_states, attention_mask, query_length, dropout=0.0, softmax_scale=None
693
+ ):
694
+ """
695
+ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
696
+ first unpad the input, then computes the attention scores and pad the final attention scores.
697
+ Args:
698
+ query_states (`torch.Tensor`):
699
+ Input query states to be passed to Flash Attention API
700
+ key_states (`torch.Tensor`):
701
+ Input key states to be passed to Flash Attention API
702
+ value_states (`torch.Tensor`):
703
+ Input value states to be passed to Flash Attention API
704
+ attention_mask (`torch.Tensor`):
705
+ The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
706
+ position of padding tokens and 1 for the position of non-padding tokens.
707
+ dropout (`int`, *optional*):
708
+ Attention dropout
709
+ softmax_scale (`float`, *optional*):
710
+ The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
711
+ query_length (`int`):
712
+ The length of the query sequence in terms of tokens. This represents the number of tokens in the
713
+ `query_states` tensor along the sequence dimension. It is used to determine the effective sequence
714
+ length for attention computations.
715
+ """
716
+ if not self._flash_attn_uses_top_left_mask:
717
+ causal = self.is_causal
718
+ else:
719
+ # TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in BailingMoeV2FlashAttention2 __init__.
720
+ causal = self.is_causal and query_length != 1
721
+
722
+ # Contains at least one padding token in the sequence
723
+ if attention_mask is not None:
724
+ batch_size = query_states.shape[0]
725
+ query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
726
+ query_states, key_states, value_states, attention_mask, query_length
727
+ )
728
+
729
+ cu_seqlens_q, cu_seqlens_k = cu_seq_lens
730
+ max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
731
+
732
+ attn_output_unpad = flash_attn_varlen_func(
733
+ query_states,
734
+ key_states,
735
+ value_states,
736
+ cu_seqlens_q=cu_seqlens_q,
737
+ cu_seqlens_k=cu_seqlens_k,
738
+ max_seqlen_q=max_seqlen_in_batch_q,
739
+ max_seqlen_k=max_seqlen_in_batch_k,
740
+ dropout_p=dropout,
741
+ softmax_scale=softmax_scale,
742
+ causal=causal,
743
+ )
744
+
745
+ attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length)
746
+ else:
747
+ attn_output = flash_attn_func(
748
+ query_states, key_states, value_states, dropout, softmax_scale=softmax_scale, causal=causal
749
+ )
750
+
751
+ return attn_output
752
+
753
+ def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
754
+ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
755
+ batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
756
+
757
+ key_layer = index_first_axis(
758
+ key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
759
+ )
760
+ value_layer = index_first_axis(
761
+ value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
762
+ )
763
+ if query_length == kv_seq_len:
764
+ query_layer = index_first_axis(
765
+ query_layer.reshape(batch_size * kv_seq_len, self.num_heads, head_dim), indices_k
766
+ )
767
+ cu_seqlens_q = cu_seqlens_k
768
+ max_seqlen_in_batch_q = max_seqlen_in_batch_k
769
+ indices_q = indices_k
770
+ elif query_length == 1:
771
+ max_seqlen_in_batch_q = 1
772
+ cu_seqlens_q = torch.arange(
773
+ batch_size + 1, dtype=torch.int32, device=query_layer.device
774
+ ) # There is a memcpy here, that is very bad.
775
+ indices_q = cu_seqlens_q[:-1]
776
+ query_layer = query_layer.squeeze(1)
777
+ else:
778
+ # The -q_len: slice assumes left padding.
779
+ attention_mask = attention_mask[:, -query_length:]
780
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, attention_mask)
781
+
782
+ return (
783
+ query_layer,
784
+ key_layer,
785
+ value_layer,
786
+ indices_q,
787
+ (cu_seqlens_q, cu_seqlens_k),
788
+ (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
789
+ )
790
+
791
+
792
+ # Copied from transformers.models.llama.modeling_llama.LlamaSdpaAttention with Llama->BailingMoeV2
793
+ class BailingMoeV2SdpaAttention(BailingMoeV2Attention):
794
+ """
795
+ BailingMoeV2 attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
796
+ `BailingMoeV2Attention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
797
+ SDPA API.
798
+ """
799
+
800
+ # Adapted from BailingMoeV2Attention.forward
801
+ def forward(
802
+ self,
803
+ hidden_states: torch.Tensor,
804
+ attention_mask: Optional[torch.Tensor] = None,
805
+ position_ids: Optional[torch.LongTensor] = None,
806
+ past_key_value: Optional[Cache] = None,
807
+ output_attentions: bool = False,
808
+ use_cache: bool = False,
809
+ position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC
810
+ **kwargs,
811
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
812
+ if output_attentions:
813
+ # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented.
814
+ logger.warning_once(
815
+ "BailingMoeV2Model is using BailingMoeV2SdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, "
816
+ 'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
817
+ )
818
+ return super().forward(
819
+ hidden_states=hidden_states,
820
+ attention_mask=attention_mask,
821
+ position_ids=position_ids,
822
+ past_key_value=past_key_value,
823
+ output_attentions=output_attentions,
824
+ use_cache=use_cache,
825
+ )
826
+
827
+ bsz, q_len, _ = hidden_states.size()
828
+
829
+ qkv = self.query_key_value(hidden_states)
830
+ qkv = qkv.view(bsz, q_len, self.num_heads + 2 * self.num_key_value_heads, self.head_dim)
831
+
832
+ query_states, key_states, value_states = qkv.split(
833
+ [self.num_heads, self.num_key_value_heads, self.num_key_value_heads], dim=-2
834
+ )
835
+ query_states = query_states.transpose(1, 2)
836
+ key_states = key_states.transpose(1, 2)
837
+ value_states = value_states.transpose(1, 2)
838
+
839
+ if self.config.use_qk_norm:
840
+ query_states = self.query_layernorm(query_states)
841
+ key_states = self.key_layernorm(key_states)
842
+
843
+ cos, sin = position_embeddings
844
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
845
+
846
+ if past_key_value is not None:
847
+ cache_kwargs = {"sin": sin, "cos": cos}
848
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
849
+
850
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
851
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
852
+
853
+ if attention_mask is not None:
854
+ kv_seq_len = key_states.shape[-2]
855
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
856
+ raise ValueError(
857
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
858
+ )
859
+
860
+ # SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
861
+ # Reference: https://github.com/pytorch/pytorch/issues/112577.
862
+ if query_states.device.type == "cuda" and attention_mask is not None:
863
+ query_states = query_states.contiguous()
864
+ key_states = key_states.contiguous()
865
+ value_states = value_states.contiguous()
866
+
867
+ attn_output = torch.nn.functional.scaled_dot_product_attention(
868
+ query_states,
869
+ key_states,
870
+ value_states,
871
+ attn_mask=attention_mask,
872
+ dropout_p=self.attention_dropout if self.training else 0.0,
873
+ # The q_len > 1 is necessary to match with AttentionMaskConverter.to_causal_4d that does not create a causal mask in case q_len == 1.
874
+ is_causal=self.is_causal and attention_mask is None and q_len > 1,
875
+ )
876
+
877
+ attn_output = attn_output.transpose(1, 2).contiguous()
878
+ attn_output = attn_output.reshape(bsz, q_len, -1)
879
+
880
+ attn_output = self.dense(attn_output)
881
+
882
+ return attn_output, None, past_key_value
883
+
884
+
885
+ ATTENTION_CLASSES = {
886
+ "eager": BailingMoeV2Attention,
887
+ "flash_attention_2": BailingMoeV2FlashAttention2,
888
+ "sdpa": BailingMoeV2SdpaAttention,
889
+ }
890
+
891
+
892
+ class BailingMoeV2LinearAttention(nn.Module):
893
+ """
894
+ BailingMoeAttention implements a linear attention mechanism based on Lightning Attention-2
895
+ (https://arxiv.org/abs/2401.04658) with efficient computation using flash-linear-attention operators.
896
+
897
+ The implementation leverages optimized kernels from the flash-linear-attention library
898
+ (https://github.com/fla-org/flash-linear-attention) for maximum performance.
899
+ """
900
+ def __init__(self, config: BailingMoeLinearV2Config, layer_idx: Optional[int] = None):
901
+ super().__init__()
902
+ self.config = config
903
+ self.layer_idx = layer_idx
904
+ if layer_idx is None:
905
+ logger.warning_once(
906
+ f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
907
+ "to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
908
+ "when creating this class."
909
+ )
910
+ self.hidden_size = config.hidden_size
911
+ self.num_heads = config.num_attention_heads
912
+ self.head_dim = config.head_dim or self.hidden_size // self.num_heads
913
+ self.num_key_value_heads = config.num_attention_heads
914
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
915
+ partial_rotary_factor = config.partial_rotary_factor if hasattr(config, "partial_rotary_factor") else 1.0
916
+ self.rope_dim = int(self.head_dim * partial_rotary_factor)
917
+
918
+ self.use_qk_norm = getattr(config, "use_qk_norm", False)
919
+ self.rms_norm_eps = getattr(config, "rms_norm_eps", 1e-5)
920
+ self.mode = 'chunk'
921
+
922
+ self.query_key_value = nn.Linear(
923
+ self.hidden_size,
924
+ (self.num_heads + 2 * self.num_key_value_heads) * self.head_dim,
925
+ bias=config.use_qkv_bias,
926
+ )
927
+
928
+ if self.config.use_qk_norm:
929
+ self.query_layernorm = BailingMoeV2RMSNorm(self.head_dim, eps=config.rms_norm_eps)
930
+ self.key_layernorm = BailingMoeV2RMSNorm(self.head_dim, eps=config.rms_norm_eps)
931
+
932
+ self.rotary_emb = BailingMoeV2RotaryEmbedding(config=config)
933
+
934
+ self.dense = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.use_bias)
935
+
936
+ self.g_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
937
+ self.g_norm = BailingMoeV2GroupRMSNorm(self.num_heads * self.head_dim, group_norm_size=config.group_norm_size, eps=self.rms_norm_eps)
938
+ slope = - BailingMoeV2LinearAttention.build_slope_tensor(self.num_heads) * (1 - (self.layer_idx - 1) / (self.config.num_hidden_layers - 1) + 1e-5)
939
+ self.register_buffer('slope', slope, persistent=False)
940
+
941
+ self.lightning_attn_ops = {
942
+ 'chunk': chunk_simple_gla,
943
+ 'fused_recurrent': fused_recurrent_simple_gla
944
+ }
945
+
946
+ @staticmethod
947
+ def build_slope_tensor(n_attention_heads: int):
948
+ """
949
+ Build a tensor of slopes for Lightning Attention-2 as described in the paper:
950
+ "Lightning Attention-2: A Free Lunch for Handling Unlimited Sequence Lengths in Large Language Models"
951
+ (https://arxiv.org/abs/2401.04658)
952
+
953
+ This function computes the slope values that control the decay rate of attention scores
954
+ based on the number of attention heads. The slopes are designed to have specific
955
+ mathematical properties that work optimally when the number of heads is a power of 2.
956
+
957
+ For non-power-of-2 head counts, a workaround is implemented to maintain similar properties.
958
+
959
+ Args:
960
+ n_attention_heads (int): Number of attention heads in the model
961
+
962
+ Returns:
963
+ torch.Tensor: A tensor of shape [n_attention_heads] containing the computed slopes
964
+
965
+ Note:
966
+ Code copied from: https://github.com/OpenNLPLab/lightning-attention/blob/d15c38529bbd5c2c82b44ddda3cac885825aa873/lightning_attn/utils/utils.py#L6
967
+ """
968
+ def get_slopes(n):
969
+ def get_slopes_power_of_2(n):
970
+ start = 2 ** (-(2 ** -(math.log2(n) - 3)))
971
+ ratio = start
972
+ return [start * ratio ** i for i in range(n)]
973
+
974
+ if math.log2(n).is_integer():
975
+ return get_slopes_power_of_2(
976
+ n) # In the paper, we only train models that have 2^a heads for some a. This function has
977
+ else: # some good properties that only occur when the input is a power of 2. To maintain that even
978
+ closest_power_of_2 = 2 ** math.floor(
979
+ math.log2(n)) # when the number of heads is not a power of 2, we use this workaround.
980
+ return (get_slopes_power_of_2(closest_power_of_2)
981
+ + get_slopes(2 * closest_power_of_2)[0::2][:n - closest_power_of_2])
982
+
983
+ slopes = torch.tensor(get_slopes(n_attention_heads), dtype=torch.float)
984
+ return slopes
985
+
986
+
987
+ def forward(
988
+ self,
989
+ hidden_states: torch.Tensor,
990
+ attention_mask: Optional[torch.Tensor] = None,
991
+ position_ids: Optional[torch.LongTensor] = None,
992
+ past_key_value: Optional[Cache] = None,
993
+ output_attentions: bool = False,
994
+ use_cache: bool = False,
995
+ position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
996
+ **kwargs,
997
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
998
+ if attention_mask is not None:
999
+ assert len(attention_mask.shape) == 2, (
1000
+ "Expected attention_mask as a 0-1 matrix with shape [batch_size, seq_len] "
1001
+ "for padding purposes (0 indicating padding). "
1002
+ "Arbitrary attention masks of shape [batch_size, seq_len, seq_len] are not allowed."
1003
+ )
1004
+
1005
+ # launching the triton kernel for just one token will actually be slower
1006
+ mode = 'fused_recurrent' if hidden_states.shape[1] <= 64 else self.mode
1007
+
1008
+ # Currently output_attentions can only be False, returning attention weights is not supported
1009
+ assert not output_attentions, "output_attentions can only be False, returning attention weights is not supported"
1010
+
1011
+ bsz, q_len, _ = hidden_states.size()
1012
+ device = hidden_states.device
1013
+
1014
+ qkv = self.query_key_value(hidden_states)
1015
+ qkv = qkv.view(bsz, q_len, self.num_heads + 2 * self.num_key_value_heads, self.head_dim)
1016
+ query_states, key_states, value_states = qkv.split(
1017
+ [self.num_heads, self.num_key_value_heads, self.num_key_value_heads], dim=-2
1018
+ )
1019
+ if self.config.use_qk_norm:
1020
+ query_states = self.query_layernorm(query_states)
1021
+ key_states = self.key_layernorm(key_states)
1022
+
1023
+ cos, sin = position_embeddings
1024
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, unsqueeze_dim=2)
1025
+
1026
+ if self.num_key_value_groups > 1:
1027
+ # [bsz, q_len, n_kv_heads, head_dim] -> [bsz, q_len, n_heads, head_dim]
1028
+ key_states = repeat_kv(key_states, self.num_key_value_groups, head_first=False)
1029
+ value_states = repeat_kv(value_states, self.num_key_value_groups, head_first=False)
1030
+
1031
+ recurrent_state = None
1032
+ if past_key_value is not None and isinstance(past_key_value, Cache):
1033
+ # ensure the cache list is long enough
1034
+ while len(past_key_value.layers) <= self.layer_idx:
1035
+ past_key_value.layers.append(DynamicLayer())
1036
+
1037
+ if past_key_value.layers[self.layer_idx].keys is not None:
1038
+ recurrent_state = past_key_value.layers[self.layer_idx].keys
1039
+ # ensure recurrent_state is on the same device as hidden_states
1040
+ if recurrent_state.device != hidden_states.device:
1041
+ recurrent_state = recurrent_state.to(device).contiguous()
1042
+
1043
+ if recurrent_state is None:
1044
+ # dealing with left-padding
1045
+ if attention_mask is not None and use_cache:
1046
+ value_states = value_states.mul_(attention_mask[:, -q_len:, None, None])
1047
+
1048
+ o, recurrent_state = self.lightning_attn_ops[mode](
1049
+ q=query_states,
1050
+ k=key_states,
1051
+ v=value_states,
1052
+ g=self.slope[None, None, :].expand(bsz, q_len, self.num_heads),
1053
+ initial_state=recurrent_state,
1054
+ output_final_state=use_cache,
1055
+ )
1056
+
1057
+ o = o.reshape(bsz, q_len, -1)
1058
+ o = self.g_norm(o)
1059
+ g_proj = self.g_proj(hidden_states)
1060
+ o = o * torch.sigmoid_(g_proj)
1061
+ o = self.dense(o)
1062
+
1063
+ if use_cache and past_key_value is not None and isinstance(past_key_value, Cache):
1064
+ target_device = None
1065
+ for cache in past_key_value.layers:
1066
+ if cache.keys is not None:
1067
+ target_device = cache.keys.device
1068
+ break
1069
+ if target_device is None:
1070
+ target_device = recurrent_state.device
1071
+
1072
+ # move to target device
1073
+ if recurrent_state.device != target_device:
1074
+ recurrent_state = recurrent_state.to(target_device)
1075
+
1076
+ past_key_value.layers[self.layer_idx].keys = recurrent_state
1077
+
1078
+ return o, None, past_key_value
1079
+
1080
+
1081
+ class BailingMoeV2MTPLayer(nn.Module):
1082
+ def __init__(self, config: BailingMoeLinearV2Config, layer_idx: int):
1083
+ super().__init__()
1084
+ self.layer_idx = layer_idx
1085
+ self.input_layernorm = BailingMoeV2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1086
+ self.enorm = BailingMoeV2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1087
+
1088
+ self.eh_proj = nn.Linear(config.hidden_size * 2, config.hidden_size, bias=False)
1089
+ self.post_attention_layernorm = BailingMoeV2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1090
+ self.attention = ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx)
1091
+ self.mlp = BailingMoeV2SparseMoeBlock(config)
1092
+
1093
+ self.hnorm = BailingMoeV2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1094
+ self.final_layernorm = BailingMoeV2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1095
+
1096
+ def forward(
1097
+ self,
1098
+ input_embeds,
1099
+ hidden_states: torch.Tensor,
1100
+ attention_mask: Optional[torch.Tensor] = None,
1101
+ position_ids: Optional[torch.LongTensor] = None,
1102
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
1103
+ output_attentions: Optional[bool] = False,
1104
+ output_router_logits: Optional[bool] = False,
1105
+ use_cache: Optional[bool] = False,
1106
+ position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC
1107
+ **kwargs,
1108
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
1109
+ input_embeds = self.enorm(input_embeds)
1110
+ hidden_states = self.hnorm(hidden_states)
1111
+ hidden_states = self.eh_proj(torch.cat([input_embeds, hidden_states], dim=-1))
1112
+ residual = hidden_states
1113
+
1114
+ hidden_states = self.input_layernorm(hidden_states)
1115
+
1116
+ # Self Attention
1117
+ hidden_states, self_attn_weights, present_key_value = self.attention(
1118
+ hidden_states=hidden_states,
1119
+ attention_mask=attention_mask,
1120
+ position_ids=position_ids,
1121
+ past_key_value=past_key_value,
1122
+ output_attentions=output_attentions,
1123
+ position_embeddings=position_embeddings,
1124
+ use_cache=use_cache,
1125
+ )
1126
+ hidden_states = residual + hidden_states
1127
+
1128
+ # Fully Connected
1129
+ residual = hidden_states
1130
+ hidden_states = self.post_attention_layernorm(hidden_states)
1131
+ hidden_states = self.mlp(hidden_states)
1132
+ if isinstance(hidden_states, tuple):
1133
+ hidden_states, router_logits = hidden_states
1134
+ else:
1135
+ router_logits = None
1136
+ hidden_states = residual + hidden_states.to(residual.device)
1137
+ hidden_states = self.final_layernorm(hidden_states)
1138
+
1139
+ outputs = (hidden_states,)
1140
+
1141
+ if output_attentions:
1142
+ outputs += (self_attn_weights,)
1143
+
1144
+ if use_cache:
1145
+ outputs += (present_key_value,)
1146
+
1147
+ if output_router_logits:
1148
+ outputs += (router_logits,)
1149
+
1150
+ return outputs
1151
+
1152
+
1153
+ class BailingMoeLinearV2DecoderLayer(nn.Module):
1154
+ def __init__(self, config: BailingMoeLinearV2Config, layer_idx: int):
1155
+ super().__init__()
1156
+ self.hidden_size = config.hidden_size
1157
+ self.attention_layer_type = "attention" if (layer_idx + 1) % config.layer_group_size == 0 or \
1158
+ layer_idx >= config.num_hidden_layers // config.layer_group_size * config.layer_group_size else "linear_attention"
1159
+
1160
+ if self.attention_layer_type == "attention":
1161
+ self.attention = ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx)
1162
+ else:
1163
+ self.attention = BailingMoeV2LinearAttention(
1164
+ config=config,
1165
+ layer_idx=layer_idx
1166
+ )
1167
+
1168
+ self.mlp = (
1169
+ BailingMoeV2SparseMoeBlock(config)
1170
+ if (config.num_experts is not None and layer_idx >= config.first_k_dense_replace)
1171
+ else BailingMoeV2MLP(config=config, intermediate_size=config.intermediate_size)
1172
+ )
1173
+ self.input_layernorm = BailingMoeV2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1174
+ self.post_attention_layernorm = BailingMoeV2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1175
+
1176
+ def forward(
1177
+ self,
1178
+ hidden_states: torch.Tensor,
1179
+ attention_mask: Optional[torch.Tensor] = None,
1180
+ position_ids: Optional[torch.LongTensor] = None,
1181
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
1182
+ output_attentions: Optional[bool] = False,
1183
+ output_router_logits: Optional[bool] = False,
1184
+ use_cache: Optional[bool] = False,
1185
+ position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC
1186
+ **kwargs,
1187
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
1188
+ """
1189
+ Args:
1190
+ hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
1191
+ attention_mask (`torch.FloatTensor`, *optional*):
1192
+ attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1,
1193
+ query_sequence_length, key_sequence_length)` if default attention is used.
1194
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1195
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
1196
+ config.n_positions - 1]`.
1197
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*):
1198
+ cached past key and value projection states
1199
+ output_attentions (`bool`, *optional*):
1200
+ Whether to return the attentions tensors of all attention layers. See `attentions` under
1201
+ returned tensors for more detail.
1202
+ output_router_logits (`bool`, *optional*):
1203
+ Whether or not to return the logits of all the routers. They are useful for computing the router loss,
1204
+ and should not be returned during inference.
1205
+ use_cache (`bool`, *optional*):
1206
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
1207
+ (see `past_key_values`).
1208
+ """
1209
+ residual = hidden_states
1210
+
1211
+ hidden_states = self.input_layernorm(hidden_states)
1212
+
1213
+ # Self Attention
1214
+ if self.attention_layer_type == "attention":
1215
+ hidden_states, self_attn_weights, present_key_value = self.attention(
1216
+ hidden_states=hidden_states,
1217
+ attention_mask=attention_mask,
1218
+ position_ids=position_ids,
1219
+ past_key_value=past_key_value,
1220
+ output_attentions=output_attentions,
1221
+ position_embeddings=position_embeddings,
1222
+ use_cache=use_cache,
1223
+ )
1224
+ else:
1225
+ batch_size, seq_len = hidden_states.shape[0], hidden_states.shape[1]
1226
+ device = hidden_states.device
1227
+
1228
+ if attention_mask is None:
1229
+ # if attention_mask is None, create a full mask
1230
+ attention_mask = torch.ones((batch_size, seq_len), dtype=torch.int32, device=device)
1231
+ elif attention_mask.dim() == 4 and attention_mask.shape[1] == 1:
1232
+ attention_mask = attention_mask[:, 0, -1, :].to(torch.int32)
1233
+ attention_mask = (attention_mask > -1e4).to(torch.int32)
1234
+ elif attention_mask.dim() == 2:
1235
+ attention_mask = attention_mask.to(torch.int32)
1236
+ else:
1237
+ raise ValueError(f"Unsupported mask dimension: {attention_mask.shape}")
1238
+
1239
+ hidden_states, self_attn_weights, present_key_value = self.attention(
1240
+ hidden_states=hidden_states,
1241
+ attention_mask=attention_mask,
1242
+ past_key_value=past_key_value,
1243
+ position_ids=position_ids,
1244
+ use_cache=use_cache,
1245
+ output_attentions=output_attentions,
1246
+ position_embeddings=position_embeddings,
1247
+ )
1248
+
1249
+ hidden_states = residual + hidden_states
1250
+
1251
+ # Fully Connected
1252
+ residual = hidden_states
1253
+ hidden_states = self.post_attention_layernorm(hidden_states)
1254
+ hidden_states = self.mlp(hidden_states)
1255
+ if isinstance(hidden_states, tuple):
1256
+ hidden_states, router_logits = hidden_states
1257
+ else:
1258
+ router_logits = None
1259
+ hidden_states = residual + hidden_states.to(residual.device)
1260
+
1261
+ outputs = (hidden_states,)
1262
+
1263
+ if output_attentions:
1264
+ outputs += (self_attn_weights,)
1265
+
1266
+ if use_cache:
1267
+ outputs += (present_key_value,)
1268
+
1269
+ if output_router_logits:
1270
+ outputs += (router_logits,)
1271
+
1272
+ return outputs
1273
+
1274
+
1275
+ BAILINGMOEV2_START_DOCSTRING = r"""
1276
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
1277
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
1278
+ etc.)
1279
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
1280
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
1281
+ and behavior.
1282
+ Parameters:
1283
+ config ([`BailingMoeLinearV2Config`]):
1284
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
1285
+ load the weights associated with the model, only the configuration. Check out the
1286
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
1287
+ """
1288
+
1289
+
1290
+ @add_start_docstrings(
1291
+ "The bare BailingMoeV2 Model outputting raw hidden-states without any specific head on top.",
1292
+ BAILINGMOEV2_START_DOCSTRING,
1293
+ )
1294
+ class BailingMoeV2PreTrainedModel(PreTrainedModel):
1295
+ config_class = BailingMoeLinearV2Config
1296
+ base_model_prefix = "model"
1297
+ supports_gradient_checkpointing = True
1298
+ _no_split_modules = ["BailingMoeLinearV2DecoderLayer"]
1299
+ _skip_keys_device_placement = "past_key_values"
1300
+ _supports_flash_attn_2 = True
1301
+ _supports_sdpa = True
1302
+ _supports_cache_class = True
1303
+
1304
+ def _init_weights(self, module):
1305
+ std = self.config.initializer_range
1306
+ if isinstance(module, nn.Linear):
1307
+ module.weight.data.normal_(mean=0.0, std=std)
1308
+ if module.bias is not None:
1309
+ module.bias.data.zero_()
1310
+ elif isinstance(module, nn.Embedding):
1311
+ module.weight.data.normal_(mean=0.0, std=std)
1312
+ if module.padding_idx is not None:
1313
+ module.weight.data[module.padding_idx].zero_()
1314
+
1315
+
1316
+ BAILINGMOEV2_INPUTS_DOCSTRING = r"""
1317
+ Args:
1318
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
1319
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
1320
+ it.
1321
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
1322
+ [`PreTrainedTokenizer.__call__`] for details.
1323
+ [What are input IDs?](../glossary#input-ids)
1324
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
1325
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
1326
+ - 1 for tokens that are **not masked**,
1327
+ - 0 for tokens that are **masked**.
1328
+ [What are attention masks?](../glossary#attention-mask)
1329
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
1330
+ [`PreTrainedTokenizer.__call__`] for details.
1331
+ If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
1332
+ `past_key_values`).
1333
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
1334
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
1335
+ information on the default strategy.
1336
+ - 1 indicates the head is **not masked**,
1337
+ - 0 indicates the head is **masked**.
1338
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1339
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
1340
+ config.n_positions - 1]`.
1341
+ [What are position IDs?](../glossary#position-ids)
1342
+ past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
1343
+ Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
1344
+ blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
1345
+ returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
1346
+ Two formats are allowed:
1347
+ - a [`~cache_utils.Cache`] instance;
1348
+ - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
1349
+ shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
1350
+ cache format.
1351
+ The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
1352
+ legacy cache format will be returned.
1353
+ If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
1354
+ have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
1355
+ of shape `(batch_size, sequence_length)`.
1356
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
1357
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
1358
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
1359
+ model's internal embedding lookup matrix.
1360
+ use_cache (`bool`, *optional*):
1361
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
1362
+ `past_key_values`).
1363
+ output_attentions (`bool`, *optional*):
1364
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
1365
+ tensors for more detail.
1366
+ output_hidden_states (`bool`, *optional*):
1367
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
1368
+ more detail.
1369
+ return_dict (`bool`, *optional*):
1370
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
1371
+ """
1372
+
1373
+
1374
+ @add_start_docstrings(
1375
+ "The bare BailingMoeV2 Model outputting raw hidden-states without any specific head on top.",
1376
+ BAILINGMOEV2_START_DOCSTRING,
1377
+ )
1378
+ class BailingMoeLinearV2Model(BailingMoeV2PreTrainedModel):
1379
+ """
1380
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`BailingMoeLinearV2DecoderLayer`]
1381
+ Args:
1382
+ config: BailingMoeLinearV2Config
1383
+ """
1384
+
1385
+ def __init__(self, config: BailingMoeLinearV2Config):
1386
+ super().__init__(config)
1387
+ self.padding_idx = config.pad_token_id
1388
+ self.vocab_size = config.vocab_size
1389
+ self.num_nextn_predict_layers = config.num_nextn_predict_layers
1390
+
1391
+ self.word_embeddings = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
1392
+ self.layers = []
1393
+ for layer_idx in range(config.num_hidden_layers + config.num_nextn_predict_layers):
1394
+ layer_cls = BailingMoeLinearV2DecoderLayer if layer_idx < config.num_hidden_layers else BailingMoeV2MTPLayer
1395
+ self.layers.append(layer_cls(config, layer_idx))
1396
+
1397
+ self.layers = nn.ModuleList(self.layers)
1398
+
1399
+ self._use_sdpa = config._attn_implementation == "sdpa"
1400
+ self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2"
1401
+ self.norm = BailingMoeV2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1402
+ self.rotary_emb = BailingMoeV2RotaryEmbedding(config=config)
1403
+ self.gradient_checkpointing = False
1404
+ # Initialize weights and apply final processing
1405
+ self.post_init()
1406
+
1407
+ def get_input_embeddings(self):
1408
+ return self.word_embeddings
1409
+
1410
+ def set_input_embeddings(self, value):
1411
+ self.word_embeddings = value
1412
+
1413
+ @add_start_docstrings_to_model_forward(BAILINGMOEV2_INPUTS_DOCSTRING)
1414
+ def forward(
1415
+ self,
1416
+ input_ids: torch.LongTensor = None,
1417
+ attention_mask: Optional[torch.Tensor] = None,
1418
+ position_ids: Optional[torch.LongTensor] = None,
1419
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1420
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1421
+ use_cache: Optional[bool] = None,
1422
+ output_attentions: Optional[bool] = None,
1423
+ output_hidden_states: Optional[bool] = None,
1424
+ output_router_logits: Optional[bool] = None,
1425
+ return_dict: Optional[bool] = None,
1426
+ **kwargs,
1427
+ ) -> Union[Tuple, MoeV2ModelOutputWithPast]:
1428
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1429
+ output_hidden_states = (
1430
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1431
+ )
1432
+ output_router_logits = (
1433
+ output_router_logits if output_router_logits is not None else self.config.output_router_logits
1434
+ )
1435
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
1436
+
1437
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1438
+
1439
+ # retrieve input_ids and inputs_embeds
1440
+ if input_ids is not None and inputs_embeds is not None:
1441
+ raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
1442
+ elif input_ids is not None:
1443
+ batch_size, seq_length = input_ids.shape[:2]
1444
+ elif inputs_embeds is not None:
1445
+ batch_size, seq_length = inputs_embeds.shape[:2]
1446
+ else:
1447
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
1448
+
1449
+ if self.gradient_checkpointing and self.training:
1450
+ if use_cache:
1451
+ logger.warning_once(
1452
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`transformers."
1453
+ )
1454
+ use_cache = False
1455
+
1456
+ if use_cache and past_key_values is None:
1457
+ past_key_values = DynamicCache()
1458
+
1459
+ if inputs_embeds is None:
1460
+ inputs_embeds = self.word_embeddings(input_ids)
1461
+
1462
+ softmax_attention_layer_id = self.config.layer_group_size - 1
1463
+ past_seen_tokens = past_key_values.get_seq_length(layer_idx=softmax_attention_layer_id) if past_key_values is not None else 0
1464
+
1465
+ if position_ids is None:
1466
+ position_ids = torch.arange(
1467
+ past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
1468
+ )
1469
+ position_ids = position_ids.unsqueeze(0)
1470
+
1471
+ if self._use_flash_attention_2:
1472
+ # 2d mask is passed through the layers
1473
+ attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
1474
+ elif self._use_sdpa and not output_attentions:
1475
+ # output_attentions=True can not be supported when using SDPA, and we fall back on
1476
+ # the manual implementation that requires a 4D causal mask in all cases.
1477
+ attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
1478
+ attention_mask,
1479
+ (batch_size, seq_length),
1480
+ inputs_embeds,
1481
+ past_seen_tokens,
1482
+ )
1483
+ else:
1484
+ # 4d mask is passed through the layers
1485
+ attention_mask = _prepare_4d_causal_attention_mask(
1486
+ attention_mask, (batch_size, seq_length), inputs_embeds, past_seen_tokens
1487
+ )
1488
+
1489
+ # embed positions
1490
+ hidden_states = inputs_embeds
1491
+
1492
+ # create position embeddings to be shared across the decoder layers
1493
+ position_embeddings = self.rotary_emb(hidden_states, position_ids)
1494
+
1495
+ # decoder layers
1496
+ all_hidden_states = () if output_hidden_states else None
1497
+ all_self_attns = () if output_attentions else None
1498
+ all_router_logits = () if output_router_logits else None
1499
+ next_decoder_cache = None
1500
+ layers = self.layers[: -self.num_nextn_predict_layers] if self.num_nextn_predict_layers > 0 else self.layers
1501
+ mtp_layers = self.layers[-self.num_nextn_predict_layers :] if self.num_nextn_predict_layers > 0 else None
1502
+
1503
+ for decoder_layer in layers:
1504
+ if output_hidden_states:
1505
+ all_hidden_states += (hidden_states,)
1506
+
1507
+ if self.gradient_checkpointing and self.training:
1508
+ layer_outputs = self._gradient_checkpointing_func(
1509
+ decoder_layer.__call__,
1510
+ hidden_states,
1511
+ attention_mask,
1512
+ position_ids,
1513
+ past_key_values,
1514
+ output_attentions,
1515
+ output_router_logits,
1516
+ use_cache,
1517
+ position_embeddings,
1518
+ )
1519
+ else:
1520
+ layer_outputs = decoder_layer(
1521
+ hidden_states,
1522
+ attention_mask=attention_mask,
1523
+ position_ids=position_ids,
1524
+ past_key_value=past_key_values,
1525
+ output_attentions=output_attentions,
1526
+ output_router_logits=output_router_logits,
1527
+ use_cache=use_cache,
1528
+ position_embeddings=position_embeddings,
1529
+ )
1530
+ hidden_states = layer_outputs[0]
1531
+
1532
+ if use_cache:
1533
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
1534
+
1535
+ if output_attentions:
1536
+ all_self_attns += (layer_outputs[1],)
1537
+
1538
+ if output_router_logits and layer_outputs[-1] is not None:
1539
+ all_router_logits += (layer_outputs[-1],)
1540
+
1541
+ hidden_states = self.norm(hidden_states)
1542
+ main_hidden_states = hidden_states
1543
+
1544
+ # add hidden states from the last decoder layer
1545
+ if output_hidden_states:
1546
+ all_hidden_states += (main_hidden_states,)
1547
+
1548
+ mtp_hidden_states = None
1549
+
1550
+ if mtp_layers:
1551
+ for decoder_layer in mtp_layers:
1552
+ input_ids, _ = roll_tensor(input_ids, shifts=-1, dims=-1)
1553
+ inputs_embeds = self.word_embeddings(input_ids)
1554
+
1555
+ if self.gradient_checkpointing and self.training:
1556
+ layer_outputs = self._gradient_checkpointing_func(
1557
+ decoder_layer.__call__,
1558
+ inputs_embeds,
1559
+ hidden_states,
1560
+ attention_mask,
1561
+ position_ids,
1562
+ past_key_values,
1563
+ output_attentions,
1564
+ output_router_logits,
1565
+ use_cache,
1566
+ position_embeddings,
1567
+ )
1568
+ else:
1569
+ layer_outputs = decoder_layer(
1570
+ inputs_embeds,
1571
+ hidden_states,
1572
+ attention_mask=attention_mask,
1573
+ position_ids=position_ids,
1574
+ past_key_value=past_key_values,
1575
+ output_attentions=output_attentions,
1576
+ output_router_logits=output_router_logits,
1577
+ use_cache=use_cache,
1578
+ position_embeddings=position_embeddings,
1579
+ )
1580
+ if mtp_hidden_states is None:
1581
+ mtp_hidden_states = []
1582
+ hidden_states = layer_outputs[0]
1583
+ mtp_hidden_states.append(hidden_states)
1584
+
1585
+ if output_hidden_states:
1586
+ all_hidden_states += (hidden_states,)
1587
+
1588
+ if use_cache:
1589
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
1590
+
1591
+ if output_attentions:
1592
+ all_self_attns += (layer_outputs[1],)
1593
+
1594
+ if output_router_logits and layer_outputs[-1] is not None:
1595
+ all_router_logits += (layer_outputs[-1],)
1596
+
1597
+ next_cache = None
1598
+ if use_cache:
1599
+ next_cache = next_decoder_cache
1600
+ if not return_dict:
1601
+ return tuple(
1602
+ v
1603
+ for v in [main_hidden_states, next_cache, all_hidden_states, all_self_attns, all_router_logits]
1604
+ if v is not None
1605
+ )
1606
+ return MoeV2ModelOutputWithPast(
1607
+ last_hidden_state=main_hidden_states,
1608
+ past_key_values=next_cache,
1609
+ hidden_states=all_hidden_states,
1610
+ mtp_hidden_states=mtp_hidden_states,
1611
+ attentions=all_self_attns,
1612
+ router_logits=all_router_logits,
1613
+ )
1614
+
1615
+
1616
+ class BailingMoeLinearV2ForCausalLM(BailingMoeV2PreTrainedModel, GenerationMixin):
1617
+ _tied_weights_keys = ["lm_head.weight"]
1618
+
1619
+ def __init__(self, config: BailingMoeLinearV2Config):
1620
+ super().__init__(config)
1621
+ self.model = BailingMoeLinearV2Model(config)
1622
+ self.vocab_size = config.vocab_size
1623
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
1624
+ self.num_nextn_predict_layers = config.num_nextn_predict_layers
1625
+ self.mtp_loss_scaling_factor = config.mtp_loss_scaling_factor
1626
+
1627
+ # Initialize weights and apply final processing
1628
+ self.post_init()
1629
+
1630
+ def get_input_embeddings(self):
1631
+ return self.model.word_embeddings
1632
+
1633
+ def set_input_embeddings(self, value):
1634
+ self.model.word_embeddings = value
1635
+
1636
+ def get_output_embeddings(self):
1637
+ return self.lm_head
1638
+
1639
+ def set_output_embeddings(self, new_embeddings):
1640
+ self.lm_head = new_embeddings
1641
+
1642
+ def set_decoder(self, decoder):
1643
+ self.model = decoder
1644
+
1645
+ def get_decoder(self):
1646
+ return self.model
1647
+
1648
+ @add_start_docstrings_to_model_forward(BAILINGMOEV2_INPUTS_DOCSTRING)
1649
+ @replace_return_docstrings(output_type=MoEV2CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
1650
+ def forward(
1651
+ self,
1652
+ input_ids: torch.LongTensor = None,
1653
+ attention_mask: Optional[torch.Tensor] = None,
1654
+ position_ids: Optional[torch.LongTensor] = None,
1655
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1656
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1657
+ labels: Optional[torch.LongTensor] = None,
1658
+ use_cache: Optional[bool] = None,
1659
+ output_attentions: Optional[bool] = None,
1660
+ output_hidden_states: Optional[bool] = None,
1661
+ output_router_logits: Optional[bool] = None,
1662
+ return_dict: Optional[bool] = None,
1663
+ **kwargs,
1664
+ ) -> Union[Tuple, MoEV2CausalLMOutputWithPast]:
1665
+ r"""
1666
+ Args:
1667
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1668
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
1669
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1670
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
1671
+ Returns:
1672
+ Example:
1673
+ ```python
1674
+ >>> from transformers import AutoTokenizer
1675
+ >>> model = BailingMoeLinearV2ForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
1676
+ >>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
1677
+ >>> prompt = "Hey, are you conscious? Can you talk to me?"
1678
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
1679
+ >>> # Generate
1680
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
1681
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1682
+ "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
1683
+ ```"""
1684
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1685
+ output_hidden_states = (
1686
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1687
+ )
1688
+ output_router_logits = (
1689
+ output_router_logits if output_router_logits is not None else self.config.output_router_logits
1690
+ )
1691
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1692
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1693
+ outputs = self.model(
1694
+ input_ids=input_ids,
1695
+ attention_mask=attention_mask,
1696
+ position_ids=position_ids,
1697
+ past_key_values=past_key_values,
1698
+ inputs_embeds=inputs_embeds,
1699
+ use_cache=use_cache,
1700
+ output_attentions=output_attentions,
1701
+ output_hidden_states=output_hidden_states,
1702
+ output_router_logits=output_router_logits,
1703
+ return_dict=return_dict,
1704
+ **kwargs,
1705
+ )
1706
+
1707
+ loss = None
1708
+ all_mtp_loss = None
1709
+ aux_loss = None
1710
+ hidden_states = outputs[0]
1711
+ logits = self.lm_head(hidden_states)
1712
+ logits = logits.float()
1713
+
1714
+ if labels is not None:
1715
+ loss = self.loss_function(logits, labels, self.config.vocab_size, **kwargs)
1716
+
1717
+ all_mtp_logits = None
1718
+ if self.num_nextn_predict_layers > 0:
1719
+ mtp_hidden_states = outputs.mtp_hidden_states
1720
+ shift_labels_mtp = None
1721
+ for i in range(self.num_nextn_predict_layers):
1722
+ mtp_hidden_states = mtp_hidden_states[i]
1723
+ mtp_logits = self.lm_head(mtp_hidden_states).float()
1724
+ if all_mtp_logits is None:
1725
+ all_mtp_logits = []
1726
+ all_mtp_logits.append(mtp_logits)
1727
+ if labels is not None:
1728
+ if shift_labels_mtp is None:
1729
+ shift_labels_mtp = labels.clone()
1730
+ shift_labels_mtp, _ = roll_tensor(shift_labels_mtp, shifts=-1, dims=-1, fill_value=-100)
1731
+ mtp_logits_ = mtp_logits.view(-1, self.config.vocab_size)
1732
+ mtp_loss = self.loss_function(mtp_logits_, shift_labels_mtp.to(mtp_logits_.device).view(-1), self.config.vocab_size, **kwargs)
1733
+ if loss is not None:
1734
+ loss += self.mtp_loss_scaling_factor * mtp_loss
1735
+ else:
1736
+ loss = self.mtp_loss_scaling_factor * mtp_loss
1737
+
1738
+ if all_mtp_loss is None:
1739
+ all_mtp_loss = []
1740
+ all_mtp_loss.append(mtp_loss)
1741
+
1742
+ if not return_dict:
1743
+ output = (logits,) + outputs[1:]
1744
+ if output_router_logits:
1745
+ output = (aux_loss,) + output
1746
+ return (loss,) + output if loss is not None else output
1747
+
1748
+ return MoEV2CausalLMOutputWithPast(
1749
+ loss=loss,
1750
+ mtp_loss=all_mtp_loss,
1751
+ aux_loss=aux_loss,
1752
+ logits=logits,
1753
+ mtp_logits=all_mtp_logits,
1754
+ past_key_values=outputs.past_key_values,
1755
+ hidden_states=outputs.hidden_states,
1756
+ attentions=outputs.attentions,
1757
+ router_logits=outputs.router_logits,
1758
+ )
recipe.yaml ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ default_stage:
2
+ default_modifiers:
3
+ GPTQModifier:
4
+ config_groups:
5
+ config_group:
6
+ targets: [Linear]
7
+ weights:
8
+ num_bits: 4
9
+ type: int
10
+ symmetric: true
11
+ group_size: 128
12
+ strategy: group
13
+ block_structure: null
14
+ dynamic: false
15
+ actorder: weight
16
+ observer: minmax
17
+ observer_kwargs: {}
18
+ input_activations: null
19
+ output_activations: null
20
+ format: null
21
+ targets: [Linear]
22
+ ignore: [lm_head, 're:.*mlp.gate$', 're:.*shared_experts*$', 're:.*.dense$']
23
+ sequential_update: true
24
+ block_size: 128
25
+ dampening_frac: 0.01
26
+ offload_hessians: false
special_tokens_map.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<role>",
4
+ "</role>"
5
+ ],
6
+ "bos_token": {
7
+ "content": "<|startoftext|>",
8
+ "lstrip": false,
9
+ "normalized": false,
10
+ "rstrip": false,
11
+ "single_word": false
12
+ },
13
+ "cls_token": {
14
+ "content": "[CLS]",
15
+ "lstrip": false,
16
+ "normalized": false,
17
+ "rstrip": false,
18
+ "single_word": false
19
+ },
20
+ "eos_token": {
21
+ "content": "<|endoftext|>",
22
+ "lstrip": false,
23
+ "normalized": false,
24
+ "rstrip": false,
25
+ "single_word": false
26
+ },
27
+ "gmask_token": {
28
+ "content": "[gMASK]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false
33
+ },
34
+ "pad_token": {
35
+ "content": "<|endoftext|>",
36
+ "lstrip": false,
37
+ "normalized": false,
38
+ "rstrip": false,
39
+ "single_word": false
40
+ }
41
+ }
tokenization_bailing.py ADDED
@@ -0,0 +1,1068 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # coding=utf-8
3
+ # Copyright (c) Ant Group. All rights reserved.
4
+
5
+ import itertools
6
+ from typing import Any, Dict, List, Optional, Union
7
+
8
+ import torch
9
+ from transformers import PreTrainedTokenizerFast
10
+ from transformers.tokenization_utils_base import AddedToken, BatchEncoding
11
+ from transformers.utils import TensorType, logging
12
+
13
+ logger = logging.get_logger(__name__)
14
+
15
+
16
+ def is_system(msg):
17
+ return msg['role'].lower() == 'system'
18
+
19
+
20
+ def is_user(msg):
21
+ return msg['role'].lower() in ['human', 'user']
22
+
23
+
24
+ def is_assistant(msg):
25
+ return msg['role'].lower() == 'assistant'
26
+
27
+
28
+ def _convert_to_conversation(query, system=None):
29
+ conversation = []
30
+ if system:
31
+ conversation.append({"role": "SYSTEM", "content": system})
32
+ if isinstance(query, str):
33
+ conversation.append({"role": "HUMAN", "content": query})
34
+ elif isinstance(query, List):
35
+ conversation.extend(query)
36
+ elif isinstance(query, Dict):
37
+ if "messages" in query:
38
+ conversation.extend(query["messages"])
39
+ if "system_message" in query and len(conversation) > 0 and not is_system(conversation[0]):
40
+ conversation.insert(0, {"role": "SYSTEM", "content": query["system_message"]})
41
+ else:
42
+ conversation.append(query)
43
+ return conversation
44
+
45
+
46
+ class BailingTokenizer(PreTrainedTokenizerFast):
47
+ is_bailing_tokenizer = True
48
+ model_input_names = ["input_ids", "attention_mask"]
49
+ slow_tokenizer_class = None
50
+
51
+ # add gmask_token
52
+ SPECIAL_TOKENS_ATTRIBUTES = [
53
+ "bos_token",
54
+ "eos_token",
55
+ "unk_token",
56
+ "sep_token",
57
+ "pad_token",
58
+ "cls_token",
59
+ "mask_token",
60
+ "gmask_token",
61
+ "additional_special_tokens",
62
+ ]
63
+
64
+ def __init__(
65
+ self,
66
+ vocab_file=None,
67
+ merges_file=None,
68
+ tokenizer_file=None,
69
+ clean_up_tokenization_spaces=False,
70
+ bos_token="<|startoftext|>",
71
+ eos_token="<|endoftext|>",
72
+ cls_token="[CLS]",
73
+ pad_token="<|endoftext|>",
74
+ gmask_token="[gMASK]",
75
+ add_bos_token=False,
76
+ add_eos_token=False,
77
+ **kwargs,
78
+ ):
79
+ self.add_bos_token = add_bos_token
80
+
81
+ self._gmask_token = (
82
+ AddedToken(gmask_token, lstrip=False, rstrip=False, normalized=False)
83
+ if isinstance(gmask_token, str)
84
+ else gmask_token
85
+ )
86
+
87
+ self._sop_token = (
88
+ AddedToken(bos_token, lstrip=False, rstrip=False, normalized=False)
89
+ if isinstance(bos_token, str)
90
+ else bos_token
91
+ )
92
+
93
+ self._eop_token = (
94
+ AddedToken(eos_token, lstrip=False, rstrip=False, normalized=False)
95
+ if isinstance(eos_token, str)
96
+ else eos_token
97
+ )
98
+
99
+ super().__init__(
100
+ vocab_file=vocab_file,
101
+ merges_file=merges_file,
102
+ tokenizer_file=tokenizer_file,
103
+ clean_up_tokenization_spaces=clean_up_tokenization_spaces,
104
+ bos_token=bos_token,
105
+ eos_token=eos_token,
106
+ cls_token=cls_token,
107
+ pad_token=pad_token,
108
+ gmask_token=gmask_token,
109
+ add_bos_token=add_bos_token,
110
+ add_eos_token=add_eos_token,
111
+ **kwargs,
112
+ )
113
+
114
+ self.check_special_tokens()
115
+
116
+ def check_special_tokens(self):
117
+ '''
118
+ eos_token, cls_token, mask_token
119
+ special tokens should init, check special token is not None
120
+ '''
121
+ for name, special_token in zip(
122
+ ['eos', 'bos', 'cls', 'gmask'],
123
+ [self.eos_token, self.bos_token, self.cls_token, self.gmask_token],
124
+ ):
125
+ assert special_token is not None, f'should init special token [{name}] in tokenizer_config.json'
126
+
127
+ @property
128
+ def gmask_token(self) -> Optional[str]:
129
+ if self._gmask_token is None:
130
+ if self.verbose:
131
+ logger.error("Using gmask_token, but it is not set yet.")
132
+ return None
133
+ return str(self._gmask_token)
134
+
135
+ @gmask_token.setter
136
+ def gmask_token(self, value):
137
+ if not isinstance(value, (str, AddedToken)) and value is not None:
138
+ raise ValueError("Cannot set a non-string value as the gmask token")
139
+ self._gmask_token = value
140
+
141
+ @property
142
+ def gmask_token_id(self) -> Optional[int]:
143
+ if self._gmask_token is None:
144
+ return None
145
+ return self.convert_tokens_to_ids(self.gmask_token)
146
+
147
+ @property
148
+ def sop_token(self) -> Optional[str]:
149
+ if self._sop_token is None:
150
+ if self.verbose:
151
+ logger.error("Using sop_token, but it is not set yet.")
152
+ return None
153
+ return str(self._sop_token)
154
+
155
+ @sop_token.setter
156
+ def sop_token(self, value):
157
+ if not isinstance(value, (str, AddedToken)) and value is not None:
158
+ raise ValueError("Cannot set a non-string value as the sop token")
159
+ self._sop_token = value
160
+
161
+ @property
162
+ def sop_token_id(self) -> Optional[int]:
163
+ if self._sop_token is None:
164
+ return None
165
+ return self.convert_tokens_to_ids(self.sop_token)
166
+
167
+ @property
168
+ def eop_token(self) -> Optional[str]:
169
+ if self._eop_token is None:
170
+ if self.verbose:
171
+ logger.error("Using eop_token, but it is not set yet.")
172
+ return None
173
+ return str(self._eop_token)
174
+
175
+ @eop_token.setter
176
+ def eop_token(self, value):
177
+ if not isinstance(value, (str, AddedToken)) and value is not None:
178
+ raise ValueError("Cannot set a non-string value as the eop token")
179
+ self._eop_token = value
180
+
181
+ @property
182
+ def eop_token_id(self) -> Optional[int]:
183
+ if self._eop_token is None:
184
+ return None
185
+ return self.convert_tokens_to_ids(self.eop_token)
186
+
187
+ @property
188
+ def vocab_size(self):
189
+ return len(self.get_vocab())
190
+
191
+ def _chat_from_json(self, chat, chat_format="antglm_chat", system=None):
192
+ msgs = chat if "messages" not in chat else chat["messages"]
193
+ _msgs = []
194
+ sys_msg = None
195
+ for msg in msgs:
196
+ if is_system(msg):
197
+ sys_msg = msg['content']
198
+ else:
199
+ _msgs.append(msg)
200
+ chat = {"messages": _msgs}
201
+ system = system or sys_msg
202
+ if system:
203
+ chat['system_message'] = system
204
+ from .chat_format import Chat
205
+
206
+ return Chat.from_json(chat, name=chat_format)
207
+
208
+ def apply_chat_template(
209
+ self,
210
+ conversation: Union[List[Dict[str, str]], List[List[Dict[str, str]]]],
211
+ tools: Optional[List[Dict]] = None,
212
+ documents: Optional[List[Dict[str, str]]] = None,
213
+ chat_template: Optional[str] = None,
214
+ add_generation_prompt: bool = False,
215
+ system: str = None, # only used for legacy chatml
216
+ tokenize=False,
217
+ padding: bool = False,
218
+ truncation: bool = False,
219
+ max_length: Optional[int] = None,
220
+ return_tensors: Optional[Union[str, TensorType]] = None,
221
+ return_dict: bool = False,
222
+ return_assistant_tokens_mask: bool = False,
223
+ tokenizer_kwargs: Optional[Dict[str, Any]] = None,
224
+ **kwargs,
225
+ ):
226
+ if hasattr(self, "chat_template") and self.chat_template:
227
+ if isinstance(conversation, Dict) and "messages" in conversation:
228
+ conversation = conversation["messages"]
229
+ # use transformers built-in method
230
+ return super().apply_chat_template(
231
+ conversation=conversation,
232
+ tools=tools,
233
+ documents=documents,
234
+ chat_template=chat_template,
235
+ add_generation_prompt=add_generation_prompt,
236
+ tokenize=tokenize,
237
+ padding=padding,
238
+ truncation=truncation,
239
+ return_tensors=return_tensors,
240
+ return_dict=return_dict,
241
+ return_assistant_tokens_mask=return_assistant_tokens_mask,
242
+ tokenizer_kwargs=tokenizer_kwargs,
243
+ )
244
+
245
+ # 非chat_template方式后续将不再支持。
246
+ logger.warning("Please set chat_template in tokenizer_config.json!")
247
+
248
+ chat_format = kwargs.get('chat_format', 'antglm_chat')
249
+
250
+ is_batched = False
251
+
252
+ if isinstance(conversation, List) and (
253
+ isinstance(conversation[0], (list, tuple)) or "messages" in conversation[0]
254
+ ):
255
+ conversations = conversation
256
+ is_batched = True
257
+
258
+ if not is_batched:
259
+ conversations = [conversation]
260
+
261
+ rendered = []
262
+ for chat in conversations:
263
+ rendered_chat = self._chat_from_json(chat, chat_format=chat_format, system=system).prompt_str
264
+ rendered.append(rendered_chat)
265
+
266
+ if not is_batched:
267
+ rendered = rendered[0]
268
+
269
+ if tokenize:
270
+ out = self(
271
+ rendered,
272
+ padding=padding,
273
+ truncation=truncation,
274
+ max_length=max_length,
275
+ add_special_tokens=False,
276
+ return_tensors=return_tensors,
277
+ )
278
+ if return_dict:
279
+ return out
280
+ else:
281
+ return out["input_ids"]
282
+ else:
283
+ return rendered
284
+
285
+ def _build_position_ids(
286
+ self,
287
+ mask_pos: int,
288
+ bos_pos: int,
289
+ max_output_length: int,
290
+ rotary_type: Optional[str] = "none",
291
+ **kwargs,
292
+ ) -> List[List[int]]:
293
+ window_size = kwargs.get("window_size", 1024) - 1
294
+ block_position_ids = [0] * bos_pos
295
+
296
+ # 获得mask所在的位置,用于后面output positionid的构造
297
+ if "1d" in rotary_type:
298
+ position_ids = list(range(bos_pos)) + list(range(mask_pos + 1, mask_pos + max_output_length + 2))
299
+ block_position_ids = block_position_ids + list(range(1, max_output_length + 2))
300
+ elif "2d" in rotary_type:
301
+ # 后面input_ids要加一个bos_id
302
+ position_ids = list(range(bos_pos))
303
+ position_ids = position_ids + [mask_pos] * (1 + max_output_length)
304
+ block_position_ids = block_position_ids + list(range(1, max_output_length + 2))
305
+ else:
306
+ # build position ids
307
+ position_ids = []
308
+ repeat_times = bos_pos // window_size
309
+ for _ in range(repeat_times):
310
+ position_ids += list(range(window_size))
311
+ position_ids += list(range(bos_pos - window_size * repeat_times))
312
+ # need consider additional bos_id after input_ids
313
+ mask_pos = position_ids[-1]
314
+ position_ids += [mask_pos] * (max_output_length + 1)
315
+
316
+ block_repeat_times = max_output_length // (window_size - 1)
317
+ additional_block_position_ids = []
318
+ for _ in range(block_repeat_times):
319
+ additional_block_position_ids += list(range(1, window_size))
320
+ additional_block_position_ids += list(
321
+ range(1, max_output_length + 2 - (window_size - 1) * block_repeat_times)
322
+ )
323
+ block_position_ids = block_position_ids + additional_block_position_ids
324
+
325
+ position_ids = [position_ids, block_position_ids]
326
+ return position_ids
327
+
328
+ def _build_inputs_for_generation(
329
+ self,
330
+ input_ids: List[int],
331
+ max_input_length=None,
332
+ left_truncate=True,
333
+ max_output_length=1024,
334
+ rotary_type="none",
335
+ unidirectional_attention: bool = True,
336
+ attention_dtype=None,
337
+ **kwargs,
338
+ ):
339
+ if max_input_length and len(input_ids) > max_input_length:
340
+ if left_truncate:
341
+ input_ids = input_ids[-max_input_length:]
342
+ else:
343
+ input_ids = input_ids[:max_input_length]
344
+
345
+ is_left_padding = input_ids[0] == self.eos_token_id
346
+ if not unidirectional_attention:
347
+ if input_ids[0] != self.cls_token_id:
348
+ input_ids = [self.cls_token_id] + input_ids
349
+
350
+ if self.gmask_token_id not in set(input_ids):
351
+ input_ids = input_ids + [self.gmask_token_id]
352
+
353
+ mask_pos = input_ids.index(self.gmask_token_id)
354
+ sep = len(input_ids)
355
+ else:
356
+ if self.add_bos_token:
357
+ input_ids = input_ids + [self.bos_token_id]
358
+ if self.eos_token_id in input_ids:
359
+ mask_pos = input_ids.index(self.eos_token_id) - 1
360
+ else:
361
+ mask_pos = len(input_ids) - 1
362
+ sep = len(input_ids) - 1
363
+ else:
364
+ sep = len(input_ids)
365
+ if self.eos_token_id in input_ids:
366
+ if is_left_padding:
367
+ ori_input_ids = input_ids
368
+ input_ids = input_ids[::-1]
369
+ mask_pos = input_ids.index(self.eos_token_id) - 1
370
+ mask_pos = max(0, mask_pos) # for empty sequence
371
+ if is_left_padding:
372
+ input_ids = ori_input_ids
373
+ mask_pos = sep - 1 - mask_pos # the first non-eos token
374
+
375
+ else:
376
+ mask_pos = len(input_ids) - 1
377
+
378
+ position_ids = self._build_position_ids(mask_pos, sep, max_output_length, rotary_type, **kwargs)
379
+
380
+ if is_left_padding:
381
+ position_ids[0] = [max(0, i - mask_pos) for i in range(len(position_ids[0]))]
382
+
383
+ # 后面input_ids要加一个bos_id
384
+ total_length = sep + max_output_length
385
+ if self.add_bos_token:
386
+ total_length += 1
387
+
388
+ def build_mask_matrix(seq_length, sep, mask_pos, unidirectional_attention):
389
+ # 长序列使用bool类型节省显存
390
+ if unidirectional_attention:
391
+ attention_mask = torch.ones([seq_length, seq_length], dtype=attention_dtype)
392
+ attention_mask = torch.tril(attention_mask)
393
+ if is_left_padding:
394
+ attention_mask[:, :mask_pos] = 0
395
+ else:
396
+ attention_mask[:, mask_pos + 1 : sep] = 0
397
+ else:
398
+ attention_mask = torch.zeros([seq_length, seq_length], dtype=attention_dtype)
399
+ attention_mask[:, : mask_pos + 1] = 1
400
+ for i in range(sep, total_length):
401
+ attention_mask[i, sep : i + 1] = 1
402
+ return attention_mask
403
+
404
+ if self.add_bos_token:
405
+ attention_mask = build_mask_matrix(total_length, sep + 1, mask_pos, unidirectional_attention)
406
+ else:
407
+ attention_mask = build_mask_matrix(total_length, sep, mask_pos, unidirectional_attention)
408
+ attention_mask = torch.unsqueeze(attention_mask, dim=0)
409
+ attention_mask = torch.unsqueeze(attention_mask, dim=1)
410
+ if attention_dtype is None:
411
+ attention_mask = attention_mask.long()
412
+ inputs = {
413
+ "input_ids": torch.Tensor([input_ids]).long(),
414
+ "position_ids": torch.Tensor([position_ids]).long(),
415
+ "attention_mask": attention_mask,
416
+ }
417
+ return BatchEncoding(inputs)
418
+
419
+ def build_inputs_for_generation(
420
+ self,
421
+ input_ids: Union[List[int], List[List[int]], torch.Tensor],
422
+ max_input_length=None,
423
+ left_truncate=True,
424
+ max_output_length=1024,
425
+ rotary_type="1d",
426
+ unidirectional_attention=True,
427
+ attention_dtype=None,
428
+ **kwargs,
429
+ ):
430
+ if isinstance(input_ids, torch.Tensor):
431
+ input_ids = input_ids.tolist()
432
+
433
+ if isinstance(input_ids[0], list):
434
+ input_ids_list = []
435
+ position_ids_list = []
436
+ attention_mask_list = []
437
+ for _input_ids in input_ids:
438
+ inputs = self._build_inputs_for_generation(
439
+ _input_ids,
440
+ max_input_length=max_input_length,
441
+ left_truncate=left_truncate,
442
+ max_output_length=max_output_length,
443
+ rotary_type=rotary_type,
444
+ unidirectional_attention=unidirectional_attention,
445
+ attention_dtype=attention_dtype,
446
+ **kwargs,
447
+ )
448
+ input_ids_list.append(inputs['input_ids'])
449
+ position_ids_list.append(inputs['position_ids'])
450
+ attention_mask_list.append(inputs["attention_mask"])
451
+
452
+ max_ids_length = max([input.size(1) for input in input_ids_list])
453
+
454
+ for i in range(len(input_ids)):
455
+ cur_ids_length = input_ids_list[i].size(1)
456
+ if cur_ids_length < max_ids_length:
457
+ # pad input ids
458
+ pad_input_ids = input_ids_list[i].new_zeros((1, max_ids_length - cur_ids_length))
459
+ input_ids_list[i] = torch.cat([pad_input_ids, input_ids_list[i]], dim=-1)
460
+
461
+ # pad postition ids with left pad
462
+ # 0, 1, 2, 3, 4 ... -> 0, ..., 0, 1, 2, 3, 4, ...
463
+ pad_position_ids = input_ids_list[i].new_zeros((1, 2, max_ids_length - cur_ids_length))
464
+ position_ids_list[i] = torch.cat([pad_position_ids, position_ids_list[i]], dim=-1)
465
+
466
+ # pad generation attention mask with left and bottom pad
467
+ new_attention_mask = input_ids_list[i].new_zeros(
468
+ 1,
469
+ 1,
470
+ max_ids_length + max_output_length,
471
+ max_ids_length + max_output_length,
472
+ )
473
+ new_attention_mask[
474
+ :,
475
+ :,
476
+ max_ids_length - cur_ids_length :,
477
+ max_ids_length - cur_ids_length :,
478
+ ] = attention_mask_list[i]
479
+ attention_mask_list[i] = new_attention_mask.contiguous()
480
+
481
+ input_ids_list = torch.cat(input_ids_list, dim=0)
482
+ position_ids_list = torch.cat(position_ids_list, dim=0)
483
+ attention_mask_list = torch.cat(attention_mask_list, dim=0)
484
+
485
+ inputs = {
486
+ "input_ids": input_ids_list,
487
+ "position_ids": position_ids_list,
488
+ "attention_mask": attention_mask_list,
489
+ }
490
+
491
+ return BatchEncoding(inputs)
492
+ else:
493
+ return self._build_inputs_for_generation(
494
+ input_ids,
495
+ max_input_length=max_input_length,
496
+ left_truncate=left_truncate,
497
+ max_output_length=max_output_length,
498
+ rotary_type=rotary_type,
499
+ unidirectional_attention=unidirectional_attention,
500
+ **kwargs,
501
+ )
502
+
503
+ def _build_inputs_for_train(
504
+ self,
505
+ inputs: Union[str, List[str]],
506
+ outputs: Union[str, List[str]],
507
+ new_conversation_offset: List[int] = None,
508
+ max_length: int = 2048,
509
+ rotary_type: str = "1d",
510
+ left_truncate: bool = True,
511
+ unidirectional_attention: bool = True,
512
+ isolation_position_ids: bool = False,
513
+ padding: bool = True,
514
+ use_fa2: bool = True,
515
+ use_packed: bool = True,
516
+ use_baichuan_packed: bool = False,
517
+ skip_truncated_turn: bool = False,
518
+ return_attention_mask: bool = True,
519
+ ):
520
+ r"""
521
+ Build tensor input for model training. If inputs and outputs are list, will pack them.
522
+
523
+ Args:
524
+ inputs (str, List[str], List[Dict], List[List[Dict]]): the input prompts.
525
+ outputs (str, List[str]): the output responses.
526
+ max_length (int, Optional): the maximum length of the final input ids for training. Default: 2048
527
+ rotary_type (str, Optional): the rotary type of position embedding. Default: 1d
528
+ left_truncate (bool, Optional): whether truncate the inputs from left. Default: True
529
+ use_fa2 (bool, Optional): whether to build attention mask under flash attention 2.
530
+ new_conversation_offset (List[int], Optional): 第idx条样本是全新的对话,[0, 1]代表:inputs[0]和outputs[0]是一个对话,inputs[1]和outputs[1]是一个对话.
531
+ """
532
+ if use_packed and use_baichuan_packed and unidirectional_attention:
533
+ return self._build_baichuan_inputs_for_train(
534
+ inputs,
535
+ outputs,
536
+ new_conversation_offset,
537
+ max_length,
538
+ rotary_type,
539
+ left_truncate,
540
+ skip_truncated_turn,
541
+ use_fa2,
542
+ padding,
543
+ )
544
+ if isinstance(inputs, str):
545
+ inputs = [inputs]
546
+ if isinstance(outputs, str):
547
+ outputs = [outputs]
548
+
549
+ assert len(inputs) == len(outputs)
550
+
551
+ input_ids = [self(item)['input_ids'] for item in inputs]
552
+ output_ids = [self(item)['input_ids'] for item in outputs]
553
+
554
+ packed_input_ids = []
555
+ packed_output_ids = []
556
+ if new_conversation_offset is None:
557
+ new_conversation_offset = list(range(0, len(inputs)))
558
+ assert 0 in new_conversation_offset, f"没有0,请检查new_conversation_offset: {new_conversation_offset}"
559
+ current_len = 0
560
+
561
+ for idx, (input, output) in enumerate(zip(input_ids, output_ids)):
562
+ num_special_tokens = 0
563
+ if not unidirectional_attention:
564
+ if idx in new_conversation_offset:
565
+ # cls and gmask
566
+ num_special_tokens += 2
567
+ else:
568
+ # only gmask
569
+ num_special_tokens += 1
570
+ else:
571
+ # sop and eos
572
+ if self.add_bos_token:
573
+ num_special_tokens += 2
574
+ else:
575
+ num_special_tokens += 1
576
+
577
+ # truncate
578
+ if len(input) + len(output) + current_len > max_length - num_special_tokens:
579
+ if not use_packed or use_fa2 and unidirectional_attention:
580
+ attention_mask = torch.tensor(0)
581
+ elif use_fa2:
582
+ attention_mask = -1 * torch.ones([2, max_length])
583
+ else:
584
+ attention_mask = torch.tril(torch.ones([max_length, max_length]))
585
+ # 返回一个空的样本,该样本不参与训练
586
+ default_return = {
587
+ 'input_ids': (torch.ones(max_length) * self.eos_token_id).long(),
588
+ 'position_ids': torch.zeros(2, max_length).long(),
589
+ 'attention_mask': (attention_mask.long()),
590
+ 'labels': (torch.ones(max_length) * -100).long(),
591
+ }
592
+ # 如果不截断,直接返回
593
+ if skip_truncated_turn:
594
+ if current_len == 0:
595
+ return default_return
596
+ else:
597
+ break
598
+ left_len = max_length - num_special_tokens - current_len
599
+ # 如果截断,只截断prompt
600
+ if left_len - len(output) > 0:
601
+ if left_truncate:
602
+ input = input[-(left_len - len(output)) :]
603
+ else:
604
+ input = input[: left_len - len(output)]
605
+ else:
606
+ # response超过left_len,直接返回
607
+ if current_len == 0:
608
+ return default_return
609
+ else:
610
+ break
611
+ if unidirectional_attention:
612
+ packed_input_ids.append(list(input))
613
+ else:
614
+ if num_special_tokens == 4:
615
+ packed_input_ids.append([self.cls_token_id] + list(input) + [self.gmask_token_id])
616
+ else:
617
+ packed_input_ids.append(list(input) + [self.gmask_token_id])
618
+
619
+ packed_output_ids.append(list(output) + [self.eos_token_id])
620
+ current_len += len(input) + len(output) + num_special_tokens
621
+
622
+ assert current_len <= max_length
623
+
624
+ if use_packed:
625
+ # pack模式
626
+ def build_mask_matrix(seq_length, sep):
627
+ # https://github.com/pytorch/pytorch/issues/101932, fix triu/tril bf16 support
628
+ m = torch.ones((1, seq_length, seq_length))
629
+ mask = torch.arange(1, m.shape[-1] + 1).reshape(1, -1, 1).to(m.device)
630
+ ids = torch.arange(1, m.shape[-1] + 1).reshape(1, 1, -1).expand(1, m.shape[-1], -1).to(m.device)
631
+ m = (ids <= mask).type_as(m)
632
+
633
+ m[0, :, : int(sep)] = 1
634
+ m = m.squeeze(0)
635
+ return m
636
+
637
+ tokens = []
638
+ attention_mask_list = []
639
+ input_length_list = []
640
+ position_id_list = []
641
+ block_position_id_list = []
642
+ for input, output in zip(packed_input_ids, packed_output_ids):
643
+ if self.add_bos_token:
644
+ data = input + [self.sop_token_id] + output
645
+ mask_pos = len(input) - 1
646
+ else:
647
+ data = input + output
648
+ mask_pos = len(input) - 2
649
+ if return_attention_mask:
650
+ if unidirectional_attention:
651
+ attention_mask = build_mask_matrix(len(data), 0)
652
+ else:
653
+ attention_mask = build_mask_matrix(len(data), len(input))
654
+ attention_mask = attention_mask.squeeze((0, 1))
655
+
656
+ attention_mask_list.append(attention_mask)
657
+ input_length_list.append(len(input))
658
+ tokens += data
659
+
660
+ sop_pos = mask_pos + 1
661
+ position_ids, block_position_ids = self._build_position_ids(
662
+ mask_pos=mask_pos, bos_pos=sop_pos, max_output_length=len(output), rotary_type=rotary_type
663
+ )
664
+
665
+ position_id_list.append(position_ids)
666
+ block_position_id_list.append(block_position_ids)
667
+
668
+ labels = []
669
+ for i in range(len(packed_input_ids)):
670
+ if self.add_bos_token:
671
+ labels += [-100] * len(packed_input_ids[i]) + packed_output_ids[i] + [-100]
672
+ else:
673
+ labels += [-100] * (len(packed_input_ids[i]) - 1) + packed_output_ids[i] + [-100]
674
+
675
+ total_len = 0
676
+ if use_fa2:
677
+ pack_attention_mask = -1 * torch.ones([2, current_len])
678
+ else:
679
+ pack_attention_mask = torch.tril(torch.ones([current_len, current_len]))
680
+
681
+ pack_position_ids = []
682
+ pack_block_position_ids = []
683
+ total_len = 0
684
+ max_index = 0
685
+ for i in range(len(position_id_list)):
686
+
687
+ if use_fa2:
688
+ pack_attention_mask[0][i] = total_len
689
+ pack_attention_mask[1][i] = total_len + input_length_list[i]
690
+ else:
691
+ pack_attention_mask[
692
+ total_len : total_len + attention_mask.shape[0],
693
+ total_len : total_len + attention_mask.shape[0],
694
+ ] = attention_mask
695
+ position_ids = [pid + max_index for pid in position_id_list[i]]
696
+ block_position_ids = block_position_id_list[i]
697
+ pack_position_ids.extend(position_ids)
698
+ pack_block_position_ids.extend(block_position_ids)
699
+ if not isolation_position_ids:
700
+ max_index = pack_position_ids[-1] + 1
701
+ total_len += len(position_id_list[i])
702
+ position_ids = [pack_position_ids, pack_block_position_ids]
703
+ else:
704
+ # 单输入模式
705
+ # 真多轮下,一条样本可能会有好几轮对话,此时需要获取第一条样本的结束位置
706
+ if len(new_conversation_offset) > 1:
707
+ end_idx = new_conversation_offset[1]
708
+ else:
709
+ end_idx = 1
710
+ input, output = list(itertools.chain(*packed_input_ids[:end_idx])), list(
711
+ itertools.chain(*packed_output_ids[:end_idx])
712
+ )
713
+ if self.add_bos_token:
714
+ tokens = input + [self.sop_token_id] + output
715
+ else:
716
+ tokens = input + output
717
+
718
+ if self.add_bos_token:
719
+ labels = [-100] * len(input) + output + [-100]
720
+ position_ids = self._build_position_ids(
721
+ mask_pos=len(input) - 1, bos_pos=len(input), max_output_length=len(output), rotary_type=rotary_type
722
+ )
723
+ else:
724
+ labels = [-100] * (len(input) - 1) + output + [-100]
725
+ position_ids = self._build_position_ids(
726
+ mask_pos=len(input) - 2,
727
+ bos_pos=len(input) - 1,
728
+ max_output_length=len(output),
729
+ rotary_type=rotary_type,
730
+ )
731
+ attention_mask = len(input)
732
+ assert current_len == len(tokens)
733
+
734
+ # 最大长度补全
735
+ if max_length > 0 and len(tokens) < max_length and padding:
736
+ pad_length = max_length - len(tokens)
737
+ tokens += [self.pad_token_id] * pad_length
738
+ labels.extend([-100] * pad_length)
739
+ position_ids[0] += [0] * pad_length
740
+ position_ids[1] += [0] * pad_length
741
+
742
+ if use_packed:
743
+ if use_fa2:
744
+ new_attention_mask = -1 * torch.ones([2, max_length])
745
+ new_attention_mask[:, :current_len] = pack_attention_mask
746
+ else:
747
+ new_attention_mask = torch.tril(torch.ones([max_length, max_length]))
748
+ new_attention_mask[:current_len, :current_len] = pack_attention_mask
749
+ pack_attention_mask = new_attention_mask.contiguous()
750
+
751
+ assert len(tokens) == len(labels)
752
+
753
+ if max_length > 0 and padding:
754
+ assert len(tokens) == max_length
755
+
756
+ if use_fa2 and unidirectional_attention:
757
+ # pack_attention_mask = torch.zeros([1], dtype=torch.long)
758
+ pack_attention_mask = torch.tensor(0)
759
+
760
+ if use_packed:
761
+ if not use_fa2:
762
+ attention_mask = pack_attention_mask.unsqueeze(0).long()
763
+ else:
764
+ attention_mask = pack_attention_mask
765
+ else:
766
+ attention_mask = torch.tensor(attention_mask).long()
767
+ return {
768
+ 'input_ids': torch.tensor(tokens).long(),
769
+ 'position_ids': torch.tensor(position_ids).long(),
770
+ 'attention_mask': attention_mask,
771
+ 'labels': torch.tensor(labels).long(),
772
+ }
773
+
774
+ def _build_baichuan_inputs_for_train(
775
+ self,
776
+ inputs: Union[str, List[str]],
777
+ outputs: Union[str, List[str]],
778
+ new_conversation_offset: List[int] = None,
779
+ max_length: int = 2048,
780
+ rotary_type: str = "1d",
781
+ left_truncate: bool = True,
782
+ skip_truncated_turn: bool = True,
783
+ use_fa2: bool = True,
784
+ padding: bool = True,
785
+ ):
786
+ '''
787
+ input: <role> HUMAN </role> u1 <role> ASSISTANT </role> a11 a12 <role> HUMAN </role> u2 <role> ASSISTANT </role> a21 a22 <|endoftext|> <role> HUMAN </role> u1 <role> ASSISTANT </role> a11 a12 <role> HUMAN </role> u2 <role> ASSISTANT </role> a21 a22 <|endoftext|>
788
+ output: x x x x x x a11 a12 <|endoftext|> x x x x x x a21 a22 <|endoftext|> x x x x x x x a11 a12 <|endoftext|> x x x x x x a21 a22 <|endoftext|> x
789
+ 只适用真多轮+pack数据训练单向模型,需要打开use_true_multiturn
790
+ '''
791
+ if isinstance(inputs, str):
792
+ inputs = [inputs]
793
+ if isinstance(outputs, str):
794
+ outputs = [outputs]
795
+ assert len(inputs) == len(outputs)
796
+
797
+ input_ids = [self(item)['input_ids'] for item in inputs]
798
+ output_ids = [self(item)['input_ids'] for item in outputs]
799
+
800
+ packed_input_ids = []
801
+ packed_output_ids = []
802
+
803
+ if new_conversation_offset is None:
804
+ new_conversation_offset = list(range(0, len(inputs)))
805
+ assert 0 in new_conversation_offset, f"没有0,请检查new_conversation_offset: {new_conversation_offset}"
806
+ current_len = 0
807
+
808
+ for idx, (input, output) in enumerate(zip(input_ids, output_ids)):
809
+ num_special_tokens = 0
810
+ if idx != 0 and idx in new_conversation_offset:
811
+ # 在input_ids加入eos,只有第0条样本不加
812
+ num_special_tokens += 1
813
+
814
+ # truncate
815
+ if len(input) + len(output) + current_len > max_length - num_special_tokens:
816
+ if use_fa2:
817
+ attention_mask = torch.tensor(0)
818
+ else:
819
+ attention_mask = torch.tril(torch.ones([max_length, max_length]))
820
+ # 返回一个空的样本,该样本不参与训练
821
+ default_return = {
822
+ 'input_ids': (torch.ones(max_length) * self.eos_token_id).long(),
823
+ 'position_ids': torch.zeros(2, max_length).long(),
824
+ 'attention_mask': (attention_mask.long()),
825
+ 'labels': (torch.ones(max_length) * -100).long(),
826
+ }
827
+
828
+ # 如果不截断,直接返回
829
+ if skip_truncated_turn:
830
+ if current_len == 0:
831
+ return default_return
832
+ else:
833
+ break
834
+ left_len = max_length - num_special_tokens - current_len
835
+ # 如果截断,只截断prompt
836
+ if left_len - len(output) > 0:
837
+ if left_truncate:
838
+ input = input[-(left_len - len(output)) :]
839
+ else:
840
+ input = input[: left_len - len(output)]
841
+ else:
842
+ # response超过left_len,直接返回
843
+ if current_len == 0:
844
+ return default_return
845
+ else:
846
+ break
847
+ # 这里拼的是input_ids
848
+ if num_special_tokens == 1:
849
+ packed_input_ids.append([self.eos_token_id] + list(input))
850
+ else:
851
+ packed_input_ids.append(list(input))
852
+ packed_output_ids.append(list(output))
853
+ current_len += len(input) + len(output) + num_special_tokens
854
+ assert current_len <= max_length
855
+
856
+ def build_mask_matrix(seq_length, sep):
857
+ # https://github.com/pytorch/pytorch/issues/101932, fix triu/tril bf16 support
858
+ m = torch.ones((1, seq_length, seq_length))
859
+ mask = torch.arange(1, m.shape[-1] + 1).reshape(1, -1, 1).to(m.device)
860
+ ids = torch.arange(1, m.shape[-1] + 1).reshape(1, 1, -1).expand(1, m.shape[-1], -1).to(m.device)
861
+ m = (ids <= mask).type_as(m)
862
+
863
+ m[0, :, : int(sep)] = 1
864
+ m = m.squeeze(0)
865
+ return m
866
+
867
+ tokens = []
868
+ attention_mask_list = []
869
+ position_id_list = []
870
+ block_position_id_list = []
871
+ token_lens = []
872
+ for input, output in zip(packed_input_ids, packed_output_ids):
873
+ data = input + output
874
+ if not use_fa2:
875
+ attention_mask = build_mask_matrix(len(data), 0)
876
+ attention_mask_list.append(attention_mask)
877
+ tokens += data
878
+ token_lens.append(len(data))
879
+
880
+ position_ids, block_position_ids = self._build_position_ids(
881
+ mask_pos=len(input) - 2, bos_pos=len(input) - 1, max_output_length=len(output), rotary_type=rotary_type
882
+ )
883
+
884
+ position_id_list.append(position_ids)
885
+ block_position_id_list.append(block_position_ids)
886
+
887
+ labels = []
888
+ for i in range(len(packed_input_ids)):
889
+ labels += [-100] * (len(packed_input_ids[i]) - 1) + packed_output_ids[i] + [self.eos_token_id]
890
+
891
+ total_len = 0
892
+ if use_fa2:
893
+ pack_attention_mask = torch.Tensor([[0], [1]])
894
+ else:
895
+ pack_attention_mask = torch.tril(torch.ones([max_length, max_length]))
896
+
897
+ pack_position_ids = []
898
+ pack_block_position_ids = []
899
+ total_len = 0
900
+ max_index = 0
901
+ for i in range(len(token_lens)):
902
+ if not use_fa2:
903
+ attention_mask = attention_mask_list[i]
904
+ pack_attention_mask[
905
+ total_len : total_len + attention_mask.shape[0], total_len : total_len + attention_mask.shape[0]
906
+ ] = attention_mask
907
+ position_ids = [pid + max_index for pid in position_id_list[i]]
908
+ block_position_ids = block_position_id_list[i]
909
+ pack_position_ids.extend(position_ids)
910
+ pack_block_position_ids.extend(block_position_ids)
911
+ max_index = pack_position_ids[-1] + 1
912
+ total_len += token_lens[i]
913
+ position_ids = [pack_position_ids, pack_block_position_ids]
914
+
915
+ if max_length > 0 and len(tokens) < max_length and padding:
916
+ pad_length = max_length - len(tokens)
917
+ tokens += [self.pad_token_id] * pad_length
918
+ labels.extend([-100] * pad_length)
919
+ position_ids[0] += [0] * pad_length
920
+ position_ids[1] += [0] * pad_length
921
+
922
+ assert len(tokens) == len(labels)
923
+
924
+ if not use_fa2:
925
+ attention_mask = pack_attention_mask.unsqueeze(0).long()
926
+ else:
927
+ attention_mask = torch.tensor(0)
928
+ return {
929
+ 'input_ids': torch.tensor(tokens).long(),
930
+ 'position_ids': torch.tensor(position_ids).long(),
931
+ 'attention_mask': attention_mask,
932
+ 'labels': torch.tensor(labels).long(),
933
+ }
934
+
935
+ def build_inputs_for_train(
936
+ self,
937
+ data: Union[Dict, List[Dict]],
938
+ new_conversation_offset: List[int] = None,
939
+ chat_format="antglm_chat",
940
+ is_chat_format=True, # 如果传入的是字符串,用于说明是否已经是
941
+ use_true_multiturn=False,
942
+ max_length: int = 2048,
943
+ rotary_type: str = "1d",
944
+ left_truncate: bool = True,
945
+ unidirectional_attention: bool = True,
946
+ isolation_position_ids: bool = False,
947
+ padding: bool = True,
948
+ use_fa2: bool = True,
949
+ use_packed: bool = True,
950
+ use_baichuan_packed: bool = False,
951
+ skip_truncated_turn: bool = False,
952
+ return_attention_mask: bool = True,
953
+ ):
954
+ r"""
955
+ Build tensor input for model training. If inputs and outputs are list, will pack them.
956
+
957
+ Args:
958
+ inputs (str, List[str], List[Dict], List[List[Dict]]): the input prompts.
959
+ outputs (str, List[str]): the output responses.
960
+ new_conversation_offset (List[int]): the offset index of the new conversation turn.
961
+ is_chat_format (bool): whether the input is already chatml format
962
+ max_length (int, Optional): the maximum length of the final input ids for training. Default: 2048
963
+ rotary_type (str, Optional): the rotary type of position embedding. Default: 1d
964
+ left_truncate (bool, Optional): whether truncate the inputs from left. Default: True
965
+ use_fa2 (bool, Optional): whether to build attention mask under flash attention 2.
966
+ """
967
+ if isinstance(data, List):
968
+ # chatml list
969
+ _inputs = []
970
+ _outputs = []
971
+ new_conversation_offset = []
972
+ for _input in data:
973
+ if use_true_multiturn:
974
+ chat = self._chat_from_json(_input, chat_format=chat_format)
975
+ chat_data = chat.prompt_pack
976
+ new_conversation_offset.append(len(_inputs))
977
+ _inputs.extend(chat_data['input'])
978
+ _outputs.extend(chat_data['output'])
979
+ else:
980
+ _conversation = _convert_to_conversation(_input)
981
+ assert is_assistant(_conversation[-1])
982
+
983
+ _inputs.append(
984
+ self.apply_chat_template(_conversation[:-1], tokenize=False, add_generation_prompt=True)
985
+ )
986
+ _outputs.append(_conversation[-1]['content'])
987
+
988
+ return self._build_inputs_for_train(
989
+ inputs=_inputs,
990
+ outputs=_outputs,
991
+ new_conversation_offset=new_conversation_offset,
992
+ max_length=max_length,
993
+ rotary_type=rotary_type,
994
+ left_truncate=left_truncate,
995
+ unidirectional_attention=unidirectional_attention,
996
+ isolation_position_ids=isolation_position_ids,
997
+ padding=padding,
998
+ use_fa2=use_fa2,
999
+ use_packed=use_packed,
1000
+ use_baichuan_packed=use_baichuan_packed,
1001
+ skip_truncated_turn=skip_truncated_turn,
1002
+ return_attention_mask=return_attention_mask,
1003
+ )
1004
+ elif isinstance(data, Dict):
1005
+ if 'messages' in data:
1006
+ # chatml format
1007
+ if use_true_multiturn:
1008
+ chat = self._chat_from_json(data, chat_format=chat_format)
1009
+ chat_data = chat.prompt_pack
1010
+ else:
1011
+ _conversation = _convert_to_conversation(data)
1012
+ assert is_assistant(_conversation[-1])
1013
+
1014
+ chat_data = {
1015
+ "input": self.apply_chat_template(
1016
+ _conversation[:-1], tokenize=False, add_generation_prompt=True
1017
+ ),
1018
+ "output": _conversation[-1]['content'],
1019
+ }
1020
+
1021
+ return self._build_inputs_for_train(
1022
+ inputs=chat_data['input'],
1023
+ outputs=chat_data['output'],
1024
+ max_length=max_length,
1025
+ rotary_type=rotary_type,
1026
+ left_truncate=left_truncate,
1027
+ unidirectional_attention=unidirectional_attention,
1028
+ isolation_position_ids=isolation_position_ids,
1029
+ padding=padding,
1030
+ use_fa2=use_fa2,
1031
+ use_packed=use_packed,
1032
+ use_baichuan_packed=use_baichuan_packed,
1033
+ skip_truncated_turn=skip_truncated_turn,
1034
+ return_attention_mask=return_attention_mask,
1035
+ )
1036
+ else:
1037
+ inputs = data['input']
1038
+ outputs = data['output']
1039
+
1040
+ if isinstance(inputs, str):
1041
+ inputs = [inputs]
1042
+ if isinstance(outputs, str):
1043
+ outputs = [outputs]
1044
+
1045
+ if not is_chat_format and chat_format:
1046
+ inputs = [
1047
+ self.apply_chat_template(
1048
+ [{"role": "HUMAN", "content": item}], tokenize=False, chat_format=chat_format
1049
+ )
1050
+ for item in inputs
1051
+ ]
1052
+
1053
+ return self._build_inputs_for_train(
1054
+ inputs=inputs,
1055
+ outputs=outputs,
1056
+ new_conversation_offset=new_conversation_offset,
1057
+ max_length=max_length,
1058
+ rotary_type=rotary_type,
1059
+ left_truncate=left_truncate,
1060
+ unidirectional_attention=unidirectional_attention,
1061
+ isolation_position_ids=isolation_position_ids,
1062
+ padding=padding,
1063
+ use_fa2=use_fa2,
1064
+ use_packed=use_packed,
1065
+ use_baichuan_packed=use_baichuan_packed,
1066
+ skip_truncated_turn=skip_truncated_turn,
1067
+ return_attention_mask=return_attention_mask,
1068
+ )
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fdcadf59ad1db38dde175f2a82d3ec2dde15986ac1f81aef69c5cdd03afc6e1b
3
+ size 12205847
tokenizer_config.json ADDED
@@ -0,0 +1,2124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_eos_token": false,
4
+ "added_tokens_decoder": {
5
+ "156891": {
6
+ "content": "<|startoftext|>",
7
+ "lstrip": false,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false,
11
+ "special": true
12
+ },
13
+ "156892": {
14
+ "content": "<|endoftext|>",
15
+ "lstrip": false,
16
+ "normalized": false,
17
+ "rstrip": false,
18
+ "single_word": false,
19
+ "special": true
20
+ },
21
+ "156893": {
22
+ "content": "[CLS]",
23
+ "lstrip": false,
24
+ "normalized": false,
25
+ "rstrip": false,
26
+ "single_word": false,
27
+ "special": true
28
+ },
29
+ "156894": {
30
+ "content": "[gMASK]",
31
+ "lstrip": false,
32
+ "normalized": false,
33
+ "rstrip": false,
34
+ "single_word": false,
35
+ "special": true
36
+ },
37
+ "156895": {
38
+ "content": "<|reserved_token_0|>",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false,
43
+ "special": true
44
+ },
45
+ "156896": {
46
+ "content": "<|reserved_token_1|>",
47
+ "lstrip": false,
48
+ "normalized": false,
49
+ "rstrip": false,
50
+ "single_word": false,
51
+ "special": true
52
+ },
53
+ "156897": {
54
+ "content": "<|reserved_token_2|>",
55
+ "lstrip": false,
56
+ "normalized": false,
57
+ "rstrip": false,
58
+ "single_word": false,
59
+ "special": true
60
+ },
61
+ "156898": {
62
+ "content": "<|reserved_token_3|>",
63
+ "lstrip": false,
64
+ "normalized": false,
65
+ "rstrip": false,
66
+ "single_word": false,
67
+ "special": true
68
+ },
69
+ "156899": {
70
+ "content": "<|reserved_token_4|>",
71
+ "lstrip": false,
72
+ "normalized": false,
73
+ "rstrip": false,
74
+ "single_word": false,
75
+ "special": true
76
+ },
77
+ "156900": {
78
+ "content": "<|reserved_token_5|>",
79
+ "lstrip": false,
80
+ "normalized": false,
81
+ "rstrip": false,
82
+ "single_word": false,
83
+ "special": true
84
+ },
85
+ "156901": {
86
+ "content": "<|reserved_token_6|>",
87
+ "lstrip": false,
88
+ "normalized": false,
89
+ "rstrip": false,
90
+ "single_word": false,
91
+ "special": true
92
+ },
93
+ "156902": {
94
+ "content": "<|reserved_token_7|>",
95
+ "lstrip": false,
96
+ "normalized": false,
97
+ "rstrip": false,
98
+ "single_word": false,
99
+ "special": true
100
+ },
101
+ "156903": {
102
+ "content": "<|reserved_token_8|>",
103
+ "lstrip": false,
104
+ "normalized": false,
105
+ "rstrip": false,
106
+ "single_word": false,
107
+ "special": true
108
+ },
109
+ "156904": {
110
+ "content": "<|reserved_token_9|>",
111
+ "lstrip": false,
112
+ "normalized": false,
113
+ "rstrip": false,
114
+ "single_word": false,
115
+ "special": true
116
+ },
117
+ "156905": {
118
+ "content": "<|reserved_token_10|>",
119
+ "lstrip": false,
120
+ "normalized": false,
121
+ "rstrip": false,
122
+ "single_word": false,
123
+ "special": true
124
+ },
125
+ "156906": {
126
+ "content": "<|reserved_token_11|>",
127
+ "lstrip": false,
128
+ "normalized": false,
129
+ "rstrip": false,
130
+ "single_word": false,
131
+ "special": true
132
+ },
133
+ "156907": {
134
+ "content": "<|reserved_token_12|>",
135
+ "lstrip": false,
136
+ "normalized": false,
137
+ "rstrip": false,
138
+ "single_word": false,
139
+ "special": true
140
+ },
141
+ "156908": {
142
+ "content": "<|reserved_token_13|>",
143
+ "lstrip": false,
144
+ "normalized": false,
145
+ "rstrip": false,
146
+ "single_word": false,
147
+ "special": true
148
+ },
149
+ "156909": {
150
+ "content": "<|reserved_token_14|>",
151
+ "lstrip": false,
152
+ "normalized": false,
153
+ "rstrip": false,
154
+ "single_word": false,
155
+ "special": true
156
+ },
157
+ "156910": {
158
+ "content": "<|reserved_token_15|>",
159
+ "lstrip": false,
160
+ "normalized": false,
161
+ "rstrip": false,
162
+ "single_word": false,
163
+ "special": true
164
+ },
165
+ "156911": {
166
+ "content": "<|reserved_token_16|>",
167
+ "lstrip": false,
168
+ "normalized": false,
169
+ "rstrip": false,
170
+ "single_word": false,
171
+ "special": true
172
+ },
173
+ "156912": {
174
+ "content": "<|reserved_token_17|>",
175
+ "lstrip": false,
176
+ "normalized": false,
177
+ "rstrip": false,
178
+ "single_word": false,
179
+ "special": true
180
+ },
181
+ "156913": {
182
+ "content": "<|reserved_token_18|>",
183
+ "lstrip": false,
184
+ "normalized": false,
185
+ "rstrip": false,
186
+ "single_word": false,
187
+ "special": true
188
+ },
189
+ "156914": {
190
+ "content": "<|reserved_token_19|>",
191
+ "lstrip": false,
192
+ "normalized": false,
193
+ "rstrip": false,
194
+ "single_word": false,
195
+ "special": true
196
+ },
197
+ "156915": {
198
+ "content": "<|reserved_token_20|>",
199
+ "lstrip": false,
200
+ "normalized": false,
201
+ "rstrip": false,
202
+ "single_word": false,
203
+ "special": true
204
+ },
205
+ "156916": {
206
+ "content": "<|reserved_token_21|>",
207
+ "lstrip": false,
208
+ "normalized": false,
209
+ "rstrip": false,
210
+ "single_word": false,
211
+ "special": true
212
+ },
213
+ "156917": {
214
+ "content": "<|reserved_token_22|>",
215
+ "lstrip": false,
216
+ "normalized": false,
217
+ "rstrip": false,
218
+ "single_word": false,
219
+ "special": true
220
+ },
221
+ "156918": {
222
+ "content": "<|reserved_token_23|>",
223
+ "lstrip": false,
224
+ "normalized": false,
225
+ "rstrip": false,
226
+ "single_word": false,
227
+ "special": true
228
+ },
229
+ "156919": {
230
+ "content": "<|reserved_token_24|>",
231
+ "lstrip": false,
232
+ "normalized": false,
233
+ "rstrip": false,
234
+ "single_word": false,
235
+ "special": true
236
+ },
237
+ "156920": {
238
+ "content": "<|reserved_token_25|>",
239
+ "lstrip": false,
240
+ "normalized": false,
241
+ "rstrip": false,
242
+ "single_word": false,
243
+ "special": true
244
+ },
245
+ "156921": {
246
+ "content": "<|reserved_token_26|>",
247
+ "lstrip": false,
248
+ "normalized": false,
249
+ "rstrip": false,
250
+ "single_word": false,
251
+ "special": true
252
+ },
253
+ "156922": {
254
+ "content": "<|reserved_token_27|>",
255
+ "lstrip": false,
256
+ "normalized": false,
257
+ "rstrip": false,
258
+ "single_word": false,
259
+ "special": true
260
+ },
261
+ "156923": {
262
+ "content": "<|reserved_token_28|>",
263
+ "lstrip": false,
264
+ "normalized": false,
265
+ "rstrip": false,
266
+ "single_word": false,
267
+ "special": true
268
+ },
269
+ "156924": {
270
+ "content": "<|reserved_token_29|>",
271
+ "lstrip": false,
272
+ "normalized": false,
273
+ "rstrip": false,
274
+ "single_word": false,
275
+ "special": true
276
+ },
277
+ "156925": {
278
+ "content": "<|reserved_token_30|>",
279
+ "lstrip": false,
280
+ "normalized": false,
281
+ "rstrip": false,
282
+ "single_word": false,
283
+ "special": true
284
+ },
285
+ "156926": {
286
+ "content": "<|reserved_token_31|>",
287
+ "lstrip": false,
288
+ "normalized": false,
289
+ "rstrip": false,
290
+ "single_word": false,
291
+ "special": true
292
+ },
293
+ "156927": {
294
+ "content": "<|reserved_token_32|>",
295
+ "lstrip": false,
296
+ "normalized": false,
297
+ "rstrip": false,
298
+ "single_word": false,
299
+ "special": true
300
+ },
301
+ "156928": {
302
+ "content": "<|reserved_token_33|>",
303
+ "lstrip": false,
304
+ "normalized": false,
305
+ "rstrip": false,
306
+ "single_word": false,
307
+ "special": true
308
+ },
309
+ "156929": {
310
+ "content": "<|reserved_token_34|>",
311
+ "lstrip": false,
312
+ "normalized": false,
313
+ "rstrip": false,
314
+ "single_word": false,
315
+ "special": true
316
+ },
317
+ "156930": {
318
+ "content": "<|reserved_token_35|>",
319
+ "lstrip": false,
320
+ "normalized": false,
321
+ "rstrip": false,
322
+ "single_word": false,
323
+ "special": true
324
+ },
325
+ "156931": {
326
+ "content": "<|reserved_token_36|>",
327
+ "lstrip": false,
328
+ "normalized": false,
329
+ "rstrip": false,
330
+ "single_word": false,
331
+ "special": true
332
+ },
333
+ "156932": {
334
+ "content": "<|reserved_token_37|>",
335
+ "lstrip": false,
336
+ "normalized": false,
337
+ "rstrip": false,
338
+ "single_word": false,
339
+ "special": true
340
+ },
341
+ "156933": {
342
+ "content": "<|reserved_token_38|>",
343
+ "lstrip": false,
344
+ "normalized": false,
345
+ "rstrip": false,
346
+ "single_word": false,
347
+ "special": true
348
+ },
349
+ "156934": {
350
+ "content": "<|reserved_token_39|>",
351
+ "lstrip": false,
352
+ "normalized": false,
353
+ "rstrip": false,
354
+ "single_word": false,
355
+ "special": true
356
+ },
357
+ "156935": {
358
+ "content": "<|reserved_token_40|>",
359
+ "lstrip": false,
360
+ "normalized": false,
361
+ "rstrip": false,
362
+ "single_word": false,
363
+ "special": true
364
+ },
365
+ "156936": {
366
+ "content": "<|reserved_token_41|>",
367
+ "lstrip": false,
368
+ "normalized": false,
369
+ "rstrip": false,
370
+ "single_word": false,
371
+ "special": true
372
+ },
373
+ "156937": {
374
+ "content": "<|reserved_token_42|>",
375
+ "lstrip": false,
376
+ "normalized": false,
377
+ "rstrip": false,
378
+ "single_word": false,
379
+ "special": true
380
+ },
381
+ "156938": {
382
+ "content": "<|reserved_token_43|>",
383
+ "lstrip": false,
384
+ "normalized": false,
385
+ "rstrip": false,
386
+ "single_word": false,
387
+ "special": true
388
+ },
389
+ "156939": {
390
+ "content": "<|reserved_token_44|>",
391
+ "lstrip": false,
392
+ "normalized": false,
393
+ "rstrip": false,
394
+ "single_word": false,
395
+ "special": true
396
+ },
397
+ "156940": {
398
+ "content": "<|reserved_token_45|>",
399
+ "lstrip": false,
400
+ "normalized": false,
401
+ "rstrip": false,
402
+ "single_word": false,
403
+ "special": true
404
+ },
405
+ "156941": {
406
+ "content": "<|reserved_token_46|>",
407
+ "lstrip": false,
408
+ "normalized": false,
409
+ "rstrip": false,
410
+ "single_word": false,
411
+ "special": true
412
+ },
413
+ "156942": {
414
+ "content": "<|reserved_token_47|>",
415
+ "lstrip": false,
416
+ "normalized": false,
417
+ "rstrip": false,
418
+ "single_word": false,
419
+ "special": true
420
+ },
421
+ "156943": {
422
+ "content": "<|reserved_token_48|>",
423
+ "lstrip": false,
424
+ "normalized": false,
425
+ "rstrip": false,
426
+ "single_word": false,
427
+ "special": true
428
+ },
429
+ "156944": {
430
+ "content": "<|reserved_token_49|>",
431
+ "lstrip": false,
432
+ "normalized": false,
433
+ "rstrip": false,
434
+ "single_word": false,
435
+ "special": true
436
+ },
437
+ "156945": {
438
+ "content": "<|reserved_token_50|>",
439
+ "lstrip": false,
440
+ "normalized": false,
441
+ "rstrip": false,
442
+ "single_word": false,
443
+ "special": true
444
+ },
445
+ "156946": {
446
+ "content": "<|reserved_token_51|>",
447
+ "lstrip": false,
448
+ "normalized": false,
449
+ "rstrip": false,
450
+ "single_word": false,
451
+ "special": true
452
+ },
453
+ "156947": {
454
+ "content": "<|reserved_token_52|>",
455
+ "lstrip": false,
456
+ "normalized": false,
457
+ "rstrip": false,
458
+ "single_word": false,
459
+ "special": true
460
+ },
461
+ "156948": {
462
+ "content": "<|reserved_token_53|>",
463
+ "lstrip": false,
464
+ "normalized": false,
465
+ "rstrip": false,
466
+ "single_word": false,
467
+ "special": true
468
+ },
469
+ "156949": {
470
+ "content": "<|reserved_token_54|>",
471
+ "lstrip": false,
472
+ "normalized": false,
473
+ "rstrip": false,
474
+ "single_word": false,
475
+ "special": true
476
+ },
477
+ "156950": {
478
+ "content": "<|reserved_token_55|>",
479
+ "lstrip": false,
480
+ "normalized": false,
481
+ "rstrip": false,
482
+ "single_word": false,
483
+ "special": true
484
+ },
485
+ "156951": {
486
+ "content": "<|reserved_token_56|>",
487
+ "lstrip": false,
488
+ "normalized": false,
489
+ "rstrip": false,
490
+ "single_word": false,
491
+ "special": true
492
+ },
493
+ "156952": {
494
+ "content": "<|reserved_token_57|>",
495
+ "lstrip": false,
496
+ "normalized": false,
497
+ "rstrip": false,
498
+ "single_word": false,
499
+ "special": true
500
+ },
501
+ "156953": {
502
+ "content": "<|reserved_token_58|>",
503
+ "lstrip": false,
504
+ "normalized": false,
505
+ "rstrip": false,
506
+ "single_word": false,
507
+ "special": true
508
+ },
509
+ "156954": {
510
+ "content": "<|reserved_token_59|>",
511
+ "lstrip": false,
512
+ "normalized": false,
513
+ "rstrip": false,
514
+ "single_word": false,
515
+ "special": true
516
+ },
517
+ "156955": {
518
+ "content": "<|reserved_token_60|>",
519
+ "lstrip": false,
520
+ "normalized": false,
521
+ "rstrip": false,
522
+ "single_word": false,
523
+ "special": true
524
+ },
525
+ "156956": {
526
+ "content": "<|reserved_token_61|>",
527
+ "lstrip": false,
528
+ "normalized": false,
529
+ "rstrip": false,
530
+ "single_word": false,
531
+ "special": true
532
+ },
533
+ "156957": {
534
+ "content": "<|reserved_token_62|>",
535
+ "lstrip": false,
536
+ "normalized": false,
537
+ "rstrip": false,
538
+ "single_word": false,
539
+ "special": true
540
+ },
541
+ "156958": {
542
+ "content": "<|reserved_token_63|>",
543
+ "lstrip": false,
544
+ "normalized": false,
545
+ "rstrip": false,
546
+ "single_word": false,
547
+ "special": true
548
+ },
549
+ "156959": {
550
+ "content": "<|reserved_token_64|>",
551
+ "lstrip": false,
552
+ "normalized": false,
553
+ "rstrip": false,
554
+ "single_word": false,
555
+ "special": true
556
+ },
557
+ "156960": {
558
+ "content": "<|reserved_token_65|>",
559
+ "lstrip": false,
560
+ "normalized": false,
561
+ "rstrip": false,
562
+ "single_word": false,
563
+ "special": true
564
+ },
565
+ "156961": {
566
+ "content": "<|reserved_token_66|>",
567
+ "lstrip": false,
568
+ "normalized": false,
569
+ "rstrip": false,
570
+ "single_word": false,
571
+ "special": true
572
+ },
573
+ "156962": {
574
+ "content": "<|reserved_token_67|>",
575
+ "lstrip": false,
576
+ "normalized": false,
577
+ "rstrip": false,
578
+ "single_word": false,
579
+ "special": true
580
+ },
581
+ "156963": {
582
+ "content": "<|reserved_token_68|>",
583
+ "lstrip": false,
584
+ "normalized": false,
585
+ "rstrip": false,
586
+ "single_word": false,
587
+ "special": true
588
+ },
589
+ "156964": {
590
+ "content": "<|reserved_token_69|>",
591
+ "lstrip": false,
592
+ "normalized": false,
593
+ "rstrip": false,
594
+ "single_word": false,
595
+ "special": true
596
+ },
597
+ "156965": {
598
+ "content": "<|reserved_token_70|>",
599
+ "lstrip": false,
600
+ "normalized": false,
601
+ "rstrip": false,
602
+ "single_word": false,
603
+ "special": true
604
+ },
605
+ "156966": {
606
+ "content": "<|reserved_token_71|>",
607
+ "lstrip": false,
608
+ "normalized": false,
609
+ "rstrip": false,
610
+ "single_word": false,
611
+ "special": true
612
+ },
613
+ "156967": {
614
+ "content": "<|reserved_token_72|>",
615
+ "lstrip": false,
616
+ "normalized": false,
617
+ "rstrip": false,
618
+ "single_word": false,
619
+ "special": true
620
+ },
621
+ "156968": {
622
+ "content": "<|reserved_token_73|>",
623
+ "lstrip": false,
624
+ "normalized": false,
625
+ "rstrip": false,
626
+ "single_word": false,
627
+ "special": true
628
+ },
629
+ "156969": {
630
+ "content": "<|reserved_token_74|>",
631
+ "lstrip": false,
632
+ "normalized": false,
633
+ "rstrip": false,
634
+ "single_word": false,
635
+ "special": true
636
+ },
637
+ "156970": {
638
+ "content": "<|reserved_token_75|>",
639
+ "lstrip": false,
640
+ "normalized": false,
641
+ "rstrip": false,
642
+ "single_word": false,
643
+ "special": true
644
+ },
645
+ "156971": {
646
+ "content": "<|reserved_token_76|>",
647
+ "lstrip": false,
648
+ "normalized": false,
649
+ "rstrip": false,
650
+ "single_word": false,
651
+ "special": true
652
+ },
653
+ "156972": {
654
+ "content": "<|reserved_token_77|>",
655
+ "lstrip": false,
656
+ "normalized": false,
657
+ "rstrip": false,
658
+ "single_word": false,
659
+ "special": true
660
+ },
661
+ "156973": {
662
+ "content": "<|reserved_token_78|>",
663
+ "lstrip": false,
664
+ "normalized": false,
665
+ "rstrip": false,
666
+ "single_word": false,
667
+ "special": true
668
+ },
669
+ "156974": {
670
+ "content": "<|reserved_token_79|>",
671
+ "lstrip": false,
672
+ "normalized": false,
673
+ "rstrip": false,
674
+ "single_word": false,
675
+ "special": true
676
+ },
677
+ "156975": {
678
+ "content": "<|reserved_token_80|>",
679
+ "lstrip": false,
680
+ "normalized": false,
681
+ "rstrip": false,
682
+ "single_word": false,
683
+ "special": true
684
+ },
685
+ "156976": {
686
+ "content": "<|reserved_token_81|>",
687
+ "lstrip": false,
688
+ "normalized": false,
689
+ "rstrip": false,
690
+ "single_word": false,
691
+ "special": true
692
+ },
693
+ "156977": {
694
+ "content": "<|reserved_token_82|>",
695
+ "lstrip": false,
696
+ "normalized": false,
697
+ "rstrip": false,
698
+ "single_word": false,
699
+ "special": true
700
+ },
701
+ "156978": {
702
+ "content": "<|reserved_token_83|>",
703
+ "lstrip": false,
704
+ "normalized": false,
705
+ "rstrip": false,
706
+ "single_word": false,
707
+ "special": true
708
+ },
709
+ "156979": {
710
+ "content": "<|reserved_token_84|>",
711
+ "lstrip": false,
712
+ "normalized": false,
713
+ "rstrip": false,
714
+ "single_word": false,
715
+ "special": true
716
+ },
717
+ "156980": {
718
+ "content": "<|reserved_token_85|>",
719
+ "lstrip": false,
720
+ "normalized": false,
721
+ "rstrip": false,
722
+ "single_word": false,
723
+ "special": true
724
+ },
725
+ "156981": {
726
+ "content": "<|reserved_token_86|>",
727
+ "lstrip": false,
728
+ "normalized": false,
729
+ "rstrip": false,
730
+ "single_word": false,
731
+ "special": true
732
+ },
733
+ "156982": {
734
+ "content": "<|reserved_token_87|>",
735
+ "lstrip": false,
736
+ "normalized": false,
737
+ "rstrip": false,
738
+ "single_word": false,
739
+ "special": true
740
+ },
741
+ "156983": {
742
+ "content": "<|reserved_token_88|>",
743
+ "lstrip": false,
744
+ "normalized": false,
745
+ "rstrip": false,
746
+ "single_word": false,
747
+ "special": true
748
+ },
749
+ "156984": {
750
+ "content": "<|reserved_token_89|>",
751
+ "lstrip": false,
752
+ "normalized": false,
753
+ "rstrip": false,
754
+ "single_word": false,
755
+ "special": true
756
+ },
757
+ "156985": {
758
+ "content": "<|reserved_token_90|>",
759
+ "lstrip": false,
760
+ "normalized": false,
761
+ "rstrip": false,
762
+ "single_word": false,
763
+ "special": true
764
+ },
765
+ "156986": {
766
+ "content": "<|reserved_token_91|>",
767
+ "lstrip": false,
768
+ "normalized": false,
769
+ "rstrip": false,
770
+ "single_word": false,
771
+ "special": true
772
+ },
773
+ "156987": {
774
+ "content": "<|reserved_token_92|>",
775
+ "lstrip": false,
776
+ "normalized": false,
777
+ "rstrip": false,
778
+ "single_word": false,
779
+ "special": true
780
+ },
781
+ "156988": {
782
+ "content": "<|reserved_token_93|>",
783
+ "lstrip": false,
784
+ "normalized": false,
785
+ "rstrip": false,
786
+ "single_word": false,
787
+ "special": true
788
+ },
789
+ "156989": {
790
+ "content": "<|reserved_token_94|>",
791
+ "lstrip": false,
792
+ "normalized": false,
793
+ "rstrip": false,
794
+ "single_word": false,
795
+ "special": true
796
+ },
797
+ "156990": {
798
+ "content": "<|reserved_token_95|>",
799
+ "lstrip": false,
800
+ "normalized": false,
801
+ "rstrip": false,
802
+ "single_word": false,
803
+ "special": true
804
+ },
805
+ "156991": {
806
+ "content": "<|reserved_token_96|>",
807
+ "lstrip": false,
808
+ "normalized": false,
809
+ "rstrip": false,
810
+ "single_word": false,
811
+ "special": true
812
+ },
813
+ "156992": {
814
+ "content": "<|reserved_token_97|>",
815
+ "lstrip": false,
816
+ "normalized": false,
817
+ "rstrip": false,
818
+ "single_word": false,
819
+ "special": true
820
+ },
821
+ "156993": {
822
+ "content": "<|reserved_token_98|>",
823
+ "lstrip": false,
824
+ "normalized": false,
825
+ "rstrip": false,
826
+ "single_word": false,
827
+ "special": true
828
+ },
829
+ "156994": {
830
+ "content": "<|reserved_token_99|>",
831
+ "lstrip": false,
832
+ "normalized": false,
833
+ "rstrip": false,
834
+ "single_word": false,
835
+ "special": true
836
+ },
837
+ "156995": {
838
+ "content": "<|reserved_token_100|>",
839
+ "lstrip": false,
840
+ "normalized": false,
841
+ "rstrip": false,
842
+ "single_word": false,
843
+ "special": true
844
+ },
845
+ "156996": {
846
+ "content": "<|reserved_token_101|>",
847
+ "lstrip": false,
848
+ "normalized": false,
849
+ "rstrip": false,
850
+ "single_word": false,
851
+ "special": true
852
+ },
853
+ "156997": {
854
+ "content": "<|reserved_token_102|>",
855
+ "lstrip": false,
856
+ "normalized": false,
857
+ "rstrip": false,
858
+ "single_word": false,
859
+ "special": true
860
+ },
861
+ "156998": {
862
+ "content": "<|reserved_token_103|>",
863
+ "lstrip": false,
864
+ "normalized": false,
865
+ "rstrip": false,
866
+ "single_word": false,
867
+ "special": true
868
+ },
869
+ "156999": {
870
+ "content": "<|reserved_token_104|>",
871
+ "lstrip": false,
872
+ "normalized": false,
873
+ "rstrip": false,
874
+ "single_word": false,
875
+ "special": true
876
+ },
877
+ "157000": {
878
+ "content": "<|reserved_token_105|>",
879
+ "lstrip": false,
880
+ "normalized": false,
881
+ "rstrip": false,
882
+ "single_word": false,
883
+ "special": true
884
+ },
885
+ "157001": {
886
+ "content": "<|reserved_token_106|>",
887
+ "lstrip": false,
888
+ "normalized": false,
889
+ "rstrip": false,
890
+ "single_word": false,
891
+ "special": true
892
+ },
893
+ "157002": {
894
+ "content": "<|reserved_token_107|>",
895
+ "lstrip": false,
896
+ "normalized": false,
897
+ "rstrip": false,
898
+ "single_word": false,
899
+ "special": true
900
+ },
901
+ "157003": {
902
+ "content": "<|reserved_token_108|>",
903
+ "lstrip": false,
904
+ "normalized": false,
905
+ "rstrip": false,
906
+ "single_word": false,
907
+ "special": true
908
+ },
909
+ "157004": {
910
+ "content": "<|reserved_token_109|>",
911
+ "lstrip": false,
912
+ "normalized": false,
913
+ "rstrip": false,
914
+ "single_word": false,
915
+ "special": true
916
+ },
917
+ "157005": {
918
+ "content": "<|reserved_token_110|>",
919
+ "lstrip": false,
920
+ "normalized": false,
921
+ "rstrip": false,
922
+ "single_word": false,
923
+ "special": true
924
+ },
925
+ "157006": {
926
+ "content": "<|reserved_token_111|>",
927
+ "lstrip": false,
928
+ "normalized": false,
929
+ "rstrip": false,
930
+ "single_word": false,
931
+ "special": true
932
+ },
933
+ "157007": {
934
+ "content": "<|reserved_token_112|>",
935
+ "lstrip": false,
936
+ "normalized": false,
937
+ "rstrip": false,
938
+ "single_word": false,
939
+ "special": true
940
+ },
941
+ "157008": {
942
+ "content": "<|reserved_token_113|>",
943
+ "lstrip": false,
944
+ "normalized": false,
945
+ "rstrip": false,
946
+ "single_word": false,
947
+ "special": true
948
+ },
949
+ "157009": {
950
+ "content": "<|reserved_token_114|>",
951
+ "lstrip": false,
952
+ "normalized": false,
953
+ "rstrip": false,
954
+ "single_word": false,
955
+ "special": true
956
+ },
957
+ "157010": {
958
+ "content": "<|reserved_token_115|>",
959
+ "lstrip": false,
960
+ "normalized": false,
961
+ "rstrip": false,
962
+ "single_word": false,
963
+ "special": true
964
+ },
965
+ "157011": {
966
+ "content": "<|reserved_token_116|>",
967
+ "lstrip": false,
968
+ "normalized": false,
969
+ "rstrip": false,
970
+ "single_word": false,
971
+ "special": true
972
+ },
973
+ "157012": {
974
+ "content": "<|reserved_token_117|>",
975
+ "lstrip": false,
976
+ "normalized": false,
977
+ "rstrip": false,
978
+ "single_word": false,
979
+ "special": true
980
+ },
981
+ "157013": {
982
+ "content": "<|reserved_token_118|>",
983
+ "lstrip": false,
984
+ "normalized": false,
985
+ "rstrip": false,
986
+ "single_word": false,
987
+ "special": true
988
+ },
989
+ "157014": {
990
+ "content": "<|reserved_token_119|>",
991
+ "lstrip": false,
992
+ "normalized": false,
993
+ "rstrip": false,
994
+ "single_word": false,
995
+ "special": true
996
+ },
997
+ "157015": {
998
+ "content": "<|reserved_token_120|>",
999
+ "lstrip": false,
1000
+ "normalized": false,
1001
+ "rstrip": false,
1002
+ "single_word": false,
1003
+ "special": true
1004
+ },
1005
+ "157016": {
1006
+ "content": "<|reserved_token_121|>",
1007
+ "lstrip": false,
1008
+ "normalized": false,
1009
+ "rstrip": false,
1010
+ "single_word": false,
1011
+ "special": true
1012
+ },
1013
+ "157017": {
1014
+ "content": "<|reserved_token_122|>",
1015
+ "lstrip": false,
1016
+ "normalized": false,
1017
+ "rstrip": false,
1018
+ "single_word": false,
1019
+ "special": true
1020
+ },
1021
+ "157018": {
1022
+ "content": "<|reserved_token_123|>",
1023
+ "lstrip": false,
1024
+ "normalized": false,
1025
+ "rstrip": false,
1026
+ "single_word": false,
1027
+ "special": true
1028
+ },
1029
+ "157019": {
1030
+ "content": "<|reserved_token_124|>",
1031
+ "lstrip": false,
1032
+ "normalized": false,
1033
+ "rstrip": false,
1034
+ "single_word": false,
1035
+ "special": true
1036
+ },
1037
+ "157020": {
1038
+ "content": "<|reserved_token_125|>",
1039
+ "lstrip": false,
1040
+ "normalized": false,
1041
+ "rstrip": false,
1042
+ "single_word": false,
1043
+ "special": true
1044
+ },
1045
+ "157021": {
1046
+ "content": "<|reserved_token_126|>",
1047
+ "lstrip": false,
1048
+ "normalized": false,
1049
+ "rstrip": false,
1050
+ "single_word": false,
1051
+ "special": true
1052
+ },
1053
+ "157022": {
1054
+ "content": "<|reserved_token_127|>",
1055
+ "lstrip": false,
1056
+ "normalized": false,
1057
+ "rstrip": false,
1058
+ "single_word": false,
1059
+ "special": true
1060
+ },
1061
+ "157023": {
1062
+ "content": "<|reserved_token_128|>",
1063
+ "lstrip": false,
1064
+ "normalized": false,
1065
+ "rstrip": false,
1066
+ "single_word": false,
1067
+ "special": true
1068
+ },
1069
+ "157024": {
1070
+ "content": "<|reserved_token_129|>",
1071
+ "lstrip": false,
1072
+ "normalized": false,
1073
+ "rstrip": false,
1074
+ "single_word": false,
1075
+ "special": true
1076
+ },
1077
+ "157025": {
1078
+ "content": "<|reserved_token_130|>",
1079
+ "lstrip": false,
1080
+ "normalized": false,
1081
+ "rstrip": false,
1082
+ "single_word": false,
1083
+ "special": true
1084
+ },
1085
+ "157026": {
1086
+ "content": "<|reserved_token_131|>",
1087
+ "lstrip": false,
1088
+ "normalized": false,
1089
+ "rstrip": false,
1090
+ "single_word": false,
1091
+ "special": true
1092
+ },
1093
+ "157027": {
1094
+ "content": "<|reserved_token_132|>",
1095
+ "lstrip": false,
1096
+ "normalized": false,
1097
+ "rstrip": false,
1098
+ "single_word": false,
1099
+ "special": true
1100
+ },
1101
+ "157028": {
1102
+ "content": "<|reserved_token_133|>",
1103
+ "lstrip": false,
1104
+ "normalized": false,
1105
+ "rstrip": false,
1106
+ "single_word": false,
1107
+ "special": true
1108
+ },
1109
+ "157029": {
1110
+ "content": "<|reserved_token_134|>",
1111
+ "lstrip": false,
1112
+ "normalized": false,
1113
+ "rstrip": false,
1114
+ "single_word": false,
1115
+ "special": true
1116
+ },
1117
+ "157030": {
1118
+ "content": "<|reserved_token_135|>",
1119
+ "lstrip": false,
1120
+ "normalized": false,
1121
+ "rstrip": false,
1122
+ "single_word": false,
1123
+ "special": true
1124
+ },
1125
+ "157031": {
1126
+ "content": "<|reserved_token_136|>",
1127
+ "lstrip": false,
1128
+ "normalized": false,
1129
+ "rstrip": false,
1130
+ "single_word": false,
1131
+ "special": true
1132
+ },
1133
+ "157032": {
1134
+ "content": "<|reserved_token_137|>",
1135
+ "lstrip": false,
1136
+ "normalized": false,
1137
+ "rstrip": false,
1138
+ "single_word": false,
1139
+ "special": true
1140
+ },
1141
+ "157033": {
1142
+ "content": "<|reserved_token_138|>",
1143
+ "lstrip": false,
1144
+ "normalized": false,
1145
+ "rstrip": false,
1146
+ "single_word": false,
1147
+ "special": true
1148
+ },
1149
+ "157034": {
1150
+ "content": "<|reserved_token_139|>",
1151
+ "lstrip": false,
1152
+ "normalized": false,
1153
+ "rstrip": false,
1154
+ "single_word": false,
1155
+ "special": true
1156
+ },
1157
+ "157035": {
1158
+ "content": "<|reserved_token_140|>",
1159
+ "lstrip": false,
1160
+ "normalized": false,
1161
+ "rstrip": false,
1162
+ "single_word": false,
1163
+ "special": true
1164
+ },
1165
+ "157036": {
1166
+ "content": "<|reserved_token_141|>",
1167
+ "lstrip": false,
1168
+ "normalized": false,
1169
+ "rstrip": false,
1170
+ "single_word": false,
1171
+ "special": true
1172
+ },
1173
+ "157037": {
1174
+ "content": "<|reserved_token_142|>",
1175
+ "lstrip": false,
1176
+ "normalized": false,
1177
+ "rstrip": false,
1178
+ "single_word": false,
1179
+ "special": true
1180
+ },
1181
+ "157038": {
1182
+ "content": "<|reserved_token_143|>",
1183
+ "lstrip": false,
1184
+ "normalized": false,
1185
+ "rstrip": false,
1186
+ "single_word": false,
1187
+ "special": true
1188
+ },
1189
+ "157039": {
1190
+ "content": "<|reserved_token_144|>",
1191
+ "lstrip": false,
1192
+ "normalized": false,
1193
+ "rstrip": false,
1194
+ "single_word": false,
1195
+ "special": true
1196
+ },
1197
+ "157040": {
1198
+ "content": "<|reserved_token_145|>",
1199
+ "lstrip": false,
1200
+ "normalized": false,
1201
+ "rstrip": false,
1202
+ "single_word": false,
1203
+ "special": true
1204
+ },
1205
+ "157041": {
1206
+ "content": "<|reserved_token_146|>",
1207
+ "lstrip": false,
1208
+ "normalized": false,
1209
+ "rstrip": false,
1210
+ "single_word": false,
1211
+ "special": true
1212
+ },
1213
+ "157042": {
1214
+ "content": "<|reserved_token_147|>",
1215
+ "lstrip": false,
1216
+ "normalized": false,
1217
+ "rstrip": false,
1218
+ "single_word": false,
1219
+ "special": true
1220
+ },
1221
+ "157043": {
1222
+ "content": "<|reserved_token_148|>",
1223
+ "lstrip": false,
1224
+ "normalized": false,
1225
+ "rstrip": false,
1226
+ "single_word": false,
1227
+ "special": true
1228
+ },
1229
+ "157044": {
1230
+ "content": "<|reserved_token_149|>",
1231
+ "lstrip": false,
1232
+ "normalized": false,
1233
+ "rstrip": false,
1234
+ "single_word": false,
1235
+ "special": true
1236
+ },
1237
+ "157045": {
1238
+ "content": "<|reserved_token_150|>",
1239
+ "lstrip": false,
1240
+ "normalized": false,
1241
+ "rstrip": false,
1242
+ "single_word": false,
1243
+ "special": true
1244
+ },
1245
+ "157046": {
1246
+ "content": "<|reserved_token_151|>",
1247
+ "lstrip": false,
1248
+ "normalized": false,
1249
+ "rstrip": false,
1250
+ "single_word": false,
1251
+ "special": true
1252
+ },
1253
+ "157047": {
1254
+ "content": "<|reserved_token_152|>",
1255
+ "lstrip": false,
1256
+ "normalized": false,
1257
+ "rstrip": false,
1258
+ "single_word": false,
1259
+ "special": true
1260
+ },
1261
+ "157048": {
1262
+ "content": "<|reserved_token_153|>",
1263
+ "lstrip": false,
1264
+ "normalized": false,
1265
+ "rstrip": false,
1266
+ "single_word": false,
1267
+ "special": true
1268
+ },
1269
+ "157049": {
1270
+ "content": "<|reserved_token_154|>",
1271
+ "lstrip": false,
1272
+ "normalized": false,
1273
+ "rstrip": false,
1274
+ "single_word": false,
1275
+ "special": true
1276
+ },
1277
+ "157050": {
1278
+ "content": "<|reserved_token_155|>",
1279
+ "lstrip": false,
1280
+ "normalized": false,
1281
+ "rstrip": false,
1282
+ "single_word": false,
1283
+ "special": true
1284
+ },
1285
+ "157051": {
1286
+ "content": "<|reserved_token_156|>",
1287
+ "lstrip": false,
1288
+ "normalized": false,
1289
+ "rstrip": false,
1290
+ "single_word": false,
1291
+ "special": true
1292
+ },
1293
+ "157052": {
1294
+ "content": "<|reserved_token_157|>",
1295
+ "lstrip": false,
1296
+ "normalized": false,
1297
+ "rstrip": false,
1298
+ "single_word": false,
1299
+ "special": true
1300
+ },
1301
+ "157053": {
1302
+ "content": "<|reserved_token_158|>",
1303
+ "lstrip": false,
1304
+ "normalized": false,
1305
+ "rstrip": false,
1306
+ "single_word": false,
1307
+ "special": true
1308
+ },
1309
+ "157054": {
1310
+ "content": "<|reserved_token_159|>",
1311
+ "lstrip": false,
1312
+ "normalized": false,
1313
+ "rstrip": false,
1314
+ "single_word": false,
1315
+ "special": true
1316
+ },
1317
+ "157055": {
1318
+ "content": "<|reserved_token_160|>",
1319
+ "lstrip": false,
1320
+ "normalized": false,
1321
+ "rstrip": false,
1322
+ "single_word": false,
1323
+ "special": true
1324
+ },
1325
+ "157056": {
1326
+ "content": "<|reserved_token_161|>",
1327
+ "lstrip": false,
1328
+ "normalized": false,
1329
+ "rstrip": false,
1330
+ "single_word": false,
1331
+ "special": true
1332
+ },
1333
+ "157057": {
1334
+ "content": "<|reserved_token_162|>",
1335
+ "lstrip": false,
1336
+ "normalized": false,
1337
+ "rstrip": false,
1338
+ "single_word": false,
1339
+ "special": true
1340
+ },
1341
+ "157058": {
1342
+ "content": "<|reserved_token_163|>",
1343
+ "lstrip": false,
1344
+ "normalized": false,
1345
+ "rstrip": false,
1346
+ "single_word": false,
1347
+ "special": true
1348
+ },
1349
+ "157059": {
1350
+ "content": "<|reserved_token_164|>",
1351
+ "lstrip": false,
1352
+ "normalized": false,
1353
+ "rstrip": false,
1354
+ "single_word": false,
1355
+ "special": true
1356
+ },
1357
+ "157060": {
1358
+ "content": "<|reserved_token_165|>",
1359
+ "lstrip": false,
1360
+ "normalized": false,
1361
+ "rstrip": false,
1362
+ "single_word": false,
1363
+ "special": true
1364
+ },
1365
+ "157061": {
1366
+ "content": "<|reserved_token_166|>",
1367
+ "lstrip": false,
1368
+ "normalized": false,
1369
+ "rstrip": false,
1370
+ "single_word": false,
1371
+ "special": true
1372
+ },
1373
+ "157062": {
1374
+ "content": "<|reserved_token_167|>",
1375
+ "lstrip": false,
1376
+ "normalized": false,
1377
+ "rstrip": false,
1378
+ "single_word": false,
1379
+ "special": true
1380
+ },
1381
+ "157063": {
1382
+ "content": "<|reserved_token_168|>",
1383
+ "lstrip": false,
1384
+ "normalized": false,
1385
+ "rstrip": false,
1386
+ "single_word": false,
1387
+ "special": true
1388
+ },
1389
+ "157064": {
1390
+ "content": "<|reserved_token_169|>",
1391
+ "lstrip": false,
1392
+ "normalized": false,
1393
+ "rstrip": false,
1394
+ "single_word": false,
1395
+ "special": true
1396
+ },
1397
+ "157065": {
1398
+ "content": "<|reserved_token_170|>",
1399
+ "lstrip": false,
1400
+ "normalized": false,
1401
+ "rstrip": false,
1402
+ "single_word": false,
1403
+ "special": true
1404
+ },
1405
+ "157066": {
1406
+ "content": "<|reserved_token_171|>",
1407
+ "lstrip": false,
1408
+ "normalized": false,
1409
+ "rstrip": false,
1410
+ "single_word": false,
1411
+ "special": true
1412
+ },
1413
+ "157067": {
1414
+ "content": "<|reserved_token_172|>",
1415
+ "lstrip": false,
1416
+ "normalized": false,
1417
+ "rstrip": false,
1418
+ "single_word": false,
1419
+ "special": true
1420
+ },
1421
+ "157068": {
1422
+ "content": "<|reserved_token_173|>",
1423
+ "lstrip": false,
1424
+ "normalized": false,
1425
+ "rstrip": false,
1426
+ "single_word": false,
1427
+ "special": true
1428
+ },
1429
+ "157069": {
1430
+ "content": "<|reserved_token_174|>",
1431
+ "lstrip": false,
1432
+ "normalized": false,
1433
+ "rstrip": false,
1434
+ "single_word": false,
1435
+ "special": true
1436
+ },
1437
+ "157070": {
1438
+ "content": "<|reserved_token_175|>",
1439
+ "lstrip": false,
1440
+ "normalized": false,
1441
+ "rstrip": false,
1442
+ "single_word": false,
1443
+ "special": true
1444
+ },
1445
+ "157071": {
1446
+ "content": "<|reserved_token_176|>",
1447
+ "lstrip": false,
1448
+ "normalized": false,
1449
+ "rstrip": false,
1450
+ "single_word": false,
1451
+ "special": true
1452
+ },
1453
+ "157072": {
1454
+ "content": "<|reserved_token_177|>",
1455
+ "lstrip": false,
1456
+ "normalized": false,
1457
+ "rstrip": false,
1458
+ "single_word": false,
1459
+ "special": true
1460
+ },
1461
+ "157073": {
1462
+ "content": "<|reserved_token_178|>",
1463
+ "lstrip": false,
1464
+ "normalized": false,
1465
+ "rstrip": false,
1466
+ "single_word": false,
1467
+ "special": true
1468
+ },
1469
+ "157074": {
1470
+ "content": "<|reserved_token_179|>",
1471
+ "lstrip": false,
1472
+ "normalized": false,
1473
+ "rstrip": false,
1474
+ "single_word": false,
1475
+ "special": true
1476
+ },
1477
+ "157075": {
1478
+ "content": "<|reserved_token_180|>",
1479
+ "lstrip": false,
1480
+ "normalized": false,
1481
+ "rstrip": false,
1482
+ "single_word": false,
1483
+ "special": true
1484
+ },
1485
+ "157076": {
1486
+ "content": "<|reserved_token_181|>",
1487
+ "lstrip": false,
1488
+ "normalized": false,
1489
+ "rstrip": false,
1490
+ "single_word": false,
1491
+ "special": true
1492
+ },
1493
+ "157077": {
1494
+ "content": "<|reserved_token_182|>",
1495
+ "lstrip": false,
1496
+ "normalized": false,
1497
+ "rstrip": false,
1498
+ "single_word": false,
1499
+ "special": true
1500
+ },
1501
+ "157078": {
1502
+ "content": "<|reserved_token_183|>",
1503
+ "lstrip": false,
1504
+ "normalized": false,
1505
+ "rstrip": false,
1506
+ "single_word": false,
1507
+ "special": true
1508
+ },
1509
+ "157079": {
1510
+ "content": "<|reserved_token_184|>",
1511
+ "lstrip": false,
1512
+ "normalized": false,
1513
+ "rstrip": false,
1514
+ "single_word": false,
1515
+ "special": true
1516
+ },
1517
+ "157080": {
1518
+ "content": "<|reserved_token_185|>",
1519
+ "lstrip": false,
1520
+ "normalized": false,
1521
+ "rstrip": false,
1522
+ "single_word": false,
1523
+ "special": true
1524
+ },
1525
+ "157081": {
1526
+ "content": "<|reserved_token_186|>",
1527
+ "lstrip": false,
1528
+ "normalized": false,
1529
+ "rstrip": false,
1530
+ "single_word": false,
1531
+ "special": true
1532
+ },
1533
+ "157082": {
1534
+ "content": "<|reserved_token_187|>",
1535
+ "lstrip": false,
1536
+ "normalized": false,
1537
+ "rstrip": false,
1538
+ "single_word": false,
1539
+ "special": true
1540
+ },
1541
+ "157083": {
1542
+ "content": "<|reserved_token_188|>",
1543
+ "lstrip": false,
1544
+ "normalized": false,
1545
+ "rstrip": false,
1546
+ "single_word": false,
1547
+ "special": true
1548
+ },
1549
+ "157084": {
1550
+ "content": "<|reserved_token_189|>",
1551
+ "lstrip": false,
1552
+ "normalized": false,
1553
+ "rstrip": false,
1554
+ "single_word": false,
1555
+ "special": true
1556
+ },
1557
+ "157085": {
1558
+ "content": "<|reserved_token_190|>",
1559
+ "lstrip": false,
1560
+ "normalized": false,
1561
+ "rstrip": false,
1562
+ "single_word": false,
1563
+ "special": true
1564
+ },
1565
+ "157086": {
1566
+ "content": "<|reserved_token_191|>",
1567
+ "lstrip": false,
1568
+ "normalized": false,
1569
+ "rstrip": false,
1570
+ "single_word": false,
1571
+ "special": true
1572
+ },
1573
+ "157087": {
1574
+ "content": "<|reserved_token_192|>",
1575
+ "lstrip": false,
1576
+ "normalized": false,
1577
+ "rstrip": false,
1578
+ "single_word": false,
1579
+ "special": true
1580
+ },
1581
+ "157088": {
1582
+ "content": "<|reserved_token_193|>",
1583
+ "lstrip": false,
1584
+ "normalized": false,
1585
+ "rstrip": false,
1586
+ "single_word": false,
1587
+ "special": true
1588
+ },
1589
+ "157089": {
1590
+ "content": "<|reserved_token_194|>",
1591
+ "lstrip": false,
1592
+ "normalized": false,
1593
+ "rstrip": false,
1594
+ "single_word": false,
1595
+ "special": true
1596
+ },
1597
+ "157090": {
1598
+ "content": "<|reserved_token_195|>",
1599
+ "lstrip": false,
1600
+ "normalized": false,
1601
+ "rstrip": false,
1602
+ "single_word": false,
1603
+ "special": true
1604
+ },
1605
+ "157091": {
1606
+ "content": "<|reserved_token_196|>",
1607
+ "lstrip": false,
1608
+ "normalized": false,
1609
+ "rstrip": false,
1610
+ "single_word": false,
1611
+ "special": true
1612
+ },
1613
+ "157092": {
1614
+ "content": "<|reserved_token_197|>",
1615
+ "lstrip": false,
1616
+ "normalized": false,
1617
+ "rstrip": false,
1618
+ "single_word": false,
1619
+ "special": true
1620
+ },
1621
+ "157093": {
1622
+ "content": "<|reserved_token_198|>",
1623
+ "lstrip": false,
1624
+ "normalized": false,
1625
+ "rstrip": false,
1626
+ "single_word": false,
1627
+ "special": true
1628
+ },
1629
+ "157094": {
1630
+ "content": "<|reserved_token_199|>",
1631
+ "lstrip": false,
1632
+ "normalized": false,
1633
+ "rstrip": false,
1634
+ "single_word": false,
1635
+ "special": true
1636
+ },
1637
+ "157095": {
1638
+ "content": "<|reserved_token_200|>",
1639
+ "lstrip": false,
1640
+ "normalized": false,
1641
+ "rstrip": false,
1642
+ "single_word": false,
1643
+ "special": true
1644
+ },
1645
+ "157096": {
1646
+ "content": "<|reserved_token_201|>",
1647
+ "lstrip": false,
1648
+ "normalized": false,
1649
+ "rstrip": false,
1650
+ "single_word": false,
1651
+ "special": true
1652
+ },
1653
+ "157097": {
1654
+ "content": "<|reserved_token_202|>",
1655
+ "lstrip": false,
1656
+ "normalized": false,
1657
+ "rstrip": false,
1658
+ "single_word": false,
1659
+ "special": true
1660
+ },
1661
+ "157098": {
1662
+ "content": "<|reserved_token_203|>",
1663
+ "lstrip": false,
1664
+ "normalized": false,
1665
+ "rstrip": false,
1666
+ "single_word": false,
1667
+ "special": true
1668
+ },
1669
+ "157099": {
1670
+ "content": "<|reserved_token_204|>",
1671
+ "lstrip": false,
1672
+ "normalized": false,
1673
+ "rstrip": false,
1674
+ "single_word": false,
1675
+ "special": true
1676
+ },
1677
+ "157100": {
1678
+ "content": "<|reserved_token_205|>",
1679
+ "lstrip": false,
1680
+ "normalized": false,
1681
+ "rstrip": false,
1682
+ "single_word": false,
1683
+ "special": true
1684
+ },
1685
+ "157101": {
1686
+ "content": "<|reserved_token_206|>",
1687
+ "lstrip": false,
1688
+ "normalized": false,
1689
+ "rstrip": false,
1690
+ "single_word": false,
1691
+ "special": true
1692
+ },
1693
+ "157102": {
1694
+ "content": "<|reserved_token_207|>",
1695
+ "lstrip": false,
1696
+ "normalized": false,
1697
+ "rstrip": false,
1698
+ "single_word": false,
1699
+ "special": true
1700
+ },
1701
+ "157103": {
1702
+ "content": "<|reserved_token_208|>",
1703
+ "lstrip": false,
1704
+ "normalized": false,
1705
+ "rstrip": false,
1706
+ "single_word": false,
1707
+ "special": true
1708
+ },
1709
+ "157104": {
1710
+ "content": "<|reserved_token_209|>",
1711
+ "lstrip": false,
1712
+ "normalized": false,
1713
+ "rstrip": false,
1714
+ "single_word": false,
1715
+ "special": true
1716
+ },
1717
+ "157105": {
1718
+ "content": "<|reserved_token_210|>",
1719
+ "lstrip": false,
1720
+ "normalized": false,
1721
+ "rstrip": false,
1722
+ "single_word": false,
1723
+ "special": true
1724
+ },
1725
+ "157106": {
1726
+ "content": "<|reserved_token_211|>",
1727
+ "lstrip": false,
1728
+ "normalized": false,
1729
+ "rstrip": false,
1730
+ "single_word": false,
1731
+ "special": true
1732
+ },
1733
+ "157107": {
1734
+ "content": "<|reserved_token_212|>",
1735
+ "lstrip": false,
1736
+ "normalized": false,
1737
+ "rstrip": false,
1738
+ "single_word": false,
1739
+ "special": true
1740
+ },
1741
+ "157108": {
1742
+ "content": "<|reserved_token_213|>",
1743
+ "lstrip": false,
1744
+ "normalized": false,
1745
+ "rstrip": false,
1746
+ "single_word": false,
1747
+ "special": true
1748
+ },
1749
+ "157109": {
1750
+ "content": "<|reserved_token_214|>",
1751
+ "lstrip": false,
1752
+ "normalized": false,
1753
+ "rstrip": false,
1754
+ "single_word": false,
1755
+ "special": true
1756
+ },
1757
+ "157110": {
1758
+ "content": "<|reserved_token_215|>",
1759
+ "lstrip": false,
1760
+ "normalized": false,
1761
+ "rstrip": false,
1762
+ "single_word": false,
1763
+ "special": true
1764
+ },
1765
+ "157111": {
1766
+ "content": "<|reserved_token_216|>",
1767
+ "lstrip": false,
1768
+ "normalized": false,
1769
+ "rstrip": false,
1770
+ "single_word": false,
1771
+ "special": true
1772
+ },
1773
+ "157112": {
1774
+ "content": "<|reserved_token_217|>",
1775
+ "lstrip": false,
1776
+ "normalized": false,
1777
+ "rstrip": false,
1778
+ "single_word": false,
1779
+ "special": true
1780
+ },
1781
+ "157113": {
1782
+ "content": "<|reserved_token_218|>",
1783
+ "lstrip": false,
1784
+ "normalized": false,
1785
+ "rstrip": false,
1786
+ "single_word": false,
1787
+ "special": true
1788
+ },
1789
+ "157114": {
1790
+ "content": "<|reserved_token_219|>",
1791
+ "lstrip": false,
1792
+ "normalized": false,
1793
+ "rstrip": false,
1794
+ "single_word": false,
1795
+ "special": true
1796
+ },
1797
+ "157115": {
1798
+ "content": "<|reserved_token_220|>",
1799
+ "lstrip": false,
1800
+ "normalized": false,
1801
+ "rstrip": false,
1802
+ "single_word": false,
1803
+ "special": true
1804
+ },
1805
+ "157116": {
1806
+ "content": "<|reserved_token_221|>",
1807
+ "lstrip": false,
1808
+ "normalized": false,
1809
+ "rstrip": false,
1810
+ "single_word": false,
1811
+ "special": true
1812
+ },
1813
+ "157117": {
1814
+ "content": "<|reserved_token_222|>",
1815
+ "lstrip": false,
1816
+ "normalized": false,
1817
+ "rstrip": false,
1818
+ "single_word": false,
1819
+ "special": true
1820
+ },
1821
+ "157118": {
1822
+ "content": "<|reserved_token_223|>",
1823
+ "lstrip": false,
1824
+ "normalized": false,
1825
+ "rstrip": false,
1826
+ "single_word": false,
1827
+ "special": true
1828
+ },
1829
+ "157119": {
1830
+ "content": "<|reserved_token_224|>",
1831
+ "lstrip": false,
1832
+ "normalized": false,
1833
+ "rstrip": false,
1834
+ "single_word": false,
1835
+ "special": true
1836
+ },
1837
+ "157120": {
1838
+ "content": "<|reserved_token_225|>",
1839
+ "lstrip": false,
1840
+ "normalized": false,
1841
+ "rstrip": false,
1842
+ "single_word": false,
1843
+ "special": true
1844
+ },
1845
+ "157121": {
1846
+ "content": "<|reserved_token_226|>",
1847
+ "lstrip": false,
1848
+ "normalized": false,
1849
+ "rstrip": false,
1850
+ "single_word": false,
1851
+ "special": true
1852
+ },
1853
+ "157122": {
1854
+ "content": "<|reserved_token_227|>",
1855
+ "lstrip": false,
1856
+ "normalized": false,
1857
+ "rstrip": false,
1858
+ "single_word": false,
1859
+ "special": true
1860
+ },
1861
+ "157123": {
1862
+ "content": "<|reserved_token_228|>",
1863
+ "lstrip": false,
1864
+ "normalized": false,
1865
+ "rstrip": false,
1866
+ "single_word": false,
1867
+ "special": true
1868
+ },
1869
+ "157124": {
1870
+ "content": "<|reserved_token_229|>",
1871
+ "lstrip": false,
1872
+ "normalized": false,
1873
+ "rstrip": false,
1874
+ "single_word": false,
1875
+ "special": true
1876
+ },
1877
+ "157125": {
1878
+ "content": "<|reserved_token_230|>",
1879
+ "lstrip": false,
1880
+ "normalized": false,
1881
+ "rstrip": false,
1882
+ "single_word": false,
1883
+ "special": true
1884
+ },
1885
+ "157126": {
1886
+ "content": "<|reserved_token_231|>",
1887
+ "lstrip": false,
1888
+ "normalized": false,
1889
+ "rstrip": false,
1890
+ "single_word": false,
1891
+ "special": true
1892
+ },
1893
+ "157127": {
1894
+ "content": "<|reserved_token_232|>",
1895
+ "lstrip": false,
1896
+ "normalized": false,
1897
+ "rstrip": false,
1898
+ "single_word": false,
1899
+ "special": true
1900
+ },
1901
+ "157128": {
1902
+ "content": "<|reserved_token_233|>",
1903
+ "lstrip": false,
1904
+ "normalized": false,
1905
+ "rstrip": false,
1906
+ "single_word": false,
1907
+ "special": true
1908
+ },
1909
+ "157129": {
1910
+ "content": "<|reserved_token_234|>",
1911
+ "lstrip": false,
1912
+ "normalized": false,
1913
+ "rstrip": false,
1914
+ "single_word": false,
1915
+ "special": true
1916
+ },
1917
+ "157130": {
1918
+ "content": "<|reserved_token_235|>",
1919
+ "lstrip": false,
1920
+ "normalized": false,
1921
+ "rstrip": false,
1922
+ "single_word": false,
1923
+ "special": true
1924
+ },
1925
+ "157131": {
1926
+ "content": "<|reserved_token_236|>",
1927
+ "lstrip": false,
1928
+ "normalized": false,
1929
+ "rstrip": false,
1930
+ "single_word": false,
1931
+ "special": true
1932
+ },
1933
+ "157132": {
1934
+ "content": "<|reserved_token_237|>",
1935
+ "lstrip": false,
1936
+ "normalized": false,
1937
+ "rstrip": false,
1938
+ "single_word": false,
1939
+ "special": true
1940
+ },
1941
+ "157133": {
1942
+ "content": "<|reserved_token_238|>",
1943
+ "lstrip": false,
1944
+ "normalized": false,
1945
+ "rstrip": false,
1946
+ "single_word": false,
1947
+ "special": true
1948
+ },
1949
+ "157134": {
1950
+ "content": "<|reserved_token_239|>",
1951
+ "lstrip": false,
1952
+ "normalized": false,
1953
+ "rstrip": false,
1954
+ "single_word": false,
1955
+ "special": true
1956
+ },
1957
+ "157135": {
1958
+ "content": "<|reserved_token_240|>",
1959
+ "lstrip": false,
1960
+ "normalized": false,
1961
+ "rstrip": false,
1962
+ "single_word": false,
1963
+ "special": true
1964
+ },
1965
+ "157136": {
1966
+ "content": "<|reserved_token_241|>",
1967
+ "lstrip": false,
1968
+ "normalized": false,
1969
+ "rstrip": false,
1970
+ "single_word": false,
1971
+ "special": true
1972
+ },
1973
+ "157137": {
1974
+ "content": "<|reserved_token_242|>",
1975
+ "lstrip": false,
1976
+ "normalized": false,
1977
+ "rstrip": false,
1978
+ "single_word": false,
1979
+ "special": true
1980
+ },
1981
+ "157138": {
1982
+ "content": "<|reserved_token_243|>",
1983
+ "lstrip": false,
1984
+ "normalized": false,
1985
+ "rstrip": false,
1986
+ "single_word": false,
1987
+ "special": true
1988
+ },
1989
+ "157139": {
1990
+ "content": "<|reserved_token_244|>",
1991
+ "lstrip": false,
1992
+ "normalized": false,
1993
+ "rstrip": false,
1994
+ "single_word": false,
1995
+ "special": true
1996
+ },
1997
+ "157140": {
1998
+ "content": "<|reserved_token_245|>",
1999
+ "lstrip": false,
2000
+ "normalized": false,
2001
+ "rstrip": false,
2002
+ "single_word": false,
2003
+ "special": true
2004
+ },
2005
+ "157141": {
2006
+ "content": "<|reserved_token_246|>",
2007
+ "lstrip": false,
2008
+ "normalized": false,
2009
+ "rstrip": false,
2010
+ "single_word": false,
2011
+ "special": true
2012
+ },
2013
+ "157142": {
2014
+ "content": "<|reserved_token_247|>",
2015
+ "lstrip": false,
2016
+ "normalized": false,
2017
+ "rstrip": false,
2018
+ "single_word": false,
2019
+ "special": true
2020
+ },
2021
+ "157143": {
2022
+ "content": "<|reserved_token_248|>",
2023
+ "lstrip": false,
2024
+ "normalized": false,
2025
+ "rstrip": false,
2026
+ "single_word": false,
2027
+ "special": true
2028
+ },
2029
+ "157144": {
2030
+ "content": "<|reserved_token_249|>",
2031
+ "lstrip": false,
2032
+ "normalized": false,
2033
+ "rstrip": false,
2034
+ "single_word": false,
2035
+ "special": true
2036
+ },
2037
+ "157145": {
2038
+ "content": "<|reserved_token_250|>",
2039
+ "lstrip": false,
2040
+ "normalized": false,
2041
+ "rstrip": false,
2042
+ "single_word": false,
2043
+ "special": true
2044
+ },
2045
+ "157146": {
2046
+ "content": "<|reserved_token_251|>",
2047
+ "lstrip": false,
2048
+ "normalized": false,
2049
+ "rstrip": false,
2050
+ "single_word": false,
2051
+ "special": true
2052
+ },
2053
+ "157147": {
2054
+ "content": "<|reserved_token_252|>",
2055
+ "lstrip": false,
2056
+ "normalized": false,
2057
+ "rstrip": false,
2058
+ "single_word": false,
2059
+ "special": true
2060
+ },
2061
+ "157148": {
2062
+ "content": "<|reserved_token_253|>",
2063
+ "lstrip": false,
2064
+ "normalized": false,
2065
+ "rstrip": false,
2066
+ "single_word": false,
2067
+ "special": true
2068
+ },
2069
+ "157149": {
2070
+ "content": "<|reserved_token_254|>",
2071
+ "lstrip": false,
2072
+ "normalized": false,
2073
+ "rstrip": false,
2074
+ "single_word": false,
2075
+ "special": true
2076
+ },
2077
+ "157150": {
2078
+ "content": "<|reserved_token_255|>",
2079
+ "lstrip": false,
2080
+ "normalized": false,
2081
+ "rstrip": false,
2082
+ "single_word": false,
2083
+ "special": true
2084
+ },
2085
+ "157151": {
2086
+ "content": "<role>",
2087
+ "lstrip": false,
2088
+ "normalized": false,
2089
+ "rstrip": false,
2090
+ "single_word": false,
2091
+ "special": true
2092
+ },
2093
+ "157152": {
2094
+ "content": "</role>",
2095
+ "lstrip": false,
2096
+ "normalized": false,
2097
+ "rstrip": false,
2098
+ "single_word": false,
2099
+ "special": true
2100
+ }
2101
+ },
2102
+ "additional_special_tokens": [
2103
+ "<role>",
2104
+ "</role>"
2105
+ ],
2106
+ "auto_map": {
2107
+ "AutoTokenizer": [
2108
+ "tokenization_bailing.BailingTokenizer",
2109
+ null
2110
+ ]
2111
+ },
2112
+ "bos_token": "<|startoftext|>",
2113
+ "clean_up_tokenization_spaces": false,
2114
+ "cls_token": "[CLS]",
2115
+ "eos_token": "<|endoftext|>",
2116
+ "extra_special_tokens": {},
2117
+ "fast_tokenizer": true,
2118
+ "gmask_token": "[gMASK]",
2119
+ "merges_file": null,
2120
+ "model_max_length": 1000000000000000019884624838656,
2121
+ "pad_token": "<|endoftext|>",
2122
+ "tokenizer_class": "BailingTokenizer",
2123
+ "trust_remote_code": true
2124
+ }