-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataset.py
319 lines (271 loc) · 10.3 KB
/
dataset.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
import torch
from torch.utils.data import Dataset
from transformers import AutoTokenizer
from preprocess import clean_code, summary_code_cell
class PointwiseDataset(Dataset):
def __init__(self, df, model_name_or_path, total_max_len, md_max_len, ctx):
super().__init__()
self.df = df.reset_index(drop=True)
self.md_max_len = md_max_len
self.total_max_len = total_max_len
self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
self.ctx = ctx
def __getitem__(self, index):
row = self.df.iloc[index]
inputs = self.tokenizer.encode_plus(
clean_code(row.source),
None,
add_special_tokens=True,
max_length=self.md_max_len,
return_token_type_ids=True,
truncation=True,
)
# 개별 코드 셀 토큰의 최대 길이를 동적으로 결정
num_codes = len(self.ctx[str(row.id)]["codes"])
code_max_len_ = (self.total_max_len - self.md_max_len) // num_codes
code_inputs = self.tokenizer.batch_encode_plus(
[clean_code(str(x)) for x in self.ctx[str(row.id)]["codes"]],
add_special_tokens=False,
max_length=code_max_len_,
truncation=True,
)
sep_token_id = self.tokenizer.sep_token_id
pad_token_id = self.tokenizer.pad_token_id
ids = inputs["input_ids"]
for x in code_inputs["input_ids"]:
ids.extend([sep_token_id] + x)
ids = ids[: self.total_max_len - 1] + [sep_token_id]
mask = [1] * len(ids)
if len(ids) != self.total_max_len:
ids = ids + [pad_token_id] * (self.total_max_len - len(ids))
mask = mask + [pad_token_id] * (self.total_max_len - len(mask))
assert len(ids) == self.total_max_len
ids = torch.LongTensor(ids)
mask = torch.LongTensor(mask)
label = torch.FloatTensor([row.pct_rank])
return ids, mask, label
def __len__(self):
return self.df.shape[0]
class PairwiseDataset(Dataset):
def __init__(
self,
samples,
df,
model_name_or_path,
total_max_len=96,
md_max_len=48,
):
super().__init__()
self.samples = samples
unique_ids = [
f"{n_id}-{cell_id}"
for n_id, cell_id in zip(df["id"].values, df["cell_id"].values)
]
self.id2src = dict(zip(unique_ids, df["source"].values))
self.total_max_len = total_max_len
self.md_max_len = md_max_len
self.code_max_len = total_max_len - md_max_len
self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
def __getitem__(self, index):
n_id, md_cell_id, code_cell_id, label = self.samples[index]
md_unique_id = f"{n_id}-{md_cell_id}"
md_inputs = self.tokenizer.encode_plus(
clean_code(self.id2src[md_unique_id]),
None,
add_special_tokens=False,
max_length=self.md_max_len,
return_token_type_ids=True,
truncation=True,
)
code_unique_id = f"{n_id}-{code_cell_id}"
code_inputs = self.tokenizer.encode_plus(
clean_code(summary_code_cell(self.id2src[code_unique_id])),
None,
add_special_tokens=False,
max_length=self.code_max_len,
return_token_type_ids=True,
truncation=True,
)
cls_token_id = self.tokenizer.cls_token_id
sep_token_id = self.tokenizer.sep_token_id
pad_token_id = self.tokenizer.pad_token_id
md_inputs["input_ids"] = md_inputs["input_ids"][: self.md_max_len - 1]
code_inputs["input_ids"] = code_inputs["input_ids"][: self.code_max_len - 2]
ids = (
[cls_token_id]
+ md_inputs["input_ids"]
+ [sep_token_id]
+ code_inputs["input_ids"]
+ [sep_token_id]
)
ids = ids[: self.total_max_len]
mask = [1] * len(ids)
if len(ids) != self.total_max_len:
ids = ids + [pad_token_id] * (self.total_max_len - len(ids))
mask = mask + [pad_token_id] * (self.total_max_len - len(mask))
assert len(ids) == self.total_max_len
ids = torch.LongTensor(ids)
mask = torch.LongTensor(mask)
label = torch.FloatTensor([label])
return ids, mask, label
def __len__(self):
return len(self.samples)
class CTPairwiseDataset(Dataset):
def __init__(
self,
samples,
df,
model_name_or_path,
total_max_len=96,
md_max_len=48,
):
super().__init__()
self.samples = samples
unique_ids = [
f"{n_id}-{cell_id}"
for n_id, cell_id in zip(df["id"].values, df["cell_id"].values)
]
self.id2src = dict(zip(unique_ids, df["source"].values))
self.total_max_len = total_max_len
self.md_max_len = md_max_len
self.code_max_len = total_max_len - md_max_len
self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
def __getitem__(self, index):
n_id, md_cell_id, code_cell_id, label = self.samples[index]
md_unique_id = f"{n_id}-{md_cell_id}"
md_inputs = self.tokenizer.encode_plus(
clean_code(summary_code_cell(self.id2src[md_unique_id])),
None,
add_special_tokens=False,
max_length=self.md_max_len,
return_token_type_ids=True,
truncation=True,
)
code_unique_id = f"{n_id}-{code_cell_id}"
code_inputs = self.tokenizer.encode_plus(
clean_code(summary_code_cell(self.id2src[code_unique_id])),
None,
add_special_tokens=False,
max_length=self.code_max_len,
return_token_type_ids=True,
truncation=True,
)
cls_token_id = self.tokenizer.cls_token_id
sep_token_id = self.tokenizer.sep_token_id
pad_token_id = self.tokenizer.pad_token_id
md_inputs["input_ids"] = md_inputs["input_ids"][: self.md_max_len - 1]
code_inputs["input_ids"] = code_inputs["input_ids"][: self.code_max_len - 2]
ids = (
[cls_token_id]
+ md_inputs["input_ids"]
+ [sep_token_id]
+ code_inputs["input_ids"]
+ [sep_token_id]
)
ids = ids[: self.total_max_len]
mask = [1] * len(ids)
if len(ids) != self.total_max_len:
ids = ids + [pad_token_id] * (self.total_max_len - len(ids))
mask = mask + [pad_token_id] * (self.total_max_len - len(mask))
assert len(ids) == self.total_max_len
ids = torch.LongTensor(ids)
mask = torch.LongTensor(mask)
label = torch.FloatTensor([label])
return ids, mask, label
def __len__(self):
return len(self.samples)
class SiameseDataset(Dataset):
def __init__(
self,
samples,
df,
model_name_or_path,
total_max_len=128,
):
super().__init__()
self.samples = samples
unique_ids = [
f"{n_id}-{cell_id}"
for n_id, cell_id in zip(df["id"].values, df["cell_id"].values)
]
self.id2src = dict(zip(unique_ids, df["source"].values))
self.total_max_len = total_max_len
self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
def __getitem__(self, index):
n_id, md_cell_id, code_cell_id, label = self.samples[index]
md_unique_id = f"{n_id}-{md_cell_id}"
md_inputs = self.tokenizer.encode_plus(
clean_code(self.id2src[md_unique_id]),
None,
add_special_tokens=True,
max_length=self.total_max_len,
return_token_type_ids=True,
truncation=True,
)
code_unique_id = f"{n_id}-{code_cell_id}"
code_inputs = self.tokenizer.encode_plus(
clean_code(self.id2src[code_unique_id]),
None,
add_special_tokens=True,
max_length=self.total_max_len,
return_token_type_ids=True,
truncation=True,
)
sep_token_id = self.tokenizer.sep_token_id
pad_token_id = self.tokenizer.pad_token_id
md_ids = md_inputs["input_ids"]
if len(md_ids) >= self.total_max_len:
md_ids = md_ids[: self.total_max_len - 1] + [sep_token_id]
md_mask = [1] * len(md_ids)
if len(md_ids) != self.total_max_len:
md_ids = md_ids + [pad_token_id] * (self.total_max_len - len(md_ids))
md_mask = md_mask + [pad_token_id] * (self.total_max_len - len(md_mask))
code_ids = code_inputs["input_ids"]
if len(code_ids) >= self.total_max_len:
code_ids = code_ids[: self.total_max_len - 1] + [sep_token_id]
code_mask = [1] * len(code_ids)
if len(code_ids) != self.total_max_len:
code_ids = code_ids + [pad_token_id] * (self.total_max_len - len(code_ids))
code_mask = code_mask + [pad_token_id] * (
self.total_max_len - len(code_mask)
)
return (
torch.LongTensor(md_ids),
torch.LongTensor(md_mask),
torch.LongTensor(code_ids),
torch.LongTensor(code_mask),
torch.FloatTensor([label]),
)
def __len__(self):
return len(self.samples)
if __name__ == "__main__":
import json
import pandas as pd
from train import generate_pairs_with_label
if False:
df = pd.read_csv("./data/valid.csv")
samples = generate_pairs_with_label(df)
dataset = PairwiseDataset(samples, df, "microsoft/codebert-base")
if False:
df_valid_md = (
pd.read_csv("./data/valid_md.csv")
.drop("parent_id", axis=1)
.dropna()
.reset_index(drop=True)
)
valid_ctx = json.load(open("./data/valid_ctx_40.json"))
dataset = PointwiseDataset(
df_valid_md,
model_name_or_path="microsoft/codebert-base",
md_max_len=64,
total_max_len=512,
ctx=valid_ctx,
)
if False:
df = pd.read_csv("./data/valid.csv")
samples = generate_pairs_with_label(df)
dataset = SimPairwiseDataset(samples, df, "microsoft/codebert-base")
for idx, data in enumerate(dataset):
print(data)
if idx > 100:
break