v2.0: Proper code extraction for base model
Browse files
scripts/eval_bigcodebench_hf.py
CHANGED
|
@@ -5,6 +5,8 @@
|
|
| 5 |
"""
|
| 6 |
BigCodeBench Evaluation: Base Devstral vs Fine-tuned Alizee-Coder
|
| 7 |
Runs on HF Jobs with GPU support
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
|
| 10 |
import os
|
|
@@ -102,7 +104,7 @@ def extract_python_code(text):
|
|
| 102 |
return text.strip()
|
| 103 |
|
| 104 |
def generate_completion_base(model, tokenizer, prompt):
|
| 105 |
-
"""Generate code completion for BASE model (
|
| 106 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=4096).to(model.device)
|
| 107 |
|
| 108 |
with torch.no_grad():
|
|
@@ -115,7 +117,27 @@ def generate_completion_base(model, tokenizer, prompt):
|
|
| 115 |
eos_token_id=tokenizer.eos_token_id,
|
| 116 |
)
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Stop at function boundary
|
| 121 |
stop_tokens = ["\ndef ", "\nclass ", "\nif __name__", "\n\n\n"]
|
|
|
|
| 5 |
"""
|
| 6 |
BigCodeBench Evaluation: Base Devstral vs Fine-tuned Alizee-Coder
|
| 7 |
Runs on HF Jobs with GPU support
|
| 8 |
+
|
| 9 |
+
VERSION: 2.0 - Proper code extraction for both base and fine-tuned models
|
| 10 |
"""
|
| 11 |
|
| 12 |
import os
|
|
|
|
| 104 |
return text.strip()
|
| 105 |
|
| 106 |
def generate_completion_base(model, tokenizer, prompt):
|
| 107 |
+
"""Generate code completion for BASE model (handles chat-like responses)"""
|
| 108 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=4096).to(model.device)
|
| 109 |
|
| 110 |
with torch.no_grad():
|
|
|
|
| 117 |
eos_token_id=tokenizer.eos_token_id,
|
| 118 |
)
|
| 119 |
|
| 120 |
+
raw_completion = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
| 121 |
+
|
| 122 |
+
# Try to extract code from ```python blocks (if model generates chat-like response)
|
| 123 |
+
completion = extract_python_code(raw_completion)
|
| 124 |
+
|
| 125 |
+
# If extracted code looks like a full function, extract just the body
|
| 126 |
+
if completion.strip().startswith("def "):
|
| 127 |
+
lines = completion.split('\n')
|
| 128 |
+
body_lines = []
|
| 129 |
+
in_function = False
|
| 130 |
+
for line in lines:
|
| 131 |
+
if line.strip().startswith("def "):
|
| 132 |
+
in_function = True
|
| 133 |
+
continue
|
| 134 |
+
if in_function:
|
| 135 |
+
body_lines.append(line)
|
| 136 |
+
if body_lines:
|
| 137 |
+
completion = '\n'.join(body_lines)
|
| 138 |
+
# If no code block, use raw completion
|
| 139 |
+
elif completion == raw_completion.strip():
|
| 140 |
+
completion = raw_completion
|
| 141 |
|
| 142 |
# Stop at function boundary
|
| 143 |
stop_tokens = ["\ndef ", "\nclass ", "\nif __name__", "\n\n\n"]
|