File size: 11,367 Bytes
8bcb60f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""
Spherical Fourier Neural Operator (SFNO) for LILITH.

Processes atmospheric state on a spherical domain using spectral methods.
Based on NVIDIA's FourCastNet architecture.
"""

import math
from typing import Optional, Tuple

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

# Try to import torch_harmonics for spherical harmonics
try:
    import torch_harmonics as th
    TORCH_HARMONICS_AVAILABLE = True
except ImportError:
    TORCH_HARMONICS_AVAILABLE = False


class SpectralConv2d(nn.Module):
    """
    2D Spectral Convolution using FFT.

    Performs convolution in the Fourier domain for global receptive field
    with O(N log N) complexity.
    """

    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        modes1: int = 32,
        modes2: int = 32,
    ):
        """
        Initialize spectral convolution.

        Args:
            in_channels: Input channels
            out_channels: Output channels
            modes1: Number of Fourier modes in first dimension
            modes2: Number of Fourier modes in second dimension
        """
        super().__init__()

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.modes1 = modes1
        self.modes2 = modes2

        # Learnable Fourier coefficients
        scale = 1 / (in_channels * out_channels)
        self.weights1 = nn.Parameter(
            scale * torch.randn(in_channels, out_channels, modes1, modes2, dtype=torch.cfloat)
        )
        self.weights2 = nn.Parameter(
            scale * torch.randn(in_channels, out_channels, modes1, modes2, dtype=torch.cfloat)
        )

    def compl_mul2d(
        self,
        input: torch.Tensor,
        weights: torch.Tensor,
    ) -> torch.Tensor:
        """Complex multiplication for batched inputs."""
        # (batch, in_ch, x, y) * (in_ch, out_ch, x, y) -> (batch, out_ch, x, y)
        return torch.einsum("bixy,ioxy->boxy", input, weights)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Forward pass.

        Args:
            x: Input tensor of shape (batch, channels, height, width)

        Returns:
            Output tensor of same shape
        """
        batch_size = x.size(0)
        height, width = x.size(-2), x.size(-1)

        # Compute FFT
        x_ft = torch.fft.rfft2(x)

        # Multiply relevant Fourier modes
        out_ft = torch.zeros(
            batch_size,
            self.out_channels,
            height,
            width // 2 + 1,
            dtype=torch.cfloat,
            device=x.device,
        )

        # Lower modes
        out_ft[:, :, :self.modes1, :self.modes2] = self.compl_mul2d(
            x_ft[:, :, :self.modes1, :self.modes2],
            self.weights1,
        )

        # Upper modes (for symmetry)
        out_ft[:, :, -self.modes1:, :self.modes2] = self.compl_mul2d(
            x_ft[:, :, -self.modes1:, :self.modes2],
            self.weights2,
        )

        # Inverse FFT
        x = torch.fft.irfft2(out_ft, s=(height, width))

        return x


class SphericalConv(nn.Module):
    """
    Spherical convolution using spherical harmonics.

    Properly handles the geometry of the sphere, avoiding polar distortion
    that occurs with standard 2D convolutions on lat-lon grids.
    """

    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        nlat: int = 721,
        nlon: int = 1440,
        lmax: Optional[int] = None,
    ):
        """
        Initialize spherical convolution.

        Args:
            in_channels: Input channels
            out_channels: Output channels
            nlat: Number of latitude points
            nlon: Number of longitude points
            lmax: Maximum spherical harmonic degree
        """
        super().__init__()

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.nlat = nlat
        self.nlon = nlon
        self.lmax = lmax or nlat // 2

        if TORCH_HARMONICS_AVAILABLE:
            # Use torch_harmonics for proper spherical harmonics
            self.sht = th.RealSHT(nlat, nlon, lmax=self.lmax)
            self.isht = th.InverseRealSHT(nlat, nlon, lmax=self.lmax)

            # Learnable spectral weights
            n_coeffs = (self.lmax + 1) * (self.lmax + 2) // 2
            self.spectral_weights = nn.Parameter(
                torch.randn(in_channels, out_channels, n_coeffs) / math.sqrt(in_channels)
            )
        else:
            # Fallback to FFT-based convolution
            self.spectral_conv = SpectralConv2d(
                in_channels, out_channels,
                modes1=min(32, nlat // 2),
                modes2=min(32, nlon // 2),
            )
            self.spectral_weights = None

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Forward pass.

        Args:
            x: Input tensor of shape (batch, channels, nlat, nlon)

        Returns:
            Output tensor of same shape
        """
        if TORCH_HARMONICS_AVAILABLE and self.spectral_weights is not None:
            batch_size = x.size(0)

            # Transform to spectral domain
            x_spec = self.sht(x)

            # Apply learnable weights
            out_spec = torch.einsum("bixk,iok->boxk", x_spec, self.spectral_weights)

            # Transform back
            x = self.isht(out_spec)
        else:
            x = self.spectral_conv(x)

        return x


class SFNOBlock(nn.Module):
    """
    Single block of the Spherical Fourier Neural Operator.

    Combines spectral convolution with pointwise MLP and residual connection.
    """

    def __init__(
        self,
        channels: int,
        nlat: int = 64,
        nlon: int = 128,
        mlp_ratio: float = 2.0,
        dropout: float = 0.1,
        use_spherical: bool = True,
    ):
        """
        Initialize SFNO block.

        Args:
            channels: Number of channels
            nlat: Number of latitude points
            nlon: Number of longitude points
            mlp_ratio: MLP hidden dimension ratio
            dropout: Dropout probability
            use_spherical: Use spherical harmonics (if available)
        """
        super().__init__()

        self.channels = channels
        self.use_spherical = use_spherical and TORCH_HARMONICS_AVAILABLE

        # Spectral convolution
        if self.use_spherical:
            self.spectral = SphericalConv(channels, channels, nlat, nlon)
        else:
            self.spectral = SpectralConv2d(
                channels, channels,
                modes1=min(32, nlat // 2),
                modes2=min(32, nlon // 2),
            )

        # Pointwise MLP
        hidden_dim = int(channels * mlp_ratio)
        self.mlp = nn.Sequential(
            nn.Conv2d(channels, hidden_dim, 1),
            nn.GELU(),
            nn.Dropout(dropout),
            nn.Conv2d(hidden_dim, channels, 1),
            nn.Dropout(dropout),
        )

        # Normalization
        self.norm1 = nn.GroupNorm(min(32, channels), channels)
        self.norm2 = nn.GroupNorm(min(32, channels), channels)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Forward pass with residual connections."""
        # Spectral path
        h = self.norm1(x)
        h = self.spectral(h)
        x = x + h

        # MLP path
        h = self.norm2(x)
        h = self.mlp(h)
        x = x + h

        return x


class SphericalFourierNeuralOperator(nn.Module):
    """
    Full Spherical Fourier Neural Operator.

    A neural operator that learns atmospheric dynamics on a spherical domain
    using spectral methods for efficient global communication.

    Based on:
    - FourCastNet (NVIDIA)
    - Spherical Fourier Neural Operators (Bonev et al.)
    """

    def __init__(
        self,
        input_dim: int,
        hidden_dim: int = 256,
        output_dim: int = 256,
        num_layers: int = 4,
        nlat: int = 64,
        nlon: int = 128,
        mlp_ratio: float = 2.0,
        dropout: float = 0.1,
        use_spherical: bool = True,
    ):
        """
        Initialize SFNO.

        Args:
            input_dim: Input feature dimension
            hidden_dim: Hidden dimension
            output_dim: Output dimension
            num_layers: Number of SFNO blocks
            nlat: Number of latitude points in grid
            nlon: Number of longitude points in grid
            mlp_ratio: MLP expansion ratio
            dropout: Dropout probability
            use_spherical: Use spherical harmonics
        """
        super().__init__()

        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.output_dim = output_dim
        self.nlat = nlat
        self.nlon = nlon

        # Input projection
        self.input_proj = nn.Conv2d(input_dim, hidden_dim, 1)
        self.input_norm = nn.GroupNorm(min(32, hidden_dim), hidden_dim)

        # SFNO blocks
        self.blocks = nn.ModuleList([
            SFNOBlock(
                channels=hidden_dim,
                nlat=nlat,
                nlon=nlon,
                mlp_ratio=mlp_ratio,
                dropout=dropout,
                use_spherical=use_spherical,
            )
            for _ in range(num_layers)
        ])

        # Output projection
        self.output_norm = nn.GroupNorm(min(32, hidden_dim), hidden_dim)
        self.output_proj = nn.Conv2d(hidden_dim, output_dim, 1)

        # Gradient checkpointing
        self.gradient_checkpointing = False

    def enable_gradient_checkpointing(self):
        """Enable gradient checkpointing for memory efficiency."""
        self.gradient_checkpointing = True

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Process gridded atmospheric state.

        Args:
            x: Input tensor of shape (batch, input_dim, nlat, nlon)

        Returns:
            Output tensor of shape (batch, output_dim, nlat, nlon)
        """
        # Input projection
        h = self.input_proj(x)
        h = self.input_norm(h)

        # Apply SFNO blocks
        for block in self.blocks:
            if self.gradient_checkpointing and self.training:
                h = torch.utils.checkpoint.checkpoint(block, h, use_reentrant=False)
            else:
                h = block(h)

        # Output projection
        h = self.output_norm(h)
        h = self.output_proj(h)

        return h

    def forward_multiscale(
        self,
        x: torch.Tensor,
        scales: Tuple[int, ...] = (1, 2, 4),
    ) -> torch.Tensor:
        """
        Multi-scale processing for capturing different spatial patterns.

        Args:
            x: Input tensor
            scales: Downsampling factors to use

        Returns:
            Combined multi-scale output
        """
        outputs = []

        for scale in scales:
            if scale > 1:
                # Downsample
                x_scaled = F.avg_pool2d(x, scale)
                # Process
                h = self.forward(x_scaled)
                # Upsample back
                h = F.interpolate(h, size=(self.nlat, self.nlon), mode="bilinear")
            else:
                h = self.forward(x)

            outputs.append(h)

        # Combine scales (simple average, could be learned)
        return torch.stack(outputs).mean(dim=0)