-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdata_utils.py
More file actions
216 lines (190 loc) · 7.26 KB
/
Copy pathdata_utils.py
File metadata and controls
216 lines (190 loc) · 7.26 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
import torch
from torch.utils.data import DataLoader
from transformers import RobertaTokenizer, RobertaForSequenceClassification, AdamW
from datasets import load_dataset
from torch.utils.data import Dataset, DataLoader, Subset
from transformers import (
GPT2Tokenizer,
GPT2LMHeadModel,
AdamW,
get_linear_schedule_with_warmup,
)
from tqdm import tqdm
import numpy as np
import pandas as pd
from peft import get_peft_model, LoraConfig, TaskType
def load_and_preprocess_data(task):
if "mnli" in task:
dataset = load_dataset("glue", "mnli")
else:
dataset = load_dataset("glue", task)
tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
def tokenize_function(examples):
# Handle different input formats
if "premise" in examples and "hypothesis" in examples:
# MNLI and similar tasks
return tokenizer(
examples["premise"],
examples["hypothesis"],
truncation=True,
padding="max_length",
max_length=128,
)
elif "question" in examples and "sentence" in examples:
# QNLI and similar tasks
return tokenizer(
examples["question"],
examples["sentence"],
truncation=True,
padding="max_length",
max_length=128,
)
elif "sentence1" in examples and "sentence2" in examples:
# MRPC, STS-B
return tokenizer(
examples["sentence1"],
examples["sentence2"],
truncation=True,
padding="max_length",
max_length=128,
)
elif "question1" in examples and "question2" in examples:
# QQP
return tokenizer(
examples["question1"],
examples["question2"],
truncation=True,
padding="max_length",
max_length=128,
)
elif "sentence" in examples:
# CoLA, SST-2
return tokenizer(
examples["sentence"],
truncation=True,
padding="max_length",
max_length=128,
)
else:
raise ValueError(f"Unexpected format for task {task}")
tokenized_datasets = dataset.map(tokenize_function, batched=True)
if task == "cola":
tokenized_datasets = tokenized_datasets.remove_columns(["sentence", "idx"])
elif task == "sst2":
tokenized_datasets = tokenized_datasets.remove_columns(["sentence", "idx"])
elif task == "mrpc":
tokenized_datasets = tokenized_datasets.remove_columns(
["sentence1", "sentence2", "idx"]
)
elif task == "qqp":
tokenized_datasets = tokenized_datasets.remove_columns(
["question1", "question2", "idx"]
)
elif task == "stsb":
tokenized_datasets = tokenized_datasets.remove_columns(
["sentence1", "sentence2", "idx"]
)
elif task == "qnli":
tokenized_datasets = tokenized_datasets.remove_columns(
["question", "sentence", "idx"]
)
elif task == "rte":
tokenized_datasets = tokenized_datasets.remove_columns(
["sentence1", "sentence2", "idx"]
)
elif task == "wnli":
tokenized_datasets = tokenized_datasets.remove_columns(
["sentence1", "sentence2", "idx"]
)
elif task == "mnli_matched" or task == "mnli_mismatched" or task == "mnli":
tokenized_datasets = tokenized_datasets.remove_columns(
["premise", "hypothesis", "idx"]
)
else:
raise ValueError(f"Unexpected task {task}")
tokenized_datasets = tokenized_datasets.rename_column("label", "labels")
tokenized_datasets.set_format("torch")
if (
task == "cola"
or task == "sst2"
or task == "mrpc"
or task == "qqp"
or task == "stsb"
or task == "qnli"
or task == "rte"
or task == "wnli"
):
train_dataset = tokenized_datasets["train"]
val_dataset = tokenized_datasets["validation"]
test_dataset = tokenized_datasets["test"]
elif task == "mnli_matched":
train_dataset = tokenized_datasets["train"]
val_dataset = tokenized_datasets["validation_matched"]
test_dataset = tokenized_datasets["test_matched"]
elif task == "mnli_mismatched":
train_dataset = tokenized_datasets["train"]
val_dataset = tokenized_datasets["validation_mismatched"]
test_dataset = tokenized_datasets["test_mismatched"]
return train_dataset, val_dataset, test_dataset
def create_dataloader(dataset, args):
return DataLoader(dataset, batch_size=args.batch_size, shuffle=False)
def create_client_dataloaders_nlg(dataset, args):
client_data = [[] for _ in range(args.num_clients)]
for data in dataset:
client_idx = np.random.randint(args.num_clients)
client_data[client_idx].append(data)
return client_data
def create_client_dataloaders(dataset, args):
client_data = [[] for _ in range(args.num_clients)]
for data in dataset:
client_idx = np.random.randint(args.num_clients)
client_data[client_idx].append(data)
return [
DataLoader(cd, batch_size=args.batch_size, shuffle=True) for cd in client_data
]
def create_e2e_data():
def preprocess_function(examples):
inputs = examples["meaning_representation"]
targets = examples["human_reference"]
# Combine the input-output pair into a single text
model_inputs = [
f"{input_} -> {target} <|endoftext|>"
for input_, target in zip(inputs, targets)
]
only_inputs = [f"{input_} ->" for input_, target in zip(inputs, targets)]
# Tokenize the combined inputs
tokenized_inputs = tokenizer(
model_inputs,
max_length=512,
padding="max_length",
truncation=True,
return_tensors="pt",
)
tokenized_only_inputs = tokenizer(
only_inputs,
max_length=512,
padding="max_length",
truncation=True,
return_tensors="pt",
)
# Labels are the same as input_ids but shift them for next-token prediction
tokenized_inputs["labels"] = tokenized_inputs["input_ids"].clone()
# Set the labels to -100 where attention mask is 0 (this will ignore padding in loss computation)
tokenized_inputs["labels"][tokenized_inputs["attention_mask"] == 0] = -100
# set the labels to -100 where meaning representation input ids are present
tokenized_inputs["labels"][tokenized_only_inputs["attention_mask"] == 1] = -100
return tokenized_inputs
dataset = load_dataset("tuetschek/e2e_nlg")
from transformers import GPT2Tokenizer
# Load the GPT-2 tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
tokenizer.pad_token = (
tokenizer.eos_token
) # GPT-2 doesn't have a pad token, so we set it to the eos token
tokenized_datasets = dataset.map(preprocess_function, batched=True)
return (
tokenized_datasets["train"],
tokenized_datasets["validation"],
tokenized_datasets["test"],
tokenizer,
)