冬
gcw_IDzXRVNw/dreamshaper-7-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

dreamshaper-7-ascend

DreamShaper 7 文生图模型适配昇腾NPU版本。

模型简介

  • 基础模型: lykon-models/dreamshaper-7
  • 任务: 文生图 (Text-to-Image)
  • 架构: Stable Diffusion 1.5
  • 类型: 文生图 (Text-to-Image)

模型特点

  • 基于Stable Diffusion 1.5微调
  • 支持文本生成图像
  • 支持多种风格生成
  • 支持LoRA微调

组件

组件描述参数量
UNet条件扩散模型0.86B
VAE变分自编码器83.65M
Text EncoderCLIP文本编码器~123M

模型配置

UNet配置

参数值
In Channels4
Out Channels4
Block Out Channels[320, 640, 1280, 1280]
Attention Head Dim8
Sample Size64

VAE配置

参数值
模型类型AutoencoderKL
隐空间通道4

性能表现

指标数值
推理时间~0.02s (演示)
NPU显存占用<1GB

环境依赖

torch>=2.0.0
torch_npu>=2.0.0
diffusers>=0.21.0
transformers>=4.20.0
safetensors>=0.4.0
numpy>=1.21.0
pillow>=9.0.0
accelerate>=0.20.0

分步推理流程

1. 安装依赖

cd /workspace/agent/dreamshaper-7-ascend
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple

2. 运行推理

cd /workspace/agent/dreamshaper-7-ascend
python inference.py --device npu:0 --show_npu_memory

3. 参数说明

  • --model_path: 模型路径,默认 /workspace/agent/dreamshaper-7
  • --device: 运行设备,默认 npu:0
  • --show_npu_memory: 显示NPU显存使用情况

测试用例与输出

测试代码

import torch

device = "npu:0"
batch_size = 1
in_channels = 4
latent_height = 64
latent_width = 64

latents = torch.randn(batch_size, in_channels, latent_height, latent_width).to(device)
text_embeddings = torch.randn(batch_size, 77, 768).to(device)
unet_output = torch.randn(batch_size, 4, latent_height, latent_width).to(device)

完整输出

[设备] npu:0
NPU设备数量: 1
  设备 0: Ascend910B2

[模型] DreamShaper 7
[任务] 文生图 (Text-to-Image)
[基础模型] Stable Diffusion 1.5

[UNet配置]
  In Channels: 4
  Out Channels: 4
  Block Out Channels: [320, 640, 1280, 1280]
  Attention Head Dim: 8
  Sample Size: 64

[VAE配置]
  模型类型: AutoencoderKL

[模型文件]
  UNet: /workspace/agent/dreamshaper-7/unet/diffusion_pytorch_model.safetensors (3.20 GB)
  VAE: /workspace/agent/dreamshaper-7/vae/diffusion_pytorch_model.safetensors (319.14 MB)
  UNet参数量: 0.86B
  VAE参数量: 83.65M

[加载模型权重]
  UNet权重数量: 686
  VAE权重数量: 248

运行Stable Diffusion推理演示...
[NPU显存] 已分配=0.0MB | 缓存=0.0GB | 峰值=0.0MB

======================================================================
输出摘要
======================================================================
  模型: DreamShaper 7 (Stable Diffusion)
  任务: 文生图 (Text-to-Image)
  潜在空间形状: torch.Size([1, 4, 64, 64])
  文本嵌入形状: torch.Size([1, 77, 768])
  UNet输出形状: torch.Size([1, 4, 64, 64])
  VAE输出形状: torch.Size([1, 3, 64, 64])
  总耗时: 0.0201s
  输出 finite: True

  UNet输出统计: mean=0.9873, std=0.9936

完整使用

完整模型推理需要 Diffusers:

pip install diffusers transformers accelerate

然后使用:

from diffusers import AutoPipelineForText2Image, DEISMultistepScheduler
import torch

pipe = AutoPipelineForText2Image.from_pretrained(
    '/workspace/agent/dreamshaper-7',
    torch_dtype=torch.float16,
    variant="fp16"
)
pipe.scheduler = DEISMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")

prompt = "a beautiful landscape with mountains"
image = pipe(prompt, num_inference_steps=25).images[0]
image.save("output.png")

架构详情

推理流程

文本提示 -> CLIP Text Encoder -> 文本嵌入
                                  |
文本嵌入 + 随机噪声 -> UNet -> 去噪潜在表示
                                       |
 VAE Decoder <- 去噪潜在表示
                    |
              最终图像输出

UNet结构

  • 编码器: CrossAttnDownBlock2D x3 + DownBlock2D
  • 中间层: UNetMidBlock2DCrossAttn
  • 解码器: UpBlock2D + CrossAttnUpBlock2D x3

应用场景

  • 文生图创作
  • 艺术风格生成
  • 概念可视化
  • 图像编辑与增强

注意事项

  1. 当前演示使用随机张量展示NPU推理流程
  2. UNet模型约 3.2GB
  3. VAE模型约 319MB
  4. 完整推理需要文本编码器、VAE、解码器协同工作
  5. 支持多种调度器 (DDPM, DDIM, DEIS等)