Diffusers 0.3 版加入影像到影像、文字反轉、修補、GPU 最佳化、Mac MPS、ONNX 支援與新文件

TL;DR

Diffusers 0.3 帶來影像到影像生成、文字反轉、實驗性修補、針對一般 GPU 的記憶體效能推論、原生支援 Mac M1/M2、ONNX 匯出管線,以及全面的文件大幅改版,顯著降低使用 Stable Diffusion 的門檻,並激發社群工具的熱潮。


影像到影像管線

全新的 StableDiffusionImg2ImgPipeline 讓使用者提供初始影像與文字提示,以產生轉換後的影像。API 與文字到影像管線相同,新增 init_imagestrength 參數,用以控制保留原始影像的程度。範例用法:

from diffusers import StableDiffusionImg2ImgPipeline
import torch

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

init_image = preprocess(your_image)
prompt = "A fantasy landscape, trending on artstation"
images = pipe(
    prompt=prompt,
    init_image=init_image,
    strength=0.75,
    guidance_scale=7.5,
    generator=torch.Generator().manual_seed(42),
)["sample"]

也提供了一個即時可執行的 Space 示範,方便快速實驗。


文字反轉

文字反轉允許僅使用 3‑5 張個人影像就能建立新概念。使用者訓練一個 token 嵌入,之後可在提示中呼叫,且產生的概念可透過 sd-concepts-library 中心分享。工作流程包括:

  • Navigator Colab – 瀏覽超過 150 個社群產生的概念。
  • Training Colab – 在自訂影像集上微調新 token。
  • Inference Colab – 使用已學習的 token 產生影像。

在發佈後的幾天內,社群貢獻了超過 200 個概念。


實驗性修補管線

StableDiffusionInpaintPipeline 接受影像、二元遮罩與提示,以取代遮罩區域同時保留周圍情境。範例程式碼:

from diffusers import StableDiffusionInpaintPipeline

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

images = pipe(
    prompt=["a cat sitting on a bench"] * 3,
    init_image=init_image,
    mask_image=mask_image,
    strength=0.75,
    guidance_scale=7.5,
).images

此功能標記為實驗性,表示仍在持續改進中。


針對較小 GPU 的最佳化

0.3 版大幅降低 VRAM 使用量;Stable Diffusion 現在只需約 3.2 GB 記憶體,且僅有約 10 % 的速度損失。關鍵設定是 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,
)
pipe = pipe.to("cuda")
pipe.enable_attention_slicing()

此最佳化擴大了消費級 GPU 使用擴散模型的可及性。


在 macOS (M1/M2) 上使用 PyTorch MPS 的 Diffusers

透過 PyTorch mps 裝置加入了對 Apple Silicon 的原生支援。使用者只需少量程式碼變更,即可在 M1/M2 Mac 上執行 Stable Diffusion:

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]

官方文件提供效能基準與設定說明。


實驗性 ONNX 匯出器與管線

基於 ONNX 的管線 (StableDiffusionOnnxPipeline) 讓任何支援 ONNX 的硬體(包括 CPU)都能進行推論。使用範例:

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]

同時提供了轉換腳本 (convert_stable_diffusion_checkpoint_to_onnx.py) 以匯出自訂 checkpoint。


新文件發佈 (v0.3.0)

專門的文件衝刺產出了首個完整版本的文件網站,涵蓋:

  • Optimization techniques – FP16、attention slicing 與 MPS 指南。
  • Training overview – 微調擴散模型的高階步驟概覽。
  • Contributing guide – 如何提交修補與擴充。
  • API reference – 調度器與管線的詳細頁面。

文件託管於 https://huggingface.co/docs/diffusers/v0.3.0/en/,歡迎社群貢獻。


社群亮點

此發佈激發了一波基於 Diffusers 的社群專案:

  • Stable Diffusion Videos – 用於潛在空間插值與提示變形的工具,提供 pip 套件與 Colab 筆記本。
  • Diffusers Interpret – 可視化擴散步驟與 token 級別歸因的可解釋性套件。
  • Japanese Stable Diffusion – 以 1億 張日文標註影像訓練的模型,捕捉文化細節。
  • Waifu Diffusion – 為高品質動漫風格生成微調的 checkpoint。
  • Cross‑Attention Control – 允許使用者編輯注意力圖以修改提示影響、替換物件或注入風格的倉庫。
  • Reusable Seeds – 示範如何將一次生成的種子重複使用於另一代,以實現受控變化的筆記本。

參與方式

Diffusers 倉庫仍為開源;鼓勵使用者為 GitHub 專案加星、加入 Hugging Face Discord,並提交 issue 或 pull request。持續的社群貢獻對於此函式庫的快速演進至關重要。

Sources