Repository navigation
Expand file tree
/
Copy pathselect_successful_tests.py
More file actions
259 lines (227 loc) · 9 KB
/
Copy pathselect_successful_tests.py
File metadata and controls
259 lines (227 loc) · 9 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
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
# Copyright (c) 2019-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
# Translate sentences from the input stream.
# The model will be faster is sentences are sorted by length.
# Input sentences must have the same tokenization and BPE codes than the ones used in the model.
#
import argparse
from logging import getLogger
from pathlib import Path
import fastBPE
import numpy as np
import pandas as pd
from utils import get_beam_size, add_root_to_path
add_root_to_path()
from codegen_sources.model.preprocess import XLM_preprocess
SOURCE_LANG = "java"
FAILURE = "failure"
logger = getLogger()
def get_arguments():
parser = argparse.ArgumentParser(description="")
parser.add_argument(
"--input_df", help="path to the input files",
)
parser.add_argument(
"--output_folder", type=str, help="where the files should be outputed",
)
parser.add_argument(
"--langs",
type=list,
nargs="+",
help="List of target langs",
default=["python", "cpp"],
)
parser.add_argument(
"--bpe_path", type=str, help="where the files should be outputed",
)
parser.add_argument(
"--bpe_vocab", type=str, help="where the files should be outputed",
)
args = parser.parse_args()
return args
def main(input_path, output_folder, langs, bpe_model, bpe_vocab):
input_path = Path(input_path)
input_dfs_paths = {
lang: input_path.joinpath(f"test_results_{lang}_df.csv") for lang in langs
}
test_results_dfs = {
lang: pd.read_csv(path) for lang, path in input_dfs_paths.items()
}
test_results_dfs = select_tests_several_asserts(test_results_dfs)
for ref_l in langs[1:]:
assert len(test_results_dfs[ref_l]) == len(
test_results_dfs[langs[0]]
), f"length of input {len(test_results_dfs[ref_l])} for {ref_l} while it is {len(test_results_dfs[langs[0]])} for {langs[0]}"
assert (
test_results_dfs[ref_l][f"{SOURCE_LANG}_function"]
== test_results_dfs[langs[0]][f"{SOURCE_LANG}_function"]
).all(), f"Dataframes order for {ref_l} and {langs[0]} do not match"
langs = sorted(langs)
assert len(langs) == len(set(langs)), langs
output_folder = Path(output_folder)
output_folder.mkdir(exist_ok=True, parents=True)
number_examples = len(test_results_dfs[langs[0]])
for ref_l in langs[1:]:
assert number_examples == len(
test_results_dfs[langs[0]]
), f"length of input {number_examples} for {ref_l} while it is {len(test_results_dfs[langs[0]])} for {langs[0]}"
assert (
test_results_dfs[ref_l][f"{SOURCE_LANG}_function"]
== test_results_dfs[langs[0]][f"{SOURCE_LANG}_function"]
).all(), f"Dataframes order for {ref_l} and {langs[0]} do not match"
langs = sorted(langs)
assert len(langs) == len(set(langs)), langs
output_folder = Path(output_folder)
output_folder.mkdir(exist_ok=True, parents=True)
first_successful_code = {
lang: get_first_success(test_results_dfs[lang], lang) for lang in langs
}
for lang in langs:
successful_translations_df = test_results_dfs[lang][
pd.Series(first_successful_code[lang]).apply(lambda x: x != FAILURE)
]
successful_translations_df["first_successful_translation"] = [
c for c in first_successful_code[lang] if c != FAILURE
]
logger.info(
f"{SOURCE_LANG}-{lang}: {len(successful_translations_df)} among {number_examples} ({len(successful_translations_df) / number_examples:.1%})"
)
print(
f"{SOURCE_LANG}-{lang}: {len(successful_translations_df)} among {number_examples} ({len(successful_translations_df) / number_examples:.1%})"
)
write_bpe_files(
output_folder,
successful_translations_df["java_function"],
successful_translations_df["first_successful_translation"],
SOURCE_LANG,
lang,
bpe_model=bpe_model,
)
for lang1 in langs:
for lang2 in langs[langs.index(lang1) + 1 :]:
# the only parallel data we have is when the tests are successful for both languages
successful_pairs = [
(c1, c2)
for c1, c2 in zip(
first_successful_code[lang1], first_successful_code[lang2]
)
if c1 != FAILURE and c2 != FAILURE
]
print(
f"{lang1}-{lang2}: {len(successful_pairs)} among {number_examples} ({len(successful_pairs) / number_examples:.1%})"
)
write_bpe_files(
output_folder,
[c1 for c1, c2 in successful_pairs],
[c2 for c1, c2 in successful_pairs],
lang1,
lang2,
bpe_model=bpe_model,
)
for file_path in Path(output_folder).glob("*.bpe"):
XLM_preprocess(
str(bpe_vocab), str(file_path), str(file_path).replace(".bpe", ".pth")
)
def select_tests_several_asserts(test_results_dfs):
tests_several_asserts = test_results_dfs["python"].python_translated_tests.apply(
lambda x: x.count("assert ") > 1
)
test_results_dfs = {
lang: df[tests_several_asserts].reset_index(drop=True)
for lang, df in test_results_dfs.items()
}
new_length = len(test_results_dfs["python"])
logger.info(
f"removed {len(tests_several_asserts) - new_length} tests with only one assert ({1 - new_length / len(tests_several_asserts):.2%})"
)
return test_results_dfs
def get_first_success(test_results, language):
beam_size = get_beam_size(
test_results, results_columns=f"translated_{language}_functions_beam_"
)
test_results_columns = [f"test_results_{language}_{i}" for i in range(beam_size)]
translations_columns = [
f"translated_{language}_functions_beam_{beam}" for beam in range(beam_size)
]
for col in test_results_columns:
test_results[col] = test_results[col].apply(lambda x: eval(x))
translations = np.array(
[test_results[col] for col in translations_columns]
).transpose()
logger.info("getting the first successful function")
tests_results = np.array(
[test_results[col] for col in test_results_columns]
).transpose((1, 0, 2))
code = []
min_successful_len = float("inf")
for translations_i, result_i in zip(translations, tests_results):
any_successful = False
for translated_code, res in zip(translations_i, result_i):
if res[0] == "success":
if not any_successful:
code.append(translated_code)
min_successful_len = len(translated_code)
any_successful = True
elif len(translated_code) < min_successful_len:
min_successful_len = len(translated_code)
code[-1] = translated_code
if not any_successful:
code.append(FAILURE)
assert len(code) == len(test_results)
first_successful_code = code
return first_successful_code
def write_bpe_files(output_folder, lang1_funcs, lang2_funcs, lang1, lang2, bpe_model):
if not lang1 < lang2:
lang1, lang2 = lang2, lang1
lang1_funcs, lang2_funcs = lang2_funcs, lang1_funcs
lang1_funcs = bpe_model.apply([f.strip() for f in lang1_funcs])
lang2_funcs = bpe_model.apply([f.strip() for f in lang2_funcs])
output_files = {
lang1: [
open(
output_folder.joinpath(
f"train.{lang1}_sa-{lang2}_sa.{lang1}_sa.{i}.bpe"
),
"w",
)
for i in range(8)
],
lang2: [
open(
output_folder.joinpath(
f"train.{lang1}_sa-{lang2}_sa.{lang2}_sa.{i}.bpe"
),
"w",
)
for i in range(8)
],
}
output_files_all = {
lang1: open(
output_folder.joinpath(f"train.{lang1}_sa-{lang2}_sa.{lang1}_sa.bpe"), "w"
),
lang2: open(
output_folder.joinpath(f"train.{lang1}_sa-{lang2}_sa.{lang2}_sa.bpe"), "w"
),
}
for i, (c1, c2) in enumerate(zip(lang1_funcs, lang2_funcs)):
c1 = c1.strip()
c2 = c2.strip()
output_files_all[lang1].write(c1)
output_files_all[lang1].write("\n")
output_files_all[lang2].write(c2)
output_files_all[lang2].write("\n")
output_files[lang1][i % 8].write(c1)
output_files[lang1][i % 8].write("\n")
output_files[lang2][i % 8].write(c2)
output_files[lang2][i % 8].write("\n")
for o in output_files[lang1] + output_files[lang2]:
o.close()
if __name__ == "__main__":
args = get_arguments()
bpe_model = fastBPE.fastBPE(args.bpe_path)
main(Path(args.input_df), args.output_folder, args.langs, bpe_model, args.bpe_vocab)