File size: 54,001 Bytes
519ab98 | 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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 | """
§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))
|