見出し画像

HuggingFace Diffusers v0.25.0の新機能

「Diffusers v0.25.0」の新機能についてまとめました。

前回


1. Diffusers v0.25.0 のリリースノート

情報元となる「Diffusers 0.25.0」のリリースノートは、以下で参照できます。

2. aMUSEd

aMUSEd」 は、MUSE アーキテクチャに基づいた軽量のテキストから画像へのモデルです。一度に多くの画像を素早く生成するなど、軽量で高速なモデルを必要とするアプリで特に役立ちます。現在は研究リリースです。

「aMUSEd」は、多くの拡散モデルよりも少ない順方向パスでイメージを生成できるVQVAEトークンベースのTransformerです。「MUSE」とは対照的に、「T5-XXL」の代わりに小型のテキスト エンコーダ「CLIP-L/14」を使用します。「ammused」はパラメータ数が少なく、フォワードパス生成プロセスが少ないため、多くの画像を高速に生成できます。この利点は、特に大きなバッチサイズで見られます。

・Text-to-image generation

import torch
from diffusers import AmusedPipeline

pipe = AmusedPipeline.from_pretrained(
    "huggingface/amused-512", variant="fp16", torch_dtype=torch.float16
)
pipe.vqvae.to(torch.float32)  # vqvae is producing nans in fp16
pipe = pipe.to("cuda")

prompt = "cowboy"
image = pipe(prompt, generator=torch.manual_seed(8)).images[0]
image.save("text2image_512.png")

・Image-to-image generation

import torch
from diffusers import AmusedImg2ImgPipeline
from diffusers.utils import load_image

pipe = AmusedImg2ImgPipeline.from_pretrained(
    "huggingface/amused-512", variant="fp16", torch_dtype=torch.float16
)
pipe.vqvae.to(torch.float32)  # vqvae is producing nans in fp16
pipe = pipe.to("cuda")

prompt = "apple watercolor"
input_image = (
    load_image(
        "https://raw.githubusercontent.com/huggingface/amused/main/assets/image2image_256_orig.png"
    )
    .resize((512, 512))
    .convert("RGB")
)

image = pipe(prompt, input_image, strength=0.7, generator=torch.manual_seed(3)).images[0]
image.save("image2image_512.png")

・Inpainting

import torch
from diffusers import AmusedInpaintPipeline
from diffusers.utils import load_image
from PIL import Image

pipe = AmusedInpaintPipeline.from_pretrained(
    "huggingface/amused-512", variant="fp16", torch_dtype=torch.float16
)
pipe.vqvae.to(torch.float32)  # vqvae is producing nans in fp16
pipe = pipe.to("cuda")

prompt = "a man with glasses"
input_image = (
    load_image(
        "https://raw.githubusercontent.com/huggingface/amused/main/assets/inpainting_256_orig.png"
    )
    .resize((512, 512))
    .convert("RGB")
)
mask = (
    load_image(
        "https://raw.githubusercontent.com/huggingface/amused/main/assets/inpainting_256_mask.png"
    )
    .resize((512, 512))
    .convert("L")
)    

image = pipe(prompt, input_image, mask, generator=torch.manual_seed(3)).images[0]
image.save(f"inpainting_512.png")

・モデル

amused-256 : (603M パラメータ)
amused-512 : (608M パラメータ)

詳しくは、ドキュメントを参照。 

3. 3x faster SDXL

3x faster SDXL」は、Text-to-Imageの拡散モデルの推論レイテンシーを高速化するための一連の最適化手法です。これらはすべて、追加の C++ コードを必要とせずにネイティブPyTorchで実行できます。

これらのテクニックは「SDXL」(Stable Diffusion XL) 固有のものではなく、他のText-to-Imageの拡散モデルにも使用できます。
詳しくは、ドキュメント参照。

4. Interruptible pipelines

Interruptible pipelines」は、ユーザーが中間結果に満足できない場合に生成プロセスを停止できるため、diffusersで動作するUIを構築する場合に特に便利です。コールバックを使用してこれをパイプラインに組み込むことができます。

このコールバック関数は、引数「pipe」「i」「t」「callback_kwargs」を取る必要があります。特定のステップ数の後に拡散プロセスを停止するには、パイプラインの _interrupt 属性をTrueに設定します。コールバック内に独自のカスタム停止ロジックを自由に実装することもできます。

以下の例では、num_inference_stepsが50に設定されているにもかかわらず、拡散プロセスは10ステップ後に停止します。

from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe.enable_model_cpu_offload()
num_inference_steps = 50

def interrupt_callback(pipe, i, t, callback_kwargs):
    stop_idx = 10
    if i == stop_idx:
        pipe._interrupt = True

    return callback_kwargs

pipe(
    "A photo of a cat",
    num_inference_steps=num_inference_steps,
    callback_on_step_end=interrupt_callback,
)

詳しくは、ドキュメントを参照。

5. LoRA学習のサンプルにPEFT組み込み

公式サポートされているすべてのLoRA学習のサンプルにPEFTを組み込みました。これによりコードが大幅に簡素化され、可読性が向上しました。PEFTのおかげで、LoRA学習がさらに簡単になりました。

6. LCM LoRA SDXL学習のメモリーフレンドリー版

PEFTのベストプラクティスを取り入れて、SDXLのLCM LoRA学習をよりメモリーフレンドリーにしました。そのため、2 つのUNet (教師と生徒) を初期化する必要はなくなりました。 このバージョンには、迅速な実験のためにデータセットライブラリも統合されています。
詳しくはドキュメントを参照。

次回



この記事が気に入ったらサポートをしてみませんか?