File size: 8,554 Bytes
632d805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Prediction heads for the dynamics model: plain MLP, multi-scale, and
residual variants.
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import List, Optional


class DynamicsPredictor(nn.Module):
    """
    MLP predictor that takes state features and query points to predict dynamics.
    """
    
    def __init__(
        self,
        state_dim: int = 512,
        query_dim: int = 3,  # 3D position
        hidden_dims: List[int] = [256, 128, 64],
        output_dim: int = 3,  # 3D acceleration/displacement
        activation: str = 'relu',
        use_batch_norm: bool = False,
        dropout: float = 0.1
    ):
        super().__init__()
        
        self.state_dim = state_dim
        self.query_dim = query_dim
        self.output_dim = output_dim
        
        # Build MLP layers
        layers = []
        input_dim = state_dim + query_dim
        
        for hidden_dim in hidden_dims:
            layers.append(nn.Linear(input_dim, hidden_dim))
            
            if use_batch_norm:
                layers.append(nn.BatchNorm1d(hidden_dim))
            
            if activation == 'relu':
                layers.append(nn.SiLU())
            elif activation == 'gelu':
                layers.append(nn.SiLU())
            elif activation == 'tanh':
                layers.append(nn.Tanh())
            else:
                raise ValueError(f"Unknown activation: {activation}")
            
            if dropout > 0:
                layers.append(nn.Dropout(dropout))
            
            input_dim = hidden_dim
        
        # Output layer
        layers.append(nn.Linear(hidden_dims[-1], output_dim))
        layers.append(nn.Tanh())  # bounded output
        self.output_scale = nn.Parameter(torch.ones(1) * 1.0)
        
        self.mlp = nn.Sequential(*layers)
        
    def forward(
        self,
        state_features: torch.Tensor,
        query_points: torch.Tensor
    ) -> torch.Tensor:
        """
        Predict dynamics for query points given state features.
        
        Args:
            state_features: Enhanced state features (B, N_state, D_state) or (B, D_state)
            query_points: Query vertex positions (B, N_query, 3)
            
        Returns:
            Predicted accelerations/displacements (B, N_query, 3)
        """
        B = query_points.shape[0]
        N_query = query_points.shape[1]
        
        # If state_features has sequence dimension, pool it
        if state_features.dim() == 3:
            # Global max pooling over state tokens
            state_features = torch.max(state_features, dim=1)[0]  # (B, D_state)
        
        # Expand state features to match number of query points
        state_features_expanded = state_features.unsqueeze(1).expand(B, N_query, -1)
        
        # Concatenate state features with query points
        combined = torch.cat([state_features_expanded, query_points], dim=-1)
        
        # Reshape for batch processing through MLP
        combined_flat = combined.view(B * N_query, -1)
        
        # Apply MLP
        output_flat = self.mlp(combined_flat) * self.output_scale
        
        # Reshape back
        output = output_flat.view(B, N_query, self.output_dim)
        
        return output


class MultiScalePredictor(nn.Module):
    """
    Multi-scale predictor that processes features at different resolutions.
    """
    
    def __init__(
        self,
        state_dim: int = 512,
        query_dim: int = 3,
        scales: List[int] = [64, 128, 256],
        output_dim: int = 3,
        activation: str = 'relu',
        dropout: float = 0.1
    ):
        super().__init__()
        
        self.scales = scales
        
        # Create predictors for each scale
        self.scale_predictors = nn.ModuleList([
            DynamicsPredictor(
                state_dim=state_dim,
                query_dim=query_dim,
                hidden_dims=[scale, scale // 2],
                output_dim=output_dim,
                activation=activation,
                dropout=dropout
            ) for scale in scales
        ])
        
        # Fusion layer
        self.fusion = nn.Linear(output_dim * len(scales), output_dim)
        
    def forward(
        self,
        state_features: torch.Tensor,
        query_points: torch.Tensor
    ) -> torch.Tensor:
        """
        Multi-scale prediction with fusion.
        """
        predictions = []
        
        for predictor in self.scale_predictors:
            pred = predictor(state_features, query_points)
            predictions.append(pred)
        
        # Concatenate all scale predictions
        combined = torch.cat(predictions, dim=-1)
        
        # Fuse predictions
        output = self.fusion(combined)
        
        return output


class ResidualPredictor(nn.Module):
    """
    Predictor with residual connections for better gradient flow.
    """
    
    def __init__(
        self,
        state_dim: int = 512,
        query_dim: int = 3,
        hidden_dim: int = 256,
        num_layers: int = 3,
        output_dim: int = 3,
        activation: str = 'relu',
        dropout: float = 0.1
    ):
        super().__init__()
        
        self.input_projection = nn.Linear(state_dim + query_dim, hidden_dim)
        
        # Residual blocks
        self.res_blocks = nn.ModuleList([
            ResidualBlock(hidden_dim, activation, dropout)
            for _ in range(num_layers)
        ])
        
        self.output_projection = nn.Linear(hidden_dim, output_dim)
        
    def forward(
        self,
        state_features: torch.Tensor,
        query_points: torch.Tensor
    ) -> torch.Tensor:
        """Forward pass with residual connections."""
        B = query_points.shape[0]
        N_query = query_points.shape[1]
        
        # Pool state features if needed
        if state_features.dim() == 3:
            state_features = torch.max(state_features, dim=1)[0]
        
        # Expand and concatenate
        state_expanded = state_features.unsqueeze(1).expand(B, N_query, -1)
        combined = torch.cat([state_expanded, query_points], dim=-1)
        
        # Initial projection
        x = self.input_projection(combined)
        
        # Apply residual blocks
        for block in self.res_blocks:
            x = block(x)
        
        # Output projection
        output = self.output_projection(x)
        
        return output


class ResidualBlock(nn.Module):
    """Single residual block."""
    
    def __init__(self, hidden_dim: int, activation: str = 'relu', dropout: float = 0.1):
        super().__init__()
        
        self.fc1 = nn.Linear(hidden_dim, hidden_dim)
        self.fc2 = nn.Linear(hidden_dim, hidden_dim)
        
        if activation == 'relu':
            self.activation = nn.ReLU()
        elif activation == 'gelu':
            self.activation = nn.GELU()
        else:
            self.activation = nn.ReLU()
        
        self.dropout = nn.Dropout(dropout)
        self.norm1 = nn.LayerNorm(hidden_dim)
        self.norm2 = nn.LayerNorm(hidden_dim)
        
    def forward(self, x):
        residual = x
        
        x = self.norm1(x)
        x = self.fc1(x)
        x = self.activation(x)
        x = self.dropout(x)
        
        x = self.norm2(x)
        x = self.fc2(x)
        x = self.dropout(x)
        
        return x + residual


if __name__ == "__main__":
    """Test predictor modules."""
    
    print("Testing DynamicsPredictor...")
    predictor = DynamicsPredictor(
        state_dim=512,
        query_dim=3,
        hidden_dims=[256, 128, 64],
        output_dim=3
    )
    
    # Test inputs
    batch_size = 4
    state_features = torch.randn(batch_size, 10, 512)  # With sequence dimension
    query_points = torch.randn(batch_size, 8, 3)  # 8 vertices
    
    output = predictor(state_features, query_points)
    print(f"Output shape: {output.shape}")  # Should be (4, 8, 3)
    
    print("\nTesting MultiScalePredictor...")
    multi_predictor = MultiScalePredictor(state_dim=512, query_dim=3)
    output_multi = multi_predictor(state_features, query_points)
    print(f"Multi-scale output shape: {output_multi.shape}")  # (4, 8, 3)
    
    print("\nTesting ResidualPredictor...")
    res_predictor = ResidualPredictor(state_dim=512, query_dim=3)
    output_res = res_predictor(state_features, query_points)
    print(f"Residual output shape: {output_res.shape}")  # (4, 8, 3)
    
    print("\nPredictor tests successful!")