Quantumleap
783 words
4 minutes
M1/M2でStable Diffusionを動かす方法について

Stable Diffusion#

今更すぎるが、登場時はまだまだだなあと思っていた画像生成 AI が随分なレベルにまで上がってきたので触れておくことにしました。

基本的に SD は RTX シリーズで動かすのが盤石なのですが、M1 シリーズもそれなりに GPU コアがあり、一説によると M1 Ultra は RTX2060 程度の性能はあるらしいので試してみることにしました。

まずはじめに#

M1/M2 Stable Diffusion で検索すると CoreML を利用したツールを紹介されることがありますが、CoreML には画像サイズを変更できなかったり、やたらと生成した画像のクオリティが低かったりと問題があったのでおすすめしません。

Stable Diffusion を簡単に利用したいのであれば WebUI 一択で、他の選択肢はありません。

Stable Diffusion WebUI#

Stable Diffusion WebUIはここから入手できます。M1/M2 での導入方法もここに書いてあるので、基本的にはこれをそのまま実行するだけで良いです。

パフォーマンス向上#

How to improve performance on M1 / M2 Macsでパフォーマンス改善が載っていますが、まあ若干上がったかなという感じです。

以下のようにwebui-user.shを書き換えます。

#!/bin/bash
#########################################################
# Uncomment and change the variables below to your need:#
#########################################################

# Install directory without trailing slash
#install_dir="/home/$(whoami)"

# Name of the subdirectory
#clone_dir="stable-diffusion-webui"

# Commandline arguments for webui.py, for example: export COMMANDLINE_ARGS="--medvram --opt-split-attention"
export COMMANDLINE_ARGS="--skip-torch-cuda-test --upcast-sampling --no-half-vae"

# python3 executable
#python_cmd="python3"

# git executable
#export GIT="git"

# python3 venv without trailing slash (defaults to ${install_dir}/${clone_dir}/venv)
#venv_dir="venv"

# script to launch to start the app
#export LAUNCH_SCRIPT="launch.py"

# install command for torch
# export TORCH_COMMAND="pip install torch==1.12.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113"
export TORCH_COMMAND="pip install --pre torch==2.0.0.dev20230506 torchvision==0.15.0.dev20230506 -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html

# Requirements file to use for stable-diffusion-webui
#export REQS_FILE="requirements_versions.txt"

# Fixed git repos
#export K_DIFFUSION_PACKAGE=""
#export GFPGAN_PACKAGE=""

# Fixed git commits
#export STABLE_DIFFUSION_COMMIT_HASH=""
#export TAMING_TRANSFORMERS_COMMIT_HASH=""
#export CODEFORMER_COMMIT_HASH=""
#export BLIP_COMMIT_HASH=""

# Uncomment to enable accelerated launch
#export ACCELERATE="True"

# Uncomment to disable TCMalloc
#export NO_TCMALLOC="True"

###########################################

Torch は 20230507 のものもリリースされているのですが、それではwebui.shが起動しなくなりました。

なのでとりあえず一つ前の 20230506 を利用しています。

パフォーマンス#

環境#

  • M1 Ultra(48Core GPU)
  • Stable Diffusion WebUI(1.1.1 5ab7f213bec2f816f9c5644becb32eb72c8ffb89)
  • Python 3.10.11
  • torch 2.0.0
  • xformers N/A
  • gradio 3.28.1
  • checkpoint cbfba64e66

設定#

パラメータ
Sampling method-
Sampling steps22
Width512
Height768
Batch count1
Batch size1
CFG Scale7
Seed1031604376
ScriptNone

メソッドには色々あるので、それを変えてみて出力画像を確認してみます。

プロンプト#

以下の呪文を使います。

ポジティブ#

masterpiece, (best quality), (ultra-detailed:1.4), high resolution, original characters, depth of field, solo focus, wearing black glasses, a high school student girl, cute adorable look, small breasts, soft delicate beautiful attractive face with expressive purple eyes, cowgirl position, disheveled clothing, close up, semi long straight hair, dark red brown, dynamic pose, cute adorable look, wearing a school uniform with blue tie, short sleeve, nsfw, bob cut hair, embarrassed, cowgirl position, in the nostalgic library

ネガティブ#

EasyNegative (worst quality low quality:1.4) text, bad_anatomy, bad_hands

結果#

シードが同じため殆どの場合でほぼ同じ画像が生成されましたが、サンプリングメソッドの違いによって大雑把に三パターンの画像に分かれました。

メソッド生成時間パターン
Euler a12.62sA
Euler12.53sB
LMS12.48s不明瞭
Heun23.71sB
DPM223.69sB
DPM2 a23.76s不明瞭
DPM++ 2S a23.65sB+
DPM++ 2M12.53sB
DPM++ SDE25.63sA
DPM fast12.47s不明瞭
DPM adaptive102.49sA
LMS Karras12.47sB
DPM2 Karras23.73sB
DPM2 a Karras23.65sC
DPM++ 2S a karras23.81sB+
DPM++ 2M karras12.52sA
DPM++ SDE Karras25.52sA
DDIM16.35sB
PLMS12.24s不明瞭
UniPC15.26s不明瞭
M1/M2でStable Diffusionを動かす方法について
https://fuwari.vercel.app/posts/2023/05/stablediffusion/
Author
tkgling
Published at
2023-05-08