File size: 8,928 Bytes
ff0fadf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

from onescience.modules.equivariant.group_conv import GroupEquivariantConv2d
from onescience.modules.fourier.group_spectral import GSpectralConv2d
from onescience.modules.mlp.GMLP import GroupEquivariantMLP2d
from onescience.modules.mlp.MLP import StandardMLP
from onescience.modules.embedding import timestep_embedding, unified_pos_embedding
from onescience.modules.fourier.geo_spectral import GeoSpectralConv2d, IPHI

class GNorm(nn.Module):
    def __init__(self, width, group_size):
        super().__init__()
        self.group_size = group_size
        self.norm = torch.nn.InstanceNorm3d(width)

    def forward(self, x):
        # x shape: (B, C*Group, H, W) -> (B, C, Group, H, W)
        x = x.view(x.shape[0], -1, self.group_size, x.shape[-2], x.shape[-1])
        x = self.norm(x)
        x = x.view(x.shape[0], -1, x.shape[-2], x.shape[-1])
        return x

class Model(nn.Module):
    """
    GFNO (Group Factorized Neural Operator) 模型。
    
    """
    def __init__(self, args, device, s1=96, s2=96):
        super(Model, self).__init__()
        self.__name__ = "GFNO"
        self.args = args
        self.device = device
        
        self.in_channels = args.fun_dim
        self.out_channels = args.out_dim
        self.modes = args.modes
        self.width = args.n_hidden

        reflection = False

        # 1. Embedding & Preprocessing
        if args.unified_pos and args.geotype != "unstructured":
            self.pos = unified_pos_embedding(args.shapelist, args.ref, device=device)
            in_dim = args.fun_dim + args.ref ** len(args.shapelist)
        else:
            in_dim = args.fun_dim + args.space_dim
        
        self.preprocess = StandardMLP(
            input_dim=in_dim,
            output_dim=args.n_hidden,
            hidden_dims=[args.n_hidden * 2],
            activation=args.act,
            use_bias=True
        )

        # 2. Time Embedding
        if args.time_input:
            self.time_fc = nn.Sequential(
                nn.Linear(args.n_hidden, args.n_hidden),
                nn.SiLU(),
                nn.Linear(args.n_hidden, args.n_hidden),
            )
        else:
            self.time_fc = None

        # 3. Geometry Projection & Output Heads
        if args.geotype == "unstructured":
            self.group_size = 4 * (1 + reflection)
            
            self.fftproject_in = GeoSpectralConv2d(
                in_channels=args.n_hidden,
                out_channels=args.n_hidden,
                modes1=args.modes,
                modes2=args.modes,
                s1=s1,
                s2=s2,
            )
            self.fftproject_out = GeoSpectralConv2d(
                in_channels=self.width * self.group_size,
                out_channels=self.width,
                modes1=args.modes,
                modes2=args.modes,
                s1=s1,
                s2=s2,
            )
            self.iphi = IPHI()
            
            grid_h, grid_w = s1, s2
            self.point_q = nn.Sequential(
                nn.Linear(self.width, self.width * 4),
                nn.GELU(),
                nn.Linear(self.width * 4, self.out_channels),
            )
        else:
            grid_h, grid_w = args.shapelist
            # 结构化输出头 (Explicit instantiation)
            self.q = GroupEquivariantMLP2d(
                in_channels=self.width,
                out_channels=self.out_channels,
                mid_channels=self.width * 4,
                reflection=reflection,
                last_layer=True,
            )

        self.padding = [(16 - size % 16) % 16 for size in [grid_h, grid_w]]

        # 4. GFNO Stem Layers
        # Lifting Layer
        self.p = GroupEquivariantConv2d(
            in_channels=args.n_hidden,
            out_channels=self.width,
            kernel_size=1,
            reflection=reflection,
            first_layer=True,
        )
        
        # Spectral Layers
        self.conv0 = GSpectralConv2d(in_channels=self.width, out_channels=self.width, modes=self.modes, reflection=reflection)
        self.conv1 = GSpectralConv2d(in_channels=self.width, out_channels=self.width, modes=self.modes, reflection=reflection)
        self.conv2 = GSpectralConv2d(in_channels=self.width, out_channels=self.width, modes=self.modes, reflection=reflection)
        self.conv3 = GSpectralConv2d(in_channels=self.width, out_channels=self.width, modes=self.modes, reflection=reflection)
        
        self.mlp0 = GroupEquivariantMLP2d(
            in_channels=self.width,
            out_channels=self.width,
            mid_channels=self.width,
            reflection=reflection
        )
        self.mlp1 = GroupEquivariantMLP2d(
            in_channels=self.width,
            out_channels=self.width,
            mid_channels=self.width,
            reflection=reflection
        )
        self.mlp2 = GroupEquivariantMLP2d(
            in_channels=self.width,
            out_channels=self.width,
            mid_channels=self.width,
            reflection=reflection
        )
        self.mlp3 = GroupEquivariantMLP2d(
            in_channels=self.width,
            out_channels=self.width,
            mid_channels=self.width,
            reflection=reflection
        )
        
        # Residual Weights (1x1 GConv)
        self.w0 = GroupEquivariantConv2d(
            in_channels=self.width,
            out_channels=self.width,
            kernel_size=1,
            reflection=reflection
        )
        self.w1 = GroupEquivariantConv2d(
            in_channels=self.width,
            out_channels=self.width,
            kernel_size=1,
            reflection=reflection
        )
        self.w2 = GroupEquivariantConv2d(
            in_channels=self.width,
            out_channels=self.width,
            kernel_size=1,
            reflection=reflection
        )
        self.w3 = GroupEquivariantConv2d(
            in_channels=self.width,
            out_channels=self.width,
            kernel_size=1,
            reflection=reflection
        )
        
        self.norm = GNorm(self.width, group_size=4 * (1 + reflection))

    def _gfno_stem_forward(self, x):
        # x: (B, C, H, W)
        x = self.p(x) # Lifting

        # 4 Layers
        x1 = self.norm(self.conv0(self.norm(x)))
        x1 = self.mlp0(x1)
        x = F.gelu(x1 + self.w0(x))
        
        x1 = self.norm(self.conv1(self.norm(x)))
        x1 = self.mlp1(x1)
        x = F.gelu(x1 + self.w1(x))
        
        x1 = self.norm(self.conv2(self.norm(x)))
        x1 = self.mlp2(x1)
        x = F.gelu(x1 + self.w2(x))
        
        x1 = self.norm(self.conv3(self.norm(x)))
        x1 = self.mlp3(x1)
        x = x1 + self.w3(x)
        return x

    def structured_geo(self, x, fx, T=None):
        B, N, _ = x.shape

        if self.args.unified_pos:
            pos = self.pos.repeat(B, 1, 1)
            feats = torch.cat([pos, fx], dim=-1) if fx is not None else pos
        else:
            feats = torch.cat([x, fx], dim=-1) if fx is not None else x

        feats = self.preprocess(feats) 

        if (T is not None) and (self.time_fc is not None):
            t_emb = timestep_embedding(T, self.args.n_hidden)
            t_emb = self.time_fc(t_emb)
            if t_emb.ndim == 2:
                t_emb = t_emb.unsqueeze(1)
            feats = feats + t_emb # Broadcast

        H, W = self.args.shapelist
        xg = feats.permute(0, 2, 1).reshape(B, self.args.n_hidden, H, W)

        if not all(p == 0 for p in self.padding):
            xg = F.pad(xg, [0, self.padding[1], 0, self.padding[0]])

        xg = self._gfno_stem_forward(xg)

        if not all(p == 0 for p in self.padding):
            xg = xg[..., : -self.padding[0], : -self.padding[1]]

        xg = self.q(xg) 
        out = xg.reshape(B, self.out_channels, -1).permute(0, 2, 1)
        return out

    def unstructured_geo(self, x, fx, T=None):
        B, N, _ = x.shape

        feats = torch.cat([x, fx], dim=-1) if fx is not None else x
        feats = self.preprocess(feats) 

        if (T is not None) and (self.time_fc is not None):
            t_emb = timestep_embedding(T, self.args.n_hidden)
            t_emb = self.time_fc(t_emb)
            if t_emb.ndim == 2:
                t_emb = t_emb.unsqueeze(1)
            feats = feats + t_emb

        xg = self.fftproject_in(
            feats.permute(0, 2, 1), x_in=x, iphi=self.iphi, code=None
        )

        xg = self._gfno_stem_forward(xg)

        xp = self.fftproject_out(xg, x_out=x, iphi=self.iphi, code=None).permute(
            0, 2, 1
        ) 

        out = self.point_q(xp) 
        return out

    def forward(self, x, fx=None, T=None, geo=None):
        if self.args.geotype == "unstructured":
            return self.unstructured_geo(x, fx, T)
        else:
            return self.structured_geo(x, fx, T)