Open SoraをGoogle Colabで動かして高速動画生成

さて、おうちで動画生成は難しいという結論になりました。
Open Soraを使っておうちのWindowsで動画生成する - きしだのHatena

ということで、GPUサーバー借りてやるのがいいのではと、Google Colabで試してみました。それなりに動画が生成できたので、末尾にいろいろ載せてます。

無償で使えるT4は16GBのGPUでOpen Soraは動かないので、課金が必要です。というか、24GBのL4でもout of memoryだったので、40GBのA100が必要です。

ガチャがんばろう。

最初にネタバレですが、セットアップだけで5コンピューティングユニットくらい消費します。まずはT4などを使って、起動してout of memoryが出るところまで手順を確認したあとで、A100を使うほうがいいです。

インストール

CUDAが12.2なので、12.1にダウングレードする必要があります。 NVIDIAのサイトのスクリプトだと「最新は12.5やで」みたいなメッセージを出して12.5をインストールしてしまうので、cudaのバージョンに12.1.0-1を指定するのと、途中でキーボードレイアウトを聞いてくるのでnoninteractiveをつけるのとが必要です。

!wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
!sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
!wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda-repo-ubuntu2204-12-1-local_12.1.0-530.30.02-1_amd64.deb
!sudo dpkg -i cuda-repo-ubuntu2204-12-1-local_12.1.0-530.30.02-1_amd64.deb
!sudo cp /var/cuda-repo-ubuntu2204-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
!sudo apt-get update
!sudo DEBIAN_FRONTEND=noninteractive apt-get -y install cuda=12.1.0-1

リンクの貼り直し

!export PATH="/usr/local/cuda-12.1/bin:$PATH"
!export LD_LIBRARY_PATH="/usr/local/cuda-12.1/lib64:$LD_LIBRARY_PATH"

!unlink /usr/local/cuda
!ln -s /usr/local/cuda-12.1 /usr/local/cuda

Open Soraをclone

!git clone https://github.com/hpcaitech/Open-Sora

カレントディレクトリを移動

cd Open-Sora

依存をインストールします。

!pip install -r requirements/requirements-cu121.txt
!pip install -v .

なんかこんなの出るけどキャンセルします。

apexをインストール。ここで17分くらいかかります。

!pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" git+https://github.com/NVIDIA/apex.git

flash_attnも必要

!pip install flash-attn --no-build-isolation

ngrokの準備

Gradioを使うので、別ブラウザからアクセスできるようにngrokでトンネルつくる必要があります。
アカウントを作りましょう。
https://ngrok.com/

authtokenを確認します。

abcdefのところを、そのauthtokenに置き換えて実行。

!wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz
!sudo tar -xvzf ngrok-v3-stable-linux-amd64.tgz -C /usr/local/bin
!ngrok config add-authtoken abcdef

起動

gradioと同時にngrokを起動します。

!python gradio/app.py & ngrok http http://localhost:7860

モデルをダウンロードしてるとディスクがギリギリになるので警告が出ますが無視しましょう。

起動すると、local URLが出ます。

もちろんそのURLではアクセスできないので、ngrok経由でアクセス。Endpointsを見に行くとアクセスのためのURLが載っています。

アクセスすると信用できるところだけにアクセスしようと警告が出ますが、自分で立てたサーバーなのでそのままVisit Site

でました。

消費リソース

動画を生成すると、25GB以上のGPUメモリを使います。

ステップあたりの時間と消費メモリはこんな感じ。480pなら16秒まで、720pの4秒までならなんとか大丈夫。生成時間は動画時間に比例していますね。

2s 4s 8s 16s
144p 0.13s / 22.7GB 0.26s / 22.7GB 0.53s / 22.7GB 1.15s / 23.3GB
240p 0.35s / 23.3GB 0.7s / 22.5GB 1.45s / 24.2GB 3.19s 26.1GB
360p 1.16s / 25.2GB 1.72s / 25.6GB 3.7s / 26.4GB 8.13s / 31.5GB
480p 1.53s / 23.4GB 3.15s / 27.7GB 6.95s / 30.1GB 15.5s / 38.7GB
720p 3.8s / 26.5GB 8.09s / 36.8GB out of memory out of memory

基本的には1-2分で生成が終わるので、いろいろ試しやすくなりました。
プロンプトを試すときには360pで10stepsくらいにして、ちゃんと生成するときに480pで50stepsにするという感じになると思います。ただ、解像度を変えると生成動画も変わるので、480pにしたときに10stepsで試してseedを探るというのも必要かもしれません。

結果

Garallyにあるプロンプトを480p 4秒で試すと、結構ちゃんと出ました。

a close-up shot of a woman standing in a dimly lit room. she is wearing a traditional chinese outfit, which includes a red and gold dress with intricate designs and a matching headpiece. the woman has her hair styled in an updo, adorned with a gold accessory. her makeup is done in a way that accentuates her features, with red lipstick and dark eyeshadow. she is looking directly at the camera with a neutral expression. the room has a rustic feel, with wooden beams and a stone wall visible in the background. the lighting in the room is soft and warm, creating a contrast with the woman's vibrant attire. there are no texts or other objects in the video. the style of the video is a portrait, focusing on the woman and her attire.

dancingのような動きを指定したプロンプトがダメっぽい。これは「japanese maiko is dancing」で奇跡的にうまくいったやつ。それでも、袖はぐちょぐちょしてますね。

「japanese maiko. she is wearing a pink kimono」にすると、安定してます。ちょっと動く静止画を生成するくらいがよさそう。

猫もおとなしく座ってれば大丈夫。というか猫はほぼ静止画ですね。

brown striped cat is sitting in shibuya street. the city is illuminated by neon light. few people is walking in the background.the camera pans slowly through the scene

海の画像をちょっと波立たせるとかはかなりいいです。プロンプトは「sea is waving. cloud is flowing in the sky」

元画像はこれです。

ということで、特性を把握して、ちょっと背景を動かすという感じであれば、結構使えそうですね。