Segmind SD-Small 與 SD-Tiny 知識蒸餾發佈

TL;DR

Segmind 發佈了 SD‑SmallSD‑Tiny 的訓練代碼與預訓練權重,這兩款擴散模型透過移除模組(block-removal)的知識蒸餾技術進行壓縮;它們在保持與基礎模型相當的圖像保真度的同時,減少了 35%–55% 的參數,並實現了高達 100% 的推理加速。


知識蒸餾方法論

發佈的模型是使用 On Architectural Compression of Text‑to‑Image Diffusion Models (Shinkook et al.) 中描述的 Block‑Removal Knowledge Distillation 技術訓練而成。

  • 教師模型 (Teacher model):Realistic‑Vision 4.0,一個高品質的 Stable Diffusion 權重。
  • 學生架構 (Student architectures):移除層後的 UNet 變體,參數分別減少了 35% (SD‑Small) 與 55% (SD‑Tiny)。
  • 損失函數組成 (Loss composition)
    1. 目標圖像與生成圖像的潛在表示(latent representations)之間的標準擴散損失。
    2. 將學生生成的潛在表示與教師生成的潛在表示對齊的潛在層損失(Latent-level loss)。
    3. 匹配教師與學生之間每個 UNet 模組輸出的特徵層損失(Feature-level loss)(這是最關鍵的組成部分)。
  • 訓練數據:篩選過圖像評分 > 7.5 的 LAION Art Aesthetic 數據集。
  • 訓練時程:100k 步(SD‑Small)與 125k 步(SD‑Tiny),共使用 1M 張圖像。

完整的蒸餾流程可在 [segmind/distill-sd](https://github.com/segmind/distill-sd) 儲存庫中找到,預訓練權重則託管在 Hugging Face 的 segmind 命名空間下。


使用 🤗 Diffusers 使用模型

這兩款模型都可以直接透過 🤗 Diffusers 函式庫中的 DiffusionPipeline 來載入:

from diffusers import DiffusionPipeline
import torch

pipeline = DiffusionPipeline.from_pretrained(
    "segmind/small-sd", torch_dtype=torch.float16
)
prompt = "Portrait of a pretty girl"
negative_prompt = (
    "(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, "
    "cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, "
    "worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, "
    "mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn "
    "face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, "
    "extra limbs, cloned face, disfigured, gross proportions, malformed limbs, "
    "missing arms, missing legs, extra arms, extra legs, fused fingers, too many "
    "fingers, long neck"
)
image = pipeline(prompt, negative_prompt=negative_prompt).images[0]
image.save("my_image.png")

只需更換模型識別碼,相同的 API 即可用於 segmind/tiny-sd


推理速度提升

在相同硬體上的基準測試顯示,與原始基礎權重相比,蒸餾模型的延遲降低了高達 2 倍。用於這些測量的推理腳本已包含在儲存庫中 (inference.py)。


已知限制

  • 這些模型屬於早期發佈版本;視覺品質可能尚無法與生產級擴散模型媲美。
  • 它們並未針對通用生成進行優化,在處理複雜的構圖提示詞或多重概念時可能會遇到困難。
  • 最佳實踐是在特定領域數據上進行微調(fine-tune)或應用 LoRA 以獲得更高的保真度。

在人像數據集上微調 SD‑Tiny

Segmind 展示了在由 Realistic‑Vision 4.0 生成的 7k 張人像圖像集上對 SD‑Tiny 進行微調的成果。訓練超參數如下:

  • 步數 (Steps): 131,000
  • 學習率 (Learning rate): 1e-4
  • 批次大小 (Batch size): 32 (梯度累積 gradient accumulation = 4)
  • 圖像解析度 (Image resolution): 768 px
  • 混合精度 (Mixed-precision): fp16

生成的樣本在保持 55% 參數減少的同時,品質接近原始教師模型。


在蒸餾模型上進行 LoRA 訓練

由於模型尺寸縮小,將低秩適配 (LoRA) 應用於 SD‑Tiny 可以獲得更快的 LoRA 收斂速度。儲存庫中提供了在抽象概念上訓練的 LoRA 權重範例 (lora_training.py)。


社群邀請

Segmind 鼓勵開發者為此專案做出貢獻、回報問題並分享微調後的權重。溝通管道包括 Discord 伺服器和 GitHub 儲存庫,我們歡迎您的 Star 與 Pull Request。

Sources