File size: 7,922 Bytes
df12227
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# InteriorFusion Training Guide

## Hardware Requirements

| Stage | GPUs | VRAM Each | Duration | Cost (Cloud) |
|-------|------|-----------|----------|-------------|
| VAE Pre-training | 8× A100 (80GB) | 80GB | 7 days | ~$15K |
| Structure DiT | 32× A100 (80GB) | 80GB | 14 days | ~$30K |
| Material DiT | 16× A100 (80GB) | 80GB | 7 days | ~$15K |
| Fine-tuning | 8× A100 (80GB) | 80GB | 3 days | ~$5K |
| **Total** | **Variable** | — | **~4 weeks** | **~$65K** |

Minimum viable: 8× A100 (all stages, longer duration)
Budget option: 8× RTX 4090 (48GB) — requires gradient accumulation, ~2× longer

## Stage 1: SLAT-Interior VAE Pre-training

### Architecture
- **Encoder**: Sparse 3D convolutional U-Net
  - Input: Dense occupancy grid O ∈ {0,1}^N³ where N=256/512/1024
  - Sparse convolution layers with channel-to-space shortcuts
  - 16× spatial compression (1024³ → 64³ latent)
  
- **Decoder**: 
  - Sparse conv upsampler with skip connections
  - Early-pruning: predict binary mask for active children before upsampling
  - Outputs: per-voxel shape features + material features

### Training Configuration
```yaml
# configs/vae_pretrain.yaml
model:
  latent_dim: 64
  base_resolution: 256
  target_resolution: 1024
  
optimizer:
  type: AdamW
  lr: 1.0e-4
  weight_decay: 0.01
  betas: [0.9, 0.999]

scheduler:
  type: cosine_with_restarts
  warmup_steps: 10000
  
training:
  batch_size: 8  # per GPU
  num_gpus: 8
  effective_batch_size: 64
  max_steps: 200000
  gradient_accumulation: 1
  mixed_precision: bf16
  
  curriculum:
    - resolution: 256
      steps: 50000
      lr: 1.0e-4
    - resolution: 512
      steps: 100000
      lr: 1.0e-4
    - resolution: 1024
      steps: 50000
      lr: 5.0e-5

data:
  dataset: InteriorFusion-Train
  num_workers: 8
  pin_memory: true
  
loss:
  reconstruction:
    weight: 1.0
    type: l1
  kl_divergence:
    weight: 1.0e-3
  depth_consistency:
    weight: 0.5
    type: l1
  normal_consistency:
    weight: 0.3
    type: cosine
  edge_preservation:
    weight: 0.2
    type: l1
```

### Loss Functions

```python
def vae_loss(pred_shape, pred_material, target_shape, target_material, 
             pred_depth, target_depth, pred_normal, target_normal, mu, logvar):
    
    # Reconstruction
    loss_recon = F.l1_loss(pred_shape, target_shape) + \
                 F.l1_loss(pred_material, target_material)
    
    # KL divergence
    loss_kl = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
    loss_kl = loss_kl * 1e-3
    
    # Depth consistency
    loss_depth = F.l1_loss(pred_depth, target_depth)
    
    # Normal consistency
    loss_normal = 1 - F.cosine_similarity(pred_normal, target_normal, dim=-1).mean()
    
    return loss_recon + loss_kl + 0.5 * loss_depth + 0.3 * loss_normal
```

## Stage 2: Structure DiT (Rectified Flow)

### Architecture
- **DiT model**: Flow-matching transformer
  - Width: 1536
  - Depth: 30 blocks
  - Heads: 12
  - MLP ratio: 8192
  - Parameters: ~1.3B
  
- **Conditioning encoders**:
  - Image: DINOv3-L (frozen, 1024-dim features)
  - Depth: Custom CNN encoder (256-dim)
  - Layout: Transformer encoder on SpatialLM tokens (512-dim)
  - Semantic: Mask2Former feature pyramid (256-dim)
  
- **Conditioning fusion**: Cross-attention + AdaLN-single modulation

### Training Configuration
```yaml
# configs/dit_structure.yaml
model:
  width: 1536
  depth: 30
  num_heads: 12
  mlp_ratio: 8192
  
optimizer:
  type: AdamW
  lr: 1.0e-4
  weight_decay: 0.01

scheduler:
  type: linear_warmup_cosine
  warmup_steps: 10000
  
training:
  batch_size: 8  # per GPU
  num_gpus: 32
  effective_batch_size: 256
  max_steps: 400000
  mixed_precision: bf16
  
  curriculum:
    - resolution: 256
      steps: 100000
      lr: 1.0e-4
    - resolution: 512
      steps: 200000
      lr: 1.0e-4
    - resolution: 1024
      steps: 100000
      lr: 2.0e-5

data:
  dataset: InteriorFusion-Train
  num_workers: 8
  
flow_matching:
  sigma_min: 0.001
  sigma_max: 80.0
  p_mean: -1.2
  p_std: 1.2
  
loss:
  flow_matching:
    weight: 1.0
  depth_guidance:
    weight: 0.3
```

### Flow Matching Loss

```python
def flow_matching_loss(model, x_1, cond_img, cond_depth, cond_layout, cond_semantic):
    """
    Rectified flow matching for 3D generation.
    x_1: target structured latent (from VAE encoder)
    """
    # Sample noise
    x_0 = torch.randn_like(x_1)
    
    # Sample timestep
    t = torch.rand(x_1.shape[0], device=x_1.device)
    
    # Interpolate
    x_t = (1 - t[:, None, None, None]) * x_0 + t[:, None, None, None] * x_1
    
    # Model predicts velocity
    v_pred = model(x_t, t, cond_img, cond_depth, cond_layout, cond_semantic)
    
    # Target velocity
    v_target = x_1 - x_0
    
    # MSE loss
    loss = F.mse_loss(v_pred, v_target)
    
    return loss
```

## Stage 3: Material DiT

### Architecture
- Same DiT backbone as Stage 2
- Additional conditioning: generated geometry latent
- Output: per-voxel material features (albedo RGB, metallic, roughness, normal XYZ)

### Training
```yaml
# configs/dit_material.yaml
training:
  batch_size: 16  # per GPU
  num_gpus: 16
  effective_batch_size: 256
  max_steps: 200000
  
loss:
  albedo:
    weight: 1.0
    type: l1
  metallic_roughness:
    weight: 0.5
    type: l1
  normal:
    weight: 0.5
    type: cosine
  perceptual:
    weight: 0.3
    type: lpips
    network: vgg
  rendering:
    weight: 0.5
    type: mse  # rendered vs ground truth
```

## Stage 4: Real-World Fine-tuning

### LoRA Configuration
```yaml
# configs/finetune_lora.yaml
lora:
  rank: 32
  alpha: 32
  target_modules:
    - "attention.qkv"
    - "attention.proj"
    - "mlp.fc1"
    - "mlp.fc2"
  dropout: 0.0

training:
  batch_size: 4
  num_gpus: 8
  max_steps: 50000
  lr: 1.0e-5
  
data:
  dataset: InteriorFusion-Real  # ScanNet + HM3D
  weight: 1.0
```

### RL Fine-tuning (Optional)
```yaml
# configs/rl_finetune.yaml
rl:
  algorithm: GRPO
  group_size: 8
  reward_weights:
    depth_consistency: 0.25
    point_cloud_consistency: 0.25
    pose_stability: 0.25
    edit_quality: 0.25
  
  vggt_model: "microsoft/VGGT-1B"  # For geometric rewards
  
training:
  num_iterations: 10000
  lr: 1.0e-6
  kl_penalty: 0.01
```

## Distributed Training

### Using Accelerate / DeepSpeed
```bash
# Launch with DeepSpeed ZeRO-3
accelerate launch --config_file configs/accelerate_deepspeed.yaml \
    scripts/train_vae.py --config configs/vae_pretrain.yaml
```

```yaml
# configs/accelerate_deepspeed.yaml
deep_speed_config:
  zero_stage: 3
  offload_optimizer_device: none
  offload_param_device: none
  gradient_accumulation_steps: 1
  gradient_clipping: 1.0
  train_batch_size: auto
  train_micro_batch_size_per_gpu: auto
```

### LR Scaling for Distributed Training
Following Grendel-GS:
```python
def scale_lr_for_distributed(base_lr, batch_size):
    """Square root scaling for distributed training."""
    return base_lr * math.sqrt(batch_size)

def scale_adam_betas_for_distributed(beta1, beta2, batch_size):
    """Exponential momentum scaling."""
    return beta1 ** batch_size, beta2 ** batch_size
```

## Checkpointing & Resumption

```python
checkpoint = {
    'model': model.state_dict(),
    'optimizer': optimizer.state_dict(),
    'scheduler': scheduler.state_dict(),
    'step': step,
    'epoch': epoch,
    'best_val_loss': best_val_loss,
    'config': OmegaConf.to_container(config),
}

torch.save(checkpoint, f'checkpoints/stage1_step{step}.pt')
```

## Validation Metrics

| Metric | Target | How to Compute |
|--------|--------|---------------|
| Chamfer Distance | < 0.01 | Point cloud comparison |
| F-Score @ 0.1 | > 0.80 | Precision/recall on surface |
| LPIPS | < 0.06 | Perceptual similarity |
| PSNR | > 28 | Rendering quality |
| SSIM | > 0.90 | Structural similarity |
| Layout IoU | > 0.85 | Room layout accuracy |
| Object Detection mAP | > 0.70 | Furniture detection |
| Scale Error | < 5% | Metric depth consistency |