zai-org/CodeGeeX2
Python
Captured source
source ↗zai-org/CodeGeeX2
Description: CodeGeeX2: A More Powerful Multilingual Code Generation Model
Language: Python
License: Apache-2.0
Stars: 7553
Forks: 535
Open issues: 211
Created: 2023-07-23T18:26:53Z
Pushed: 2024-07-10T12:34:55Z
Default branch: main
Fork: no
Archived: no
README: 
🏠 主页|🛠 插件 VS Code, Jetbrains|🤗 模型下载|📄 论文|👋 加入微信开发者交流群
Read this in [English](README_EN.md)
[日本語](README_JA.md)で読む
Lire en [Français](README_FR.md)
⭐️ 最新一代 CodeGeeX4 模型已经正式开源。 The newest CodeGeeX4 has been released.
CodeGeeX2: 更强大的多语言代码生成模型
CodeGeeX2 是多语言代码生成模型 CodeGeeX (KDD’23) 的第二代模型。不同于一代 CodeGeeX(完全在国产华为昇腾芯片平台训练) ,CodeGeeX2 是基于 ChatGLM2 架构加入代码预训练实现,得益于 ChatGLM2 的更优性能,CodeGeeX2 在多项指标上取得性能提升(+107% > CodeGeeX;仅60亿参数即超过150亿参数的 StarCoder-15B 近10%),更多特性包括:
- 更强大的代码能力:基于 ChatGLM2-6B 基座语言模型,CodeGeeX2-6B 进一步经过了 600B 代码数据预训练,相比一代模型,在代码能力上全面提升,HumanEval-X 评测集的六种编程语言均大幅提升 (Python +57%, C++ +71%, Java +54%, JavaScript +83%, Go +56%, Rust +321\%),在Python上达到 35.9\% 的 Pass@1 一次通过率,超越规模更大的 StarCoder-15B。
- 更优秀的模型特性:继承 ChatGLM2-6B 模型特性,CodeGeeX2-6B 更好支持中英文输入,支持最大 8192 序列长度,推理速度较一代 CodeGeeX-13B 大幅提升,量化后仅需6GB显存即可运行,支持轻量级本地化部署。
- 更全面的AI编程助手:CodeGeeX插件(VS Code, Jetbrains)后端升级,支持超过100种编程语言,新增上下文补全、跨文件补全等实用功能。结合 Ask CodeGeeX 交互式AI编程助手,支持中英文对话解决各种编程问题,包括且不限于代码解释、代码翻译、代码纠错、文档生成等,帮助程序员更高效开发。
- 更开放的协议:CodeGeeX2-6B 权重对学术研究完全开放,填写登记表申请商业使用。
使用教程
- [快速开始](#快速开始)
- [推理教程(多卡推理,加速推理,多平台推理等)](docs/zh/inference_zh.md)
AI编程助手

我们开发了支持 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()
# remember adding a language tag for better performance
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]))启动 Gradio DEMO:
python ./demo/run_demo.py usage: 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] # 若要启用身份验证,请先启用--auth,然后定义--username与--password,如: 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
启动FAST API:
python ./demo/fastapicpu.py usage: 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 参数即可。
API使用示例
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 定义与论文中一致):
HumanEval (Pass@1,10,100)
| Model | 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。
HumanEval-X (Pass@1)
| Model | Python | C++ | Java | JavaScript | Go | Rust | Overall | | :------------------: | :--------: | :-------: | :-------: | :------------: | :-------: | :-------: | :---------: | | 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 | |…
Excerpt shown — open the source for the full document.