【Llama2】をdocker 環境で簡単に動かす方法

2023年7月23日2023年8月2日

今回の記事は、Llama2を手軽に試してみたい方向けです!
llama.cppを使用して、簡易的なChatbotのWEBサーバーを作成します。
GPUなしVerは、こちらを参考にしてください!

目次

Llama2とは

Llama2は、米Meta社が7月18日にオープンソースとして公開した最新の大規模言語モデルです。このモデルには、通常版とChat用にチューニングされたものがあり、それぞれパラメーター数が70億、130億、700億(7b, 13b, 70b)に対応しています。この記事では、Llama-2-13b-chatを使用しています。

以下は公開された6種類のLlama2モデルです:

  • Llama-2-7b
  • Llama-2-7b-chat
  • Llama-2-13b
  • Llama-2-13b-chat
  • Llama-2-70b
  • Llama-2-70b-chat

動作環境

  • メモリ(RAM)16GB以上
  • windows
  • Docker Desktop
  • WSL2
  • NVIDIA GeForce GTX 1660 SUPER
  • CUDA 12.2.0

準備

Docker Desktopのインストール

docker をインストールしていない場合は、インストールを行ってください。
こちらを参考にしながらインストールしてください。

git をインストール

git をインストールしていない場合は、インストールを行ってください。
こちらを参考にしながらインストールしてください。

NVIDIAドライバインストール

公式ページを使用して、ご自身の使用している製品に適したドライバーをダウンロードし、インストールまで行ってください。

CUDA Toolkitのインストール

公式ページのコマンドをWSL内で実行してください。

下記のコマンドを実行し、「 CUDA Version: 12.2 」が表示されていれば、インストールは完了です。

nvidia-smi

llama.cppリポジトリをクローン

今回は、llama.cppを使用させていただきます。

下記のgit コマンドを実行してリポジトリをクローンします。

git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp/

docker-compose.ymlファイルの追加

llama.cpp/docker-compose.ymlに配置します。

touch docker-compose.yml

docker-compose.yml

version: '3.9' services: llama-cpp: build: context: . dockerfile: .devops/main-cuda.Dockerfile args: - CUDA_VERSION=12.2.0 # 自分の環境のバージョンに変更する container_name: llama-cpp ports: - 2600:2600 deploy: resources: reservations: devices: - capabilities: [gpu] tty: true command: -m models/llama-2-13b-chat.ggmlv3.q2_K.bin -ngl 30 --port 2600 --host 0.0.0.0 volumes: - ./models:/models environment: - GGML_CUDA_NO_PINNED=1

Dockerfile の修正

llama.cpp/.devops/main-cuda.Dockerfile

修正内容は、下記になります。

FROM ${BASE_CUDA_RUN_CONTAINER} as runtime - COPY --from=build /app/main /main + COPY --from=build /app/server /server - ENTRYPOINT [ "/main" ] + ENTRYPOINT [ "/server" ]

修正後のDockerfile

ARG UBUNTU_VERSION=22.04 # This needs to generally match the container host's environment. ARG CUDA_VERSION=11.7.1 # Target the CUDA build image ARG BASE_CUDA_DEV_CONTAINER=nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION} # Target the CUDA runtime image ARG BASE_CUDA_RUN_CONTAINER=nvidia/cuda:${CUDA_VERSION}-runtime-ubuntu${UBUNTU_VERSION} FROM ${BASE_CUDA_DEV_CONTAINER} as build # Unless otherwise specified, we make a fat build. ARG CUDA_DOCKER_ARCH=all RUN apt-get update && \ apt-get install -y build-essential WORKDIR /app COPY . . # Set nvcc architecture ENV CUDA_DOCKER_ARCH=${CUDA_DOCKER_ARCH} # Enable cuBLAS ENV LLAMA_CUBLAS=1 RUN make FROM ${BASE_CUDA_RUN_CONTAINER} as runtime COPY --from=build /app/server /server ENTRYPOINT [ "/server" ]

modelファイルのダウンロード

llama.cppを使用する時は、変換されたモデルを使用する必要があります。
そのため今回は、Llama-2-13B-chat-GGMLのモデルを使用させていただきます。
(GGMLファイルは、llama.cppとこのフォーマットをサポートするライブラリやUIを使用したCPU + GPU推論用です)

cd models/ wget https://huggingface.co/TheBloke/Llama-2-13B-chat-GGML/resolve/main/llama-2-13b-chat.ggmlv3.q2_K.bin cd ..

Llama2を動かしてみる

docker コンテナの起動

※初回時は build を行うため、時間がかかります。
自分の場合は、5~10分ほどかかりました。 2回目以降に実行する場合は、--build オプションは不要です。

docker compose up -d --build

command: -m models/llama-2-7b-chat.ggmlv3.q2_K.bin --port 2600 --host 0.0.0.0
※コンテナ起動時に実行されるコマンドは、https://github.com/ggerganov/llama.cpp/tree/master/examples/server を確認して自分の環境にあったものに修正します。

ローカル環境へアクセス

http://localhost:2600/ へアクセスし、下記の画面が表示されたら、構築完了です。

実際に使用してみた

英語で質問

llama: user: Hello! I'm looking for some advice on how to learn Spanish. Do you have any tips? User: What are five things you are good at about Llama2? llama: Well, I can do many things, but here are five things that I am particularly good at: 1. Conversational language learning: I can help you practice your Spanish conversations and provide feedback on your pronunciation and grammar. 2. Vocabulary building: I can assist you in expanding your vocabulary by providing definitions and examples of words and phrases in context. 3. Cultural insights: As a South American native, I can offer unique perspectives on the culture and customs of Spanish-speaking countries. 4. Pronunciation practice: I can help you improve your pronunciation by modeling correct sounds and intonation patterns. 5. Interactive exercises: I can engage with you through interactive exercises like role-playing games or dialogue simulations to make learning more fun and effective. What would you like to focus on today?

日本語は、あまり得意じゃなさそうですね。(たまに日本語で返信することもあります。)

llama: Hello! How can I help you today? User: こんにちは llama: Konnichiwa! (Hello!) What's your question or topic for our discussion today? User: Llama2について、日本語で教えてください。 llama: Ah, you want to learn about LLAMA in Japanese! Sure thing! So, 「LLAMA」is short for "Linguine with Lime and Miso Amenities." It's a popular dish that originated from Japan, and it's known for its unique flavor combination of lime and miso. Now, let me give you a brief overview of how to order LLAMA in Japanese. First, you would say "Llamasan wa ikimasu" which means "I'd like the LLAMA set, please." Then, if you want to add more toppings, you can say "O-naka de tsukete kudasai" which means "Can I have some extra toppings, please?" For example, if you want to add more cheese, you can say "Mozuku o kudasai" which means "I'd like some mozzarella cheese, please." And finally, when you receive your order, you would say "Gomen nasai" which means "Thank you!" So there you have it! With these phrases, you should be able to order LLAMA in Japanese with confidence. Do you have any other questions about this topic?

まとめ

今回実際にLlama2のモデルを使用して、Chatbotを作成してみましたが、パフォーマンスはChatGPTがやはり優勢であると感じました。
しかし、オープンソースで商用利用可能な点が魅力であり、社内での検証や勉強のために活用することができるでしょう。
Llamaの今後のアップデートも期待しています。ぜひ皆さんもお試しください!