-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththe_glue.py
294 lines (272 loc) · 11.7 KB
/
the_glue.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
import itertools
import numpy as np
from datasets import load_dataset, Dataset
from torch.utils.data import DataLoader
from transformers import RobertaTokenizerFast, T5ForConditionalGeneration
from Modules.Code2Explanation.code2doc import Code2DocModule
from Modules.Code2Code.Extracontent.code_snippet_dataset import CodeSnippetDataset
from Modules.IntentClustering.data2clusters import IntentClustering
from Modules.ScoreClusters.clusters2score import ScoreClusters
from Modules.Code2Code.models.t5_code_2_code_model import T5Code2CodeModel
from pprint import pprint
def run_end_to_end_with_parameters(
FUNCTIONS_DATASET_URI,
MAX_FUNCTION_STRING_LENGTH,
C2D_LLM,
IC_METHOD,
IC_EMBEDDER,
IC_KVAL,
SC_LOWPERC,
SC_BOUNDARY,
SC_METHOD,
SC_SCORING,
C2C_MODEL_OUTPUT_DIR,
C2C_LLM,
C2C_TEST_SIZE,
C2C_LR,
C2C_EPOCH_N,
C2C_BATCH_SIZE,
C2C_WEIGHT_DECAY,
):
assert C2D_LLM in ('CODETRANS', 'CODEX', 'GPT')
assert IC_METHOD in ("kmeans", "dbscan")
assert IC_EMBEDDER in ("tfidf", "strans", "elmo")
assert SC_METHOD in ('PERCENTILE', 'SHARED')
assert SC_SCORING in ('QUADRATIC', 'LINEAR')
assert C2C_LLM in ('CODE-T5')
assert 1 <= C2C_EPOCH_N <= 5
assert 1 <= C2C_BATCH_SIZE <= 64
assert 0.01 <= C2C_WEIGHT_DECAY <= 0.1
dataset = load_dataset(FUNCTIONS_DATASET_URI, split="train")
detailed_docs = np.array(dataset["detailed_description"])
NUM_FUNCTIONS_TO_FINETUNE_UNDER = len(detailed_docs[detailed_docs != ""]) - 500
C2C_MODEL_OUTPUT_DIR = str(NUM_FUNCTIONS_TO_FINETUNE_UNDER) + "_" + C2C_MODEL_OUTPUT_DIR
code_snippets = dataset.filter(lambda example: len(example["function"].split()) <= MAX_FUNCTION_STRING_LENGTH)[:NUM_FUNCTIONS_TO_FINETUNE_UNDER]
code2doc = Code2DocModule()
data_with_docs = code2doc.get_docs(code_snippets, C2D_LLM = C2D_LLM)
doc2clusters = IntentClustering(function_ids=data_with_docs['function_ids'], code_reference=data_with_docs['code_reference'])
clusters = doc2clusters.core_get_clusters(embedder=IC_EMBEDDER, method=IC_METHOD, n_clusters=IC_KVAL,
eps=0.5, min_samples=5, n_jobs=-1,
doc_source='Detailed' if C2D_LLM == 'GPT' else 'CodeTrans')
clusters2scoredDataset = ScoreClusters(clusters, data_with_docs['code_reference'],
SC_SCORING=SC_SCORING,
SC_METHOD=SC_METHOD,
SC_LOWPERC=SC_LOWPERC if SC_METHOD == 'PERCENTILE' else None,
SC_HIGHPERC=100-SC_LOWPERC if SC_METHOD == 'PERCENTILE' else None,
SC_BOUNDARY=SC_BOUNDARY if SC_METHOD == 'SHARED' else None)
scored_dataset = Dataset.from_dict(clusters2scoredDataset.get_scored_dataset().shuffle(seed=420)[:100_000])
# try:
# scored_dataset.push_to_hub(C2C_MODEL_OUTPUT_DIR);
# except Exception as e:
# print(f"Pushing dataset failure due to {e}")
model = T5Code2CodeModel("small")
model.train(scored_dataset,
C2C_MODEL_OUTPUT_DIR,
C2C_TEST_SIZE=C2C_TEST_SIZE,
C2C_LR=C2C_LR,
C2C_BATCH_SIZE=C2C_BATCH_SIZE,
C2C_WEIGHT_DECAY=C2C_WEIGHT_DECAY,
C2C_EPOCH_N=C2C_EPOCH_N
)
def compute_quadratic_reputation_score(features):
return (features[0]**2) + (features[1]**1.5) + features[2]
def create_score_column(entries):
# print(entries.keys())
entries["score"] = [compute_quadratic_reputation_score(feature_set) for feature_set in entries["features"]]
return entries
def simulate_baseline():
FUNCTIONS_DATASET_URI = "michaelnath/annotated_github_dataset_2"
ds = load_dataset(FUNCTIONS_DATASET_URI, split='train')
augmented_ds = ds.shuffle(seed=420)
# augmented_ds = ds.map(create_score_column, batched=True, batch_size=8).sort("score")
first_half = list(augmented_ds[:round(len(augmented_ds)/2)]["function"])
second_half = list(augmented_ds[round(len(augmented_ds) / 2):]["function"])
mappings = list(itertools.product(first_half, second_half))[:100_000]
bad_code = np.array(mappings)[:, 0]
good_code = np.array(mappings)[:, 1]
model = T5Code2CodeModel("small")
print(bad_code[0])
print(good_code[0])
scored_dataset = Dataset.from_dict({"input": bad_code, "target": good_code})
print(scored_dataset[0])
C2C_MODEL_OUTPUT_DIR = "baseline_codet5_50_50_split_with_reps"
C2C_TEST_SIZE = 0.02
C2C_LR = 1e-4
C2C_BATCH_SIZE = 8
C2C_WEIGHT_DECAY = 0.01
C2C_EPOCH_N = 1
print(len(scored_dataset))
model.train(scored_dataset,
C2C_MODEL_OUTPUT_DIR,
C2C_TEST_SIZE=C2C_TEST_SIZE,
C2C_LR=C2C_LR,
C2C_BATCH_SIZE=C2C_BATCH_SIZE,
C2C_WEIGHT_DECAY=C2C_WEIGHT_DECAY,
C2C_EPOCH_N=C2C_EPOCH_N
)
def simulate():
C2D_LLMS=['GPT', 'CODETRANS']
# Two
SC_LOWPERCS=[10, 40]
SC_BOUNDARIES=[50]
IC_METHODS=["kmeans", "dbscan"]
IC_EMBEDDERS=["strans"]
# Two
SC_SCORING=["QUADRATIC"]
# Three
IC_KVALS=[30, 60, 50, 120, 180, 250]
C2C_LLMS=['CODE-T5']
C2C_TEST_SIZE=[0.02]
C2C_BATCH_SIZES=[16]
C2C_WEIGHT_DECAYS=[0.01]
C2C_EPOCH_NS=[1]
# Two
C2C_LR=[0.01, 3e-4, 1e-4]
shared_combination = itertools.product(
C2D_LLMS,
SC_BOUNDARIES,
IC_METHODS,
IC_EMBEDDERS,
SC_SCORING,
["SHARED"],
IC_KVALS,
C2C_LLMS,
C2C_TEST_SIZE,
C2C_BATCH_SIZES,
C2C_WEIGHT_DECAYS,
C2C_EPOCH_NS,
C2C_LR
)
percentile_combinations = itertools.product(
C2D_LLMS,
SC_LOWPERCS,
IC_METHODS,
IC_EMBEDDERS,
SC_SCORING,
["PERCENTILE"],
IC_KVALS,
C2C_LLMS,
C2C_TEST_SIZE,
C2C_BATCH_SIZES,
C2C_WEIGHT_DECAYS,
C2C_EPOCH_NS,
C2C_LR
)
combinations = itertools.chain(shared_combination, percentile_combinations)
for combination in list(combinations):
output_dir_name = "_".join([str(x) for x in combination])
try:
run_end_to_end_with_parameters(
FUNCTIONS_DATASET_URI="michaelnath/annotated_github_dataset_2",
MAX_FUNCTION_STRING_LENGTH=512,
C2D_LLM = combination[0],
SC_LOWPERC = combination[1] if combination[5] == "PERCENTILE" else None,
SC_BOUNDARY = combination[1] if combination[5] == "SHARED" else None,
IC_METHOD = combination[2],
IC_EMBEDDER = combination[3],
SC_SCORING = combination[4],
SC_METHOD = combination[5],
IC_KVAL = combination[6],
C2C_LLM = combination[7],
C2C_TEST_SIZE=combination[8],
C2C_BATCH_SIZE=combination[9],
C2C_WEIGHT_DECAY=combination[10],
C2C_EPOCH_N=combination[11],
C2C_LR=combination[12],
C2C_MODEL_OUTPUT_DIR=output_dir_name
)
except Exception as e:
print(f"Main failure: {e}")
def evaluate_models_on_test_dataset():
C2D_LLMS=['GPT', 'CODETRANS']
# Two
SC_LOWPERCS=[10, 40]
SC_BOUNDARIES=[50]
IC_METHODS=["kmeans", "dbscan"]
IC_EMBEDDERS=["strans"]
# Two
SC_SCORING=["QUADRATIC"]
# Three
IC_KVALS=[30, 60, 50, 120, 180, 250]
C2C_LLMS=['CODE-T5']
C2C_TEST_SIZE=[0.02]
C2C_BATCH_SIZES=[16]
C2C_WEIGHT_DECAYS=[0.01]
C2C_EPOCH_NS=[1]
# Two
C2C_LR=[0.01, 3e-4, 1e-4]
shared_combination = itertools.product(
C2D_LLMS,
SC_BOUNDARIES,
IC_METHODS,
IC_EMBEDDERS,
SC_SCORING,
["SHARED"],
IC_KVALS,
C2C_LLMS,
C2C_TEST_SIZE,
C2C_BATCH_SIZES,
C2C_WEIGHT_DECAYS,
C2C_EPOCH_NS,
C2C_LR
)
percentile_combinations = itertools.product(
C2D_LLMS,
SC_LOWPERCS,
IC_METHODS,
IC_EMBEDDERS,
SC_SCORING,
["PERCENTILE"],
IC_KVALS,
C2C_LLMS,
C2C_TEST_SIZE,
C2C_BATCH_SIZES,
C2C_WEIGHT_DECAYS,
C2C_EPOCH_NS,
C2C_LR
)
combinations = itertools.chain(shared_combination, percentile_combinations)
for combination in list(combinations):
output_dir_name = "_".join([str(x) for x in combination])
try:
model = T5ForConditionalGeneration.from_pretrained("8681_" + output_dir_name)
except Exception as e:
print("Error Fetching Model as it doesn't exit")
continue
FUNCTIONS_DATASET_URI="michaelnath/annotated_github_dataset_2"
dataset = load_dataset(FUNCTIONS_DATASET_URI, split="train")
detailed_docs = np.array(dataset["detailed_description"])
NUM_FUNCTIONS_TO_FINETUNE_UNDER = len(detailed_docs[detailed_docs != ""]) - 500
MAX_FUNCTION_STRING_LENGTH=512,
code_snippets = dataset.filter(lambda example: len(example["function"].split()) <= MAX_FUNCTION_STRING_LENGTH)[NUM_FUNCTIONS_TO_FINETUNE_UNDER:]
code2doc = Code2DocModule()
C2D_LLM = combination[0],
SC_LOWPERC = combination[1] if combination[5] == "PERCENTILE" else None,
SC_BOUNDARY = combination[1] if combination[5] == "SHARED" else None,
IC_METHOD = combination[2]
IC_EMBEDDER = combination[3]
SC_SCORING = combination[4]
SC_METHOD = combination[5]
IC_KVAL = combination[6]
C2C_TEST_SIZE=combination[8]
data_with_docs = code2doc.get_docs(code_snippets, C2D_LLM = C2D_LLM)
doc2clusters = IntentClustering(function_ids=data_with_docs['function_ids'], code_reference=data_with_docs['code_reference'])
clusters = doc2clusters.core_get_clusters(embedder=IC_EMBEDDER, method=IC_METHOD, n_clusters=IC_KVAL,
eps=0.5, min_samples=5, n_jobs=-1,
doc_source='Detailed' if C2D_LLM == 'GPT' else 'CodeTrans')
clusters2scoredDataset = ScoreClusters(clusters, data_with_docs['code_reference'],
SC_SCORING=SC_SCORING,
SC_METHOD=SC_METHOD,
SC_LOWPERC=SC_LOWPERC if SC_METHOD == 'PERCENTILE' else None,
SC_HIGHPERC=100-SC_LOWPERC if SC_METHOD == 'PERCENTILE' else None,
SC_BOUNDARY=SC_BOUNDARY if SC_METHOD == 'SHARED' else None)
scored_dataset = Dataset.from_dict(clusters2scoredDataset.get_scored_dataset())
loader = DataLoader(scored_dataset, batch_size=8, num_workers=2)
for _, batch in enumerate(loader):
print(batch["input"])
break
break
if __name__ == "__main__":
# simulate()
simulate_baseline()
# evaluate_models_on_test_dataset()