Download code/section43_v3_1.py from qinxunhui/deepellipse-circleid: direct link, hf CLI and curl.
- Browser
- Download file 54 kB
-
https://huggingface.co/qinxunhui/deepellipse-circleid/resolve/main/code/section43_v3_1.py
- Command line
-
hf download hf://qinxunhui/deepellipse-circleid/code/section43_v3_1.py
-
curl -L -o section43_v3_1.py https://huggingface.co/qinxunhui/deepellipse-circleid/resolve/main/code/section43_v3_1.py
54 kB
"""
§4.3 v3.1:DBSA / FGSS 段级汇聚。
DBSA-v2(默认):
Step0 各 stage 段内 avg → h_s^(m)
Step1 vec(A_fuse^(m)), vec(Ā_global) → MLP_shared → φ_loc, φ_glob
Step2 stage1/2: [φ_glob; φ_loc^(m); h_s^(m)] × M tokens → 共享 2L Transformer
→ 各 stage 独立 weight head → σ → 归一化 Σ_m w_m=1
Step3 stage3/4: φ_glob 作 Query,对 h_s^(m) 交叉打分 → σ → 归一化
Step4 v_s = Σ_m w_m h_s^(m),Concat → f_ellipse
另保留 global / seg_sigmoid(FGSS)作消融。
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List, Literal, Optional, Sequence, Tuple
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from section42 import S_RES_CHANNELS, build_segment_geometry, responsibility_indices
from section42_cwt import (
CWTStagePhiEncoder,
CWTStagePhiEncoderValid,
CWTPyramid,
CWTStageProjector,
Section42CWTConfig,
build_scale_indices,
cwt_pyramid_in_channels,
)
from section43_v3 import (
ConvNeXt1DPyramid,
Section43ConfigV3,
pool_sequence_v3,
)
def _interp_segments_batched(
feat: torch.Tensor,
i0: torch.Tensor,
i1: torch.Tensor,
w: torch.Tensor,
) -> torch.Tensor:
f0 = feat[:, i0, :]
f1 = feat[:, i1, :]
return f0 * (1.0 - w) + f1 * w
def _segment_mean(
h: torch.Tensor,
valid_seg: Optional[torch.Tensor] = None,
) -> torch.Tensor:
"""h: (B, M, s_l, d_h) → (B, M, d_h)。"""
if valid_seg is None:
return h.mean(dim=2)
w = valid_seg.unsqueeze(-1).to(h.dtype)
denom = w.sum(dim=2).clamp(min=1e-6)
return (h * w).sum(dim=2) / denom
def _normalize_segment_weights(w_raw: torch.Tensor, dim: int = 1) -> torch.Tensor:
"""Sigmoid 后沿段维归一化,使 Σ_m w_m = 1。"""
w = torch.sigmoid(w_raw)
return w / w.sum(dim=dim, keepdim=True).clamp(min=1e-6)
class StageProjector(nn.Module):
def __init__(self, stage_dims: Tuple[int, ...], d_h: int):
super().__init__()
self.projs = nn.ModuleList([nn.Linear(c, d_h) for c in stage_dims])
def forward(self, stage_idx: int, x: torch.Tensor) -> torch.Tensor:
return self.projs[stage_idx](x)
class AmpSharedEncoder(nn.Module):
"""vec(A_fuse^(m)), vec(Ā_global) → 共享 MLP → φ_loc, φ_glob。"""
def __init__(self, s_l: int, n_ch: int = S_RES_CHANNELS, d_out: int = 64, hidden: int = 128):
super().__init__()
self.in_dim = s_l * n_ch
self.mlp = nn.Sequential(
nn.Linear(self.in_dim, hidden),
nn.GELU(),
nn.Dropout(0.1),
nn.Linear(hidden, d_out),
)
def forward(
self,
amp_fuse: torch.Tensor,
amp_global_bar: torch.Tensor,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
amp_fuse: (B, M, s_l, 4)
amp_global_bar: (B, s_l, 4)
→ phi_loc (B, M, d), phi_glob (B, d)
"""
b, m_count = amp_fuse.shape[:2]
loc_flat = amp_fuse.reshape(b * m_count, self.in_dim)
phi_loc = self.mlp(loc_flat).reshape(b, m_count, -1)
phi_glob = self.mlp(amp_global_bar.reshape(b, self.in_dim))
return phi_loc, phi_glob
class FineSharedTransformerPool(nn.Module):
"""
Stage 1/2:M 个段 token → 共享 Transformer → 各 stage 独立 weight head。
token_m = Proj_s([φ_glob; φ_loc^(m); h_s^(m)])
"""
def __init__(
self,
d_amp: int,
d_h: int,
d_model: int = 128,
n_heads: int = 4,
n_layers: int = 2,
gate_hidden: int = 64,
gate_dropout: float = 0.2,
):
super().__init__()
self.token_projs = nn.ModuleList(
[nn.Linear(2 * d_amp + d_h, d_model) for _ in range(2)]
)
enc_layer = nn.TransformerEncoderLayer(
d_model=d_model,
nhead=n_heads,
dim_feedforward=d_model * 4,
dropout=gate_dropout,
batch_first=True,
norm_first=True,
activation="gelu",
)
self.transformer = nn.TransformerEncoder(enc_layer, num_layers=n_layers)
self.weight_heads = nn.ModuleList(
[
nn.Sequential(
nn.Linear(d_model, gate_hidden),
nn.GELU(),
nn.Dropout(gate_dropout),
nn.Linear(gate_hidden, 1),
)
for _ in range(2)
]
)
def forward(
self,
phi_glob: torch.Tensor,
phi_loc: torch.Tensor,
h_seg_stages: List[torch.Tensor],
) -> List[torch.Tensor]:
"""
返回长度 2 的 list,每项 w (B, M, 1),沿 M 求和为 1。
"""
pg = phi_glob.unsqueeze(1).expand(-1, phi_loc.size(1), -1)
weights: List[torch.Tensor] = []
for sid, h_seg in enumerate(h_seg_stages):
tokens = self.token_projs[sid](torch.cat([pg, phi_loc, h_seg], dim=-1))
z = self.transformer(tokens)
w = _normalize_segment_weights(self.weight_heads[sid](z).squeeze(-1), dim=1)
weights.append(w.unsqueeze(-1))
return weights
class CoarseGlobQueryPool(nn.Module):
"""
Stage 3/4:φ_glob 作 Query,h_s^(m) 作 Key → Sigmoid 打分 → 段维归一化。
"""
def __init__(self, d_amp: int, d_h: int, d_prime: int = 32, gate_dropout: float = 0.2):
super().__init__()
self.q_proj = nn.Linear(d_amp, d_prime)
self.k_proj = nn.Linear(d_h, d_prime)
self.scale = d_prime ** -0.5
self.score_mlp = nn.Sequential(
nn.Linear(1, 16),
nn.GELU(),
nn.Dropout(gate_dropout),
nn.Linear(16, 1),
)
def forward(self, phi_glob: torch.Tensor, h_seg: torch.Tensor) -> torch.Tensor:
"""
phi_glob: (B, d_amp)
h_seg: (B, M, d_h)
→ w (B, M, 1),Σ_m w_m = 1
"""
q = self.q_proj(phi_glob).unsqueeze(1)
k = self.k_proj(h_seg)
logits = (q * k).sum(dim=-1, keepdim=True) * self.scale
w_raw = self.score_mlp(logits).squeeze(-1)
w = _normalize_segment_weights(w_raw, dim=1)
return w.unsqueeze(-1)
class DBSASegmentPool(nn.Module):
"""DBSA-v2 四 stage 段级汇聚。"""
def __init__(self, cfg: "Section43ConfigV3_1", s_l: int):
super().__init__()
d_amp = cfg.d_amp
d_h = cfg.d_h
self.amp_enc = AmpSharedEncoder(s_l, d_out=d_amp, hidden=cfg.amp_mlp_hidden)
self.fine_pool = FineSharedTransformerPool(
d_amp,
d_h,
d_model=cfg.dbsa_d_model,
n_heads=cfg.dbsa_n_heads,
n_layers=cfg.dbsa_n_layers,
gate_hidden=cfg.seg_gate_hidden,
gate_dropout=cfg.seg_gate_dropout,
)
self.coarse_pools = nn.ModuleList(
[
CoarseGlobQueryPool(
d_amp,
d_h,
d_prime=cfg.dbsa_coarse_d_prime,
gate_dropout=cfg.seg_gate_dropout,
)
for _ in range(2)
]
)
def forward(
self,
h_stack: torch.Tensor,
amp_fuse: torch.Tensor,
amp_global_bar: torch.Tensor,
valid_seg: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
phi_loc, phi_glob = self.amp_enc(amp_fuse, amp_global_bar)
n_stages = h_stack.size(2)
h_seg_list = [
_segment_mean(h_stack[:, :, sid, :, :], valid_seg) for sid in range(n_stages)
]
w_fine = self.fine_pool(phi_glob, phi_loc, h_seg_list[:2])
w_coarse = [
self.coarse_pools[i](phi_glob, h_seg_list[i + 2]) for i in range(2)
]
all_weights = w_fine + w_coarse
vecs: List[torch.Tensor] = []
w_cols: List[torch.Tensor] = []
for sid, (w, h_seg) in enumerate(zip(all_weights, h_seg_list)):
v_s = (w * h_seg).sum(dim=1)
vecs.append(v_s)
w_cols.append(w.squeeze(-1))
w_seg = torch.stack(w_cols, dim=-1)
h_seg_stack = torch.stack(h_seg_list, dim=2)
return torch.cat(vecs, dim=-1), w_seg, h_seg_stack, phi_loc, phi_glob
class SegmentSigmoidGate(nn.Module):
def __init__(self, d_freq: int, d_h: int, hidden: int = 64, dropout: float = 0.2):
super().__init__()
self.mlp = nn.Sequential(
nn.Linear(d_freq + d_h, hidden),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(hidden, 1),
)
def forward(self, f_bar: torch.Tensor, h_seg: torch.Tensor) -> torch.Tensor:
g = torch.cat([f_bar, h_seg], dim=-1)
w_raw = self.mlp(g).squeeze(-1)
return _normalize_segment_weights(w_raw, dim=1).unsqueeze(-1)
class SegmentSigmoidStagePool(nn.Module):
"""FGSS 消融基线。"""
def __init__(
self,
n_stages: int,
d_freq: int,
d_h: int,
hidden: int = 64,
gate_dropout: float = 0.2,
):
super().__init__()
self.gates = nn.ModuleList(
[SegmentSigmoidGate(d_freq, d_h, hidden, gate_dropout) for _ in range(n_stages)]
)
def forward(
self,
h_stack: torch.Tensor,
f_bar: torch.Tensor,
valid_seg: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
vecs: List[torch.Tensor] = []
weights: List[torch.Tensor] = []
h_seg_all: List[torch.Tensor] = []
for sid, gate in enumerate(self.gates):
h_seg = _segment_mean(h_stack[:, :, sid, :, :], valid_seg)
w = gate(f_bar, h_seg)
v_s = (w * h_seg).sum(dim=1)
vecs.append(v_s)
weights.append(w.squeeze(-1))
h_seg_all.append(h_seg)
return (
torch.cat(vecs, dim=-1),
torch.stack(weights, dim=-1),
torch.stack(h_seg_all, dim=2),
)
def _interp_theta_batched(
feat: torch.Tensor,
i0: torch.Tensor,
i1: torch.Tensor,
w: torch.Tensor,
) -> torch.Tensor:
"""feat: (B, N_theta, D) → (B, L_s, D)。"""
f0 = feat[:, i0, :]
f1 = feat[:, i1, :]
return f0 * (1.0 - w) + f1 * w
AngularNormMode = Literal["softmax", "sum_relu"]
def angular_pool_with_norm(
scores: torch.Tensor,
norm_mode: AngularNormMode,
valid_s: Optional[torch.Tensor] = None,
dropout: Optional[nn.Dropout] = None,
) -> torch.Tensor:
"""
角向注意力权重归一化。
softmax:E1 默认,单峰倾向。
sum_relu:E3 Sum-Norm,ReLU 后除以总和,允许多峰。
"""
if valid_s is not None:
if norm_mode == "softmax":
scores = scores.masked_fill(valid_s < 0.5, -1e4)
else:
scores = scores.masked_fill(valid_s < 0.5, 0.0)
if norm_mode == "softmax":
w = F.softmax(scores, dim=-1)
else:
scores_pos = F.relu(scores)
denom = scores_pos.sum(dim=-1, keepdim=True)
l_s = scores_pos.size(-1)
uniform = torch.full_like(scores_pos, 1.0 / max(l_s, 1))
w = torch.where(denom > 1e-6, scores_pos / denom.clamp(min=1e-6), uniform)
if dropout is not None:
w = dropout(w)
return w
def _make_cwt_stage_phi_encoder(cfg: "Section43ConfigV3_1") -> nn.Module:
cwt_cfg = Section42CWTConfig(
d_phi=cfg.d_phi,
n_scales=cfg.cwt_n_scales,
wavelet=cfg.cwt_wavelet,
scale_affinity=True,
scale_index_mode=cfg.cwt_scale_index_mode,
n_stages=len(cfg.pyramid_dims),
)
scale_indices = build_scale_indices(cwt_cfg, n_theta=cfg.n_theta)
if cfg.cwt_scale_index_mode == "physics":
try:
from elipse_feature import describe_cwt_scale_groups
except ImportError:
describe_cwt_scale_groups = None # type: ignore[assignment,misc]
if describe_cwt_scale_groups is not None:
print(
describe_cwt_scale_groups(
scale_indices, n_scales=cfg.cwt_n_scales, n_theta=cfg.n_theta
)
)
if cfg.cwt_phi_valid_emb > 0:
return CWTStagePhiEncoderValid(
cwt_cfg, scale_indices, valid_emb_dim=cfg.cwt_phi_valid_emb
)
return CWTStagePhiEncoder(cwt_cfg, scale_indices)
class PhiStageTransformer(nn.Module):
"""E2b:每 stage 独立 2L Transformer,在 N_theta 维精炼 φ(对齐 §4.1.4)。"""
def __init__(
self,
d_phi: int = 64,
n_heads: int = 4,
n_layers: int = 2,
ffn_dim: int = 256,
dropout: float = 0.1,
):
super().__init__()
if n_layers <= 0:
self.norm = None
self.transformer = None
return
self.norm = nn.LayerNorm(d_phi)
enc_layer = nn.TransformerEncoderLayer(
d_model=d_phi,
nhead=n_heads,
dim_feedforward=ffn_dim,
dropout=dropout,
batch_first=True,
norm_first=True,
activation="gelu",
)
self.transformer = nn.TransformerEncoder(enc_layer, num_layers=n_layers)
def forward(
self,
phi: torch.Tensor,
valid_ang: Optional[torch.Tensor] = None,
) -> torch.Tensor:
if self.transformer is None:
return phi
h = self.norm(phi)
mask = None
if valid_ang is not None:
if valid_ang.dim() == 1:
valid_ang = valid_ang.unsqueeze(0).expand(phi.size(0), -1)
mask = valid_ang < 0.5
return self.transformer(h, src_key_padding_mask=mask)
class CWTAngularPool(nn.Module):
"""单 stage:φ_s 作 Query,H_s 作 Key/Value 的角向位置注意力汇聚。"""
def __init__(
self,
d_phi: int,
d_h: int,
d_k: int = 64,
dropout: float = 0.1,
norm_mode: AngularNormMode = "softmax",
):
super().__init__()
self.norm_mode = norm_mode
self.q_proj = nn.Linear(d_phi, d_k)
self.k_proj = nn.Linear(d_h, d_k)
self.v_proj = nn.Linear(d_h, d_h)
self.scale = d_k ** -0.5
self.dropout = nn.Dropout(dropout)
def forward(
self,
phi_s: torch.Tensor,
h_s: torch.Tensor,
valid_s: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
phi_s: (B, L_s, d_phi)
h_s: (B, L_s, d_h)
→ v_s (B, d_h), w_ang (B, L_s)
"""
q = self.q_proj(phi_s)
k = self.k_proj(h_s)
v = self.v_proj(h_s)
scores = (q * k).sum(dim=-1) * self.scale
w = angular_pool_with_norm(scores, self.norm_mode, valid_s, self.dropout)
v_out = (w.unsqueeze(-1) * v).sum(dim=1)
return v_out, w
CrossQuerySide = Literal["orig", "cwt"]
class CrossDomainStagePool(nn.Module):
"""
单 stage 跨域角向注意力。
query_side=orig:Q=H_orig, K/V=H_cwt(路2 orig_cnn)
query_side=cwt:Q=H_cwt, K/V=H_orig(路1 cwt_cnn)
"""
def __init__(
self,
d_h: int,
d_k: int = 64,
dropout: float = 0.1,
norm_mode: AngularNormMode = "sum_relu",
query_side: CrossQuerySide = "orig",
):
super().__init__()
self.norm_mode = norm_mode
self.query_side = query_side
self.q_proj = nn.Linear(d_h, d_k)
self.k_proj = nn.Linear(d_h, d_k)
self.v_proj = nn.Linear(d_h, d_h)
self.scale = d_k ** -0.5
self.dropout = nn.Dropout(dropout)
def forward(
self,
h_orig: torch.Tensor,
h_cwt: torch.Tensor,
valid_s: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
if self.query_side == "orig":
q_src, kv_src = h_orig, h_cwt
else:
q_src, kv_src = h_cwt, h_orig
q = self.q_proj(q_src)
k = self.k_proj(kv_src)
v = self.v_proj(kv_src)
scores = (q * k).sum(dim=-1) * self.scale
w = angular_pool_with_norm(scores, self.norm_mode, valid_s, self.dropout)
v_out = (w.unsqueeze(-1) * v).sum(dim=1)
return v_out, w
class CWTAngularStagePool(nn.Module):
"""CWT-GAPA:四 stage 均以 CWT φ 为 Query 的角向 cross-attention 汇聚。"""
def __init__(self, cfg: "Section43ConfigV3_1"):
super().__init__()
self.cfg = cfg
n_stages = len(cfg.pyramid_dims)
norm_mode: AngularNormMode = cfg.angular_norm # type: ignore[assignment]
self.pools = nn.ModuleList(
[
CWTAngularPool(
cfg.d_phi,
cfg.d_h,
d_k=cfg.angular_attn_d_k,
dropout=cfg.angular_attn_dropout,
norm_mode=norm_mode,
)
for _ in range(n_stages)
]
)
if cfg.cwt_scale_affinity:
self.stage_phi_enc = _make_cwt_stage_phi_encoder(cfg)
else:
self.stage_phi_enc = None
self.gcc_enabled = bool(getattr(cfg, "gcc_enabled", False))
if self.gcc_enabled:
from elipse_feature import pyramid_stage_lengths_from_n_theta
from gcc_phi_pyramid import GCCPhiPyramid
stage_lengths = pyramid_stage_lengths_from_n_theta(cfg.n_theta)
self.gcc = GCCPhiPyramid(
n_stages=n_stages,
stage_lengths=stage_lengths,
d_phi=cfg.d_phi,
d_stage_emb=cfg.gcc_d_stage_emb,
gate_init=cfg.gcc_gate_init,
dw_kernels_bu=cfg.gcc_dw_kernels_bu,
dw_kernels_td=cfg.gcc_dw_kernels_td,
phi_tx_layers=cfg.phi_tx_layers,
phi_tx_heads=cfg.phi_tx_heads,
phi_tx_ffn=cfg.phi_tx_ffn,
phi_tx_dropout=cfg.phi_tx_dropout,
)
self.phi_transformers = None
elif cfg.phi_tx_layers > 0:
self.gcc = None
self.phi_transformers = nn.ModuleList(
[
PhiStageTransformer(
d_phi=cfg.d_phi,
n_heads=cfg.phi_tx_heads,
n_layers=cfg.phi_tx_layers,
ffn_dim=cfg.phi_tx_ffn,
dropout=cfg.phi_tx_dropout,
)
for _ in range(n_stages)
]
)
else:
self.gcc = None
self.phi_transformers = None
self.e2_cwt_branch = cfg.e2_cwt_branch
if cfg.e2_cwt_branch:
if cfg.e2_cross_path2 == "orig_cnn":
self.path2_pools = nn.ModuleList(
[
CrossDomainStagePool(
cfg.d_h,
d_k=cfg.angular_attn_d_k,
dropout=cfg.angular_attn_dropout,
norm_mode=norm_mode,
query_side="orig",
)
for _ in range(n_stages)
]
)
else:
self.path2_pools = None
in_ch = cwt_pyramid_in_channels(cfg.cwt_n_scales)
self.cwt_pyramid = CWTPyramid(
in_channels=in_ch,
dims=cfg.pyramid_dims,
depths=cfg.pyramid_depths,
stem_kernel=cfg.stem_kernel,
stem_stride=cfg.stem_stride,
dw_kernel=cfg.dw_kernel,
mlp_ratio=cfg.mlp_ratio,
)
self.cwt_stage_proj = CWTStageProjector(cfg.pyramid_dims, cfg.d_h)
else:
self.path2_pools = None
self.cwt_pyramid = None
self.cwt_stage_proj = None
def _valid_at_stage(
self,
sid: int,
valid_ang: Optional[torch.Tensor],
phi_i0: List[torch.Tensor],
phi_i1: List[torch.Tensor],
phi_w: List[torch.Tensor],
) -> Optional[torch.Tensor]:
if valid_ang is None:
return None
return _interp_theta_batched(
valid_ang.unsqueeze(-1).float(),
phi_i0[sid],
phi_i1[sid],
phi_w[sid],
).squeeze(-1)
def _compute_cwt_projected(
self, cwt_scalo: torch.Tensor
) -> List[torch.Tensor]:
assert self.cwt_pyramid is not None and self.cwt_stage_proj is not None
cwt_feats = self.cwt_pyramid(cwt_scalo)
return [
self.cwt_stage_proj(sid, feat) for sid, feat in enumerate(cwt_feats)
]
def _encode_phi_list(
self,
cwt_scalo: torch.Tensor,
phi_cwt: torch.Tensor,
valid_ang: Optional[torch.Tensor],
n_stages: int,
stage_lengths: Optional[Sequence[int]] = None,
) -> List[torch.Tensor]:
if self.stage_phi_enc is not None:
if isinstance(self.stage_phi_enc, CWTStagePhiEncoderValid):
phi_raw_list = self.stage_phi_enc(cwt_scalo, valid_ang)
else:
phi_raw_list = self.stage_phi_enc(cwt_scalo)
else:
phi_raw_list = [phi_cwt] * n_stages
if self.gcc is not None:
return self.gcc(phi_raw_list, valid_ang, stage_lengths=stage_lengths)
if self.phi_transformers is not None:
return [
tx(phi_raw, valid_ang)
for tx, phi_raw in zip(self.phi_transformers, phi_raw_list)
]
return phi_raw_list
def _pool_path1(
self,
projected: List[torch.Tensor],
phi_full_list: List[torch.Tensor],
phi_i0: List[torch.Tensor],
phi_i1: List[torch.Tensor],
phi_w: List[torch.Tensor],
valid_ang: Optional[torch.Tensor],
) -> Tuple[torch.Tensor, torch.Tensor]:
vecs: List[torch.Tensor] = []
weights: List[torch.Tensor] = []
for sid, (pool, h_s, phi_full) in enumerate(
zip(self.pools, projected, phi_full_list)
):
if self.gcc_enabled:
phi_s = phi_full
else:
phi_s = _interp_theta_batched(
phi_full, phi_i0[sid], phi_i1[sid], phi_w[sid]
)
valid_s = self._valid_at_stage(sid, valid_ang, phi_i0, phi_i1, phi_w)
v_s, w_s = pool(phi_s, h_s, valid_s)
vecs.append(v_s)
weights.append(w_s)
max_l = max(w.size(1) for w in weights)
padded = []
for w in weights:
if w.size(1) < max_l:
w = F.pad(w, (0, max_l - w.size(1)))
padded.append(w)
w_ang = torch.stack(padded, dim=-1)
return torch.cat(vecs, dim=-1), w_ang
def _pool_path2(
self,
projected: List[torch.Tensor],
cwt_projected: List[torch.Tensor],
valid_ang: Optional[torch.Tensor],
phi_i0: List[torch.Tensor],
phi_i1: List[torch.Tensor],
phi_w: List[torch.Tensor],
) -> torch.Tensor:
vecs: List[torch.Tensor] = []
if self.path2_pools is not None:
for sid, pool in enumerate(self.path2_pools):
valid_s = self._valid_at_stage(sid, valid_ang, phi_i0, phi_i1, phi_w)
v_s, _ = pool(projected[sid], cwt_projected[sid], valid_s)
vecs.append(v_s)
else:
for sid, h_s in enumerate(cwt_projected):
valid_s = self._valid_at_stage(sid, valid_ang, phi_i0, phi_i1, phi_w)
vecs.append(
pool_sequence_v3(h_s.transpose(1, 2), "max", valid_s)
)
return torch.cat(vecs, dim=-1)
def forward(
self,
projected: List[torch.Tensor],
phi_cwt: torch.Tensor,
cwt_scalo: Optional[torch.Tensor],
phi_i0: List[torch.Tensor],
phi_i1: List[torch.Tensor],
phi_w: List[torch.Tensor],
valid_ang: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
projected: 各 stage H_s (B, L_s, d_h)
phi_cwt: (B, N_theta, d_phi) — scale_affinity=False 时使用
"""
n_stages = len(projected)
stage_lengths = [h_s.size(1) for h_s in projected]
if self.stage_phi_enc is not None:
assert cwt_scalo is not None
phi_full_list = self._encode_phi_list(
cwt_scalo, phi_cwt, valid_ang, n_stages, stage_lengths=stage_lengths
)
else:
phi_full_list = [phi_cwt] * n_stages
f_gapa, w_ang = self._pool_path1(
projected, phi_full_list, phi_i0, phi_i1, phi_w, valid_ang
)
if self.e2_cwt_branch:
assert cwt_scalo is not None
cwt_projected = self._compute_cwt_projected(cwt_scalo)
f_path2 = self._pool_path2(
projected, cwt_projected, valid_ang, phi_i0, phi_i1, phi_w
)
return torch.cat([f_gapa, f_path2], dim=-1), w_ang
return f_gapa, w_ang
class CWTDualStagePool(nn.Module):
"""
E3 双域 1024:路1 + 路2 独立配置。
路1 e3_cross_query:phi(φ→H_orig)| cwt_cnn(H_cwt→H_orig,默认)
路2 e3_cross_query_path2:maxpool(默认)| orig_cnn(H_orig→H_cwt)
e3_cwt_branch=False 时仅路1(E3a 消融,512 维)。
"""
def __init__(self, cfg: "Section43ConfigV3_1"):
super().__init__()
self.cfg = cfg
n_stages = len(cfg.pyramid_dims)
norm_mode: AngularNormMode = cfg.angular_norm # type: ignore[assignment]
if cfg.e3_cross_query == "cwt_cnn":
self.pools = nn.ModuleList(
[
CrossDomainStagePool(
cfg.d_h,
d_k=cfg.angular_attn_d_k,
dropout=cfg.angular_attn_dropout,
norm_mode=norm_mode,
query_side="cwt",
)
for _ in range(n_stages)
]
)
self.stage_phi_enc = None
elif cfg.e3_cross_query == "phi":
self.pools = nn.ModuleList(
[
CWTAngularPool(
cfg.d_phi,
cfg.d_h,
d_k=cfg.angular_attn_d_k,
dropout=cfg.angular_attn_dropout,
norm_mode=norm_mode,
)
for _ in range(n_stages)
]
)
if cfg.cwt_scale_affinity:
self.stage_phi_enc = _make_cwt_stage_phi_encoder(cfg)
else:
self.stage_phi_enc = None
else:
raise ValueError(f"未知 e3_cross_query: {cfg.e3_cross_query}")
self.e3_cwt_branch = cfg.e3_cwt_branch
if cfg.e3_cross_query_path2 == "orig_cnn":
self.path2_pools = nn.ModuleList(
[
CrossDomainStagePool(
cfg.d_h,
d_k=cfg.angular_attn_d_k,
dropout=cfg.angular_attn_dropout,
norm_mode=norm_mode,
query_side="orig",
)
for _ in range(n_stages)
]
)
else:
self.path2_pools = None
needs_cwt_feats = cfg.e3_cwt_branch or cfg.e3_cross_query == "cwt_cnn"
if needs_cwt_feats:
in_ch = cwt_pyramid_in_channels(cfg.cwt_n_scales)
self.cwt_pyramid = CWTPyramid(
in_channels=in_ch,
dims=cfg.pyramid_dims,
depths=cfg.pyramid_depths,
stem_kernel=cfg.stem_kernel,
stem_stride=cfg.stem_stride,
dw_kernel=cfg.dw_kernel,
mlp_ratio=cfg.mlp_ratio,
)
self.cwt_stage_proj = CWTStageProjector(cfg.pyramid_dims, cfg.d_h)
else:
self.cwt_pyramid = None
self.cwt_stage_proj = None
def _valid_at_stage(
self,
sid: int,
valid_ang: Optional[torch.Tensor],
phi_i0: List[torch.Tensor],
phi_i1: List[torch.Tensor],
phi_w: List[torch.Tensor],
) -> Optional[torch.Tensor]:
if valid_ang is None:
return None
return _interp_theta_batched(
valid_ang.unsqueeze(-1).float(),
phi_i0[sid],
phi_i1[sid],
phi_w[sid],
).squeeze(-1)
def _compute_cwt_projected(
self, cwt_scalo: torch.Tensor
) -> List[torch.Tensor]:
assert self.cwt_pyramid is not None and self.cwt_stage_proj is not None
cwt_feats = self.cwt_pyramid(cwt_scalo)
return [
self.cwt_stage_proj(sid, feat) for sid, feat in enumerate(cwt_feats)
]
def _pool_path1(
self,
projected: List[torch.Tensor],
phi_cwt: torch.Tensor,
cwt_scalo: Optional[torch.Tensor],
phi_i0: List[torch.Tensor],
phi_i1: List[torch.Tensor],
phi_w: List[torch.Tensor],
cwt_projected: Optional[List[torch.Tensor]],
valid_ang: Optional[torch.Tensor],
) -> Tuple[torch.Tensor, torch.Tensor]:
if self.stage_phi_enc is not None:
assert cwt_scalo is not None
phi_full_list = self.stage_phi_enc(cwt_scalo)
else:
phi_full_list = [phi_cwt] * len(projected)
vecs: List[torch.Tensor] = []
weights: List[torch.Tensor] = []
for sid, (pool, h_orig) in enumerate(zip(self.pools, projected)):
valid_s = self._valid_at_stage(sid, valid_ang, phi_i0, phi_i1, phi_w)
if self.cfg.e3_cross_query == "cwt_cnn":
assert cwt_projected is not None
v_s, w_s = pool(h_orig, cwt_projected[sid], valid_s)
else:
phi_s = _interp_theta_batched(
phi_full_list[sid], phi_i0[sid], phi_i1[sid], phi_w[sid]
)
v_s, w_s = pool(phi_s, h_orig, valid_s)
vecs.append(v_s)
weights.append(w_s)
max_l = max(w.size(1) for w in weights)
padded = []
for w in weights:
if w.size(1) < max_l:
w = F.pad(w, (0, max_l - w.size(1)))
padded.append(w)
w_ang = torch.stack(padded, dim=-1)
return torch.cat(vecs, dim=-1), w_ang
def _pool_path2(
self,
projected: List[torch.Tensor],
cwt_projected: List[torch.Tensor],
valid_ang: Optional[torch.Tensor],
phi_i0: List[torch.Tensor],
phi_i1: List[torch.Tensor],
phi_w: List[torch.Tensor],
) -> torch.Tensor:
vecs: List[torch.Tensor] = []
if self.path2_pools is not None:
for sid, pool in enumerate(self.path2_pools):
valid_s = self._valid_at_stage(sid, valid_ang, phi_i0, phi_i1, phi_w)
v_s, _ = pool(projected[sid], cwt_projected[sid], valid_s)
vecs.append(v_s)
else:
for sid, h_s in enumerate(cwt_projected):
valid_s = self._valid_at_stage(sid, valid_ang, phi_i0, phi_i1, phi_w)
vecs.append(
pool_sequence_v3(h_s.transpose(1, 2), "max", valid_s)
)
return torch.cat(vecs, dim=-1)
def forward(
self,
projected: List[torch.Tensor],
phi_cwt: torch.Tensor,
cwt_scalo: Optional[torch.Tensor],
phi_i0: List[torch.Tensor],
phi_i1: List[torch.Tensor],
phi_w: List[torch.Tensor],
valid_ang: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
cwt_projected: Optional[List[torch.Tensor]] = None
if self.cwt_pyramid is not None and cwt_scalo is not None:
cwt_projected = self._compute_cwt_projected(cwt_scalo)
f_gapa, w_ang = self._pool_path1(
projected,
phi_cwt,
cwt_scalo,
phi_i0,
phi_i1,
phi_w,
cwt_projected,
valid_ang,
)
if self.e3_cwt_branch:
assert cwt_projected is not None
f_path2 = self._pool_path2(
projected, cwt_projected, valid_ang, phi_i0, phi_i1, phi_w
)
return torch.cat([f_gapa, f_path2], dim=-1), w_ang
return f_gapa, w_ang
StagePoolMode = str # "global" | "seg_sigmoid" | "dbsa" | "cwt_angular" | "cwt_dual"
@dataclass
class Section43ConfigV3_1(Section43ConfigV3):
d_h: int = 128
stage_pool_mode: StagePoolMode = "dbsa"
seg_gate_hidden: int = 64
seg_gate_dropout: float = 0.2
d_amp: int = 64
amp_mlp_hidden: int = 128
dbsa_d_model: int = 128
dbsa_n_heads: int = 4
dbsa_n_layers: int = 2
dbsa_coarse_d_prime: int = 32
# CWT-GAPA
d_phi: int = 64
cwt_n_scales: int = 40
cwt_wavelet: str = "morl"
cwt_scale_affinity: bool = False
cwt_scale_index_mode: str = "uniform" # uniform | physics
angular_attn_d_k: int = 64
angular_attn_dropout: float = 0.1
# E3 双域
angular_norm: AngularNormMode = "sum_relu"
e3_cross_query: str = "cwt_cnn" # "phi" | "cwt_cnn"
e3_cross_query_path2: str = "maxpool" # "maxpool" | "orig_cnn"
e3_cwt_branch: bool = True # False → E3a 仅路1
# E2b
e2_cwt_branch: bool = False
cwt_phi_valid_emb: int = 0
e2_cross_path2: str = "orig_cnn" # maxpool | orig_cnn
phi_tx_layers: int = 0
phi_tx_heads: int = 4
phi_tx_ffn: int = 256
phi_tx_dropout: float = 0.1
# E2c GCC-φ-Pyramid
gcc_enabled: bool = False
gcc_gate_init: float = -2.0
gcc_d_stage_emb: int = 16
gcc_dw_kernels_bu: Tuple[int, ...] = (3, 5, 7)
gcc_dw_kernels_td: Tuple[int, ...] = (7, 5, 3)
@property
def d_ellipse(self) -> int:
n = self.d_h * len(self.pyramid_dims)
if self.stage_pool_mode == "cwt_dual" and self.e3_cwt_branch:
return n * 2
if self.stage_pool_mode == "cwt_angular" and self.e2_cwt_branch:
return n * 2
return n
def scheme_label(self) -> str:
tag = f"cat{self.d_ellipse}"
if self.stage_pool_mode == "cwt_dual":
q1 = "phi" if self.e3_cross_query == "phi" else "cwtCNN"
q2 = (
self.e3_cross_query_path2
if self.e3_cwt_branch
else "off"
)
br_tag = "dual" if self.e3_cwt_branch else "path1-only"
return f"v3.1(E3-{q1}+{q2}-{self.angular_norm}-{br_tag}-{tag})"
if self.stage_pool_mode == "cwt_angular":
if self.e2_cwt_branch or self.cwt_phi_valid_emb > 0 or self.phi_tx_layers > 0:
aff = "scaleAff" if self.cwt_scale_affinity else "unified"
if self.cwt_scale_affinity and self.cwt_scale_index_mode == "physics":
aff = "scaleAff-physics"
valid_tag = (
f"validPhi{self.cwt_phi_valid_emb}"
if self.cwt_phi_valid_emb > 0
else "noValidPhi"
)
tx_tag = f"phiTx{self.phi_tx_layers}L" if self.phi_tx_layers > 0 else "noPhiTx"
p2 = self.e2_cross_path2 if self.e2_cwt_branch else "off"
dual_tag = "dual" if self.e2_cwt_branch else "single"
gcc_tag = "-GCC" if self.gcc_enabled else ""
return (
f"v3.1(E2{'c' if self.gcc_enabled else 'b'}-CWT-GAPA-{aff}-{valid_tag}-"
f"{tx_tag if not self.gcc_enabled else 'gccTx' + tx_tag}-"
f"{self.angular_norm}-{p2}-{dual_tag}{gcc_tag}-{tag})"
)
aff = "scaleAff" if self.cwt_scale_affinity else "unified"
if self.cwt_scale_affinity and self.cwt_scale_index_mode == "physics":
aff = "scaleAff-physics"
norm_tag = f"-{self.angular_norm}" if self.angular_norm != "sum_relu" else ""
return f"v3.1(CWT-GAPA-{aff}{norm_tag}-{tag})"
if self.stage_pool_mode == "dbsa":
return f"v3.1(DBSA-{tag})"
if self.stage_pool_mode == "seg_sigmoid":
return f"v3.1(FGSS-segSigmoid-{tag})"
return f"v3.1(global-{self.pool_mode}-{tag})"
class Section43EncoderV3_1(nn.Module):
def __init__(self, config: Optional[Section43ConfigV3_1] = None):
super().__init__()
self.config = config or Section43ConfigV3_1()
cfg = self.config
self.geom = build_segment_geometry(cfg.n_theta, cfg.n_segments)
self.s_l = self.geom.s_l
self.M = self.geom.n_segments
self.n_stages = len(cfg.pyramid_dims)
self.pyramid = ConvNeXt1DPyramid(
in_channels=cfg.d_seq,
dims=cfg.pyramid_dims,
depths=cfg.pyramid_depths,
stem_kernel=cfg.stem_kernel,
stem_stride=cfg.stem_stride,
dw_kernel=cfg.dw_kernel,
mlp_ratio=cfg.mlp_ratio,
)
self.stage_proj = StageProjector(cfg.pyramid_dims, cfg.d_h)
self.stage_dropout = nn.Dropout(0.2)
if cfg.stage_pool_mode == "dbsa":
self.segment_pool: Optional[nn.Module] = DBSASegmentPool(cfg, self.s_l)
elif cfg.stage_pool_mode == "seg_sigmoid":
self.segment_pool = SegmentSigmoidStagePool(
self.n_stages,
cfg.d_freq,
cfg.d_h,
cfg.seg_gate_hidden,
cfg.seg_gate_dropout,
)
elif cfg.stage_pool_mode == "cwt_angular":
self.segment_pool = CWTAngularStagePool(cfg)
elif cfg.stage_pool_mode == "cwt_dual":
self.segment_pool = CWTDualStagePool(cfg)
else:
self.segment_pool = None
self._register_align_buffers()
self._phi_n_stages = 0
if cfg.stage_pool_mode in ("cwt_angular", "cwt_dual"):
self._register_phi_interp_buffers()
def _register_align_buffers(self) -> None:
seg_theta = np.stack(
[responsibility_indices(m, self.geom) for m in range(self.M)],
axis=0,
)
seg_theta_t = torch.from_numpy(seg_theta.astype(np.float32))
n_theta = float(self.config.n_theta)
with torch.no_grad():
dummy = torch.zeros(1, self.config.d_seq, self.config.n_theta)
_, pyramid_feats = self.pyramid(dummy)
i0_list: List[torch.Tensor] = []
i1_list: List[torch.Tensor] = []
w_list: List[torch.Tensor] = []
for P_s in pyramid_feats:
L_s = P_s.shape[-1]
pos = seg_theta_t * (float(L_s) / n_theta)
pos = pos.clamp(0, max(L_s - 1, 0))
i0 = pos.long()
i1 = torch.clamp(i0 + 1, max=L_s - 1)
w = (pos - i0.float()).unsqueeze(-1)
i0_list.append(i0)
i1_list.append(i1)
w_list.append(w)
self.register_buffer("align_i0", torch.stack(i0_list, dim=0), persistent=False)
self.register_buffer("align_i1", torch.stack(i1_list, dim=0), persistent=False)
self.register_buffer("align_w", torch.stack(w_list, dim=0), persistent=False)
def _register_phi_interp_buffers(self) -> None:
"""φ(t) 从 N_theta 线性插值到各 stage 长度 L_s。"""
n_theta = float(self.config.n_theta)
with torch.no_grad():
dummy = torch.zeros(1, self.config.d_seq, self.config.n_theta)
_, pyramid_feats = self.pyramid(dummy)
i0_list: List[torch.Tensor] = []
i1_list: List[torch.Tensor] = []
w_list: List[torch.Tensor] = []
for P_s in pyramid_feats:
l_s = P_s.shape[-1]
tau = torch.arange(l_s, dtype=torch.float32)
pos = tau * (n_theta / float(l_s))
pos = pos.clamp(0, max(n_theta - 1, 0))
i0 = pos.long()
i1 = torch.clamp(i0 + 1, max=int(n_theta) - 1)
w = (pos - i0.float()).unsqueeze(-1)
i0_list.append(i0)
i1_list.append(i1)
w_list.append(w)
for sid, (i0, i1, w) in enumerate(zip(i0_list, i1_list, w_list)):
self.register_buffer(f"phi_i0_{sid}", i0, persistent=False)
self.register_buffer(f"phi_i1_{sid}", i1, persistent=False)
self.register_buffer(f"phi_w_{sid}", w, persistent=False)
self._phi_n_stages = len(i0_list)
def _project_pyramid_stages(
self,
pyramid_feats: List[torch.Tensor],
*,
training: bool = False,
) -> List[torch.Tensor]:
projected: List[torch.Tensor] = []
for sid, P_s in enumerate(pyramid_feats):
H_s = self.stage_proj(sid, P_s.transpose(1, 2))
if training:
H_s = self.stage_dropout(H_s)
projected.append(H_s)
return projected
def _align_projected_stages(self, projected: List[torch.Tensor]) -> torch.Tensor:
aligned_stages = []
for sid, H_s in enumerate(projected):
h = _interp_segments_batched(
H_s,
self.align_i0[sid],
self.align_i1[sid],
self.align_w[sid],
)
aligned_stages.append(h.unsqueeze(2))
return torch.cat(aligned_stages, dim=2)
def _pool_stage_vectors_global(self, projected: List[torch.Tensor]) -> torch.Tensor:
vecs = [
pool_sequence_v3(H_s.transpose(1, 2), self.config.pool_mode)
for H_s in projected
]
return torch.cat(vecs, dim=-1)
def _encode_s_seq(
self,
s_seq: torch.Tensor,
*,
training: bool = False,
) -> Tuple[List[torch.Tensor], List[torch.Tensor], Optional[torch.Tensor]]:
"""
§4.3.1 ConvNeXt1D 金字塔 + stage 投影(与 stage_pool_mode 无关)。
与 DBSA/global/seg_sigmoid 训练 checkpoint 共享 enc43.pyramid / enc43.stage_proj 权重。
"""
_, pyramid_feats = self.pyramid(s_seq.transpose(1, 2))
projected = self._project_pyramid_stages(pyramid_feats, training=training)
h_stack: Optional[torch.Tensor] = None
if isinstance(self.segment_pool, (DBSASegmentPool, SegmentSigmoidStagePool)):
h_stack = self._align_projected_stages(projected)
return projected, pyramid_feats, h_stack
def _resolve_f_bar(
self,
f_freq: Optional[torch.Tensor],
batch_size: int,
device: torch.device,
dtype: torch.dtype,
) -> torch.Tensor:
cfg = self.config
if f_freq is None:
return torch.zeros(batch_size, self.M, cfg.d_freq, device=device, dtype=dtype)
if f_freq.dim() == 3:
f_freq = f_freq.unsqueeze(0)
return f_freq.mean(dim=-1)
def _resolve_amp(
self,
amp_fuse: Optional[torch.Tensor],
amp_global_bar: Optional[torch.Tensor],
batch_size: int,
device: torch.device,
dtype: torch.dtype,
) -> Tuple[torch.Tensor, torch.Tensor]:
if amp_fuse is not None and amp_global_bar is not None:
if amp_fuse.dim() == 3:
amp_fuse = amp_fuse.unsqueeze(0)
if amp_global_bar.dim() == 2:
amp_global_bar = amp_global_bar.unsqueeze(0)
return amp_fuse.to(device=device, dtype=dtype), amp_global_bar.to(
device=device, dtype=dtype
)
zeros_fuse = torch.zeros(
batch_size, self.M, self.s_l, S_RES_CHANNELS, device=device, dtype=dtype
)
zeros_glob = torch.zeros(
batch_size, self.s_l, S_RES_CHANNELS, device=device, dtype=dtype
)
return zeros_fuse, zeros_glob
def _resolve_phi_cwt(
self,
phi_cwt: Optional[torch.Tensor],
batch_size: int,
device: torch.device,
dtype: torch.dtype,
) -> torch.Tensor:
if phi_cwt is None:
return torch.zeros(
batch_size,
self.config.n_theta,
self.config.d_phi,
device=device,
dtype=dtype,
)
if phi_cwt.dim() == 2:
phi_cwt = phi_cwt.unsqueeze(0)
return phi_cwt.to(device=device, dtype=dtype)
def _resolve_cwt_scalo(
self,
cwt_scalo: Optional[torch.Tensor],
batch_size: int,
device: torch.device,
dtype: torch.dtype,
) -> Optional[torch.Tensor]:
if cwt_scalo is None:
return None
if cwt_scalo.dim() == 3:
cwt_scalo = cwt_scalo.unsqueeze(0)
return cwt_scalo.to(device=device, dtype=dtype)
def forward(
self,
s_seq: torch.Tensor,
f_freq: Optional[torch.Tensor] = None,
g_vec: Optional[torch.Tensor] = None,
e_pen: Optional[torch.Tensor] = None,
*,
amp_fuse: Optional[torch.Tensor] = None,
amp_global_bar: Optional[torch.Tensor] = None,
phi_cwt: Optional[torch.Tensor] = None,
cwt_scalo: Optional[torch.Tensor] = None,
valid_mask: Optional[torch.Tensor] = None,
training: bool = False,
return_details: bool = True,
) -> Dict[str, torch.Tensor]:
del g_vec, e_pen
cfg = self.config
batched = s_seq.dim() == 3
if not batched:
s_seq = s_seq.unsqueeze(0)
if f_freq is not None and f_freq.dim() == 3:
f_freq = f_freq.unsqueeze(0)
if amp_fuse is not None and amp_fuse.dim() == 3:
amp_fuse = amp_fuse.unsqueeze(0)
if amp_global_bar is not None and amp_global_bar.dim() == 2:
amp_global_bar = amp_global_bar.unsqueeze(0)
if phi_cwt is not None and phi_cwt.dim() == 2:
phi_cwt = phi_cwt.unsqueeze(0)
if cwt_scalo is not None and cwt_scalo.dim() == 3:
cwt_scalo = cwt_scalo.unsqueeze(0)
if valid_mask is not None and valid_mask.dim() == 1:
valid_mask = valid_mask.unsqueeze(0)
b = s_seq.size(0)
device = s_seq.device
dtype = s_seq.dtype
valid_seg: Optional[torch.Tensor] = None
valid_ang: Optional[torch.Tensor] = None
if valid_mask is not None:
if valid_mask.shape[-1] == self.M * self.s_l:
valid_seg = valid_mask.reshape(b, self.M, self.s_l)
elif valid_mask.shape[-2:] == (self.M, self.s_l):
valid_seg = valid_mask
if valid_mask.shape[-1] == self.config.n_theta:
valid_ang = valid_mask
elif valid_mask.dim() == 2 and valid_mask.shape[-1] == self.config.n_theta:
valid_ang = valid_mask
projected, pyramid_feats, h_stack = self._encode_s_seq(
s_seq, training=training
)
phi_loc = phi_glob = None
w_seg = None
w_ang = None
h_seg = None
if isinstance(self.segment_pool, DBSASegmentPool):
amp_f, amp_g = self._resolve_amp(amp_fuse, amp_global_bar, b, device, dtype)
f_ellipse, w_seg, h_seg, phi_loc, phi_glob = self.segment_pool(
h_stack, amp_f, amp_g, valid_seg
)
elif isinstance(self.segment_pool, SegmentSigmoidStagePool):
f_bar = self._resolve_f_bar(f_freq, b, device, dtype)
f_ellipse, w_seg, h_seg = self.segment_pool(h_stack, f_bar, valid_seg)
elif isinstance(self.segment_pool, CWTAngularStagePool):
phi_cwt_r = self._resolve_phi_cwt(phi_cwt, b, device, dtype)
cwt_scalo_r = self._resolve_cwt_scalo(cwt_scalo, b, device, dtype)
f_ellipse, w_ang = self.segment_pool(
projected,
phi_cwt_r,
cwt_scalo_r,
[getattr(self, f"phi_i0_{sid}") for sid in range(self._phi_n_stages)],
[getattr(self, f"phi_i1_{sid}") for sid in range(self._phi_n_stages)],
[getattr(self, f"phi_w_{sid}") for sid in range(self._phi_n_stages)],
valid_ang,
)
elif isinstance(self.segment_pool, CWTDualStagePool):
phi_cwt_r = self._resolve_phi_cwt(phi_cwt, b, device, dtype)
cwt_scalo_r = self._resolve_cwt_scalo(cwt_scalo, b, device, dtype)
f_ellipse, w_ang = self.segment_pool(
projected,
phi_cwt_r,
cwt_scalo_r,
[getattr(self, f"phi_i0_{sid}") for sid in range(self._phi_n_stages)],
[getattr(self, f"phi_i1_{sid}") for sid in range(self._phi_n_stages)],
[getattr(self, f"phi_w_{sid}") for sid in range(self._phi_n_stages)],
valid_ang,
)
else:
f_ellipse = self._pool_stage_vectors_global(projected)
if return_details and h_seg is None and h_stack is not None:
h_seg = h_stack.mean(dim=3)
out: Dict[str, torch.Tensor] = {"f_ellipse": f_ellipse, "s_smooth": s_seq}
if w_seg is not None:
out["W_seg"] = w_seg
if w_ang is not None:
out["W_ang"] = w_ang
if return_details:
if h_stack is not None:
out["H_aligned"] = h_stack
out["h_seg"] = h_seg
out["pyramid_feats"] = pyramid_feats
if phi_loc is not None:
out["phi_loc"] = phi_loc
out["phi_glob"] = phi_glob
if phi_cwt is not None:
out["phi_cwt"] = phi_cwt if batched else phi_cwt.unsqueeze(0)
if not isinstance(self.segment_pool, DBSASegmentPool):
if cfg.stage_pool_mode not in ("cwt_angular", "cwt_dual"):
out["f_bar"] = self._resolve_f_bar(f_freq, b, device, dtype)
if not batched:
out = _squeeze_batch(out)
return out
def forward_from_upstream(
self,
o41: Dict[str, torch.Tensor],
o42: Dict[str, torch.Tensor],
e_pen: Optional[torch.Tensor] = None,
*,
valid_mask: Optional[torch.Tensor] = None,
training: bool = False,
return_details: bool = True,
) -> Dict[str, torch.Tensor]:
del e_pen
return self.forward(
o41["s_seq"],
f_freq=o42.get("f_freq"),
amp_fuse=o42.get("amp_fuse"),
amp_global_bar=o42.get("amp_global_bar"),
phi_cwt=o42.get("phi_cwt"),
cwt_scalo=o42.get("cwt_scalo"),
valid_mask=valid_mask,
training=training,
return_details=return_details,
)
def _squeeze_batch(out: Dict) -> Dict:
squeezed = {}
for k, v in out.items():
if k == "pyramid_feats":
squeezed[k] = [t.squeeze(0) for t in v]
elif isinstance(v, torch.Tensor):
squeezed[k] = v.squeeze(0)
else:
squeezed[k] = v
return squeezed
def v3_1_needs_valid_mask(enc43: nn.Module) -> bool:
return isinstance(enc43, Section43EncoderV3_1)
if __name__ == "__main__":
dual_cases = [
("E3", {"e3_cross_query": "cwt_cnn", "e3_cross_query_path2": "maxpool"}),
("E3+", {"e3_cross_query": "cwt_cnn", "e3_cross_query_path2": "orig_cnn"}),
("E3-phi", {"e3_cross_query": "phi", "e3_cross_query_path2": "maxpool"}),
]
for mode in ("dbsa", "cwt_angular"):
cfg_kwargs = {"stage_pool_mode": mode}
cfg = Section43ConfigV3_1(**cfg_kwargs)
enc = Section43EncoderV3_1(cfg)
b = 2
m, sl = cfg.n_segments, enc.s_l
s_seq = torch.randn(b, cfg.n_theta, cfg.d_seq)
kwargs = {}
if mode == "dbsa":
kwargs = {
"amp_fuse": torch.randn(b, m, sl, 4),
"amp_global_bar": torch.randn(b, sl, 4),
}
else:
kwargs = {
"phi_cwt": torch.randn(b, cfg.n_theta, cfg.d_phi),
"cwt_scalo": torch.randn(b, 4, cfg.cwt_n_scales, cfg.n_theta),
}
out = enc(s_seq, training=True, return_details=True, **kwargs)
print("scheme:", cfg.scheme_label())
print("f_ellipse", tuple(out["f_ellipse"].shape))
for tag, dual_kw in dual_cases:
cfg = Section43ConfigV3_1(
stage_pool_mode="cwt_dual",
angular_norm="sum_relu",
**dual_kw,
)
enc = Section43EncoderV3_1(cfg)
b = 2
s_seq = torch.randn(b, cfg.n_theta, cfg.d_seq)
kwargs = {
"phi_cwt": torch.randn(b, cfg.n_theta, cfg.d_phi),
"cwt_scalo": torch.randn(b, 4, cfg.cwt_n_scales, cfg.n_theta),
}
out = enc(s_seq, training=True, return_details=True, **kwargs)
print(f"[{tag}] scheme:", cfg.scheme_label())
print(f"[{tag}] f_ellipse", tuple(out["f_ellipse"].shape))
cfg_e2b = Section43ConfigV3_1(
stage_pool_mode="cwt_angular",
cwt_scale_affinity=True,
angular_norm="sum_relu",
e2_cwt_branch=True,
cwt_phi_valid_emb=8,
phi_tx_layers=2,
e2_cross_path2="orig_cnn",
)
enc_e2b = Section43EncoderV3_1(cfg_e2b)
b = 2
s_seq = torch.randn(b, cfg_e2b.n_theta, cfg_e2b.d_seq)
valid = torch.ones(b, cfg_e2b.n_theta)
valid[:, :10] = 0.0
out_e2b = enc_e2b(
s_seq,
training=True,
return_details=False,
phi_cwt=torch.randn(b, cfg_e2b.n_theta, cfg_e2b.d_phi),
cwt_scalo=torch.randn(b, 4, cfg_e2b.cwt_n_scales, cfg_e2b.n_theta),
valid_mask=valid,
)
print("[E2b] scheme:", cfg_e2b.scheme_label())
print("[E2b] f_ellipse", tuple(out_e2b["f_ellipse"].shape))
"""
§4.3 v3.1:DBSA / FGSS 段级汇聚。
DBSA-v2(默认):
Step0 各 stage 段内 avg → h_s^(m)
Step1 vec(A_fuse^(m)), vec(Ā_global) → MLP_shared → φ_loc, φ_glob
Step2 stage1/2: [φ_glob; φ_loc^(m); h_s^(m)] × M tokens → 共享 2L Transformer
→ 各 stage 独立 weight head → σ → 归一化 Σ_m w_m=1
Step3 stage3/4: φ_glob 作 Query,对 h_s^(m) 交叉打分 → σ → 归一化
Step4 v_s = Σ_m w_m h_s^(m),Concat → f_ellipse
另保留 global / seg_sigmoid(FGSS)作消融。
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Dict, List, Literal, Optional, Sequence, Tuple
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from section42 import S_RES_CHANNELS, build_segment_geometry, responsibility_indices
from section42_cwt import (
CWTStagePhiEncoder,
CWTStagePhiEncoderValid,
CWTPyramid,
CWTStageProjector,
Section42CWTConfig,
build_scale_indices,
cwt_pyramid_in_channels,
)
from section43_v3 import (
ConvNeXt1DPyramid,
Section43ConfigV3,
pool_sequence_v3,
)
def _interp_segments_batched(
feat: torch.Tensor,
i0: torch.Tensor,
i1: torch.Tensor,
w: torch.Tensor,
) -> torch.Tensor:
f0 = feat[:, i0, :]
f1 = feat[:, i1, :]
return f0 * (1.0 - w) + f1 * w
def _segment_mean(
h: torch.Tensor,
valid_seg: Optional[torch.Tensor] = None,
) -> torch.Tensor:
"""h: (B, M, s_l, d_h) → (B, M, d_h)。"""
if valid_seg is None:
return h.mean(dim=2)
w = valid_seg.unsqueeze(-1).to(h.dtype)
denom = w.sum(dim=2).clamp(min=1e-6)
return (h * w).sum(dim=2) / denom
def _normalize_segment_weights(w_raw: torch.Tensor, dim: int = 1) -> torch.Tensor:
"""Sigmoid 后沿段维归一化,使 Σ_m w_m = 1。"""
w = torch.sigmoid(w_raw)
return w / w.sum(dim=dim, keepdim=True).clamp(min=1e-6)
class StageProjector(nn.Module):
def __init__(self, stage_dims: Tuple[int, ...], d_h: int):
super().__init__()
self.projs = nn.ModuleList([nn.Linear(c, d_h) for c in stage_dims])
def forward(self, stage_idx: int, x: torch.Tensor) -> torch.Tensor:
return self.projs[stage_idx](x)
class AmpSharedEncoder(nn.Module):
"""vec(A_fuse^(m)), vec(Ā_global) → 共享 MLP → φ_loc, φ_glob。"""
def __init__(self, s_l: int, n_ch: int = S_RES_CHANNELS, d_out: int = 64, hidden: int = 128):
super().__init__()
self.in_dim = s_l * n_ch
self.mlp = nn.Sequential(
nn.Linear(self.in_dim, hidden),
nn.GELU(),
nn.Dropout(0.1),
nn.Linear(hidden, d_out),
)
def forward(
self,
amp_fuse: torch.Tensor,
amp_global_bar: torch.Tensor,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
amp_fuse: (B, M, s_l, 4)
amp_global_bar: (B, s_l, 4)