Token导航 LogoToken导航TokenDH.com
待分类需要联网github未标认证来源可访问许可证需确认审计提醒

diffusers-ascend-pipeline扩散器上升管道

Agent Skill

diffusers-ascend-pipeline 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要围绕仓库状态、代码变更或协作事项进行整理时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

428

周安装

18

GitHub Stars

60

下载量

150
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:diffusers-ascend-pipeline(扩散器上升管道)
来源仓库:https://github.com/ascend-ai-coding/awesome-ascend-skills
仓库路径:skills/diffusers-ascend-pipeline
安装命令:
npx skills add https://github.com/ascend-ai-coding/awesome-ascend-skills --skill diffusers-ascend-pipeline
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/ascend-ai-coding/awesome-ascend-skills --skill diffusers-ascend-pipeline

简介

diffusers-ascend-pipeline 支持在昇腾 NPU 上运行 Diffusers Pipeline 推理任务。

  • 适用于图像或视频生成场景,兼容任意 HuggingFace Diffusers 模型格式。
  • 包含预检脚本与环境验证模块,确保 NPU 内存与模型权重资源可用。
  • 使用前须先完成环境配置(diffusers-ascend-env-setup)并准备合法模型权重文件。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

Diffusers 昇腾 NPU Pipeline 推理

本 Skill 指导用户在华为昇腾 NPU 上运行 HuggingFace Diffusers Pipeline 推理,适用于任意 Diffusers 模型。

使用顺序

按下面顺序使用本 Skill:

  1. 先做预检,确认环境、NPU 和权重可用。
  2. 再执行图像或视频 Pipeline 推理。
  3. 需要时再启用内存优化、LoRA 或多卡推理。
  4. 查询 API 时,按版本先定位 GitHub API 路径,再打开官网文档。

前置要求

依赖说明参考
CANN + torch_npuNPU 运行环境diffusers-ascend-env-setup
模型权重真实权重或假权重diffusers-ascend-weight-prep
diffuserspip install diffusers["torch"] transformers accelerate

快速开始

1. 预检

运行预检脚本,确认环境、NPU 内存和模型权重就绪:

python scripts/validate_pipeline.py --model ./my_model --device npu:0 --min-memory 16

检查项:

检查项说明
Python 包torch, torch_npu, diffusers, transformers
CANN 环境ASCEND_HOME_PATH 等环境变量
NPU 可用性设备数量和名称
NPU 内存空闲内存是否满足最低要求
模型权重model_index.json 存在,组件完整

2. 图像模型推理

通用模式(适用于 FLUX、SDXL、SD3 等任何图像 Pipeline):

import torch
import torch_npu

from diffusers import DiffusionPipeline

# 加载 Pipeline(自动识别 Pipeline 类型)
pipe = DiffusionPipeline.from_pretrained(
    "./my_model",
    torch_dtype=torch.bfloat16,
)
pipe = pipe.to("npu:0")

# 生成图像
generator = torch.Generator("npu").manual_seed(42)
image = pipe(
    prompt="a cat sitting on a windowsill, watercolor style",
    num_inference_steps=20,
    guidance_scale=3.5,
    generator=generator,
).images[0]

image.save("output.png")

使用脚本:

# FLUX.1-dev 推理
python scripts/run_pipeline.py \
    --model ./fake_flux_dev \
    --prompt "a cat sitting on a windowsill" \
    --device npu:0 --dtype bfloat16 \
    --steps 20 --seed 42 \
    --output flux_output.png --benchmark

# SDXL 推理
python scripts/run_pipeline.py \
    --model stabilityai/stable-diffusion-xl-base-1.0 \
    --prompt "a beautiful landscape" \
    --device npu:0 --dtype float16 \
    --steps 30 --guidance-scale 7.5 \
    --output sdxl_output.png --benchmark

3. 视频模型推理

视频 Pipeline(Wan、CogVideoX 等)输出帧序列,脚本自动检测并导出为 MP4:

import torch
import torch_npu

from diffusers import DiffusionPipeline
from diffusers.utils import export_to_video

pipe = DiffusionPipeline.from_pretrained(
    "./wan_model",
    torch_dtype=torch.bfloat16,
)
pipe = pipe.to("npu:0")

generator = torch.Generator("npu").manual_seed(42)
output = pipe(
    prompt="a dog running on the beach",
    num_inference_steps=30,
    generator=generator,
)

# 导出视频
export_to_video(output.frames[0], "output.mp4", fps=16)

使用脚本:

python scripts/run_pipeline.py \
    --model ./wan_model \
    --prompt "a dog running on the beach" \
    --device npu:0 --dtype bfloat16 \
    --steps 30 --seed 42 \
    --output wan_output.mp4 --benchmark

4. 内存优化

大模型推理时可能遇到 NPU 内存不足,可组合使用以下优化技术:

技术方法NPU 兼容内存节省
Attention Slicingpipe.enable_attention_slicing()✅ 验证通过中等
VAE Slicingpipe.enable_vae_slicing()✅ 验证通过
VAE Tilingpipe.enable_vae_tiling()✅ 验证通过高(高分辨率)
BF16 推理torch_dtype=torch.bfloat16✅ 推荐约 50%
Sequential CPU Offloadpipe.enable_sequential_model_cpu_offload()⚠️ 实验性很高
Model CPU Offloadpipe.enable_model_cpu_offload()⚠️ 实验性
# 使用内存优化运行
python scripts/run_pipeline.py \
    --model ./fake_flux_dev \
    --prompt "a landscape" \
    --device npu:0 --dtype bfloat16 \
    --steps 20 \
    --attention-slicing --vae-tiling \
    --output optimized.png --benchmark

详细指南:references/memory-optimization.md

5. LoRA 集成

加载 LoRA 适配器增强生成效果:

import torch
import torch_npu

from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("./my_model", torch_dtype=torch.bfloat16)
pipe = pipe.to("npu:0")

# 加载 LoRA
pipe.load_lora_weights("./my_lora", adapter_name="style")

# 生成(控制 LoRA 强度)
image = pipe(
    prompt="a portrait in oil painting style",
    num_inference_steps=20,
    cross_attention_kwargs={"scale": 0.8},
).images[0]

多 LoRA 叠加:

pipe.load_lora_weights("./lora_style", adapter_name="style")
pipe.load_lora_weights("./lora_detail", adapter_name="detail")
pipe.set_adapters(["style", "detail"], adapter_weights=[0.7, 0.3])

使用脚本:

python scripts/run_pipeline.py \
    --model ./my_model \
    --prompt "a portrait" \
    --device npu:0 --dtype bfloat16 \
    --lora ./my_lora --lora-scale 0.8 \
    --output lora_output.png

详细指南:references/lora-guide.md

6. 多卡并行推理(Context Parallel)

diffusers>=0.36.0 开始,可参考 Parallel API 在多卡上做上下文并行(Context Parallel),核心入口为:

  • ContextParallelConfig
  • ParallelConfig
  • apply_context_parallel
Ascend NPU 需使用 torch.distributed + hccl 后端启动多进程。
import os

import torch
import torch.distributed as dist
import torch_npu

from diffusers import ContextParallelConfig, DiffusionPipeline

def setup_dist():
    if not dist.is_initialized():
        dist.init_process_group(backend="hccl")
    local_rank = int(os.environ["LOCAL_RANK"])
    world_size = dist.get_world_size()
    torch.npu.set_device(local_rank)
    return local_rank, world_size

local_rank, world_size = setup_dist()
device = f"npu:{local_rank}"

pipe = DiffusionPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev",
    torch_dtype=torch.bfloat16,
).to(device)

# NPU 推荐使用 _native_npu;CUDA 示例通常使用 _native_cudnn
pipe.transformer.set_attention_backend("_native_npu")

cp_config = ContextParallelConfig(ulysses_degree=world_size)
pipe.transformer.enable_parallelism(config=cp_config)

image = pipe(
    prompt="a tiny astronaut hatching from an egg on the moon",
    guidance_scale=3.5,
    num_inference_steps=30,
).images[0]

if dist.get_rank() == 0:
    image.save("flux_cp_output.png")

dist.destroy_process_group()

在单机 2 卡示例中可用:

torchrun --nproc_per_node=2 scripts/run_context_parallel.py \
    --model ./fake_flux_dev \
    --prompt "a tiny astronaut hatching from an egg on the moon" \
    --parallel-mode context \
    --device-type npu --backend hccl \
    --attention-backend _native_npu \
    --steps 20 --output flux_cp_output.png

若当前后端暂不支持 context parallel(例如底层 attention 内核限制),可退化为多进程 data parallel:

torchrun --nproc_per_node=2 scripts/run_context_parallel.py \
    --model ./fake_flux_dev \
    --prompt "a tiny astronaut hatching from an egg on the moon" \
    --parallel-mode data \
    --device-type npu --backend hccl \
    --steps 20 --output flux_dp_output.png

默认推荐直接使用 Ulysses Attention:

cp_config = ContextParallelConfig(ulysses_degree=world_size)
pipe.transformer.enable_parallelism(config=cp_config)

若需要,再按模型/后端特性切换到 Ring Attention:

cp_config = ContextParallelConfig(ring_degree=world_size)
pipe.transformer.enable_parallelism(config=cp_config)

若需要进一步调优,请优先查看官方 attention backend 文档:

  • https://huggingface.co/docs/diffusers/v0.36.0/en/optimization/attention_backends
  • 镜像:https://hf-mirror.com/docs/diffusers/v0.36.0/en/optimization/attention_backends

版本注意:

  • diffusers>=0.36.0:可使用 api/parallel
  • diffusers<=0.35.2:仓库路径中无 docs/source/en/api/parallel.md,应退化为常规多进程并行(按卡拆分请求)或升级版本。

详细流程:references/distributed-inference.md

7. API 文档索引与按版本检索

当用户询问 "某个 API 怎么用" 时,不要先猜 URL,先按下面流程检索:

  1. 读取用户实际版本(例如 0.35.20.36.0)。
  2. 枚举对应 Tag 的 API 文件树:docs/source/en/api/**
  3. 根据用户问题定位具体路径(例如 parallel.mdpipelines/flux.md)。
  4. 再映射到官网文档 URL;如果官网不可达,回退 hf-mirror

路径映射规则:

  • GitHub 源文件:

- https://github.com/huggingface/diffusers/blob/v{version}/docs/source/en/api/{path}.md

  • 官方文档:

- https://huggingface.co/docs/diffusers/v{version}/en/api/{path}

  • 镜像文档:

- https://hf-mirror.com/docs/diffusers/v{version}/en/api/{path}

示例:parallel.mdv0.36.0 存在,但在 v0.35.2 的 API 路径中不存在。

详细索引:references/api-navigation.md

性能基准测试

使用 benchmark 脚本测量推理性能:

python scripts/benchmark_pipeline.py \
    --model ./model_weights \
    --prompt "a photo of a cat" \
    --num-runs 5 \
    --warmup-runs 1 \
    --output-json benchmark_results.json

关键指标:

指标说明
首次推理延迟含图编译/缓存构建,通常较慢
平均推理延迟稳态性能(排除预热)
P50 / P95 延迟延迟分布
NPU 内存峰值torch.npu.max_memory_allocated()
吞吐量images/sec 或 frames/sec

脚本参考

run_pipeline.py

通用 Pipeline 推理脚本,支持任意 Diffusers 模型:

参数必需默认说明
--model-模型路径(本地目录或 HF 模型 ID)
--prompt-生成提示词
--devicenpu:0目标设备
--dtypebfloat16数据类型:float32, float16, bfloat16
--steps20推理步数
--seed42随机种子
--outputoutput.png输出文件(.png 图像 /.mp4 视频)
--width模型默认输出宽度
--height模型默认输出高度
--guidance-scale3.5Classifier-free guidance
--lora-LoRA 权重路径
--lora-scale1.0LoRA 强度
--attention-slicing关闭启用 attention slicing
--vae-slicing关闭启用 VAE slicing
--vae-tiling关闭启用 VAE tiling
--cpu-offload关闭启用 sequential CPU offload
--benchmark关闭打印详细计时指标

validate_pipeline.py

推理前预检脚本:

参数必需默认说明
--model-模型路径(提供时检查权重结构)
--devicenpu:0目标设备
--min-memory16最低空闲 NPU 内存(GB)

benchmark_pipeline.py

详细性能基准测试脚本:

参数必需默认说明
--model-模型权重路径
--prompt-生成提示词
--devicenpu:0设备
--dtypebfloat16数据类型
--steps20推理步数
--warmup-runs1预热次数
--num-runs5测试次数
--attention-slicing关闭启用 attention slicing
--vae-slicing关闭启用 VAE slicing
--vae-tiling关闭启用 VAE tiling
--output-json-结果 JSON 输出路径

run_context_parallel.py

多卡 context parallel 推理脚本(需配合 torchrun):

参数必需默认说明
--model-本地模型路径
--prompt-生成提示词
--parallel-modecontextcontext(Parallel API)或 data(多进程并行)
--device-typenpunpu/cuda/cpu
--backendhccl分布式后端
--attention-backend按设备自动选择NPU 默认 _native_npu,CUDA 默认 _native_cudnn
--ring-degree-Ring Attention 并行度
--ulysses-degree自动(默认=world_size)Ulysses Attention 并行度
--dtypebfloat16数据类型
--steps20推理步数
--outputcp_output.pngRank0 输出图像

常见模型参考

模型Pipeline 类型推荐 dtype推荐 NPU 内存
FLUX.1-devFluxPipelinebfloat16≥24 GB
SDXLStableDiffusionXLPipelinefloat16≥8 GB
SD 3.5StableDiffusion3Pipelinebfloat16≥16 GB
Wan 2.1WanPipelinebfloat16≥24 GB
CogVideoXCogVideoXPipelinebfloat16≥24 GB

常见问题

NPU 内存不足(OOM)

RuntimeError: NPU out of memory

解决:启用内存优化(--attention-slicing --vae-tiling),使用 bfloat16,减少分辨率或步数。

首次推理很慢

NPU 首次推理需要编译计算图,后续推理速度正常。脚本的 --benchmark 模式会自动进行预热。

Generator 设备错误

RuntimeError: Expected a 'npu' device type for generator

解决:使用 torch.Generator("npu") 而非 torch.Generator("cpu")

CPU Offload 不生效

accelerate 的设备钩子可能不完全支持 npu。建议优先使用 attention slicing 和 VAE tiling。

更多问题:references/troubleshooting.md

参考资源

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

33.44%
按下载量换算50

Claude

33.23%
按下载量换算50

Cursor

17.36%
按下载量换算26

Gemini CLI

9.11%
按下载量换算14

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

可疑

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。来源安全扫描存在 warning/failed 结果,不能写成本站确认安全。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills