File size: 14,535 Bytes
bcb05d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from typing import *
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
from ...modules.utils import zero_module, convert_module_to_f16, convert_module_to_f32
from ...modules import sparse as sp
from .base import SparseTransformerBase


class SparseSubdivideBlock3d(nn.Module):

    def __init__(
        self,
        channels: int,
        out_channels: Optional[int] = None,
        use_checkpoint: bool = False,
    ):
        super().__init__()
        self.channels = channels
        self.out_channels = out_channels or channels
        self.use_checkpoint = use_checkpoint

        self.act_layers = nn.Sequential(
            sp.SparseConv3d(channels, self.out_channels, 3, padding=1),
            sp.SparseSiLU()
        )
        
        self.sub = sp.SparseSubdivide()
        
        self.out_layers = nn.Sequential(
            sp.SparseConv3d(self.out_channels, self.out_channels, 3, padding=1),
            sp.SparseSiLU(),
        )
        
    def _forward(self, x: sp.SparseTensor) -> sp.SparseTensor:
        h = self.act_layers(x)
        h = self.sub(h)
        h = self.out_layers(h)
        return h
    
    def forward(self, x: torch.Tensor):
        if self.use_checkpoint:
            return torch.utils.checkpoint.checkpoint(self._forward, x, use_reentrant=False)
        else:
            return self._forward(x)


class SparseSDFDecoder(SparseTransformerBase):
    def __init__(
        self,
        resolution: int,
        model_channels: int,
        latent_channels: int,
        num_blocks: int,
        num_heads: Optional[int] = None,
        num_head_channels: Optional[int] = 64,
        mlp_ratio: float = 4,
        attn_mode: Literal["full", "shift_window", "shift_sequence", "shift_order", "swin"] = "swin",
        window_size: int = 8,
        pe_mode: Literal["ape", "rope"] = "ape",
        use_fp16: bool = False,
        use_checkpoint: bool = False,
        qk_rms_norm: bool = False,
        representation_config: dict = None,
        out_channels: int = 1,
        chunk_size: int = 1,
    ):
        super().__init__(
            in_channels=latent_channels,
            model_channels=model_channels,
            num_blocks=num_blocks,
            num_heads=num_heads,
            num_head_channels=num_head_channels,
            mlp_ratio=mlp_ratio,
            attn_mode=attn_mode,
            window_size=window_size,
            pe_mode=pe_mode,
            use_fp16=use_fp16,
            use_checkpoint=use_checkpoint,
            qk_rms_norm=qk_rms_norm,
        )
        self.resolution = resolution
        self.rep_config = representation_config
        self.out_channels = out_channels
        self.chunk_size = chunk_size
        self.upsample = nn.ModuleList([
            SparseSubdivideBlock3d(
                channels=model_channels,
                out_channels=model_channels // 4,
                use_checkpoint=use_checkpoint,
            ),
            SparseSubdivideBlock3d(
                channels=model_channels // 4,
                out_channels=model_channels // 8,
                use_checkpoint=use_checkpoint,
            ),
            SparseSubdivideBlock3d(
                channels=model_channels // 8,
                out_channels=model_channels // 16,
                use_checkpoint=use_checkpoint,
            )
        ])

        self.out_layer = sp.SparseLinear(model_channels // 16, self.out_channels)
        self.out_active = sp.SparseTanh()

        self.initialize_weights()
        if use_fp16:
            self.convert_to_fp16()

    def initialize_weights(self) -> None:
        super().initialize_weights()
        # Zero-out output layers:
        nn.init.constant_(self.out_layer.weight, 0)
        nn.init.constant_(self.out_layer.bias, 0)

    def convert_to_fp16(self) -> None:
        """
        Convert the torso of the model to float16.
        """
        super().convert_to_fp16()
        self.upsample.apply(convert_module_to_f16)

    def convert_to_fp32(self) -> None:
        """
        Convert the torso of the model to float32.
        """
        super().convert_to_fp32()
        self.upsample.apply(convert_module_to_f32)  
    
    @torch.no_grad()
    def split_for_meshing(self, x: sp.SparseTensor, chunk_size=4, padding=4):
        
        sub_resolution = self.resolution // chunk_size
        upsample_ratio = 8 # hard-coded here
        assert sub_resolution % padding == 0
        out = []
        
        for i in range(chunk_size):
            for j in range(chunk_size):
                for k in range(chunk_size):
                    # Calculate padded boundaries
                    start_x = max(0, i * sub_resolution - padding)
                    end_x = min((i + 1) * sub_resolution + padding, self.resolution)
                    start_y = max(0, j * sub_resolution - padding)
                    end_y = min((j + 1) * sub_resolution + padding, self.resolution)
                    start_z = max(0, k * sub_resolution - padding)
                    end_z = min((k + 1) * sub_resolution + padding, self.resolution)
                    
                    # Store original (unpadded) boundaries for later cropping
                    orig_start_x = i * sub_resolution
                    orig_end_x = (i + 1) * sub_resolution
                    orig_start_y = j * sub_resolution
                    orig_end_y = (j + 1) * sub_resolution
                    orig_start_z = k * sub_resolution
                    orig_end_z = (k + 1) * sub_resolution

                    mask = torch.logical_and(
                        torch.logical_and(
                            torch.logical_and(x.coords[:, 1] >= start_x, x.coords[:, 1] < end_x),
                            torch.logical_and(x.coords[:, 2] >= start_y, x.coords[:, 2] < end_y)
                        ),
                        torch.logical_and(x.coords[:, 3] >= start_z, x.coords[:, 3] < end_z)
                    )

                    if mask.sum() > 0:
                        # Get the coordinates and shift them to local space
                        coords = x.coords[mask].clone()
                        # Shift to local coordinates
                        coords[:, 1:] = coords[:, 1:] - torch.tensor([start_x, start_y, start_z], 
                                                                    device=coords.device).view(1, 3)

                        chunk_tensor = sp.SparseTensor(x.feats[mask], coords)
                        # Store the boundaries and offsets as metadata for later reconstruction
                        chunk_tensor.bounds = {
                            'original': (orig_start_x * upsample_ratio, orig_end_x * upsample_ratio + (upsample_ratio - 1), orig_start_y * upsample_ratio, orig_end_y * upsample_ratio + (upsample_ratio - 1), orig_start_z * upsample_ratio, orig_end_z * upsample_ratio + (upsample_ratio - 1)),
                            'offsets': (start_x * upsample_ratio, start_y * upsample_ratio, start_z * upsample_ratio)  # Store offsets for reconstruction
                        }
                        out.append(chunk_tensor)

                    del mask
                    torch.cuda.empty_cache()
        return out
    
    @torch.no_grad()
    def split_single_chunk(self, x: sp.SparseTensor, chunk_size=4, padding=4):
        sub_resolution = self.resolution // chunk_size
        upsample_ratio = 8 # hard-coded here
        assert sub_resolution % padding == 0

        mask_sum = -1
        while mask_sum < 1:
            orig_start_x = random.randint(0, self.resolution - sub_resolution)
            orig_end_x = orig_start_x + sub_resolution
            orig_start_y = random.randint(0, self.resolution - sub_resolution)
            orig_end_y = orig_start_y + sub_resolution
            orig_start_z = random.randint(0, self.resolution - sub_resolution)
            orig_end_z = orig_start_z + sub_resolution
            start_x = max(0, orig_start_x - padding)
            end_x = min(orig_end_x + padding, self.resolution)
            start_y = max(0, orig_start_y - padding)
            end_y = min(orig_end_y + padding, self.resolution)
            start_z = max(0, orig_start_z - padding)
            end_z = min(orig_end_z + padding, self.resolution)

            mask_ori = torch.logical_and(
                torch.logical_and(
                    torch.logical_and(x.coords[:, 1] >= orig_start_x, x.coords[:, 1] < orig_end_x),
                    torch.logical_and(x.coords[:, 2] >= orig_start_y, x.coords[:, 2] < orig_end_y)
                ),
                torch.logical_and(x.coords[:, 3] >= orig_start_z, x.coords[:, 3] < orig_end_z)
            )
            mask_sum = mask_ori.sum()

        # Store the boundaries and offsets as metadata for later reconstruction
        bounds = {
            'original': (orig_start_x * upsample_ratio, orig_end_x * upsample_ratio + (upsample_ratio - 1), orig_start_y * upsample_ratio, orig_end_y * upsample_ratio + (upsample_ratio - 1), orig_start_z * upsample_ratio, orig_end_z * upsample_ratio + (upsample_ratio - 1)),
            'start': (start_x, end_x, start_y, end_y, start_z, end_z),
            'offsets': (start_x * upsample_ratio, start_y * upsample_ratio, start_z * upsample_ratio)  # Store offsets for reconstruction
        }
        return bounds
    
    def forward_single_chunk(self, x: sp.SparseTensor, padding=4):
        
        bounds = self.split_single_chunk(x, self.chunk_size, padding=padding)

        start_x, end_x, start_y, end_y, start_z, end_z = bounds['start']
        mask = torch.logical_and(
            torch.logical_and(
                torch.logical_and(x.coords[:, 1] >= start_x, x.coords[:, 1] < end_x),
                torch.logical_and(x.coords[:, 2] >= start_y, x.coords[:, 2] < end_y)
            ),
            torch.logical_and(x.coords[:, 3] >= start_z, x.coords[:, 3] < end_z)
        )

        # Shift to local coordinates
        coords = x.coords.clone()
        coords[:, 1:] = coords[:, 1:] - torch.tensor([start_x, start_y, start_z], 
                                                    device=coords.device).view(1, 3)

        chunk = sp.SparseTensor(x.feats[mask], coords[mask])
    
        chunk_result = self.upsamples(chunk)

        coords = chunk_result.coords.clone()

        # Restore global coordinates
        offsets = torch.tensor(bounds['offsets'], 
                                device=coords.device).view(1, 3)
        coords[:, 1:] = coords[:, 1:] + offsets

        # Filter points within original bounds
        original = bounds['original']
        within_bounds = torch.logical_and(
            torch.logical_and(
                torch.logical_and(
                    coords[:, 1] >= original[0],
                    coords[:, 1] < original[1]
                ),
                torch.logical_and(
                    coords[:, 2] >= original[2],
                    coords[:, 2] < original[3]
                )
            ),
            torch.logical_and(
                coords[:, 3] >= original[4],
                coords[:, 3] < original[5]
            )
        )

        final_coords = coords[within_bounds]
        final_feats = chunk_result.feats[within_bounds]

        return sp.SparseTensor(final_feats, final_coords)

    def upsamples(self, x, return_feat: bool = False):
        dtype = x.dtype
        for block in self.upsample:
            x = block(x)
        x = x.type(dtype)

        output = self.out_active(self.out_layer(x))

        if return_feat:
            return output, x
        else:
            return output
    
    def forward(self, x: sp.SparseTensor, factor: float = None, return_feat: bool = False):
        h = super().forward(x, factor)
        if self.chunk_size <= 1:
            for block in self.upsample:
                h = block(h)
            h = h.type(x.dtype)

            if return_feat:
                return self.out_active(self.out_layer(h)), h
        
            h = self.out_layer(h)
            h = self.out_active(h)
            return h
        else:
            if self.training:
                return self.forward_single_chunk(h)
            else:
                batch_size = x.shape[0]                
                chunks = self.split_for_meshing(h, chunk_size=self.chunk_size)
                all_coords, all_feats = [], []
                for chunk_idx, chunk in enumerate(chunks):
                    chunk_result = self.upsamples(chunk)

                    for b in range(batch_size):
                        mask = torch.nonzero(chunk_result.coords[:, 0] == b).squeeze(-1)
                        if mask.numel() > 0:
                            coords = chunk_result.coords[mask].clone()

                            # Restore global coordinates
                            offsets = torch.tensor(chunk.bounds['offsets'], 
                                                    device=coords.device).view(1, 3)
                            coords[:, 1:] = coords[:, 1:] + offsets

                            # Filter points within original bounds
                            bounds = chunk.bounds['original']
                            within_bounds = torch.logical_and(
                                torch.logical_and(
                                    torch.logical_and(
                                        coords[:, 1] >= bounds[0],
                                        coords[:, 1] < bounds[1]
                                    ),
                                    torch.logical_and(
                                        coords[:, 2] >= bounds[2],
                                        coords[:, 2] < bounds[3]
                                    )
                                ),
                                torch.logical_and(
                                    coords[:, 3] >= bounds[4],
                                    coords[:, 3] < bounds[5]
                                )
                            )
                            
                            if within_bounds.any():
                                all_coords.append(coords[within_bounds])
                                all_feats.append(chunk_result.feats[mask][within_bounds])
                    
                    if not self.training:
                        torch.cuda.empty_cache()

                final_coords = torch.cat(all_coords)
                final_feats = torch.cat(all_feats)
                
                return sp.SparseTensor(final_feats, final_coords)