-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
329 lines (261 loc) · 11.3 KB
/
utils.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
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
# Modified from https://github.com/microsoft/TransformerCompression
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
import datetime
import time
import gc
import inspect
import logging
import pathlib
from typing import TypeVar
import datasets
import torch
from torch.utils.data import DataLoader, Dataset, SubsetRandomSampler
from transformers import PreTrainedTokenizerBase
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def configure_logging(
log_to_console: bool = True,
log_to_file: bool = True,
log_dir: str = 'log',
level: int = logging.INFO,
) -> None:
handlers: list[logging.Handler] = []
if log_to_console:
handler = logging.StreamHandler()
handler.setLevel(level)
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
handlers.append(handler)
if log_to_file:
path = pathlib.Path.cwd() / log_dir / f'{datetime.datetime.now():log_%Y-%m-%d-%H-%M-%S}.log'
path.parent.mkdir(parents=True, exist_ok=True)
file_handler = logging.FileHandler(path, encoding='utf-8')
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s.%(msecs)04d\t%(levelname)s\t%(name)s\t%(message)s', datefmt='%Y-%m-%dT%H:%M:%S'
)
file_handler.setFormatter(formatter)
handlers.append(file_handler)
logging.basicConfig(
handlers=handlers,
level=logging.NOTSET,
)
def cleanup_memory() -> None:
"""Run GC and clear GPU memory."""
caller_name = ''
try:
caller_name = f' (from {inspect.stack()[1].function})'
except (ValueError, KeyError):
pass
def total_reserved_mem() -> int:
return sum(torch.cuda.memory_reserved(device=i) for i in range(torch.cuda.device_count()))
memory_before = total_reserved_mem()
# gc.collect and empty cache are necessary to clean up GPU memory if the model was distributed
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
memory_after = total_reserved_mem()
logging.debug(
f"GPU memory{caller_name}: {memory_before / (1024 ** 3):.2f} -> {memory_after / (1024 ** 3):.2f} GB"
f" ({(memory_after - memory_before) / (1024 ** 3):.2f} GB)"
)
T = TypeVar('T')
def map_tensors(obj: T, device: torch.device | str | None = None, dtype: torch.dtype | None = None) -> T:
"""Recursively map tensors to device and dtype."""
if isinstance(obj, torch.Tensor):
if device is not None:
obj = obj.to(device=device)
if dtype is not None:
obj = obj.to(dtype=dtype)
return obj
elif isinstance(obj, (list, tuple)):
return type(obj)(map_tensors(x, device, dtype) for x in obj)
elif isinstance(obj, dict):
return {k: map_tensors(v, device, dtype) for k, v in obj.items()} # type: ignore
else:
return obj
@torch.no_grad()
def evaluate_ppl(
model: torch.nn.Module, pad_token_id: int | None, testloader: DataLoader[dict[str, torch.Tensor]], silence=True
) -> float:
"""
Evaluate the model's perplexity on the test set using batch processing.
It is expected that model is already on the correct device.
"""
sync_gpus()
start_time = time.time()
model.eval()
if pad_token_id:
loss_fn = torch.nn.CrossEntropyLoss(reduction="none", ignore_index=pad_token_id)
else:
loss_fn = torch.nn.CrossEntropyLoss(reduction="none")
nlls = []
if not silence:
logging.info("Evaluating perplexity...")
for batch in testloader:
if not silence:
logging.debug(f"Evaluating batch {len(nlls)}")
batch = map_tensors(batch, device)
logits = model(**batch).logits
# shift outputs and labels autoregressively.
logits = logits[:, :-1, :]
shift_labels = batch["input_ids"][:, 1:]
# CrossEntropyLoss demands data dimension is dimension 1.
nll = loss_fn(logits.permute(0, 2, 1), shift_labels).float()
mask = shift_labels != loss_fn.ignore_index
nll_means = (nll * mask).sum(dim=1) / mask.sum(dim=1)
nlls.append(nll_means)
nlls_tensor = torch.cat(nlls)
ppl = torch.exp(nlls_tensor.mean())
sync_gpus()
elapsed = time.time() - start_time
if not silence:
logging.info(
"Time spent on evaluation: %s",
time.strftime("%H:%M:%S.{}".format(str(elapsed % 1)[2:])[:13], time.gmtime(elapsed)),
)
return ppl.item()
def sync_gpus() -> None:
"""Sync all GPUs to make sure all operations are finished, needed for correct benchmarking of latency/throughput."""
for i in range(torch.cuda.device_count()):
torch.cuda.synchronize(device=i)
def get_dataset(name: str) -> datasets.DatasetDict:
"""
Get the dataset from the HuggingFace datasets library.
Args:
name: The name of the HuggingFace dataset to load. Must be one of "wikitext2", "ptb", "c4" or "alpaca".
Returns:
The dataset.
"""
logging.info(f"Loading dataset: {name}")
ds_properties = {
"wikitext2": {"path": "/data/lgzhong/tiny/train/svd/data/wikitext", "config_name": "wikitext-2-raw-v1"},
"ptb": {"path": "ptb_text_only", "config_name": "penn_treebank"},
"c4": {
"path": "allenai/c4",
"config_name": "allenai--c4",
"data_files": {
"train": "en/c4-train.00000-of-01024.json.gz",
"validation": "en/c4-validation.00000-of-00008.json.gz",
},
"cols_to_remove": ['url', 'timestamp'],
},
"alpaca": {"path": "tatsu-lab/alpaca", "cols_to_remove": ['input', 'output', 'instruction']},
}
if name not in ds_properties:
raise NotImplementedError("The provided dataset is not supported")
properties = ds_properties[name]
ds = datasets.load_dataset(
properties["path"], name=properties.get("config_name"), data_files=properties.get("data_files")
)
if "cols_to_remove" in properties:
ds = ds.remove_columns(properties["cols_to_remove"])
# if alpaca, create a test and validation set from the training set
if name == "alpaca":
ds = ds["train"].train_test_split(test_size=0.2, seed=42)
temp_ds = ds.pop("test")
temp_ds = temp_ds.train_test_split(test_size=0.5, seed=42)
ds["test"] = temp_ds["train"]
ds["validation"] = temp_ds["test"]
logging.info("Loading dataset done")
return ds
def prepare_test_dataloader(
dataset: datasets.Dataset, tokenizer: PreTrainedTokenizerBase, seqlen: int = 2048, batch_size: int = 1
) -> DataLoader[dict[str, torch.Tensor]]:
"""
Get a DataLoader from a test dataset. This dataloader should be used when comparing WikiText2 perplexities with other papers, e.g. SparseGPT (arxiv.org/abs/2301.00774).
Args:
dataset: The dataset to create a dataloader from.
tokenizer: The tokenizer to use.
seqlen: The sequence length of sequences in the dataset.
batch_size: The batch size.
Returns:
A DataLoader.
"""
logging.info(f"Preparing test dataloader")
class TestDataset(Dataset):
def __init__(self, ds, tokenizer, seqlen=2048):
"""Tokenize the entire dataset and reshape it into sequences of length seqlen."""
tokenized_ds = tokenizer("\n\n".join(ds['text']), return_tensors='pt')
nsamples = tokenized_ds.input_ids.numel() // seqlen
input_ids = tokenized_ds.input_ids[0, : nsamples * seqlen]
input_ids = input_ids.reshape(nsamples, seqlen)
attn_mask = tokenized_ds.attention_mask[0, : nsamples * seqlen]
attn_mask = attn_mask.reshape(nsamples, seqlen)
self.input_ids = input_ids
self.attn_mask = attn_mask
def __getitem__(self, idx):
return {"input_ids": self.input_ids[idx], "attention_mask": self.attn_mask[idx]}
def __len__(self):
return len(self.input_ids)
test_ds = TestDataset(dataset, tokenizer, seqlen)
loader = DataLoader(test_ds, batch_size=batch_size)
logging.info(f"Preparing test dataloader done")
return loader
def prepare_dataloader(
dataset: datasets.Dataset,
tokenizer: PreTrainedTokenizerBase,
max_seqlen: int = 2048,
batch_size: int = 1,
nsamples: int = 128,
varied_seqlen: bool = False,
seed=42,
) -> DataLoader[dict[str, torch.Tensor]]:
"""
Get a DataLoader from a dataset.
Args:
dataset: The dataset to create a dataloader from.
tokenizer: The tokenizer to use.
max_seqlen: The maximum sequence length, used for truncation of sequences in the dataset.
batch_size: The batch size.
nsamples: The number of samples to produce.
varied_seqlen: If False, concatenate multiple examples from the dataset into one example until max_seqlen is reached.
seed: The seed for sampling the dataset.
Returns:
A DataLoader.
"""
logging.info(f"Preparing dataloader")
if not varied_seqlen and not nsamples:
logging.warning(
"varied_seqlen=False, but nsamples is not specified. This will lead to tokenization of the entire dataset, which will be slow."
)
data_name = dataset.column_names[0]
ds = dataset.filter(lambda x: len(x[data_name]) > 0)
if not varied_seqlen:
# create a new dataset where each example is a concatenation of multiple examples of total length = max_seqlen.
data_list = ds[data_name]
new_data_list = []
torch.manual_seed(seed)
indices = list(range(len(data_list)))
while len(new_data_list) < nsamples and len(indices) > 0:
start_idx = torch.randint(0, len(indices), (1,)).item()
idx = start_idx
tokens = []
while len(tokens) < max_seqlen and idx < len(indices):
item = data_list[indices[idx]]
sep = "" if not tokens else "\n\n"
tokens += tokenizer.tokenize(sep + item)
idx += 1
indices = indices[:start_idx] + indices[idx:] # remove the used indices
if len(tokens) >= max_seqlen:
tokens = tokens[:max_seqlen] # truncate to max_seqlen
new_data_list.append(tokenizer.convert_tokens_to_string(tokens))
ds = datasets.Dataset.from_dict({data_name: new_data_list})
def tokenize(data_batch):
# tokenize then pad each batch according to the longest sequence in the batch
batch = tokenizer(
data_batch[data_name],
padding="longest",
max_length=max_seqlen,
truncation=True,
return_tensors="pt",
)
batch["labels"] = batch["input_ids"].clone()
return batch
# tokenize lazily
ds.set_transform(tokenize)
torch.manual_seed(seed)
sampler = SubsetRandomSampler(torch.randperm(len(ds))[:nsamples])
loader = DataLoader(ds, batch_size=batch_size, sampler=sampler)
logging.info(f"Preparing dataloader done")
return loader