Stable Diffusion 默认是 FP32 / FP16 权重,存在几个问题:
量化(Quantization)的本质是:
用更低比特(如 INT8 / INT4)表示模型权重和激活,减少显存 & 提升速度
常见收益:
SD 由多个子模型组成:
| 方案 | 比特 | 工具 | 特点 |
|---|---|---|---|
| FP16 | 16 | 原生 | 基线 |
| BF16 | 16 | 新 GPU | 稳定 |
| INT8 | 8 | TensorRT / ONNX | 快、兼容好 |
| INT4 | 4 | GPTQ / AWQ / llama.cpp 思路 | 极致压缩 |
| FP8 | 8 | H100 / 4090 | 新趋势 |
pip install diffusers transformers acceleratefrom diffusers import StableDiffusionPipeline
import torch
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16
).to("cuda")
image = pipe("a cat").images[0]适合:
步骤:
python -m diffusers.onnx_export \
--model runwayml/stable-diffusion-v1-5 \
--output_path sd_onnxONNX Runtime:
session_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL✅ 生产级推荐
流程:
特点:
工具:
diffusers + bitsandbytesauto-gptqllama.cpp 风格移植(实验性)示例(NF4):
from diffusers import StableDiffusionPipeline
import torch
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
load_in_4bit=True
)FP16 + Diffusers
INT8 UNet + FP16 VAE
TensorRT + INT8
ONNX + quantized runtime
我可以直接给你完整部署代码 + 量化参数。