-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess_data.py
163 lines (151 loc) · 5.58 KB
/
preprocess_data.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
import json
import os
os.environ["TOKENIZERS_PARALLELISM"] = "true"
import torch
from datasets import load_dataset
from argparse import ArgumentParser
from transformers import AutoTokenizer
from multiprocessing import Pool
from tqdm import tqdm
parser = ArgumentParser()
parser.add_argument(
"--dataset_name_or_path",
type=str,
default="HuggingFaceH4/ultrafeedback_binarized",
)
parser.add_argument(
"--split",
type=str,
default="train",
)
parser.add_argument(
"--start",
type=int,
default=0,
)
parser.add_argument(
"--end",
type=int,
default=None,
)
parser.add_argument(
"--output_file",
type=str,
)
parser.add_argument(
"--tokenizer_name_or_path",
type=str,
required=True
)
parser.add_argument("--max_seq_length", type=int, default=4096)
parser.add_argument("--preprocessing_num_workers", type=int, default=64)
args = parser.parse_args()
tokenizer = AutoTokenizer.from_pretrained(args.tokenizer_name_or_path)
print(f"load tokenizer from {args.tokenizer_name_or_path} done.")
max_seq_length = args.max_seq_length
input_data = load_dataset(args.dataset_name_or_path)
if args.split:
input_data = input_data[args.split]
if args.end is None:
args.end = len(input_data)
input_data = input_data.select(range(args.start, args.end))
print(
f"load input data from {args.dataset_name_or_path} done. len(input_data): {len(input_data)}"
)
def encode_sft_example(example, verbose=False):
"""
This function encodes a single example into a format that can be used for sft training.
Here, we assume each example has a 'messages' field. Each message in it is a dict with 'role' and 'content' fields.
We use the `apply_chat_template` function from the tokenizer to tokenize the messages and prepare the input and label tensors.
"""
messages = example["messages"]
if len(messages) == 0:
raise ValueError("messages field is empty.")
if verbose:
chat_messages = tokenizer.apply_chat_template(
conversation=messages,
tokenize=False,
return_tensors="pt",
padding=False,
truncation=True,
max_length=max_seq_length,
add_generation_prompt=False,
)
print(f"chat_messages:\n[{chat_messages}]")
input_ids = tokenizer.apply_chat_template(
conversation=messages,
tokenize=True,
return_tensors="pt",
padding=False,
truncation=True,
max_length=max_seq_length,
add_generation_prompt=False,
)
labels = input_ids.clone()
# mask the non-assistant part for avoiding loss
for message_idx, message in enumerate(messages):
if message["role"] != "assistant":
# we calculate the start index of this non-assistant message
if message_idx == 0:
message_start_idx = 0
else:
message_start_idx = tokenizer.apply_chat_template(
conversation=messages[
:message_idx
], # here marks the end of the previous messages
tokenize=True,
return_tensors="pt",
padding=False,
truncation=True,
max_length=max_seq_length,
add_generation_prompt=False,
).shape[1]
# next, we calculate the end index of this non-assistant message
if (
message_idx < len(messages) - 1
and messages[message_idx + 1]["role"] == "assistant"
):
# for intermediate messages that follow with an assistant message, we need to
# set `add_generation_prompt=True` to avoid the assistant generation prefix being included in the loss
# (e.g., `<|assistant|>`)
message_end_idx = tokenizer.apply_chat_template(
conversation=messages[: message_idx + 1],
tokenize=True,
return_tensors="pt",
padding=False,
truncation=True,
max_length=max_seq_length,
add_generation_prompt=True,
).shape[1]
else:
# for the last message or the message that doesn't follow with an assistant message,
# we don't need to add the assistant generation prefix
message_end_idx = tokenizer.apply_chat_template(
conversation=messages[: message_idx + 1],
tokenize=True,
return_tensors="pt",
padding=False,
truncation=True,
max_length=max_seq_length,
add_generation_prompt=False,
).shape[1]
# set the label to -100 for the non-assistant part
labels[:, message_start_idx:message_end_idx] = -100
if max_seq_length and message_end_idx >= max_seq_length:
break
attention_mask = torch.ones_like(input_ids)
return {
"input_ids": input_ids.flatten().tolist(),
"labels": labels.flatten().tolist(),
"attention_mask": attention_mask.flatten().tolist(),
}
print(encode_sft_example(input_data[0], verbose=True))
tokenized_data = []
with Pool(args.preprocessing_num_workers) as p:
pbar = tqdm(input_data, desc=f"tokenizing")
for tokenized_example in p.imap(encode_sft_example, pbar):
dump = json.dumps(tokenized_example)
tokenized_data.append(dump)
with open(args.output_file, "w") as fw:
for dump in tokenized_data:
fw.write(dump + "\n")