【Diffusers】FLUX.1-devでIP-Adapterを使ってみる

はじめに

FLUX.1-devでIP-Adapterが使えるということでさっそく使ってみました。

自身の環境でどの方法が最適かを探るためにいろいろな方法で実行しています。

今回8bit量子化、4bit量子化は行っていません。

使用したPC

プロセッサ	Intel(R) Core(TM) i7-12700H
実装 RAM	32.0 GB
GPU		RTX 3080 Laptop (VRAM 16GB)
Python 3.12
CUDA 12.4

Pythonスクリプト

import torch
from diffusers import FluxPipeline
from diffusers.utils import load_image
from decorator import print_memory, gpu_monitor, time_monitor

@gpu_monitor(interval=0.5)
@time_monitor
def main():
    pipe = FluxPipeline.from_pretrained(
        "black-forest-labs/FLUX.1-dev",
        torch_dtype=torch.bfloat16
    )

    image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/flux_ip_adapter_input.jpg").resize((1024, 1024))

    pipe.load_ip_adapter(
        "XLabs-AI/flux-ip-adapter",
        weight_name="ip_adapter.safetensors",
        image_encoder_pretrained_model_name_or_path="openai/clip-vit-large-patch14",
    )
    pipe.set_ip_adapter_scale(1.0)

    pipe.enable_model_cpu_offload()
    #pipe.enable_sequential_cpu_offload()
    #pipe.vae.enable_tiling()

    image = pipe(
        width=1024,
        height=1024,
        prompt="wearing sunglasses",
        negative_prompt="",
        true_cfg_scale=4.0,
        generator=torch.Generator().manual_seed(4444),
        ip_adapter_image=image,
    ).images[0]

    image.save('flux_ip_adapter_output.jpg')

    print_memory()

if __name__ == "__main__":
    main()

結果

enable_model_cpu_offload()

pipe.enable_model_cpu_offload()
#pipe.enable_sequential_cpu_offload()
#pipe.vae.enable_tiling()
pipe.enable_model_cpu_offload()
#pipe.enable_sequential_cpu_offload()
pipe.vae.enable_tiling()

単独でもvae.enable_tiling()を併用してもVRAM使用量16GBを超えます。

enable_sequential_cpu_offload()

#pipe.enable_model_cpu_offload()
pipe.enable_sequential_cpu_offload()
#pipe.vae.enable_tiling()
max_memory=2.4 GB
max_reserved=2.4 GB
time: 767.63 sec
GPU 0 - Used memory: 2.93/16.00 GB
#pipe.enable_model_cpu_offload()
pipe.enable_sequential_cpu_offload()
pipe.vae.enable_tiling()
max_memory=2.4 GB
max_reserved=2.4 GB
time: 749.42 sec
GPU 0 - Used memory: 2.30/16.00 GB

vae.enable_tiling()を使っても使わなくても1枚の画像生成に12分程度かかっているので使いものになりません。

今後他の方法を模索してみようと思います。

今のところは量子化しか解決方法を見つけていません。

作成画像

左の画像から右の画像を作成しました。