mcpField
E
evaluating-code-models

evaluating-code-models

github.com/orchestra-research测试与质量14.1k961 次安装19 周前更新
代码仓库

关于 evaluating-code-models

BigCode Evaluation Harness 是用于评估代码生成模型的基准测试工具,覆盖 HumanEval、MBPP 和 MultiPL-E 等任务。它可运行代码并计算 pass@k,也支持多语言评测、指令模型任务和模型间比较。

主要功能

  1. 01覆盖多种代码基准测试
  2. 02支持 18 种语言评测
  3. 03运行生成代码并计算 pass@k
  4. 04支持指令模型专用任务
  5. 05保存生成结果并读取评测

适用场景

  1. 01比较多个代码生成模型
  2. 02评测 Python 代码补全能力
  3. 03跨语言测试代码生成质量
  4. 04检查指令模型的代码合成能力

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/orchestra-research/ai-research-skills --skill evaluating-code-models

安装后在 Claude Code 中输入/evaluating-code-models确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
benchmarks.md
custom-tasks.md
issues.md

详细介绍

BigCode 评测工具集 - 代码模型基准测试

快速开始

BigCode 评测工具集可在 15 多个基准上评估代码生成模型,包括 HumanEval、MBPP 和 MultiPL-E(18 种语言)。

安装:

git clone https://github.com/bigcode-project/bigcode-evaluation-harness.git
cd bigcode-evaluation-harness
pip install -e .
accelerate config

在 HumanEval 上评估:

accelerate launch main.py \
  --model bigcode/starcoder2-7b \
  --tasks humaneval \
  --max_length_generation 512 \
  --temperature 0.2 \
  --n_samples 20 \
  --batch_size 10 \
  --allow_code_execution \
  --save_generations

查看可用任务:

python -c "from bigcode_eval.tasks import ALL_TASKS; print(ALL_TASKS)"

常见工作流

工作流 1:标准代码基准评测

在核心代码基准(HumanEval、MBPP、HumanEval+)上评估模型。

检查清单:

Code Benchmark Evaluation:
- [ ] Step 1: Choose benchmark suite
- [ ] Step 2: Configure model and generation
- [ ] Step 3: Run evaluation with code execution
- [ ] Step 4: Analyze pass@k results

步骤 1:选择基准套件

Python 代码生成(最常见):

  • HumanEval:164 道手工编写的题目,函数补全
  • HumanEval+:相同的 164 道题目,测试数量增加 80×(要求更严格)
  • MBPP:500 道众包题目,入门级难度
  • MBPP+:399 道精选题目,测试数量增加 35×

多语言(18 种语言):

  • MultiPL-E:将 HumanEval/MBPP 翻译为 C++、Java、JavaScript、Go、Rust 等语言

进阶:

  • APPS:10,000 道题目(入门/面试/竞赛)
  • DS-1000:涵盖 7 个库的 1,000 道数据科学题目

步骤 2:配置模型和生成

# Standard HuggingFace model
accelerate launch main.py \
  --model bigcode/starcoder2-7b \
  --tasks humaneval \
  --max_length_generation 512 \
  --temperature 0.2 \
  --do_sample True \
  --n_samples 200 \
  --batch_size 50 \
  --allow_code_execution

# Quantized model (4-bit)
accelerate launch main.py \
  --model codellama/CodeLlama-34b-hf \
  --tasks humaneval \
  --load_in_4bit \
  --max_length_generation 512 \
  --allow_code_execution

# Custom/private model
accelerate launch main.py \
  --model /path/to/my-code-model \
  --tasks humaneval \
  --trust_remote_code \
  --use_auth_token \
  --allow_code_execution

步骤 3:运行评测

# Full evaluation with pass@k estimation (k=1,10,100)
accelerate launch main.py \
  --model bigcode/starcoder2-7b \
  --tasks humaneval \
  --temperature 0.8 \
  --n_samples 200 \
  --batch_size 50 \
  --allow_code_execution \
  --save_generations \
  --metric_output_path results/starcoder2-humaneval.json

步骤 4:分析结果

结果位于 results/starcoder2-humaneval.json:

{
  "humaneval": {
    "pass@1": 0.354,
    "pass@10": 0.521,
    "pass@100": 0.689
  },
  "config": {
    "model": "bigcode/starcoder2-7b",
    "temperature": 0.8,
    "n_samples": 200
  }
}
工作流 2:多语言评测(MultiPL-E)

在 18 种编程语言中评估代码生成能力。

检查清单:

Multi-Language Evaluation:
- [ ] Step 1: Generate solutions (host machine)
- [ ] Step 2: Run evaluation in Docker (safe execution)
- [ ] Step 3: Compare across languages

步骤 1:在主机上生成解答

# Generate without execution (safe)
accelerate launch main.py \
  --model bigcode/starcoder2-7b \
  --tasks multiple-py,multiple-js,multiple-java,multiple-cpp \
  --max_length_generation 650 \
  --temperature 0.8 \
  --n_samples 50 \
  --batch_size 50 \
  --generation_only \
  --save_generations \
  --save_generations_path generations_multi.json

步骤 2:在 Docker 容器中评测

# Pull the MultiPL-E Docker image
docker pull ghcr.io/bigcode-project/evaluation-harness-multiple

# Run evaluation inside container
docker run -v $(pwd)/generations_multi.json:/app/generations.json:ro \
  -it evaluation-harness-multiple python3 main.py \
  --model bigcode/starcoder2-7b \
  --tasks multiple-py,multiple-js,multiple-java,multiple-cpp \
  --load_generations_path /app/generations.json \
  --allow_code_execution \
  --n_samples 50

支持的语言:Python、JavaScript、Java、C++、Go、Rust、TypeScript、C#、PHP、Ruby、Swift、Kotlin、Scala、Perl、Julia、Lua、R、Racket

工作流 3:指令微调模型评测

使用正确的格式评估聊天/指令模型。

检查清单:

Instruction Model Evaluation:
- [ ] Step 1: Use instruction-tuned tasks
- [ ] Step 2: Configure instruction tokens
- [ ] Step 3: Run evaluation

步骤 1:选择指令任务

  • instruct-humaneval:使用指令提示词的 HumanEval
  • humanevalsynthesize-{lang}:HumanEvalPack 合成任务

步骤 2:配置指令标记

# For models with chat templates (e.g., CodeLlama-Instruct)
accelerate launch main.py \
  --model codellama/CodeLlama-7b-Instruct-hf \
  --tasks instruct-humaneval \
  --instruction_tokens "<s>[INST],</s>,[/INST]" \
  --max_length_generation 512 \
  --allow_code_execution

步骤 3:面向指令模型的 HumanEvalPack

# Test code synthesis across 6 languages
accelerate launch main.py \
  --model codellama/CodeLlama-7b-Instruct-hf \
  --tasks humanevalsynthesize-python,humanevalsynthesize-js \
  --prompt instruct \
  --max_length_generation 512 \
  --allow_code_execution
工作流 4:比较多个模型

用于模型比较的基准测试套件。

步骤 1:创建评测脚本

#!/bin/bash
# eval_models.sh

MODELS=(
  "bigcode/starcoder2-7b"
  "codellama/CodeLlama-7b-hf"
  "deepseek-ai/deepseek-coder-6.7b-base"
)
TASKS="humaneval,mbpp"

for model in "${MODELS[@]}"; do
  model_name=$(echo $model | tr '/' '-')
  echo "Evaluating $model"

  accelerate launch main.py \
    --model $model \
    --tasks $TASKS \
    --temperature 0.2 \
    --n_samples 20 \
    --batch_size 20 \
    --allow_code_execution \
    --metric_output_path results/${model_name}.json
done

步骤 2:生成比较表

import json
import pandas as pd

models = ["bigcode-starcoder2-7b", "codellama-CodeLlama-7b-hf", "deepseek-ai-deepseek-coder-6.7b-base"]
results = []

for model in models:
    with open(f"results/{model}.json") as f:
        data = json.load(f)
        results.append({
            "Model": model,
            "HumanEval pass@1": f"{data['humaneval']['pass@1']:.3f}",
            "MBPP pass@1": f"{data['mbpp']['pass@1']:.3f}"
        })

df = pd.DataFrame(results)
print(df.to_markdown(index=False))

何时使用及替代方案

适用于以下情况:

  • 专门评估代码生成模型
  • 需要进行多语言评测(通过 MultiPL-E 支持 18 种语言)
  • 使用单元测试检查功能正确性(pass@k)
  • 针对 BigCode/HuggingFace 榜单进行基准测试
  • 评估中间填充(FIM)能力

可改用以下方案:

  • lm-evaluation-harness:通用 LLM 基准(MMLU、GSM8K、HellaSwag)
  • EvalPlus:包含更多测试用例、要求更严格的 HumanEval+/MBPP+
  • SWE-bench:解决真实世界的 GitHub 问题
  • LiveCodeBench:无污染且持续更新的题目
  • CodeXGLUE:代码理解任务(克隆检测、缺陷预测)

支持的基准

基准题目数语言指标用途
HumanEval164Pythonpass@k标准代码补全
HumanEval+164Pythonpass@k更严格的评测(测试数量增加 80×)
MBPP500Pythonpass@k入门级题目
MBPP+399Pythonpass@k更严格的评测(测试数量增加 35×)
MultiPL-E164×1818 种语言pass@k多语言评测
APPS10,000Pythonpass@k竞赛级题目
DS-10001,000Pythonpass@k数据科学(pandas、numpy 等)
HumanEvalPack164×3×66 种语言pass@k合成/修复/解释
Mercury1,889Python效率计算效率

常见问题

问题:结果与论文报告的结果不同

请检查以下因素:

# 1. Verify n_samples (need 200 for accurate pass@k)
--n_samples 200

# 2. Check temperature (0.2 for greedy-ish, 0.8 for sampling)
--temperature 0.8

# 3. Verify task name matches exactly
--tasks humaneval  # Not "human_eval" or "HumanEval"

# 4. Check max_length_generation
--max_length_generation 512  # Increase for longer problems

问题:CUDA 显存不足

# Use quantization
--load_in_8bit
# OR
--load_in_4bit

# Reduce batch size
--batch_size 1

# Set memory limit
--max_memory_per_gpu "20GiB"

问题:代码执行卡住或超时

使用 Docker 安全地执行:

# Generate on host (no execution)
--generation_only --save_generations

# Evaluate in Docker
docker run ... --allow_code_execution --load_generations_path ...

问题:指令模型得分较低

确保指令格式正确:

# Use instruction-specific tasks
--tasks instruct-humaneval

# Set instruction tokens for your model
--instruction_tokens "<s>[INST],</s>,[/INST]"

问题:MultiPL-E 语言执行失败

使用专用 Docker 镜像:

docker pull ghcr.io/bigcode-project/evaluation-harness-multiple

命令参考

参数默认值说明
--model-HuggingFace 模型 ID 或本地路径
--tasks-以逗号分隔的任务名称
--n_samples1每道题的采样数(pass@k 为 200)
--temperature0.2采样温度
--max_length_generation512最大 token 数(提示词 + 生成内容)
--batch_size1每个 GPU 的批次大小
--allow_code_executionFalse启用代码执行(必需)
--generation_onlyFalse仅生成,不进行评估
--load_generations_path-加载预生成的解答
--save_generationsFalse保存生成的代码
--metric_output_pathresults.json指标输出文件
--load_in_8bitFalse8 位量化
--load_in_4bitFalse4 位量化
--trust_remote_codeFalse允许使用自定义模型代码
--precisionfp32模型精度(fp32/fp16/bf16)

硬件要求

模型规模显存(fp16)显存(4-bit)耗时(HumanEval,n=200)
7B14GB6GB~30 分钟(A100)
13B26GB10GB~1 小时(A100)
34B68GB20GB~2 小时(A100)

资源

评论

支持 Markdown