# SPDX-FileCopyrightText: Copyright (c) 2023 - 2024 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
from dataclasses import dataclass
import numpy as np
import torch
from ..layers import DownSample3D, FuserLayer, UpSample3D
from ..meta import ModelMetaData
from ..module import Module
from ..utils import (
PatchEmbed2D,
PatchEmbed3D,
PatchRecovery2D,
PatchRecovery3D,
)
[docs]class Pangu(Module):
"""
Pangu A PyTorch impl of: `Pangu-Weather: A 3D High-Resolution Model for Fast and Accurate Global Weather Forecast`
- https://arxiv.org/abs/2211.02556
Args:
img_size (tuple[int]): Image size [Lat, Lon].
patch_size (tuple[int]): Patch token size [Lat, Lon].
embed_dim (int): Patch embedding dimension. Default: 192
num_heads (tuple[int]): Number of attention heads in different layers.
window_size (tuple[int]): Window size.
"""
def __init__(
self,
img_size=(721, 1440),
patch_size=(2, 4, 4),
embed_dim=192,
num_heads=(6, 12, 12, 6),
window_size=(2, 6, 12),
):
super().__init__(meta=MetaData())
drop_path = np.linspace(0, 0.2, 8).tolist()
# In addition, three constant masks(the topography mask, land-sea mask and soil type mask)
self.patchembed2d = PatchEmbed2D(
img_size=img_size,
patch_size=patch_size[1:],
in_chans=4 + 3, # add
embed_dim=embed_dim,
)
self.patchembed3d = PatchEmbed3D(
img_size=(13, img_size[0], img_size[1]),
patch_size=patch_size,
in_chans=5,
embed_dim=embed_dim,
)
patched_inp_shape = (
8,
math.ceil(img_size[0] / patch_size[1]),
math.ceil(img_size[1] / patch_size[2]),
)
self.layer1 = FuserLayer(
dim=embed_dim,
input_resolution=patched_inp_shape,
depth=2,
num_heads=num_heads[0],
window_size=window_size,
drop_path=drop_path[:2],
)
patched_inp_shape_downsample = (
8,
math.ceil(patched_inp_shape[1] / 2),
math.ceil(patched_inp_shape[2] / 2),
)
self.downsample = DownSample3D(
in_dim=embed_dim,
input_resolution=patched_inp_shape,
output_resolution=patched_inp_shape_downsample,
)
self.layer2 = FuserLayer(
dim=embed_dim * 2,
input_resolution=patched_inp_shape_downsample,
depth=6,
num_heads=num_heads[1],
window_size=window_size,
drop_path=drop_path[2:],
)
self.layer3 = FuserLayer(
dim=embed_dim * 2,
input_resolution=patched_inp_shape_downsample,
depth=6,
num_heads=num_heads[2],
window_size=window_size,
drop_path=drop_path[2:],
)
self.upsample = UpSample3D(
embed_dim * 2, embed_dim, patched_inp_shape_downsample, patched_inp_shape
)
self.layer4 = FuserLayer(
dim=embed_dim,
input_resolution=patched_inp_shape,
depth=2,
num_heads=num_heads[3],
window_size=window_size,
drop_path=drop_path[:2],
)
# The outputs of the 2nd encoder layer and the 7th decoder layer are concatenated along the channel dimension.
self.patchrecovery2d = PatchRecovery2D(
img_size, patch_size[1:], 2 * embed_dim, 4
)
self.patchrecovery3d = PatchRecovery3D(
(13, img_size[0], img_size[1]), patch_size, 2 * embed_dim, 5
)
[docs] def forward(self, x):
"""
Args:
x (torch.Tensor): [batch, 4+3+5*13, lat, lon]
"""
surface = x[:, :7, :, :]
upper_air = x[:, 7:, :, :].reshape(x.shape[0], 5, 13, x.shape[2], x.shape[3])
surface = self.patchembed2d(surface)
upper_air = self.patchembed3d(upper_air)
x = torch.concat([surface.unsqueeze(2), upper_air], dim=2)
B, C, Pl, Lat, Lon = x.shape
x = x.reshape(B, C, -1).transpose(1, 2)
x = self.layer1(x)
skip = x
x = self.downsample(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.upsample(x)
x = self.layer4(x)
output = torch.concat([x, skip], dim=-1)
output = output.transpose(1, 2).reshape(B, -1, Pl, Lat, Lon)
output_surface = output[:, :, 0, :, :]
output_upper_air = output[:, :, 1:, :, :]
output_surface = self.patchrecovery2d(output_surface)
output_upper_air = self.patchrecovery3d(output_upper_air)
return output_surface, output_upper_air