trellis_sparse_mrp_mlx/trellis_sparse_mlx/__init__.py
John 8981919ee3 Fix RoPE: complex phases, and RMS-norm BEFORE rotation
Two silent bugs, both found by diffing against upstream running on CPU torch. The flow
models have no sparse conv, so upstream is runnable here with flash-attn swapped for
SDPA - a real numerical oracle, unlike the sparse path.

1. rope_phases ships as COMPLEX64 (torch.polar), so the rotation is a complex multiply
   and cos/sin are the phase's real/imag parts. Taking cos() of a complex phase was
   completely wrong. Verified against torch's view_as_complex formulation to 1.2e-7.

2. Upstream applies qk RMS norm BEFORE RoPE; I had it reversed. They do not commute -
   RMS applies a per-component gain, RoPE rotates within pairs. Reversed, one block
   still correlated 0.9998, which compounded to 0.84 across 30 blocks.

After both: SparseStructureFlowModel matches upstream at correlation 1.00000000,
max abs diff 1.2e-5, on the real 1.3B checkpoint.
2026-08-02 12:02:44 +10:00

50 lines
1.8 KiB
Python

"""MLX implementation of the TRELLIS-lineage sparse module.
Microsoft's TRELLIS.2 sparse stack has been inherited, near-verbatim, by a growing
family of 3D generation models — LATO.2 and TencentARC's Pixal3D among them. All of
them hard-require spconv or torchsparse, neither of which has a Metal build, and that
single dependency is what keeps the whole lineage off Apple Silicon.
The blocker is one operation: submanifold 3x3x3 convolution. Every SparseConv3d in
these models is constructed `stride=1, padding=None`, which spconv dispatches to
SubMConv3d. Implement that in MLX and the rest is ordinary linear/norm/attention work.
Packaged separately from any one model so each port depends on a tested core rather
than vendoring its own copy.
"""
from .conv import SubMConv3d, build_indice_map
from .dit import (
ProjectAttention,
DiTAttention,
ModulatedTransformerCrossBlock,
MultiHeadRMSNorm,
TimestepEmbedder,
apply_rope,
)
from .tensor import SparseTensor, VarLenTensor, downsample, subdivide, upsample
from .ops import (
LayerNorm32,
SparseFeedForwardNet,
SparseGELU,
SparseGroupNorm32,
SparseLinear,
SparseMultiHeadAttention,
SparseResBlock,
SparseSiLU,
SparseTransformerBlock,
SparseTransformerCrossBlock,
)
__all__ = [
"SparseTensor", "VarLenTensor", "subdivide", "downsample", "upsample",
"SubMConv3d", "build_indice_map",
"SparseLinear", "LayerNorm32", "SparseGroupNorm32", "SparseSiLU", "SparseGELU",
"SparseResBlock", "SparseFeedForwardNet", "SparseMultiHeadAttention",
"SparseTransformerBlock", "SparseTransformerCrossBlock",
# DiT / flow-model pieces
"MultiHeadRMSNorm", "apply_rope", "TimestepEmbedder", "DiTAttention",
"ModulatedTransformerCrossBlock", "ProjectAttention",
]
__version__ = "0.1.0"