多语言代码生成模型显著提升开发效率
CodeGeeX2是基于ChatGLM2架构的多语言代码生 成模型,经600B代码数据预训练。支持超过100种编程语言,在多项指标上性能显著提升。模型具备代码补全、解释、翻译等功能,可显著提高开发效率。CodeGeeX2开放学术研究使用,商用需申请授权。
⭐️ 最新一代 CodeGeeX4 模型已经正式开源。 最新的 CodeGeeX4 已经发布。
CodeGeeX2 是多语言代码生成模型 CodeGeeX (KDD'23) 的第二代模型。与第一代 CodeGeeX(完全在国产华为昇腾芯片平台训练)不同,CodeGeeX2 是基于 ChatGLM2 架构加入代码预训练实现的。得益于 ChatGLM2 的更优性能,CodeGeeX2 在多项指标上取得性能提升(相比 CodeGeeX 提升107%;仅60亿参数即超过150亿参数的 StarCoder-15B 近10%),更多特性包括:
我们开发了支持 VS Code、IntelliJ IDEA、PyCharm、GoLand、WebStorm、Android Studio 等IDE的 CodeGeeX 插件。在插件中,可以更直接地体验到 CodeGeeX2 模型在代码生成与补全、添加注释、代码翻译及技术问答方面的能力为开发效率带来的提升。欢迎在IDE中下载 CodeGeeX 插件获得更加全面的AI编程体验,详情见CodeGeeX主页。
transformers
快速调用CodeGeeX2-6B:from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("THUDM/codegeex2-6b", trust_remote_code=True) model = AutoModel.from_pretrained("THUDM/codegeex2-6b", trust_remote_code=True, device='cuda') model = model.eval() # 记得添加语言标签以获得更好的性能 prompt = "# language: Python\n# write a bubble sort function\n" inputs = tokenizer.encode(prompt, return_tensors="pt").to(model.device) outputs = model.generate(inputs, max_length=256, top_k=1) response = tokenizer.decode(outputs[0]) >>> print(response) # language: Python # write a bubble sort function def bubble_sort(list): for i in range(len(list) - 1): for j in range(len(list) - 1): if list[j] > list[j + 1]: list[j], list[j + 1] = list[j + 1], list[j] return list print(bubble_sort([5, 2, 1, 8, 4]))
python ./demo/run_demo.py
用法: run_demo.py [-h] [--model-path MODEL_PATH] [--example-path EXAMPLE_PATH] [--quantize QUANTIZE] [--chatglm-cpp] [--fastllm] [--n-gpus N_GPUS] [--gpu GPU] [--cpu] [--auth] [--username yourname] [--password yourpassword] [--port PORT] [--listen ADDRESS]
python run_demo.py --auth --username user --password password # 如需监听所有地址请指定 --listen 0.0.0.0
支持使用 ChatGLM.cpp 进行量化推理加速:
python ./demo/run_demo.py --quantize 4 --chatglm-cpp
python ./demo/fastapicpu.py
用法: fastapicpu.py [-h] [--model-path MODEL_PATH] [--listen ADDRESS] [--port PORT] [--workders NUM] [--cpu] [--half] [--quantize QUANTIZE] [--chatglm-cpp]
# --cpu启用CPU --half启用.half()
同样支持使用 ChatGLM.cpp 进行量化推理加速,只需添加 --quantize 4 --chatglm-cpp
参数即可。
curl -X POST "http://127.0.0.1:7860" \
-H 'Content-Type: application/json' \
-d '{"lang": "Python", "prompt": "# Write a quick sort function"}'
❗️请注意:
CodeGeeX2-6B 是一个基础代码生成模型,不具备聊天能力。请前往插件中体验更全面的 Ask CodeGeeX 聊天功能。
使用 CodeGeeX2-6B 的补全功能时,输入prompt需要遵循特定格式以获得最佳效果。例如需要在开头添加编程语言标签(# language: Python
,请查看完整语言列表),以注释形式编写prompt等。参考run_demo.py
中的处理。
如果显卡不支持bfloat16
格式,将会输出错误内容,需要将模型转换为float16
格式:
model = AutoModel.from_pretrained("THUDM/codegeex2-6b", trust_remote_code=True).half().cuda()
如需使 用多显卡加载模型,可将以下代码:
tokenizer = AutoTokenizer.from_pretrained("THUDM/codegeex2-6b", trust_remote_code=True) model = AutoModel.from_pretrained("THUDM/codegeex2-6b", trust_remote_code=True, device='cuda') model = model.eval()
替换为
def get_model(): tokenizer = AutoTokenizer.from_pretrained("THUDM/codegeex2-6b", trust_remote_code=True) from gpus import load_model_on_gpus # gpus文件在demo文件夹中 model = load_model_on_gpus("THUDM/codegeex2-6b", num_gpus=2) model = model.eval() return tokenizer, model tokenizer, model = get_model()
作为一个多语言代码生成基座模型,CodeGeeX2 的代码能力较上一代有显著提升。以下是在 HumanEval、HumanEval-X 和 DS1000 基准上的评测结果(评价指标 Pass@k 定义与论文中一致):
模型 | Pass@1 | Pass@10 | Pass@100 |
---|---|---|---|
CodeGen-16B-multi | 19.2 | 34.6 | 55.2 |
CodeGeeX-13B | 22.9 | 39.6 | 60.9 |
Codex-12B | 28.8 | 46.8 | 72.3 |
CodeT5Plus-16B-mono | 30.9 | 51.6 | 76.7 |
Code-Cushman-001 | 33.5 | 54.3 | 77.4 |
LLaMA-65B | 23.7 | - | 79.3 |
LLaMA2-70B | 29.9 | - | - |
CodeGen2.5-7B-mono | 33.4 | 58.4 | 82.7 |
StarCoder-15B | 33.2 | 61.0 | 84.7 |
CodeGeeX2-6B | 35.9 | 62.6 | 88.3 |
Pass@1 使用
n=20, t=0.2, top_p=0.95
;Pass@10,Pass@100 使用n=200, t=0.8, top_p=0.95
。
模型 | Python | C++ | Java | JavaScript | Go | Rust | 总体 |
---|---|---|---|---|---|---|---|
CodeGen-16B-multi | 19.2 | 18.1 | 15.0 | 18.4 | 13.0 | 1.8 | 14.2 |
CodeGeeX-13B | 22.9 | 17.1 | 20.0 | 17.6 | 14.4 | 4.3 | 16.0 |
Replit-code-v1-3B | 22.0 | 20.1 | 20.1 | 20.1 | 12.2 | 8.6 | 17.2 |
CodeGen2.5-7B-multi | 30.6 | 24.3 | 29.0 | 27.5 | 18.9 | 20.1 | 25.1 |
StarCoder-15B | 35.5 | 28.2 | 31.5 | 33.2 | 21.3 | 17.8 | 27.9 |
CodeGeeX2-6B | 35.9 | 29.3 | 30.8 | 32.2 | 22.5 | 18.1 | 28.1 |
Pass@1 使用
n=20, t=0.2, top_p=0.95
。
以上结果可使用脚本scripts/run_humanevalx.sh
复现。环境配置和说明参见评测环境。
模型 | Matplotlib | Numpy | Pandas | Pytorch | SciPy | Scikit-learn | TensorFlow | 总体 |
---|---|---|---|---|---|---|---|---|
样本数 | 155 | 220 | 291 | 68 | 106 | 115 | 45 | 1000 |
CodeGen-16B-Mono | 31.7 | 10.9 | 3.4 | 7.0 | 9.0 | 10.8 | 15.2 | 11.7 |
code-cushman-001 | 40.7 | 21.8 | 7.9 | 12.4 | 11.3 | 18.0 | 12.2 | 18.1 |
Codex-001 | 41.8 | 26.6 | 9.4 | 9.7 | 15.0 | 18.5 | 17.2 | 20.2 |
CodeGeeX2-6B | 40.5 | 25.5 | 14.5 | 17.3 | 19.3 | 24.0 | 23.0 | 23.1 |
StarCoder-15B | 51.7 | 29.7 | 11.4 | 21.4 | 20.2 | 29.5 | 24.5 | 26.0 |
Codex-002 | 57.0 | 43.1 | 26.5 | 41.8 | 31.8 | 44.8 | 39.3 | 39.2 |
Pass@1 使用
n=40, t=0.2, top_p=0.5
。
以上结果可使用DS1000评测代码复现。
与上一代相比,CodeGeeX2更适合部署。得益于使用多查询注意力和闪现注意力,推理速度更快,且量化后仅需6GB显存即可运行:
模型 | FP16/BF16 | INT8 | INT4 |
---|---|---|---|
CodeGeeX-13B | 26.9 GB | 14.7 GB | - |
CodeGeeX2-6B | 13.1 GB | 8.2 GB | 5.5 GB |
基于 PyTorch 2.0 测试,利用
torch.nn.functional.scaled_dot_product_attention
实现高效的注意力计算。
模型 | 推理速度 (字符/秒) |
---|---|
CodeGeeX-13B | 32 |
CodeGeeX2-6B | 94 |
batch_size=1, max_length=2048
,均使用加速框架,测试硬件为GeForce RTX-3090
。
本仓库的代码依照 Apache-2.0 协议开源,模型权重的使用则需要遵循 模型许可。CodeGeeX2-6B 权重对学术研究完全开放,填写登记表申请商业使用。
如果觉得我们的工作有帮助,欢迎引用以下论文:
@inproceedings{zheng2023codegeex,
title={CodeGeeX: A Pre-Trained Model for Code Generation with Multilingual Benchmarking on HumanEval-X},
author={Qinkai Zheng and Xiao Xia and Xu Zou and Yuxiao Dong and Shan Wang and Yufei Xue and Zihan Wang and Lei Shen and Andi Wang and Yang Li and Teng Su and Zhilin Yang and Jie Tang},
booktitle={Proceedings of the 29th ACM SIGKDD Conference on Knowledge Discovery and Data Mining},
pages={5673--5684},
year={2023}
}