Views
No views yet
1@article{10.1145/3735129,
2author = {Yang, Boyang and Tian, Haoye and Ren, Jiadong and Zhang, Hongyu and Klein, Jacques and Bissyande, Tegawende and Le Goues, Claire and Jin, Shunfu},
3title = {MORepair: Teaching LLMs to Repair Code via Multi-Objective Fine-Tuning},
4year = {2025},
5publisher = {Association for Computing Machinery},
6issn = {1049-331X},
7url = {https://doi.org/10.1145/3735129},
8doi = {10.1145/3735129},
9journal = {ACM Trans. Softw. Eng. Methodol.},
10}pip install transformers torch1from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2
3# Load model and tokenizer
4model_name = "barty/CodeLlama-13B-MORepair"
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModelForCausalLM.from_pretrained(
7 model_name,
8 device_map="auto",
9 load_in_8bit=True,
10 torch_dtype=torch.float16
11)
12pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
13
14def repair_code(buggy_code, filename="example.java"):
15 # Construct prompt in the format model expects
16 prompt = f"""[INST] This is an incorrect code({filename}):
17```java
18{buggy_code}
19```
20You are a software engineer. Can you repair the incorrect code?
21[/INST]
22```java
23"""
24
25 # Calculate token count for length control
26 prompt_tokens = len(tokenizer.tokenize(prompt))
27 max_new_tokens = 500 - prompt_tokens
28
29 # Generate repair
30 output = pipe(
31 prompt,
32 min_length=prompt_tokens + 64,
33 max_length=prompt_tokens + max_new_tokens,
34 temperature=1.0,
35 do_sample=True
36 )
37
38 # Extract the generated code
39 full_text = output[0]['generated_text']
40 fixed_code = full_text.split('[/INST]')[1].strip()
41
42 return full_text, fixed_code
43
44# Example usage
45buggy_code = """
46public static int findMinRotated(int[] arr) {
47 int left = 0;
48 int right = arr.length - 1;
49
50 while (left < right) {
51 int mid = (left + right) / 2;
52 if (arr[mid] > arr[right])
53 left = mid; // Bug: should be mid + 1
54 else
55 right = mid;
56 }
57 return arr[left];
58}
59"""
60
61full_response, fixed_code = repair_code(buggy_code)
62print("Fixed code:")
63print(fixed_code)load_in_8bit=True: Enables 8-bit quantization for efficient inferencetemperature=1.0: Controls randomness in generationdo_sample=True: Enables sampling-based generationmin_length: Minimum length of generated textmax_length: Maximum length of generated text