-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathchat_templete.py
More file actions
474 lines (384 loc) · 15.3 KB
/
Copy pathchat_templete.py
File metadata and controls
474 lines (384 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# Copyright 2024 the LlamaFactory team.
# Copyright (c) 2024, AIAK team. All rights reserved.
# This code was adopted from https://github.com/hiyouga/LLaMA-Factory
# and the source code is licensed under the Apache license found in the
# LICENSE file in the root directory of this source tree.
"""Chat templates
About chat templates, can see more info at
https://huggingface.co/docs/transformers/main/en/chat_templating#templates-for-chat-models.
"""
import re
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Set, Tuple, Type, Union
from aiak_training_llm.utils.constants import DataRoles
from .mm_plugin import Gemma4VLPlugin, MMPlugin, Qwen2VLPlugin
if TYPE_CHECKING:
from aiak_training_llm.tokenizer import AutoTokenizerFromHF
SlotsType = Sequence[Union[str, Set[str], Dict[str, str]]]
@dataclass
class Formatter(ABC):
"""Base class of all formatters."""
slots: SlotsType = field(default_factory=list)
@abstractmethod
def apply(self, **kwargs) -> SlotsType:
"""Apply the formatter to the given arguments"""
raise NotImplementedError
@dataclass
class EmptyFormatter(Formatter):
"""An empty formatter that does nothing"""
def __post_init__(self):
has_placeholder = False
for slot in filter(lambda s: isinstance(s, str), self.slots):
if re.search(r"\{\{[a-zA-Z_][a-zA-Z0-9_]*\}\}", slot):
has_placeholder = True
if has_placeholder:
raise ValueError("Empty formatter should not contain any placeholder.")
def apply(self, **kwargs) -> SlotsType:
"""Apply the formatter to the given arguments"""
return self.slots
@dataclass
class StringFormatter(Formatter):
"""String formatter"""
def __post_init__(self):
has_placeholder = False
for slot in filter(lambda s: isinstance(s, str), self.slots):
if re.search(r"\{\{[a-zA-Z_][a-zA-Z0-9_]*\}\}", slot):
has_placeholder = True
if not has_placeholder:
raise ValueError("A placeholder is required in the string formatter.")
def apply(self, **kwargs) -> SlotsType:
"""Apply the formatter to the given arguments"""
elements = []
for slot in self.slots:
if isinstance(slot, str):
for name, value in kwargs.items():
if not isinstance(value, str):
raise RuntimeError("Expected a string, got {}".format(value))
slot = slot.replace("{{" + name + "}}", value, 1)
elements.append(slot)
elif isinstance(slot, (dict, set)):
elements.append(slot)
else:
raise RuntimeError("Input must be string, set[str] or dict[str, str], got {}".format(type(slot)))
return elements
@dataclass
class ChatTemplate:
"""ChatTemplate class."""
format_user: Optional[Formatter] = None
format_assistant: Optional[Formatter] = None
format_system: Optional[Formatter] = None
format_separator: Optional[Formatter] = None
format_prefix: Optional[Formatter] = None
default_system: str = ""
stop_words: List[str] = field(default_factory=list)
efficient_eos: bool = False
replace_eos: bool = False
mm_plugin: Optional[MMPlugin] = None
def __post_init__(self):
if self.format_user is None:
self.format_user = StringFormatter(slots=["{{content}}"])
# if efficient_eos=true, we will not add eos_token among the multiple turns,
# and it will be added in the end of the last response.
eos_slots = [] if self.efficient_eos else [{"eos_token"}]
if self.format_assistant is None:
self.format_assistant = StringFormatter(slots=["{{content}}"] + eos_slots)
if self.format_system is None:
self.format_system = StringFormatter(slots=["{{content}}"])
if self.format_separator is None:
self.format_separator = EmptyFormatter()
if self.format_prefix is None:
self.format_prefix = EmptyFormatter()
def encode_multiturn(
self,
tokenizer: "AutoTokenizerFromHF",
messages: Sequence[Dict[str, str]],
system: Optional[str] = None,
) -> List[Tuple[List[int], List[int]]]:
"""
Returns multiple pairs of token ids representing prompts and responses respectively.
"""
if len(messages) % 2 != 0:
system = messages[0]['content']
messages = messages[1:]
encoded_messages = self._encode(tokenizer, messages, system)
return [(encoded_messages[i], encoded_messages[i + 1]) for i in range(0, len(encoded_messages), 2)]
def encode_oneturn(
self,
tokenizer: "AutoTokenizerFromHF",
messages: Sequence[Dict[str, str]],
system: Optional[str] = None,
) -> Tuple[List[int], List[int]]:
"""
Returns a single pair of token ids representing prompt and response respectively.
"""
encoded_messages = self._encode(tokenizer, messages, system)
prompt_ids = []
for encoded_ids in encoded_messages[:-1]:
prompt_ids += encoded_ids
answer_ids = encoded_messages[-1]
return prompt_ids, answer_ids
def _encode(
self,
tokenizer: "AutoTokenizerFromHF",
messages: Sequence[Dict[str, str]],
system: Optional[str],
) -> List[List[int]]:
"""
Encodes formatted inputs to pairs of token ids.
Turn 0: prefix + system + query resp
Turn t: sep + query resp
"""
system = system or self.default_system
encoded_messages = []
for i, message in enumerate(messages):
elements = []
if i == 0:
elements += self.format_prefix.apply()
if system:
elements += self.format_system.apply(content=system)
elif i > 0 and i % 2 == 0:
elements += self.format_separator.apply()
if message["role"] == DataRoles.USER:
elements += self.format_user.apply(content=message["content"], idx=str(i // 2))
elif message["role"] == DataRoles.ASSISTANT:
elements += self.format_assistant.apply(content=message["content"])
else:
raise NotImplementedError("Unexpected role: {}".format(message["role"]))
encoded_messages.append(self._convert_elements_to_ids(tokenizer, elements))
return encoded_messages
def _convert_elements_to_ids(
self,
tokenizer: "AutoTokenizerFromHF",
elements: "SlotsType",
) -> List[int]:
"""
Converts elements to token ids.
"""
token_ids = []
for elem in elements:
if isinstance(elem, str):
if len(elem) != 0:
token_ids += tokenizer.tokenize(elem, add_special_tokens=False)
elif isinstance(elem, dict):
token_ids += [tokenizer.convert_tokens_to_ids(elem.get("token"))]
elif isinstance(elem, set):
if "bos_token" in elem and tokenizer.bos is not None:
token_ids += [tokenizer.bos]
elif "eos_token" in elem and tokenizer.eos is not None:
token_ids += [tokenizer.eos]
else:
raise ValueError("Input must be string, set[str] or dict[str, str], got {}".format(type(elem)))
return token_ids
@classmethod
def from_name(cls, name: str) -> "ChatTemplate":
"""build template."""
return MAPPING_NAME_TO_TEMPLATE.get(name, None)
@dataclass
class Llama2Template(ChatTemplate):
"""LLaMA-2 Template"""
def _encode(
self,
tokenizer: "AutoTokenizerFromHF",
messages: Sequence[Dict[str, str]],
system: str,
) -> List[List[int]]:
"""
Encodes formatted inputs to pairs of token ids.
Turn 0: prefix + system + query resp
Turn t: sep + query resp
"""
system = system or self.default_system
encoded_messages = []
for i, message in enumerate(messages):
elements = []
system_text = ""
if i == 0:
elements += self.format_prefix.apply()
if system:
system_text = self.format_system.apply(content=system)[0]
if i > 0 and i % 2 == 0:
elements += self.format_separator.apply()
if message["role"] == DataRoles.USER:
elements += self.format_user.apply(content=system_text + message["content"])
elif message["role"] == DataRoles.ASSISTANT:
elements += self.format_assistant.apply(content=message["content"])
else:
raise NotImplementedError("Unexpected role: {}".format(message["role"]))
encoded_messages.append(self._convert_elements_to_ids(tokenizer, elements))
return encoded_messages
MAPPING_NAME_TO_TEMPLATE: Dict[str, ChatTemplate] = {}
def _register_chat_template(
name: str,
cls: Type[ChatTemplate] = ChatTemplate,
format_user: Optional[Formatter] = None,
format_assistant: Optional[Formatter] = None,
format_system: Optional[Formatter] = None,
format_separator: Optional[Formatter] = None,
format_prefix: Optional[Formatter] = None,
default_system: str = "",
stop_words: Sequence[str] = [],
efficient_eos: bool = False,
replace_eos: bool = False,
mm_plugin: Optional[MMPlugin] = None,
) -> None:
"""
Registers a chat template.
To add the following chat template:
```
[HUMAN]:
user prompt here
[AI]:
model response here
[HUMAN]:
user prompt here
[AI]:
model response here
```
The corresponding code should be:
```
_register_chat_template(
name="custom",
format_user=StringFormatter(slots=["[HUMAN]:\n{{content}}\n[AI]:\n"]),
format_separator=EmptyFormatter(slots=["\n\n"]),
efficient_eos=True,
)
```
"""
if name in MAPPING_NAME_TO_TEMPLATE:
raise ValueError(f"Cannot register duplicate template with name {name}.")
MAPPING_NAME_TO_TEMPLATE[name] = cls(
format_user=format_user,
format_assistant=format_assistant,
format_system=format_system,
format_separator=format_separator,
format_prefix=format_prefix,
default_system=default_system,
stop_words=stop_words,
efficient_eos=efficient_eos,
replace_eos=replace_eos,
mm_plugin=mm_plugin,
)
def get_support_templates() -> List[str]:
"""
Returns a list of supported chat templates.
"""
return list(MAPPING_NAME_TO_TEMPLATE.keys())
_register_chat_template(
name="empty",
efficient_eos=True,
)
_register_chat_template(
name="default",
format_user=StringFormatter(slots=["Human: {{content}}\nAssistant:"]),
format_system=StringFormatter(slots=["{{content}}\n"]),
format_separator=EmptyFormatter(slots=["\n"]),
)
_register_chat_template(
name="alpaca",
format_user=StringFormatter(slots=["### Instruction:\n{{content}}\n\n### Response:\n"]),
format_separator=EmptyFormatter(slots=["\n\n"]),
default_system=(
"Below is an instruction that describes a task. "
"Write a response that appropriately completes the request.\n\n"
),
)
_register_chat_template(
name="baichuan",
format_user=StringFormatter(slots=[{"token": "<reserved_102>"}, "{{content}}", {"token": "<reserved_103>"}]),
efficient_eos=True,
)
_register_chat_template(
name="baichuan2",
format_user=StringFormatter(slots=["<reserved_106>{{content}}<reserved_107>"]),
efficient_eos=True,
)
_register_chat_template(
name="llama2",
cls=Llama2Template,
format_user=StringFormatter(slots=[{"bos_token"}, "[INST] {{content}} [/INST]"]),
format_system=StringFormatter(slots=["<<SYS>>\n{{content}}\n<</SYS>>\n\n"]),
)
_register_chat_template(
name="llama2_zh",
cls=Llama2Template,
format_user=StringFormatter(slots=[{"bos_token"}, "[INST] {{content}} [/INST]"]),
format_system=StringFormatter(slots=["<<SYS>>\n{{content}}\n<</SYS>>\n\n"]),
default_system="You are a helpful assistant. 你是一个乐于助人的助手。",
)
_register_chat_template(
name="llama3",
format_user=StringFormatter(
slots=[
(
"<|start_header_id|>user<|end_header_id|>\n\n{{content}}<|eot_id|>"
"<|start_header_id|>assistant<|end_header_id|>\n\n"
)
]
),
format_system=StringFormatter(slots=["<|start_header_id|>system<|end_header_id|>\n\n{{content}}<|eot_id|>"]),
format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
stop_words=["<|eot_id|>"],
replace_eos=True,
)
_register_chat_template(
name="llama3.1",
format_user=StringFormatter(
slots=[
(
"<|start_header_id|>user<|end_header_id|>\n\n{{content}}<|eot_id|>"
"<|start_header_id|>assistant<|end_header_id|>\n\n"
)
]
),
format_system=StringFormatter(slots=["<|start_header_id|>system<|end_header_id|>\n\n{{content}}<|eot_id|>"]),
format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
stop_words=["<|eot_id|>"],
replace_eos=True,
)
_register_chat_template(
name="mistral",
format_user=StringFormatter(slots=["[INST] {{content}} [/INST]"]),
format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
)
_register_chat_template(
name="qwen",
format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
format_separator=EmptyFormatter(slots=["\n"]),
default_system="You are a helpful assistant.",
stop_words=["<|im_end|>"],
replace_eos=True,
)
_register_chat_template(
name="qwen2-vl",
format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
format_separator=EmptyFormatter(slots=["\n"]),
default_system="You are a helpful assistant.",
stop_words=["<|im_end|>"],
replace_eos=True,
mm_plugin=Qwen2VLPlugin(image_token="<|image_pad|>", video_token="<|video_pad|>"),
)
_register_chat_template(
name="deepseek",
format_user=StringFormatter(slots=["User: {{content}}\n\nAssistant:"]),
format_system=StringFormatter(slots=["{{content}}\n\n"]),
format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
)
_register_chat_template(
name="deepseek3",
format_user=StringFormatter(slots=["<|User|>{{content}}<|Assistant|>"]),
format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
)
_register_chat_template(
name="gemma4",
format_user=StringFormatter(
slots=["<|turn>user\n{{content}}<turn|>\n<|turn>model\n"]
),
format_assistant=StringFormatter(slots=["{{content}}<turn|>\n"]),
format_system=StringFormatter(slots=["<|turn>system\n{{content}}<turn|>\n"]),
format_separator=EmptyFormatter(slots=[""]),
format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
mm_plugin=Gemma4VLPlugin(image_token="<|image|>", video_token=None),
)