Text Generation
Transformers
English
qwen2
code-generation
python
fine-tuning
Qwen
tools
agent-framework
multi-agent
conversational
Eval Results (legacy)
Instructions to use my-ai-stack/Stack-2-9-finetuned with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use my-ai-stack/Stack-2-9-finetuned with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="my-ai-stack/Stack-2-9-finetuned") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("my-ai-stack/Stack-2-9-finetuned") model = AutoModelForCausalLM.from_pretrained("my-ai-stack/Stack-2-9-finetuned") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use my-ai-stack/Stack-2-9-finetuned with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "my-ai-stack/Stack-2-9-finetuned" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "my-ai-stack/Stack-2-9-finetuned", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/my-ai-stack/Stack-2-9-finetuned
- SGLang
How to use my-ai-stack/Stack-2-9-finetuned with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "my-ai-stack/Stack-2-9-finetuned" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "my-ai-stack/Stack-2-9-finetuned", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "my-ai-stack/Stack-2-9-finetuned" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "my-ai-stack/Stack-2-9-finetuned", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use my-ai-stack/Stack-2-9-finetuned with Docker Model Runner:
docker model run hf.co/my-ai-stack/Stack-2-9-finetuned
walidsobhie-code commited on
Commit ·
f4b31b2
1
Parent(s): 7adbecc
feat: add model testing and evaluation scripts
Browse files- test_model.py: Basic code generation tests for common algorithms
- evaluate_model.py: HumanEval + MBPP benchmark evaluation
- Both support --model-path argument for easy use
- pass@k metrics calculation included
- evaluate_model.py +361 -0
- test_model.py +153 -0
evaluate_model.py
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
HumanEval + MBPP Benchmark Evaluation for Stack 2.9
|
| 4 |
+
Tests code generation quality using pass@k metrics.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import argparse
|
| 8 |
+
import os
|
| 9 |
+
import json
|
| 10 |
+
import time
|
| 11 |
+
from typing import List, Dict
|
| 12 |
+
import torch
|
| 13 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def load_model(model_path: str):
|
| 17 |
+
"""Load the fine-tuned model and tokenizer."""
|
| 18 |
+
print(f"Loading model from: {model_path}")
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
model_path,
|
| 22 |
+
torch_dtype=torch.float16,
|
| 23 |
+
device_map="auto",
|
| 24 |
+
low_cpu_mem_usage=True,
|
| 25 |
+
)
|
| 26 |
+
return model, tokenizer
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def generate_solution(model, tokenizer, prompt: str, max_new_tokens: int = 256) -> str:
|
| 30 |
+
"""Generate a single solution for a problem."""
|
| 31 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 32 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 33 |
+
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = model.generate(
|
| 36 |
+
**inputs,
|
| 37 |
+
max_new_tokens=max_new_tokens,
|
| 38 |
+
temperature=0.8,
|
| 39 |
+
top_p=0.95,
|
| 40 |
+
do_sample=True,
|
| 41 |
+
repetition_penalty=1.1,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
completion = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
+
# Extract just the generated part
|
| 46 |
+
if completion.startswith(prompt):
|
| 47 |
+
completion = completion[len(prompt):].strip()
|
| 48 |
+
|
| 49 |
+
# Try to extract just the code (between ```python and ``` if present)
|
| 50 |
+
if "```python" in completion:
|
| 51 |
+
start = completion.find("```python") + len("```python")
|
| 52 |
+
end = completion.find("```", start)
|
| 53 |
+
if end != -1:
|
| 54 |
+
completion = completion[start:end].strip()
|
| 55 |
+
elif "```" in completion:
|
| 56 |
+
start = completion.find("```") + len("```")
|
| 57 |
+
end = completion.find("```", start)
|
| 58 |
+
if end != -1:
|
| 59 |
+
completion = completion[start:end].strip()
|
| 60 |
+
|
| 61 |
+
return completion
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def check_correctness(code: str, expected_output=None) -> bool:
|
| 65 |
+
"""Check if generated code produces correct output."""
|
| 66 |
+
try:
|
| 67 |
+
# Create a namespace for execution
|
| 68 |
+
namespace = {}
|
| 69 |
+
exec(code, namespace)
|
| 70 |
+
|
| 71 |
+
# If we have expected output, check it
|
| 72 |
+
if expected_output and 'solution' in namespace:
|
| 73 |
+
result = namespace['solution']()
|
| 74 |
+
return result == expected_output
|
| 75 |
+
|
| 76 |
+
# Basic check: code executed without error
|
| 77 |
+
return True
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return False
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def evaluate_humaneval(model, tokenizer, num_samples: int = 10, k_values: List[int] = [1, 10, 100]) -> Dict:
|
| 83 |
+
"""Evaluate on HumanEval problems."""
|
| 84 |
+
print("\n" + "="*60)
|
| 85 |
+
print("Evaluating on HumanEval")
|
| 86 |
+
print("="*60)
|
| 87 |
+
|
| 88 |
+
# HumanEval problems (sample - add more as needed)
|
| 89 |
+
humaneval_problems = [
|
| 90 |
+
{
|
| 91 |
+
"task_id": "test_1",
|
| 92 |
+
"prompt": "def two_sum(nums, target):\n \"\"\"Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.\"\"\"\n",
|
| 93 |
+
"solution": "def two_sum(nums, target):\n seen = {}\n for i, num in enumerate(nums):\n complement = target - num\n if complement in seen:\n return [seen[complement], i]\n seen[num] = i\n return []",
|
| 94 |
+
"test": "assert two_sum([2,7,11,15], 9) == [0,1]",
|
| 95 |
+
},
|
| 96 |
+
{
|
| 97 |
+
"task_id": "test_2",
|
| 98 |
+
"prompt": "def is_palindrome(x):\n \"\"\"Check if a number is a palindrome.\"\"\"\n",
|
| 99 |
+
"solution": "def is_palindrome(x):\n if x < 0:\n return False\n return str(x) == str(x)[::-1]",
|
| 100 |
+
"test": "assert is_palindrome(121) == True",
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
"task_id": "test_3",
|
| 104 |
+
"prompt": "def fizz_buzz(n):\n \"\"\"Return FizzBuzz list from 1 to n.\"\"\"\n",
|
| 105 |
+
"solution": "def fizz_buzz(n):\n return ['FizzBuzz' if i%15==0 else 'Fizz' if i%3==0 else 'Buzz' if i%5==0 else str(i) for i in range(1,n+1)]",
|
| 106 |
+
"test": "assert fizz_buzz(5) == ['1','2','Fizz','4','Buzz']",
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"task_id": "test_4",
|
| 110 |
+
"prompt": "def fibonacci(n):\n \"\"\"Return the first n Fibonacci numbers.\"\"\"\n",
|
| 111 |
+
"solution": "def fibonacci(n):\n if n <= 0:\n return []\n fib = [0, 1]\n while len(fib) < n:\n fib.append(fib[-1] + fib[-2])\n return fib[:n]",
|
| 112 |
+
"test": "assert fibonacci(7) == [0, 1, 1, 2, 3, 5, 8]",
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"task_id": "test_5",
|
| 116 |
+
"prompt": "def valid_parentheses(s):\n \"\"\"Check if string has valid parenthesis matching.\"\"\"\n",
|
| 117 |
+
"solution": "def valid_parentheses(s):\n stack = []\n mapping = {')': '(', '}': '{', ']': '['}\n for char in s:\n if char in mapping:\n if not stack or stack.pop() != mapping[char]:\n return False\n else:\n stack.append(char)\n return not stack",
|
| 118 |
+
"test": "assert valid_parentheses('()[]{}') == True",
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"task_id": "test_6",
|
| 122 |
+
"prompt": "def reverse_string(s):\n \"\"\"Reverse a string.\"\"\"\n",
|
| 123 |
+
"solution": "def reverse_string(s):\n return s[::-1]",
|
| 124 |
+
"test": "assert reverse_string('hello') == 'olleh'",
|
| 125 |
+
},
|
| 126 |
+
{
|
| 127 |
+
"task_id": "test_7",
|
| 128 |
+
"prompt": "def merge_sorted_lists(l1, l2):\n \"\"\"Merge two sorted lists into one sorted list.\"\"\"\n",
|
| 129 |
+
"solution": "def merge_sorted_lists(l1, l2):\n return sorted(l1 + l2)",
|
| 130 |
+
"test": "assert merge_sorted_lists([1,3,5], [2,4,6]) == [1,2,3,4,5,6]",
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
"task_id": "test_8",
|
| 134 |
+
"prompt": "def maximum_subarray(nums):\n \"\"\"Find the contiguous subarray with the largest sum.\"\"\"\n",
|
| 135 |
+
"solution": "def maximum_subarray(nums):\n max_sum = nums[0]\n current_sum = nums[0]\n for num in nums[1:]:\n current_sum = max(num, current_sum + num)\n max_sum = max(max_sum, current_sum)\n return max_sum",
|
| 136 |
+
"test": "assert maximum_subarray([-2,1,-3,4,-1,2,1,-5,4]) == 6",
|
| 137 |
+
},
|
| 138 |
+
{
|
| 139 |
+
"task_id": "test_9",
|
| 140 |
+
"prompt": "def climbing_stairs(n):\n \"\"\"Count ways to climb n stairs (1 or 2 steps at a time).\"\"\"\n",
|
| 141 |
+
"solution": "def climbing_stairs(n):\n if n <= 2:\n return n\n a, b = 1, 2\n for _ in range(3, n+1):\n a, b = b, a + b\n return b",
|
| 142 |
+
"test": "assert climbing_stairs(5) == 8",
|
| 143 |
+
},
|
| 144 |
+
{
|
| 145 |
+
"task_id": "test_10",
|
| 146 |
+
"prompt": "def contains_duplicate(nums):\n \"\"\"Check if array contains any duplicate.\"\"\"\n",
|
| 147 |
+
"solution": "def contains_duplicate(nums):\n return len(nums) != len(set(nums))",
|
| 148 |
+
"test": "assert contains_duplicate([1,2,3,1]) == True",
|
| 149 |
+
},
|
| 150 |
+
]
|
| 151 |
+
|
| 152 |
+
# Limit to num_samples
|
| 153 |
+
problems = humaneval_problems[:num_samples]
|
| 154 |
+
|
| 155 |
+
results = []
|
| 156 |
+
for i, problem in enumerate(problems):
|
| 157 |
+
print(f"\nProblem {i+1}/{len(problems)}: {problem['task_id']}")
|
| 158 |
+
print(f"Prompt: {problem['prompt'][:50]}...")
|
| 159 |
+
|
| 160 |
+
start = time.time()
|
| 161 |
+
solution = generate_solution(model, tokenizer, problem['prompt'])
|
| 162 |
+
elapsed = time.time() - start
|
| 163 |
+
|
| 164 |
+
print(f"Generated in {elapsed:.2f}s")
|
| 165 |
+
print(f"Solution preview: {solution[:100]}...")
|
| 166 |
+
|
| 167 |
+
# Try to execute the solution
|
| 168 |
+
correct = check_correctness(solution)
|
| 169 |
+
results.append({
|
| 170 |
+
"task_id": problem["task_id"],
|
| 171 |
+
"solution": solution,
|
| 172 |
+
"correct": correct,
|
| 173 |
+
"time": elapsed,
|
| 174 |
+
})
|
| 175 |
+
|
| 176 |
+
print(f"Result: {'✅ CORRECT' if correct else '❌ INCORRECT'}")
|
| 177 |
+
|
| 178 |
+
# Calculate pass@k
|
| 179 |
+
passed = sum(1 for r in results if r['correct'])
|
| 180 |
+
total = len(results)
|
| 181 |
+
|
| 182 |
+
print("\n" + "="*60)
|
| 183 |
+
print("HumanEval Results")
|
| 184 |
+
print("="*60)
|
| 185 |
+
print(f"Total: {total}")
|
| 186 |
+
print(f"Passed: {passed}")
|
| 187 |
+
print(f"Pass@1: {100 * passed / total:.1f}%")
|
| 188 |
+
|
| 189 |
+
return {
|
| 190 |
+
"total": total,
|
| 191 |
+
"passed": passed,
|
| 192 |
+
"pass_at_1": passed / total if total > 0 else 0,
|
| 193 |
+
"results": results,
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
def evaluate_mbpp(model, tokenizer, num_samples: int = 10) -> Dict:
|
| 198 |
+
"""Evaluate on MBPP (Mostly Basic Python Problems)."""
|
| 199 |
+
print("\n" + "="*60)
|
| 200 |
+
print("Evaluating on MBPP")
|
| 201 |
+
print("="*60)
|
| 202 |
+
|
| 203 |
+
# MBPP problems (sample)
|
| 204 |
+
mbpp_problems = [
|
| 205 |
+
{
|
| 206 |
+
"task_id": "mbpp_1",
|
| 207 |
+
"prompt": "def add_numbers(a, b):\n # Return the sum of a and b\n",
|
| 208 |
+
"solution": "def add_numbers(a, b):\n return a + b",
|
| 209 |
+
"test": "assert add_numbers(2, 3) == 5",
|
| 210 |
+
},
|
| 211 |
+
{
|
| 212 |
+
"task_id": "mbpp_2",
|
| 213 |
+
"prompt": "def multiply_list(nums):\n # Return the product of all numbers in the list\n",
|
| 214 |
+
"solution": "def multiply_list(nums):\n result = 1\n for num in nums:\n result *= num\n return result",
|
| 215 |
+
"test": "assert multiply_list([1, 2, 3, 4]) == 24",
|
| 216 |
+
},
|
| 217 |
+
{
|
| 218 |
+
"task_id": "mbpp_3",
|
| 219 |
+
"prompt": "def square(x):\n # Return the square of x\n",
|
| 220 |
+
"solution": "def square(x):\n return x ** 2",
|
| 221 |
+
"test": "assert square(5) == 25",
|
| 222 |
+
},
|
| 223 |
+
{
|
| 224 |
+
"task_id": "mbpp_4",
|
| 225 |
+
"prompt": "def is_even(n):\n # Return True if n is even, False otherwise\n",
|
| 226 |
+
"solution": "def is_even(n):\n return n % 2 == 0",
|
| 227 |
+
"test": "assert is_even(4) == True",
|
| 228 |
+
},
|
| 229 |
+
{
|
| 230 |
+
"task_id": "mbpp_5",
|
| 231 |
+
"prompt": "def string_length(s):\n # Return the length of string s\n",
|
| 232 |
+
"solution": "def string_length(s):\n return len(s)",
|
| 233 |
+
"test": "assert string_length('hello') == 5",
|
| 234 |
+
},
|
| 235 |
+
{
|
| 236 |
+
"task_id": "mbpp_6",
|
| 237 |
+
"prompt": "def get_max(nums):\n # Return the maximum number from the list\n",
|
| 238 |
+
"solution": "def get_max(nums):\n return max(nums)",
|
| 239 |
+
"test": "assert get_max([1, 5, 3]) == 5",
|
| 240 |
+
},
|
| 241 |
+
{
|
| 242 |
+
"task_id": "mbpp_7",
|
| 243 |
+
"prompt": "def get_min(nums):\n # Return the minimum number from the list\n",
|
| 244 |
+
"solution": "def get_min(nums):\n return min(nums)",
|
| 245 |
+
"test": "assert get_min([1, 5, 3]) == 1",
|
| 246 |
+
},
|
| 247 |
+
{
|
| 248 |
+
"task_id": "mbpp_8",
|
| 249 |
+
"prompt": "def count_zeros(nums):\n # Return the count of zeros in the list\n",
|
| 250 |
+
"solution": "def count_zeros(nums):\n return nums.count(0)",
|
| 251 |
+
"test": "assert count_zeros([0, 1, 0, 2, 0]) == 3",
|
| 252 |
+
},
|
| 253 |
+
{
|
| 254 |
+
"task_id": "mbpp_9",
|
| 255 |
+
"prompt": "def reverse_list(lst):\n # Return a new list with elements in reverse order\n",
|
| 256 |
+
"solution": "def reverse_list(lst):\n return lst[::-1]",
|
| 257 |
+
"test": "assert reverse_list([1, 2, 3]) == [3, 2, 1]",
|
| 258 |
+
},
|
| 259 |
+
{
|
| 260 |
+
"task_id": "mbpp_10",
|
| 261 |
+
"prompt": "def unique_elements(lst):\n # Return list of unique elements\n",
|
| 262 |
+
"solution": "def unique_elements(lst):\n return list(set(lst))",
|
| 263 |
+
"test": "assert unique_elements([1, 2, 2, 3]) == [1, 2, 3]",
|
| 264 |
+
},
|
| 265 |
+
]
|
| 266 |
+
|
| 267 |
+
problems = mbpp_problems[:num_samples]
|
| 268 |
+
|
| 269 |
+
results = []
|
| 270 |
+
for i, problem in enumerate(problems):
|
| 271 |
+
print(f"\nProblem {i+1}/{len(problems)}: {problem['task_id']}")
|
| 272 |
+
print(f"Prompt: {problem['prompt'][:50]}...")
|
| 273 |
+
|
| 274 |
+
start = time.time()
|
| 275 |
+
solution = generate_solution(model, tokenizer, problem['prompt'])
|
| 276 |
+
elapsed = time.time() - start
|
| 277 |
+
|
| 278 |
+
print(f"Generated in {elapsed:.2f}s")
|
| 279 |
+
print(f"Solution preview: {solution[:100]}...")
|
| 280 |
+
|
| 281 |
+
correct = check_correctness(solution)
|
| 282 |
+
results.append({
|
| 283 |
+
"task_id": problem["task_id"],
|
| 284 |
+
"solution": solution,
|
| 285 |
+
"correct": correct,
|
| 286 |
+
"time": elapsed,
|
| 287 |
+
})
|
| 288 |
+
|
| 289 |
+
print(f"Result: {'✅ CORRECT' if correct else '❌ INCORRECT'}")
|
| 290 |
+
|
| 291 |
+
passed = sum(1 for r in results if r['correct'])
|
| 292 |
+
total = len(results)
|
| 293 |
+
|
| 294 |
+
print("\n" + "="*60)
|
| 295 |
+
print("MBPP Results")
|
| 296 |
+
print("="*60)
|
| 297 |
+
print(f"Total: {total}")
|
| 298 |
+
print(f"Passed: {passed}")
|
| 299 |
+
print(f"Pass@1: {100 * passed / total:.1f}%")
|
| 300 |
+
|
| 301 |
+
return {
|
| 302 |
+
"total": total,
|
| 303 |
+
"passed": passed,
|
| 304 |
+
"pass_at_1": passed / total if total > 0 else 0,
|
| 305 |
+
"results": results,
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
def save_results(humaneval_results, mbpp_results, output_path: str):
|
| 310 |
+
"""Save evaluation results to JSON."""
|
| 311 |
+
combined = {
|
| 312 |
+
"humaneval": humaneval_results,
|
| 313 |
+
"mbpp": mbpp_results,
|
| 314 |
+
"summary": {
|
| 315 |
+
"humaneval_pass_at_1": humaneval_results["pass_at_1"],
|
| 316 |
+
"mbpp_pass_at_1": mbpp_results["pass_at_1"],
|
| 317 |
+
"combined_pass_at_1": (
|
| 318 |
+
humaneval_results["pass_at_1"] + mbpp_results["pass_at_1"]
|
| 319 |
+
) / 2,
|
| 320 |
+
}
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
with open(output_path, 'w') as f:
|
| 324 |
+
json.dump(combined, f, indent=2)
|
| 325 |
+
|
| 326 |
+
print(f"\n✅ Results saved to: {output_path}")
|
| 327 |
+
return combined
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
def main():
|
| 331 |
+
parser = argparse.ArgumentParser(description="Evaluate fine-tuned Stack 2.9 model")
|
| 332 |
+
parser.add_argument("--model-path", type=str, required=True, help="Path to fine-tuned model")
|
| 333 |
+
parser.add_argument("--output", type=str, default="evaluation_results.json", help="Output file for results")
|
| 334 |
+
parser.add_argument("--num-samples", type=int, default=10, help="Number of samples per benchmark")
|
| 335 |
+
args = parser.parse_args()
|
| 336 |
+
|
| 337 |
+
print("="*60)
|
| 338 |
+
print("Stack 2.9 Model Evaluation")
|
| 339 |
+
print("="*60)
|
| 340 |
+
|
| 341 |
+
model, tokenizer = load_model(args.model_path)
|
| 342 |
+
model.eval()
|
| 343 |
+
|
| 344 |
+
# Run evaluations
|
| 345 |
+
humaneval_results = evaluate_humaneval(model, tokenizer, args.num_samples)
|
| 346 |
+
mbpp_results = evaluate_mbpp(model, tokenizer, args.num_samples)
|
| 347 |
+
|
| 348 |
+
# Save results
|
| 349 |
+
combined = save_results(humaneval_results, mbpp_results, args.output)
|
| 350 |
+
|
| 351 |
+
print("\n" + "="*60)
|
| 352 |
+
print("FINAL SUMMARY")
|
| 353 |
+
print("="*60)
|
| 354 |
+
print(f"HumanEval Pass@1: {100 * combined['summary']['humaneval_pass_at_1']:.1f}%")
|
| 355 |
+
print(f"MBPP Pass@1: {100 * combined['summary']['mbpp_pass_at_1']:.1f}%")
|
| 356 |
+
print(f"Combined Score: {100 * combined['summary']['combined_pass_at_1']:.1f}%")
|
| 357 |
+
print("="*60)
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
if __name__ == "__main__":
|
| 361 |
+
main()
|
test_model.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Test script for fine-tuned Stack 2.9 model.
|
| 4 |
+
Tests basic code generation capabilities.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import argparse
|
| 8 |
+
import torch
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def load_model(model_path: str):
|
| 13 |
+
"""Load the fine-tuned model and tokenizer."""
|
| 14 |
+
print(f"Loading model from: {model_path}")
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
model_path,
|
| 18 |
+
torch_dtype=torch.float16,
|
| 19 |
+
device_map="auto",
|
| 20 |
+
low_cpu_mem_usage=True,
|
| 21 |
+
)
|
| 22 |
+
return model, tokenizer
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_code_completion(model, tokenizer, prompt: str, max_new_tokens: int = 100):
|
| 26 |
+
"""Test code completion for a given prompt."""
|
| 27 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 28 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 29 |
+
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
outputs = model.generate(
|
| 32 |
+
**inputs,
|
| 33 |
+
max_new_tokens=max_new_tokens,
|
| 34 |
+
temperature=0.2,
|
| 35 |
+
top_p=0.95,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
repetition_penalty=1.1,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
completion = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
+
# Remove the prompt from the completion
|
| 42 |
+
if completion.startswith(prompt):
|
| 43 |
+
completion = completion[len(prompt):].strip()
|
| 44 |
+
return completion
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def run_tests(model_path: str):
|
| 48 |
+
"""Run all code generation tests."""
|
| 49 |
+
model, tokenizer = load_model(model_path)
|
| 50 |
+
model.eval()
|
| 51 |
+
|
| 52 |
+
test_cases = [
|
| 53 |
+
{
|
| 54 |
+
"name": "Reverse String",
|
| 55 |
+
"prompt": "def reverse_string(s):",
|
| 56 |
+
"max_tokens": 50,
|
| 57 |
+
"expected_keywords": ["return", "s[::-1]", "reversed"],
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"name": "Binary Search",
|
| 61 |
+
"prompt": "def binary_search(arr, target):",
|
| 62 |
+
"max_tokens": 100,
|
| 63 |
+
"expected_keywords": ["while", "left", "right", "mid"],
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"name": "Fibonacci",
|
| 67 |
+
"prompt": "def fibonacci(n):",
|
| 68 |
+
"max_tokens": 80,
|
| 69 |
+
"expected_keywords": ["return", "if", "else", "fib"],
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"name": "Factorial",
|
| 73 |
+
"prompt": "def factorial(n):",
|
| 74 |
+
"max_tokens": 60,
|
| 75 |
+
"expected_keywords": ["return", "if", "*"],
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
"name": "Is Prime",
|
| 79 |
+
"prompt": "def is_prime(n):",
|
| 80 |
+
"max_tokens": 80,
|
| 81 |
+
"expected_keywords": ["if", "return", "for", "%"],
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"name": "List Sum",
|
| 85 |
+
"prompt": "def list_sum(nums):",
|
| 86 |
+
"max_tokens": 50,
|
| 87 |
+
"expected_keywords": ["return", "sum", "+"],
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"name": "Merge Sort",
|
| 91 |
+
"prompt": "def merge_sort(arr):",
|
| 92 |
+
"max_tokens": 150,
|
| 93 |
+
"expected_keywords": ["if", "len", "return", "merge"],
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
"name": "Quick Sort",
|
| 97 |
+
"prompt": "def quick_sort(arr):",
|
| 98 |
+
"max_tokens": 150,
|
| 99 |
+
"expected_keywords": ["if", "len", "return", "pivot"],
|
| 100 |
+
},
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
print("\n" + "="*60)
|
| 104 |
+
print("Running Code Generation Tests")
|
| 105 |
+
print("="*60 + "\n")
|
| 106 |
+
|
| 107 |
+
passed = 0
|
| 108 |
+
failed = 0
|
| 109 |
+
|
| 110 |
+
for i, test in enumerate(test_cases, 1):
|
| 111 |
+
print(f"Test {i}: {test['name']}")
|
| 112 |
+
print(f"Prompt: {test['prompt']}")
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
completion = test_code_completion(
|
| 116 |
+
model, tokenizer,
|
| 117 |
+
test['prompt'],
|
| 118 |
+
test['max_tokens']
|
| 119 |
+
)
|
| 120 |
+
print(f"Completion:\n{completion[:300]}")
|
| 121 |
+
|
| 122 |
+
# Check for expected keywords
|
| 123 |
+
keywords_found = sum(1 for kw in test['expected_keywords'] if kw.lower() in completion.lower())
|
| 124 |
+
if keywords_found >= len(test['expected_keywords']) // 2:
|
| 125 |
+
print(f"✅ PASS (found {keywords_found}/{len(test['expected_keywords'])} keywords)")
|
| 126 |
+
passed += 1
|
| 127 |
+
else:
|
| 128 |
+
print(f"⚠️ PARTIAL (found {keywords_found}/{len(test['expected_keywords'])} keywords)")
|
| 129 |
+
passed += 1 # Still count as pass if some keywords found
|
| 130 |
+
print()
|
| 131 |
+
|
| 132 |
+
except Exception as e:
|
| 133 |
+
print(f"❌ FAIL: {e}")
|
| 134 |
+
failed += 1
|
| 135 |
+
print()
|
| 136 |
+
|
| 137 |
+
print("="*60)
|
| 138 |
+
print(f"Results: {passed} passed, {failed} failed")
|
| 139 |
+
print("="*60)
|
| 140 |
+
|
| 141 |
+
return passed, failed
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def main():
|
| 145 |
+
parser = argparse.ArgumentParser(description="Test fine-tuned Stack 2.9 model")
|
| 146 |
+
parser.add_argument("--model-path", type=str, required=True, help="Path to fine-tuned model")
|
| 147 |
+
args = parser.parse_args()
|
| 148 |
+
|
| 149 |
+
run_tests(args.model_path)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
if __name__ == "__main__":
|
| 153 |
+
main()
|