盘点开源“Copilot”,do it yourself

· transformers / nlp / text generation / github / gpt / onnxruntime / codegen / vscode / fastgpt

目录

1.背景

  1. Github Copilot即将收费:
    • Copilot 官方近期宣布结束技术预览,并将在 2022 年 8 月 22 日开始收费, 收费标准为每月 10 美元或者每年 100 美元。学生和热门开源项目维护者可以免费使用。
  2. 程序员已经离不开 Copilot:
    • Github声称,当前网站上 1/3 的代码都是在 Copilot 工具下完成的。而笔者也是在使用了半年的 Copilot 后,已经很难离开它的帮助,它已帮我做了很多重复性的编程工作。
  3. 开源代码生成模型:
    • Huggingface Model Hub社区有很多开源模型可以直接下载使用,其中不乏一些开源代码生成模型,那么为什么不可以do it yourself。
  4. 私有化部署一套”Copilot”:
    • 如果我们使用开源的代码生成模型自己部署一个代码生成服务,再辅以编辑器/IDE 插件,就可以模拟 Copilot 为自己和同事做代码生成服务。而且还有以下优点:
      1. 免去连 Copilot 偶尔的网络不稳定问题
      2. 免去代码上传 Copilot 的安全问题
      3. 根据自己的编码习惯,已有代码,对开源模型进行二次训练,为自己定制更懂自己的模型

2.简述

在本博客中,我们先从用户的角度,盘点一下当前开源代码生成模型的生成效果;然后自己搭建代码生成服务,搭建 Vscode 插件,为自己提供私有化”Copilot”。

3. 盘点开源代码生成模型

3.1. 模型清单

编号 模型 参数量 贡献者 训练语料 链接 支持 transformers Huggingface 社区月下载量(May 2022)
1 code-autocomplete-distilgpt2-python 81.91M shibing624 Python 链接 ✔️ 37k
2 code-autocomplete-gpt2-base 124.44M shibing624 Python 链接 ✔️ 129
3 CodeGPT-small-py-adaptedGPT2 124.44M Microsoft Python 链接 ✔️ 5.57k
4 CodeGPT-small-java-adaptedGPT2 124.44M Microsoft Java 链接 ✔️ 3.31k
5 incoder-6B 6.7B Facebook Python JavaScript 链接 ✔️ 782
6 incoder-1B 1B Facebook Python JavaScript 链接 ✔️ 4.53k
7 codegen-350M-mono 350M Salesforce Python 链接 ❌ 232
8 codegen-2B-mono 2B Salesforce Python 链接 ❌ 438
9 codegen-6B-mono 6B Salesforce Python 链接 ❌ 180
10 codegen-16B-mono 16B Salesforce Python 链接 ❌ 89
11 codegen-350M-multi 350M Salesforce multiple programming languages 链接 ❌ 160
12 codegen-2B-multi 2B Salesforce multiple programming languages 链接 ❌ 140
13 codegen-6B-multi 6B Salesforce multiple programming languages 链接 ❌ 100
14 codegen-16B-multi 16B Salesforce multiple programming languages 链接 ❌ 30
15 gpt-neo-125M-code-search-py 125.20M Flax-community Python 链接 ✔️ 817
16 gpt-neo-125M-code-clippy 125.20M Flax-community multiple programming languages 链接 ✔️ 397
17 GPT2-python-code-generator 124.44M SIC98 Python 链接 ✔️ 815
18 codeparrot 1.5B CodeParrot Python 链接 ✔️ 304
19 codeparrot-small 110M CodeParrot Python 链接 ✔️ 88

3.2. 模型测试

3.2.1. Python语言代码生成测试1


3.2.2. Python语言代码生成测试2


3.2.3. Python语言代码生成测试3

from flask_jwt_extended import JWTManager
from flask_apscheduler import APScheduler
from flask import Flask
from utils import scheduler_build

jwt_app = Flask(__name__)
class SchedulerConfig(object):
    JOBS = [
        {
            "id": "scheduler_build",
            "func": scheduler_build,
            "trigger": "interval",
            "seconds": 10,
        }
    ]
    SCHEDULER_API_ENABLED = True

scheduler = APScheduler()

3.2.4.Vue.js语言代码生成测试4



3.3.亮点

  1. codegen-6B-mono在测试1中,完美的写出了TextCNN的网络结构

3.4.结论

  1. 模型大真的能力会更强,参考codegen-6B-mono的表现
  2. Model Hub的下载量水分很大,参考code-autocomplete-distilgpt2-python的表现
  3. 分领域很有用,参考codegen-6B-mono相比codegen-6B-multi在Python问题上的表现
  4. Sailesforce的codegen系列,比其他开源代码生成模型好了一个档次
  5. 开源代码生成模型都是围绕Python语言居多,偶尔有全栈语言

4.搭建私有化代码生成服务

4.1. onnx量化压缩

4.1.1. fastgpt安装方法

pip install fastgpt

4.1.2. fastgpt快速使用

from transformers import AutoTokenizer
from fastgpt import CausalLMModelForOnnxGeneration
model = CausalLMModelForOnnxGeneration.from_pretrained("distilgpt2")
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")

prompt_text = "Natural language processing (NLP) is the ability of a computer program to understand human language as it is spoken and written"
input_ids = tokenizer(
    prompt_text, return_tensors="pt", add_special_tokens=False
).input_ids

generated_ids = model.generate(   # 这里完全兼容transformers的generate函数
    input_ids,
    max_length=64 + input_ids.shape[1],
    decoder_start_token_id=tokenizer.cls_token_id,
    eos_token_id=tokenizer.sep_token_id,
    output_scores=True,
    temperature=1,
    repetition_penalty=1.0,
    top_k=50,
    top_p=0.9,
    do_sample=True,
    num_return_sequences=1,
    length_penalty=2.0,
    early_stopping=True,
)
print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
print("=" * 20)

4.2. 私有化web服务

4.2.1. docker-compose启动

version: "2.3"
services:
  fastgpt-codegen:
    container_name: fastgpt-codegen
    image: lowinli98/fastgpt-codegen:v0.0.7
    expose:
      - 7104
    ports:
      - "7104:7104"
    environment:
      - PORT=7104
      - GUNICORN_WORKER=1
      - GUNICORN_THREADS=1
    restart: always

4.2.2. 测试

codegen-350M-mono

curl --location --request POST 'http://172.16.104.29:7104/generate_mono' \
--header 'Content-Type: application/json' \
--data-raw '{
    "inputs": "def calculdate_mean(x, y): \n",
    "parameters": {
        "do_sample": true
    }
}'

codegen-350M-multi

curl --location --request POST 'http://172.16.104.29:7104/generate_multi' \
--header 'Content-Type: application/json' \
--data-raw '{
    "inputs": "def calculdate_mean(x, y): \n",
    "parameters": {
        "do_sample": true
    }
}'

5.制作私有化Vscode插件

6.enjoy-coding

附录:

1. 推理计算资源

模型 硬盘占用 torch加载使用内存 测试1用时 测试2用时 测试3用时
codegen-16B-mono 30G * * * *
codegen-6B-mono 14G 40G 346s 96s 45s
codegen-2B-mono 614M 17G 38s 13s 38s
codegen-350M-mono 167M 3G 5s 5s 5s