-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathtest_chat_formatters.py
216 lines (178 loc) · 6.56 KB
/
test_chat_formatters.py
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
"""
Unit tests for chat formatters
"""
# Third Party
import pytest
# Local
from torchchat.generate import (
HFTokenizerChatFormatter,
Llama2ChatFormatter,
Llama3ChatFormatter,
)
## Helpers #####################################################################
class DummyTokenizer:
"""Dummy tokenizer that encodes as strings so it's easy to check formatting"""
def encode(self, text, *_, **__):
return text
class DummySPTokenizer(DummyTokenizer):
"""Emulated Sentencepiece tokenizer with bos/eos"""
bos = "<s>"
eos = "</s>"
class DummyLlama3Tokenizer(DummyTokenizer):
class _IdentityDict:
def __getitem__(self, key):
return key
special_tokens = _IdentityDict()
class DummyHFTokenizer(DummyTokenizer):
"""Dummy made up chat template scheme"""
# Sequence
bos = "<bos>"
# Turn
bot = "<bot>"
eot = "<eot>"
# Role
bor = "<bor>"
eor = "<eor>"
def apply_chat_template(self, messages, add_generation_prompt):
out = [self.bos]
role = None
for msg in messages:
role = msg["role"]
content = msg["content"]
out.append(f"{self.bot}{self.bor}{role}{self.eor}{content}{self.eot}")
if add_generation_prompt and role != "assistant":
out.append(f"{self.bot}{self.bor}assistant{self.eor}")
return "\n".join(out)
def check_rendering(fmt, messages, expected, add_generation_prompt):
"""Render messages and compare to expected output"""
assert "".join(fmt.encode_dialog_prompt(messages, add_generation_prompt)) == expected
def make_message(role, text):
return {"role": role, "content": text}
SYSTEM_PROMPT = "You are a helpful assistant, feel free to ask me anything."
USER1 = "Hello world!"
ASSISTANT1 = "Greetings! How can I help you?"
USER2 = "Why is the sky blue?"
ASSISTANT2 = "The sky appears blue because of a phenomenon called Rayleigh scattering."
# Stock sets of messages to test
MSGS_NO_SYS= [
make_message("user", USER1),
]
MSGS_SYS_USR = [
make_message("system", SYSTEM_PROMPT),
make_message("user", USER1),
]
MSGS_SYS_USR_ASST = [
make_message("system", SYSTEM_PROMPT),
make_message("user", USER1),
make_message("assistant", ASSISTANT1),
]
MSGS_MULTI_TURN = [
make_message("system", SYSTEM_PROMPT),
make_message("user", USER1),
make_message("assistant", ASSISTANT1),
make_message("user", USER2),
make_message("assistant", ASSISTANT2),
]
## Llama2ChatFormatter #########################################################
@pytest.mark.parametrize(
["messages", "expected"],
[
# single user message (no system prompt)
(MSGS_NO_SYS, f"<s>[INST] {USER1} [/INST]"),
# sys, usr
(MSGS_SYS_USR, f"""<s>[INST] <<SYS>>
{SYSTEM_PROMPT}
<</SYS>>
{USER1} [/INST]"""),
# sys, usr, asst
(MSGS_SYS_USR_ASST, f"""<s>[INST] <<SYS>>
{SYSTEM_PROMPT}
<</SYS>>
{USER1} [/INST] {ASSISTANT1} </s>
"""),
# sys, usr, asst, usr, asst
(MSGS_MULTI_TURN, f"""<s>[INST] <<SYS>>
{SYSTEM_PROMPT}
<</SYS>>
{USER1} [/INST] {ASSISTANT1} </s>
<s>[INST] {USER2} [/INST] {ASSISTANT2} </s>
"""),
]
)
def test_llama2_chat_formatter(messages, expected):
"""Tests for Llama2 following the official guide
https://www.llama.com/docs/model-cards-and-prompt-formats/meta-llama-2/
"""
tok = DummySPTokenizer()
fmt = Llama2ChatFormatter(tok)
# NOTE: add_generation_prompt not used by Llama2
check_rendering(fmt, messages, expected, True)
## Llama3ChatFormatter #########################################################
@pytest.mark.parametrize(
["messages", "expected"],
[
# single user message (no system prompt)
(MSGS_NO_SYS, f"""<|begin_of_text|><|start_header_id|>user<|end_header_id|>
{USER1}<|eot_id|>"""),
# sys, usr
(MSGS_SYS_USR, f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
{SYSTEM_PROMPT}<|eot_id|><|start_header_id|>user<|end_header_id|>
{USER1}<|eot_id|>"""),
# sys, usr, asst
(MSGS_SYS_USR_ASST, f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
{SYSTEM_PROMPT}<|eot_id|><|start_header_id|>user<|end_header_id|>
{USER1}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
{ASSISTANT1}<|eot_id|>"""),
# sys, usr, asst, usr, asst
(MSGS_MULTI_TURN, f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
{SYSTEM_PROMPT}<|eot_id|><|start_header_id|>user<|end_header_id|>
{USER1}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
{ASSISTANT1}<|eot_id|><|start_header_id|>user<|end_header_id|>
{USER2}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
{ASSISTANT2}<|eot_id|>"""),
]
)
@pytest.mark.parametrize("add_generation_prompt", [True, False])
def test_llama3_chat_formatter(messages, expected, add_generation_prompt):
"""Tests for Llama3 following the official guide
https://www.llama.com/docs/model-cards-and-prompt-formats/meta-llama-3/
"""
tok = DummyLlama3Tokenizer()
fmt = Llama3ChatFormatter(tok)
# No assistant prompt added if the last message is from the assistant
if add_generation_prompt and messages[-1]["role"] != "assistant":
expected += "<|start_header_id|>assistant<|end_header_id|>\n\n"
check_rendering(fmt, messages, expected, add_generation_prompt)
## HFTokenizerChatFormatter ####################################################
@pytest.mark.parametrize(
["messages", "expected"],
[
# single user message (no system prompt)
(MSGS_NO_SYS, f"""<bos>
<bot><bor>user<eor>{USER1}<eot>"""),
# sys, usr
(MSGS_SYS_USR, f"""<bos>
<bot><bor>system<eor>{SYSTEM_PROMPT}<eot>
<bot><bor>user<eor>{USER1}<eot>"""),
# sys, usr, asst
(MSGS_SYS_USR_ASST, f"""<bos>
<bot><bor>system<eor>{SYSTEM_PROMPT}<eot>
<bot><bor>user<eor>{USER1}<eot>
<bot><bor>assistant<eor>{ASSISTANT1}<eot>"""),
# sys, usr, asst, usr, asst
(MSGS_MULTI_TURN, f"""<bos>
<bot><bor>system<eor>{SYSTEM_PROMPT}<eot>
<bot><bor>user<eor>{USER1}<eot>
<bot><bor>assistant<eor>{ASSISTANT1}<eot>
<bot><bor>user<eor>{USER2}<eot>
<bot><bor>assistant<eor>{ASSISTANT2}<eot>"""),
]
)
@pytest.mark.parametrize("add_generation_prompt", [True, False])
def test_hf_chat_formatter(messages, expected, add_generation_prompt):
tok = DummyHFTokenizer()
fmt = HFTokenizerChatFormatter(tok)
# No assistant prompt added if the last message is from the assistant
if add_generation_prompt and messages[-1]["role"] != "assistant":
expected += f"\n{tok.bot}{tok.bor}assistant{tok.eor}"
check_rendering(fmt, messages, expected, add_generation_prompt)