运行Vicuna-7B需要RAM>30GB或者14GB的显存
运行Vicuna-13B需要RAM>60GB或者28GB的显存
如果没有上面的硬件配置请绕行了,我笔记本有64G内存,两个都跑跑看,使用python3.9,当时转换13b时一直崩溃后来发现是没有设定虚拟内存,后来加上了9个G,才可以跑起来
![](https://img-blog.csdnimg.cn/eac64b36b34c4c2abb6265243a89062f.png)
下载llama原始模型
nyanko7/LLaMA-7B at mainWe’re on a journey to advance and democratize artificial intelligence through open source and open science.
https://huggingface.co/nyanko7/LLaMA-7B/tree/mainhuggyllama/llama-13b at mainWe’re on a journey to advance and democratize artificial intelligence through open source and open science.
https://huggingface.co/huggyllama/llama-13b/tree/main也可以用迅雷下载下面的链接,注只要7b,13b就可以了
磁力链接:magnet:?xt=urn:btih:b8287ebfa04f879b048d4d4404108cf3e8014352&dn=LLaMA
下载的文件如下:
![](https://img-blog.csdnimg.cn/4287ca75e1034dad9ae768dd21e0e812.png)
![](https://img-blog.csdnimg.cn/b2aa215881794d178f42978ff1804e99.png)
下载vicuna-7b-delta-v1.1 和vicuna-13b-delta-v1.1
https://huggingface.co/lmsys/vicuna-7b-delta-v1.1/tree/mainWe’re on a journey to advance and democratize artificial intelligence through open source and open science.
https://huggingface.co/lmsys/vicuna-7b-delta-v1.1/tree/mainlmsys/vicuna-13b-delta-v1.1 at mainWe’re on a journey to advance and democratize artificial intelligence through open source and open science.
https://huggingface.co/lmsys/vicuna-13b-delta-v1.1/tree/main
![](https://img-blog.csdnimg.cn/92b0b181640040f8866408a5f636b984.png)
安装相关软件
pip install fschat
pip install protobuf==3.20.0
git clone https://github.com/huggingface/transformers.git
cd transformers
python setup.py install
转换llaMA模型
7b
python transformers/src/transformers/models/llama/convert_llama_weights_to_hf.py --input_dir LLaMA/ --model_size 7B --output_dir ./output/llama-7b
13b
python transformers/src/transformers/models/llama/convert_llama_weights_to_hf.py --input_dir LLaMA/ --model_size 13B --output_dir ./output/llama-13b
![](https://img-blog.csdnimg.cn/c00f8f8180a44493bb4e675f42b2a8ac.png)
![](https://img-blog.csdnimg.cn/cb69b5e53bbc402eb47c06b2fd550346.png)
合并生成Vicuna模型,13b的64g内存罩不住,得要设定虚拟内存16G-64G左右就够了
python -m fastchat.model.apply_delta --base ./output/llama-7b --target ./vicuna-7b --delta ./vicuna-7b-delta-v1.1
python -m fastchat.model.apply_delta --base ./output/llama-13b --target ./vicuna-13b --delta ./vicuna-13b-delta-v1.1
参数介绍:
base |
转换llaMA模型后的路径 |
target |
合并生成后的保存路径 |
delta |
下载的vicuna-7b-delta-v1.1路径 |
![](https://img-blog.csdnimg.cn/b65eb7b7e3214ecd95744c1cf4ac4012.png)
![](https://img-blog.csdnimg.cn/430ce949c2974286b0175fd5f4834238.png)
运行模型,使用半浮点数16位
python -m fastchat.serve.cli --model-path ./vicuna-7b --device cpu
python -m fastchat.serve.cli --model-path ./vicuna-13b --device cpu
7b的占用约26G内存,在64G内存上,i9 12900h运行、响应速度还可以,
![](https://img-blog.csdnimg.cn/e1ea7262bcf4462aaa79494e3cae3527.png)
13b的占用大约50G内存,在64G内存上,i9 12900h运行缓慢
![](https://img-blog.csdnimg.cn/f64382006970472e93d0e631fec105d4.png)
使用量化版本,就是把32位的浮点参数压缩成8位 ,速度会快,占用内存变小,智商会下降
python -m fastchat.serve.cli --model-path ./vicuna-7b --device cpu --load-8bit
python -m fastchat.serve.cli --model-path ./vicuna-13b --device cpu --load-8bit
7B占了7个G![](https://img-blog.csdnimg.cn/1988ce3dee4a42e39f4cf340ad5227c5.png)
13B占了13个G
![](https://img-blog.csdnimg.cn/f8e174f06a4f44b7980c7987ecf9a798.png)
总结:尽管小点的模型可以运行了,如果想自己进行微调还是要使用gpu ,推荐A100显卡,或者A800. 先期不投硬件先租用可以用矩池云 - 专注于人工智能领域的云服务商矩池云是一家专注于人工智能领域的GPU云服务商。提供稳定的人工智能云服务器、人工智能教学实训环境、高速网盘等服务,支持公有云、私有云、专有云、硬件直采等专业级人工智能解决方案。
https://matpool.com/
不过在上述机器上cpu运行得很慢,于是想尝试一下gpu版本,我手里只有一张1080ti,11g内存的,就只能勉强运行一下6B的再加上量化才可以。如何部署呢?
Start Locally | PyTorch
https://pytorch.org/get-started/locally/![](https://img-blog.csdnimg.cn/80e67bd9b7504d808f520362c7649ba1.png)
由于这台电脑 之前的NVIDIA GPU Computing Toolkit是 v10.0的这个pytorch 2.0.1 要求的是11.8的,于是要更新一下CUDA Toolkit Archive | NVIDIA Developer
https://developer.nvidia.com/cuda-toolkit-archive
要注册帐号下载,11.8的版本
![](https://img-blog.csdnimg.cn/79bc2e70f5f54dc9a4f0e03d61898c53.png)
下载完这个以后安装,这时环境变量也发生了变化。
![](https://img-blog.csdnimg.cn/be36abfdef5c45d884fa77270d275696.png)
然后必须再下载cuDNN,也要下载对应的版本
cuDNN Archive | NVIDIA Developer
https://developer.nvidia.com/rdp/cudnn-archive![](https://img-blog.csdnimg.cn/0ec69a2e44d147869f56df0d1dd1d8b1.png)
上图中红框的都可以。下载完成后解压缩,
![](https://img-blog.csdnimg.cn/822e3073c7974465a58c0e2639cf245a.png)
然后把这个目录的东西全部拷贝到
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8
目录会重合,这样就不用再配一次环境变量了。
装好这两个应用后,我们再到最上面下载pytorch gpu版本
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
之后写个代码testgpu.py看看gup是否有效
import torch
print(torch.cuda.is_available())
print(torch.cuda.get_device_name(0))
输出为true时,就表示可以用了
![](https://img-blog.csdnimg.cn/a25463da6f0740768f06e8f44db9570d.png)
运行gpu执行,因为我的1080ti只有11G,只能用--load-8bit
python -m fastchat.serve.cli --model-path ./vicuna-7b --load-8bit
如果有12G以上显存的 运行
python -m fastchat.serve.cli --model-path ./vicuna-7b
![](https://img-blog.csdnimg.cn/c12606e03c734b438e7cbcc0ed2b2245.png)
模型推理(Web UI方式)
如果想要以web UI方式提供服务,则需要配置3个部分。
web servers,用户的交互界面
model workers,托管模型
controller,用以协调web server和model worker
启动控制器
python -m fastchat.serve.controller --host 0.0.0.0
启动model worker,使用cpu
python -m fastchat.serve.model_worker --model-path ./vicuna-7b --model-name vicuna-7b --host 0.0.0.0 --device cpu --load-8bit
使用gpu
python -m fastchat.serve.model_worker --model-path ./vicuna-7b --model-name vicuna-7b --host 0.0.0.0 --load-8bit
等到该过程完成加载模型,您会看到“Uvicorn 正在运行......".模型工作者会将自身注册到控制器。
要确保您的模型工作人员已正确连接到控制器,请使用以下命令发送测试消息:
python -m fastchat.serve.test_message --model-name vicuna-7b
l输出如下就表示可以了![](https://img-blog.csdnimg.cn/76bc51cdbc674d4d92b781432b96528f.png)
然后启动一个web服务器
python -m fastchat.serve.gradio_web_server --port 8809
这时在浏览器上输入
http://localhost:8809
就可以使用了!好像用gpu可以支持多路用户并行访问!下图两个请求是同时发的
![](https://img-blog.csdnimg.cn/f56ef7f3a1814c409ec4f45b00abba02.png)
![](https://img-blog.csdnimg.cn/4ceded6d45e54b61b22de9ca7789525d.png)