初始化项目,由ModelHub XC社区提供模型
Model: aiXcoder/aixcoder-7b-base Source: Original Platform
This commit is contained in:
310
README.md
Normal file
310
README.md
Normal file
@@ -0,0 +1,310 @@
|
||||
# aiXcoder-7B Code Large Language Model
|
||||
|
||||
<p align="center">
|
||||
🏠 <a href="https://www.aixcoder.com/" target="_blank">官网</a>|🛠 <a href="https://marketplace.visualstudio.com/items?itemName=aixcoder-plugin.aixcoder" target="_blank">VS Code 插件</a>|🛠 <a href="https://plugins.jetbrains.com/plugin/13574-aixcoder-code-completer" target="_blank">Jetbrains 插件</a>|🤗 <a href="https://huggingface.co/aiXcoder/aixcoder-7b-base" target="_blank">模型下载</a>|<a href="./assets/wechat_1.jpg" target="_blank">技术交流群</a>|<a href="./assets/wechat_2.jpg" target="_blank">公众号</a>
|
||||
</p>
|
||||
|
||||
欢迎来到aiXcoder-7B代码大型语言模型的官方仓库。该模型旨在理解和生成多种编程语言中的代码,提供在代码完成、理解、生成以及更多关于编程语言的任务中的最先进性能。
|
||||
|
||||
目录
|
||||
|
||||
1. [模型简介](#模型简介)
|
||||
2. [快速上手](#快速上手)
|
||||
- [运行环境](#运行环境)
|
||||
- [模型权重](#模型权重)
|
||||
- [推理示例](#推理示例)
|
||||
6. [License](#license)
|
||||
7. [Acknowledgments](#acknowledgments)
|
||||
|
||||
|
||||
|
||||
## 模型简介
|
||||
|
||||
随着代码大模型的能力逐渐被挖掘出来,aiXcoder 也一直在思考怎样才能令代码大模型在实际开发场景中有更大的帮助。为此,我们开源了 aiXcoder 7B Base,该模型在1.2T Unique Tokens上做了大量的训练,并且该模型的预训练任务及上下文信息都为真实代码生成场景做了独特的设计。
|
||||
|
||||
aiXcoder 7B Base 在代码补全场景下是所有同等级参数量模型中效果最好的,主流多语言nl2code benchmark 平均上效果也超过codellama 34B 和StarCoder2 15B。
|
||||
|
||||
在我们持续推动代码大模型应用的探索过程中,aiXcoder 7B Base 模型的发布标志着一个重要的里程碑。当前版本的 aiXcoder 7B Base 是一个基础模型,专注于提升代码补全和代码生成的效率与准确性,旨在为开发人员在这些场景下提供强有力的支持。值得注意的是,这个版本尚未经过特别的instruct微调,意味着它在特定的高级任务如测试用例生成和代码调试方面可能还未达到最优表现。
|
||||
|
||||
然而,我们已经在规划中包含了对aiXcoder模型系列的进一步发展。在不久的将来,我们计划发布新的模型版本,这些版本将经过精心的Instruct微调,专门针对更广泛的编程任务,包括但不限于测试用例生成和代码调试。通过这些经过Instruct微调的模型,我们期待能够为开发者提供更全面、更深入的编程支持,帮助他们在软件开发的每一个阶段都能发挥出最大的效率。
|
||||
|
||||
## 快速上手
|
||||
|
||||
### 运行环境
|
||||
|
||||
#### 选择一:构建一个运行环境
|
||||
|
||||
主要的环境依赖为:
|
||||
|
||||
- Python 3.8 or higher
|
||||
- PyTorch 2.1.0 or higher
|
||||
- sentencepiece 0.2.0 or higher
|
||||
- transformers 4.34.1 or higher (if run inference by transformers library)
|
||||
|
||||
在支持CUDA环境的宿主机或者容器内,执行以下命令安装环境依赖项:
|
||||
|
||||
```bash
|
||||
conda create -n aixcoder-7b python=3.11
|
||||
conda activate aixcoder-7b
|
||||
git clone git@github.com:aixcoder-plugin/aiXcoder-7b.git
|
||||
cd aiXcoder-7b
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
`requirements.txt` 列举了所有的依赖项及其版本号。
|
||||
|
||||
如果想要加快推理速度,我们强烈建议安装 FlashAttention 库(可选)。在确定您的芯片版本与CUDA版本支持FlashAttention 的条件下,可通过以下步骤进行安装:
|
||||
|
||||
```bash
|
||||
git clone git@github.com:Dao-AILab/flash-attention.git
|
||||
cd flash-attention
|
||||
MAX_JOBS=8 python setup.py install
|
||||
```
|
||||
|
||||
#### Option 2: Docker
|
||||
|
||||
为了更好地隔离开发环境,我们建议您可以在 Docker 容器内运行模型推理。如下是启动准备 docker 环境的步骤:
|
||||
|
||||
1. 安装 Docker:如果您的机器还没有安装Docker,您可以参考官方的安装步骤安装。
|
||||
|
||||
2. 拉取镜像: 从 Docker Hub 拉取 PyTorch 镜像。
|
||||
|
||||
```bash
|
||||
docker pull pytorch/pytorch:2.1.0-cuda11.8-cudnn8-devel
|
||||
```
|
||||
|
||||
3. 启动容器: 拉取docker 镜像后,可以启动容器,并在容器中运行模型。
|
||||
|
||||
```bash
|
||||
docker run --gpus all -it -v /dev/shm:/dev/shm --name aix_instance pytorch/pytorch:2.1.0-cuda11.8-cudnn8-devel /bin/bash
|
||||
pip install sentencepiece
|
||||
git clone git@github.com:aixcoder-plugin/aiXcoder-7b.git
|
||||
cd aiXcoder-7b
|
||||
```
|
||||
|
||||
如果想要加快推理速度,我们强烈建议安装 FlashAttention 库(可选)。在确定您的芯片版本与CUDA版本支持FlashAttention 的条件下,可通过以下步骤进行安装:
|
||||
|
||||
```bash
|
||||
git clone git@github.com:Dao-AILab/flash-attention.git
|
||||
cd flash-attention
|
||||
MAX_JOBS=8 python setup.py install
|
||||
```
|
||||
|
||||
4. 模型推理: 在容器内,您可以安装推理示例代码进行预测。
|
||||
|
||||
|
||||
### 模型权重
|
||||
|
||||
您能从以下地址下载模型:
|
||||
|
||||
- [aiXcoder Base Download](https://www.modelscope.cn/models/aiXcoder/aiXcoder-7b-base)
|
||||
- aiXcoder Instruct Download (Comming soon...)
|
||||
|
||||
### 推理示例
|
||||
|
||||
#### 命令行执行
|
||||
|
||||
如果需要快速执行,只需要通过以下命令行即可运行推理样本:
|
||||
|
||||
```bash
|
||||
torchrun --nproc_per_node 1 sess_megatron.py --model_dir "path/to/model_weights_dir"
|
||||
```
|
||||
|
||||
将 "path/to/model_weights_dir"替换为您下载模型权重后的本地地址。
|
||||
|
||||
或者通过 huggingface 的 transformers 库进行推理测试:
|
||||
|
||||
```bash
|
||||
python sess_huggingface.py
|
||||
```
|
||||
|
||||
或者通过魔塔的 modelscope 库进行推理测试:
|
||||
|
||||
```bash
|
||||
python sess_modelscope.py
|
||||
```
|
||||
|
||||
#### Python 脚本
|
||||
|
||||
如果您想嵌入自己的工具流,或者获得更灵活的使用方式,您能通过以下代码直接调用:
|
||||
|
||||
```python
|
||||
|
||||
from sess_megatron import TestInference
|
||||
|
||||
infer = TestInference()
|
||||
res = infer.run_infer(
|
||||
# for FIM style input, code_string stands for prefix context
|
||||
code_string="""# 快速排序算法""",
|
||||
# for FIM style input, later_code stands for suffix context
|
||||
later_code="\n",
|
||||
# file_path should be a path from project to file
|
||||
file_path="test.py",
|
||||
# max num for generated tokens
|
||||
max_new_tokens=256,
|
||||
)
|
||||
print(res)
|
||||
|
||||
"""output:
|
||||
|
||||
def quick_sort(arr):
|
||||
if len(arr) <= 1:
|
||||
return arr
|
||||
pivot = arr[0]
|
||||
less = [i for i in arr[1:] if i <= pivot]
|
||||
greater = [i for i in arr[1:] if i > pivot]
|
||||
return quick_sort(less) + [pivot] + quick_sort(greater)
|
||||
|
||||
|
||||
# 测试
|
||||
arr = [3, 2, 1, 4, 5]
|
||||
print(quick_sort(arr)) # [1, 2, 3, 4, 5]
|
||||
"""
|
||||
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
|
||||
import torch
|
||||
import sys
|
||||
from hf_mini.utils import input_wrapper
|
||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||
|
||||
device = "cuda" # the device to load the model onto
|
||||
|
||||
tokenizer = AutoTokenizer.from_pretrained("aiXcoder/aixcoder-7b-base")
|
||||
model = AutoModelForCausalLM.from_pretrained("aiXcoder/aixcoder-7b-base", torch_dtype=torch.bfloat16)
|
||||
|
||||
|
||||
text = input_wrapper(
|
||||
# for FIM style input, code_string stands for prefix context
|
||||
code_string="# 快速排序算法",
|
||||
# for FIM style input, later_code stands for suffix context
|
||||
later_code="\n# 测试\narr = [3, 2, 1, 4, 5]\nprint(quick_sort(arr)) # [1, 2, 3, 4, 5]",
|
||||
# file_path should be a path from project to file
|
||||
path="test.py"
|
||||
)
|
||||
|
||||
if len(text) == 0:
|
||||
sys.exit()
|
||||
|
||||
inputs = tokenizer(text, return_tensors="pt", return_token_type_ids=False)
|
||||
|
||||
inputs = inputs.to(device)
|
||||
model.to(device)
|
||||
|
||||
outputs = model.generate(**inputs, max_new_tokens=256)
|
||||
print(tokenizer.decode(outputs[0], skip_special_tokens=False))
|
||||
|
||||
|
||||
|
||||
"""output:
|
||||
def quick_sort(arr):
|
||||
# 如果数组长度小于等于1,直接返回
|
||||
if len(arr) <= 1:
|
||||
return arr
|
||||
# 选择数组的第一个元素作为基准
|
||||
pivot = arr[0]
|
||||
# 初始化左右指针
|
||||
left, right = 1, len(arr) - 1
|
||||
# 循环直到左指针小于右指针
|
||||
while left < right:
|
||||
# 从右到左找到第一个小于基准的元素,与左指针元素交换
|
||||
if arr[right] < pivot:
|
||||
arr[left], arr[right] = arr[right], arr[left]
|
||||
left += 1
|
||||
# 从左到右找到第一个大于等于基准的元素,与右指针元素交换
|
||||
if arr[left] >= pivot:
|
||||
right -= 1
|
||||
# 将基准元素与左指针元素交换
|
||||
arr[left], arr[0] = arr[0], arr[left]
|
||||
# 对左半部分进行递归排序
|
||||
quick_sort(arr[:left])
|
||||
# 对右半部分进行递归排序
|
||||
quick_sort(arr[left + 1:])
|
||||
return arr</s>
|
||||
"""
|
||||
|
||||
```
|
||||
|
||||
如果要在 modelscope 中使用模型,请先通过 `pip install modelscope` 命令安装推理库。
|
||||
|
||||
```python
|
||||
|
||||
import torch
|
||||
import sys
|
||||
from hf_mini.utils import input_wrapper
|
||||
from modelscope import AutoModelForCausalLM, AutoTokenizer
|
||||
|
||||
device = "cuda" # the device to load the model onto
|
||||
|
||||
tokenizer = AutoTokenizer.from_pretrained("aiXcoder/aixcoder-7b-base")
|
||||
model = AutoModelForCausalLM.from_pretrained("aiXcoder/aixcoder-7b-base", torch_dtype=torch.bfloat16)
|
||||
|
||||
|
||||
text = input_wrapper(
|
||||
# for FIM style input, code_string stands for prefix context
|
||||
code_string="# 快速排序算法",
|
||||
# for FIM style input, later_code stands for suffix context
|
||||
later_code="\n# 测试\narr = [3, 2, 1, 4, 5]\nprint(quick_sort(arr)) # [1, 2, 3, 4, 5]",
|
||||
# file_path should be a path from project to file
|
||||
path="test.py"
|
||||
)
|
||||
|
||||
if len(text) == 0:
|
||||
sys.exit()
|
||||
|
||||
inputs = tokenizer(text, return_tensors="pt", return_token_type_ids=False)
|
||||
|
||||
inputs = inputs.to(device)
|
||||
model.to(device)
|
||||
|
||||
outputs = model.generate(**inputs, max_new_tokens=256)
|
||||
print(tokenizer.decode(outputs[0], skip_special_tokens=False))
|
||||
|
||||
|
||||
|
||||
"""output:
|
||||
def quick_sort(arr):
|
||||
# 如果数组长度小于等于1,直接返回
|
||||
if len(arr) <= 1:
|
||||
return arr
|
||||
# 选择数组的第一个元素作为基准
|
||||
pivot = arr[0]
|
||||
# 初始化左右指针
|
||||
left, right = 1, len(arr) - 1
|
||||
# 循环直到左指针小于右指针
|
||||
while left < right:
|
||||
# 从右到左找到第一个小于基准的元素,与左指针元素交换
|
||||
if arr[right] < pivot:
|
||||
arr[left], arr[right] = arr[right], arr[left]
|
||||
left += 1
|
||||
# 从左到右找到第一个大于等于基准的元素,与右指针元素交换
|
||||
if arr[left] >= pivot:
|
||||
right -= 1
|
||||
# 将基准元素与左指针元素交换
|
||||
arr[left], arr[0] = arr[0], arr[left]
|
||||
# 对左半部分进行递归排序
|
||||
quick_sort(arr[:left])
|
||||
# 对右半部分进行递归排序
|
||||
quick_sort(arr[left + 1:])
|
||||
return arr</s>
|
||||
"""
|
||||
|
||||
```
|
||||
|
||||
|
||||
## License
|
||||
|
||||
|
||||
The model weights are licensed under the [Model License](./MODEL_LICENSE) for academic research use; for commercial use, please apply by sending an email to support@aixcoder.com.
|
||||
|
||||
|
||||
## Acknowledgments
|
||||
|
||||
We would like to thank all contributors to the open-source projects and datasets that made this work possible.
|
||||
|
||||
For any questions or issues, please open an issue on this repository.
|
||||
|
||||
Thank you for your interest in our Code Large Language Model. We look forward to your contributions and feedback!
|
||||
Reference in New Issue
Block a user