-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_utils.py
More file actions
237 lines (207 loc) · 8.71 KB
/
Copy pathdata_utils.py
File metadata and controls
237 lines (207 loc) · 8.71 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
import random
from datasets import load_dataset
import torch
from transformers import AutoTokenizer
# Only for evaluation
def get_wikitext2(
tokenizer: AutoTokenizer,
sequence_length: int,
) -> list[torch.Tensor]:
# the canonical wikitext lives under the Salesforce namespace; the bare "wikitext" id is rejected by newer
# huggingface_hub (HfUriError: "Repository id must be 'namespace/name'").
test_dataset_raw = load_dataset("Salesforce/wikitext", "wikitext-2-raw-v1", split="test")
test_dataset_tok = tokenizer("\n\n".join(test_dataset_raw["text"]), return_tensors="pt").input_ids
num_test_sequences = test_dataset_tok.numel() // sequence_length
test_loader = []
for i in range(num_test_sequences):
test_loader.append(test_dataset_tok[:, i * sequence_length : (i + 1) * sequence_length])
return test_loader
def get_c4(
tokenizer: AutoTokenizer,
max_sequence_length: int,
num_calibration_samples: int,
seed: int = 42
) -> list[torch.Tensor]:
assert num_calibration_samples is not None
train_datasetraw = load_dataset(
'allenai/c4',
data_files={'train': 'en/c4-train.00000-of-01024.json.gz'},
split='train'
)
all_id =[]
trainloader = []
for _ in range(num_calibration_samples):
while True:
i = random.randint(0, len(train_datasetraw) - 1)
if i in all_id:
continue
trainenc = tokenizer(train_datasetraw[i]['text'], return_tensors='pt')
if trainenc.input_ids.shape[1] >= max_sequence_length:
break
all_id.append(i)
i = random.randint(0, trainenc.input_ids.shape[1] - max_sequence_length)
tokenized_sample = trainenc.input_ids[:, i:i + max_sequence_length]
trainloader.append(tokenized_sample)
return trainloader
def get_open_thoughts(
tokenizer: AutoTokenizer,
max_sequence_length: int,
num_calibration_samples: int | None = None,
seed: int = 42
) -> list[torch.Tensor]:
train_dataset_raw = load_dataset("open-thoughts/OpenThoughts-114k", split="train")
if num_calibration_samples:
train_dataset_raw = train_dataset_raw.shuffle(seed=seed).select(range(num_calibration_samples))
# Preprocess the data into the format the model is trained with.
def preprocess(example):
messages = []
# add system prompt
messages.append({"role": "system", "content": example['system']})
# add dialogue
for message in example['conversations']:
messages.append({"role": message["from"], "content": message["value"]})
return {"text": tokenizer.apply_chat_template(messages, tokenize=False)}
train_dataset_raw = train_dataset_raw.map(preprocess)
# Tokenize the data
def tokenize(sample):
return tokenizer(
sample["text"],
padding=False,
max_length=max_sequence_length,
truncation=True,
add_special_tokens=False,
)
train_dataset = train_dataset_raw.map(tokenize, remove_columns=train_dataset_raw.column_names)
train_dataset = [torch.tensor(sample['input_ids']).unsqueeze(0) for sample in train_dataset]
return train_dataset
def get_open_platypus(
tokenizer: AutoTokenizer,
max_sequence_length: int,
num_calibration_samples: int | None = None,
seed: int = 42
) -> list[torch.Tensor]:
train_dataset_raw = load_dataset("garage-bAInd/Open-Platypus", split="train")
if num_calibration_samples:
train_dataset_raw = train_dataset_raw.shuffle(seed=seed).select(range(num_calibration_samples))
# Preprocess the data into the format the model is trained with.
def preprocess(example):
messages = [
{"role": "user", "content": example["instruction"]},
{"role": "assistant", "content": example["output"]},
]
return {"text": tokenizer.apply_chat_template(messages, tokenize=False)}
train_dataset_raw = train_dataset_raw.map(preprocess)
# Tokenize the data
def tokenize(sample):
return tokenizer(
sample["text"],
padding=False,
max_length=max_sequence_length,
truncation=True,
add_special_tokens=False,
)
train_dataset = train_dataset_raw.map(tokenize, remove_columns=train_dataset_raw.column_names)
train_dataset = [torch.tensor(sample['input_ids']).unsqueeze(0) for sample in train_dataset]
return train_dataset
def get_fineweb_edu(
tokenizer: AutoTokenizer,
max_sequence_length: int,
num_calibration_samples: int,
seed: int = 42
) -> list[torch.Tensor]:
assert num_calibration_samples is not None
train_dataset_raw = load_dataset("HuggingFaceFW/fineweb-edu", "sample-10BT", split="train", streaming=True)
train_dataset_raw = train_dataset_raw.shuffle(seed=seed, buffer_size=1_000)
trainloader = []
for j, sample in enumerate(train_dataset_raw):
trainenc = tokenizer(
sample['text'],
return_tensors="pt"
)
if trainenc.input_ids.shape[1] < max_sequence_length:
continue
i = random.randint(0, trainenc.input_ids.shape[1] - max_sequence_length)
tokenized_sample = trainenc.input_ids[:, i:i + max_sequence_length]
trainloader.append(tokenized_sample)
if len(trainloader) >= num_calibration_samples:
break
return trainloader
def get_ultrachat_200k(
tokenizer: AutoTokenizer,
max_sequence_length: int,
num_calibration_samples: int | None = None,
seed: int = 42
) -> list[torch.Tensor]:
train_dataset_raw = load_dataset("HuggingFaceH4/ultrachat_200k", split="train_sft")
if num_calibration_samples:
train_dataset_raw = train_dataset_raw.shuffle(seed=seed).select(range(num_calibration_samples))
# Preprocess the data into the format the model is trained with.
def preprocess(example):
return {"text": tokenizer.apply_chat_template(example["messages"], tokenize=False)}
train_dataset_raw = train_dataset_raw.map(preprocess)
# Tokenize the data
def tokenize(sample):
return tokenizer(
sample["text"],
padding=False,
max_length=max_sequence_length,
truncation=True,
add_special_tokens=False,
)
train_dataset = train_dataset_raw.map(tokenize, remove_columns=train_dataset_raw.column_names)
train_dataset = [torch.tensor(sample['input_ids']).unsqueeze(0) for sample in train_dataset]
return train_dataset
def get_tulu3_sft_mixture(
tokenizer: AutoTokenizer,
max_sequence_length: int,
num_calibration_samples: int | None = None,
seed: int = 42
) -> list[torch.Tensor]:
# Load raw dataset
train_dataset_raw = load_dataset("allenai/tulu-3-sft-mixture", split="train")
# Optionally subsample early for efficiency
if num_calibration_samples:
train_dataset_raw = train_dataset_raw.shuffle(seed=seed).select(range(num_calibration_samples))
# Preprocess into chat text
def preprocess(example):
return {
"text": tokenizer.apply_chat_template(
example["messages"], tokenize=False
)
}
train_dataset_raw = train_dataset_raw.map(preprocess)
# Tokenize into input_ids
def tokenize(sample):
tokenized = tokenizer(
sample["text"],
padding=False,
max_length=max_sequence_length,
truncation=True,
add_special_tokens=False,
)
return tokenized
tokenized_dataset = train_dataset_raw.map(tokenize, remove_columns=train_dataset_raw.column_names)
# Convert to list of tensors
train_dataset = [torch.tensor(sample['input_ids']).unsqueeze(0) for sample in tokenized_dataset]
return train_dataset
def get_data(
dataset_name: str,
tokenizer: AutoTokenizer,
max_sequence_length: int,
num_calibration_samples: int | None = None,
seed: int = 42
) -> list[torch.Tensor]:
if dataset_name == "open-thoughts":
return get_open_thoughts(tokenizer, max_sequence_length, num_calibration_samples, seed)
if dataset_name == "open-platypus":
return get_open_platypus(tokenizer, max_sequence_length, num_calibration_samples, seed)
if dataset_name == "ultrachat-200k":
return get_ultrachat_200k(tokenizer, max_sequence_length, num_calibration_samples, seed)
if dataset_name == "fineweb-edu":
return get_fineweb_edu(tokenizer, max_sequence_length, num_calibration_samples, seed)
if dataset_name == "c4":
return get_c4(tokenizer, max_sequence_length, num_calibration_samples, seed)
if dataset_name == "tulu":
return get_tulu3_sft_mixture(tokenizer, max_sequence_length, num_calibration_samples, seed)
else:
raise ValueError("Unknown dataset")