"""Pipe-G architecture from arXiv:2601.17407, independently implemented. The paper defines two physical coordinate inputs, width 96, seven residual DS blocks and the dilation order. Paper omissions (DC intermediate GELU, zero padding and projection width 128) follow the separately audited read-only official reference; no repository code is copied here. """ from __future__ import annotations from collections.abc import Sequence import torch from torch import nn class SqueezeExcitation(nn.Module): """Spatial mean and two pointwise convolutions produce channel gates.""" def __init__(self, channels: int, reduction: int = 1): super().__init__() if reduction < 1 or channels % reduction: raise ValueError("SE reduction must divide the channel width") hidden = channels // reduction self.reduce = nn.Conv2d(channels, hidden, 1, bias=True) self.activate = nn.GELU() self.expand = nn.Conv2d(hidden, channels, 1, bias=True) def forward(self, features: torch.Tensor) -> torch.Tensor: pooled = features.mean(dim=(-2, -1), keepdim=True) gates = torch.sigmoid(self.expand(self.activate(self.reduce(pooled)))) return features * gates class DSBlock(nn.Module): """Two dilated convolutions, SE gating, residual addition and GELU.""" def __init__(self, channels: int, dilation: int, se_reduction: int = 1): super().__init__() if dilation < 1: raise ValueError("Dilation must be positive") self.first = nn.Conv2d(channels, channels, 3, padding=dilation, dilation=dilation, bias=True) self.second = nn.Conv2d(channels, channels, 3, padding=dilation, dilation=dilation, bias=True) self.se = SqueezeExcitation(channels, se_reduction) self.activate = nn.GELU() def forward(self, features: torch.Tensor) -> torch.Tensor: transformed = self.second(self.activate(self.first(features))) return self.activate(features + self.se(transformed)) class DSENO(nn.Module): """Normalized coordinates [B,H,W,2] to normalized velocity [B,H,W].""" def __init__(self, width: int = 96, dilations: Sequence[int] = (23, 17, 13, 9, 7, 3, 1), projection_width: int = 128, input_channels: int = 2, output_channels: int = 1, se_reduction: int = 1): super().__init__() if input_channels != 2 or output_channels != 1: raise ValueError("Pipe requires exactly X/Y inputs and one velocity output") if width < 1 or projection_width < 1 or not dilations: raise ValueError("Widths and dilation sequence must be nonempty and positive") self.input_channels = input_channels self.lifting = nn.Conv2d(input_channels, width, 1, bias=True) self.blocks = nn.Sequential(*(DSBlock(width, int(d), se_reduction) for d in dilations)) self.projection = nn.Sequential(nn.Conv2d(width, projection_width, 1, bias=True), nn.GELU(), nn.Conv2d(projection_width, output_channels, 1, bias=True)) def forward(self, coordinates: torch.Tensor) -> torch.Tensor: if coordinates.ndim != 4 or coordinates.shape[-1] != self.input_channels: raise ValueError("DSENO expects coordinates shaped [B,H,W,2]") features = coordinates.permute(0, 3, 1, 2).contiguous(memory_format=getattr(self, "memory_format", torch.channels_last)) features = self.blocks(self.lifting(features)) return self.projection(features).squeeze(1)