LoginSignup
0
0

ローカル環境で大規模言語モデルAPIサーバを動作させる TGI編

Last updated at Posted at 2024-01-16

はじめに

NVIDIA GPU が乗っている Windows ゲーミング PC にて、大規模言語モデルを使う API サーバを用意する手順をざっと紹介します。
今回は HuggingFace Text Generation Interface (TGI) を使ってみました。

前提条件

手順

手順は次のようになります。

  • TGI コンテナの起動
  • curl を使ったローカルでの動作確認
  • ネットワーク経由のアクセス設定と確認

TGI コンテナの起動

TGI は ghcr.io/huggingface/text-generation-inference にコンテナイメージが提供されています。

こちらのサンプルでは、TheBloke/TinyLlama-1.1B-Chat-v1.0-GPTQ をモデルとして指定しています。ファイルサイズが 768MB 程度と非常にコンパクトになります。

TheBloke/TinyLlama-1.1B-Chat-v1.0-GPTQ 起動後のnvidia-smi実行結果は次のようになりました。モデルサイズはコンパクトなのに、VRAM を 6626MiB も使っていることが確認できます。

$  nvidia-smi
Sat Jan  6 15:22:36 2024
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 530.54                 Driver Version: 531.97       CUDA Version: 12.1     |
|-----------------------------------------+----------------------+----------------------+
| 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 RTX 4060 L...    On | 00000000:01:00.0 Off |                  N/A |
| N/A   35C    P8                3W /  N/A|   6626MiB /  8188MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/A  N/A       203      C   /python3.10                               N/A      |
+---------------------------------------------------------------------------------------+

70 億パラメータで 3.9GB のモデルサイズの TheBloke/Llama-2-7B-Chat-GPTQ の起動を試したのですが、VRAM 8GB だと起動できないようです。エラーメッセージは次のようになりました。

 ERROR warmup{max_input_length=1024 max_prefill_tokens=4096 max_total_tokens=2048}:warmup: text_generation_client: router/client/src/lib.rs:33: Server error: CUDA out of memory. Tried to allocate 5.54 GiB. GPU 0 has a total capacty of 8.00 GiB of which 2.43 GiB is free. Including non-PyTorch memory, this process has 17179869184.00 GiB memory in use. Of the allocated memory 3.67 GiB is allocated by PyTorch, and 37.52 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

curl を使ったローカルでの動作確認

まずは ubuntu にて curl を実行し動作を確認しました。ローカルホストでアクセスしています。

$ curl 127.0.0.1:8080/generate     -X POST     -d '{"inputs":"What is Deep Learning?","parameters":{"max_new_tokens":20}}'     -H 'Content-Type: application/json'

上記コマンドの実行結果は次のようになりました。

{"generated_text":"\nDeep Learning is a type of machine learning that uses deep neural networks to learn complex patterns from"}

ネットワーク経由のアクセス設定と確認

次のステップとして、別マシンにてネットワーク経由でアクセスしてみましょう。具体的なステップは、こちらの記事のネットワーク経由のアクセス設定と確認を参考にして下さい。

注意事項

TGI のサポートしているモデル

TGI がサポートしているモデルのリストがこちらにあります。Quantization (量子化)は GPTQ と AWQ などをサポートしている、とあります。こちらに詳しいガイドがあります。主に GPTQ についての記載となります。

所感

完全に個人の見解ですが位置付けを考えてみました。いくつかのモデルを使って起動を試しましたが、VRAM 8GB の環境では起動できるモデルが限られることがよく分かりました。ノート PC で簡単に動作させて利用するのは少々難しそうです。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0