StarCoder2本地部署上手体验:程序猿要下岗了吗?

2024年3月2日 1044点热度 0人点赞 0条评论

ServiceNow、Hugging Face 和 NVIDIA 于2月28日宣布发布 StarCoder2,这是一个用于代码生成的开放式大型语言模型系列,为性能、透明度和成本效益设定了新标准。StarCoder2 是与 BigCode 社区合作开发的,由ServiceNow和 Hugging Face管理。

StarCoder2简介

StarCoder2 经过 619 种编程语言的培训,可以进一步培训并嵌入到企业应用程序中,以执行专门的任务,例如应用程序源代码生成、工作流生成、文本摘要等。开发人员可以使用其代码补全、高级代码摘要、代码片段检索和其他功能来加速创新并提高生产力。

StarCoder2 提供三种模型大小:由 ServiceNow 训练的 30 亿参数模型;Hugging Face 训练的 70 亿参数模型;一个由 NVIDIA 与NVIDIA NeMo构建并在 NVIDIA 加速基础设施上进行训练的 150 亿参数模型。较小的变体提供了强大的性能,同时节省了计算成本,因为更少的参数在推理过程中需要更少的计算。事实上,新的 30 亿参数模型与原始 StarCoder 150 亿参数模型的性能相匹配。

ServiceNow StarCoder2 开发团队负责人兼 BigCode 联合负责人 Harm de Vries 强调说:“StarCoder2 证明了开放科学合作和负责任的人工智能实践与道德数据供应链的综合力量。” “最先进的开放访问模型改进了之前的生成式人工智能性能,以提高开发人员的工作效率,并为开发人员提供平等的机会享受代码生成人工智能的好处,这反过来又使任何规模的组织能够更轻松地满足他们的全部需求。商业潜力。”

“在 Hugging Face、ServiceNow 和 NVIDIA 的共同努力下,我们能够发布强大的基础模型,使社区能够在数据和训练完全透明的情况下更高效地构建各种应用程序。” Hugging Face 和 BigCode 联合领导。“随着我们努力实现负责任的人工智能民主化,StarCoder2 证明了开源和开放科学的潜力。” 

NVIDIA 应用研究副总裁 Jonathan Cohen 表示:“由于每个软件生态系统都有专有的编程语言,因此LLM可以推动每个行业在效率和创新方面取得突破。” “NVIDIA 与 ServiceNow 和 Hugging Face 的合作引入了安全、负责任地开发的模型,并支持更广泛地使用负责任的生成式 AI,我们相信这将使全球社区受益。”

StarCoder2模型增强定制应用程序开发

StarCoder2 模型共享最先进的架构和来自 BigCode 的精心策划的数据源,优先考虑透明度和开放治理,以实现大规模负责任的创新。  

StarCoder2 提升了未来人工智能驱动编码应用的潜力,包括文本到代码和文本到工作流功能。通过更广泛、更深入的编程培训,它提供存储库上下文,从而实现准确的、上下文感知的预测。这些进步为经验丰富的软件工程师和公民开发人员提供服务,加速商业价值和数字化转型。 

StarCoder2 的基础是一个名为The Stack v2 的新代码数据集,它比 Stack v1 大 7 倍多。除了高级数据集之外,新的训练技术还可以帮助模型理解低资源编程语言(例如 COBOL)、数学和程序源代码讨论。

StarCoder2利用特定于业务的数据进行微调以提高功能

用户可以使用 NVIDIA NeMo 或Hugging Face TRL等开源工具,根据行业或组织特定的数据对开放式 StarCoder2 模型进行微调。他们可以创建高级聊天机器人来处理更复杂的摘要或分类任务,开发个性化的编码助手,可以快速轻松地完成编程任务,检索相关代码片段,并启用文本到工作流功能。 

组织已经开始微调 StarCoder 基础模型,为其业务创建专门的任务特定功能。 

ServiceNow 的text-to-code Now LLM 专门构建在具有 150 亿参数的 StarCoder LLM 的专用版本上,并针对其工作流程模式、用例和流程进行了微调和培训。Hugging Face 还使用该模型创建了 StarChat 助手。

BigCode 促进人工智能领域的开放科学合作

BigCode 代表了由 Hugging Face 和 ServiceNow 领导的开放式科学合作,致力于负责任的LLM开发。BigCode 社区通过工作组和工作组积极参与 StarCoder2 项目的技术方面,利用 ServiceNow 的 Fast LLM 框架训练 30 亿参数模型、Hugging Face 的 nanotron 框架训练 70 亿参数模型以及 NVIDIA NeMo 云原生框架和NVIDIA TensorRT-LLM软件用于训练和优化 150 亿参数模型。 

StarCoder2 与其前身一样,将在 BigCode Open RAIL-M 许可证下提供,允许免版税访问和使用。为了进一步促进透明度和协作,该模型的支持代码将继续驻留在 BigCode 项目的 GitHub 页面上。

所有 StarCoder2 模型也可从 Hugging Face 下载,并且 StarCoder2 150 亿参数模型可在NVIDIA AI Foundation 模型上使用,供开发人员直接从浏览器或通过 API 端点进行试验。

StarCoder2本地部署上手体验

我上手使用的平台配置是英特尔12代i7 12700K CPU+NVIDIA RTX-4090,24G显存,操作系统为Ubuntu 22.04。

首先要安装并升级到最新开发版本的transformer:

pip install transformers --upgrade
pip install git+https://github.com/huggingface/transformers.git

如果torch和tensorflow没有安装的话则执行如下命令安装:

pip install torch torchvision torchaudio 
pip install tensorflow

运行python3进入python shell:

python3

接下来在python shell里执行如下的指令来加载和使用StarCoder2。因为显卡能力的原因我们这里只尝试了starcoder2-3b的模型。加载7b和15b的模型的时候会出现显卡内存不足的问题。

>>> from transformers import AutoModelForCausalLM, AutoTokenizer 
>>> checkpoint = "bigcode/starcoder2-3b" 
>>> device = "cuda" 
>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) 
>>> model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device) 
>>> inputs = tokenizer.encode("input your desired prompt for code generation", max_length=512, return_tensors="pt").to(device) 
>>> outputs = model.generate(inputs, max_length=2048) 
>>> print(tokenizer.decode(outputs[0]))

在上述的prompt中,我们使用了如下的输入让StarCoder2分别生成python, shell和C++代码:

  • create a python script to search all.txt files in the current directory and replace hello with good-bye in each file
  • create a shell script to search all.txt files in the current directory and replace hello with good-bye in each file
  • create C++ code to search all.txt files in the current directory and replace hello with good-bye in each file

让我们来看看StarCoder2的表现吧。

Python脚本

create a python script to search all.txt files in the current directory and replace hello with good-bye in each file.

# +
import os

def replace_hello(file_name):
    with open(file_name, 'r') as f:
        lines = f.readlines()
    with open(file_name, 'w') as f:
        for line in lines:
            f.write(line.replace('hello', 'good-bye'))

for file_name in os.listdir('.'):
    if file_name.endswith('.txt'):
        replace_hello(file_name)
# -

Shell脚本

create a shell script to search all.txt files in the current directory and replace hello with good-bye in each file.

```
#!/bin/bash

for file in *.txt
do
    sed -i's/hello/good-bye/g' $file
done

```

C++代码

create C++ code to search all.txt files in the current directory and replace hello with good-bye in each file.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
        string line;
        ifstream file("all.txt");
        ofstream newfile("new.txt");
        if (file.is_open())
        {
                while (getline(file, line))
                {
                        newfile << line.replace(line.find("hello"), 5, "good-bye") << endl;
                }
                file.close();
                newfile.close();
        }
        else cout << "Unable to open file";
        return 0;
}

StarCode2本地部署演示视频

予人玫瑰,手有余香。如果您觉得本文对您有帮助,请点赞或打赏。

文章评论

您需要 登录 之后才可以评论