本篇文章,我們聊聊如何使用 Docker 容器快速上手 Meta AI 出品的 LLaMA2 開源大模型。
寫在前面
昨天特別忙,早晨申請(qǐng)完 LLaMA2 模型下載權(quán)限后,直到晚上才顧上折騰了一個(gè) Docker 容器運(yùn)行方案,都沒來(lái)得及寫文章來(lái)聊聊這個(gè)容器怎么回事,以及怎么使用。
所以,現(xiàn)在就來(lái)聊聊如何快速上手 LLaMA2 官方版本的大模型。
完整的開源項(xiàng)目代碼,我上傳到了 soulteary/docker-llama2-chat,有需要的同學(xué)可以自取。
先來(lái)一起做下準(zhǔn)備工作吧。
準(zhǔn)備工作
準(zhǔn)備工作中,主要有兩步:準(zhǔn)備模型文件和模型運(yùn)行環(huán)境。
關(guān)于模型運(yùn)行環(huán)境,我們?cè)谥暗奈恼隆痘?Docker 的深度學(xué)習(xí)環(huán)境:入門篇》中聊過,就不贅述了,還不熟悉的同學(xué)可以閱讀參考。
只要你安裝好 Docker 環(huán)境,配置好能夠在 Docker 容器中調(diào)用顯卡的基礎(chǔ)環(huán)境,就可以進(jìn)行下一步啦。
模型下載
想要愉快的進(jìn)行 LLaMA2 的本地部署,我們首先需要申請(qǐng)模型文件的下載許可,目前一共有兩個(gè)地方可以申請(qǐng):Meta AI 官方網(wǎng)站和 Meta 在 HuggingFace 的模型頁(yè)面。
- https://ai.meta.com/llama/
- https://huggingface.co/meta-llama
不論選擇哪一種方式,在申請(qǐng)之后,稍等片刻,我們都將收到審核通過郵件,然后刷新頁(yè)面,就可以進(jìn)行模型下載啦。
HuggingFace 的模型下載速度比 Meta AI 官網(wǎng)快一些,所以我們側(cè)重聊如何從 HuggingFace 準(zhǔn)備我們所需要的模型。
從 HuggingFace 獲取模型文件
我們能從 HuggingFace 上獲取到的模型一共有兩種模型(原始基礎(chǔ)模型和 Chat 模型);以及在 HuggingFace 平臺(tái)上,它們分別被保存為了兩種格式:PyTorch Pickle 格式和 HuggingFace SafeTensors 格式。
如果你也使用 HuggingFace 全家桶,我強(qiáng)烈推薦后一種格式:面向未來(lái),方便可靠,加載性能也更強(qiáng)(快)。
為了方便演示和得到一個(gè)還湊合的體驗(yàn)效果,本文中,我們采用 “LLaMA2-Chat-HF” 版本的模型,你可以在下面的地址申請(qǐng)模型下載授權(quán):
- https://huggingface.co/llamaste/Llama-2-7b-chat-hf
- https://huggingface.co/llamaste/Llama-2-13b-hf
- https://huggingface.co/llamaste/Llama-2-70b-hf
其中 7B 和 13B 版本的模型,我們能夠在普通家用顯卡中跑起來(lái)(大概使用 10G~14G顯存)。
當(dāng)下載授權(quán)審核通過之后,我們可以使用下面的命令,來(lái)分別根據(jù)你的需求下載下面的三個(gè)模型:
# 本地需要按照 Git LFS,https://git-lfs.com
# 安裝完畢,進(jìn)行初始化
git lfs install
# 下載 7B 模型
git clone https://huggingface.co/meta-llama/Llama-2-7b-chat-hf
# 下載 13B 模型
git clone https://huggingface.co/meta-llama/Llama-2-13b-chat-hf
# 下載 70B 模型
git clone https://huggingface.co/meta-llama/Llama-2-70b-chat-hf
等待我們選擇的模型下載完后,調(diào)整下目錄結(jié)構(gòu):
# 創(chuàng)建一個(gè)新的目錄,用于存放我們的模型
mkdir meta-llama
# 將下載好的模型移動(dòng)到目錄中
mv Llama-2-7b-chat-hf meta-llama/
mv Llama-2-13b-chat-hf meta-llama/
mv Llama-2-70b-chat-hf meta-llama/
完整的目錄結(jié)構(gòu)類似下面這樣,所有的模型都在我們創(chuàng)建的 meta-llama
目錄的下一級(jí)中:
# tree -L 2 meta-llama
meta-llama
├── Llama-2-13b-chat-hf
│ ├── added_tokens.json
│ ├── config.json
│ ├── generation_config.json
│ ├── LICENSE.txt
│ ├── model-00001-of-00003.safetensors
│ ├── model-00002-of-00003.safetensors
│ ├── model-00003-of-00003.safetensors
│ ├── model.safetensors.index.json
│ ├── pytorch_model-00001-of-00003.bin
│ ├── pytorch_model-00002-of-00003.bin
│ ├── pytorch_model-00003-of-00003.bin
│ ├── pytorch_model.bin.index.json
│ ├── README.md
│ ├── Responsible-Use-Guide.pdf
│ ├── special_tokens_map.json
│ ├── tokenizer_config.json
│ ├── tokenizer.model
│ └── USE_POLICY.md
└── Llama-2-7b-chat-hf
├── added_tokens.json
├── config.json
├── generation_config.json
├── LICENSE.txt
├── model-00001-of-00002.safetensors
├── model-00002-of-00002.safetensors
├── model.safetensors.index.json
├── models--meta-llama--Llama-2-7b-chat-hf
├── pytorch_model-00001-of-00003.bin
├── pytorch_model-00002-of-00003.bin
├── pytorch_model-00003-of-00003.bin
├── pytorch_model.bin.index.json
├── README.md
├── special_tokens_map.json
├── tokenizer_config.json
├── tokenizer.json
├── tokenizer.model
└── USE_POLICY.md
上面的內(nèi)容都準(zhǔn)備好后,我們開始準(zhǔn)備運(yùn)行模型。
啟動(dòng)模型應(yīng)用程序
使用下面的命令,下載 Docker LLaMA2 Chat 模型應(yīng)用程序文件:
git clone https://github.com/soulteary/docker-llama2-chat.git
等待程序下載完畢后,我們進(jìn)入程序目錄,然后開始構(gòu)建我們所需要的模型容器鏡像:
# 進(jìn)入程序目錄
cd docker-llama2-chat
# 構(gòu)建 7B 鏡像
bash scripts/make-7b.sh
# 或者,構(gòu)建 13B 鏡像
bash scripts/make-13b.sh
耐心等待鏡像構(gòu)建完畢后,我們將之前準(zhǔn)備好的存放著模型的 meta-llama
目錄,移動(dòng)到當(dāng)前程序目錄中,然后選擇要啟動(dòng)的模型程序即可:
# 運(yùn)行 7B 鏡像,應(yīng)用程序
bash scripts/run-7b.sh
# 或者,運(yùn)行 13B 鏡像,應(yīng)用程序
bash scripts/run-13b.sh
命令執(zhí)行后,如果一切順利,你將看到類似下面的日志:
=============
== PyTorch ==
=============
NVIDIA Release 23.06 (build 63009835)
PyTorch Version 2.1.0a0+4136153
Container image Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Copyright (c) 2014-2023 Facebook Inc.
Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)
Copyright (c) 2012-2014 Deepmind Technologies (Koray Kavukcuoglu)
Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu)
Copyright (c) 2011-2013 NYU (Clement Farabet)
Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston)
Copyright (c) 2006 Idiap Research Institute (Samy Bengio)
Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz)
Copyright (c) 2015 Google Inc.
Copyright (c) 2015 Yangqing Jia
Copyright (c) 2013-2016 The Caffe contributors
All rights reserved.
Various files include modifications (c) NVIDIA CORPORATION & AFFILIATES. All rights reserved.
This container image and its contents are governed by the NVIDIA Deep Learning Container License.
By pulling and using the container, you accept the terms and conditions of this license:
https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license
WARNING: CUDA Minor Version Compatibility mode ENABLED.
Using driver version 525.105.17 which has support for CUDA 12.0. This container
was built with CUDA 12.1 and will be run in Minor Version Compatibility mode.
CUDA Forward Compatibility is preferred over Minor Version Compatibility for use
with this container but was unavailable:
[[Forward compatibility was attempted on non supported HW (CUDA_ERROR_COMPAT_NOT_SUPPORTED_ON_DEVICE) cuInit()=804]]
See https://docs.nvidia.com/deploy/cuda-compatibility/ for details.
Loading checkpoint shards: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:05<00:00, 2.52s/it]
Caching examples at: '/app/gradio_cached_examples/20'
Caching example 1/5
/usr/local/lib/python3.10/dist-packages/transformers/generation/utils.py:1270: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use a generation configuration file (see https://huggingface.co/docs/transformers/main_classes/text_generation )
warnings.warn(
Caching example 2/5
Caching example 3/5
Caching example 4/5
Caching example 5/5
Caching complete
/usr/local/lib/python3.10/dist-packages/gradio/utils.py:839: UserWarning: Expected 7 arguments for function <function generate at 0x7f3e096a1000>, received 6.
warnings.warn(
/usr/local/lib/python3.10/dist-packages/gradio/utils.py:843: UserWarning: Expected at least 7 arguments for function <function generate at 0x7f3e096a1000>, received 6.
warnings.warn(
Running on local URL: http://0.0.0.0:7860
To create a public link, set `share=True` in `launch()`.
接著,我們使用瀏覽器打開 http://localhost:7860
或者 http://你的IP:7860
就能夠開始體驗(yàn) LLaMA2 Chat 模型啦。
顯存使用情況
7B 模型實(shí)際運(yùn)行起來(lái),會(huì)占用 13 G 左右顯存。
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.105.17 Driver Version: 525.105.17 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... Off | 00000000:01:00.0 On | Off |
| 31% 42C P8 34W / 450W | 14158MiB / 24564MiB | 2% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1414 G /usr/lib/xorg/Xorg 103MiB |
| 0 N/A N/A 1593 G /usr/bin/gnome-shell 16MiB |
| 0 N/A N/A 2772 C python 14034MiB |
+-----------------------------------------------------------------------------+
13B 模型運(yùn)行起來(lái),消耗顯存 9G 左右。
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.105.17 Driver Version: 525.105.17 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... Off | 00000000:01:00.0 Off | Off |
| 31% 44C P2 70W / 450W | 9057MiB / 24564MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1414 G /usr/lib/xorg/Xorg 167MiB |
| 0 N/A N/A 1593 G /usr/bin/gnome-shell 16MiB |
| 0 N/A N/A 4686 C python 8870MiB |
+-----------------------------------------------------------------------------+
好了,如果你只想了解如何使用模型,那么到這里就足夠啦。
模型鏡像的封裝
接下來(lái),我們來(lái)簡(jiǎn)單展開下上面的腳本中,都做了哪些工作。
封裝這個(gè) LLaMA2 Docker 鏡像其實(shí)特別簡(jiǎn)單,和之前的模型相關(guān)的文章沒有差別,我們基于 Nvidia 基礎(chǔ)鏡像,做一個(gè)簡(jiǎn)單的多階段構(gòu)建即可。
比如,我們可以先定義一個(gè)基礎(chǔ)鏡像,包含所有的模型程序運(yùn)行需要的依賴文件:
ROM nvcr.io/nvidia/pytorch:23.06-py3
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install accelerate==0.21.0 bitsandbytes==0.40.2 gradio==3.37.0 protobuf==3.20.3 scipy==1.11.1 sentencepiece==0.1.99 transformers==4.31.0
WORKDIR /app
然后,我們將上面的內(nèi)容保存為 Dockerfile.base
,接著使用 docker build -t soulteary/llama2:base . -f docker/Dockerfile.base
進(jìn)行基礎(chǔ)鏡像的構(gòu)建。
接著,我們準(zhǔn)備好模型調(diào)用文件即可,相關(guān)的程序我上傳到了 soulteary/docker-llama2-chat/llama2-7b 和 soulteary/docker-llama2-chat/llama2-13b,主要有兩個(gè)文件構(gòu)成,分別是 Gradio Web 交互界面,和模型加載運(yùn)行程序。
編寫模型應(yīng)用程序鏡像文件:
FROM soulteary/llama2:base
COPY llama2-7b/* ./
CMD ["python", "app.py"]
將上面的文件保存為 Dockerfile.7b
后,我買使用命令 docker build -t soulteary/llama2:7b . -f docker/Dockerfile.7b
完成應(yīng)用鏡像的構(gòu)建。
最后,使用下面的命令,將程序跑起來(lái)就可以玩啦:
docker run --gpus all --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 --rm -it -v `pwd`/meta-llama:/app/meta-llama -p 7860:7860 soulteary/llama2:7b
最后
本篇文章是 LLaMA2 相關(guān)的第一篇文章,短短幾個(gè)月的時(shí)間,開源項(xiàng)目就能夠取得這么快速的進(jìn)步,還是挺令人欣慰和令人期待的。
LLaMA2 不是結(jié)束,而是新一輪的開始,開源的世界里,我們永遠(yuǎn)可以期待更強(qiáng)的家伙出現(xiàn),不斷逼進(jìn)和挑戰(zhàn)當(dāng)前世界的王。
這篇文章的標(biāo)題里隱藏著下一篇文章的線索,你猜得到嗎?
–EOF
本文使用「署名 4.0 國(guó)際 (CC BY 4.0)」許可協(xié)議,歡迎轉(zhuǎn)載、或重新修改使用,但需要注明來(lái)源。 署名 4.0 國(guó)際 (CC BY 4.0)
本文作者: 蘇洋文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-599971.html
創(chuàng)建時(shí)間: 2023年07月21日
統(tǒng)計(jì)字?jǐn)?shù): 10092字
閱讀時(shí)間: 21分鐘閱讀
本文鏈接: https://soulteary.com/2023/07/21/use-docker-to-quickly-get-started-with-the-official-version-of-llama2-open-source-large-model.html文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-599971.html
到了這里,關(guān)于使用 Docker 快速上手官方版 LLaMA2 開源大模型的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!