ホーム 暮らし New Features in HuggingFace Diffusers v0.3.0

New Features in HuggingFace Diffusers v0.3.0

八木 麻衣 八木 麻衣 • 更新: • 公開: • 読了約 14 分
目次

I have summarized the new features in "Diffusers v0.3.0".

1. Diffusers v0.3.0 Release Notes

nd2585209acea

The release notes for "Diffusers 0.3.0", which serve as the source of information, can be viewed below.

2. New Documentation

We have created a documentation page for "Diffusers" that includes guides, links, and an API reference.

3. New API

The output of "Pipeline", "Model", and "Scheduler" has been replaced from "sample" to "images".

The same applies to the following.

Additionally, breaking changes have been made to the following methods.

◎ VQModel.encode
Returns a dict/dataclass instead of a single tensor. It is highly likely that it will return multiple tensors in the future.
Please change latents = model.encode(...) to latents = model.encode(...)[0] or latents = model.encode(...).latents.

◎ VQModel.decode
Returns a dict/dataclass instead of a single tensor. It is highly likely that it will return multiple tensors in the future.
Please change sample = model.decode(...) to sample = model.decode(...)[0] or sample = model.decode(...).sample.

◎ VQModel.forward
Returns a dict/dataclass instead of a single tensor. It is highly likely that it will return multiple tensors in the future.
Please change sample = model(...) to sample = model(...)[0] or sample = model(...).sample.

◎ AutoencoderKL.encode
Returns a dict/dataclass instead of a single tensor. It is highly likely that it will return multiple tensors in the future.
Please change latent_dist = model.encode(...) to latent_dist = model.encode(...)[0] or latent_dist = model.encode(...).latent_dist.

◎ AutoencoderKL.decode
Returns a dict/dataclass instead of a single tensor. It is highly likely that it will become necessary to return multiple tensors in the future.
Please change sample = model.decode(...) to sample = model.decode(...)[0] or sample = model.decode(...).sample.

◎ AutoencoderKL.forward
Returns a dict/dataclass instead of a single tensor. It is highly likely that it will become necessary to return multiple tensors in the future.
Please change sample = model(...) to sample = model(...)[0] or sample = model(...).sample.

4. New StableDiffusion Pipeline

A new StableDiffusion pipeline has been added.

For more details, please refer to the new API reference.

Colab samples are as follows:

5. Usage on machines with low GPU memory

Current Diffusion models run with significantly less VRAM, at the cost of 10% speed, thanks to the optimizations described in basujindal/stable-diffusion#117. To use the attention optimization, simply enable it after loading the pipeline with enable_attention_slicing().

from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4", 
    revision="fp16", 
    torch_dtype=torch.float16,
    use_auth_token=True
).to("cuda")
pipe.enable_attention_slicing()

This will allow more users to use "Stable Diffusion" on their own computers.

7. Textual Inversion

"Textual Inversion" allows you to personalize "Stable Diffusion" models with your own images using only 3 to 5 images.

8. Apple Silicon MPS backend

"Diffusers" is compatible with Apple Silicon for "Stable Diffusion" inference using the PyTorch mps device. You need to install PyTorch Preview (Nightly) on a Mac with an M1 or M2 CPU and use the pipeline as usual.

from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=True)
pipe = pipe.to("mps")

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]

While we see a significant speedup (31 seconds versus 214 seconds on an M1 Max), there are still some limitations. For more details, please refer to the documentation.

9. Experimental ONNX exporter and pipeline for Stable Diffusion

We are introducing a new (and experimental) Stable Diffusion pipeline compatible with the ONNX runtime, which allows you to run "Stable Diffusion" on any hardware that supports ONNX (including significant speedups on CPU).

You must use "StableDiffusionOnnxPipeline" instead of "StableDiffusionPipeline". You also need to download the weights from the onnx branch of the repository and specify the runtime provider to use (CPU in the following example).

from diffusers import StableDiffusionOnnxPipeline

pipe = StableDiffusionOnnxPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    revision="onnx",
    provider="CPUExecutionProvider",
    use_auth_token=True,
)

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]

[Note] The script above takes time to download external ONNX weights, so it is faster to convert the checkpoints yourself. To convert your own checkpoints, run the conversion script locally.

python scripts/convert_stable_diffusion_checkpoint_to_onnx.py --model_path="CompVis/stable-diffusion-v1-4" --output_path="./stable_diffusion_onnx"

After that, you can load them from a local path.

pipe = StableDiffusionOnnxPipeline.from_pretrained("./stable_diffusion_onnx", provider="CPUExecutionProvider")

Next time



同じカテゴリの記事

八木 麻衣
トレンドリサーチャー 八木 麻衣
プロフィールを見る →

全記事の事実確認を担当。一次情報・公式発表・公開資料の三点を確認できない場合は公開を見送る判断をします。

おすすめの関連記事