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
本仓将核心 DiT 与 VAE Decoder 迁移至 Ascend 910B npu:0,BF16 推理,验证 CPU-NPU 一致性与性能。
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
默认命令 (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 数值稳定。
=== 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。
共享权重 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。
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 可承载。
assets/agent_workflow.png - 下载/侦察/加载/NPU推理/验证/性能全流程assets/npu_device_call.png - npu-smi + torch.npu.is_available + npu:0 + bf16assets/model_result.png - 默认推理真实输出 + PASS.to("npu:0").to(bf16) 即可复用同一代码Hardware: NPU, NPU, Ascend, Ascend910, Trellis, 3D-generation, image-to-3d, diffusion, flow-matching, O-Voxel