はじめに
Stable Diffusion 3がDiffusersから使えるようになったので、久しぶりに画像生成してみました。PC環境
使用したのはこちらのPCです。Windows 11 CUDA 11.8 (RTX 3080 Laptop VRAM 16GB) Python 3.12
Python環境構築
pip install torch==2.3.1+cu118 --index-url https://download.pytorch.org/whl/cu118 pip install diffusers[torch] pip install transformers peft sentencepiece protobuf
Pythonスクリプト
import time import torch from diffusers import StableDiffusion3Pipeline start = time.perf_counter() # model was downloaded from https://huggingface.co/stabilityai/stable-diffusion-3-medium-diffusers pipe = StableDiffusion3Pipeline.from_pretrained( "stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16 ) pipe.to("cuda") #pipe.enable_sequential_cpu_offload() #pipe.enable_model_cpu_offload() image = pipe( "A cat holding a sign that says hello world", negative_prompt="", num_inference_steps=28, guidance_scale=7.0, ).images[0] image.save("sd3_result.png") end = time.perf_counter() print(f"time: {(end - start):.2f}sec")
結果
1024x1024の画像が生成されました。![](https://cdn-ak.f.st-hatena.com/images/fotolife/t/touch-sp/20240613/20240613125338.jpg)
文字が正確(?)に反映されています。
真ん中の画像は「L」が一文字多いですが。
補足
ダウンロードしたファイルのサイズは14.4GBでした。使用するVRAMは16GBを超えていました。(画面出力も同じGPUを使っているので純粋な使用量ではありません)RTX 3080 LaptopのVRAM 16GBを超えているので一部システムメモリを使用します。そのため1枚の画像を生成するのにかなりの時間がかかってしまいました。「pipe.enable_model_cpu_offload()」や「pipe.enable_sequential_cpu_offload()」をつけるとVRAM使用量が16GB以下に抑えられました。もともとVRAM使用量削減を目的としたものですが、VRAM 16GBの環境では生成速度が速くなりました。参考までに1枚の画像を生成するのにかかった時間を示します。
画像生成時間
「pipe.to("cuda")」time: 1035.89sec
「pipe.enable_sequential_cpu_offload()」
time: 88.97sec
「pipe.enable_model_cpu_offload()」
time: 57.23sec
「pipe.enable_sequential_cpu_offload()」+「pipe.enable_model_cpu_offload()」
time: 44.79sec
![このエントリーをはてなブックマークに追加](https://b.st-hatena.com/images/v4/public/entry-button/button-only@2x.png)