ALISY/flooddiffusion-npu
模型介绍
文件和版本
Pull Requests
讨论
分析

FloodDiffusion (NPU) · 昇腾 NPU 适配版

FloodDiffusion 是基于 Latent Diffusion Forcing 的**文本到动作(text-to-motion)**扩散模型(Wan2.5-based,humanml3d 动作空间)。给定动作描述文本,生成 3D 人体动作轨迹。

本仓库为 昇腾 NPU 迁移适配版(原模型 ShandaAI/FloodDiffusion,HuggingFace 镜像 id AlayaLab/FloodDiffusion):

  • 全域移除 CUDA 调用,模型与输入张量迁移到 npu:0(import torch_npu)。
  • 提供 inference.py 双模式:命令行 + FastAPI。
  • 权重缓存固定于 model_weights/AlayaLab-FloodDiffusion/,本地优先,缺失自动从 hf-mirror.com 下载。

模型信息

项值
模型FloodDiffusion (text-to-motion, Wan2.5-based Diffusion-Forcing)
输入自然语言动作描述(如 "a person walks forward")
输出263 维 HumanML3D 动作特征 + 22×3 关节 3D 轨迹
编码器UMT5-XXL 文本编码器 (bf16, ~11GB)
主干Transformer 扩散主干(8 层, dim 1024, 扩散步数 10)
解码器1D 卷积 VAE(latent 4 → 263)
帧率20 FPS(length×4 帧)

环境

  • Python 3.11, torch 2.9.0+cpu, torch_npu, CANN 8.5.1, Ascend 910
  • 依赖:torch torch_npu transformers safetensors diffusers lightning omegaconf ftfy regex fastapi uvicorn python-multipart
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple diffusers lightning omegaconf ftfy

快速开始

# 命令行单次推理 (默认文本 "a person walks forward")
python inference.py --text "a person walks forward"

# 指定动作与长度 (length=60 -> 约 240 帧)
python inference.py --text "a person running quickly" --length 60

输出 JSON 结构:

{
  "model": "ShandaAI/FloodDiffusion",
  "text": "a person walks forward",
  "format": "humanml3d_263",
  "frames": 120,
  "dim": 263,
  "device": "npu:0",
  "num_denoise_steps": 10,
  "elapsed_sec": 3.2,
  "motion_263": [...],       // (frames, 263) HumanML3D 特征
  "joints_22x3": [...],      // (frames, 22, 3) 3D 关节轨迹
  "joints_shape": [120, 22, 3]
}

FastAPI 服务

uvicorn inference:app --host 0.0.0.0 --port 8000

测试:

curl -s -X POST http://127.0.0.1:8000/predict \
  -H 'Content-Type: application/json' \
  -d '{"text": "a person walks forward"}'

请求字段:{"text": "...", "length": 60, "num_denoise_steps": 10, "output_joints": true},返回动作生成 JSON。

权重

  • 缓存路径:model_weights/AlayaLab-FloodDiffusion/(相对工程根目录,已通过 .gitignore 排除,不提交大权重)。
  • 首次运行若本地缓存缺失,inference.py 会自动从 https://hf-mirror.com/AlayaLab/FloodDiffusion 快照下载(约 12GB,含 UMT5-XXL 编码器)。

NPU 适配说明

  • 移除全部 .cuda() / torch.cuda.* / device='cuda' 调用;新增 torch_npu 初始化与设备校验(失败打印明确原因)。
  • torch.amp.autocast("cuda", ...) → "npu"。
  • flash-attention(CUDA-only)在 NPU 上自动回退到 scaled_dot_product_attention。
  • RoPE 复数运算由 float64/complex128 调整为 float32/complex64(NPU 不支持 complex128 的 cat)。