iTranslated by AI
Exploring Stable Diffusion (1) — Stable Diffusion 3
Purpose
I saw the keyword "Stable Diffusion 3" somewhere, so I decided to try running it.
Stable Diffusion 3
It seems to be based on the content of arXiv:2403.03206 Scaling Rectified Flow Transformers for High-Resolution Image Synthesis. I haven't read the paper yet.
In this work, we improve existing noise sampling techniques for training rectified flow models by biasing them towards perceptually relevant scales.
I think this part is the main highlight.
Setup
Authentication for Model Access and Issuing Access Tokens
First, you need to enable access to download the model stabilityai/stable-diffusion-3-medium-diffusers.
Once everything is set up and ready for download, the model will be downloaded to ~/.cache/huggingface/hub/models--stabilityai--stable-diffusion-3-medium-diffusers.
however,
$ du -h
...
15G .
the size is a bit large and might be a concern.
Now, after reading the content and understanding that it is for non-commercial use, you need to register your personal information for authentication. Once that's finished, issue an appropriate access token from [Settings] - [Access Tokens] in the user menu at the top right of the Hugging Face portal.
To persist the token on your disk:
$ pip install 'huggingface_hub[cli,torch]'
and then:
$ huggingface-cli login
Doing this will write the access token to ~/.cache/huggingface/token, and you should be ready.
Setting up Necessary Modules
In addition to a standard PyTorch environment, something like this seems to be necessary:
$ pip install -U diffusers transformers peft
- diffusers
$ pip list | grep diffusers
diffusers 0.29.0
Since it is v0.29.0: Stable Diffusion 3, you need version >= 0.29.0. For more details, you can refer to Add Stable Diffusion 3.
- transformers
$ pip list | grep transformers
transformers 4.41.2
Confirm that the version is > 4.37.2. c.f. data did not match any variant of untagged enum PyPreTokenizerTypeWrapper at line 960 column 3
- peft
Without peft,
Passing
scaleviajoint_attention_kwargswhen not using the PEFT backend is ineffective.
this message will be output repeatedly. c.f. how can we fix the message "Passing scale via joint_attention_kwargs when not using the PEFT backend is ineffective."
Let's try running it
Just like the tutorials you often see, you can generate an image with something like the following:
from __future__ import annotations
from diffusers import DiffusionPipeline
import torch
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-3-medium-diffusers",
text_encoder_3=None,
tokenizer_3=None,
torch_dtype=torch.float16,
)
pipeline.to("cuda")
prompt = "XXXXXXXXXXXXXXXXX"
image = pipeline(prompt, num_inference_steps=25).images[0]
display(image)
I'm not exactly sure about text_encoder_3=None and tokenizer_3=None, but without them, I encountered:
OutOfMemoryError: CUDA out of memory. Tried to allocate 28.00 MiB. GPU
c.f. How much VRAM ?
With the above settings, it seems to work even on a GTX 1080 (8 GB VRAM). Checking with nvidia-smi, the VRAM consumption fluctuated between about 6 to 12 GB, depending on the usage.
Output Sample
I generated an image that looks like an RPG hero using a simple prompt. By default, it seems to output at a size of 1024x1024. It's impressive that it can produce images of this size.

Summary
I don't really know much about it, but it seems like Stable Diffusion 3 is working.
Discussion
補足
DiffusionPipeline.from_pretrainedの実行でモデルのダウンロードが始まる。で始めると、
になる。
だが、実際、
なので符合する。