TRELLIS.2(microsoft/TRELLIS.2-4B)是微软开源的 4B 参数 3D 资产生成模型,实现 image → latents → mesh / 3DGS 的端到端 pipeline,基于 O-Voxel 稀疏体素结构与 Flow-Matching Transformer,可生成最高 1536³ 分辨率的 PBR 3D 资产。
本仓库自包含以下三个子模块(本次 NPU 适配对象):
| 子模块 | 类型 | 参数量 | 精度 |
|---|---|---|---|
shape_dec_next_dc_f16c32_fp16 | FlexiDualGridVaeDecoder(3D 稀疏 VAE 解码器) | ~474M | fp16 |
shape_enc_next_dc_f16c32_fp16 | FlexiDualGridVaeEncoder(3D 稀疏 VAE 编码器) | ~354M | fp16 |
slat_flow_img2shape_dit_1_3B_1024_bf16 | SLatFlowModel(1.3B 稀疏 DiT 流模型) | ~1292M | bf16 |
适配目标:任意子模型可在昇腾 NPU(Ascend 910)上完成一次 forward(输入合成 latent tensor,输出 shape / slat 流),并对 NPU 与 CPU 的输出做数值与耗时对比。
原始实现依赖 CUDA 专属库(flex_gemm 稀疏卷积、flash_attn 稀疏注意力、o_voxel /
cumesh 网格后处理)。本次适配:
trellis2_src/trellis2/modules/sparse/conv/conv_none.py
(子流形稀疏卷积,通过 SPARSE_CONV_BACKEND=none 启用)。full_attn.py / windowed_attn.py 增加 torch 后端
(varlen 切块 + F.scaled_dot_product_attention,通过 SPARSE_ATTN_BACKEND=torch 启用)。o_voxel / cumesh / flex_gemm 改为可选导入;
o_voxel 不可用时 VAE 解码器直接返回原始解码流(不做 mesh 后处理)。*.json(模型结构)+ *.safetensors(参数)格式通过
trellis2.models.from_pretrained 加载。| 组件 | 版本 |
|---|---|
| 操作系统 | Linux(aarch64) |
| Python | 3.11.14 |
| PyTorch | 2.9.0+cpu(NPU 运行时亦以 cpu wheel 为底座) |
| torch_npu | 2.9.0.post1 |
| CANN | 8.5.1 |
| Ascend | 910 × 2(使用 npu:0) |
| transformers | 4.57.6 |
pip install -r requirements.txt# TRELLIS.2 源码(含 NPU 适配 patch,已随仓库提供:trellis2_src/)
# 权重(约 10-16G,本仓库不包含,需自行下载到 /data/dl/trellis2/):
# ckpts/shape_dec_next_dc_f16c32_fp16.{json,safetensors}
# ckpts/shape_enc_next_dc_f16c32_fp16.{json,safetensors}
# ckpts/slat_flow_img2shape_dit_1_3B_1024_bf16.{json,safetensors}权重目录为 /data/dl/trellis2/ckpts(可在 inference.py 中通过 CKPT_DIR 修改)。
python inference.py --device cpu # 保存 cpu_output.txt + cpu_run.log + cpu_outputs.npzpython inference.py --device npu # 保存 npu_output.txt + npu_run.log + npu_outputs.npz两个设备使用相同 seed(12345)与相同合成输入(256 个 latent 点,每子模块 forward 计时 3 次取平均),保证可对比性。
python compare.py # 生成 assets/compare_result.txt(6 项指标)=== TRELLIS.2-4B submodule smoke inference @ npu ===
[shape_dec] output shape: [80954, 7] | avg time: 825.41 ms | cum time: 825.41 ms | mem peak: 1054.9 MiB
[shape_enc] output shape: [63, 32] | avg time: 275.99 ms | cum time: 1101.40 ms | mem peak: 695.7 MiB
[slat_flow] output shape: [256, 32] | avg time: 110.86 ms | cum time: 1212.25 ms | mem peak: 2561.2 MiB
=== TOTAL cumulative avg time on npu: 1212.25 ms ===| # | 指标 | 数值 | 说明 |
|---|---|---|---|
| 1 | cos_sim(拼接 flatten) | 0.5532 | 三子模块输出拼接后余弦相似度 |
| 2 | max_abs(拼接 flatten) | 1.083e+02 | 拼接输出最大绝对误差 |
| 3 | mean_abs(拼接 flatten) | 5.877 | 拼接输出平均绝对误差 |
| 4 | 子模块输出形状一致率 | 0.67(2/3) | shape_enc / slat_flow 一致;shape_dec 点数略异(CPU 80679 vs NPU 80954,fp16 稀疏 conv 下采样/去重差异) |
| 5 | 拼接整体 L2 误差 | 7.660e+03(rel 9.45e-01) | L2 范数差 / CPU L2 范数 |
| 6 | NPU vs CPU 耗时比(3 次平均) | 0.0021 | shape_dec 0.0015 / shape_enc 0.0079 / slat_flow 0.0080,累计 NPU 1212.25ms vs CPU 589827.47ms(约 486× 加速) |
| 子模块 | CPU 平均 (ms) | NPU 平均 (ms) | 比值 (npu/cpu) |
|---|---|---|---|
| shape_dec | 541165.27 | 825.41 | 0.0015 |
| shape_enc | 34782.76 | 275.99 | 0.0079 |
| slat_flow | 13879.43 | 110.86 | 0.0080 |
| 累计 | 589827.47 | 1212.25 | 0.0021 |
| 子模块 | CPU (MiB) | NPU (MiB) |
|---|---|---|
| shape_dec | 3309.0 | 1054.9 |
| shape_enc | 3574.5 | 695.7 |
| slat_flow | 6908.6 | 2561.2 |
<1 表示 NPU 更快。