[ PROMPT_NODE_22774 ]
Multimodal Stable Diffusion 故障排查
[ SKILL_DOCUMENTATION ]
# Stable Diffusion 故障排除指南
## 安装问题
### 包冲突
**错误**: `ImportError: cannot import name 'cached_download' from 'huggingface_hub'`
**修复**:
bash
# 更新 huggingface_hub
pip install --upgrade huggingface_hub
# 重新安装 diffusers
pip install --upgrade diffusers
### xFormers 安装失败
**错误**: `RuntimeError: CUDA error: no kernel image is available for execution`
**修复**:
bash
# 检查 CUDA 版本
nvcc --version
# 安装匹配的 xformers
pip install xformers --index-url https://download.pytorch.org/whl/cu121 # 针对 CUDA 12.1
# 或从源码构建
pip install -v -U git+https://github.com/facebookresearch/xformers.git@main#egg=xformers
### Torch/CUDA 不匹配
**错误**: `RuntimeError: CUDA error: CUBLAS_STATUS_NOT_INITIALIZED`
**修复**:
bash
# 检查版本
python -c "import torch; print(torch.__version__, torch.cuda.is_available())"
# 使用正确的 CUDA 重新安装 PyTorch
pip uninstall torch torchvision
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
## 内存问题
### CUDA 显存溢出
**错误**: `torch.cuda.OutOfMemoryError: CUDA out of memory`
**解决方案**:
python
# 方案 1: 启用 CPU 卸载
pipe.enable_model_cpu_offload()
# 方案 2: 顺序 CPU 卸载(更激进)
pipe.enable_sequential_cpu_offload()
# 方案 3: 注意力切片
pipe.enable_attention_slicing()
# 方案 4: 大图 VAE 切片
pipe.enable_vae_slicing()
# 方案 5: 使用更低精度
pipe = DiffusionPipeline.from_pretrained(
"model-id",
torch_dtype=torch.float16 # 或 torch.bfloat16
)
# 方案 6: 减小批次大小
image = pipe(prompt, num_images_per_prompt=1).images[0]
# 方案 7: 生成更小的图像
image = pipe(prompt, height=512, width=512).images[0]
# 方案 8: 在生成之间清理缓存
import gc
torch.cuda.empty_cache()
gc.collect()
### 内存随时间增长
**问题**: 内存占用随每次生成而增加
**修复**:
python
import gc
import torch
def generate_with_cleanup(pipe, prompt, **kwargs):
try:
image = pipe(prompt, **kwargs).images[0]
return image
finally:
# 生成后清理缓存
if torch.cuda.is_available():
torch.cuda.empty_cache()
gc.collect()
### 大模型加载失败
**错误**: `RuntimeError: Unable to load model weights`
**修复**:
python
# 使用低 CPU 内存模式
pipe = Diffusion