#!/usr/bin/env python3 # -*- coding: utf-8 -*- """GCC-φ-Pyramid v2: gated cross-stage CWT context + SelfAttn@360 + MaxPool→L_s.""" from __future__ import annotations from typing import List, Optional, Sequence, Tuple import torch import torch.nn as nn import torch.nn.functional as F class CircularDWConv1d(nn.Module): """Depthwise conv along angular dim with circular padding.""" def __init__(self, channels: int, kernel_size: int): super().__init__() self.kernel_size = kernel_size self.dw = nn.Conv1d( channels, channels, kernel_size, padding=0, groups=channels, bias=False, ) def forward(self, x: torch.Tensor) -> torch.Tensor: # x: (B, L, C) t = x.transpose(1, 2) pad = self.kernel_size // 2 t = F.pad(t, (pad, pad), mode="circular") t = self.dw(t) return t.transpose(1, 2) class GatedContextMerge(nn.Module): """local + sigmoid(W[local; proj(source); e_s]) * proj(source).""" def __init__( self, d_phi: int, d_stage_emb: int, *, gate_init: float = -2.0, ): super().__init__() self.proj = nn.Linear(d_phi, d_phi) self.gate = nn.Linear(2 * d_phi + d_stage_emb, d_phi) nn.init.constant_(self.gate.bias, gate_init) def forward( self, local: torch.Tensor, source: torch.Tensor, stage_emb: torch.Tensor, valid_ang: Optional[torch.Tensor] = None, ) -> torch.Tensor: ctx = self.proj(source) b, n, _ = local.shape if stage_emb.dim() == 1: emb = stage_emb.view(1, 1, -1).expand(b, n, -1) else: emb = stage_emb.unsqueeze(1).expand(b, n, -1) gate_in = torch.cat([local, ctx, emb], dim=-1) g = torch.sigmoid(self.gate(gate_in)) if valid_ang is not None: v = valid_ang.unsqueeze(-1).to(dtype=g.dtype) g = g * v return local + g * ctx def maxpool_theta_to_stage(phi: torch.Tensor, l_s: int) -> torch.Tensor: """(B, N_theta, d) → (B, L_s, d) via adaptive max pool.""" x = phi.transpose(1, 2) y = F.adaptive_max_pool1d(x, l_s) return y.transpose(1, 2) class GCCPhiPyramid(nn.Module): """ GCC internal flow: Bottom-Up → Top-Down → SelfAttn@360 → MaxPool→L_s. Input: list of phi_local (B, 360, d_phi) per stage. Output: list of phi_out (B, L_s, d_phi) aligned with ConvNeXt stages. """ def __init__( self, *, n_stages: int = 4, stage_lengths: Sequence[int], d_phi: int = 64, d_stage_emb: int = 16, gate_init: float = -2.0, dw_kernels_bu: Sequence[int] = (3, 5, 7), dw_kernels_td: Sequence[int] = (7, 5, 3), phi_tx_layers: int = 2, phi_tx_heads: int = 4, phi_tx_ffn: int = 256, phi_tx_dropout: float = 0.1, ): super().__init__() self.n_stages = n_stages self.stage_lengths = list(stage_lengths) self.d_phi = d_phi self.stage_emb = nn.Embedding(n_stages, d_stage_emb) self.norm_bu = nn.ModuleList([nn.LayerNorm(d_phi) for _ in range(n_stages)]) self.norm_td = nn.ModuleList([nn.LayerNorm(d_phi) for _ in range(n_stages)]) self.norm_post = nn.ModuleList([nn.LayerNorm(d_phi) for _ in range(n_stages)]) self.bu_dw = nn.ModuleList( [CircularDWConv1d(d_phi, k) for k in dw_kernels_bu[: n_stages - 1]] ) self.td_dw = nn.ModuleList( [CircularDWConv1d(d_phi, k) for k in dw_kernels_td[: n_stages - 1]] ) self.bu_merge = nn.ModuleList( [ GatedContextMerge(d_phi, d_stage_emb, gate_init=gate_init) for _ in range(n_stages - 1) ] ) self.td_merge = nn.ModuleList( [ GatedContextMerge(d_phi, d_stage_emb, gate_init=gate_init) for _ in range(n_stages - 1) ] ) from section43_v3_1 import PhiStageTransformer self.phi_tx = nn.ModuleList( [ PhiStageTransformer( d_phi=d_phi, n_heads=phi_tx_heads, n_layers=phi_tx_layers, ffn_dim=phi_tx_ffn, dropout=phi_tx_dropout, ) for _ in range(n_stages) ] ) def _stage_emb(self, sid: int, batch: int, device: torch.device) -> torch.Tensor: idx = torch.full((batch,), sid, dtype=torch.long, device=device) return self.stage_emb(idx) def forward( self, phi_local_list: List[torch.Tensor], valid_ang: Optional[torch.Tensor] = None, stage_lengths: Optional[Sequence[int]] = None, ) -> List[torch.Tensor]: n = len(phi_local_list) assert n == self.n_stages lengths = list(stage_lengths) if stage_lengths is not None else self.stage_lengths if len(lengths) != n: raise ValueError(f"stage_lengths 长度须为 {n},got {len(lengths)}") # Bottom-Up (fine → coarse) phi_prime: List[torch.Tensor] = [None] * n # type: ignore[list-item] phi_prime[0] = self.norm_bu[0](phi_local_list[0]) for s in range(1, n): src = self.bu_dw[s - 1](phi_prime[s - 1]) emb = self._stage_emb(s, phi_local_list[s].size(0), phi_local_list[s].device) merged = self.bu_merge[s - 1](phi_local_list[s], src, emb, valid_ang) phi_prime[s] = self.norm_bu[s](merged) # Top-Down (coarse → fine) phi_ctx: List[torch.Tensor] = [None] * n # type: ignore[list-item] phi_ctx[n - 1] = self.norm_td[n - 1](phi_prime[n - 1]) for s in range(n - 2, -1, -1): src = self.td_dw[s](phi_ctx[s + 1]) emb = self._stage_emb(s, phi_prime[s].size(0), phi_prime[s].device) merged = self.td_merge[s](phi_prime[s], src, emb, valid_ang) phi_ctx[s] = self.norm_td[s](merged) # SelfAttn @360 + MaxPool → L_s out: List[torch.Tensor] = [] for s in range(n): h = self.phi_tx[s](phi_ctx[s], valid_ang) pooled = maxpool_theta_to_stage(h, lengths[s]) out.append(self.norm_post[s](pooled)) return out def freeze_for_e2c_gcc(model: nn.Module) -> Tuple[int, int]: """ Freeze enc41/42, ConvNeXt pyramid, GAPA, stage_phi_enc; train GCC + classifier. Returns (trainable_count, total_count). """ for p in model.parameters(): p.requires_grad = False pool = getattr(model.enc43, "segment_pool", None) if pool is not None and getattr(pool, "gcc", None) is not None: for p in pool.gcc.parameters(): p.requires_grad = True if hasattr(model, "classifier"): for p in model.classifier.parameters(): p.requires_grad = True total = sum(p.numel() for p in model.parameters()) trainable = sum(p.numel() for p in model.parameters() if p.requires_grad) return trainable, total