[Apple Silicon] Add MPS and Metal inference support
Summary
This PR adds source-native Apple Silicon support to TRELLIS.2.
On arm64 macOS, the automatic backend selects PyTorch MPS together with capability-probed Metal extensions. The existing CUDA/Linux route remains available, while macOS receives explicit fallbacks for operations that are unavailable or unstable on MPS.
This addresses the Apple Silicon / MPS portion of #74. It intentionally does not close the AMD or Intel parts of that issue.
What changed
- Add automatic Apple Silicon backend resolution:
- PyTorch MPS for model inference
flex_gemmMetal sparse convolution and sparse attentionmtldiffrastMetal rasterizationmtlmesh/mtlbvhmesh and BVH operations
- Add capability probes before enabling individual Metal components.
- Add controlled fallback paths:
- pure-PyTorch sparse convolution
- PyTorch SDPA attention
- CPU mesh extraction
- KDTree texture baking
- Use SDPA for dense attention on macOS while preserving the upstream
flash_attndefault on other platforms. - Add actionable diagnostics for MPS OOM, empty meshes, and Metal watchdog/BVH failures.
- Add a reproducible Python 3.11 macOS setup:
- primary pair: PyTorch 2.13.0 / torchvision 0.28.0
- ABI fallback: PyTorch 2.11.0 / torchvision 0.26.0
- pinned Metal extension revisions
pip check, MPS, SDPA, MLX, KDTree, raster, and BVH probes
- Pin the runtime model revisions and support strict offline loading from a dedicated Hugging Face cache.
- Preserve authored RGBA transparency; opaque inputs still use the official RMBG-2.0 model.
- Add a CLI for reproducible image-to-3D generation with:
auto,mps, and experimentalmlxbackend selection- Metal/KDTree baker selection
- 512, 1024, and 1024 cascade pipelines
- deterministic seeds and configurable texture size
- no default triangle limit
- Produce reproducible artifacts:
raw_full.glbcandidate_pbr.glbmeta.jsonwith revisions, timings, hashes, bounds, backend probes, and fallback attempts
- Preserve generated UV0, base color, metallic, roughness, and alpha during PBR export.
- Add an experimental MLX backend with small numerical parity tests. End-to-end acceptance still uses PyTorch MPS.
The PBR fallback order is:
- full-resolution Metal bake
- full-resolution KDTree bake
- technical ~200k-face candidate with Metal
- technical ~200k-face candidate with KDTree
raw_full.glb is never decimated, and every fallback is recorded in meta.json.
Validation
Tested on:
- Apple M4 Max, 36 GB unified memory
- macOS 26.4
- Python 3.11.15
- PyTorch 2.13.0
- torchvision 0.28.0
Automated checks:
28 passedin the pytest suite- standalone Metal sparse-convolution integration smoke passed
- dense, masked, and production split-K Metal kernels produced equivalent output
- MPS, SDPA, Metal sparse attention, raster, BVH, KDTree, and MLX probes passed
- strict offline model-cache verification passed
pip checkandcompileallpassed- simulated Metal BVH failure selected the expected full-resolution KDTree fallback
End-to-end generation:
- pipeline:
512 - seed:
42 - texture size:
1024 - input: repository example RGBA image
raw_full.glb: 3,090,522 trianglescandidate_pbr.glb: 3,025,626 triangles- first full-resolution Metal PBR attempt succeeded
- technical decimation was not used
- wall time: 1004.60 seconds
- maximum RSS: 17.82 GB
- completed without OOM
The resulting glTF 2.0 files were validated for non-empty geometry, UV0, vertex normals, consistent bounds, and embedded base-color, metallic-roughness, and alpha textures.
The official RMBG-2.0 path was also tested separately with an opaque input and produced a non-trivial alpha mask.
Compatibility and known limitations
- End-to-end testing was performed on Apple M4 Max only.
- The CUDA/Linux route is retained but was not end-to-end tested on CUDA hardware in this environment.
- PyTorch currently runs
segment_reducethrough its CPU fallback on MPS. This is functional but affects performance. - MLX support is experimental and currently covered by small parity tests rather than end-to-end acceptance.
- DINOv3 and RMBG-2.0 require accepted Hugging Face access terms and authentication.
- The macOS setup currently depends on pinned Metal extension forks.
- This is a large change. I am happy to split the core MPS/Metal backend, setup/CLI, and experimental MLX work into separate PRs if that is easier to review.
Credits
This work builds on and preserves the authorship of the Apple backend work from:
The CLI diagnostics and fallback design were also informed by:
@microsoft-github-policy-service agree
Nice to see the Apple Silicon path getting upstreamed — thanks for carrying the pedronaugusto/trellis2-apple lineage forward with credit.
Heads up on a small overlap: I opened two tiny fixes against main (#173, #174) that branch from the same 6055b86 you credit, and this PR touches both spots without carrying the fix, so they'll conflict at merge and — more importantly — the underlying bugs would survive:
-
from_pretrained(#174): you editreturn globals()[config['name']].from_pretrained(path)to add**hub_kwargs, but keep theglobals()[...]lookup. That bypasses the module's lazy__getattr__, so it raisesKeyError: 'Trellis2ImageTo3DPipeline'wheneverfrom_pretrainedis the first touch of thepipelinesmodule (nothing has populatedglobals()yet). One-char-ish fix:getattr(sys.modules[__name__], config['name']). -
texturing keep-existing-UVs (#173): you rewrite
postprocess_mesh(.cuda()→.to(self.device)) but leavenormals = mesh.vertex_normalsand don't carryvisual=mesh.visualthroughpreprocess_mesh. Without thevisual=pass-through,run()normalizes the mesh beforepostprocess_meshsees it and the keep-existing-UVs branch is unreachable — and the moment it is reachable, the in-place glTF axis swap on the read-onlyvertex_normalscache crashes (hence thenp.array()copies). We verified the fixed path re-textures with bit-identical UVs (max delta 0.0) in production.
Both are 1–8 line changes. Happy to have you fold them into this PR (I don't need the attribution), or I can rebase #173/#174 on top of yours once one of them lands — whatever's least friction for you. No urgency on our end.
@xocialize Thanks for the detailed heads-up. I’ve folded both fixes into #175: the lazy from_pretrained resolution fix, plus the visual/UV pass-through and copied mesh arrays before the in-place transforms.