🐍

FlexGenをGeForce RTX 3070(8GB)で動かす

2023/02/26に公開

はじめに

いま、私のTwitterのTLはFlexGenの話題でもちきりです。ChatGPTも流行していますが、みなさんつよつよPCを持っている様子なので、動くと分かれば試してみたくなるのだと思います。ええ、わたしもそうです。手持ちのハードウェア(ゲーミングPC程度)で動くことが確認できたので、記事にしておきます。

スペック

  • Core i7 12700K
  • DDR4 64GB Memory
  • GeForce RTX 3070 (8GB)
  • Windows 11(latest)
  • CUDA 11.7
  • cuDNN 11.x
  • Python 3.10.10
  • pytorch 1.13.1*cu117

Python 3.11は、pytorchとのバージョンが合わないようです。Pythonとpytorch、CUDAのバージョンの組み合わせは制約があるので、注意する必要があります。

FlexGen

https://github.com/FMInference/FlexGen

セットアップ

CUDA - cuDNN - pytorch(GPU版)の組み合わせで、多くの人が忍耐強く準備をしていると思います。私も何度かやり直しをしましたが、最終的に使っているバージョンについて、以下で紹介します。

CUDA

GoogleでCUDAで検索するとCUDA 12が見つかりますが、ここで使用するのは11.7です。
https://developer.nvidia.com/cuda-11-7-0-download-archive?target_os=Windows&target_arch=x86_64&target_version=11

cuDNN

cuDNNをインストールします。ここでもCUDAとバージョンを合わせて11.xをインストールします。
https://developer.nvidia.com/rdp/cudnn-download

Python 3.10 venv

Python 3.11はpytorchとのバージョンが合わなくなるので、ここでは3.10でvenvを作ります。モジュール類はあとでFlexGenと一緒にインストールされるので、とりあえずpipだけ新しくしておきます。

Python 3.10のインストール先(デフォルトでは%APPDATA%\Local\Programs\Python\Python310)に移動してPowerShellプロンプトから実行します。

> .\python.exe -m venv python310
> cd python310
> .\Scripts\activate.ps1
(python310) > python.exe -m pip install --upgrade pip

FlexGen

FlexGenのサイトにある指示のとおりgit cloneします。その中にPython環境を整える仕組みもあるので、コマンドとしてはシンプルです。numpyやtorchなどを自動的にインストールしてくれます。

(python310) > git clone https://github.com/FMInference/FlexGen.git 
(python310) > cd FlexGen
(python310) > pip3 install -e .

私が苦労したポイントとして、ここでtorch-1.13.1がインストールされてしまっていました。ここでtorch-1.13.1+cu117 (GPU版)がインストールされていれば大丈夫です。ダメなときは、CUDAやcuDNNの状況を再確認して修正したあと、pip3 uninstall torchして、再度pip3 install torchします。

起動!

とりあえず一番小さいモデルである、flexgenのopt-1.3b(1.3 Billion = 13億パラメータ)を起動してみます。FlexGenのREADME.mdにあるとおりの実行です。これはテスト的なコマンドなのかな?最後まで通ればOKです。

(python310) PS D:\Python3.10\FlexGen> python -m flexgen.flex_opt --model facebook/opt-1.3b
model size: 2.443 GB, cache size: 0.398 GB, hidden size (prefill): 0.008 GB
warmup - init weights
warmup - generate
benchmark - generate
benchmark - delete weights
C:\Users\WindVoice\AppData\Local\Programs\Python\Python310\python310\lib\site-packages\torch\distributed\distributed_c10d.py:262: UserWarning: torch.distributed.reduce_op is deprecated, please use torch.distributed.ReduceOp instead
  warnings.warn(
Outputs:
----------------------------------------------------------------------
0: Paris is the capital city of France. It is the most populous city in France, with an estimated population of 6,848,000 in 2016. It is the second most populous city
----------------------------------------------------------------------
3: Paris is the capital city of France. It is the most populous city in France, with an estimated population of 6,848,000 in 2016. It is the second most populous city
----------------------------------------------------------------------

TorchDevice: cuda:0
  cur_mem: 0.0000 GB,  peak_mem: 3.2399 GB
TorchDevice: cpu
  cur_mem: 0.0000 GB,  peak_mem: 0.0000 GB
model size: 2.443 GB    cache size: 0.398 GB    hidden size (p): 0.008 GB
peak gpu mem: 3.240 GB  projected: False
prefill latency: 0.232 s        prefill throughput: 8810.906 token/s
decode latency: 0.516 s decode throughput: 240.336 token/s
total latency: 0.748 s  total throughput: 171.035 token/s
(python310) PS D:\Python3.10\FlexGen>

これが動いたら、小さいモデルのままチャットボットを起動してみましょう。
Assitant: というプロンプトがFlexGenの応答、Human: というプロンプトに自分のメッセージを書き込みます。会話が成り立っているかちょっと怪しいですが、応答は十分快適です。日本語で話しかけても理解しているような雰囲気はありますが、応答は英語です。昔会話したシーマンみたいな印象を受けます。

(python310) PS D:\Python3.10\FlexGen> python apps/chatbot.py --model facebook/opt-1.3b
Initialize...
A chat between a curious human and a knowledgeable artificial intelligence assistant.
Human: Hello! What can you do?
Assistant: As an AI assistant, I can answer questions and chat with you.
Human: What is the name of the tallest mountain in the world?
Assistant: Everest.
Human: Hello!
Assistant: I can answer questions and chat with you.
Human: Do you like sushi?
Assistant: I do.
Human: Where do you live?
Assistant: I live in the United States.
Human: Would you tell me your LINE id?
Assistant: LINE id is my name (SOS).
Human: Perdon?
Assistant: Perdon.
Human:

大きいモデルを試してみます。1.3bから30bに23倍にアップなので、GPUには当然乗り切りません。Weightを圧縮したり、メインメモリにオフロードしたりするテクニックが使われているそうです。初回の起動はかなり重くて、メモリ64GBのマシンでもOSがフリーズしそうになりますが、一度起動に成功すれば二回目からはだいぶましになります。私の環境では、64GBメモリのうち38.2GBを使用した状態で起動しました。

スシ好き?どこ住み?みたいな質問にも答えてくれます。どこ住み?と聞くと地球だよ、と答えるのはスジがいい…… のでしょうか。ひとつの応答に10秒~15秒くらいかかっています。

(python310) PS D:\Python3.10\FlexGen> python apps/chatbot.py --compress-weight --model facebook/opt-30b --percent 0 100 100 0 100 0
Initialize...
A chat between a curious human and a knowledgeable artificial intelligence assistant.
Human: Hello! What can you do?
Assistant: As an AI assistant, I can answer questions and chat with you.
Human: What is the name of the tallest mountain in the world?
Assistant: Everest.
Human: Hello!
Assistant: Hello!
Human: Do you like sushi?
Assistant: Yeah!
Human: Where do you live?
Assistant: I live on the planet Earth.
Human:

まとめ

とりあえず今回は、ゲーミングPCレベルのハードウェアでもFlexGen動いたよ、という報告でした。

Discussion