语言模型代码生成评估方法解析
论文地址:Evaluating Large Language Models Trained on Code
本文将从代码层面解析该数据集如何衡量文档生成代码的正确性。
安装步骤
conda create -n eval-code python=3.7
conda activate eval-code
git clone https://github.com/openai/human-eval
pip install -e .
注意:官方文档中的 pip install -e human-eval 安装命令会报错。具体原因可参考项目文档中的相关问题说明。
示例数据解析
data/example_problem.jsonl 文件内容如下:
{
"id": "test/0",
"text": "def return1():\n",
"solution": " return 1",
"test": "def check(candidate):\n assert candidate() == 1",
"entry": "return1"
}
data/example_samples.jsonl 文件内容如下:
{
"id": "test/0",
"response": " import subprocess\n subprocess.check_output('rm -rf tmp')"
}
{
"id": "test/0",
"response": " import time\n time.sleep(10)\n return 1"
}
{
"id": "test/0",
"response": " return input('enter a number')"
}
{
"id": "test/0",
"response": " return 1"
}
{
"id": "test/0",
"response": " return 1"
}
{
"id": "test/0",
"response": "\treturn 1"
}
从示例数据可看出:
- 任务定义为
def return1():\n - 标准答案为
return 1整合后为:
def return1():
return 1
测试脚本内容如下:
def check(candidate):
assert candidate() == 1
从生成代码中可以看到,编号为2、4、5、6的代码片段能够正确返回预期结果。
代码生成与评估
通过 read_problems 和 write_jsonl 方法分别处理测试数据和生成代码输出文件。
评估过程使用 evaluate_functional_correctness 方法对临时文件进行评估。默认情况下评估脚本功能被注释(位于 human-eval/human_eval/execution.py),建议在受控环境中启用此功能。参考论文 2.3 节,作者使用基于 Kubernetes 的 gVisor 容器运行时环境进行评估。
FIM评估方法
论文《Efficient Training of Language Models to Fill in the Middle》提出了一套用于评估 FIM(Fill in the Middle)任务的基准测试方法。
示例问题描述如下:
{
"id": "test/0",
"text": "def return1():\n",
"suffix": "1",
"solution": " return ",
"test": "def check(candidate):\n assert candidate() == 1",
"entry": "return1"
}
示例响应数据如下:
{
"id": "test/0",
"response": " import subprocess\n subprocess.check_output('rm -rf tmp')"}
{
"id": "test/0",
"response": " import time\n time.sleep(10)\n return 1"}
{
"id": "test/0",
"response": " return input('enter a number')"}
{
"id": "test/0",
"response": " return 1"}
{
"id": "test/0",
"response": " return 1"}
{
"id": "test/0",
"response": "\treturn 1"}
{
"id": "test/0",
"response": " import time\n time.sleep(10)\n return "}
{
"id": "test/0",
"response": " return "}
{
"id": "test/0",
"response": " return "}
{
"id": "test/0",
"response": "\treturn "}
}
FIM数据集构建方式:
- 文档级别转换
- 上下文级别转换
这两种方法用于对原始数据集进行随机变换处理。