大模型落地加速工具-fastllm

## 功能概述

- 纯c++实现,便于跨平台移植,可以在安卓上直接编译
- ARM平台支持NEON指令集加速,X86平台支持AVX指令集加速,NVIDIA平台支持CUDA加速,各个平台速度都很快就是了
- 支持浮点模型(FP32), 半精度模型(FP16), 量化模型(INT8, INT4) 加速
- 支持Batch速度优化
- 支持流式输出,很方便实现打字机效果
- 支持并发计算时动态拼Batch
- 支持python调用
- 前后端分离设计,便于支持新的计算设备
- 目前支持ChatGLM模型,各种LLAMA模型(ALPACA, VICUNA等),BAICHUAN模型,MOSS模型

## 两行代码加速 (测试中,暂时只支持ubuntu)

使用如下命令安装fastllm_pytools包

``` sh
cd fastllm
mkdir build
cd build
cmake .. -DUSE_CUDA=ON # 如果不使用GPU编译,那么使用 cmake .. -DUSE_CUDA=OFF
make -j
cd tools && python setup.py install
```

然后只需要在原本的推理程序中加入两行即可使用fastllm加速

``` python
# 这是原来的程序,通过huggingface接口创建模型
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code = True)
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code = True)

# 加入下面这两行,将huggingface模型转换成fastllm模型
from fastllm_pytools import llm
model = llm.from_hf(model, tokenizer, dtype = "float16") # dtype支持 "float16", "int8", "int4"
```

model支持了ChatGLM的API函数chat, stream_chat,因此ChatGLM的demo程序无需改动其他代码即可运行

model还支持下列API用于生成回复

``` python
# 生成回复
print(model.response("你好"))

# 流式生成回复
for response in model.stream_response("你好"):
    print(response, flush = True, end = "")
```

转好的模型也可以导出到本地文件,之后可以直接读取,也可以使用fastllm cpp接口读取

``` python
model.save("model.flm"); # 导出fastllm模型
new_model = llm.model("model.flm"); # 导入fastllm模型
```

注: 该功能处于测试阶段,目前仅验证了ChatGLM、ChatGLM2模型可以通过2行代码加速

## 推理速度

6B级int4模型单4090延迟最低约5.5ms

6B级fp16模型单4090最大吞吐量超过10000 token / s

6B级int4模型在骁龙865上速度大约为4~5 token / s

你可能感兴趣的:(开源大模型微调,人工智能)