-
Notifications
You must be signed in to change notification settings - Fork 2
/
glue_datamodule.py
147 lines (125 loc) · 4.46 KB
/
glue_datamodule.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
import warnings
from functools import partial
from typing import Literal
import lightning as L
from datasets import load_dataset
from lightning.pytorch.utilities.types import EVAL_DATALOADERS, TRAIN_DATALOADERS
from torch.utils.data import DataLoader
warnings.filterwarnings(
"ignore", ".*Consider increasing the value of the `num_workers` argument*"
)
TASK_NAME = Literal[
"cola",
"sst2",
"mrpc",
"qqp",
"stsb",
"mnli",
"qnli",
"rte",
"wnli",
"ax",
]
class GLUEDataModule(L.LightningDataModule):
task_text_field_map = {
"cola": ["sentence"],
"sst2": ["sentence"],
"mrpc": ["sentence1", "sentence2"],
"qqp": ["question1", "question2"],
"stsb": ["sentence1", "sentence2"],
"mnli": ["premise", "hypothesis"],
"qnli": ["question", "sentence"],
"rte": ["sentence1", "sentence2"],
"wnli": ["sentence1", "sentence2"],
"ax": ["premise", "hypothesis"],
}
glue_task_num_labels = {
"cola": 2,
"sst2": 2,
"mrpc": 2,
"qqp": 2,
"stsb": 1,
"mnli": 3,
"qnli": 2,
"rte": 2,
"wnli": 2,
"ax": 3,
}
def __init__(
self,
task_name: TASK_NAME = "mrpc",
batch_size: int = 32,
num_workers: int = 0,
pin_memory: bool = False,
):
super().__init__()
self.save_hyperparameters()
self.task_name = task_name
self.num_labels = self.glue_task_num_labels[task_name]
self.text_fields = self.task_text_field_map[task_name]
def prepare_data(self) -> None:
# setup first to prevent datasets cache conflicts in multiple processes.
self.setup()
def setup(self, stage: str | None = None) -> None:
if not hasattr(self, "datasets"):
convert_to_features = self.trainer.model.convert_to_features
preprocess_fn = partial(self._preprocess, text_fields=self.text_fields)
def preprocess(x):
return convert_to_features(preprocess_fn(x))
datasets = load_dataset("glue", self.task_name)
columns_names = self.text_fields + ["label", "idx"]
self.datasets = datasets.map(
preprocess,
batched=True,
remove_columns=columns_names,
)
self.datasets.set_format(type="torch")
self.val_splits = [x for x in self.datasets if "validation" in x]
self.test_splits = [x for x in self.datasets if "test" in x]
self.collate_fn = getattr(self.trainer.model, "collate_fn", None)
def train_dataloader(self) -> TRAIN_DATALOADERS:
return DataLoader(
dataset=self.datasets["train"],
batch_size=self.hparams.batch_size,
num_workers=self.hparams.num_workers,
pin_memory=self.hparams.pin_memory,
collate_fn=self.collate_fn,
persistent_workers=self.hparams.num_workers > 0,
shuffle=True,
)
def val_dataloader(self) -> EVAL_DATALOADERS:
val_dataloaders = [
DataLoader(
dataset=self.datasets[x],
batch_size=self.hparams.batch_size,
num_workers=self.hparams.num_workers,
pin_memory=self.hparams.pin_memory,
collate_fn=self.collate_fn,
persistent_workers=self.hparams.num_workers > 0,
shuffle=False,
)
for x in self.val_splits
]
return val_dataloaders[0] if len(val_dataloaders) == 1 else val_dataloaders
def test_dataloader(self) -> EVAL_DATALOADERS:
test_dataloaders = [
DataLoader(
dataset=self.datasets[x],
batch_size=self.hparams.batch_size,
num_workers=self.hparams.num_workers,
pin_memory=self.hparams.pin_memory,
collate_fn=self.collate_fn,
persistent_workers=self.hparams.num_workers > 0,
shuffle=False,
)
for x in self.test_splits
]
return test_dataloaders[0] if len(test_dataloaders) == 1 else test_dataloaders
@staticmethod
def _preprocess(batch, text_fields):
if len(text_fields) > 1:
text = list(zip(batch[text_fields[0]], batch[text_fields[1]], strict=True))
else:
text = batch[text_fields[0]]
labels = batch["label"]
return {"text": text, "labels": labels}