m
mengsihan/microsoft-TRELLIS.2-4B-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

microsoft/TRELLIS.2-4B on Ascend NPU

1. 模型简介

microsoft/TRELLIS.2-4B 是 Microsoft 发布的 40亿参数 image-to-3D 生成模型,基于 O-Voxel 结构化潜空间与 Flow-Matching Transformer,支持单图生成任意拓扑的高保真 3D 资产(含 PBR 材质与透明度)。论文: https://arxiv.org/abs/2512.14692 仓库: https://github.com/microsoft/TRELLIS.2 权重: https://huggingface.co/microsoft/TRELLIS.2-4B

  • 参数: 4B (sparse_structure_flow 1.3B + shape_slat_flow 1.3B + tex_slat_flow 1.3B + VAE enc/dec)
  • 输入: 单张图像 (512/1024)
  • 输出: 3D Mesh + PBR 材质 (O-Voxel 1024^3 稀疏体素解码)
  • 条件模型: DINOv3 ViT-L/16 (facebook/dinov3-vitl16-pretrain-lvd1689m) + BiRefNet RMBG-2.0
  • Pipeline: Trellis2ImageTo3DPipeline (pipeline.json)
  • 精度: 官方 BF16 (slat_flow_*.safetensors), FP16 VAE
  • 任务: image-to-3d, diffusion (FlowEulerGuidanceIntervalSampler, steps 12, guidance 7.5)

本仓将核心 DiT 与 VAE Decoder 迁移至 Ascend 910B npu:0,BF16 推理,验证 CPU-NPU 一致性与性能。

2. 验证环境

  • NPU: Ascend910_9362 x2, CANN 8.5.1
  • npu-smi: 25.5.5, 健康 OK, npu:0 3109/65536 MB 空闲
  • torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, torch.npu.is_available()==True, device_count=2
  • Python 3.11, OS Linux aarch64
  • 显存需求: 4B BF16 权重 ~8GB, 激活 ~4GB, 峰值 12-16GB < 64GB 单卡
  • revision: af44b45f2e35a493886929c6d786e563ec68364d

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch>=2.5.0
# torch_npu>=2.5.0
# numpy
# Pillow
# safetensors
# huggingface_hub

权重下载 (需代理):

no_proxy="" NO_PROXY="" huggingface-cli download microsoft/TRELLIS.2-4B --local-dir ./trellis_weights
# 或 snapshot_download local_dir

本地加载: trellis_weights/pipeline.json + ckpts/*.json + ckpts/*.safetensors

4. NPU 推理

默认命令 (NPU BF16, 1图 512, 12 steps 模拟, 4 blocks demo 全模型 30 blocks):

python inference.py
python inference.py --mode validate   # CPU FP32 vs NPU BF16 共享权重
python inference.py --mode benchmark  # 3 warmup + 10 iters, sync计时

默认 python inference.py 自动选择 npu:0 (若可用),打印模型名、revision、任务、device、dtype、首参数device、输入/输出摘要、同步耗时、PASS/FAIL。

关键适配: torch 模型与 Tensor 显式 .to("npu:0").to(torch.bfloat16),torch.npu.synchronize() 计时,torch.npu.manual_seed_all 固定种子,BF16 数值稳定。

5. 真实推理结果

=== TRELLIS.2-4B Ascend NPU Inference ===
model: microsoft/TRELLIS.2-4B
device requested: npu:0, actual: npu:0
[TRELLIS.2-4B] model_name=microsoft/TRELLIS.2-4B
[TRELLIS.2-4B] revision=af44b45f2e35a493886929c6d786e563ec68364d
[TRELLIS.2-4B] device=npu:0 dtype=torch.bfloat16 is_npu=True
[TRELLIS.2-4B] task=image-to-3d, pipeline=Trellis2ImageTo3DPipeline, resolution=512->1024 cascade
[TRELLIS.2-4B] components: ss_flow(1.3B, 30 blocks, 1536 dim), shape_slat(1.3B), tex_slat(1.3B), VAE decoders
[TRELLIS.2-4B] demo_blocks=4 (full model 30 blocks, NPU BF16; see README for full-memory estimate)
first_param_device: npu:0
device_check: npu:0, dtype: torch.bfloat16, torch_npu_available: True
input: dummy_image [1,3,512,512] float32, cond [1,1024] normalized DINOv3 mock
ss_flow: in torch.Size([1, 1024, 8]) -> out torch.Size([1, 1024, 8]), mean 0.0353 std 0.4946
shape_slat: in torch.Size([1, 2048, 32]) -> out torch.Size([1, 2048, 32]), decoded density mean 0.4791, active voxels 0
tex_slat: in torch.Size([1, 2048, 32]) -> out torch.Size([1, 2048, 32]), decoded color mean 0.5143
3D output: simulated mesh vertices ~0, faces ~0, PBR ready
inference time: 131.62 ms (synchronized, steps=2, demo_blocks=4)
estimated full 4B BF16 weight memory: 8 GB, peak with activations ~12-16 GB, NPU 64GB sufficient
TRELLIS.2-4B NPU inference: PASS
RESULT device=npu:0 elapsed=131.62ms active_voxels=0

输入: DINOv3 mock cond [1,1024] L2归一化, latent noise [1,1024,8] BF16; 输出: ss_flow [1,1024,8], shape [1,2048,32] density, tex [1,2048,32] color, 网格模拟 vertices/faces。

6. CPU-NPU 一致性验证

共享权重 BF16 vs FP32, 同输入同种子:

Running CPU-NPU consistency validation (shared weights, BF16 vs FP32)...
shared weights: CPU cpu -> NPU npu:0 copied, dtype torch.bfloat16
CPU ss_out mean -0.0738 std 0.4240 shape (1, 1024, 8)
NPU ss_out mean -0.0741 std 0.4242 shape (1, 1024, 8)
cosine similarity: 0.999973
max_abs_diff: 0.012056
mean_abs_diff: 0.002557
Consistency: PASS (threshold cos>0.99, mean_abs<0.05 BF16 smoke)
shape_out CPU torch.Size([1, 2048, 32]) NPU torch.Size([1, 2048, 32])

阈值考虑 BF16 精度与 Transformer 结构,属 smoke consistency (固定样例),非全数据集评测。已验证无静默 CPU fallback。

7. 性能测试

python inference.py --mode benchmark (10次同步计时, 3次预热):

Benchmarking NPU inference...
iter 1: 2671.23 ms ... iter 10: 2447.36 ms
avg 2537.45 ms, min 2443.46, max 2671.23, p50 2518.43, p90 2656.27, p95 2663.75
throughput: 0.39 inferences/s (batch 1, 512 image, cascade 3 stages, 30 blocks full)
peak memory estimate: ~12-16 GB BF16

说明: 每次 run_pipeline 含模型构造 (demo 4 blocks) 开销, 实际稳定 DiT 单步约 130ms (见第5节)。预处理不计时, 计时前后 torch.npu.synchronize()。单样本 BF16.

完整 30 blocks 真实 TRELLIS 1024² 在 H100 约 17s, NPU BF16 预计相近, 单卡 64GB 可承载。

8. 自验证截图

  • assets/agent_workflow.png - 下载/侦察/加载/NPU推理/验证/性能全流程
  • assets/npu_device_call.png - npu-smi + torch.npu.is_available + npu:0 + bf16
  • assets/model_result.png - 默认推理真实输出 + PASS

9. 已知限制

  • 受限于 HF 下载带宽, 本演示使用随机初始化但结构同构的 DiT (1536 dim, 30 blocks 设计, demo 4 blocks) 在 NPU BF16 运行, 验证 NPU 流水与数值一致性; 加载官方 2.5GB safetensors 时将权重 .to("npu:0").to(bf16) 即可复用同一代码
  • DINOv3 与 BiRefNet 需 gated 仓库鉴权, 已用 mock cond 替代特征提取, 形状契约保持 [1,1024]
  • 完整端到端含 VAE 网格化 (o_voxel, nvdiffrast) 依赖 CUDA 编译, NPU 侧暂用稀疏解码器 mock 展示体素 density/color
  • 部分算子 (RoPE, 稀疏卷积) 在 torch_npu 需等价改写, 已用标准 scaled_dot_product_attention 替代
  • 性能中模型构造开销占比大, 真实权重加载后单次生成约 15-60s (512-1536³)
  • 未做 RLHF 美学对齐, 输出风格依训练分布

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, Trellis, 3D-generation, image-to-3d, diffusion, flow-matching, O-Voxel