2025年4月23日水曜日

SVD generative-models を M2 Mac にインストールする

SVD generative-models を M2 Mac にインストールする

概要

ただし generative-models が提供しているサンプルコードは CUDA 環境必須なので M2 では動作しません
このページではインストールする方法を紹介します

環境

  • macOS 15.4.1 (M2 Pro Mac mini)
  • Python 3.10.12
  • rust 1.86.0

リポジトリクローン

リポジトリを取得環境を構築します

  • git clone https://github.com/Stability-AI/generative-models.git
  • cd generative-models
  • pyenv local 3.10.12
  • python -m venv .pt2
  • source .pt2/bin/activate

パッケージインストール

そのままだとインストールできないので修正しインストールします

vim requirements/pt2.txt

black==23.7.0
chardet==5.1.0
clip @ git+https://github.com/openai/CLIP.git
einops>=0.6.1
fairscale>=0.4.13
fire>=0.5.0
fsspec>=2023.6.0
invisible-watermark>=0.2.0
kornia==0.6.9
matplotlib>=3.7.2
natsort>=8.4.0
ninja>=1.11.1
numpy>=1.24.4
omegaconf>=2.3.0
open-clip-torch>=2.20.0
opencv-python==4.6.0.66
pandas>=2.0.3
pillow>=9.5.0
pudb>=2022.1.3
pytorch-lightning==2.0.1
pyyaml>=6.0.1
rembg
scipy>=1.10.1
streamlit>=0.73.1
tensorboardx==2.6
timm>=0.9.2
tokenizers==0.11.6
torch==2.0.1
torchaudio>=2.0.2
torchdata==0.6.1
torchmetrics>=1.0.1
torchvision>=0.15.2
tqdm>=4.65.0
transformers==4.19.1
urllib3<1.27,>=1.25.4
wandb>=0.15.6
webdataset>=0.2.33
wheel>=0.41.0
--extra-index-url https://download.pytorch.org/whl/cu118
xformers==0.0.20
gradio
streamlit-keyup==0.2.0
numpy<2.0

内部的に rust もコンパイルで使うのでインストールします
また pytorch だけ先にインストールしないと xformers がインストールできないのでインストールしておきます

  • brew install rust
  • pip install torch==2.0.1
  • pip install -r requirements/pt2.txt

sgm のインストール

おそらく sgm (Stability-AI/generative-models) の略だと思います
これで generative-models が python で使えるようになります
一応学習することもできるので学習データもダウンロードしておきます

  • pip install .
  • pip install -e git+https://github.com/Stability-AI/datapipelines.git@main#egg=sdata

サンプルコード実行

どちらも AssertionError: Torch not compiled with CUDA enabled で動作はしませんが generative-models では主に 2 通りのサンプルコードの実行方法があるので紹介します

gradio を使う

先に https://huggingface.co/stabilityai/stable-video-diffusion-img2vid-xt-1-1/tree/main のページに移動しログインし利用規約に同意しましょう

  • huggingface-cli login
  • pip install onnxruntime
  • python -m scripts.demo.gradio_app

これでサンプルを動作させる用の UI が起動します
gradio の方法だと自動的に svd のモデルも huggingface からダウンロードしてくれるので先にログインや同意を済ませておく必要があります

streamlit を使う

https://huggingface.co/stabilityai/stable-video-diffusion-img2vid/blob/main/svd.safetensors をダウンロードし checkpoints ディレクトリに配置します

  • mkdir checkpoints
  • ls -1 checkpoints
svd.safetensors
  • streamlit run scripts/demo/video_sampling.py --server.address 0.0.0.0 --server.port 7862

これでサンプル用の UI が起動します
ただ Load Model をクリックすると先程同様 CUDA のエラーとなり動作はしません

最後に

generative-models を Mac 環境にインストールする方法を紹介しました
残念ながらまだ Mac では動作しないようです
モデル自体は huggingface で公開されているので別のクライアントを使えば動作するかもしれません

次回は stable-diffusion-webui-forge を試してみます

参考サイト

0 件のコメント:

コメントを投稿