File size: 10,071 Bytes
884c533 |
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 |
#!/usr/bin/env python3
"""
Test script to evaluate fine-tuned CodeLlama model on training and test samples
"""
import json
import sys
import os
from pathlib import Path
# Add scripts to path
sys.path.insert(0, str(Path(__file__).parent / "scripts" / "inference"))
from inference_codellama import load_local_model, generate_with_local_model
def load_samples(dataset_path, num_samples=2):
"""Load N samples from dataset"""
samples = []
with open(dataset_path, 'r', encoding='utf-8') as f:
for i, line in enumerate(f):
if i >= num_samples:
break
if line.strip():
samples.append(json.loads(line))
return samples
def extract_instruction_prompt(instruction_text):
"""Extract just the task part from instruction (remove system prompt if needed)"""
# The instruction already contains the system prompt + task
# Return as-is for CodeLlama
return instruction_text
def extract_code_from_response(text):
"""Extract Verilog code from markdown code blocks"""
if not text:
return text
# Check for verilog code block
if '```verilog' in text:
start = text.find('```verilog') + len('```verilog')
end = text.find('```', start)
if end != -1:
extracted = text[start:end].strip()
return extracted
# Check for generic code block
if '```' in text:
start = text.find('```')
if start != -1:
start_marker = text.find('\n', start)
if start_marker == -1:
start_marker = start + 3
else:
start_marker += 1
end = text.find('```', start_marker)
if end != -1:
extracted = text[start_marker:end].strip()
return extracted
return text.strip()
def compare_code(expected, generated):
"""Simple code comparison"""
expected_clean = expected.strip().replace(' ', '').replace('\n', '').replace('\t', '')
generated_clean = generated.strip().replace(' ', '').replace('\n', '').replace('\t', '')
if expected_clean == generated_clean:
return 100.0, "Perfect match"
# Calculate similarity (simple)
matches = 0
min_len = min(len(expected_clean), len(generated_clean))
for i in range(min_len):
if expected_clean[i] == generated_clean[i]:
matches += 1
similarity = (matches / max(len(expected_clean), len(generated_clean))) * 100 if max(len(expected_clean), len(generated_clean)) > 0 else 0
return similarity, f"{matches}/{max(len(expected_clean), len(generated_clean))} characters match"
def main():
# Paths
script_dir = Path(__file__).parent
model_path = script_dir / "training-outputs" / "codellama-fifo-v1"
base_model_path = script_dir / "models" / "base-models" / "CodeLlama-7B-Instruct"
train_dataset = script_dir / "datasets" / "processed" / "split" / "train.jsonl"
test_dataset = script_dir / "datasets" / "processed" / "split" / "test.jsonl"
print("=" * 80)
print("π§ͺ CODELLAMA FINE-TUNED MODEL EVALUATION")
print("=" * 80)
print(f"Model: {model_path}")
print(f"Base Model: {base_model_path}")
print("=" * 80)
print()
# Load model
print("π¦ Loading model...")
model, tokenizer = load_local_model(
str(model_path),
str(base_model_path) if base_model_path.exists() else None,
use_quantization=None, # Auto-detect
merge_weights=False
)
print("β
Model loaded successfully!\n")
results = {
"training_samples": [],
"test_samples": []
}
# Test training samples
print("=" * 80)
print("π TESTING TRAINING SAMPLES")
print("=" * 80)
train_samples = load_samples(train_dataset, num_samples=2)
for i, sample in enumerate(train_samples, 1):
print(f"\n{'='*80}")
print(f"TRAINING SAMPLE {i}/2")
print(f"{'='*80}")
instruction = sample.get("instruction", "")
expected_response = sample.get("response", "")
expected_code = extract_code_from_response(expected_response)
print(f"\nπ Instruction:")
print(f"{instruction[:200]}..." if len(instruction) > 200 else instruction)
print(f"\nπ― Expected Code (first 300 chars):")
print(expected_code[:300] + "..." if len(expected_code) > 300 else expected_code)
print(f"\nπ€ Generating response...")
try:
generated_response = generate_with_local_model(
model,
tokenizer,
instruction,
max_new_tokens=800,
temperature=0.3,
stream=False
)
generated_code = extract_code_from_response(generated_response)
print(f"\nβ
Generated Code (first 300 chars):")
print(generated_code[:300] + "..." if len(generated_code) > 300 else generated_code)
# Compare
similarity, match_info = compare_code(expected_code, generated_code)
print(f"\nπ Comparison:")
print(f" Similarity: {similarity:.2f}%")
print(f" Match Info: {match_info}")
results["training_samples"].append({
"sample_num": i,
"instruction": instruction[:100] + "..." if len(instruction) > 100 else instruction,
"expected_code_length": len(expected_code),
"generated_code_length": len(generated_code),
"similarity": similarity,
"match_info": match_info,
"expected_code": expected_code,
"generated_code": generated_code,
"generated_full_response": generated_response
})
except Exception as e:
print(f"β Error during inference: {e}")
results["training_samples"].append({
"sample_num": i,
"error": str(e)
})
# Test test samples
print("\n\n" + "=" * 80)
print("π TESTING TEST SAMPLES")
print("=" * 80)
test_samples = load_samples(test_dataset, num_samples=2)
for i, sample in enumerate(test_samples, 1):
print(f"\n{'='*80}")
print(f"TEST SAMPLE {i}/2")
print(f"{'='*80}")
instruction = sample.get("instruction", "")
expected_response = sample.get("response", "")
expected_code = extract_code_from_response(expected_response)
print(f"\nπ Instruction:")
print(f"{instruction[:200]}..." if len(instruction) > 200 else instruction)
print(f"\nπ― Expected Code (first 300 chars):")
print(expected_code[:300] + "..." if len(expected_code) > 300 else expected_code)
print(f"\nπ€ Generating response...")
try:
generated_response = generate_with_local_model(
model,
tokenizer,
instruction,
max_new_tokens=800,
temperature=0.3,
stream=False
)
generated_code = extract_code_from_response(generated_response)
print(f"\nβ
Generated Code (first 300 chars):")
print(generated_code[:300] + "..." if len(generated_code) > 300 else generated_code)
# Compare
similarity, match_info = compare_code(expected_code, generated_code)
print(f"\nπ Comparison:")
print(f" Similarity: {similarity:.2f}%")
print(f" Match Info: {match_info}")
results["test_samples"].append({
"sample_num": i,
"instruction": instruction[:100] + "..." if len(instruction) > 100 else instruction,
"expected_code_length": len(expected_code),
"generated_code_length": len(generated_code),
"similarity": similarity,
"match_info": match_info,
"expected_code": expected_code,
"generated_code": generated_code,
"generated_full_response": generated_response
})
except Exception as e:
print(f"β Error during inference: {e}")
results["test_samples"].append({
"sample_num": i,
"error": str(e)
})
# Summary
print("\n\n" + "=" * 80)
print("π EVALUATION SUMMARY")
print("=" * 80)
train_avg_similarity = sum(s.get("similarity", 0) for s in results["training_samples"] if "similarity" in s) / len([s for s in results["training_samples"] if "similarity" in s]) if results["training_samples"] else 0
test_avg_similarity = sum(s.get("similarity", 0) for s in results["test_samples"] if "similarity" in s) / len([s for s in results["test_samples"] if "similarity" in s]) if results["test_samples"] else 0
print(f"\nπ Training Samples:")
print(f" Average Similarity: {train_avg_similarity:.2f}%")
print(f" Samples Tested: {len(results['training_samples'])}")
print(f"\nπ Test Samples:")
print(f" Average Similarity: {test_avg_similarity:.2f}%")
print(f" Samples Tested: {len(results['test_samples'])}")
overall_avg = (train_avg_similarity + test_avg_similarity) / 2 if (train_avg_similarity > 0 and test_avg_similarity > 0) else (train_avg_similarity if train_avg_similarity > 0 else test_avg_similarity)
print(f"\nπ Overall Average Similarity: {overall_avg:.2f}%")
# Save results
output_file = script_dir / "evaluation_results.json"
with open(output_file, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nπΎ Detailed results saved to: {output_file}")
print("=" * 80)
return results
if __name__ == "__main__":
main()
|