GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Psearch/beam_search.py at main · iSEngLab/Psearch

Expand file tree

/

beam_search.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
402 lines (343 loc) · 14.4 KB
/

beam_search.py

Copy path

File metadata and controls

402 lines (343 loc) · 14.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import argparse
import copy
import dataclasses
import json
import os
import jsonlines
from transformers import AutoTokenizer
from vllm import LLM
from vllm.sampling_params import BeamSearchParams, SamplingParams
from get_d4j_bug_list import get_defects4j_projects, get_defects4j_bugs_by_project
from gpt import generate as generate_gpt
import framework
from framework import Bug
import prompt_mcts as prompt
from llm import make_model
from utils import utc_now_str, run_bash, get_test_names, extract_method, make_failing_tests_short
import tqdm
# python beam_search.py --policy_model /mnt/data/hhc/Qwen2.5-Coder-7B-Instruct --reward_model qwen-max-2025-01-25
class Config:
beam_width = 5
max_tokens = 1024
max_iter = 4
pool_size = 4
generate_once = 2
init_pool_temperature = 0.9
after_init_temperature = 0.4
policy_model = ''
reward_model = ''
plausible_save_path = ''
output_path = ''
@dataclasses.dataclass
class Patch:
bug: Bug
patch_code: str
patch_diff: str
reflection: str
hash_id: int
score: int
is_plausible: bool
gmt_created: str
class PatchPool:
def __init__(self, pool_size: int):
self.pool_size = pool_size
self.patch_pool = []
self.plausible_patches = []
self.all_patches = []
def add_patch(self, patch: Patch):
self.all_patches.append(patch)
# Store plausible patches separately
if patch.is_plausible:
self.plausible_patches.append(patch)
return
# 1) Insert into the candidate pool first
self.patch_pool.append(patch)
# 2) If capacity is exceeded, sort by ascending score and earlier-to-later time, then discard the worst one
if len(self.patch_pool) > self.pool_size:
# ISO-8601 or similar time strings are chronologically ordered lexicographically
self.patch_pool.sort(key=lambda p: (p.score, p.gmt_created))
# Remove index 0 from the list (lowest score and earliest timestamp)
self.patch_pool.pop(0)
def is_patch_exist(self, p: Patch):
for patch in self.all_patches:
if patch.patch_code.strip() == p.patch_code:
return True
return False
def is_patch_error_exist(self, p: Patch):
for patch in self.all_patches:
if patch.bug.test_error_message.strip() == p.bug.test_error_message.strip():
return True
return False
def extract_reflection_from_response(response):
if "```java" not in response:
return response[:100]
# raise Exception("No patch found in response.")
return response.split("```java")[0]
def extract_patch_from_response(response, mode=None):
splitter_count = len(response.split("```java")) - 1
if splitter_count > 2:
patch = response[response.rfind("```java") + len("```java") + 1:]
patch = patch[:patch.find("\n```")]
elif splitter_count > 0:
patch = response[response.find("```java") + len("```java") + 1:]
patch = patch[:patch.find("\n```")]
else:
patch = response
if mode == "SL":
while len(patch) > 0 and patch.startswith("\n"):
patch = patch[1:]
while len(patch) > 0 and patch.endswith("\n"):
patch = patch[:-1]
if "\n" in patch:
patch = patch[:patch.find("\n")]
return patch
def get_reward(bug: Bug, patch: Patch, patch_pool: PatchPool):
modes = list(bug.bug_type.split())
mode = modes[0]
# If a compilation error occurs
if 'cannot find symbol' in patch.bug.test_error_message:
return -100
if '\';\' expected' in patch.bug.test_error_message:
return -100
if '\')\' expected' in patch.bug.test_error_message:
return -100
if 'illegal' in patch.bug.test_error_message:
return -100
# If the same patch appears
if patch_pool.is_patch_exist(patch):
return 0
# If the same error appears
if patch_pool.is_patch_error_exist(patch):
return 0
reward_prompt = prompt.construct_gpt_reward_prompt(bug=bug, wrong_patch=patch.patch_code,
reflection=patch.reflection,
mode=mode, tokenizer=None)
response = generate_gpt(reward_prompt, Config.reward_model)
score_list = response.split()
score = 0
for i in range(len(score_list)):
score_list[i] = score_list[i].replace("**", "").replace(".", "").replace(",", "").strip()
if score_list[i].isdigit():
score = int(score_list[i])
break
if score < 0:
score = 0
if score > 100:
score = 100
return score
def execute_round(bug: Bug, patch_pool: PatchPool, model: LLM, tokenizer: AutoTokenizer, round_num: int):
if round_num > Config.max_iter:
raise Exception('exceed max iter')
modes = list(bug.bug_type.split())
mode = modes[0]
generated_patches = []
# If the patch pool is empty, sample pool_size patches to initialize it
if len(patch_pool.all_patches) == 0:
repair_prompt = prompt.construct_llm_policy_prompt(bug=bug, mode=mode, tokenizer=tokenizer)
print(repair_prompt + "\n")
sampling_params = SamplingParams(
temperature=Config.init_pool_temperature,
max_tokens=Config.max_tokens,
frequency_penalty=1.0,
presence_penalty=1.0,
n=Config.pool_size*2,
)
responses = model.generate(
repair_prompt,
sampling_params
)
responses = [x.text for x in responses[0].outputs]
patch_code_list = [extract_patch_from_response(x, mode) for x in responses]
reflection_list = [extract_reflection_from_response(x) for x in responses]
for patch_code, reflection in zip(patch_code_list, reflection_list):
generated_patches.append(Patch(
bug=bug,
patch_code=patch_code,
patch_diff='',
reflection=reflection,
hash_id=hash(patch_code),
score=-1,
is_plausible=False,
gmt_created=utc_now_str()
))
else:
for patch in patch_pool.patch_pool:
repair_prompt = prompt.construct_llm_policy_prompt(bug=patch.bug, mode=mode, tokenizer=tokenizer)
print(repair_prompt + "\n")
params = BeamSearchParams(beam_width=Config.beam_width,
max_tokens=Config.max_tokens
)
responses = model.beam_search([{'prompt':repair_prompt}], params)
responses = [responses[0].sequences[0].text]
# sampling_params = SamplingParams(
# temperature=Config.after_init_temperature,
# max_tokens=Config.max_tokens,
# frequency_penalty=1.0,
# presence_penalty=1.0,
# # top_p=1.0,
# # top_k=10,
# n=Config.generate_once,
# )
#
# responses = model.generate(
# repair_prompt,
# sampling_params
# )
#
# responses = [x.text for x in responses[0].outputs]
for response in responses:
print('-' * 40)
print("Repair Response is:")
print(response + "\n")
new_patch = extract_patch_from_response(response, mode=mode)
generated_patches.append(Patch(
bug=patch.bug,
patch_code=new_patch,
patch_diff='',
reflection=extract_reflection_from_response(response),
hash_id=hash(new_patch),
score=-1,
is_plausible=False,
gmt_created=utc_now_str()
))
for patch in generated_patches:
# validate patch
test_result, result_reason, patch_diff = framework.validate_patch(bug=patch.bug,
proposed_patch=patch.patch_code,
mode=mode)
print('-' * 40)
print(f"Patch Validation Result:{result_reason}\n")
patch.patch_diff = patch_diff
if test_result == "PASS":
patch.is_plausible = True
patch.score = 100
else:
old_bug = patch.bug
next_bug_state = copy.deepcopy(patch.bug)
next_bug_state.test_line = ""
next_bug_state.test_error_message = result_reason
failing_tests = run_bash("get_failing_tests", patch.bug.project, patch.bug.bug_id).stdout
test_names = get_test_names(failing_tests)
test_code = patch.bug.test_code
test_methods = ""
methods_found = []
for test_name in test_names:
t = extract_method(test_code, test_name)
if t != "":
methods_found.append(test_name)
test_methods += t + "\n\n"
failing_tests = make_failing_tests_short(failing_tests, methods_found)
if test_result == "ERROR":
next_bug_state.failing_tests = result_reason
else:
next_bug_state.failing_tests = failing_tests
next_bug_state.extract_test_code = test_methods
# For SL and SH bugs, the next state uses the current patch as buggy_lines; for SF bugs, directly update the next state code with the patch
if patch.bug.bug_type != "SF":
next_bug_state.buggy_lines = patch.patch_code
next_bug_state.code = patch.bug.masked_code.replace(">>> [ INFILL ] <<<", patch.patch_code)
else:
next_bug_state.code = patch.patch_code
patch.bug = next_bug_state
patch.is_plausible = False
patch.score = get_reward(old_bug, patch, patch_pool)
patch_pool.add_patch(patch)
def check_bug_detail(bug_detail: Bug):
if bug_detail.code is None or bug_detail.code == "":
return False
if not bug_detail.bug_type.startswith("SL") and not bug_detail.bug_type.startswith(
"SH") and not bug_detail.bug_type.startswith(
"SF"):
return False
if bug_detail.bug_type.startswith("SL") or bug_detail.bug_type.startswith("SH"):
if not bug_detail.masked_code:
return False
if ">>> [ INFILL ] <<<" not in bug_detail.masked_code:
return False
return True
def beam_search_repair():
result_list = []
if os.path.exists(Config.output_path):
with open(Config.output_path, "r") as f:
for line in f.readlines():
json_line = json.loads(line)
result_list.append((json_line["project"], str(json_line["bug_id"])))
projects = get_defects4j_projects()
# initialize policy model and tokenizer
policy_model = make_model(Config.policy_model)
tokenizer = AutoTokenizer.from_pretrained(Config.policy_model, use_fast=False)
for proj in projects:
bugs = get_defects4j_bugs_by_project(proj)
for bug in tqdm.tqdm(bugs):
if (proj, str(bug)) in result_list:
continue
try:
bug_detail = framework.get_bug_details(proj, bug)
except Exception as e:
print(e)
continue
mode = bug_detail.bug_type.split()[0]
if mode not in ["SL","SH"]:
continue
if not check_bug_detail(bug_detail):
with jsonlines.open(Config.output_path, "a") as f:
f.write(
{"project": proj,
"bug_id": bug,
"eval": "FAIL",
"patch": "",
"patch_diff": "",
"score": 0,
"gmt_created": utc_now_str(),
"reflection": ''
}
)
continue
patch_pool = PatchPool(Config.pool_size)
for i in range(Config.max_iter):
execute_round(bug_detail, patch_pool, policy_model, tokenizer, i + 1)
for patch in patch_pool.all_patches:
with jsonlines.open(Config.output_path, "a") as f:
f.write(
{"project": proj,
"bug_id": bug,
"eval": "FAIL" if not patch.is_plausible else "PASS",
"patch": patch.patch_code,
"patch_diff": patch.patch_diff,
"score": patch.score,
"gmt_created": patch.gmt_created,
"reflection": patch.reflection
}
)
def main():
parser = argparse.ArgumentParser(
description="Beam-search based APR"
)
parser.add_argument("--policy_model", required=True,
help="Local model path")
parser.add_argument("--reward_model", required=True,
help="GPT used for reward evaluation")
parser.add_argument("--output_path", default="results.jsonl",
help="JSONL file for patch/evaluation output")
parser.add_argument("--plausible_save_path", default="",
help="Directory for storing plausible patches (optional)")
parser.add_argument("--beam_width", type=int, default=Config.beam_width,
help="Beam search width")
parser.add_argument("--max_tokens", type=int, default=Config.max_tokens,
help="Maximum number of tokens generated by the LLM")
parser.add_argument("--max_iter", type=int, default=Config.max_iter,
help="Maximum number of iterations per bug")
parser.add_argument("--pool_size", type=int, default=Config.pool_size,
help="PatchPool capacity")
args = parser.parse_args()
Config.policy_model = args.policy_model
Config.reward_model = args.reward_model
Config.output_path = args.output_path
Config.plausible_save_path = args.plausible_save_path
Config.beam_width = args.beam_width
Config.max_tokens = args.max_tokens
Config.max_iter = args.max_iter
Config.pool_size = args.pool_size
beam_search_repair()
if __name__ == '__main__':
main()
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.