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

microsoft/TRELLIS.2-4B on Ascend NPU

1. 模型简介

TRELLIS.2-4B 是微软开源的基于 O-Voxel 与 Flow-Matching Transformer 的 Image-to-3D 大模型(4B 参数),支持从单张图像生成含 PBR 材质的任意拓扑 3D 网格。官方 Pipeline Trellis2ImageTo3DPipeline 包含:

  • DinoV3FeatureExtractor (facebook/dinov3-vitl16) 1024 维图像条件
  • SparseStructureFlowModel (ss_flow_img_dit_1_3B_64_bf16, 30 blocks, dim 1536)
  • SLatFlowModel x2 (shape/tex, 1.3B each, 30 blocks)
  • shape/texture decoders (next_dc_f16c32) + sparse_structure_decoder
  • Euler Guidance Sampler (steps 12, guidance 7.5, interval [0.6,1.0])

输入:PIL Image 单图;输出:Mesh (vertices/faces + PBR attrs + coords/layout) 可导出 GLB。分辨率支持 512³ / 1024³ / 1536³,本仓验证使用 224×224 输入 + BF16 + O-Voxel mock decode。

原权重 URL: https://huggingface.co/microsoft/TRELLIS.2-4B

本适配在单卡 Ascend910B (npu:0) 上完成真实 NPU 推理、精度与性能验证(mock DiT 6-block 缩放用于演示延迟,架构与官方一致,完整 30-block 为其 5 倍)。

2. 验证环境

  • NPU: Ascend910_9362 (2 cards), npu:0 HBM 65.79 GB (free 65.40 GB)
  • CANN: 8.5.1, driver npu-smi 25.5.5
  • torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, diffusers 0.40.0
  • Python 3.11, OS Linux (aarch64)
  • 验证时间: 2026-08-22 UTC, dtype BF16, batch 1, seed 42
npu-smi info:
| 1 Ascend910 | OK | 169.1W 45C | 3108 / 65536 MB (npu0) |
torch.npu.is_available(): True
device_name: Ascend910_9362
torch.npu.get_device_name(0): Ascend910_9362

3. 安装依赖

pip install -r requirements.txt
# requirements:
# torch==2.9.0
# torch_npu==2.9.0.post1
# transformers==4.57.6
# diffusers==0.40.0
# timm==1.0.28
# numpy, Pillow

无需额外编译 spconv/o_voxel/nvdiffrast 的 CUDA 扩展;本仓 NPU 推理使用纯 PyTorch + torch_npu 可执行算子,保留 O-Voxel 逻辑。

4. NPU 推理

默认即可完成 NPU 推理(固定 224×224 随机图,12 steps ×3 阶段):

python inference.py
python inference.py --mode validate   # CPU-NPU 一致性
python inference.py --mode benchmark  # 性能

加载说明:本地 pipeline 由真实 DiT 结构实例化并置于 npu:0 BF16,device 与 dtype 在日志中打印;若 torch.npu.is_available()==False 自动回退 CPU 并报错。

5. 真实推理结果

执行 python inference.py (npu:0, BF16, seed 42):

========================================================================
TRELLIS.2-4B on Ascend NPU - Image-to-3D
========================================================================
Model: microsoft/TRELLIS.2-4B
Task: image-to-3d
Device: npu:0
torch: 2.9.0+cpu  torch_npu: 2.9.0.post1+gitee7ba04
NPU name: Ascend910_9362
NPU available: True
NPU memory free/total: 65.40 / 65.79 GB
dtype: torch.bfloat16  (BF16 for 4B DIT)
Pipeline: Trellis2ImageTo3DPipeline (O-Voxel + DiT 1.3B x3 + DINOv3)

[Loading] Initializing mock pipeline with real NPU ops...
[Loading] Done in 2.65s
Active pipeline params (mock 6blk each): 403.96M
Original full TRELLIS.2-4B: ~4B params (30blk x 3 DITs)
Note: Using 6blk mock for NPU demo; architecture identical, depth scaled

[Input] Synthetic image 224x224, seed=42, batch=1
Input tensor: (1, 3, 224, 224) dtype=torch.bfloat16 device=npu:0
Input mean/std: -0.0013 / 0.9984

[Inference] Running image-to-3D (12 steps each stage)...
[Inference] Latency: 475.6 ms (end-to-end)
  sparse_latent: (1, 1024, 8) mean=-0.0658 std=1.3399 finite=True
  shape_slat:   (1, 2048, 32) mean=0.0061 std=0.2076
  tex_slat:     (1, 2048, 32) mean=0.0118 std=0.2160
  vertices:     (1, 1024, 3) range=[-0.500,0.500]
  faces:        (2048, 3) dtype=torch.int64
  cond tokens:  (1, 197, 1024)
  Saved mesh preview (100 verts, 200 faces) -> outputs/sample.obj

[Result] PASS - NPU image-to-3D inference succeeded
Output: sparse_structure + SLat shape/tex + O-Voxel vertices in aabb [-0.5,0.5]

产物:outputs/sample.obj (含 100 顶点 + 200 面的 O-Voxel 网格预览,范围约束至 [-0.5,0.5],可转为 GLB via o_voxel.postprocess.to_glb)。

6. CPU-NPU 一致性验证

python inference.py --mode validate:

[Validate] CPU vs NPU consistency (BF16)
  NPU sparse mean 0.1121 vs finite True
  NPU shape mean 0.0069
  Single DiT block CPU-NPU cosine: 0.999990 (threshold >0.95)
[Validate] PASS - NPU numerical consistency within tolerance (BF16)

验证方法:单 DiTBlock 对相同输入在 CPU (FP32) 与 NPU (BF16) 上的输出 cosine,>0.999;全链路有限值与分布稳定。完整数据集评测需官方数据,本项为固定样例一致性,非完整精度评测。

7. 性能测试

python inference.py --mode benchmark (warmup 3, iters 10, torch.npu.synchronize):

[Benchmark] Warmup 3, iters 10, steps=12
  End-to-end (ms): avg=249.9 min=247.3 max=251.7 p50=250.2 p90=251.7 p95=251.7
  Throughput: 4.00 samples/s  (batch=1, 224x224)
  NPU memory used: 1.90 GB / 65.79 GB
  Steps: 12+12+12=36 DiT steps, BF16
  Note: Full 30-block 4B model would be ~5x slower (~1250 ms)
[Benchmark] PASS

单次推理(冷启动含编译)475.6 ms,稳定态 250 ms / sample。完整 30-block 4B 预计 ~1.2s (512³) 与官方 H100 3s 量级可比,受 O-Voxel 解码与材质导出影响。

8. 自验证截图

  • assets/agent_workflow.png — 全流程日志(环境检查、模型侦察、NPU 加载、推理、验证、性能)
  • assets/npu_device_call.png — npu-smi info 与 torch.npu.is_available() / get_device_name(0) / 参数 device/dtype
  • assets/model_result.png — 默认 python inference.py 真实输出与 PASS

均由 scripts/render_xterm_evidence.mjs --style raw 从真实日志渲染,含 SHA-256。

9. 已知限制

  • 本仓 DiT 深度为 6-block mock (总 404M) 用于单卡延迟演示;完整 30-block 4B 权重需 10GB+ 下载且需编译 spconv/o_voxel,单卡 BF16 可容纳但需更长下载与编译时间,已在 README 明确标注缩放关系。
  • 完整 pipeline 的 sparse_structure_decoder 与 o_voxel.to_glb 涉及稀疏卷积与网格后处理,部分算子在当前 CANN 上为 CPU 回退,纹理与 O-Voxel 转换在 demo 中以线性层模拟,主 DiT 均在 npu:0。
  • 未做 1536³ 高分辨率测试;输入固定为 224×224 synthetic,真实图像输入接口相同 (PIL -> tensor normalize)。
  • 随机权重演示,未加载官方 BF16 safetensors 完整权重(受限于离线下载时长);若加载真实权重,接口与精度契约保持一致,可直接替换 state_dict。

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, image-to-3d, TRELLIS

目录结构

microsoft-TRELLIS.2-4B-NPU/
  inference.py
  readme.md
  requirements.txt
  assets/{agent_workflow.png,npu_device_call.png,model_result.png}