File size: 4,815 Bytes
4d99a96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Fourier Neural Operator baseline for FlowBench LDC_NS_2D data."""

from __future__ import annotations

from typing import Iterable, Sequence

import torch
from torch import nn
import torch.nn.functional as F


class SpectralConv2d(nn.Module):
    """2D Fourier convolution using a truncated set of low-frequency modes."""

    def __init__(self, in_channels: int, out_channels: int, n_modes: Sequence[int]) -> None:
        super().__init__()
        if len(n_modes) != 2:
            raise ValueError("n_modes must contain [modes_height, modes_width].")
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.modes_height = int(n_modes[0])
        self.modes_width = int(n_modes[1])
        scale = 1.0 / max(1, in_channels * out_channels)
        shape = (in_channels, out_channels, self.modes_height, self.modes_width)
        self.weights_pos = nn.Parameter(scale * torch.randn(*shape, dtype=torch.cfloat))
        self.weights_neg = nn.Parameter(scale * torch.randn(*shape, dtype=torch.cfloat))

    @staticmethod
    def compl_mul2d(inputs: torch.Tensor, weights: torch.Tensor) -> torch.Tensor:
        return torch.einsum("bixy,ioxy->boxy", inputs, weights)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        batch_size, _, height, width = x.shape
        x_ft = torch.fft.rfft2(x, norm="ortho")
        out_ft = torch.zeros(
            batch_size,
            self.out_channels,
            height,
            width // 2 + 1,
            dtype=torch.cfloat,
            device=x.device,
        )
        mh = min(self.modes_height, height)
        mw = min(self.modes_width, width // 2 + 1)
        out_ft[:, :, :mh, :mw] = self.compl_mul2d(
            x_ft[:, :, :mh, :mw], self.weights_pos[:, :, :mh, :mw]
        )
        out_ft[:, :, -mh:, :mw] = self.compl_mul2d(
            x_ft[:, :, -mh:, :mw], self.weights_neg[:, :, :mh, :mw]
        )
        return torch.fft.irfft2(out_ft, s=(height, width), norm="ortho")


class FNOBlock2d(nn.Module):
    def __init__(self, channels: int, n_modes: Sequence[int]) -> None:
        super().__init__()
        self.spectral = SpectralConv2d(channels, channels, n_modes)
        self.pointwise = nn.Conv2d(channels, channels, kernel_size=1)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.spectral(x) + self.pointwise(x)


class FlowBenchFNO(nn.Module):
    """FNO baseline for mapping FlowBench LDC inputs to dense output fields.

    Expected input shape is ``[batch, in_channels, height, width]``.
    The default constructor arguments match the requested FlowBench baseline:
    ``n_modes=[32, 32]``, ``n_layers=5``, ``in_channels=3``,
    ``out_channels=4``, ``hidden_channels=32``, and
    ``projection_channels=128``.
    """

    def __init__(
        self,
        n_modes: Sequence[int] | Iterable[int] = (32, 32),
        n_layers: int = 5,
        in_channels: int = 3,
        out_channels: int = 4,
        hidden_channels: int = 32,
        projection_channels: int = 128,
    ) -> None:
        super().__init__()
        self.n_modes = list(n_modes)
        self.n_layers = int(n_layers)
        self.in_channels = int(in_channels)
        self.out_channels = int(out_channels)
        self.hidden_channels = int(hidden_channels)
        self.projection_channels = int(projection_channels)

        self.lifting = nn.Conv2d(self.in_channels, self.hidden_channels, kernel_size=1)
        self.fno_blocks = nn.ModuleList(
            [FNOBlock2d(self.hidden_channels, self.n_modes) for _ in range(self.n_layers)]
        )
        self.projection = nn.Sequential(
            nn.Conv2d(self.hidden_channels, self.projection_channels, kernel_size=1),
            nn.GELU(),
            nn.Conv2d(self.projection_channels, self.out_channels, kernel_size=1),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        if x.ndim != 4:
            raise ValueError(f"Expected [B,C,H,W] input, got shape {tuple(x.shape)}.")
        if x.shape[1] != self.in_channels:
            raise ValueError(f"Expected {self.in_channels} channels, got {x.shape[1]}.")
        x = self.lifting(x)
        for idx, block in enumerate(self.fno_blocks):
            x = block(x)
            if idx != len(self.fno_blocks) - 1:
                x = F.gelu(x)
        return self.projection(x)


def build_model_from_config(config: dict) -> FlowBenchFNO:
    model_cfg = config.get("model", {})
    return FlowBenchFNO(
        n_modes=model_cfg.get("n_modes", [32, 32]),
        n_layers=model_cfg.get("n_layers", 5),
        in_channels=model_cfg.get("in_channels", 3),
        out_channels=model_cfg.get("out_channels", 4),
        hidden_channels=model_cfg.get("hidden_channels", 32),
        projection_channels=model_cfg.get("projection_channels", 128),
    )