
代码大语言模型指令微调与评估工具集
OctoPack是一个开源的代码大语言模型指令微调与评估工具集。它包含CommitPack数据集、OctoCoder和OctoGeeX模型、HumanEvalPack评估基准等关键组件。该项目提供数据处理、模型训练和评估的完整流程,助力研究人员开发和优化代码大语言模型。OctoPack的所有组件均可自由使用,为代码AI研究提供了宝贵资源。

本仓库概述了论文OctoPack: 指令微调代码大语言模型中的所有组件。
<!-- TOC --> <!-- /TOC -->CommitPack已上传至此处。重新创建步骤如下:
dataset/commitpack/sql中找到。按顺序执行,从第一个到第五个。由于BigQuery在执行单个语句时会引发资源超限错误,因此将它们分开并逐一执行。每次SQL查询后创建一个数据集,并按文件名指示进行命名。例如,执行sql_1_commits_table_base.sql后,将输出数据集命名为commits_table_base,然后在第二个语句中引用它。dataset/commitpack/scrape_github.py脚本,从GitHub下载每次git提交前后的文件。该脚本包含一些基本过滤器来移除噪声文件(依赖于dataset/commitpack/programming_languages.json中的扩展名文件),然后使用多线程和多进程进行抓取。建议在一个非常大的实例上运行。dataset/commitpack/shard.sh脚本进行分片。dataset/commitpack/licenses_langs.py脚本,删除选择退出该步骤的用户的仓库(第一部分带有__main__,需要取消注释),并将上一步的大文件拆分为每种编程语言的文件(第二部分带有__main__,当前未注释)。您可能 需要更改一些路径名并根据需要取消注释部分内容。dataset/commitpack/shard.py脚本,可以将每种语言的大型jsonl文件分割成指定大小限制的较小块。CommitPackFT已上传至此处。重新创建步骤如下:
git clone bigcode/commitpack下载CommitPack,或按照上述所有步骤重新创建它。python dataset/commitpackft/commitpackft_filters1.py和python dataset/commitpackft/commitpackft_filters2.py。您可能需要修改脚本中定义的一些全局变量。dataset/xp3x/filter_xp3x.py脚本。dataset/oasst/filter_oasst.py脚本。jsonl文件中的每一行都是一个对话树。我们只保留每个对话树的前两条消息,即问题和答案。-b octopack标志来克隆我们用于论文的分支。通常,我们建议使用最新版本的代码。git clone https://github.com/bigcode-project/bigcode-evaluation-harness # 如果您想要精确的论文分支:git clone -b octopack https://github.com/bigcode-project/bigcode-evaluation-harness cd bigcode-evaluation-harness pip install -q -r requirements.txt accelerate config
accelerate launch main.py \ --model bigcode/octocoder \ --tasks humanevalfixtests-python \ --do_sample True \ --temperature 0.2 \ --n_samples 20 \ --batch_size 5 \ --allow_code_execution \ --save_generations \ --trust_remote_code \ --prompt octocoder \ --save_generations_path generations_humanevalfixpython_octocoder.json \ --metric_output_path evaluation_humanevalfixpython_octocoder.json \ --max_length_generation 2048 \ --precision bf16
注意:
accelerate: 你也可以直接使用 python main.py。Accelerate 的优势在于可以自动处理混合精度和设备。prompt: 这定义了提示。示例值包括 octocoder、octogeex、wizardcoder、instructcodet5p、starchat,它们使用各自模型创建者提出的提示格式。你可以参考实际的评估文件来了解提示的具体样式。allow_code_execution: 这将直接在你当前的机器上执行评估并保存结果。如果你只想生成结果并稍后评估,可以添加 --generation_only 标志,然后使用我们在下一节提供的 Colab 笔记本进行评估。这对于你机器上可能没有安装的语言(如 Rust)很实用。tasks: 对于 HumanEvalPack,任务包括:'humanevalfixdocs-cpp', 'humanevalfixdocs-go', 'humanevalfixdocs-java', 'humanevalfixdocs-js', 'humanevalfixdocs-python', 'humanevalfixdocs-rust', 'humanevalfixtests-cpp', 'humanevalfixtests-go', 'humanevalfixtests-java', 'humanevalfixtests-js', 'humanevalfixtests-python', 'humanevalfixtests-rust', 'humanevalexplaindescribe-cpp', 'humanevalexplaindescribe-go', 'humanevalexplaindescribe-java', 'humanevalexplaindescribe-js', 'humanevalexplaindescribe-python', 'humanevalexplaindescribe-rust', 'humanevalexplainsynthesize-cpp', 'humanevalexplainsynthesize-go', 'humanevalexplainsynthesize-java', 'humanevalexplainsynthesize-js', 'humanevalexplainsynthesize-python', 'humanevalexplainsynthesize-rust', 'humanevalsynthesize-cpp', 'humanevalsynthesize-go', 'humanevalsynthesize-java', 'humanevalsynthesize-js', 'humanevalsynthesize-python', 'humanevalsynthesize-rust'。
--generation_only,因为还没有评估。对于合成部分,你需要通过 --load_data_path 提供描述,然后用于合成答案。合成时 n_samples 设置为 1,因为我们为每个描述生成 1 个答案(通过 n_samples 已经为描述生成了多个样本)。示例如下:accelerate launch main.py \ --model bigcode/octocoder \ --tasks humanevalexplaindescribe-python \ --generation_only \ --do_sample True \ --temperature 0.2 \ --n_samples 20 \ --batch_size 5 \ --allow_code_execution \ --save_generations \ --trust_remote_code \ --prompt octocoder \ --save_generations_path generations_humanevalexplaindescribepython_octocoder.json \ --max_length_generation 2048 \ --precision bf16 accelerate launch main.py \ --model bigcode/octocoder \ --tasks humanevalexplainsynthesize-python \ --do_sample True \ --temperature 0.2 \ --n_samples 1 \ --batch_size 1 \ --allow_code_execution \ --save_generations \ --trust_remote_code \ --prompt octocoder \ --load_data_path generations_humanevalexplaindescribepython_octocoder.json \ --save_generations_path generations_humanevalexplainsynthesizepython_octocoder.json \ --metric_output_path evaluation_humanevalexplainpython_octocoder.json \ --max_length_generation 2048 \ --precision bf16
--prompt continue 标志。OctoCoder 使用 --prompt octocoder,如下面的脚本所示。以下脚本应该能重现 OctoCoder 在 HumanEval 上 46.2% 的 pass@1 分数:accelerate launch main.py \ --model bigcode/octocoder \ --tasks humanevalsynthesize-python \ --do_sample True \ --temperature 0.2 \ --n_samples 20 \ --batch_size 5 \ --allow_code_execution \ --save_generations \ --trust_remote_code \ --prompt octocoder \ --save_generations_path generations_humanevalsynthesizepython_octocoder.json \ --metric_output_path evaluation_humanevalsynthesizepython_octocoder.json \ --max_length_generation 2048 \ --precision bf16
batch_size,结果可能会有一些随机性。我们使用 batch_size=5 和 Python 3.9.13evaluation/run/eval_scripts 中为每个模型提供了我们使用的确切脚本。每个任务还有一个 _range.sh 脚本(例如 evaluation/run/eval_scripts/eval_humanevalfix_range.sh),它单独运行每个样本。如果你有多个 GPU 可用,这会快得多。在 _range.sh 脚本中,你需要指定要运行的模型和语言。运行后,你将得到 164 个生成文件,需要用 python evaluation/run/merge_generations.py "generations_*json" 合并它们。随后,你需要按照下一步的说明进行评估。--generation_only 标志或使用 _range.sh 脚本),你可以使用 evaluation/run/humanevalpack_evaluation 中的笔记本或这 个 colab 来评估生成结果。它包含每种编程语言的部分,首先安装该语言,然后给定你的生成路径,评估它们并提供 pass@k 分数。我们使用以下版本来评估每种语言:Python 3.9.13 torch 1.13.0+rocm5.2 accelerate 0.20.3 transformers 4.32.1(用于运行和评估)11.4.0(但更新的版本也应该可以)js-md5@0.7.3java version "18" 2022-03-22go1.18.4rustc 1.71.1 (eb26296b5 2023-08-03)要创建 HumanEvalPack,我们遵循以下步骤:
evaluation/create/prepare_humaneval.py 脚本上半部分被注释掉的部分,为 evaluation/create/humaneval-x/data 中的每种 humaneval 语言创建一个包含解决方案的 JSON。evaluation/create/humaneval-x/data/cpp/data/humanevalpack.json),在所有语言中并行引入一个 bug。evaluation/create/humaneval-x/README.md 的顶部。evaluation/create/prepare_humaneval.py 的下半部分,将 JSON 文件转回 JSONL 文件,包含有错误的解决方案、指令列和其他元数据。这些位于 evaluation/create/humaneval-x/data/cpp/data/humanevalpack.jsonl 等位置的 JSONL 文件随后被上传到 https://huggingface.co/datasets/bigcode/humanevalpack 的 HF 数据集中。创建 OctoCoder 的微调脚本位于 finetuning/starcoder/finetune.py。该文件夹包含一个带有说明的 README.md。
OctoGeeX 是基于 CodeGeeX2-6B 使用内部训练框架进行微调的。超参数如下:
| 参数 | 值 |
|---|---|
tp_size | 2 |
global_batch_size | 48 |
lr | 5e-5 |
train_step | 50 |
seq_length | 8192 |
precision | bf16 |
它也兼容 finetuning/finetune.py。
SantaCoder 的微调脚本位于 finetuning/santacoder/finetune.py。默认超参数设置用于"行差异"格式,如附录 H 所述。
通过执行 git clone https://github.com/bigcode-project/Megatron-LM 获取 Megatron-LM。
下载数据集:使用 git clone https://huggingface.co/datasets/bigcode/commitpack-subset-cf 下载预训练数据集(commitpack-subset-cf),并将所有 jsonl 文件合并为一个 jsonl 文件。您可以随意命名,例如 commitpack_cf.jsonl。
将 training/preprocess_santacoderpack.sh 和 training/pretraining_santacoderpack.sh 文件移动到 Megatron-LM 目录。
修改 preprocess_santacoderpack.sh 以指向您的 jsonl 文件,从而对预训练数据集进行分词。同时,使用 wget https://huggingface.co/bigcode/starcoderbase/raw/main/tokenizer.json 将分词器路径更改为指向 StarCoder 的 tokenizer.json。最后,指定存储分词后数据的输出前缀,并使用 bash preprocess_santacoderpack.sh 运行脚本。
修改 pretraining_santacoderpack.sh,调整 CHECKPOINT_PATH 以指向保存的 Megatron-LM 检查点,并将 TOKENIZER_FILE 设置为 StarCoder 的 tokenizer.json。确保指向正确的环境和缓存位置,并根据您的设置更改任何自定义设置。通过执行 bash pretraining_santacoderpack.sh 运行脚本!
使用位于 convert_large.sh 的脚本转换保存的检查点。它包含了需要下载哪些仓库的说明。
我们最终没有在论文中使用 Megatron-LM 微调模型,但仍然实现了它。您可以按照以下说明使用它:
git clone https://huggingface.co/bigcode/starcoderbase-megatrongit clone -b mtf https://github.com/bigcode-project/Megatron-LMinputs 和 outputs。inputs 应包含提示和指令,而 outputs 包含目标。损失仅在 outputs 上计算。参见 dataset/commits_to_jsonl.py 了解如何执行此操作的示例。在该示例中,我们将指令(提交消息)放在目标中,但最好将其放在输入中。dataset/preprocess.sh 以指向您的 jsonl 数据集,从而对微调数据集进行分词。同时修改分词器路径,在我们的情况下指向 StarCoder 的 tokenizer.json(wget https://huggingface.co/bigcode/starcoderbase/raw/main/tokenizer.json)。最后指定存储分词后数据的输出前缀。然后使用 bash dataset/preprocess.sh 运行它。train_data_paths.txt.tmp 和 valid_data_paths.txt.tmp,其中包含上述创建的分词数据集的路径。例如,它们可能看起来像 "train: 1.0 0:0.95 output_prefix" 和 "valid: 1.0 0.95:1.0 output_prefix"。在这种情况下,数据集被分为 95% 训练和 5% 验证。第一个数字是数据集的权重,第二个数字是数据集的开始,第三个数字是数据集的结束。release,即 mv starcoderbase-megatron/iter* starcoderbase-megatron/release,并创建一个文件 starcoderbase-megatron/latest_checkpointed_iteration.txt,其中只包含 release(echo release > starcoderbase-megatron/latest_checkpointed_iteration.txt)。training/finetune_starcoderbase.sh,调整 CHECKPOINT_PATH 以指向下载的 Megatron-LM 检查点,WEIGHTS_TRAIN 和 WEIGHTS_VALID 以指向上面创建的 txt 文件,TOKENIZER_FILE 指向 StarCoder 的 tokenizer.json,指向您的环境和缓存位置,并修改 SBATCH 设置以适应您的设置。然后使用 bash training/finetune_starcoderbase.sh 运行它。您可以中断和恢复训练,但如果恢复,需要从脚本的命令行参数中删除 --no_load_optim 和 --no_load_rng,以从新保存的检查点加载优化器和随机数生成器状态(我们只是不想从 starcoderbase 加载它们)。convert_large.sh 中的脚本转换保存的检查点。它包含了需要下载哪些仓库的说明。图表:
visuals/main.pdf,在 visuals/plots.ipynb 或通过此 colab 创建主图,然后将其添加到 visuals/visuals.drawio 的正确标签页中,可使用 drawio 打开visuals/distribution.pdf,通过 visuals/plots.ipynb 或 colab 创建visuals/tasks.pdf,通过 visuals/distribution_tasks.py 创建visuals/humanevalpack.pdf,通过 visuals/visuals.drawio 创建,可使用 drawio 打开visuals/ablations.pdf,通过 visuals/plots.ipynb 或此 colab 创建表格:
visual/distribution_languages.py 创建所有内容都以我们可能的最宽松方式授权。
CommitPack、CommitPackFT、HumanEvalPack 和所有代码均根据本仓库的 MIT 许可证授权。请注意,CommitPack 和 CommitPackFT 中的每个样本都有其自己的许可证,对应于其来源仓库,由 license 字段指示。所有样本均来自宽松许可的仓库。您可以查看论文附录了解我们筛选的许可证。
OctoCoder 根据与 StarCoder 相同的许可证授权(商业用途,除非被视为有害的用例)。
OctoGeeX 根据与 CodeGeeX2 相同的许可证授权(商业用途,但需要提交表格)。
@article{muennighoff2023octopack, title={OctoPack: Instruction Tuning Code Large Language Models}, author={Niklas Muennighoff and Qian Liu and Armel Zebaze and Qinkai Zheng and Binyuan Hui and Terry Yue Zhuo and Swayam Singh and Xiangru Tang and Leandro von Werra and Shayne Longpre}, journal={arXiv preprint arXiv:2308.07124}, year={2023} }


职场AI,就用扣子
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!


多风格AI绘画神器
堆友平台由阿里巴巴设计团 队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。

