-
Notifications
You must be signed in to change notification settings - Fork 5
/
data.py
283 lines (249 loc) · 9.53 KB
/
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
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
# https://github.com/huggingface/optimum/blob/main/optimum/gptq/data.py
# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random
from typing import Any, Dict, List, Optional
import numpy as np
import torch
from datasets import load_dataset
"""
Set of utilities for loading most used datasets (original dataset from GPTQ paper) and be able to easily use them during quantization
"""
def prepare_dataset(examples: List[Dict[str, torch.LongTensor]],
batch_size: int = 1):
"""
Prepare the dataset by making sure that we have the right format and `batch_size`
Args:
examples (`List[Dict[str, torch.LongTensor]]`):
List of data to prepare
batch_size (`int`, defaults to `1`):
Batch size of the data
Returns:
` List[Dict[str, torch.LongTensor]]`: Batched dataset
"""
new_examples = []
for example in examples:
input_ids = example["input_ids"]
attention_mask = example["attention_mask"]
new_examples.append({
"input_ids": torch.LongTensor(input_ids),
"attention_mask": torch.LongTensor(attention_mask)
})
new_examples = [
collate_data(new_examples[start:start + batch_size])
for start in range(0, len(new_examples), batch_size)
]
return new_examples
def collate_data(
blocks: List[Dict[str, torch.LongTensor]],
contain_labels: bool = False,
) -> Dict[str, torch.LongTensor]:
"""
Collate data in `blocks`
Args:
blocks (`List[Dict[str, torch.LongTensor]]`):
List of tensors that we need to batch together
contain_labels (`bool`, defaults to `False`):
Set True to also process the labels
Returns:
`Dict[str, torch.LongTensor]`: Batched data
"""
input_ids_blocks = [block["input_ids"] for block in blocks]
attention_mask_blocks = [block["attention_mask"] for block in blocks]
if contain_labels:
label_blocks = [block["labels"] for block in blocks]
data = {
"input_ids": torch.cat(input_ids_blocks, dim=0).long(),
"attention_mask": torch.cat(attention_mask_blocks, dim=0).long(),
}
if contain_labels:
data["labels"] = torch.cat(label_blocks, dim=0).long()
return data
def get_wikitext2(tokenizer: Any,
seqlen: int,
nsamples: int,
split: str = "train"):
if split == "train":
data = load_dataset("wikitext", "wikitext-2-raw-v1", split="train")
elif split == "validation":
data = load_dataset("wikitext", "wikitext-2-raw-v1", split="test")
text = "".join([" \n" if s == "" else s for s in data["text"]])
enc = tokenizer(text, return_tensors="pt")
dataset = []
for k in range(nsamples):
i = random.randint(0, enc.input_ids.shape[1] - seqlen)
j = i + seqlen
inp = enc.input_ids[:, i:j]
attention_mask = torch.ones_like(inp)
dataset.append({"input_ids": inp, "attention_mask": attention_mask})
return dataset
def get_c4(tokenizer: Any, seqlen: int, nsamples: int, split: str = "train"):
if split == "train":
data = load_dataset(
"allenai/c4",
"allenai--c4",
data_files={"train": "en/c4-train.00000-of-01024.json.gz"},
split="train")
elif split == "validation":
data = load_dataset(
"allenai/c4",
"allenai--c4",
data_files={
"validation": "en/c4-validation.00000-of-00008.json.gz"
},
split="validation",
)
dataset = []
for _ in range(nsamples):
while True:
i = random.randint(0, len(data) - 1)
enc = tokenizer(data[i]["text"], return_tensors="pt")
if enc.input_ids.shape[1] >= seqlen:
break
i = random.randint(0, enc.input_ids.shape[1] - seqlen)
j = i + seqlen
inp = enc.input_ids[:, i:j]
attention_mask = torch.ones_like(inp)
dataset.append({"input_ids": inp, "attention_mask": attention_mask})
return dataset
def get_c4_new(tokenizer: Any,
seqlen: int,
nsamples: int,
split: str = "train"):
if split == "train":
data = load_dataset(
"allenai/c4",
"allenai--c4",
data_files={"train": "en/c4-train.00000-of-01024.json.gz"},
split="train")
elif split == "validation":
data = load_dataset(
"allenai/c4",
"allenai--c4",
data_files={
"validation": "en/c4-validation.00000-of-00008.json.gz"
},
split="validation",
)
dataset = []
for _ in range(nsamples):
while True:
i = random.randint(0, len(data) - 1)
enc = tokenizer(data[i]["text"], return_tensors="pt")
if enc.input_ids.shape[1] >= seqlen:
break
i = random.randint(0, enc.input_ids.shape[1] - seqlen)
j = i + seqlen
inp = enc.input_ids[:, i:j]
attention_mask = torch.ones_like(inp)
dataset.append({"input_ids": inp, "attention_mask": attention_mask})
return dataset
def get_ptb(tokenizer: Any, seqlen: int, nsamples: int, split: str = "train"):
if split == "train":
data = load_dataset("ptb_text_only", "penn_treebank", split="train")
elif split == "validation":
data = load_dataset("ptb_text_only",
"penn_treebank",
split="validation")
enc = tokenizer(" ".join(data["sentence"]), return_tensors="pt")
dataset = []
for _ in range(nsamples):
i = random.randint(0, enc.input_ids.shape[1] - seqlen)
j = i + seqlen
inp = enc.input_ids[:, i:j]
attention_mask = torch.ones_like(inp)
dataset.append({"input_ids": inp, "attention_mask": attention_mask})
return dataset
def get_ptb_new(tokenizer: Any,
seqlen: int,
nsamples: int,
split: str = "train"):
if split == "train":
data = load_dataset("ptb_text_only", "penn_treebank", split="train")
elif split == "validation":
data = load_dataset("ptb_text_only", "penn_treebank", split="test")
enc = tokenizer(" ".join(data["sentence"]), return_tensors="pt")
dataset = []
for _ in range(nsamples):
i = random.randint(0, enc.input_ids.shape[1] - seqlen)
j = i + seqlen
inp = enc.input_ids[:, i:j]
attention_mask = torch.ones_like(inp)
dataset.append({"input_ids": inp, "attention_mask": attention_mask})
return dataset
def get_redpajama(tokenizer: Any, seqlen: int, nsamples: int, split: str = "train"):
assert split == "train"
data = load_dataset(
"togethercomputer/RedPajama-Data-1T-Sample",
split="train")
dataset = []
for _ in range(nsamples):
while True:
i = random.randint(0, len(data) - 1)
enc = tokenizer(data[i]["text"], return_tensors="pt")
if enc.input_ids.shape[1] >= seqlen:
break
i = random.randint(0, enc.input_ids.shape[1] - seqlen)
j = i + seqlen
inp = enc.input_ids[:, i:j]
attention_mask = torch.ones_like(inp)
dataset.append({"input_ids": inp, "attention_mask": attention_mask})
return dataset
def get_dataset(dataset_name: str,
tokenizer: Any,
nsamples: int = 128,
seqlen: int = 2048,
seed: int = 0,
split: str = "train"):
"""
Get the dataset from the original paper of GPTQ
Args:
dataset_name (`str`):
Dataset name. Available options are `['wikitext2', 'c4', 'ptb', 'c4-new', 'ptb_new']`.
tokenizer (`Any`):
Tokenizer of the model
nsamples (`int`, defaults to `128`):
Number of samples
seqlen (`int`, defaults to `2048`):
The sequence length of the model
seed (`int`, defaults to `0`):
Seed
split (`str`, defaults to `train`):
Split of the dataset. Can be either "train" or "validation"
Returns:
`List[Dict[str,torch.LongTensor]]`: The tokenized dataset.
"""
random.seed(seed)
np.random.seed(seed)
torch.random.manual_seed(seed)
get_dataset_map = {
"wikitext2": get_wikitext2,
"c4": get_c4,
"c4-new": get_c4_new,
"ptb": get_ptb,
"ptb-new": get_ptb_new,
"redpajama": get_redpajama,
}
if split not in ["train", "validation"]:
raise ValueError(
f"The split need to be 'train' or 'validation' but found {split}")
if dataset_name not in get_dataset_map:
raise ValueError(
f"Expected a value in {list(get_dataset_map.keys())} but found {dataset_name}"
)
get_dataset_fn = get_dataset_map[dataset_name]
return get_dataset_fn(tokenizer=tokenizer,
nsamples=nsamples,
seqlen=seqlen,
split=split)