File size: 11,366 Bytes
8b16bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from collections.abc import Sequence

import torch
from torch import nn


def _activation(name: str) -> nn.Module:
    activations = {
        "gelu": nn.GELU,
        "relu": nn.ReLU,
        "silu": nn.SiLU,
        "swish": nn.SiLU,
        "tanh": nn.Tanh,
    }
    try:
        return activations[name.lower()]()
    except KeyError as error:
        raise ValueError(f"Unsupported activation: {name}") from error


class StandardMLP(nn.Module):
    """Pointwise MLP with state-dict names compatible with OneScience."""

    def __init__(
        self,
        input_dim: int,
        output_dim: int,
        hidden_dims: Sequence[int],
        activation: str = "gelu",
        use_bias: bool = True,
    ) -> None:
        super().__init__()
        dimensions = [int(input_dim), *(int(value) for value in hidden_dims), int(output_dim)]
        self.layers = nn.ModuleList(
            nn.Linear(in_features, out_features, bias=use_bias)
            for in_features, out_features in zip(dimensions[:-1], dimensions[1:])
        )
        self.activation = _activation(activation)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        for layer in self.layers[:-1]:
            x = self.activation(layer(x))
        return self.layers[-1](x)


class OneMlp(nn.Module):
    """Small compatibility wrapper used by released OneScience checkpoints."""

    def __init__(self, **kwargs) -> None:
        super().__init__()
        self.mlp = StandardMLP(**kwargs)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.mlp(x)


class _PoolingReducer(nn.Module):
    def __init__(self, in_dim: int, hidden_dim: int, out_dim: int) -> None:
        super().__init__()
        self.to_in = nn.Linear(in_dim, hidden_dim, bias=False)
        self.out_ffn = nn.Sequential(
            nn.LayerNorm(hidden_dim),
            nn.Linear(hidden_dim, hidden_dim),
            nn.GELU(),
            nn.Linear(hidden_dim, out_dim),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.to_in(x)
        if x.ndim > 3:
            x = x.mean(dim=tuple(range(2, x.ndim - 1)))
        return self.out_ffn(x)


class _SwapGridAxes(nn.Module):
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return x.permute(0, 2, 1, 3)


class _FactAttnWeight(nn.Module):
    def __init__(self, heads: int, dim_head: int, dropout: float) -> None:
        super().__init__()
        self.dim_head = int(dim_head)
        self.heads = int(heads)
        self.scale = self.dim_head**-0.5
        self.softmax = nn.Softmax(dim=-1)
        self.dropout = nn.Dropout(dropout)
        self.to_q = nn.Linear(self.dim_head, self.dim_head, bias=False)
        self.to_k = nn.Linear(self.dim_head, self.dim_head, bias=False)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        batch, length, _ = x.shape
        x = x.reshape(batch, length, self.heads, self.dim_head)
        x = x.permute(0, 2, 1, 3).contiguous()
        query = self.to_q(x)
        key = self.to_k(x)
        return self.softmax(torch.matmul(query, key.transpose(-1, -2)) * self.scale)


class FactAttention2D(nn.Module):
    """Factorized axial attention for a structured two-dimensional grid."""

    def __init__(
        self,
        dim: int,
        heads: int,
        dim_head: int,
        dropout: float,
        shapelist: Sequence[int],
    ) -> None:
        super().__init__()
        if len(shapelist) != 2:
            raise ValueError("FactAttention2D requires shapelist=(height, width)")
        if dim != heads * dim_head:
            raise ValueError("dim must equal heads * dim_head")
        self.dim_head = int(dim_head)
        self.heads = int(heads)
        self.H, self.W = (int(value) for value in shapelist)
        self.attn_x = _FactAttnWeight(self.heads, self.dim_head, dropout)
        self.attn_y = _FactAttnWeight(self.heads, self.dim_head, dropout)
        self.to_v = nn.Linear(self.dim_head, self.dim_head, bias=False)
        self.to_x = nn.Sequential(_PoolingReducer(dim, dim, dim))
        self.to_y = nn.Sequential(_SwapGridAxes(), _PoolingReducer(dim, dim, dim))
        self.to_out = nn.Sequential(nn.Linear(dim, dim), nn.Dropout(dropout))

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        batch, length, channels = x.shape
        if length != self.H * self.W:
            raise ValueError(
                f"Sequence length {length} does not match grid {self.H}x{self.W}"
            )
        grid = x.reshape(batch, self.H, self.W, channels).contiguous()
        values = self.to_v(
            grid.reshape(batch, self.H, self.W, self.heads, self.dim_head)
        )
        values = values.permute(0, 3, 1, 2, 4).contiguous()
        result_x = torch.einsum(
            "bhij,bhjmc->bhimc", self.attn_x(self.to_x(grid)), values
        )
        result_y = torch.einsum(
            "bhlm,bhimc->bhilc", self.attn_y(self.to_y(grid)), result_x
        )
        result = result_y.permute(0, 2, 3, 1, 4).contiguous()
        result = result.reshape(batch, length, channels)
        return self.to_out(result)


class OneAttention(nn.Module):
    """Compatibility wrapper retaining the original checkpoint hierarchy."""

    def __init__(self, **kwargs) -> None:
        super().__init__()
        self.attentioner = FactAttention2D(**kwargs)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.attentioner(x)


class FactformerBlock(nn.Module):
    def __init__(
        self,
        num_heads: int,
        hidden_dim: int,
        dropout: float,
        activation: str,
        mlp_ratio: int,
        spatial_shape: Sequence[int],
    ) -> None:
        super().__init__()
        self.ln_1 = nn.LayerNorm(hidden_dim)
        self.Attn = OneAttention(
            dim=hidden_dim,
            heads=num_heads,
            dim_head=hidden_dim // num_heads,
            dropout=dropout,
            shapelist=spatial_shape,
        )
        self.ln_2 = nn.LayerNorm(hidden_dim)
        self.mlp = OneMlp(
            input_dim=hidden_dim,
            output_dim=hidden_dim,
            hidden_dims=[hidden_dim * mlp_ratio],
            activation=activation,
            use_bias=True,
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.Attn(self.ln_1(x)) + x
        return self.mlp(self.ln_2(x)) + x


class OneTransformer(nn.Module):
    """Compatibility wrapper retaining the original checkpoint hierarchy."""

    def __init__(self, **kwargs) -> None:
        super().__init__()
        self.transformer = FactformerBlock(**kwargs)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.transformer(x)


class FactFormer2D(nn.Module):
    """FactFormer neural operator for structured 2D flow fields.

    ``pos`` has shape ``(B, H*W, 2)`` and ``fx`` has shape
    ``(B, H*W, t_in*out_dim)``. The output contains ``latent_steps`` future
    fields concatenated along the final dimension.
    """

    def __init__(
        self,
        in_dim: int,
        out_dim: int,
        spatial_shape: Sequence[int],
        hidden_dim: int = 128,
        depth: int = 4,
        heads: int = 8,
        mlp_ratio: int = 2,
        dropout: float = 0.0,
        activation: str = "gelu",
        include_pos: bool = True,
        space_dim: int = 2,
        latent_multiplier: float = 2.0,
        max_latent_steps: int = 4,
    ) -> None:
        super().__init__()
        self.in_dim = int(in_dim)
        self.out_dim = int(out_dim)
        self.spatial_shape = tuple(int(value) for value in spatial_shape)
        self.hidden_dim = int(hidden_dim)
        self.depth = int(depth)
        self.heads = int(heads)
        self.include_pos = bool(include_pos)
        self.space_dim = int(space_dim)
        self.latent_dim = int(self.hidden_dim * float(latent_multiplier))
        self.max_latent_steps = int(max_latent_steps)

        if len(self.spatial_shape) != 2:
            raise ValueError("FactFormer2D requires a two-dimensional spatial shape")
        if self.hidden_dim % self.heads:
            raise ValueError("hidden_dim must be divisible by heads")
        if self.max_latent_steps < 1:
            raise ValueError("max_latent_steps must be positive")

        input_dim = self.in_dim + (self.space_dim if self.include_pos else 0)
        self.preprocess = OneMlp(
            input_dim=input_dim,
            output_dim=self.hidden_dim,
            hidden_dims=[self.hidden_dim * 2],
            activation=activation,
            use_bias=True,
        )
        self.blocks = nn.ModuleList(
            OneTransformer(
                num_heads=self.heads,
                hidden_dim=self.hidden_dim,
                dropout=float(dropout),
                activation=activation,
                mlp_ratio=int(mlp_ratio),
                spatial_shape=self.spatial_shape,
            )
            for _ in range(self.depth)
        )
        self.expand_latent = nn.Linear(self.hidden_dim, self.latent_dim, bias=False)
        self.latent_time_embedding = nn.Parameter(
            torch.randn(1, self.max_latent_steps, 1, self.latent_dim) * 0.02
        )
        self.propagator = OneMlp(
            input_dim=self.latent_dim,
            output_dim=self.latent_dim,
            hidden_dims=[self.hidden_dim],
            activation=activation,
            use_bias=True,
        )
        self.to_out = OneMlp(
            input_dim=self.latent_dim,
            output_dim=self.out_dim,
            hidden_dims=[self.hidden_dim],
            activation=activation,
            use_bias=True,
        )
        self.initialize_weights()

    def initialize_weights(self) -> None:
        for module in self.modules():
            if isinstance(module, nn.Linear):
                nn.init.trunc_normal_(module.weight, std=0.02)
                if module.bias is not None:
                    nn.init.zeros_(module.bias)
            elif isinstance(module, nn.LayerNorm):
                nn.init.ones_(module.weight)
                nn.init.zeros_(module.bias)

    def forward(
        self,
        pos: torch.Tensor,
        fx: torch.Tensor,
        latent_steps: int = 1,
    ) -> torch.Tensor:
        if fx.ndim != 3:
            raise ValueError(f"fx must have shape (B, N, C), got {tuple(fx.shape)}")
        if pos.ndim == 2:
            pos = pos.unsqueeze(0)
        if pos.shape[0] == 1 and fx.shape[0] > 1:
            pos = pos.expand(fx.shape[0], -1, -1)
        if pos.shape[:2] != fx.shape[:2]:
            raise ValueError(f"Incompatible pos and fx shapes: {pos.shape}, {fx.shape}")
        if not 1 <= int(latent_steps) <= self.max_latent_steps:
            raise ValueError(
                f"latent_steps must be in [1, {self.max_latent_steps}], got {latent_steps}"
            )

        hidden = torch.cat((pos, fx), dim=-1) if self.include_pos else fx
        hidden = self.preprocess(hidden)
        for block in self.blocks:
            hidden = block(hidden)

        latent = self.expand_latent(hidden)
        outputs = []
        for step in range(int(latent_steps)):
            latent = latent + self.latent_time_embedding[:, step]
            latent = self.propagator(latent) + latent
            outputs.append(self.to_out(latent))
        return torch.cat(outputs, dim=-1)


__all__ = ["FactFormer2D"]