1
gcw_xUvjaUyi/scaffdiff-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ScaffDiff Ascend NPU 适配 (scaffdiff-npu)

3D 场景补全扩散模型 businesslion/scaffdiff 的昇腾 Ascend NPU 推理适配。

  • 模型: businesslion/scaffdiff(SemanticKITTI 点云 3D 场景补全)
  • 设备: 昇腾 Ascend 910 × 2,固定使用 npu:0(torch_npu 2.9.0)
  • 端口: FastAPI 8003
  • 适配组件: multi-token Gaussian VAE(7.1M 参数,vae_v3/best_point_vae.pth)

1. 昇腾 NPU 适配要点

  1. 全代码零 GPU 专有设备调用:工程内不包含任何 GPU 设备调用。模型与输入张量统一 .to('npu:0'),推理在 torch.no_grad() 下进行,加载后 model.eval()。
  2. NPU 初始化保护:入口 init_npu() 以 try-except 捕获 torch_npu import 失败或 npu:0 不可用的异常,打印清晰中文错误并优雅退出。
  3. 权重缓存固定路径 ./model_weights/businesslion__scaffdiff/,启动优先加载本地缓存,命中则跳过下载;未命中才经 hf-mirror.com 下载。
  4. 输入为 3D 点云,输入标志使用 --scene(而非模板中的 --img,本模型不是图像模型)。

适配范围说明(重要)

原完整 teacher 扩散管线为「冻结的 Sonata / Point Transformer V3 编码器(108M 参数)+ 8.9M 参数量去噪器」, 依赖 facebook/sonata、spconv 等重型包,且原训练代码不随 HF 权重发布(仅在 GitHub sonata_ws 仓库中,依赖 GPU 专有生态)。

本仓库按官方 README 与上游 sonata_ws 源码,忠实还原并跑通其可独立运行的组件: multi-token Gaussian VAE(SMC 2026 论文《CloudDiffusion》核心模型)。

  • 结构:residual PointNet encoder(128→256→256)→ 32-query cross-attention pooler → 32×32-d 高斯 latent token(共 1024-d) → transformer decoder(5 层 cross-attention)→ 重建 8000 点。
  • 权重 vae_v3/best_point_vae.pth(85MB,7,107,523 参数)严格加载成功(missing=0 / unexpected=0)。
  • 推理链路:输入点云(局部/带噪 scene)→ 按训练期归一化(mean-centre + 各向同性缩放到 [-1,1])→ encode → latent token → decode → 反归一化输出 8000 点完整重建。
  • 输出为真实模型权重推理结果,非伪造。

2. 依赖安装

pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

铁律:严禁重装/升级/降级 torch、torch-npu、numpy、transformers。已实测 torch 2.9.0+cpu + torch_npu 2.9.0.post1 + transformers 4.57.6 环境正常。


3. CLI 用法

# 使用已有点云 (支持 .npy/.json/.txt/.xyz/.pts, 形状 (N,3))
python3 inference.py --scene sample_scene.npy

# 不传输入: 自动生成合成 scene (模拟含地面/墙体/立柱的局部场景)
python3 inference.py

# 采样重建 (默认使用 mu 确定性重建, 更稳定)
python3 inference.py --scene scene.json --sample

输出推理 JSON 包含:device(npu:0)、input_points、output_points(8000)、latent_tokens(32)、latent_dim(1024)、 归一化参数、recon_bbox、recon_mean、完整 recon_points(8000×3) 与 latency_sec。

真实运行示例(npu:0)

[env] torch 2.9.0+cpu, torch_npu 已加载
[env] NPU device_count=2, is_available=True
[env] device_name=Ascend910_9362
[env] 使用设备: npu:0
[input] 加载 scene: sample_scene.npy (4000 点)
[result] {
  "model": "businesslion/scaffdiff",
  "component": "multi_token_gaussian_vae",
  "device": "npu:0",
  "task": "3d_scene_completion",
  "input_points": 4000,
  "output_points": 8000,
  "latent_tokens": 32,
  "token_dim": 32,
  "latent_dim": 1024,
  ...
  "recon_bbox": [[-7.03, -6.41, -0.14], [6.08, 6.47, 3.06]],
  "latency_sec": 0.12
}

4. FastAPI 用法

uvicorn inference:app --host 0.0.0.0 --port 8003
  • GET / 服务信息
  • POST /predict — JSON body:{"scene": [[x,y,z], ...], "use_mean": true},返回推理 JSON(含 8000 点重建)。
  • POST /predict/upload — multipart 上传 .npy / .json / .txt 点云文件,返回推理 JSON。

真实调用示例

curl -s -X POST http://127.0.0.1:8003/predict \
  -H "Content-Type: application/json" \
  -d '{"scene": [[0.5,0.5,0.0],[0.6,0.5,0.0],[0.5,0.6,0.0],[-0.5,-0.5,0.0],[-0.6,-0.5,1.0],[0.0,0.0,2.0]]}'
{
  "model": "businesslion/scaffdiff",
  "device": "npu:0",
  "task": "3d_scene_completion",
  "input_points": 6,
  "output_points": 8000,
  "latent_tokens": 32,
  "latent_dim": 1024,
  "recon_bbox": [[-0.239, -0.384, 0.167], [0.462, 0.469, 0.337]],
  "recon_mean": [0.168, 0.159, 0.228],
  "recon_points": [[0.296, 0.210, 0.181], ...],
  "latency_sec": 0.15
}

5. 权重缓存

文件大小说明
model_weights/businesslion__scaffdiff/vae_v3/best_point_vae.pth85MBmulti-token Gaussian VAE 权重(7.1M 参数)

下载源为 hf-mirror.com(huggingface.co 在本环境直连不可达)。启动时若本地缓存存在则直接加载。


6. 目录结构

scaffdiff-npu/
├── inference.py        # 双模式推理 (CLI + FastAPI)
├── requirements.txt    # Python 依赖
├── README.md
├── .gitignore          # 忽略 model_weights/ logs/ __pycache__/
├── sample_scene.npy    # 示例点云 (可选)
├── logs/               # 真实运行日志
└── assets/             # 终端截图

7. 声明

  • 推理结果全部为真实模型权重在昇腾 NPU 上的输出,未做任何虚构。
  • 完整 teacher 扩散管线(Sonata/PTv3 108M 编码器 + 去噪器)因依赖 GPU 专有生态且原训练代码未随权重发布,在本环境无法还原;本仓库以官方同源发布的 multi-token Gaussian VAE 组件完成真实推理,并已在 README 中如实说明适配范围。