Mini-GPT4というマルチモーダルLLMをDockerを使ってバッチ処理をしてみました。係数を部分的にDockerイメージに組み込んだり、使用するLLMを切り替えられるようにちょっと工夫をしてみました。
目次
Mini-GPT4というマルチモーダルなLLMがあります。これは対話型に実行するデモが公開されているのですが、実際はバッチ処理したいことが多いです。これをDockerを使って行っていきます
https://github.com/Vision-CAIR/MiniGPT-4
以下のようなディレクトリ構成にします。
+ data
+ input
+ output
+ docker
+ app
- batch.py
+ build_src
- convert_yaml.py
- init_image_encoders.py
- dockder-compose.yaml
- Dockerfile
- download_weights.py
リポジトリは以下にあります。
https://github.com/koshian2/Mini-GPT4-Batch/
これはバッチ処理の推論で読み書きされるデータです。Dockerイメージにマウントさせます。
Dockerイメージに関するフォルダです。
カレントディレクトリを、このリポジトリ内のdockerフォルダとします。DockerはWSL2で動かしています(docker-compose.yamlのローカルパスの指定が若干他と変わるのでご注意ください)
また、LLMの係数の保存は「G:/LLMs」フォルダに保存するものとします。環境によって適宜読み替えてください
docker-compose build
LLMの係数だけは別途ローカルからダウンロードします。「download_weights.py」を開きます。以下のコードになっていますが、「local_root_dir」を書き換えると保存先を変えられます
from huggingface_hub import snapshot_download
def main():
local_root_dir = "G://LLMs/"
snapshot_download(repo_id="Vision-CAIR/vicuna", local_dir=local_root_dir+"Vision-CAIR/vicuna", local_dir_use_symlinks=False)
snapshot_download(repo_id="Vision-CAIR/vicuna-7b", local_dir=local_root_dir+"Vision-CAIR/vicuna-7b", local_dir_use_symlinks=False)
# snapshot_download(repo_id="meta-llama/Llama-2-7b-chat", local_dir=local_root_dir+"meta-llama/Llama-2-7b-chat", local_dir_use_symlinks=False)
if __name__ == "__main__":
main()
LLaMA-2だけHuggingfaceのリポジトリへの認証が必要なので、認証が終わっていないとエラーになると思います。ここではVicuna-7Bで動かすことを想定します。
docker-compose up
これはVicuna-7Bを読み込む場合です。LLMの切り替えや設定値の切り替えはdocker-compose.yamlをいじります。
version: '1'
services:
minigpt4:
build: .
image: minigpt4
deploy:
resources:
reservations:
devices:
- capabilities: [gpu]
volumes:
- /mnt/g/LLMs/Vision-CAIR/vicuna-7b:/llm_weights/vicuna
- ./app:/MiniGPT-4/app
- ../data:/MiniGPT-4/data
tty: true
environment:
- EVAL_CONFIG=eval_configs/minigpt4_vicuna_7b_eval.yaml
ports:
- 30485:7860
今は共通して「Discribe this image」というプロンプトですが、app/batch.pyのprompt変数をいじってください。
(ここを環境変数で管理すればよかったね…)
以下のようなInputディレクトリに対し、
以下のように返ってきました。
設定ファイルはMiniGPT4のデフォルトを使いました。量子化ビットやトークン数、ビームサーチのパラメーターを変えればもっとパフォーマンス・チューニングできると思います。Vicuna-7B、デフォルトの設定でGPU使用量は11~12GBぐらいでした(量子化効いている)
このシュールな画像に対し「Discribe this image」とすると、
The image shows a person wearing a Star Wars stormtrooper outfit standing on a street corner, holding a sandwich in one hand and a lightsaber in the other. The person is standing on a street corner with buildings in the background. The image is taken from a low angle, looking up at the person from the street. The person is wearing a black and white stormtrooper suit with a helmet, a white belt, and a long white cape. The person is holding a sandwich in one hand and a lightsaber in the other. The lightsaber has a blue blade and a silver hilt. The image is taken from a low angle, looking up at the person from the street. The person is standing on a street corner with buildings in the background. The person is wearing a black and white stormtrooper suit with a helmet, a white belt, and a long white cape. The person is holding a sandwich in one hand and a lightsaber in the other. The lightsaber has a blue blade and a silver hilt.
という結果に。ストームトルーパーが何なのかはわかってるらしい。