Repository navigation
Expand file tree
/
Copy pathmlm4dnn.py
More file actions
216 lines (179 loc) · 6.86 KB
/
Copy pathmlm4dnn.py
File metadata and controls
216 lines (179 loc) · 6.86 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 os
import sys
import json
import argparse
import subprocess
import scripts._rs_utils as pgrsu
def get_root_dir() -> str:
return os.path.dirname(os.path.abspath(__file__))
def get_path(relpath: str, check=True) -> str:
path = os.path.join(get_root_dir(), relpath)
if check and not os.path.exists(path):
raise RuntimeError(f"Not found {path}")
return path
def system(cmd: str) -> int:
return subprocess.run(
[cmd], cwd=get_root_dir(), stdout=None, stderr=None, shell=True
).returncode
def repair_and_validate(
bug_files_dir,
fixed_files_dir,
bugfixed_train_result_dir,
train_work_dir,
train_env_name,
output_dir,
infill_api_config_file,
):
repo2model_file = get_path("scripts/repo2model.py")
repo2model_s_file = get_path("scripts/repo2model_s.py")
infill_api_name = pgrsu._load_json(infill_api_config_file)["_infill_api_name"]
# Preprocess bug_files and fixed_files to formatted versions
fmt_bug_files_dir = os.path.join(output_dir, "fmt_bug_files")
fmt_fixed_files_dir = os.path.join(output_dir, "fmt_fixed_files")
os.makedirs(fmt_bug_files_dir, exist_ok=True)
os.makedirs(fmt_fixed_files_dir, exist_ok=True)
launch_format_cmd = """\
python scripts/format_models.py \
"{files_dir}" \
"{fmt_files_dir}" \
{repo2model_s_file}"""
pgrsu._ilog("===================== Preprocess... =====================")
if sorted(os.listdir(bug_files_dir)) == sorted(os.listdir(fmt_bug_files_dir)):
pgrsu._wlog(">>> Bug files have been processed, SKIP")
else:
pgrsu._ilog(">>> Process bug files...")
system(
launch_format_cmd.format(
files_dir=bug_files_dir,
fmt_files_dir=fmt_bug_files_dir,
repo2model_s_file=repo2model_s_file,
)
)
if os.path.exists(fixed_files_dir):
if sorted(os.listdir(fixed_files_dir)) == sorted(
os.listdir(fmt_fixed_files_dir)
):
pgrsu._wlog(">>> Fixed files have been processed, SKIP")
else:
pgrsu._ilog(">>> Process fixed files...")
system(
launch_format_cmd.format(
files_dir=fixed_files_dir,
fmt_files_dir=fmt_fixed_files_dir,
repo2model_s_file=repo2model_s_file,
)
)
else:
pgrsu._wlog(">>> Not found fixed files, IGNORE")
# Launch reapir procedure
print("======================= Repairing... =======================")
launch_repair_cmd = """\
python scripts/model_repair.py \
--buggy-models-dir {fmt_bug_files_dir} \
--correct-models-dir {fmt_fixed_files_dir} \
--train-work-dir {train_work_dir} \
--infill-api-name {infill_api_name} \
--infill-api-config-file {infill_api_config_file} \
--repo2model-path {repo2model_file} \
--model-train-env-name {train_env_name} \
--out-dir {output_dir} \
--ops {ops}"""
system(
launch_repair_cmd.format(
fmt_bug_files_dir=fmt_bug_files_dir,
fmt_fixed_files_dir=fmt_fixed_files_dir,
train_work_dir=train_work_dir,
infill_api_name=infill_api_name,
infill_api_config_file=infill_api_config_file,
repo2model_file=repo2model_file,
train_env_name=train_env_name,
output_dir=output_dir,
ops="1 2 3", # 1: Gen DNNm; 2: Infill DNNm; 3: Filter Patch
)
)
# Launch validation procedure
print("======================= Validating... =======================")
system(
launch_repair_cmd.format(
fmt_bug_files_dir=fmt_bug_files_dir,
fmt_fixed_files_dir=fmt_fixed_files_dir,
train_work_dir=train_work_dir,
infill_api_name=infill_api_name,
infill_api_config_file=infill_api_config_file,
repo2model_file=repo2model_file,
train_env_name=train_env_name,
output_dir=output_dir,
ops="4", # 4: Prepare files for validation
)
)
launch_validation_cmd = """\
python scripts/validate_patches.py \
--bug_fixed_train_result_dir {bug_fixed_train_result_dir} \
--patches_root_dir {patches_root_dir} \
--output_dir {output_dir}"""
system(
launch_validation_cmd.format(
bug_fixed_train_result_dir=bugfixed_train_result_dir,
patches_root_dir=output_dir,
output_dir=f"{output_dir}/validate_results",
)
)
# Collect results
print("======================= Collecting Results... =======================")
launch_collect_result_cmd = """\
python scripts/collect_result.py \
--correct_models_dir {fmt_fixed_files_dir} \
--patch_source_filename filtered_possible_repaired_models.json \
--repair_result_dir {output_dir} \
--verbose"""
system(
launch_collect_result_cmd.format(
fmt_fixed_files_dir=fmt_fixed_files_dir,
output_dir=output_dir,
)
)
def train():
finetune_config_jf = get_path("configs/finetune_config.json")
with open(finetune_config_jf, "r", encoding="UTF-8") as fp:
finetune_config = json.load(fp)
launch_cmd = """\
IM4DNN_REMOVE_ROOT=1 bash scripts/finetune_unixcoder.sh \
{model_name_or_path} \
{dataset_path} \
{output_dir} \
{tag_prefix}"""
system(launch_cmd.format_map(finetune_config))
def repro():
parser = argparse.ArgumentParser()
parser.add_argument("--infill-api-config-file", type=str, default=None)
parser.add_argument("--dnn-train-env-name", type=str, required=True)
parser.add_argument("--output-dir", type=str, required=True)
args = parser.parse_args()
infill_api_config_file = args.infill_api_config_file
dnn_train_env_name = args.dnn_train_env_name
output_dir = args.output_dir
if not infill_api_config_file:
infill_api_config_file = get_path("configs/infill_api_config.json")
bug_files_dir = get_path("benchmark/mlm4dnn_benchmark/samples/bug")
fixed_files_dir = get_path("benchmark/mlm4dnn_benchmark/samples/fixed")
train_work_dir = get_path("benchmark/mlm4dnn_benchmark/train_work_dir")
bugfixed_train_result_dir = get_path("benchmark/mlm4dnn_benchmark/train_result_dir")
repair_and_validate(
bug_files_dir=bug_files_dir,
fixed_files_dir=fixed_files_dir,
bugfixed_train_result_dir=bugfixed_train_result_dir,
train_work_dir=train_work_dir,
train_env_name=dnn_train_env_name,
output_dir=output_dir,
infill_api_config_file=infill_api_config_file,
)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python mlm4dnn.py <subcmd> ...")
exit(-1)
available_subcmds = ["train", "repro"]
subcmd = sys.argv.pop(1)
if subcmd not in available_subcmds:
print(f"Available subcmds: {available_subcmds}")
exit(-1)
eval(f"{subcmd}")()