Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# CodeLlama-70B_for_NMT
|
| 2 |
+
|
| 3 |
+
We fine-tuned [CodeLlama-70B](https://huggingface.co/codellama/CodeLlama-70b-hf) on [Transfer_dataset](https://drive.google.com/drive/folders/1Z-2xcLSmh643BfX_j0yQW2GmdPoru6j3?usp=drive_link) under the NMT workflow for APR research.
|
| 4 |
+
|
| 5 |
+
## Model Use
|
| 6 |
+
|
| 7 |
+
To use this model, please make sure to install transformers, peft, bitsandbytes, and accelerate.
|
| 8 |
+
|
| 9 |
+
```bash
|
| 10 |
+
pip install transformers
|
| 11 |
+
pip install peft
|
| 12 |
+
pip install bitsandbytes
|
| 13 |
+
pip install accelerate
|
| 14 |
+
```
|
| 15 |
+
|
| 16 |
+
Then, please run the following script to merge the adapter into the CodeLlama.
|
| 17 |
+
|
| 18 |
+
```bash
|
| 19 |
+
bash merge.sh
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
Finally, you can load the model to generate patches for buggy code.
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 26 |
+
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
|
| 27 |
+
import torch
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# load model and tokenizer
|
| 31 |
+
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained("CodeLlama-70B_for_NMT/Epoch_1/-merged", use_auth_token=True)
|
| 33 |
+
|
| 34 |
+
nf4_config = BitsAndBytesConfig(
|
| 35 |
+
load_in_4bit=True,
|
| 36 |
+
bnb_4bit_quant_type="nf4",
|
| 37 |
+
bnb_4bit_use_double_quant=True,
|
| 38 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 42 |
+
"CodeLlama-70B_for_NMT/Epoch_1/-merged",
|
| 43 |
+
quantization_config=nf4_config,
|
| 44 |
+
device_map='auto'
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
model = prepare_model_for_kbit_training(model)
|
| 48 |
+
|
| 49 |
+
lora_config = LoraConfig(
|
| 50 |
+
r=16,
|
| 51 |
+
lora_alpha=32,
|
| 52 |
+
lora_dropout=0.05,
|
| 53 |
+
bias="none",
|
| 54 |
+
task_type="CAUSAL_LM",
|
| 55 |
+
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj"]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
model = get_peft_model(model, lora_config)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# a bug-fix pairs
|
| 62 |
+
|
| 63 |
+
buggy_code = "
|
| 64 |
+
/*
|
| 65 |
+
* Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers
|
| 66 |
+
Example
|
| 67 |
+
is_equal_to_sum_even(4) == False
|
| 68 |
+
is_equal_to_sum_even(6) == False
|
| 69 |
+
is_equal_to_sum_even(8) == True
|
| 70 |
+
*/
|
| 71 |
+
public class IS_EQUAL_TO_SUM_EVEN {
|
| 72 |
+
public static boolean is_equal_to_sum_even(int n) {
|
| 73 |
+
// bug_start
|
| 74 |
+
return ((n * 2 == 1) ^ (n < 8));
|
| 75 |
+
// bug_end
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
"
|
| 79 |
+
|
| 80 |
+
fixed_code = "
|
| 81 |
+
// fix_start
|
| 82 |
+
return ((n % 2 == 0) && (n >= 8));
|
| 83 |
+
// fix_end
|
| 84 |
+
"
|
| 85 |
+
|
| 86 |
+
# model inference
|
| 87 |
+
|
| 88 |
+
B_INST, E_INST = "[INST]", "[/INST]"
|
| 89 |
+
input_text = tokenizer.bos_token + B_INST +'\n[bug_function]\n' + buggy_code + '\n[fix_code]\n' + E_INST
|
| 90 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to(0)
|
| 91 |
+
|
| 92 |
+
eos_id = tokenizer.convert_tokens_to_ids(tokenizer.eos_token)
|
| 93 |
+
generated_ids = model.generate(
|
| 94 |
+
input_ids=input_ids,
|
| 95 |
+
max_new_tokens=256,
|
| 96 |
+
num_beams=10,
|
| 97 |
+
num_return_sequences=10,
|
| 98 |
+
early_stopping=True,
|
| 99 |
+
pad_token_id=eos_id,
|
| 100 |
+
eos_token_id=eos_id
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
for generated_id in generated_ids:
|
| 104 |
+
generated_text = tokenizer.decode(generated_id, skip_special_tokens=False)
|
| 105 |
+
patch = generated_text.split(E_INST)[1]
|
| 106 |
+
patch = text.replace(tokenizer.eos_token,'')
|
| 107 |
+
print(patch)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
```
|