-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_args.py
302 lines (285 loc) · 10.5 KB
/
dataset_args.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
"""
DatasetArgs Class
=================
"""
from dataclasses import dataclass
import textattack
from textattack.shared.utils import ARGS_SPLIT_TOKEN, load_module_from_file
HUGGINGFACE_DATASET_BY_MODEL = {
#
# bert-base-uncased
#
"bert-base-uncased-ag-news": ("ag_news", None, "test"),
"bert-base-uncased-cola": ("glue", "cola", "validation"),
"bert-base-uncased-imdb": ("imdb", None, "test"),
"bert-base-uncased-mnli": (
"glue",
"mnli",
"validation_matched",
None,
{0: 1, 1: 2, 2: 0},
),
"bert-base-uncased-mrpc": ("glue", "mrpc", "validation"),
"bert-base-uncased-qnli": ("glue", "qnli", "validation"),
"bert-base-uncased-qqp": ("glue", "qqp", "validation"),
"bert-base-uncased-rte": ("glue", "rte", "validation"),
"bert-base-uncased-sst2": ("glue", "sst2", "validation"),
"bert-base-uncased-stsb": (
"glue",
"stsb",
"validation",
None,
None,
None,
5.0,
),
"bert-base-uncased-wnli": ("glue", "wnli", "validation"),
"bert-base-uncased-mr": ("rotten_tomatoes", None, "test"),
"bert-base-uncased-snli": ("snli", None, "test", None, {0: 1, 1: 2, 2: 0}),
"bert-base-uncased-yelp": ("yelp_polarity", None, "test"),
#
# distilbert-base-cased
#
"distilbert-base-cased-cola": ("glue", "cola", "validation"),
"distilbert-base-cased-mrpc": ("glue", "mrpc", "validation"),
"distilbert-base-cased-qqp": ("glue", "qqp", "validation"),
"distilbert-base-cased-snli": ("snli", None, "test"),
"distilbert-base-cased-sst2": ("glue", "sst2", "validation"),
"distilbert-base-cased-stsb": (
"glue",
"stsb",
"validation",
None,
None,
None,
5.0,
),
"distilbert-base-uncased-ag-news": ("ag_news", None, "test"),
"distilbert-base-uncased-cola": ("glue", "cola", "validation"),
"distilbert-base-uncased-imdb": ("imdb", None, "test"),
"distilbert-base-uncased-mnli": (
"glue",
"mnli",
"validation_matched",
None,
{0: 1, 1: 2, 2: 0},
),
"distilbert-base-uncased-mr": ("rotten_tomatoes", None, "test"),
"distilbert-base-uncased-mrpc": ("glue", "mrpc", "validation"),
"distilbert-base-uncased-qnli": ("glue", "qnli", "validation"),
"distilbert-base-uncased-rte": ("glue", "rte", "validation"),
"distilbert-base-uncased-wnli": ("glue", "wnli", "validation"),
#
# roberta-base (RoBERTa is cased by default)
#
"roberta-base-ag-news": ("ag_news", None, "test"),
"roberta-base-cola": ("glue", "cola", "validation"),
"roberta-base-imdb": ("imdb", None, "test"),
"roberta-base-mr": ("rotten_tomatoes", None, "test"),
"roberta-base-mrpc": ("glue", "mrpc", "validation"),
"roberta-base-qnli": ("glue", "qnli", "validation"),
"roberta-base-rte": ("glue", "rte", "validation"),
"roberta-base-sst2": ("glue", "sst2", "validation"),
"roberta-base-stsb": ("glue", "stsb", "validation", None, None, None, 5.0),
"roberta-base-wnli": ("glue", "wnli", "validation"),
#
# albert-base-v2 (ALBERT is cased by default)
#
"albert-base-v2-ag-news": ("ag_news", None, "test"),
"albert-base-v2-cola": ("glue", "cola", "validation"),
"albert-base-v2-imdb": ("imdb", None, "test"),
"albert-base-v2-mr": ("rotten_tomatoes", None, "test"),
"albert-base-v2-rte": ("glue", "rte", "validation"),
"albert-base-v2-qqp": ("glue", "qqp", "validation"),
"albert-base-v2-snli": ("snli", None, "test"),
"albert-base-v2-sst2": ("glue", "sst2", "validation"),
"albert-base-v2-stsb": ("glue", "stsb", "validation", None, None, None, 5.0),
"albert-base-v2-wnli": ("glue", "wnli", "validation"),
"albert-base-v2-yelp": ("yelp_polarity", None, "test"),
#
# xlnet-base-cased
#
"xlnet-base-cased-cola": ("glue", "cola", "validation"),
"xlnet-base-cased-imdb": ("imdb", None, "test"),
"xlnet-base-cased-mr": ("rotten_tomatoes", None, "test"),
"xlnet-base-cased-mrpc": ("glue", "mrpc", "validation"),
"xlnet-base-cased-rte": ("glue", "rte", "validation"),
"xlnet-base-cased-stsb": (
"glue",
"stsb",
"validation",
None,
None,
None,
5.0,
),
"xlnet-base-cased-wnli": ("glue", "wnli", "validation"),
}
#
# Models hosted by textattack.
#
TEXTATTACK_DATASET_BY_MODEL = {
#
# LSTMs
#
"lstm-ag-news": ("ag_news", None, "test"),
"lstm-imdb": ("imdb", None, "test"),
"lstm-mr": ("rotten_tomatoes", None, "test"),
"lstm-sst2": ("glue", "sst2", "validation"),
"lstm-yelp": ("yelp_polarity", None, "test"),
#
# CNNs
#
"cnn-ag-news": ("ag_news", None, "test"),
"cnn-imdb": ("imdb", None, "test"),
"cnn-mr": ("rotten_tomatoes", None, "test"),
"cnn-sst2": ("glue", "sst2", "validation"),
"cnn-yelp": ("yelp_polarity", None, "test"),
#
# T5 for translation
#
"t5-en-de": (
"textattack.datasets.helpers.TedMultiTranslationDataset",
"en",
"de",
),
"t5-en-fr": (
"textattack.datasets.helpers.TedMultiTranslationDataset",
"en",
"fr",
),
"t5-en-ro": (
"textattack.datasets.helpers.TedMultiTranslationDataset",
"en",
"de",
),
#
# T5 for summarization
#
"t5-summarization": ("gigaword", None, "test"),
}
@dataclass
class DatasetArgs:
"""Arguments for loading dataset from command line input."""
dataset_by_model: str = None
dataset_from_huggingface: str = None
dataset_from_file: str = None
dataset_split: str = None
filter_by_labels: list = None
@classmethod
def _add_parser_args(cls, parser):
"""Adds dataset-related arguments to an argparser."""
dataset_group = parser.add_mutually_exclusive_group()
dataset_group.add_argument(
"--dataset-by-model",
type=str,
required=False,
default=None,
help="Dataset to load depending on the name of the model",
)
dataset_group.add_argument(
"--dataset-from-huggingface",
type=str,
required=False,
default=None,
help="Dataset to load from `datasets` repository.",
)
dataset_group.add_argument(
"--dataset-from-file",
type=str,
required=False,
default=None,
help="Dataset to load from a file.",
)
parser.add_argument(
"--dataset-split",
type=str,
required=False,
default=None,
help="Split of dataset to use when specifying --dataset-by-model or --dataset-from-huggingface.",
)
parser.add_argument(
"--filter-by-labels",
nargs="+",
type=int,
required=False,
default=None,
help="List of labels to keep in the dataset and discard all others.",
)
return parser
@classmethod
def _create_dataset_from_args(cls, args):
"""Given ``DatasetArgs``, return specified
``textattack.dataset.Dataset`` object."""
assert isinstance(
args, cls
), f"Expect args to be of type `{type(cls)}`, but got type `{type(args)}`."
# Automatically detect dataset for huggingface & textattack models.
# This allows us to use the --model shortcut without specifying a dataset.
if hasattr(args, "model"):
args.dataset_by_model = args.model
if args.dataset_by_model in HUGGINGFACE_DATASET_BY_MODEL:
args.dataset_from_huggingface = HUGGINGFACE_DATASET_BY_MODEL[
args.dataset_by_model
]
elif args.dataset_by_model in TEXTATTACK_DATASET_BY_MODEL:
dataset = TEXTATTACK_DATASET_BY_MODEL[args.dataset_by_model]
if dataset[0].startswith("textattack"):
# unsavory way to pass custom dataset classes
# ex: dataset = ('textattack.datasets.helpers.TedMultiTranslationDataset', 'en', 'de')
dataset = eval(f"{dataset[0]}")(*dataset[1:])
return dataset
else:
args.dataset_from_huggingface = dataset
# Get dataset from args.
if args.dataset_from_file:
textattack.shared.logger.info(
f"Loading model and tokenizer from file: {args.model_from_file}"
)
if ARGS_SPLIT_TOKEN in args.dataset_from_file:
dataset_file, dataset_name = args.dataset_from_file.split(
ARGS_SPLIT_TOKEN
)
else:
dataset_file, dataset_name = args.dataset_from_file, "dataset"
try:
dataset_module = load_module_from_file(dataset_file)
except Exception:
raise ValueError(f"Failed to import file {args.dataset_from_file}")
try:
dataset = getattr(dataset_module, dataset_name)
except AttributeError:
raise AttributeError(
f"Variable ``dataset`` not found in module {args.dataset_from_file}"
)
elif args.dataset_from_huggingface:
dataset_args = args.dataset_from_huggingface
if isinstance(dataset_args, str):
if ARGS_SPLIT_TOKEN in dataset_args:
dataset_args = dataset_args.split(ARGS_SPLIT_TOKEN)
else:
dataset_args = (dataset_args,)
if args.dataset_split:
if len(dataset_args) > 1:
dataset_args = (
dataset_args[:2] + (args.dataset_split,) + dataset_args[3:]
)
dataset = textattack.datasets.HuggingFaceDataset(
*dataset_args, shuffle=False
)
else:
dataset = textattack.datasets.HuggingFaceDataset(
*dataset_args, split=args.dataset_split, shuffle=False
)
else:
dataset = textattack.datasets.HuggingFaceDataset(
*dataset_args, shuffle=False
)
else:
raise ValueError("Must supply pretrained model or dataset")
assert isinstance(
dataset, textattack.datasets.Dataset
), "Loaded `dataset` must be of type `textattack.datasets.Dataset`."
if args.filter_by_labels:
dataset.filter_by_labels_(args.filter_by_labels)
return dataset