dualmapref 0.2.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. args_parse.py +593 -0
  2. candidate_corrector.py +161 -0
  3. candidate_scorer.py +359 -0
  4. conformend/__init__.py +36 -0
  5. conformend/refiner.py +25 -0
  6. dataset/__init__.py +1 -0
  7. dataset/drugdataset.py +644 -0
  8. dataset/manifold.py +237 -0
  9. dataset/sharded_drugdataset.py +124 -0
  10. dataset/smiles2graph.py +217 -0
  11. dualmapref/__init__.py +24 -0
  12. dualmapref/refiner.py +16 -0
  13. dualmapref-0.2.8.dist-info/LICENSE +21 -0
  14. dualmapref-0.2.8.dist-info/METADATA +167 -0
  15. dualmapref-0.2.8.dist-info/RECORD +186 -0
  16. dualmapref-0.2.8.dist-info/WHEEL +5 -0
  17. dualmapref-0.2.8.dist-info/entry_points.txt +10 -0
  18. dualmapref-0.2.8.dist-info/top_level.txt +29 -0
  19. exputils.py +289 -0
  20. infer.py +23 -0
  21. infer_base_conformers.py +5589 -0
  22. infer_mean_dispersion_torsion.py +1117 -0
  23. infer_torsion.py +752 -0
  24. infer_torsional_denoiser.py +1021 -0
  25. manimol/__init__.py +30 -0
  26. manimol/__main__.py +6 -0
  27. manimol/cli.py +36 -0
  28. manimol/full_cli.py +41 -0
  29. manimol/legacy_imports.py +43 -0
  30. manimol/manifest.py +56 -0
  31. manimol/postprocess.py +148 -0
  32. manimol/priors.py +60 -0
  33. manimol/protocols.py +119 -0
  34. manimol/refiner.py +645 -0
  35. manimol/runtime.py +194 -0
  36. manimol/selection.py +85 -0
  37. manimol/torsion.py +22 -0
  38. manimol/train_cli.py +14 -0
  39. mixture_flow/src/__init__.py +1 -0
  40. mixture_flow/src/_path.py +7 -0
  41. mixture_flow/src/infer28_mixture_torsion_flow.py +1093 -0
  42. mixture_flow/src/mixture_torsion_flow.py +291 -0
  43. mixture_flow/src/summarize_mixture_debug.py +80 -0
  44. mixture_flow/src/train28_mixture_torsion_flow.py +583 -0
  45. mixture_torsion_flow.py +7 -0
  46. models/ manifold_learning.py +86 -0
  47. models/__init__.py +8 -0
  48. models/dist.py +273 -0
  49. models/dist2coords.py +23 -0
  50. models/egnn.py +107 -0
  51. models/experimental_backbones.py +364 -0
  52. models/gnnconv.py +512 -0
  53. models/kernel_inversion.py +141 -0
  54. models/kernels.py +427 -0
  55. models/losses.py +154 -0
  56. models/mean_dispersion_bridge.py +119 -0
  57. models/mean_dispersion_torsion_context.py +155 -0
  58. models/model.py +73 -0
  59. models/model0.py +2375 -0
  60. models/vis.py +128 -0
  61. pgraph_controls.py +107 -0
  62. scripts/__init__.py +2 -0
  63. scripts/analyze_basin_alignment.py +63 -0
  64. scripts/analyze_hif2a_conformer_aggregation.py +267 -0
  65. scripts/analyze_hif2a_contact_pharmacophore.py +1111 -0
  66. scripts/analyze_hif2a_fixed_budget.py +202 -0
  67. scripts/analyze_hif2a_pharmacophore.py +405 -0
  68. scripts/analyze_hif2a_pharmacophore_match_metric.py +386 -0
  69. scripts/analyze_p_control_candidate_flow.py +154 -0
  70. scripts/analyze_scaffold_basin.py +83 -0
  71. scripts/audit_dispersion_proposals.py +208 -0
  72. scripts/audit_etflow_protocol.py +258 -0
  73. scripts/audit_external_overlap.py +150 -0
  74. scripts/audit_oldbest_fulltrainval_split.py +82 -0
  75. scripts/audit_pdbbind_contact_pharmacophores.py +146 -0
  76. scripts/audit_qm9_geomol_test_split.py +94 -0
  77. scripts/audit_stage1_stage2_cross.py +131 -0
  78. scripts/build_geom_drugs_lowref_diagnostic.py +84 -0
  79. scripts/build_qm9_backfill_manifest.py +99 -0
  80. scripts/check_etflow_runtime.py +56 -0
  81. scripts/compare_prior_sources.py +122 -0
  82. scripts/compose_binding_mode_figure.py +87 -0
  83. scripts/compose_hif2a_contact_pharmacophore_suppfig.py +255 -0
  84. scripts/compose_hif2a_functional_geometry_pipeline.py +266 -0
  85. scripts/compose_manimol_internal_architecture.py +291 -0
  86. scripts/diagnose_flow_basin_intervention.py +54 -0
  87. scripts/diagnose_hif2a_pharmacophore_budget.py +185 -0
  88. scripts/diagnose_route2_p_ranking.py +123 -0
  89. scripts/discover_binding_mode_inputs.py +30 -0
  90. scripts/evaluate_functional_prior_stage1.py +230 -0
  91. scripts/evaluate_geomanimol.py +74 -0
  92. scripts/evaluate_geomanimol_flow.py +23 -0
  93. scripts/evaluate_geomanimol_multiscaffold.py +46 -0
  94. scripts/evaluate_geomanimol_residual.py +45 -0
  95. scripts/evaluate_geomanimol_torsion.py +40 -0
  96. scripts/evaluate_hif2a_bioactive_enrichment.py +91 -0
  97. scripts/evaluate_hif2a_pms_suite.py +440 -0
  98. scripts/evaluate_pdbbind_contact_pms.py +155 -0
  99. scripts/evaluate_platinum_external.py +114 -0
  100. scripts/evaluate_platinum_pool.py +82 -0
  101. scripts/evaluate_route2_calibrated_p.py +104 -0
  102. scripts/evaluate_route2_library_components.py +596 -0
  103. scripts/evaluate_route2_p_basin_budget.py +288 -0
  104. scripts/evaluate_stage1_checkpoint_only.py +30 -0
  105. scripts/export_dispersion_bond_audit.py +136 -0
  106. scripts/export_top20_shards.py +207 -0
  107. scripts/external_validation_metrics.py +152 -0
  108. scripts/fit_topology_prior.py +117 -0
  109. scripts/frozen_dispersion_audit.py +281 -0
  110. scripts/geomanimol_debug.py +50 -0
  111. scripts/geomanimol_smoke.py +37 -0
  112. scripts/infer_etflow_matched.py +524 -0
  113. scripts/inspect_checkpoint.py +12 -0
  114. scripts/inspect_conformer_capacity.py +71 -0
  115. scripts/inspect_pool.py +12 -0
  116. scripts/make_figure3_selection_loss.py +474 -0
  117. scripts/make_figure3_selection_summary.py +275 -0
  118. scripts/make_geom_debug_subset.py +62 -0
  119. scripts/make_low_reference_test_subset.py +70 -0
  120. scripts/merge_stage1_into_stage2.py +82 -0
  121. scripts/place_hif2a_candidates_for_evaluation.py +66 -0
  122. scripts/plot_hif2a_low_budget_efficiency.py +100 -0
  123. scripts/plot_prior_validation.py +48 -0
  124. scripts/precompute_stage2_assignments.py +90 -0
  125. scripts/prepare_bace_activity.py +98 -0
  126. scripts/prepare_bace_etkdg_benchmark.py +90 -0
  127. scripts/prepare_figure5_4x4_cases.py +470 -0
  128. scripts/prepare_geom_drugs_geomol_top20.py +472 -0
  129. scripts/prepare_pdbbind_core_raw.py +90 -0
  130. scripts/prepare_platinum_raw.py +74 -0
  131. scripts/prepare_pubchem_etkdg.py +54 -0
  132. scripts/rebuild_hif2a_final_sdf_and_metrics.py +55 -0
  133. scripts/rebuild_hif2a_fixed_pool_sweep.py +175 -0
  134. scripts/rebuild_hif2a_strict.py +220 -0
  135. scripts/recompute_hif2a_bootstrap.py +63 -0
  136. scripts/reference_free_geometry.py +112 -0
  137. scripts/render_binding_modes.py +413 -0
  138. scripts/render_figure5_4x4.py +330 -0
  139. scripts/render_figure6_pharmacophore_pymol.py +162 -0
  140. scripts/render_hif2a_contact_pharmacophore_pymol.py +414 -0
  141. scripts/render_hif2a_figure11.py +397 -0
  142. scripts/run_bace_2d_baseline.py +73 -0
  143. scripts/run_drugs_validation_matrix.py +225 -0
  144. scripts/run_external_reference_free.py +53 -0
  145. scripts/run_manimol_finalk_sensitivity.py +106 -0
  146. scripts/run_manimol_oversampling_sensitivity.py +109 -0
  147. scripts/run_pdisp_matched_intervention.py +116 -0
  148. scripts/run_stage1_stage2_cross20.py +83 -0
  149. scripts/score_hif2a_candidates_with_pgraph.py +221 -0
  150. scripts/summarize_dispersion_intervention.py +88 -0
  151. scripts/test_mean_dispersion_bridge.py +50 -0
  152. scripts/train_dispersion_head_diagnostic.py +320 -0
  153. scripts/train_dispersion_head_streaming.py +519 -0
  154. scripts/train_geomanimol.py +100 -0
  155. scripts/train_geomanimol_flow.py +26 -0
  156. scripts/train_geomanimol_residual.py +70 -0
  157. scripts/train_geomanimol_torsion.py +50 -0
  158. scripts/train_quantile_representation_diagnostic.py +204 -0
  159. scripts/validate_mean_dispersion_prior.py +173 -0
  160. scripts/validate_packed_shard.py +41 -0
  161. scripts/validate_stage1_prior.py +221 -0
  162. scripts/validate_top20_shards.py +36 -0
  163. scripts/validation/__init__.py +1 -0
  164. scripts/validation/hif2a_downstream_baselines.py +105 -0
  165. scripts/validation/hif2a_pairwise_ranker.py +218 -0
  166. scripts/verify_binding_mode_figure.py +47 -0
  167. torsion_manifold.py +281 -0
  168. train.py +78 -0
  169. train_graph_encoder.py +206 -0
  170. train_local_torsion_corrector.py +511 -0
  171. train_mean_dispersion_torsion_prior.py +329 -0
  172. train_probability_basin.py +235 -0
  173. train_torsion_generator.py +1631 -0
  174. train_torsion_manifold_prior.py +258 -0
  175. train_torsional_denoiser.py +415 -0
  176. trainer.py +1038 -0
  177. utils/__init__.py +7 -0
  178. utils/checkpoint.py +40 -0
  179. utils/device.py +17 -0
  180. utils/helpers.py +98 -0
  181. utils/kabsch.py +29 -0
  182. utils/lookup_table.py +164 -0
  183. utils/metrics.py +50 -0
  184. utils/optuna.py +111 -0
  185. utils/save_mol.py +366 -0
  186. utils/util.py +36 -0
models/model0.py ADDED
@@ -0,0 +1,2375 @@
1
+ # model0.py
2
+ # Probability-guided manifold molecular conformation generation
3
+ # Clean multi-stage version: gnn / coarse / refine / geom / multimode / joint
4
+ # Enhanced: probability map + chemical edge features dual-conditioned decoding
5
+
6
+ import math
7
+ from typing import Dict, List, Optional, Tuple, Any
8
+
9
+ import torch
10
+ import torch.nn as nn
11
+ import torch.nn.functional as F
12
+ from torch_geometric.nn import global_add_pool, global_max_pool, global_mean_pool
13
+
14
+ from .gnnconv import GNN_node, MLP
15
+ from utils.kabsch import kabsch_rmsd
16
+
17
+
18
+ # ============================================================
19
+ # Basic utilities
20
+ # ============================================================
21
+
22
+ def pairwise_distance(x: torch.Tensor, eps: float = 1e-8) -> torch.Tensor:
23
+ x = torch.nan_to_num(x, nan=0.0, posinf=0.0, neginf=0.0)
24
+ diff = x[:, None, :] - x[None, :, :]
25
+ return torch.sqrt((diff ** 2).sum(dim=-1) + eps)
26
+
27
+
28
+ def center_and_rescale(coords: torch.Tensor, target_rms: float = 1.0, eps: float = 1e-8) -> torch.Tensor:
29
+ coords = torch.nan_to_num(coords, nan=0.0, posinf=0.0, neginf=0.0)
30
+ coords = coords - coords.mean(dim=0, keepdim=True)
31
+ rms = torch.sqrt((coords ** 2).mean() + eps).clamp(min=eps)
32
+ coords = coords * (float(target_rms) / rms)
33
+ return torch.nan_to_num(coords, nan=0.0, posinf=0.0, neginf=0.0)
34
+
35
+
36
+ def cross_entropy_loss(P: torch.Tensor, Q: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:
37
+ P = torch.nan_to_num(P, nan=eps, posinf=1.0 - eps, neginf=eps).clamp(eps, 1.0 - eps)
38
+ Q = torch.nan_to_num(Q, nan=eps, posinf=1.0 - eps, neginf=eps).clamp(eps, 1.0 - eps)
39
+ loss = -(Q * torch.log(P) + (1.0 - Q) * torch.log(1.0 - P))
40
+ n = P.size(0)
41
+ if n <= 1:
42
+ return loss.sum() * 0.0
43
+ eye = torch.eye(n, dtype=torch.bool, device=P.device)
44
+ loss = loss.masked_fill(eye, 0.0)
45
+ return loss.sum() / max(1, n * (n - 1))
46
+
47
+
48
+ def weighted_cross_entropy_loss(
49
+ P: torch.Tensor,
50
+ Q: torch.Tensor,
51
+ weights: Optional[torch.Tensor] = None,
52
+ eps: float = 1e-6,
53
+ ) -> torch.Tensor:
54
+ """Pair-weighted BCE used by the isolated relevance-aware Stage-I variant."""
55
+ if weights is None:
56
+ return cross_entropy_loss(P, Q, eps=eps)
57
+ P = torch.nan_to_num(P, nan=eps, posinf=1.0 - eps, neginf=eps).clamp(eps, 1.0 - eps)
58
+ Q = torch.nan_to_num(Q, nan=eps, posinf=1.0 - eps, neginf=eps).clamp(eps, 1.0 - eps)
59
+ weights = torch.nan_to_num(weights, nan=1.0, posinf=1.0, neginf=1.0).clamp_min(0.0)
60
+ loss = -(Q * torch.log(P) + (1.0 - Q) * torch.log(1.0 - P))
61
+ n = P.size(0)
62
+ if n <= 1:
63
+ return loss.sum() * 0.0
64
+ off_diag = ~torch.eye(n, dtype=torch.bool, device=P.device)
65
+ weighted = loss[off_diag] * weights[off_diag]
66
+ return weighted.sum() / weights[off_diag].sum().clamp_min(eps)
67
+
68
+
69
+ def prob_low_dim(coords: torch.Tensor, a: torch.Tensor, b: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
70
+ a = torch.as_tensor(a, dtype=coords.dtype, device=coords.device).view(()).clamp(min=1e-6, max=1e6)
71
+ b = torch.as_tensor(b, dtype=coords.dtype, device=coords.device).view(()).clamp(min=1e-6, max=10.0)
72
+ dist = pairwise_distance(coords)
73
+ prob = 1.0 / (1.0 + a * torch.pow(dist.pow(2) + eps, b))
74
+ prob = torch.nan_to_num(prob, nan=0.0, posinf=1.0, neginf=0.0)
75
+ prob = (prob + prob.t()) * 0.5
76
+ prob = prob * (1.0 - torch.eye(prob.size(0), device=prob.device))
77
+ return prob.clamp(1e-6, 1.0 - 1e-6)
78
+
79
+
80
+ def prob_low_dim_kernel(
81
+ coords: torch.Tensor,
82
+ a: torch.Tensor,
83
+ b: torch.Tensor,
84
+ kernel: str = "umap",
85
+ eps: float = 1e-12,
86
+ ) -> torch.Tensor:
87
+ """Build a teacher probability graph with switchable kernels.
88
+
89
+ The default UMAP kernel keeps the previous learnable a,b behavior.
90
+ Gaussian/Laplacian/Cauchy/Matern/Triweight/Logistic use a as a learnable
91
+ positive bandwidth, while Student/Rational-Quadratic use b as a learnable
92
+ tail parameter. All variants return a symmetric zero-diagonal probability
93
+ matrix in (0, 1).
94
+ """
95
+ kernel = str(kernel).lower()
96
+ if kernel == "umap":
97
+ return prob_low_dim(coords, a, b, eps=eps)
98
+
99
+ dist = pairwise_distance(coords)
100
+ eye = torch.eye(dist.size(0), device=dist.device, dtype=dist.dtype)
101
+ bandwidth = torch.as_tensor(a, dtype=coords.dtype, device=coords.device).view(()).clamp(1e-3, 1e3)
102
+ shape = torch.as_tensor(b, dtype=coords.dtype, device=coords.device).view(()).clamp(1e-3, 10.0)
103
+
104
+ if kernel == "gaussian":
105
+ prob = torch.exp(-dist.pow(2) / (2.0 * bandwidth.pow(2) + eps))
106
+ elif kernel == "laplacian":
107
+ prob = torch.exp(-dist / (bandwidth + eps))
108
+ elif kernel == "student":
109
+ prob = 1.0 / (1.0 + dist.pow(2) / (shape + eps))
110
+ elif kernel == "cauchy":
111
+ prob = 1.0 / (1.0 + dist.pow(2) / (bandwidth.pow(2) + eps))
112
+ elif kernel == "rational_quadratic":
113
+ alpha = shape
114
+ prob = torch.pow(
115
+ 1.0 + dist.pow(2) / (2.0 * alpha * bandwidth.pow(2) + eps),
116
+ -alpha,
117
+ )
118
+ elif kernel == "inverse_multiquadric":
119
+ prob = torch.rsqrt(1.0 + dist.pow(2) / (bandwidth.pow(2) + eps))
120
+ elif kernel == "matern32":
121
+ r = math.sqrt(3.0) * dist / (bandwidth + eps)
122
+ prob = (1.0 + r) * torch.exp(-r)
123
+ elif kernel == "matern52":
124
+ r = math.sqrt(5.0) * dist / (bandwidth + eps)
125
+ prob = (1.0 + r + r.pow(2) / 3.0) * torch.exp(-r)
126
+ elif kernel == "triweight":
127
+ scaled = (dist / (bandwidth + eps)).clamp(min=0.0)
128
+ prob = torch.where(
129
+ scaled < 1.0,
130
+ torch.pow(1.0 - scaled.pow(2), 3),
131
+ torch.zeros_like(scaled),
132
+ )
133
+ elif kernel == "logistic":
134
+ # Smooth distance threshold. a controls the transition location, b the
135
+ # steepness. This keeps Q near 1 for close atom pairs and decays
136
+ # smoothly around the learned bandwidth.
137
+ steepness = shape.clamp(0.5, 10.0)
138
+ prob = torch.sigmoid(steepness * (bandwidth - dist))
139
+ else:
140
+ raise ValueError(f"Unknown kernel={kernel}")
141
+
142
+ prob = torch.nan_to_num(prob, nan=0.0, posinf=1.0, neginf=0.0)
143
+ prob = 0.5 * (prob + prob.t())
144
+ prob = prob * (1.0 - eye)
145
+ return prob.clamp(1e-6, 1.0 - 1e-6)
146
+
147
+
148
+ def _functional_atom_masks(mol, n_atoms: int, device: torch.device) -> Dict[str, torch.Tensor]:
149
+ """Infer protein-free atom roles used by the optional functional teacher.
150
+
151
+ These are intentionally coarse intramolecular chemical roles. They encode
152
+ no receptor, assay, docking, or crystal-complex information and therefore
153
+ remain available for ordinary GEOM training molecules.
154
+ """
155
+ masks = {
156
+ "hbd": torch.zeros(n_atoms, dtype=torch.bool, device=device),
157
+ "hba": torch.zeros(n_atoms, dtype=torch.bool, device=device),
158
+ "aromatic": torch.zeros(n_atoms, dtype=torch.bool, device=device),
159
+ "hydrophobic": torch.zeros(n_atoms, dtype=torch.bool, device=device),
160
+ }
161
+ if mol is None or not hasattr(mol, "GetNumAtoms") or int(mol.GetNumAtoms()) != n_atoms:
162
+ return masks
163
+
164
+ for atom in mol.GetAtoms():
165
+ idx = int(atom.GetIdx())
166
+ if idx < 0 or idx >= n_atoms:
167
+ continue
168
+ atomic_num = int(atom.GetAtomicNum())
169
+ formal_charge = int(atom.GetFormalCharge())
170
+ total_h = int(atom.GetTotalNumHs(includeNeighbors=True))
171
+ aromatic = bool(atom.GetIsAromatic())
172
+
173
+ # Conservative, protein-free HBD/HBA proxies. They are used only to
174
+ # reweight a geometry teacher, never as labels of protein interactions.
175
+ donor = atomic_num in {7, 8, 16} and total_h > 0 and formal_charge >= 0
176
+ acceptor = atomic_num in {7, 8, 16} and formal_charge <= 0
177
+ if atomic_num == 7 and formal_charge > 0:
178
+ acceptor = False
179
+
180
+ masks["hbd"][idx] = donor
181
+ masks["hba"][idx] = acceptor
182
+ masks["aromatic"][idx] = aromatic
183
+ masks["hydrophobic"][idx] = atomic_num in {6, 9, 17, 35, 53}
184
+ return masks
185
+
186
+
187
+ def functional_aware_teacher_graph(
188
+ q_geom: torch.Tensor,
189
+ mol,
190
+ args,
191
+ eps: float = 1e-6,
192
+ ) -> torch.Tensor:
193
+ """Build a protein-free functional-aware variant of a geometry teacher.
194
+
195
+ The base proximity probabilities are retained. Atom pairs with plausible
196
+ donor--acceptor, aromatic, or hydrophobic roles receive a modest relative
197
+ enrichment, after which the full graph is mixed back with Q_geom. This is
198
+ an ensemble-level intramolecular relational target, not a binding model.
199
+ """
200
+ n_atoms = int(q_geom.size(0))
201
+ if n_atoms <= 1:
202
+ return q_geom
203
+ masks = _functional_atom_masks(mol, n_atoms, q_geom.device)
204
+ weights = torch.ones_like(q_geom)
205
+ hbond_pairs = (masks["hbd"][:, None] & masks["hba"][None, :]) | (
206
+ masks["hba"][:, None] & masks["hbd"][None, :]
207
+ )
208
+ aromatic_pairs = masks["aromatic"][:, None] & masks["aromatic"][None, :]
209
+ hydrophobic_pairs = masks["hydrophobic"][:, None] & masks["hydrophobic"][None, :]
210
+ weights = weights + float(getattr(args, "functional_hbond_weight", 0.35)) * hbond_pairs.to(q_geom.dtype)
211
+ weights = weights + float(getattr(args, "functional_aromatic_weight", 0.20)) * aromatic_pairs.to(q_geom.dtype)
212
+ weights = weights + float(getattr(args, "functional_hydrophobic_weight", 0.10)) * hydrophobic_pairs.to(q_geom.dtype)
213
+
214
+ off_diag = ~torch.eye(n_atoms, dtype=torch.bool, device=q_geom.device)
215
+ mean_weight = weights[off_diag].mean().clamp_min(eps)
216
+ q_enriched = (q_geom * (weights / mean_weight)).clamp(eps, 1.0 - eps)
217
+ q_enriched = 0.5 * (q_enriched + q_enriched.t())
218
+ q_enriched = q_enriched.masked_fill(~off_diag, 0.0)
219
+
220
+ alpha = float(getattr(args, "functional_teacher_alpha", 0.80))
221
+ alpha = max(0.0, min(1.0, alpha))
222
+ q_mix = alpha * q_geom + (1.0 - alpha) * q_enriched
223
+ q_mix = 0.5 * (q_mix + q_mix.t())
224
+ q_mix = q_mix.masked_fill(~off_diag, 0.0)
225
+ return q_mix.clamp(eps, 1.0 - eps)
226
+
227
+
228
+ def _rdmol_for_graph(data, graph_idx: int):
229
+ """Recover the RDKit molecule from a PyG batch without assuming collation style."""
230
+ mols = getattr(data, "rdmol", None)
231
+ if isinstance(mols, (list, tuple)) and 0 <= graph_idx < len(mols):
232
+ return mols[graph_idx]
233
+ if mols is not None and hasattr(mols, "GetNumAtoms"):
234
+ return mols
235
+ return None
236
+
237
+
238
+ def safe_kabsch_rmsd(pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
239
+ pred = center_and_rescale(pred, target_rms=1.0)
240
+ target = center_and_rescale(target, target_rms=1.0)
241
+ if pred.shape != target.shape or pred.size(0) <= 1:
242
+ return pred.sum() * 0.0
243
+ try:
244
+ val = kabsch_rmsd(pred, target)
245
+ if torch.isfinite(val):
246
+ return val
247
+ except Exception:
248
+ pass
249
+ return F.mse_loss(pred, target).sqrt()
250
+
251
+
252
+ def heavy_atom_mask_from_x(x: Optional[torch.Tensor]) -> Optional[torch.Tensor]:
253
+ if x is None or x.dim() != 2 or x.size(-1) == 0:
254
+ return None
255
+ # Atom feature index 0 is the H symbol one-hot entry.
256
+ mask = x[:, 0] < 0.5
257
+ if int(mask.sum().item()) <= 1:
258
+ return None
259
+ return mask
260
+
261
+
262
+ def safe_heavy_kabsch_rmsd(pred: torch.Tensor, target: torch.Tensor,
263
+ atom_x: Optional[torch.Tensor] = None) -> torch.Tensor:
264
+ mask = heavy_atom_mask_from_x(atom_x)
265
+ if mask is not None and pred.size(0) == mask.size(0) and target.size(0) == mask.size(0):
266
+ pred = pred[mask]
267
+ target = target[mask]
268
+ return safe_kabsch_rmsd(pred, target)
269
+
270
+
271
+ # ============================================================
272
+ # Geometry constraints
273
+ # ============================================================
274
+
275
+ def bond_length_loss(coords: torch.Tensor, edge_index: Optional[torch.Tensor], target: float = 1.5) -> torch.Tensor:
276
+ if edge_index is None or edge_index.numel() == 0:
277
+ return coords.sum() * 0.0
278
+ src, dst = edge_index
279
+ if src.numel() == 0:
280
+ return coords.sum() * 0.0
281
+ dist = torch.norm(coords[src] - coords[dst], dim=-1)
282
+ return F.smooth_l1_loss(dist, torch.full_like(dist, float(target)))
283
+
284
+
285
+ def bond_distance_match_loss(pred: torch.Tensor, ref: torch.Tensor, edge_index: Optional[torch.Tensor]) -> torch.Tensor:
286
+ """Match predicted bonded distances to the current reference conformer."""
287
+ if edge_index is None or edge_index.numel() == 0:
288
+ return pred.sum() * 0.0
289
+ src, dst = edge_index
290
+ if src.numel() == 0:
291
+ return pred.sum() * 0.0
292
+ pred_dist = torch.norm(pred[src] - pred[dst], dim=-1)
293
+ ref_dist = torch.norm(ref[src] - ref[dst], dim=-1).detach()
294
+ return F.smooth_l1_loss(pred_dist, ref_dist)
295
+
296
+
297
+ def pair_distance_match_loss(
298
+ pred: torch.Tensor,
299
+ ref: torch.Tensor,
300
+ atom_x: Optional[torch.Tensor] = None,
301
+ edge_index: Optional[torch.Tensor] = None,
302
+ max_pairs: int = 2048,
303
+ min_ref: float = 0.0,
304
+ max_ref: float = 12.0,
305
+ ) -> torch.Tensor:
306
+ """Match heavy-atom pairwise distances to preserve global conformer shape."""
307
+ if pred.shape != ref.shape or pred.size(0) <= 2:
308
+ return pred.sum() * 0.0
309
+
310
+ mask = heavy_atom_mask_from_x(atom_x)
311
+ if mask is not None and pred.size(0) == mask.size(0):
312
+ pred = pred[mask]
313
+ ref = ref[mask]
314
+ n = pred.size(0)
315
+ if n <= 2:
316
+ return pred.sum() * 0.0
317
+
318
+ pair_mask = torch.triu(torch.ones(n, n, dtype=torch.bool, device=pred.device), diagonal=1)
319
+ if edge_index is not None and edge_index.numel() > 0:
320
+ bonded = torch.zeros(n, n, dtype=torch.bool, device=pred.device)
321
+ src, dst = edge_index
322
+ if mask is not None and edge_index.max().item() < mask.size(0):
323
+ local_map = torch.full((mask.size(0),), -1, dtype=torch.long, device=pred.device)
324
+ local_map[mask.to(pred.device)] = torch.arange(n, device=pred.device)
325
+ valid_global = (src >= 0) & (src < mask.size(0)) & (dst >= 0) & (dst < mask.size(0))
326
+ src = local_map[src[valid_global]]
327
+ dst = local_map[dst[valid_global]]
328
+ valid = (src >= 0) & (src < n) & (dst >= 0) & (dst < n)
329
+ src = src[valid]
330
+ dst = dst[valid]
331
+ if src.numel() > 0:
332
+ bonded[src, dst] = True
333
+ bonded[dst, src] = True
334
+ pair_mask = pair_mask & (~bonded)
335
+
336
+ ref_dist = pairwise_distance(ref).detach()
337
+ pair_mask = pair_mask & (ref_dist >= float(min_ref)) & (ref_dist <= float(max_ref))
338
+ idx = pair_mask.nonzero(as_tuple=False)
339
+ if idx.numel() == 0:
340
+ return pred.sum() * 0.0
341
+
342
+ if idx.size(0) > int(max_pairs):
343
+ # Deterministic evenly-spaced subsampling keeps validation stable.
344
+ take = torch.linspace(0, idx.size(0) - 1, steps=int(max_pairs), device=idx.device).long()
345
+ idx = idx[take]
346
+
347
+ pred_dist = torch.norm(pred[idx[:, 0]] - pred[idx[:, 1]], dim=-1)
348
+ ref_sel = ref_dist[idx[:, 0], idx[:, 1]]
349
+ loss = F.smooth_l1_loss(pred_dist, ref_sel)
350
+ return torch.nan_to_num(loss, nan=0.0, posinf=10.0, neginf=0.0).clamp(0.0, 10.0)
351
+
352
+
353
+ def steric_clash_loss(coords: torch.Tensor, min_dist: float = 0.8,
354
+ edge_index: Optional[torch.Tensor] = None) -> torch.Tensor:
355
+ n = coords.size(0)
356
+ if n <= 1:
357
+ return coords.sum() * 0.0
358
+ dist = pairwise_distance(coords)
359
+ eye = torch.eye(n, dtype=torch.bool, device=coords.device)
360
+ exclude = eye
361
+ if edge_index is not None and edge_index.numel() > 0:
362
+ bonded = torch.zeros(n, n, dtype=torch.bool, device=coords.device)
363
+ src, dst = edge_index
364
+ valid = (src >= 0) & (src < n) & (dst >= 0) & (dst < n)
365
+ src = src[valid]
366
+ dst = dst[valid]
367
+ bonded[src, dst] = True
368
+ bonded[dst, src] = True
369
+ exclude = exclude | bonded
370
+ penalty = F.relu(float(min_dist) - dist).masked_fill(exclude, 0.0)
371
+ return penalty.sum() / max(1, n * (n - 1))
372
+
373
+
374
+ def angle_loss(coords: torch.Tensor, edge_index: Optional[torch.Tensor]) -> torch.Tensor:
375
+ if edge_index is None or edge_index.numel() == 0:
376
+ return coords.sum() * 0.0
377
+ src, dst = edge_index
378
+ if src.numel() < 2:
379
+ return coords.sum() * 0.0
380
+ losses = []
381
+ max_pairs = min(32, src.numel() - 1)
382
+ target = coords.new_tensor(math.radians(109.5))
383
+ for k in range(max_pairs):
384
+ i, j = int(src[k]), int(dst[k])
385
+ j2, l = int(src[k + 1]), int(dst[k + 1])
386
+ if j != j2 or len({i, j, l}) < 3:
387
+ continue
388
+ v1 = coords[i] - coords[j]
389
+ v2 = coords[l] - coords[j]
390
+ cos = (v1 * v2).sum() / (torch.norm(v1) * torch.norm(v2) + 1e-8)
391
+ ang = torch.acos(cos.clamp(-1.0, 1.0))
392
+ losses.append(torch.abs(ang - target))
393
+ if not losses:
394
+ return coords.sum() * 0.0
395
+ return torch.stack(losses).mean()
396
+
397
+
398
+ def torsion_angle(a: torch.Tensor, b: torch.Tensor, c: torch.Tensor, d: torch.Tensor,
399
+ eps: float = 1e-8) -> torch.Tensor:
400
+ b1 = b - a
401
+ b2 = c - b
402
+ b3 = d - c
403
+ n1 = torch.cross(b1, b2, dim=0)
404
+ n2 = torch.cross(b2, b3, dim=0)
405
+ n1_norm = torch.norm(n1).clamp(min=eps)
406
+ n2_norm = torch.norm(n2).clamp(min=eps)
407
+ n1 = n1 / n1_norm
408
+ n2 = n2 / n2_norm
409
+ b2_norm = torch.norm(b2).clamp(min=eps)
410
+ b2 = b2 / b2_norm
411
+ m1 = torch.cross(n1, b2, dim=0)
412
+ x = (n1 * n2).sum().clamp(-1.0 + eps, 1.0 - eps)
413
+ y = (m1 * n2).sum().clamp(-1.0 + eps, 1.0 - eps)
414
+ return torch.atan2(y, x)
415
+
416
+
417
+ def torsion_angles(coords: torch.Tensor, torsions: torch.Tensor, eps: float = 1e-8) -> torch.Tensor:
418
+ if torsions is None or torsions.numel() == 0:
419
+ return coords.sum().view(1) * 0.0
420
+ p0 = coords[torsions[:, 0]]
421
+ p1 = coords[torsions[:, 1]]
422
+ p2 = coords[torsions[:, 2]]
423
+ p3 = coords[torsions[:, 3]]
424
+
425
+ b1 = p1 - p0
426
+ b2 = p2 - p1
427
+ b3 = p3 - p2
428
+ n1 = torch.cross(b1, b2, dim=-1)
429
+ n2 = torch.cross(b2, b3, dim=-1)
430
+ n1 = n1 / torch.norm(n1, dim=-1, keepdim=True).clamp(min=eps)
431
+ n2 = n2 / torch.norm(n2, dim=-1, keepdim=True).clamp(min=eps)
432
+ b2 = b2 / torch.norm(b2, dim=-1, keepdim=True).clamp(min=eps)
433
+ m1 = torch.cross(n1, b2, dim=-1)
434
+ x = (n1 * n2).sum(dim=-1).clamp(-1.0 + eps, 1.0 - eps)
435
+ y = (m1 * n2).sum(dim=-1).clamp(-1.0 + eps, 1.0 - eps)
436
+ return torch.atan2(y, x)
437
+
438
+
439
+ def torsion_loss(coords: torch.Tensor, torsion_index: Optional[torch.Tensor] = None) -> torch.Tensor:
440
+ if torsion_index is None or torsion_index.numel() == 0:
441
+ return coords.sum() * 0.0
442
+ if torsion_index.dim() != 2:
443
+ return coords.sum() * 0.0
444
+ if torsion_index.size(0) == 4:
445
+ torsions = torsion_index.t()
446
+ else:
447
+ torsions = torsion_index
448
+ vals = []
449
+ for row in torsions[: min(32, torsions.size(0))]:
450
+ i, j, k, l = [int(x) for x in row.tolist()]
451
+ if max(i, j, k, l) >= coords.size(0) or min(i, j, k, l) < 0:
452
+ continue
453
+ t = torsion_angle(coords[i], coords[j], coords[k], coords[l])
454
+ vals.append(torch.abs(torch.sin(t)))
455
+ if not vals:
456
+ return coords.sum() * 0.0
457
+ return torch.stack(vals).mean()
458
+
459
+
460
+ def _torsion_rows(torsion_index: Optional[torch.Tensor]) -> Optional[torch.Tensor]:
461
+ if torsion_index is None or torsion_index.numel() == 0 or torsion_index.dim() != 2:
462
+ return None
463
+ if torsion_index.size(0) == 4:
464
+ return torsion_index.t()
465
+ return torsion_index
466
+
467
+
468
+ def signed_torsion_supervision_loss(
469
+ pred: torch.Tensor,
470
+ target: torch.Tensor,
471
+ torsion_index: Optional[torch.Tensor] = None,
472
+ max_torsions: int = 64,
473
+ ) -> torch.Tensor:
474
+ torsions = _torsion_rows(torsion_index)
475
+ if torsions is None:
476
+ return pred.sum() * 0.0
477
+
478
+ torsions = torsions[: min(int(max_torsions), torsions.size(0))].to(pred.device).long()
479
+ valid = (torsions >= 0).all(dim=1) & (torsions < pred.size(0)).all(dim=1)
480
+ torsions = torsions[valid]
481
+ if torsions.numel() == 0:
482
+ return pred.sum() * 0.0
483
+
484
+ pred_t = torsion_angles(pred, torsions)
485
+ ref_t = torsion_angles(target, torsions).detach()
486
+ # Periodic signed angle difference. 1-cos(diff) penalizes mirror-flipped
487
+ # torsions while staying smooth near +/-pi.
488
+ loss = 1.0 - torch.cos(pred_t - ref_t)
489
+ return loss.mean().clamp(0.0, 10.0)
490
+
491
+
492
+ def periodic_angle_delta(target: torch.Tensor, source: torch.Tensor) -> torch.Tensor:
493
+ return torch.atan2(torch.sin(target - source), torch.cos(target - source))
494
+
495
+
496
+ def _torsion_rotate_mask(n: int, edge_index: Optional[torch.Tensor], fixed: int, moving: int, device) -> torch.Tensor:
497
+ """Atoms on the moving side of a rotatable bond fixed-moving."""
498
+ mask = torch.zeros(n, dtype=torch.bool, device=device)
499
+ if edge_index is None or edge_index.numel() == 0:
500
+ if 0 <= moving < n:
501
+ mask[moving] = True
502
+ return mask
503
+ src, dst = edge_index.detach().cpu()
504
+ adj: Dict[int, List[int]] = {}
505
+ for s, d in zip(src.tolist(), dst.tolist()):
506
+ s, d = int(s), int(d)
507
+ if not (0 <= s < n and 0 <= d < n) or s == d:
508
+ continue
509
+ if (s == fixed and d == moving) or (s == moving and d == fixed):
510
+ continue
511
+ adj.setdefault(s, []).append(d)
512
+ stack = [int(moving)]
513
+ seen = {int(fixed)}
514
+ while stack:
515
+ cur = stack.pop()
516
+ if cur in seen or not (0 <= cur < n):
517
+ continue
518
+ seen.add(cur)
519
+ mask[cur] = True
520
+ for nxt in adj.get(cur, []):
521
+ if nxt not in seen:
522
+ stack.append(nxt)
523
+ return mask
524
+
525
+
526
+ def rotate_points_around_axis(points: torch.Tensor, p0: torch.Tensor, p1: torch.Tensor, angle: torch.Tensor) -> torch.Tensor:
527
+ axis = p1 - p0
528
+ axis = axis / torch.norm(axis).clamp(min=1e-8)
529
+ rel = points - p0
530
+ cos = torch.cos(angle)
531
+ sin = torch.sin(angle)
532
+ cross = torch.cross(axis.view(1, 3).expand_as(rel), rel, dim=-1)
533
+ dot = (rel * axis.view(1, 3)).sum(dim=-1, keepdim=True)
534
+ return p0 + rel * cos + cross * sin + axis.view(1, 3) * dot * (1.0 - cos)
535
+
536
+
537
+ def apply_torsion_residuals(
538
+ coords: torch.Tensor,
539
+ torsions: Optional[torch.Tensor],
540
+ deltas: Optional[torch.Tensor],
541
+ edge_index: Optional[torch.Tensor],
542
+ max_torsions: int = 32,
543
+ rotation_masks: Optional[List[torch.Tensor]] = None,
544
+ ) -> torch.Tensor:
545
+ rows = _torsion_rows(torsions)
546
+ if rows is None or deltas is None:
547
+ return coords
548
+ rows = rows[: min(int(max_torsions), rows.size(0), deltas.size(0))].to(coords.device).long()
549
+ valid = (rows >= 0).all(dim=1) & (rows < coords.size(0)).all(dim=1)
550
+ rows = rows[valid]
551
+ deltas = deltas[: rows.size(0)]
552
+ if rows.numel() == 0:
553
+ return coords
554
+ out = coords
555
+ if rotation_masks is not None and len(rotation_masks) != rows.size(0):
556
+ raise ValueError("rotation_masks must match the number of valid torsion rows")
557
+ for idx, row in enumerate(rows):
558
+ _, j, k, _ = [int(x) for x in row.detach().cpu().tolist()]
559
+ if rotation_masks is None:
560
+ side = _torsion_rotate_mask(out.size(0), edge_index, fixed=j, moving=k, device=out.device)
561
+ else:
562
+ side = rotation_masks[idx].to(device=out.device)
563
+ if int(side.sum().item()) == 0:
564
+ continue
565
+ rotated = rotate_points_around_axis(out[side], out[j], out[k], deltas[idx])
566
+ out = out.clone()
567
+ out[side] = rotated
568
+ return torch.nan_to_num(out, nan=0.0, posinf=0.0, neginf=0.0)
569
+
570
+
571
+ def torsion_rotation_masks(
572
+ num_atoms: int,
573
+ torsions: Optional[torch.Tensor],
574
+ edge_index: Optional[torch.Tensor],
575
+ max_torsions: int = 32,
576
+ ) -> List[torch.Tensor]:
577
+ """Precompute topology-only moving-side masks for repeated torsion updates."""
578
+ rows = _torsion_rows(torsions)
579
+ if rows is None:
580
+ return []
581
+ rows = rows[: min(int(max_torsions), rows.size(0))].long()
582
+ valid = (rows >= 0).all(dim=1) & (rows < int(num_atoms)).all(dim=1)
583
+ rows = rows[valid]
584
+ device = torsions.device if torch.is_tensor(torsions) else (
585
+ edge_index.device if torch.is_tensor(edge_index) else torch.device("cpu")
586
+ )
587
+ masks: List[torch.Tensor] = []
588
+ for row in rows:
589
+ _, j, k, _ = [int(x) for x in row.detach().cpu().tolist()]
590
+ masks.append(
591
+ _torsion_rotate_mask(
592
+ int(num_atoms), edge_index, fixed=j, moving=k, device=device
593
+ )
594
+ )
595
+ return masks
596
+
597
+
598
+ def _unit_signed_volume(coords: torch.Tensor, center: int, neigh: List[int], eps: float = 1e-8) -> torch.Tensor:
599
+ a = coords[int(neigh[0])] - coords[int(center)]
600
+ b = coords[int(neigh[1])] - coords[int(center)]
601
+ c = coords[int(neigh[2])] - coords[int(center)]
602
+ denom = (torch.norm(a) * torch.norm(b) * torch.norm(c)).clamp(min=eps)
603
+ vol = torch.dot(torch.cross(a, b, dim=0), c) / denom
604
+ return torch.nan_to_num(vol, nan=0.0, posinf=0.0, neginf=0.0).clamp(-1.0, 1.0)
605
+
606
+
607
+ def signed_volume_supervision_loss(
608
+ pred: torch.Tensor,
609
+ target: torch.Tensor,
610
+ edge_index: Optional[torch.Tensor],
611
+ max_centers: int = 64,
612
+ ref_abs_min: float = 0.05,
613
+ ) -> torch.Tensor:
614
+ if edge_index is None or edge_index.numel() == 0 or edge_index.dim() != 2:
615
+ return pred.sum() * 0.0
616
+ src, dst = edge_index
617
+ neighbors: Dict[int, List[int]] = {}
618
+ for s, d in zip(src.detach().cpu().tolist(), dst.detach().cpu().tolist()):
619
+ s, d = int(s), int(d)
620
+ if 0 <= s < pred.size(0) and 0 <= d < pred.size(0) and s != d:
621
+ neighbors.setdefault(s, [])
622
+ if d not in neighbors[s]:
623
+ neighbors[s].append(d)
624
+
625
+ vals = []
626
+ for center in sorted(neighbors.keys()):
627
+ neigh = sorted(neighbors[center])
628
+ if len(neigh) < 3:
629
+ continue
630
+ ref_vol = _unit_signed_volume(target, center, neigh[:3]).detach()
631
+ if torch.abs(ref_vol) < float(ref_abs_min):
632
+ continue
633
+ pred_vol = _unit_signed_volume(pred, center, neigh[:3])
634
+ vals.append(F.smooth_l1_loss(pred_vol, ref_vol))
635
+ if len(vals) >= int(max_centers):
636
+ break
637
+ if not vals:
638
+ return pred.sum() * 0.0
639
+ return torch.stack(vals).mean().clamp(0.0, 10.0)
640
+
641
+
642
+ def chirality_loss(coords: torch.Tensor) -> torch.Tensor:
643
+ if coords.size(0) < 4:
644
+ return coords.sum() * 0.0
645
+ a, b, c, d = coords[0], coords[1], coords[2], coords[3]
646
+ vol = torch.dot(torch.cross(b - a, c - a, dim=0), d - a)
647
+ return torch.nan_to_num(F.relu(-vol), nan=0.0, posinf=0.0, neginf=0.0)
648
+
649
+
650
+ # ============================================================
651
+ # Probability-conditioned coordinate modules
652
+ # ============================================================
653
+
654
+ class ProbabilityEGNNLayer(nn.Module):
655
+ """概率图 + 化学边特征 双重引导的等变消息传递层"""
656
+ def __init__(self, hidden_dim: int, edge_dim: int = 13):
657
+ super().__init__()
658
+ self.hidden_dim = hidden_dim
659
+ self.edge_dim = edge_dim
660
+ self.edge_mlp = nn.Sequential(
661
+ nn.Linear(2 * hidden_dim + 2 + edge_dim, hidden_dim),
662
+ nn.SiLU(),
663
+ nn.Linear(hidden_dim, hidden_dim),
664
+ nn.SiLU(),
665
+ )
666
+ self.coord_mlp = nn.Sequential(
667
+ nn.Linear(hidden_dim, hidden_dim),
668
+ nn.SiLU(),
669
+ nn.Linear(hidden_dim, 1),
670
+ nn.Tanh(),
671
+ )
672
+ self.node_mlp = nn.Sequential(
673
+ nn.Linear(2 * hidden_dim, hidden_dim),
674
+ nn.SiLU(),
675
+ nn.Linear(hidden_dim, hidden_dim),
676
+ )
677
+
678
+ def forward(self, h: torch.Tensor, x: torch.Tensor, P: torch.Tensor,
679
+ edge_index: Optional[torch.Tensor] = None,
680
+ edge_attr: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]:
681
+ n = h.size(0)
682
+ device = h.device
683
+ P = torch.nan_to_num(P, nan=0.0, posinf=1.0, neginf=0.0).clamp(1e-6, 1.0 - 1e-6)
684
+ P = (P + P.t()) * 0.5
685
+ eye = torch.eye(n, device=device, dtype=torch.bool)
686
+ P = P.masked_fill(eye, 0.0)
687
+
688
+ hi = h.unsqueeze(1).expand(n, n, -1)
689
+ hj = h.unsqueeze(0).expand(n, n, -1)
690
+ diff = x.unsqueeze(1) - x.unsqueeze(0)
691
+ dist2 = (diff ** 2).sum(dim=-1, keepdim=True).clamp(max=100.0)
692
+ dist = torch.sqrt(dist2 + 1e-8)
693
+ direction = diff / (dist + 1e-8)
694
+ p_ij = P.unsqueeze(-1)
695
+
696
+ if edge_attr is not None and edge_index is not None and edge_index.numel() > 0:
697
+ edge_attr_mat = torch.zeros(n, n, edge_attr.size(-1), device=device, dtype=h.dtype)
698
+ src, dst = edge_index
699
+ edge_attr_mat[src, dst] = edge_attr
700
+ edge_attr_mat[dst, src] = edge_attr
701
+ else:
702
+ edge_attr_mat = torch.zeros(n, n, self.edge_dim, device=device, dtype=h.dtype)
703
+
704
+ edge_input = torch.cat([hi, hj, dist2, p_ij, edge_attr_mat], dim=-1)
705
+ m = self.edge_mlp(edge_input)
706
+ m = m.masked_fill(eye.unsqueeze(-1), 0.0)
707
+
708
+ coord_weight = self.coord_mlp(m).clamp(-0.02, 0.02)
709
+ row_weights = F.softmax(P / 0.1, dim=1).detach()
710
+ dx = (direction * coord_weight * row_weights.unsqueeze(-1)).sum(dim=1)
711
+ dx = torch.nan_to_num(dx, nan=0.0, posinf=0.0, neginf=0.0).clamp(-0.05, 0.05)
712
+ x = x + dx
713
+
714
+ msg = (m * row_weights.unsqueeze(-1)).sum(dim=1)
715
+ h = h + self.node_mlp(torch.cat([h, msg], dim=-1))
716
+ h = torch.nan_to_num(h, nan=0.0, posinf=0.0, neginf=0.0)
717
+ return h, x
718
+
719
+
720
+ class ModePairAttention(nn.Module):
721
+ """Mode-conditioned full atom attention with probability and bond bias."""
722
+ def __init__(
723
+ self,
724
+ emb_dim: int = 128,
725
+ mode_dim: int = 32,
726
+ edge_dim: int = 13,
727
+ num_heads: int = 4,
728
+ dropout: float = 0.0,
729
+ ):
730
+ super().__init__()
731
+ heads = max(1, int(num_heads))
732
+ if int(emb_dim) % heads != 0:
733
+ heads = 1
734
+ self.emb_dim = int(emb_dim)
735
+ self.mode_dim = int(mode_dim)
736
+ self.edge_dim = int(edge_dim)
737
+ self.num_heads = heads
738
+ self.head_dim = self.emb_dim // self.num_heads
739
+
740
+ self.q_proj = nn.Linear(self.emb_dim, self.emb_dim)
741
+ self.k_proj = nn.Linear(self.emb_dim, self.emb_dim)
742
+ self.v_proj = nn.Linear(self.emb_dim, self.emb_dim)
743
+ self.out_proj = nn.Linear(self.emb_dim, self.emb_dim)
744
+ self.z_gate = nn.Sequential(
745
+ nn.Linear(self.mode_dim, self.emb_dim),
746
+ nn.Sigmoid(),
747
+ )
748
+ self.z_p_scale = nn.Sequential(
749
+ nn.Linear(self.mode_dim, self.num_heads),
750
+ nn.Softplus(),
751
+ )
752
+ self.edge_bias = nn.Linear(self.edge_dim, self.num_heads)
753
+ self.dropout = nn.Dropout(float(dropout))
754
+ self.norm = nn.LayerNorm(self.emb_dim)
755
+
756
+ def _edge_bias_matrix(
757
+ self,
758
+ n: int,
759
+ edge_index: Optional[torch.Tensor],
760
+ edge_attr: Optional[torch.Tensor],
761
+ device,
762
+ dtype,
763
+ ) -> torch.Tensor:
764
+ bias = torch.zeros(self.num_heads, n, n, device=device, dtype=dtype)
765
+ if edge_index is None or edge_attr is None or edge_index.numel() == 0:
766
+ return bias
767
+ src, dst = edge_index
768
+ valid = (src >= 0) & (src < n) & (dst >= 0) & (dst < n)
769
+ src = src[valid]
770
+ dst = dst[valid]
771
+ if src.numel() == 0:
772
+ return bias
773
+ e = edge_attr[valid].to(device=device, dtype=dtype)
774
+ eb = self.edge_bias(e).transpose(0, 1)
775
+ bias[:, src, dst] = eb
776
+ bias[:, dst, src] = eb
777
+ return bias
778
+
779
+ def forward(
780
+ self,
781
+ node_feat: torch.Tensor,
782
+ P: torch.Tensor,
783
+ z: torch.Tensor,
784
+ edge_index: Optional[torch.Tensor] = None,
785
+ edge_attr: Optional[torch.Tensor] = None,
786
+ ) -> torch.Tensor:
787
+ n = node_feat.size(0)
788
+ if n <= 1:
789
+ return node_feat
790
+
791
+ z = z.view(-1)
792
+ if z.numel() != self.mode_dim:
793
+ z_fixed = torch.zeros(self.mode_dim, device=node_feat.device, dtype=node_feat.dtype)
794
+ z_fixed[: min(self.mode_dim, z.numel())] = z[: min(self.mode_dim, z.numel())]
795
+ z = z_fixed
796
+
797
+ q = self.q_proj(node_feat).view(n, self.num_heads, self.head_dim).transpose(0, 1)
798
+ k = self.k_proj(node_feat).view(n, self.num_heads, self.head_dim).transpose(0, 1)
799
+ v = self.v_proj(node_feat).view(n, self.num_heads, self.head_dim).transpose(0, 1)
800
+
801
+ scores = torch.matmul(q, k.transpose(-1, -2)) / math.sqrt(float(self.head_dim))
802
+ P = torch.nan_to_num(P, nan=0.0, posinf=1.0, neginf=0.0).clamp(1e-6, 1.0 - 1e-6)
803
+ P = 0.5 * (P + P.t())
804
+ p_logit = torch.logit(P).unsqueeze(0)
805
+ p_scale = self.z_p_scale(z).view(self.num_heads, 1, 1).to(dtype=node_feat.dtype)
806
+ scores = scores + p_scale * p_logit
807
+ scores = scores + self._edge_bias_matrix(n, edge_index, edge_attr, node_feat.device, node_feat.dtype)
808
+
809
+ eye = torch.eye(n, dtype=torch.bool, device=node_feat.device).unsqueeze(0)
810
+ scores = scores.masked_fill(eye, scores.diagonal(dim1=-2, dim2=-1).mean().detach())
811
+ attn = F.softmax(scores.clamp(-30.0, 30.0), dim=-1)
812
+ attn = self.dropout(attn)
813
+ context = torch.matmul(attn, v).transpose(0, 1).reshape(n, self.emb_dim)
814
+ context = self.out_proj(context)
815
+ gate = self.z_gate(z).view(1, -1).to(dtype=node_feat.dtype)
816
+ return self.norm(node_feat + gate * context)
817
+
818
+
819
+ class ConditionedCoarseDecoder(nn.Module):
820
+ """
821
+ 潜变量条件化的粗解码器(增强版)
822
+ P_global + z + edge_attr → Y0
823
+ """
824
+ def __init__(self, emb_dim: int = 128, hidden_dim: int = 128, num_layers: int = 2,
825
+ mode_dim: int = 32, edge_dim: int = 13,
826
+ use_mode_pair_attention: bool = False,
827
+ mode_attention_heads: int = 4,
828
+ mode_attention_dropout: float = 0.0):
829
+ super().__init__()
830
+ self.mode_dim = mode_dim
831
+ self.edge_dim = edge_dim
832
+ self.use_mode_pair_attention = bool(use_mode_pair_attention)
833
+ self.mode_pair_attention = ModePairAttention(
834
+ emb_dim=emb_dim,
835
+ mode_dim=mode_dim,
836
+ edge_dim=edge_dim,
837
+ num_heads=mode_attention_heads,
838
+ dropout=mode_attention_dropout,
839
+ ) if self.use_mode_pair_attention else None
840
+
841
+ self.z_proj = nn.Sequential(
842
+ nn.Linear(mode_dim, hidden_dim),
843
+ nn.SiLU(),
844
+ nn.Linear(hidden_dim, hidden_dim),
845
+ )
846
+
847
+ self.node_proj = nn.Sequential(
848
+ nn.Linear(emb_dim + 5 + hidden_dim, hidden_dim),
849
+ nn.SiLU(),
850
+ nn.Linear(hidden_dim, hidden_dim),
851
+ )
852
+ self.coord_init = nn.Sequential(
853
+ nn.Linear(hidden_dim, hidden_dim),
854
+ nn.SiLU(),
855
+ nn.Linear(hidden_dim, 3),
856
+ )
857
+ self.layers = nn.ModuleList([
858
+ ProbabilityEGNNLayer(hidden_dim, edge_dim) for _ in range(num_layers)
859
+ ])
860
+
861
+ def _node_stats_from_P(self, P: torch.Tensor) -> torch.Tensor:
862
+ degree = P.sum(dim=-1, keepdim=True)
863
+ mean = P.mean(dim=-1, keepdim=True)
864
+ maxv = P.max(dim=-1, keepdim=True).values
865
+ std = P.std(dim=-1, keepdim=True)
866
+ P_safe = P.clamp(1e-6, 1.0 - 1e-6)
867
+ entropy = -(P_safe * torch.log(P_safe)).sum(dim=-1, keepdim=True)
868
+ return torch.cat([degree, mean, maxv, std, entropy], dim=-1)
869
+
870
+ def forward(self, P_global: torch.Tensor, node_feat: torch.Tensor, z: torch.Tensor,
871
+ edge_index: Optional[torch.Tensor] = None,
872
+ edge_attr: Optional[torch.Tensor] = None) -> torch.Tensor:
873
+ n = P_global.size(0)
874
+ P_global = (P_global + P_global.t()) * 0.5
875
+ P_global = P_global * (1.0 - torch.eye(n, device=P_global.device))
876
+
877
+ z_feat = self.z_proj(z.view(-1)).unsqueeze(0).expand(n, -1)
878
+ stats = self._node_stats_from_P(P_global)
879
+ node_ctx = node_feat
880
+ if self.mode_pair_attention is not None:
881
+ node_ctx = self.mode_pair_attention(node_feat, P_global, z, edge_index, edge_attr)
882
+ h = self.node_proj(torch.cat([node_ctx, stats, z_feat], dim=-1))
883
+ x = self.coord_init(h)
884
+ x = center_and_rescale(x, target_rms=1.0)
885
+
886
+ for layer in self.layers:
887
+ h, x = layer(h, x, P_global, edge_index, edge_attr)
888
+ x = center_and_rescale(x, target_rms=1.0)
889
+ return x
890
+
891
+
892
+ class CoarseCoordinateDecoder(nn.Module):
893
+ """向后兼容包装器"""
894
+ def __init__(self, emb_dim: int = 128, hidden_dim: int = 128, num_layers: int = 2, mode_dim: int = 32):
895
+ super().__init__()
896
+ self.decoder = ConditionedCoarseDecoder(emb_dim, hidden_dim, num_layers, mode_dim)
897
+
898
+ def forward(self, P: torch.Tensor, node_feat: torch.Tensor, z: Optional[torch.Tensor] = None,
899
+ edge_index: Optional[torch.Tensor] = None,
900
+ edge_attr: Optional[torch.Tensor] = None) -> torch.Tensor:
901
+ if z is None:
902
+ z = torch.zeros(self.decoder.mode_dim, device=P.device, dtype=P.dtype)
903
+ return self.decoder(P, node_feat, z, edge_index, edge_attr)
904
+
905
+
906
+ class GeometryRefiner(nn.Module):
907
+ def __init__(self, emb_dim: int = 128, hidden_dim: int = 128, num_steps: int = 2, edge_dim: int = 13):
908
+ super().__init__()
909
+ self.num_steps = int(num_steps)
910
+ self.node_proj = nn.Sequential(
911
+ nn.Linear(emb_dim + 3 + 5, hidden_dim),
912
+ nn.SiLU(),
913
+ nn.Linear(hidden_dim, hidden_dim),
914
+ )
915
+ self.layers = nn.ModuleList([
916
+ ProbabilityEGNNLayer(hidden_dim, edge_dim) for _ in range(self.num_steps)
917
+ ])
918
+
919
+ def _node_stats_from_P(self, P: torch.Tensor) -> torch.Tensor:
920
+ degree = P.sum(dim=-1, keepdim=True)
921
+ mean = P.mean(dim=-1, keepdim=True)
922
+ maxv = P.max(dim=-1, keepdim=True).values
923
+ std = P.std(dim=-1, keepdim=True)
924
+ P_safe = P.clamp(1e-6, 1.0 - 1e-6)
925
+ entropy = -(P_safe * torch.log(P_safe)).sum(dim=-1, keepdim=True)
926
+ return torch.cat([degree, mean, maxv, std, entropy], dim=-1)
927
+
928
+ def forward(
929
+ self,
930
+ P: Optional[torch.Tensor] = None,
931
+ node_feat: Optional[torch.Tensor] = None,
932
+ y0: Optional[torch.Tensor] = None,
933
+ edge_index: Optional[torch.Tensor] = None,
934
+ edge_attr: Optional[torch.Tensor] = None,
935
+ coords: Optional[torch.Tensor] = None,
936
+ edge_index_i: Optional[torch.Tensor] = None,
937
+ ):
938
+ if y0 is None and coords is not None:
939
+ y0 = coords
940
+ if P is None:
941
+ n = y0.size(0)
942
+ P = torch.ones(n, n, device=y0.device, dtype=y0.dtype) * (1.0 - torch.eye(n, device=y0.device, dtype=y0.dtype))
943
+ if node_feat is None:
944
+ node_feat = torch.zeros(y0.size(0), self.node_proj[0].in_features - 8, device=y0.device, dtype=y0.dtype)
945
+
946
+ P = torch.nan_to_num(P, nan=0.0, posinf=1.0, neginf=0.0).clamp(1e-6, 1.0 - 1e-6)
947
+ P = (P + P.t()) * 0.5
948
+ P = P * (1.0 - torch.eye(P.size(0), device=P.device))
949
+ x = center_and_rescale(y0, target_rms=1.0)
950
+ stats = self._node_stats_from_P(P)
951
+ h = self.node_proj(torch.cat([node_feat, x, stats], dim=-1))
952
+ traj = []
953
+ for layer in self.layers:
954
+ h, x = layer(h, x, P, edge_index, edge_attr)
955
+ x = center_and_rescale(x, target_rms=1.0)
956
+ traj.append(x)
957
+ return x, traj
958
+
959
+
960
+ class TorsionResidualHead(nn.Module):
961
+ """Predict small residual rotations for torsion-index rows."""
962
+ def __init__(self, emb_dim: int = 128, hidden_dim: int = 128, max_delta: float = 1.0):
963
+ super().__init__()
964
+ self.max_delta = float(max_delta)
965
+ self.mlp = nn.Sequential(
966
+ nn.Linear(4 * emb_dim + 3, hidden_dim),
967
+ nn.SiLU(),
968
+ nn.Linear(hidden_dim, hidden_dim),
969
+ nn.SiLU(),
970
+ nn.Linear(hidden_dim, 1),
971
+ )
972
+
973
+ def forward(
974
+ self,
975
+ node_feat: torch.Tensor,
976
+ coords: torch.Tensor,
977
+ P: torch.Tensor,
978
+ torsion_index: Optional[torch.Tensor],
979
+ max_torsions: int = 32,
980
+ ) -> torch.Tensor:
981
+ rows = _torsion_rows(torsion_index)
982
+ if rows is None:
983
+ return coords.new_zeros((0,))
984
+ rows = rows[: min(int(max_torsions), rows.size(0))].to(coords.device).long()
985
+ valid = (rows >= 0).all(dim=1) & (rows < coords.size(0)).all(dim=1)
986
+ rows = rows[valid]
987
+ if rows.numel() == 0:
988
+ return coords.new_zeros((0,))
989
+ feats = []
990
+ current = torsion_angles(coords, rows)
991
+ for idx, row in enumerate(rows):
992
+ i, j, k, l = row
993
+ pjk = P[j, k].view(1)
994
+ feats.append(torch.cat([
995
+ node_feat[i],
996
+ node_feat[j],
997
+ node_feat[k],
998
+ node_feat[l],
999
+ torch.sin(current[idx]).view(1),
1000
+ torch.cos(current[idx]).view(1),
1001
+ pjk,
1002
+ ], dim=0))
1003
+ x = torch.stack(feats, dim=0)
1004
+ delta = torch.tanh(self.mlp(x).view(-1)) * self.max_delta
1005
+ return torch.nan_to_num(delta, nan=0.0, posinf=0.0, neginf=0.0)
1006
+
1007
+
1008
+ class ProbabilityModeGenerator(nn.Module):
1009
+ """Generate mode-specific probability graphs P_k from a frozen P_global.
1010
+
1011
+ The old implementation returned P_global unchanged. This residual generator
1012
+ keeps backwards compatibility while allowing Stage 2 to learn different
1013
+ probability maps for different conformational modes.
1014
+ """
1015
+ def __init__(
1016
+ self,
1017
+ emb_dim: int = 128,
1018
+ mode_dim: int = 32,
1019
+ hidden_dim: int = 128,
1020
+ pair_dim: Optional[int] = None,
1021
+ delta_scale: float = 1.0,
1022
+ ):
1023
+ super().__init__()
1024
+ self.mode_dim = int(mode_dim)
1025
+ self.delta_scale = float(delta_scale)
1026
+ if pair_dim is None:
1027
+ pair_dim = 4 * int(emb_dim) + 3
1028
+ self.z_proj = nn.Sequential(
1029
+ nn.Linear(self.mode_dim, hidden_dim),
1030
+ nn.SiLU(),
1031
+ nn.Linear(hidden_dim, hidden_dim),
1032
+ nn.SiLU(),
1033
+ )
1034
+ self.delta_head = nn.Sequential(
1035
+ nn.Linear(int(pair_dim) + hidden_dim + 1, hidden_dim),
1036
+ nn.SiLU(),
1037
+ nn.Linear(hidden_dim, hidden_dim),
1038
+ nn.SiLU(),
1039
+ nn.Linear(hidden_dim, 1),
1040
+ )
1041
+
1042
+ def forward(self, P_global: torch.Tensor, pair_feat: Optional[torch.Tensor], z: torch.Tensor) -> torch.Tensor:
1043
+ if pair_feat is None:
1044
+ return P_global
1045
+ n = P_global.size(0)
1046
+ z = z.view(-1)
1047
+ if z.numel() != self.mode_dim:
1048
+ z_fixed = torch.zeros(self.mode_dim, device=P_global.device, dtype=P_global.dtype)
1049
+ z_fixed[: min(self.mode_dim, z.numel())] = z[: min(self.mode_dim, z.numel())]
1050
+ z = z_fixed
1051
+ z_feat = self.z_proj(z).view(1, 1, -1).expand(n, n, -1)
1052
+ p_in = P_global.clamp(1e-6, 1.0 - 1e-6).unsqueeze(-1)
1053
+ delta_in = torch.cat([pair_feat, z_feat, p_in], dim=-1)
1054
+ delta = self.delta_head(delta_in.reshape(n * n, -1)).reshape(n, n)
1055
+ delta = torch.tanh(delta) * self.delta_scale
1056
+ logits = torch.logit(P_global.clamp(1e-6, 1.0 - 1e-6)) + delta
1057
+ P_mode = torch.sigmoid(logits)
1058
+ P_mode = torch.nan_to_num(P_mode, nan=0.5, posinf=1.0, neginf=0.0)
1059
+ P_mode = 0.5 * (P_mode + P_mode.t())
1060
+ P_mode = P_mode * (1.0 - torch.eye(n, device=P_global.device))
1061
+ return P_mode.clamp(1e-6, 1.0 - 1e-6)
1062
+
1063
+
1064
+ # ============================================================
1065
+ # Main model: GNNEncoder
1066
+ # ============================================================
1067
+
1068
+ class GNNEncoder(nn.Module):
1069
+ def __init__(self, args, config=None):
1070
+ super().__init__()
1071
+ self.args = args
1072
+ self.config = config or {}
1073
+ self.max_confs = int(getattr(args, "max_confs", 5))
1074
+ self.num_modes = int(getattr(args, "num_modes", self.max_confs))
1075
+ self.mode_dim = int(getattr(args, "mode_dim", getattr(args, "latent_dim", 32)))
1076
+
1077
+ emb_dim = int(getattr(args, "emb_dim", 128))
1078
+ hidden_dim = int(getattr(args, "mlp_hidden", getattr(args, "coarse_hidden", 128)))
1079
+ node_dim = int(getattr(args, "node_dim", 39))
1080
+ edge_dim = int(getattr(args, "edge_dim", 13))
1081
+ num_refine_steps = int(getattr(args, "num_refine_steps", getattr(args, "refine_steps", 2)))
1082
+
1083
+ try:
1084
+ self.gnn = GNN_node(
1085
+ num_layer=int(getattr(args, "layer", 4)),
1086
+ emb_dim=emb_dim,
1087
+ edge_dim=edge_dim,
1088
+ node_dim=node_dim,
1089
+ drop_ratio=float(getattr(args, "dropout", 0.5)),
1090
+ JK="last",
1091
+ residual=False,
1092
+ gnn_type=getattr(args, "gnn_type", "gin"),
1093
+ )
1094
+ except TypeError:
1095
+ self.gnn = GNN_node(
1096
+ num_layer=int(getattr(args, "layer", 4)),
1097
+ emb_dim=emb_dim,
1098
+ drop_ratio=float(getattr(args, "dropout", 0.5)),
1099
+ JK="last",
1100
+ residual=False,
1101
+ gnn_type=getattr(args, "gnn_type", "gin"),
1102
+ )
1103
+
1104
+ self.pair_dim = 4 * emb_dim + 3
1105
+ self.global_edge_predictor = MLP(
1106
+ num_layers=int(getattr(args, "mlp_layer", 4)),
1107
+ input_dim=self.pair_dim,
1108
+ hidden_dim=hidden_dim,
1109
+ output_dim=1,
1110
+ )
1111
+ # Optional Route-2 extension. The legacy single-map checkpoint remains
1112
+ # loadable because this head is only used when mean_dispersion_prior is
1113
+ # enabled; missing legacy keys are handled by the checkpoint loader.
1114
+ self.dispersion_edge_predictor = MLP(
1115
+ num_layers=int(getattr(args, "mlp_layer", 4)),
1116
+ input_dim=self.pair_dim,
1117
+ hidden_dim=hidden_dim,
1118
+ output_dim=1,
1119
+ )
1120
+
1121
+ self.conf_embedding = nn.Embedding(self.max_confs, emb_dim)
1122
+ self.ab_head = nn.Sequential(
1123
+ nn.Linear(emb_dim * 2, hidden_dim),
1124
+ nn.SiLU(),
1125
+ nn.Linear(hidden_dim, hidden_dim),
1126
+ nn.SiLU(),
1127
+ nn.Linear(hidden_dim, 2),
1128
+ )
1129
+ self.fusion_weight_head = nn.Sequential(
1130
+ nn.Linear(emb_dim * 2, hidden_dim),
1131
+ nn.SiLU(),
1132
+ nn.Linear(hidden_dim, 1),
1133
+ )
1134
+
1135
+ self.mode_generator = ProbabilityModeGenerator(
1136
+ emb_dim=emb_dim,
1137
+ mode_dim=self.mode_dim,
1138
+ hidden_dim=hidden_dim,
1139
+ pair_dim=self.pair_dim,
1140
+ delta_scale=float(getattr(args, "mode_p_delta_scale", 1.0)),
1141
+ )
1142
+ self.conditioned_decoder = ConditionedCoarseDecoder(
1143
+ emb_dim=emb_dim,
1144
+ hidden_dim=hidden_dim,
1145
+ num_layers=2,
1146
+ mode_dim=self.mode_dim,
1147
+ edge_dim=edge_dim,
1148
+ use_mode_pair_attention=bool(getattr(args, "use_mode_pair_attention", False)),
1149
+ mode_attention_heads=int(getattr(args, "mode_attention_heads", 4)),
1150
+ mode_attention_dropout=float(getattr(args, "mode_attention_dropout", 0.0)),
1151
+ )
1152
+ self.coarse_decoder = CoarseCoordinateDecoder(
1153
+ emb_dim=emb_dim, hidden_dim=hidden_dim, num_layers=2, mode_dim=self.mode_dim
1154
+ )
1155
+ self.refiner = GeometryRefiner(
1156
+ emb_dim=emb_dim, hidden_dim=hidden_dim, num_steps=num_refine_steps, edge_dim=edge_dim
1157
+ )
1158
+ self.torsion_residual_head = TorsionResidualHead(
1159
+ emb_dim=emb_dim,
1160
+ hidden_dim=hidden_dim,
1161
+ max_delta=float(getattr(args, "torsion_residual_scale", 1.0)),
1162
+ )
1163
+
1164
+ pooling = getattr(args, "pooling_type", "mean")
1165
+ if pooling == "mean":
1166
+ self.pool = global_mean_pool
1167
+ elif pooling == "add":
1168
+ self.pool = global_add_pool
1169
+ elif pooling == "max":
1170
+ self.pool = global_max_pool
1171
+ else:
1172
+ raise ValueError(f"Unknown pooling method: {pooling}")
1173
+
1174
+ # --------------------------------------------------------
1175
+ # Graph / pair helpers
1176
+ # --------------------------------------------------------
1177
+ def _get_local_edge_index(self, edge_index, mask):
1178
+ if edge_index is None or edge_index.numel() == 0:
1179
+ return None
1180
+ device = edge_index.device
1181
+ global_nodes = mask.nonzero(as_tuple=True)[0]
1182
+ mapping = torch.full((mask.size(0),), -1, dtype=torch.long, device=device)
1183
+ mapping[global_nodes] = torch.arange(global_nodes.size(0), device=device)
1184
+ row, col = edge_index
1185
+ keep = mask[row] & mask[col]
1186
+ if keep.sum() == 0:
1187
+ return None
1188
+ return torch.stack([mapping[row[keep]], mapping[col[keep]]], dim=0)
1189
+
1190
+ def _get_local_edge_attr(self, edge_attr, edge_index, mask):
1191
+ """获取局部子图的边特征和边索引"""
1192
+ if edge_attr is None or edge_index is None or edge_index.numel() == 0:
1193
+ return None, None
1194
+ device = edge_index.device
1195
+ global_nodes = mask.nonzero(as_tuple=True)[0]
1196
+ mapping = torch.full((mask.size(0),), -1, dtype=torch.long, device=device)
1197
+ mapping[global_nodes] = torch.arange(global_nodes.size(0), device=device)
1198
+ row, col = edge_index
1199
+ keep = mask[row] & mask[col]
1200
+ if keep.sum() == 0:
1201
+ return None, None
1202
+ local_ei = torch.stack([mapping[row[keep]], mapping[col[keep]]], dim=0)
1203
+ local_ea = edge_attr[keep]
1204
+ return local_ei, local_ea
1205
+
1206
+ def _get_local_torsion_index(self, torsion_index, mask):
1207
+ if torsion_index is None or torsion_index.numel() == 0 or torsion_index.dim() != 2:
1208
+ return torsion_index
1209
+ torsions = torsion_index.t() if torsion_index.size(0) == 4 else torsion_index
1210
+ device = mask.device
1211
+ global_nodes = mask.nonzero(as_tuple=True)[0]
1212
+ if global_nodes.numel() == 0:
1213
+ return torch.empty((4, 0), dtype=torch.long, device=device)
1214
+ mapping = torch.full((mask.size(0),), -1, dtype=torch.long, device=device)
1215
+ mapping[global_nodes] = torch.arange(global_nodes.size(0), device=device)
1216
+
1217
+ torsions = torsions.to(device)
1218
+ valid_global = (torsions >= 0).all(dim=1) & (torsions < mask.size(0)).all(dim=1)
1219
+ if valid_global.any():
1220
+ local = mapping[torsions[valid_global]]
1221
+ keep = (local >= 0).all(dim=1)
1222
+ local = local[keep]
1223
+ if local.numel() > 0:
1224
+ return local.t().contiguous()
1225
+
1226
+ # Fallback for processed files that may already keep local torsion indices.
1227
+ n_local = int(global_nodes.numel())
1228
+ valid_local = (torsions >= 0).all(dim=1) & (torsions < n_local).all(dim=1)
1229
+ local = torsions[valid_local]
1230
+ if local.numel() == 0:
1231
+ return torch.empty((4, 0), dtype=torch.long, device=device)
1232
+ return local.t().contiguous()
1233
+
1234
+ def _pair_graph_features(self, edge_index, num_nodes, device):
1235
+ n = num_nodes
1236
+ adj = torch.zeros(n, n, device=device)
1237
+ if edge_index is not None and edge_index.numel() > 0:
1238
+ row, col = edge_index
1239
+ adj[row, col] = 1.0
1240
+ adj[col, row] = 1.0
1241
+ eye = torch.eye(n, device=device)
1242
+ adj = adj * (1.0 - eye)
1243
+ dist = torch.full((n, n), 1e6, device=device)
1244
+ dist[eye.bool()] = 0.0
1245
+ direct = adj > 0
1246
+ dist[direct] = 1.0
1247
+ for k in range(n):
1248
+ dist = torch.minimum(dist, dist[:, k:k + 1] + dist[k:k + 1, :])
1249
+ finite = dist < 1e5
1250
+ max_dist = dist[finite].max().clamp(min=1.0) if finite.any() else torch.tensor(1.0, device=device)
1251
+ dist_norm = torch.where(finite, dist / max_dist, torch.ones_like(dist))
1252
+ inv_dist = torch.where(finite & (dist > 0), 1.0 / (dist + 1.0), torch.zeros_like(dist))
1253
+ return torch.stack([adj, dist_norm, inv_dist], dim=-1)
1254
+
1255
+ def _build_pair_features(self, feat_i, edge_index_i=None):
1256
+ n = feat_i.shape[0]
1257
+ fi = feat_i.unsqueeze(1).expand(-1, n, -1)
1258
+ fj = feat_i.unsqueeze(0).expand(n, -1, -1)
1259
+ graph_pair = self._pair_graph_features(edge_index_i, n, feat_i.device)
1260
+ return torch.cat([fi, fj, torch.abs(fi - fj), fi * fj, graph_pair], dim=-1)
1261
+
1262
+ def _predict_P_global(self, feat_i, edge_index_i=None):
1263
+ P_mean, _, pair_feat = self._predict_mean_dispersion(feat_i, edge_index_i)
1264
+ return P_mean, pair_feat
1265
+
1266
+ def _predict_mean_dispersion(self, feat_i, edge_index_i=None):
1267
+ """Predict mean proximity and bounded pairwise dispersion maps.
1268
+
1269
+ The legacy P_global API is preserved above. Dispersion is bounded by
1270
+ 0.25, the maximum variance of a value in [0, 1], which keeps the new
1271
+ head numerically compatible with the teacher target.
1272
+ """
1273
+ n = feat_i.shape[0]
1274
+ pair_feat = self._build_pair_features(feat_i, edge_index_i)
1275
+ flat = pair_feat.reshape(n * n, -1)
1276
+ mean_logits = torch.clamp(self.global_edge_predictor(flat), -10.0, 10.0).reshape(n, n)
1277
+ disp_logits = torch.clamp(self.dispersion_edge_predictor(flat), -10.0, 10.0).reshape(n, n)
1278
+ P_mean = torch.sigmoid(mean_logits)
1279
+ P_disp = 0.25 * torch.sigmoid(disp_logits)
1280
+ eye = torch.eye(n, device=feat_i.device)
1281
+ P_mean = torch.nan_to_num(P_mean, nan=0.5, posinf=1.0, neginf=0.0)
1282
+ P_disp = torch.nan_to_num(P_disp, nan=0.0, posinf=0.25, neginf=0.0)
1283
+ P_mean = 0.5 * (P_mean + P_mean.t()) * (1.0 - eye)
1284
+ P_disp = 0.5 * (P_disp + P_disp.t()) * (1.0 - eye)
1285
+ return P_mean.clamp(1e-6, 1.0 - 1e-6), P_disp.clamp(0.0, 0.25), pair_feat
1286
+
1287
+ def _group_graph_indices_by_mol_id(self, data, n_graphs: int) -> Dict[int, List[int]]:
1288
+ groups: Dict[int, List[int]] = {}
1289
+ if hasattr(data, "mol_id"):
1290
+ mol_ids = data.mol_id.detach().cpu().tolist()
1291
+ else:
1292
+ mol_ids = list(range(n_graphs))
1293
+ for idx, mol_id in enumerate(mol_ids):
1294
+ groups.setdefault(int(mol_id), []).append(idx)
1295
+ return groups
1296
+
1297
+ def _get_conf_idx(self, data, graph_idx: int) -> int:
1298
+ if hasattr(data, "conf_id"):
1299
+ return int(data.conf_id[graph_idx].item()) % self.max_confs
1300
+ return graph_idx % self.max_confs
1301
+
1302
+ # --------------------------------------------------------
1303
+ # a,b and fusion
1304
+ # --------------------------------------------------------
1305
+ def predict_ab(self, graph_feat_i, conf_idx: Optional[int] = None):
1306
+ device = graph_feat_i.device
1307
+ graph_feat_i = graph_feat_i.view(-1)
1308
+ fixed_a = float(getattr(self.args, "stage1_fixed_kernel_a", 0.0))
1309
+ fixed_b = float(getattr(self.args, "stage1_fixed_kernel_b", 0.0))
1310
+ if fixed_a > 0.0 and fixed_b > 0.0:
1311
+ return (
1312
+ graph_feat_i.new_tensor(fixed_a).clamp(1e-4, 1e4),
1313
+ graph_feat_i.new_tensor(fixed_b).clamp(1e-4, 10.0),
1314
+ )
1315
+ if conf_idx is None:
1316
+ conf_idx = 0
1317
+ conf_idx_tensor = torch.tensor(int(conf_idx) % self.max_confs, dtype=torch.long, device=device)
1318
+ conf_emb = self.conf_embedding(conf_idx_tensor)
1319
+ raw = self.ab_head(torch.cat([graph_feat_i, conf_emb], dim=-1))
1320
+ log_a = torch.clamp(raw[0], -10.0, 10.0)
1321
+ log_b = torch.clamp(raw[1], -10.0, 10.0)
1322
+ a = F.softplus(log_a) + 1e-4
1323
+ b = F.softplus(log_b) + 1e-4
1324
+ return a.clamp(1e-4, 1e4), b.clamp(1e-4, 10.0)
1325
+
1326
+ def predict_fusion_logits(self, graph_feat_i):
1327
+ graph_feat_i = graph_feat_i.view(-1)
1328
+ logits = []
1329
+ for k in range(self.max_confs):
1330
+ idx = torch.tensor(k, dtype=torch.long, device=graph_feat_i.device)
1331
+ conf_emb = self.conf_embedding(idx)
1332
+ logits.append(self.fusion_weight_head(torch.cat([graph_feat_i, conf_emb], dim=-1)).view(()))
1333
+ return torch.stack(logits, dim=0)
1334
+
1335
+ def _ab_regularization(self, a, b):
1336
+ if (
1337
+ float(getattr(self.args, "stage1_fixed_kernel_a", 0.0)) > 0.0
1338
+ and float(getattr(self.args, "stage1_fixed_kernel_b", 0.0)) > 0.0
1339
+ ):
1340
+ return a.sum() * 0.0
1341
+ lam = float(getattr(self.args, "lambda_ab", 1e-4))
1342
+ if lam <= 0:
1343
+ return a.sum() * 0.0
1344
+ reg = torch.log(a.clamp(min=1e-6)).pow(2) + torch.log(b.clamp(min=1e-6)).pow(2)
1345
+ return lam * reg
1346
+
1347
+ # --------------------------------------------------------
1348
+ # Decoding / modes
1349
+ # --------------------------------------------------------
1350
+ def _make_z(self, mode_idx: int, device, dtype, deterministic: bool) -> torch.Tensor:
1351
+ if deterministic:
1352
+ z = torch.zeros(self.mode_dim, device=device, dtype=dtype)
1353
+ if self.mode_dim > 0:
1354
+ z[mode_idx % self.mode_dim] = 1.0
1355
+ return z
1356
+ return torch.randn(self.mode_dim, device=device, dtype=dtype)
1357
+
1358
+ def predict_probability_modes(
1359
+ self,
1360
+ P_global: torch.Tensor,
1361
+ pair_feat: Optional[torch.Tensor] = None,
1362
+ graph_feat_i: Optional[torch.Tensor] = None,
1363
+ num_modes: Optional[int] = None,
1364
+ deterministic: bool = False,
1365
+ ) -> List[torch.Tensor]:
1366
+ if num_modes is None:
1367
+ num_modes = int(getattr(self.args, "num_modes", self.num_modes))
1368
+ use_mode_specific = bool(getattr(self.args, "use_mode_specific_p", False))
1369
+ modes = []
1370
+ for k in range(int(num_modes)):
1371
+ z = self._make_z(k, P_global.device, P_global.dtype, deterministic=deterministic)
1372
+ if use_mode_specific:
1373
+ modes.append(self.mode_generator(P_global, pair_feat, z))
1374
+ else:
1375
+ modes.append(P_global.clone())
1376
+ return modes
1377
+
1378
+ def decode_probability_mode(self, P: torch.Tensor, node_feat: torch.Tensor, z: Optional[torch.Tensor] = None,
1379
+ edge_index_i: Optional[torch.Tensor] = None,
1380
+ edge_attr: Optional[torch.Tensor] = None):
1381
+ if z is None:
1382
+ z = torch.zeros(self.mode_dim, device=P.device, dtype=P.dtype)
1383
+ y0 = self.conditioned_decoder(P_global=P, node_feat=node_feat, z=z,
1384
+ edge_index=edge_index_i, edge_attr=edge_attr)
1385
+ yT, traj = self.refiner(P=P, node_feat=node_feat, y0=y0,
1386
+ edge_index=edge_index_i, edge_attr=edge_attr)
1387
+ return y0, yT, traj
1388
+
1389
+ def decode_with_z(self, P_global: torch.Tensor, node_feat: torch.Tensor, z: torch.Tensor,
1390
+ edge_index_i: Optional[torch.Tensor] = None,
1391
+ edge_attr: Optional[torch.Tensor] = None):
1392
+ y0 = self.conditioned_decoder(P_global=P_global, node_feat=node_feat, z=z,
1393
+ edge_index=edge_index_i, edge_attr=edge_attr)
1394
+ yT, traj = self.refiner(P=P_global, node_feat=node_feat, y0=y0,
1395
+ edge_index=edge_index_i, edge_attr=edge_attr)
1396
+ return y0, yT, traj
1397
+
1398
+ # --------------------------------------------------------
1399
+ # Stage 1 loss
1400
+ # --------------------------------------------------------
1401
+ def compute_stage1_loss(self, data, node_feat, graph_feat):
1402
+ batch = data.batch
1403
+ n_graphs = int(data.num_graphs)
1404
+ groups = self._group_graph_indices_by_mol_id(data, n_graphs)
1405
+ target_strategy = str(getattr(self.args, "stage1_target_strategy", "fusion")).lower()
1406
+ stage1_kernel = str(getattr(self.args, "stage1_kernel", "umap")).lower()
1407
+ functional_mode = str(getattr(self.args, "functional_teacher_mode", "geometry")).lower()
1408
+ lambda_geom = float(getattr(self.args, "lambda_stage1_geometry", 1.0))
1409
+ lambda_func = float(getattr(self.args, "lambda_stage1_functional", 0.25))
1410
+ total = torch.tensor(0.0, device=node_feat.device)
1411
+ ab_reg = torch.tensor(0.0, device=node_feat.device)
1412
+ valid = 0
1413
+
1414
+ for _, graph_indices in groups.items():
1415
+ ref_idx = graph_indices[0]
1416
+ ref_mask = batch == ref_idx
1417
+ feat_i = node_feat[ref_mask]
1418
+ ref_graph_feat = graph_feat[ref_idx]
1419
+ if feat_i.size(0) <= 1:
1420
+ continue
1421
+ edge_i = self._get_local_edge_index(data.edge_index, ref_mask)
1422
+ use_mean_dispersion = bool(getattr(self.args, "mean_dispersion_prior", False))
1423
+ if use_mean_dispersion:
1424
+ P_global, P_disp, _ = self._predict_mean_dispersion(feat_i, edge_i)
1425
+ else:
1426
+ P_global, _ = self._predict_P_global(feat_i, edge_i)
1427
+ P_disp = None
1428
+ mol = _rdmol_for_graph(data, ref_idx)
1429
+ Q_geom_list, Q_functional_list, conf_idxs, q_graph_indices = [], [], [], []
1430
+ for gi in graph_indices:
1431
+ pos = data.pos[batch == gi]
1432
+ if pos.size(0) != feat_i.size(0):
1433
+ continue
1434
+ cidx = self._get_conf_idx(data, gi)
1435
+ a, b = self.predict_ab(ref_graph_feat, cidx)
1436
+ ab_reg = ab_reg + self._ab_regularization(a, b)
1437
+ q_geom = prob_low_dim_kernel(pos, a, b, kernel=stage1_kernel)
1438
+ Q_geom_list.append(q_geom)
1439
+ Q_functional_list.append(functional_aware_teacher_graph(q_geom, mol, self.args))
1440
+ conf_idxs.append(cidx)
1441
+ q_graph_indices.append(gi)
1442
+ if not Q_geom_list:
1443
+ continue
1444
+
1445
+ relevance_strength = max(0.0, float(getattr(self.args, "stage1_pair_relevance_weight", 0.0)))
1446
+ pair_weights = None
1447
+ if bool(getattr(self.args, "stage1_nonlocal_heavy_only", False)):
1448
+ n_atoms = int(feat_i.size(0))
1449
+ adjacency = torch.zeros((n_atoms, n_atoms), dtype=torch.bool, device=feat_i.device)
1450
+ if edge_i is not None and edge_i.numel() > 0:
1451
+ adjacency[edge_i[0], edge_i[1]] = True
1452
+ adjacency[edge_i[1], edge_i[0]] = True
1453
+ topo = torch.full((n_atoms, n_atoms), n_atoms + 1, dtype=torch.long, device=feat_i.device)
1454
+ topo.fill_diagonal_(0)
1455
+ topo[adjacency] = 1
1456
+ for pivot in range(n_atoms):
1457
+ topo = torch.minimum(topo, topo[:, pivot:pivot + 1] + topo[pivot:pivot + 1, :])
1458
+ atom_x = data.x[ref_mask] if hasattr(data, "x") else None
1459
+ heavy = heavy_atom_mask_from_x(atom_x)
1460
+ if heavy is None:
1461
+ heavy = torch.ones(n_atoms, dtype=torch.bool, device=feat_i.device)
1462
+ informative = (topo >= 3) & heavy[:, None] & heavy[None, :]
1463
+ informative.fill_diagonal_(False)
1464
+ if bool(informative.any()):
1465
+ pair_weights = informative.to(dtype=feat_i.dtype)
1466
+ if relevance_strength > 0.0 and len(Q_geom_list) > 1:
1467
+ # Pairs whose proximity changes across the reference ensemble
1468
+ # carry conformational information; nearly invariant bonded or
1469
+ # topology-dominated pairs receive their ordinary unit weight.
1470
+ variation = torch.stack(Q_geom_list, dim=0).std(dim=0, unbiased=False)
1471
+ off_diag = ~torch.eye(variation.size(0), dtype=torch.bool, device=variation.device)
1472
+ scale = variation[off_diag].mean().clamp_min(1e-6)
1473
+ clip = max(1.0, float(getattr(self.args, "stage1_pair_relevance_clip", 4.0)))
1474
+ relevance = (variation / scale).clamp(0.0, clip)
1475
+ relevance_weights = 1.0 + relevance_strength * relevance
1476
+ relevance_weights = 0.5 * (relevance_weights + relevance_weights.t())
1477
+ relevance_weights = relevance_weights.masked_fill(~off_diag, 0.0)
1478
+ pair_weights = relevance_weights if pair_weights is None else pair_weights * relevance_weights
1479
+
1480
+ def teacher_loss(q_geom: torch.Tensor, q_functional: torch.Tensor) -> torch.Tensor:
1481
+ if functional_mode == "geometry":
1482
+ return weighted_cross_entropy_loss(P_global, q_geom, pair_weights)
1483
+ if functional_mode == "functional":
1484
+ return weighted_cross_entropy_loss(P_global, q_functional, pair_weights)
1485
+ if functional_mode == "dual":
1486
+ return lambda_geom * weighted_cross_entropy_loss(P_global, q_geom, pair_weights) + lambda_func * weighted_cross_entropy_loss(
1487
+ P_global, q_functional, pair_weights
1488
+ )
1489
+ raise ValueError(
1490
+ f"Unknown functional_teacher_mode={functional_mode}. Expected geometry, functional, or dual."
1491
+ )
1492
+
1493
+ if use_mean_dispersion:
1494
+ # The new prior is supervised against the ensemble mean and
1495
+ # variance directly. This avoids treating each conformer as
1496
+ # an independent target for the dispersion head.
1497
+ q_geom_stack = torch.stack(Q_geom_list, dim=0)
1498
+ q_mean = q_geom_stack.mean(dim=0).clamp(1e-6, 1.0 - 1e-6)
1499
+ q_var = q_geom_stack.var(dim=0, unbiased=False).clamp(0.0, 0.25)
1500
+ mean_loss = teacher_loss(q_mean, torch.stack(Q_functional_list, dim=0).mean(dim=0))
1501
+ off_diag = ~torch.eye(P_disp.size(0), dtype=torch.bool, device=P_disp.device)
1502
+ disp_loss = F.mse_loss(P_disp[off_diag], q_var[off_diag])
1503
+ lambda_disp = float(getattr(self.args, "lambda_stage1_dispersion", 1.0))
1504
+ total = total + mean_loss + lambda_disp * disp_loss
1505
+
1506
+ elif target_strategy == "fusion":
1507
+ logits = self.predict_fusion_logits(ref_graph_feat)
1508
+ valid_logits = torch.stack([logits[c] for c in conf_idxs], dim=0)
1509
+ alpha = F.softmax(valid_logits, dim=0)
1510
+ q_geom_global = torch.zeros_like(Q_geom_list[0])
1511
+ q_functional_global = torch.zeros_like(Q_functional_list[0])
1512
+ for a_w, q_geom, q_functional in zip(alpha, Q_geom_list, Q_functional_list):
1513
+ q_geom_global = q_geom_global + a_w * q_geom
1514
+ q_functional_global = q_functional_global + a_w * q_functional
1515
+ total = total + teacher_loss(
1516
+ q_geom_global.clamp(1e-6, 1.0 - 1e-6),
1517
+ q_functional_global.clamp(1e-6, 1.0 - 1e-6),
1518
+ )
1519
+
1520
+ elif target_strategy == "conformer":
1521
+ # No fused teacher graph is constructed. Each true conformer
1522
+ # supervises the student independently, which is easier to
1523
+ # justify than a learnable mixture of probability graphs.
1524
+ ce_vals = [teacher_loss(q_geom, q_functional) for q_geom, q_functional in zip(Q_geom_list, Q_functional_list)]
1525
+ total = total + torch.stack(ce_vals).mean()
1526
+
1527
+ elif target_strategy in {"lowest_energy", "first"}:
1528
+ if target_strategy == "lowest_energy" and hasattr(data, "totalenergy"):
1529
+ energies = []
1530
+ for gi in q_graph_indices:
1531
+ e = data.totalenergy[gi]
1532
+ energies.append(torch.as_tensor(e, device=node_feat.device, dtype=node_feat.dtype).view(-1)[0])
1533
+ if energies:
1534
+ chosen = int(torch.argmin(torch.stack(energies)).detach().item())
1535
+ else:
1536
+ chosen = 0
1537
+ else:
1538
+ chosen = 0
1539
+ chosen = max(0, min(chosen, len(Q_geom_list) - 1))
1540
+ total = total + teacher_loss(
1541
+ Q_geom_list[chosen].clamp(1e-6, 1.0 - 1e-6),
1542
+ Q_functional_list[chosen].clamp(1e-6, 1.0 - 1e-6),
1543
+ )
1544
+
1545
+ else:
1546
+ raise ValueError(
1547
+ f"Unknown stage1_target_strategy={target_strategy}. "
1548
+ "Expected one of: fusion, conformer, lowest_energy, first."
1549
+ )
1550
+ valid += 1
1551
+
1552
+ if valid == 0:
1553
+ zero = node_feat.sum() * 0.0
1554
+ return zero, zero
1555
+ return total / valid, ab_reg / max(1, valid)
1556
+
1557
+ # --------------------------------------------------------
1558
+ # Geometry loss helper
1559
+ # --------------------------------------------------------
1560
+ def _geometry_loss_one(self, y, pos, edge_index_i, torsion_index_i=None):
1561
+ loss = y.sum() * 0.0
1562
+ lb = float(getattr(self.args, "lambda_bond", 0.0))
1563
+ la = float(getattr(self.args, "lambda_angle", 0.0))
1564
+ lt = float(getattr(self.args, "lambda_torsion", 0.0))
1565
+ lc = float(getattr(self.args, "lambda_clash", getattr(self.args, "lambda_steric", 0.0)))
1566
+ lch = float(getattr(self.args, "lambda_chiral", 0.0))
1567
+ if lb > 0:
1568
+ loss = loss + lb * bond_length_loss(y, edge_index_i)
1569
+ if la > 0:
1570
+ loss = loss + la * angle_loss(y, edge_index_i)
1571
+ if lt > 0:
1572
+ loss = loss + lt * torsion_loss(y, torsion_index_i)
1573
+ if lc > 0:
1574
+ loss = loss + lc * steric_clash_loss(y)
1575
+ if lch > 0:
1576
+ loss = loss + lch * chirality_loss(y)
1577
+ return loss
1578
+
1579
+ def _collect_pred_pos(self, data, y_pred_by_graph: List[Optional[torch.Tensor]]):
1580
+ out = []
1581
+ for gi in range(int(data.num_graphs)):
1582
+ pos_g = data.pos[data.batch == gi]
1583
+ y = y_pred_by_graph[gi]
1584
+ out.append(torch.zeros_like(pos_g) if y is None else y)
1585
+ return torch.cat(out, dim=0) if out else torch.zeros_like(data.pos)
1586
+
1587
+ def _reference_diversity_margin(self, data, graph_indices, batch, fallback, scale):
1588
+ ref_positions = []
1589
+ atom_x = None
1590
+ for gi in graph_indices:
1591
+ graph_mask = batch == gi
1592
+ pos = data.pos[graph_mask]
1593
+ if pos.size(0) <= 1:
1594
+ continue
1595
+ ref_positions.append(pos.detach())
1596
+ if atom_x is None and hasattr(data, "x"):
1597
+ atom_x = data.x[graph_mask]
1598
+ if len(ref_positions) < 2:
1599
+ return float(fallback)
1600
+
1601
+ vals = []
1602
+ max_refs = min(len(ref_positions), 5)
1603
+ for i in range(max_refs):
1604
+ for j in range(i + 1, max_refs):
1605
+ val = safe_heavy_kabsch_rmsd(ref_positions[i], ref_positions[j], atom_x)
1606
+ if torch.isfinite(val):
1607
+ vals.append(val.detach())
1608
+ if not vals:
1609
+ return float(fallback)
1610
+ mean_ref = torch.stack(vals).mean().clamp(0.0, 5.0)
1611
+ margin = float(mean_ref.item()) * float(scale)
1612
+ return max(0.05, min(float(fallback), margin))
1613
+
1614
+ def _mode_diversity_loss(self, mode_outputs, atom_x, margin, max_pairs):
1615
+ y_modes = [item[1] for item in mode_outputs if len(item) >= 2 and item[1] is not None]
1616
+ if len(y_modes) < 2:
1617
+ if y_modes:
1618
+ return y_modes[0].sum() * 0.0
1619
+ if mode_outputs:
1620
+ return mode_outputs[0][0].sum() * 0.0
1621
+ return torch.tensor(0.0)
1622
+
1623
+ loss = y_modes[0].sum() * 0.0
1624
+ pair_count = 0
1625
+ for i in range(len(y_modes)):
1626
+ for j in range(i + 1, len(y_modes)):
1627
+ dist = safe_heavy_kabsch_rmsd(y_modes[i], y_modes[j], atom_x).clamp(0.0, 10.0)
1628
+ loss = loss + F.relu(float(margin) - dist).pow(2)
1629
+ pair_count += 1
1630
+ if pair_count >= int(max_pairs):
1631
+ break
1632
+ if pair_count >= int(max_pairs):
1633
+ break
1634
+ if pair_count == 0:
1635
+ return y_modes[0].sum() * 0.0
1636
+ return (loss / pair_count).clamp(0.0, 10.0)
1637
+
1638
+ def _set_coverage_loss(
1639
+ self,
1640
+ mode_outputs,
1641
+ ref_positions: List[torch.Tensor],
1642
+ atom_x: Optional[torch.Tensor],
1643
+ lambda_recall: float,
1644
+ lambda_precision: float,
1645
+ clamp_value: float,
1646
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
1647
+ """Set-level coverage objective between generated modes and references.
1648
+
1649
+ Recall term: every reference conformer should be close to at least one
1650
+ generated mode. Precision term: every generated mode should remain close
1651
+ to at least one reference. This explicitly trades some precision for
1652
+ better ensemble coverage when lambda_recall dominates.
1653
+ """
1654
+ y_modes = [item[1] for item in mode_outputs if len(item) >= 2 and item[1] is not None]
1655
+ if len(y_modes) == 0 or len(ref_positions) == 0:
1656
+ base = mode_outputs[0][0] if mode_outputs else None
1657
+ zero = base.sum() * 0.0 if base is not None else torch.tensor(0.0)
1658
+ return zero, zero, zero
1659
+
1660
+ rows = []
1661
+ for ref in ref_positions:
1662
+ vals = []
1663
+ for y in y_modes:
1664
+ val = safe_heavy_kabsch_rmsd(y, ref, atom_x)
1665
+ val = torch.nan_to_num(val, nan=float(clamp_value), posinf=float(clamp_value), neginf=0.0)
1666
+ vals.append(val.clamp(0.0, float(clamp_value)))
1667
+ rows.append(torch.stack(vals, dim=0))
1668
+ dist = torch.stack(rows, dim=0)
1669
+
1670
+ recall = dist.min(dim=1).values.mean()
1671
+ precision = dist.min(dim=0).values.mean()
1672
+ loss = float(lambda_recall) * recall + float(lambda_precision) * precision
1673
+ return loss.clamp(0.0, float(clamp_value)), recall.detach(), precision.detach()
1674
+
1675
+ def _cluster_mode_assignments(self, data, graph_indices, batch, num_modes: int) -> Dict[int, int]:
1676
+ """Assign reference conformers to geometry clusters using heavy-atom RMSD.
1677
+
1678
+ A small farthest-point clustering is enough here because the training
1679
+ set usually has only max_confs reference conformers per molecule.
1680
+ Cluster ids are used as stable mode ids during Stage 2 supervision.
1681
+ """
1682
+ valid_graphs = []
1683
+ positions = []
1684
+ atom_x = None
1685
+ for gi in graph_indices:
1686
+ graph_mask = batch == gi
1687
+ pos = data.pos[graph_mask]
1688
+ if pos.size(0) <= 1:
1689
+ continue
1690
+ valid_graphs.append(int(gi))
1691
+ positions.append(pos.detach())
1692
+ if atom_x is None and hasattr(data, "x"):
1693
+ atom_x = data.x[graph_mask]
1694
+
1695
+ n = len(valid_graphs)
1696
+ if n == 0:
1697
+ return {}
1698
+ if n == 1 or int(num_modes) <= 1:
1699
+ return {valid_graphs[i]: 0 for i in range(n)}
1700
+
1701
+ dist = positions[0].new_zeros((n, n))
1702
+ for i in range(n):
1703
+ for j in range(i + 1, n):
1704
+ val = safe_heavy_kabsch_rmsd(positions[i], positions[j], atom_x).detach()
1705
+ val = torch.nan_to_num(val, nan=0.0, posinf=10.0, neginf=0.0).clamp(0.0, 10.0)
1706
+ dist[i, j] = val
1707
+ dist[j, i] = val
1708
+
1709
+ k_clusters = max(1, min(int(num_modes), n))
1710
+ mean_dist = dist.mean(dim=1)
1711
+ anchors = [int(torch.argmax(mean_dist).item())]
1712
+ while len(anchors) < k_clusters:
1713
+ min_to_anchor = dist[:, anchors].min(dim=1).values
1714
+ min_to_anchor[torch.tensor(anchors, device=dist.device, dtype=torch.long)] = -1.0
1715
+ anchors.append(int(torch.argmax(min_to_anchor).item()))
1716
+
1717
+ anchor_dist = dist[:, anchors]
1718
+ nearest = torch.argmin(anchor_dist, dim=1).detach().cpu().tolist()
1719
+ return {valid_graphs[i]: int(nearest[i]) for i in range(n)}
1720
+
1721
+ # --------------------------------------------------------
1722
+ # Stage 2 loss: Z-conditioned + edge_attr
1723
+ # --------------------------------------------------------
1724
+ def compute_multimode_loss(self, data, node_feat, graph_feat):
1725
+ """
1726
+ Stage 2: 多模式坐标解码(coarse + refine)
1727
+ 损失: RMSD + CE(P(Y), P_global) 流形保持
1728
+ """
1729
+ batch = data.batch
1730
+ n_graphs = int(data.num_graphs)
1731
+ groups = self._group_graph_indices_by_mol_id(data, n_graphs)
1732
+ total = torch.tensor(0.0, device=node_feat.device)
1733
+ pos_log = torch.tensor(0.0, device=node_feat.device)
1734
+ y_pred_by_graph: List[Optional[torch.Tensor]] = [None for _ in range(n_graphs)]
1735
+ valid = 0
1736
+
1737
+ num_modes = int(getattr(self.args, "num_modes", self.num_modes))
1738
+ lc = float(getattr(self.args, "lambda_coarse_rmsd", 1.0))
1739
+ lr = float(getattr(self.args, "lambda_refine_rmsd", 1.0))
1740
+ lambda_ce_manifold = float(getattr(self.args, "lambda_ce_manifold", 0.1))
1741
+ lambda_set_recall = float(getattr(self.args, "lambda_set_recall", 0.0))
1742
+ lambda_set_precision = float(getattr(self.args, "lambda_set_precision", 0.0))
1743
+ set_loss_clamp = float(getattr(self.args, "set_loss_clamp", 10.0))
1744
+ use_set_coverage = lambda_set_recall > 0 or lambda_set_precision > 0
1745
+ lambda_bond = float(getattr(self.args, "lambda_bond", 0.0))
1746
+ lambda_angle = float(getattr(self.args, "lambda_angle", 0.0))
1747
+ lambda_pair_dist = float(getattr(self.args, "lambda_pair_dist", 0.0))
1748
+ lambda_coarse_pair_dist = float(getattr(self.args, "lambda_coarse_pair_dist", 0.0))
1749
+ pair_dist_max_pairs = int(getattr(self.args, "pair_dist_max_pairs", 2048))
1750
+ pair_dist_min_ref = float(getattr(self.args, "pair_dist_min_ref", 0.0))
1751
+ pair_dist_max_ref = float(getattr(self.args, "pair_dist_max_ref", 12.0))
1752
+ lambda_clash = float(getattr(self.args, "lambda_clash", getattr(self.args, "lambda_steric", 0.0)))
1753
+ lambda_mode_diversity = float(getattr(self.args, "lambda_mode_diversity", 0.0))
1754
+ mode_diversity_margin = float(getattr(self.args, "mode_diversity_margin", 0.6))
1755
+ mode_diversity_ref_scale = float(getattr(self.args, "mode_diversity_ref_scale", 0.7))
1756
+ mode_diversity_max_pairs = int(getattr(self.args, "mode_diversity_max_pairs", 15))
1757
+ lambda_signed_torsion = float(getattr(self.args, "lambda_signed_torsion", 0.0))
1758
+ use_torsion_residual = bool(getattr(self.args, "use_torsion_residual", False))
1759
+ lambda_torsion_residual_rmsd = float(getattr(self.args, "lambda_torsion_residual_rmsd", 0.0))
1760
+ lambda_torsion_delta = float(getattr(self.args, "lambda_torsion_delta", 0.0))
1761
+ torsion_residual_max = int(getattr(self.args, "torsion_residual_max", 32))
1762
+ lambda_chiral_volume = float(getattr(self.args, "lambda_chiral_volume", 0.0))
1763
+ signed_torsion_max = int(getattr(self.args, "signed_torsion_max", 64))
1764
+ chiral_volume_max_centers = int(getattr(self.args, "chiral_volume_max_centers", 64))
1765
+ chiral_volume_ref_abs_min = float(getattr(self.args, "chiral_volume_ref_abs_min", 0.05))
1766
+ mode_assignment_strategy = str(getattr(self.args, "mode_assignment_strategy", "min")).lower()
1767
+ deterministic_modes = bool(getattr(self.args, "deterministic_modes", False))
1768
+ detach_refiner_input = bool(getattr(self.args, "detach_refiner_input", True))
1769
+
1770
+ for _, graph_indices in groups.items():
1771
+ cluster_assignments = {}
1772
+ if mode_assignment_strategy == "cluster":
1773
+ cluster_assignments = self._cluster_mode_assignments(data, graph_indices, batch, num_modes)
1774
+
1775
+ ref_idx = graph_indices[0]
1776
+ ref_mask = batch == ref_idx
1777
+ feat_i = node_feat[ref_mask]
1778
+ graph_feat_i = graph_feat[ref_idx]
1779
+ if feat_i.size(0) <= 1:
1780
+ continue
1781
+
1782
+ edge_i = self._get_local_edge_index(data.edge_index, ref_mask)
1783
+ local_ei, local_ea = self._get_local_edge_attr(
1784
+ getattr(data, "edge_attr", None), data.edge_index, ref_mask
1785
+ )
1786
+ local_torsion_index = self._get_local_torsion_index(
1787
+ getattr(data, "torsion_index", None), ref_mask
1788
+ )
1789
+ P_global, pair_feat = self._predict_P_global(feat_i, edge_i)
1790
+ end2end_pglobal = bool(getattr(self.args, "use_end2end_pglobal", False))
1791
+ P_det = P_global if end2end_pglobal else P_global.detach()
1792
+ pair_det = pair_feat if end2end_pglobal else pair_feat.detach()
1793
+ feat_det = feat_i if end2end_pglobal else feat_i.detach()
1794
+ use_mode_specific_p = bool(getattr(self.args, "use_mode_specific_p", False))
1795
+
1796
+ mode_outputs = []
1797
+ for k in range(num_modes):
1798
+ z = self._make_z(k, P_global.device, P_global.dtype, deterministic=deterministic_modes)
1799
+ if use_mode_specific_p:
1800
+ P_mode = self.mode_generator(P_det, pair_det, z)
1801
+ else:
1802
+ P_mode = P_det
1803
+ y0 = self.conditioned_decoder(
1804
+ P_global=P_mode, node_feat=feat_det, z=z,
1805
+ edge_index=local_ei, edge_attr=local_ea
1806
+ )
1807
+ y0_for_refiner = y0.detach() if detach_refiner_input else y0
1808
+ yT, _ = self.refiner(
1809
+ P=P_mode, node_feat=feat_det, y0=y0_for_refiner,
1810
+ edge_index=local_ei, edge_attr=local_ea
1811
+ )
1812
+ if use_torsion_residual:
1813
+ torsion_delta = self.torsion_residual_head(
1814
+ node_feat=feat_det,
1815
+ coords=yT,
1816
+ P=P_mode,
1817
+ torsion_index=local_torsion_index,
1818
+ max_torsions=torsion_residual_max,
1819
+ )
1820
+ yTR = apply_torsion_residuals(
1821
+ yT,
1822
+ torsions=local_torsion_index,
1823
+ deltas=torsion_delta,
1824
+ edge_index=local_ei,
1825
+ max_torsions=torsion_residual_max,
1826
+ )
1827
+ else:
1828
+ torsion_delta = None
1829
+ yTR = yT
1830
+ mode_outputs.append((y0, yTR, P_mode, yT, torsion_delta))
1831
+
1832
+ group_diversity_loss = torch.tensor(0.0, device=node_feat.device)
1833
+ if lambda_mode_diversity > 0 and len(mode_outputs) > 1:
1834
+ ref_atom_x = data.x[ref_mask] if hasattr(data, "x") else None
1835
+ margin = self._reference_diversity_margin(
1836
+ data=data,
1837
+ graph_indices=graph_indices,
1838
+ batch=batch,
1839
+ fallback=mode_diversity_margin,
1840
+ scale=mode_diversity_ref_scale,
1841
+ )
1842
+ group_diversity_loss = self._mode_diversity_loss(
1843
+ mode_outputs=mode_outputs,
1844
+ atom_x=ref_atom_x,
1845
+ margin=margin,
1846
+ max_pairs=mode_diversity_max_pairs,
1847
+ )
1848
+ group_diversity_loss = torch.nan_to_num(
1849
+ group_diversity_loss, nan=0.0, posinf=10.0, neginf=0.0
1850
+ ).clamp(0.0, 10.0)
1851
+ total = total + lambda_mode_diversity * group_diversity_loss
1852
+
1853
+ if use_set_coverage:
1854
+ ref_positions = []
1855
+ set_atom_x = None
1856
+ for gi in graph_indices:
1857
+ graph_mask = batch == gi
1858
+ pos = data.pos[graph_mask]
1859
+ if pos.size(0) != feat_i.size(0):
1860
+ continue
1861
+ ref_positions.append(pos)
1862
+ if set_atom_x is None and hasattr(data, "x"):
1863
+ set_atom_x = data.x[graph_mask]
1864
+ if ref_positions:
1865
+ set_loss, _, _ = self._set_coverage_loss(
1866
+ mode_outputs=mode_outputs,
1867
+ ref_positions=ref_positions,
1868
+ atom_x=set_atom_x,
1869
+ lambda_recall=lambda_set_recall,
1870
+ lambda_precision=lambda_set_precision,
1871
+ clamp_value=set_loss_clamp,
1872
+ )
1873
+ set_loss = torch.nan_to_num(
1874
+ set_loss, nan=set_loss_clamp, posinf=set_loss_clamp, neginf=0.0
1875
+ ).clamp(0.0, set_loss_clamp)
1876
+ # Keep scaling compatible with the final /valid normalization.
1877
+ total = total + set_loss * max(1, len(ref_positions))
1878
+
1879
+ for gi in graph_indices:
1880
+ graph_mask = batch == gi
1881
+ pos = data.pos[graph_mask]
1882
+ if pos.size(0) != feat_i.size(0):
1883
+ continue
1884
+ atom_x = data.x[graph_mask] if hasattr(data, "x") else None
1885
+
1886
+ # 获取该构象的 a, b
1887
+ cidx = self._get_conf_idx(data, gi)
1888
+ ab_feat = graph_feat_i if end2end_pglobal else graph_feat_i.detach()
1889
+ a, b = self.predict_ab(ab_feat, cidx)
1890
+ if not end2end_pglobal:
1891
+ a = a.detach()
1892
+ b = b.detach()
1893
+ a = a.clamp(1e-4, 1e4)
1894
+ b = b.clamp(1e-4, 10.0)
1895
+ Q_ref = None
1896
+ lambda_mode_p_ce = float(getattr(self.args, "lambda_mode_p_ce", 0.0))
1897
+ if lambda_mode_p_ce > 0:
1898
+ try:
1899
+ stage1_kernel = str(getattr(self.args, "stage1_kernel", "umap")).lower()
1900
+ Q_ref = prob_low_dim_kernel(pos, a, b, kernel=stage1_kernel)
1901
+ except Exception:
1902
+ Q_ref = None
1903
+
1904
+ rmsds = []
1905
+ selection_rmsds = []
1906
+ for y0, yT, P_mode, y_base, torsion_delta in mode_outputs:
1907
+ rmsd0 = safe_heavy_kabsch_rmsd(y0, pos, atom_x).clamp(0.0, 10.0)
1908
+ rmsdT = safe_heavy_kabsch_rmsd(yT, pos, atom_x).clamp(0.0, 10.0)
1909
+ selection_rmsds.append(rmsdT)
1910
+ torsion_residual_rmsd = torch.tensor(0.0, device=node_feat.device)
1911
+ if lambda_torsion_residual_rmsd > 0 and use_torsion_residual:
1912
+ torsion_residual_rmsd = rmsdT
1913
+
1914
+ geom_reg = torch.tensor(0.0, device=node_feat.device)
1915
+ if lambda_bond > 0:
1916
+ try:
1917
+ bond_reg = 0.5 * (
1918
+ bond_distance_match_loss(y0, pos, local_ei)
1919
+ + bond_distance_match_loss(yT, pos, local_ei)
1920
+ )
1921
+ geom_reg = geom_reg + lambda_bond * torch.nan_to_num(
1922
+ bond_reg, nan=0.0, posinf=10.0, neginf=0.0
1923
+ ).clamp(0.0, 10.0)
1924
+ except Exception:
1925
+ pass
1926
+ if lambda_angle > 0:
1927
+ try:
1928
+ angle_reg = 0.5 * (angle_loss(y0, local_ei) + angle_loss(yT, local_ei))
1929
+ geom_reg = geom_reg + lambda_angle * torch.nan_to_num(
1930
+ angle_reg, nan=0.0, posinf=10.0, neginf=0.0
1931
+ ).clamp(0.0, 10.0)
1932
+ except Exception:
1933
+ pass
1934
+ if lambda_pair_dist > 0 or lambda_coarse_pair_dist > 0:
1935
+ try:
1936
+ if lambda_coarse_pair_dist > 0:
1937
+ coarse_pair_reg = pair_distance_match_loss(
1938
+ y0,
1939
+ pos,
1940
+ atom_x=atom_x,
1941
+ edge_index=local_ei,
1942
+ max_pairs=pair_dist_max_pairs,
1943
+ min_ref=pair_dist_min_ref,
1944
+ max_ref=pair_dist_max_ref,
1945
+ )
1946
+ geom_reg = geom_reg + lambda_coarse_pair_dist * coarse_pair_reg
1947
+ if lambda_pair_dist > 0:
1948
+ pair_reg = pair_distance_match_loss(
1949
+ yT,
1950
+ pos,
1951
+ atom_x=atom_x,
1952
+ edge_index=local_ei,
1953
+ max_pairs=pair_dist_max_pairs,
1954
+ min_ref=pair_dist_min_ref,
1955
+ max_ref=pair_dist_max_ref,
1956
+ )
1957
+ geom_reg = geom_reg + lambda_pair_dist * pair_reg
1958
+ except Exception:
1959
+ pass
1960
+ if lambda_clash > 0:
1961
+ try:
1962
+ clash_reg = 0.5 * (
1963
+ steric_clash_loss(y0, edge_index=local_ei)
1964
+ + steric_clash_loss(yT, edge_index=local_ei)
1965
+ )
1966
+ geom_reg = geom_reg + lambda_clash * torch.nan_to_num(
1967
+ clash_reg, nan=0.0, posinf=10.0, neginf=0.0
1968
+ ).clamp(0.0, 10.0)
1969
+ except Exception:
1970
+ pass
1971
+
1972
+ # 流形保持 CE 损失
1973
+ ce_manifold = torch.tensor(0.0, device=node_feat.device)
1974
+ if lambda_ce_manifold > 0:
1975
+ try:
1976
+ P_pred = prob_low_dim(yT, a, b)
1977
+ P_pred = torch.nan_to_num(P_pred, nan=0.5, posinf=1.0, neginf=0.0).clamp(1e-6, 1.0 - 1e-6)
1978
+ target_P = P_mode if use_mode_specific_p else P_det
1979
+ ce_manifold = cross_entropy_loss(P_pred, target_P)
1980
+ ce_manifold = torch.nan_to_num(ce_manifold, nan=0.0, posinf=10.0, neginf=0.0).clamp(0.0,
1981
+ 10.0)
1982
+ except Exception:
1983
+ ce_manifold = torch.tensor(0.0, device=node_feat.device)
1984
+
1985
+ mode_p_ce = torch.tensor(0.0, device=node_feat.device)
1986
+ if lambda_mode_p_ce > 0 and Q_ref is not None:
1987
+ try:
1988
+ mode_p_ce = cross_entropy_loss(P_mode, Q_ref)
1989
+ mode_p_ce = torch.nan_to_num(
1990
+ mode_p_ce, nan=0.0, posinf=10.0, neginf=0.0
1991
+ ).clamp(0.0, 10.0)
1992
+ except Exception:
1993
+ mode_p_ce = torch.tensor(0.0, device=node_feat.device)
1994
+
1995
+ signed_torsion = torch.tensor(0.0, device=node_feat.device)
1996
+ if lambda_signed_torsion > 0:
1997
+ try:
1998
+ signed_torsion = signed_torsion_supervision_loss(
1999
+ yT,
2000
+ pos,
2001
+ local_torsion_index,
2002
+ max_torsions=signed_torsion_max,
2003
+ )
2004
+ signed_torsion = torch.nan_to_num(
2005
+ signed_torsion, nan=0.0, posinf=10.0, neginf=0.0
2006
+ ).clamp(0.0, 10.0)
2007
+ except Exception:
2008
+ signed_torsion = torch.tensor(0.0, device=node_feat.device)
2009
+
2010
+ chiral_volume = torch.tensor(0.0, device=node_feat.device)
2011
+ if lambda_chiral_volume > 0:
2012
+ try:
2013
+ chiral_volume = signed_volume_supervision_loss(
2014
+ yT,
2015
+ pos,
2016
+ local_ei,
2017
+ max_centers=chiral_volume_max_centers,
2018
+ ref_abs_min=chiral_volume_ref_abs_min,
2019
+ )
2020
+ chiral_volume = torch.nan_to_num(
2021
+ chiral_volume, nan=0.0, posinf=10.0, neginf=0.0
2022
+ ).clamp(0.0, 10.0)
2023
+ except Exception:
2024
+ chiral_volume = torch.tensor(0.0, device=node_feat.device)
2025
+
2026
+ torsion_delta_loss = torch.tensor(0.0, device=node_feat.device)
2027
+ if lambda_torsion_delta > 0 and torsion_delta is not None and torsion_delta.numel() > 0:
2028
+ try:
2029
+ rows = _torsion_rows(local_torsion_index)
2030
+ if rows is not None:
2031
+ rows = rows[: min(torsion_residual_max, rows.size(0), torsion_delta.size(0))].to(y_base.device).long()
2032
+ torsion_valid_mask = (rows >= 0).all(dim=1) & (rows < y_base.size(0)).all(dim=1)
2033
+ rows = rows[torsion_valid_mask]
2034
+ if rows.numel() > 0:
2035
+ base_t = torsion_angles(y_base, rows)
2036
+ ref_t = torsion_angles(pos, rows).detach()
2037
+ target_delta = periodic_angle_delta(ref_t, base_t).detach()
2038
+ pred_delta = torsion_delta[: rows.size(0)]
2039
+ torsion_delta_loss = (1.0 - torch.cos(pred_delta - target_delta)).mean()
2040
+ torsion_delta_loss = torch.nan_to_num(
2041
+ torsion_delta_loss, nan=0.0, posinf=10.0, neginf=0.0
2042
+ ).clamp(0.0, 10.0)
2043
+ except Exception:
2044
+ torsion_delta_loss = torch.tensor(0.0, device=node_feat.device)
2045
+
2046
+ total_rmsd = (
2047
+ (0.0 if use_set_coverage else lc) * rmsd0
2048
+ + (0.0 if use_set_coverage else lr) * rmsdT
2049
+ + lambda_torsion_residual_rmsd * torsion_residual_rmsd
2050
+ + geom_reg
2051
+ + lambda_ce_manifold * ce_manifold
2052
+ + lambda_mode_p_ce * mode_p_ce
2053
+ + lambda_signed_torsion * signed_torsion
2054
+ + lambda_torsion_delta * torsion_delta_loss
2055
+ + lambda_chiral_volume * chiral_volume
2056
+ )
2057
+ total_rmsd = torch.nan_to_num(total_rmsd, nan=10.0, posinf=10.0, neginf=0.0).clamp(0.0, 10.0)
2058
+ rmsds.append(total_rmsd)
2059
+
2060
+ if not rmsds:
2061
+ continue
2062
+
2063
+ rmsds_t = torch.stack(rmsds)
2064
+ rmsds_t = torch.nan_to_num(rmsds_t, nan=10.0, posinf=10.0, neginf=0.0)
2065
+ selection_t = torch.stack(selection_rmsds)
2066
+ selection_t = torch.nan_to_num(selection_t, nan=10.0, posinf=10.0, neginf=0.0)
2067
+ if mode_assignment_strategy == "conf_id":
2068
+ best = int(cidx) % int(len(mode_outputs))
2069
+ elif mode_assignment_strategy == "cluster":
2070
+ best = int(cluster_assignments.get(int(gi), int(cidx) % int(len(mode_outputs))))
2071
+ best = max(0, min(best, int(len(mode_outputs)) - 1))
2072
+ else:
2073
+ best = int(torch.argmin(selection_t.detach()).item())
2074
+
2075
+ total = total + rmsds_t[best]
2076
+ pos_val = safe_heavy_kabsch_rmsd(mode_outputs[best][1], pos, atom_x).clamp(0.0, 10.0).detach()
2077
+ pos_val = torch.nan_to_num(pos_val, nan=10.0, posinf=10.0, neginf=0.0)
2078
+ pos_log = pos_log + pos_val
2079
+ y_pred_by_graph[gi] = mode_outputs[best][1]
2080
+ valid += 1
2081
+
2082
+ if valid == 0:
2083
+ zero = node_feat.sum() * 0.0
2084
+ return zero, zero, y_pred_by_graph
2085
+ return (total / valid).clamp(0.0, 10.0), (pos_log / valid).clamp(0.0, 10.0), y_pred_by_graph
2086
+
2087
+ # --------------------------------------------------------
2088
+ # Legacy geometry helpers kept for checkpoint compatibility.
2089
+ # --------------------------------------------------------
2090
+ def compute_coarse_loss(self, data, node_feat, graph_feat):
2091
+ return self._compute_single_geometry_stage(data, node_feat, graph_feat, mode="coarse")
2092
+
2093
+ def compute_refine_loss(self, data, node_feat, graph_feat):
2094
+ return self._compute_single_geometry_stage(data, node_feat, graph_feat, mode="refine")
2095
+
2096
+ def compute_geom_loss(self, data, node_feat, graph_feat):
2097
+ return self._compute_single_geometry_stage(data, node_feat, graph_feat, mode="geom")
2098
+
2099
+ def _compute_single_geometry_stage(self, data, node_feat, graph_feat, mode: str):
2100
+ batch = data.batch
2101
+ n_graphs = int(data.num_graphs)
2102
+ groups = self._group_graph_indices_by_mol_id(data, n_graphs)
2103
+ total = torch.tensor(0.0, device=node_feat.device)
2104
+ pos_log = torch.tensor(0.0, device=node_feat.device)
2105
+ y_pred_by_graph: List[Optional[torch.Tensor]] = [None for _ in range(n_graphs)]
2106
+ valid = 0
2107
+
2108
+ lc = float(getattr(self.args, "lambda_coarse_rmsd", 1.0))
2109
+ lr = float(getattr(self.args, "lambda_refine_rmsd", getattr(self.args, "lambda_rmsd", 1.0)))
2110
+ lambda_ce_manifold = float(getattr(self.args, "lambda_ce_manifold", 0.1))
2111
+
2112
+ for _, graph_indices in groups.items():
2113
+ ref_idx = graph_indices[0]
2114
+ ref_mask = batch == ref_idx
2115
+ feat_i = node_feat[ref_mask]
2116
+ graph_feat_i = graph_feat[ref_idx]
2117
+ if feat_i.size(0) <= 1:
2118
+ continue
2119
+
2120
+ edge_i = self._get_local_edge_index(data.edge_index, ref_mask)
2121
+ local_ei, local_ea = self._get_local_edge_attr(
2122
+ getattr(data, "edge_attr", None), data.edge_index, ref_mask
2123
+ )
2124
+ P_global, _ = self._predict_P_global(feat_i, edge_i)
2125
+ P_det = P_global.detach()
2126
+ feat_det = feat_i.detach()
2127
+
2128
+ for gi in graph_indices:
2129
+ graph_mask = batch == gi
2130
+ pos = data.pos[graph_mask]
2131
+ if pos.size(0) != feat_i.size(0):
2132
+ continue
2133
+ atom_x = data.x[graph_mask] if hasattr(data, "x") else None
2134
+
2135
+ cidx = self._get_conf_idx(data, gi)
2136
+ a, b = self.predict_ab(graph_feat_i.detach(), cidx)
2137
+ a = a.detach().clamp(1e-4, 1e4)
2138
+ b = b.detach().clamp(1e-4, 10.0)
2139
+
2140
+ if mode == "coarse":
2141
+ z = torch.zeros(self.mode_dim, device=P_det.device, dtype=P_det.dtype)
2142
+ y0 = self.conditioned_decoder(
2143
+ P_global=P_det, node_feat=feat_det, z=z,
2144
+ edge_index=local_ei, edge_attr=local_ea
2145
+ )
2146
+ rmsd0 = safe_heavy_kabsch_rmsd(y0, pos, atom_x).clamp(0.0, 10.0)
2147
+
2148
+ ce_manifold = torch.tensor(0.0, device=node_feat.device)
2149
+ if lambda_ce_manifold > 0:
2150
+ try:
2151
+ P_pred = prob_low_dim(y0, a, b)
2152
+ P_pred = torch.nan_to_num(P_pred, nan=0.5, posinf=1.0, neginf=0.0).clamp(1e-6, 1.0 - 1e-6)
2153
+ ce_manifold = cross_entropy_loss(P_pred, P_det)
2154
+ ce_manifold = torch.nan_to_num(ce_manifold, nan=0.0, posinf=10.0, neginf=0.0).clamp(0.0,
2155
+ 10.0)
2156
+ except Exception:
2157
+ ce_manifold = torch.tensor(0.0, device=node_feat.device)
2158
+
2159
+ loss = lc * rmsd0 + lambda_ce_manifold * ce_manifold
2160
+ loss = torch.nan_to_num(loss, nan=10.0, posinf=10.0, neginf=0.0).clamp(0.0, 10.0)
2161
+ y_pred = y0
2162
+ pos_val = rmsd0
2163
+
2164
+ elif mode == "refine":
2165
+ with torch.no_grad():
2166
+ z = torch.zeros(self.mode_dim, device=P_det.device, dtype=P_det.dtype)
2167
+ y0 = self.conditioned_decoder(
2168
+ P_global=P_det, node_feat=feat_det, z=z,
2169
+ edge_index=local_ei, edge_attr=local_ea
2170
+ )
2171
+ yT, traj = self.refiner(
2172
+ P=P_det, node_feat=feat_det, y0=y0.detach(),
2173
+ edge_index=local_ei, edge_attr=local_ea
2174
+ )
2175
+ rmsdT = safe_heavy_kabsch_rmsd(yT, pos, atom_x).clamp(0.0, 10.0)
2176
+
2177
+ step_loss = torch.tensor(0.0, device=node_feat.device)
2178
+ if traj is not None and len(traj) > 0:
2179
+ valid_steps = 0
2180
+ for yt in traj:
2181
+ if yt is None or yt.shape != pos.shape:
2182
+ continue
2183
+ rmsd_step = safe_heavy_kabsch_rmsd(yt, pos, atom_x)
2184
+ if torch.isfinite(rmsd_step):
2185
+ step_loss = step_loss + rmsd_step
2186
+ valid_steps += 1
2187
+ if valid_steps > 0:
2188
+ step_loss = step_loss / valid_steps
2189
+
2190
+ delta_loss = torch.norm(yT - y0.detach(), dim=-1).mean()
2191
+ lambda_step = float(getattr(self.args, "lambda_refine_step", 0.3))
2192
+ lambda_delta = float(getattr(self.args, "lambda_refine_delta", 0.01))
2193
+
2194
+ ce_manifold = torch.tensor(0.0, device=node_feat.device)
2195
+ if lambda_ce_manifold > 0:
2196
+ try:
2197
+ P_pred = prob_low_dim(yT, a, b)
2198
+ P_pred = torch.nan_to_num(P_pred, nan=0.5, posinf=1.0, neginf=0.0).clamp(1e-6, 1.0 - 1e-6)
2199
+ ce_manifold = cross_entropy_loss(P_pred, P_det)
2200
+ ce_manifold = torch.nan_to_num(ce_manifold, nan=0.0, posinf=10.0, neginf=0.0).clamp(0.0,
2201
+ 10.0)
2202
+ except Exception:
2203
+ ce_manifold = torch.tensor(0.0, device=node_feat.device)
2204
+
2205
+ loss = (lr * rmsdT + lambda_step * step_loss + lambda_delta * delta_loss
2206
+ + lambda_ce_manifold * ce_manifold)
2207
+ loss = torch.nan_to_num(loss, nan=10.0, posinf=10.0, neginf=0.0).clamp(0.0, 10.0)
2208
+ y_pred = yT
2209
+ pos_val = rmsdT
2210
+
2211
+ else: # geom / joint
2212
+ z = torch.zeros(self.mode_dim, device=P_det.device, dtype=P_det.dtype)
2213
+ y0 = self.conditioned_decoder(
2214
+ P_global=P_det, node_feat=feat_det, z=z,
2215
+ edge_index=local_ei, edge_attr=local_ea
2216
+ )
2217
+ yT, _ = self.refiner(
2218
+ P=P_det, node_feat=feat_det, y0=y0,
2219
+ edge_index=local_ei, edge_attr=local_ea
2220
+ )
2221
+ rmsd0 = safe_heavy_kabsch_rmsd(y0, pos, atom_x).clamp(0.0, 10.0)
2222
+ rmsdT = safe_heavy_kabsch_rmsd(yT, pos, atom_x).clamp(0.0, 10.0)
2223
+
2224
+ ce_manifold = torch.tensor(0.0, device=node_feat.device)
2225
+ if lambda_ce_manifold > 0:
2226
+ try:
2227
+ P_pred = prob_low_dim(yT, a, b)
2228
+ P_pred = torch.nan_to_num(P_pred, nan=0.5, posinf=1.0, neginf=0.0).clamp(1e-6, 1.0 - 1e-6)
2229
+ ce_manifold = cross_entropy_loss(P_pred, P_det)
2230
+ ce_manifold = torch.nan_to_num(ce_manifold, nan=0.0, posinf=10.0, neginf=0.0).clamp(0.0,
2231
+ 10.0)
2232
+ except Exception:
2233
+ ce_manifold = torch.tensor(0.0, device=node_feat.device)
2234
+
2235
+ geom = self._geometry_loss_one(yT, pos, edge_i, getattr(data, "torsion_index", None))
2236
+ loss = lc * rmsd0 + lr * rmsdT + geom + lambda_ce_manifold * ce_manifold
2237
+ loss = torch.nan_to_num(loss, nan=10.0, posinf=10.0, neginf=0.0).clamp(0.0, 10.0)
2238
+ y_pred = yT
2239
+ pos_val = rmsdT
2240
+
2241
+ total = total + loss
2242
+ pos_log = pos_log + pos_val.detach()
2243
+ y_pred_by_graph[gi] = y_pred
2244
+ valid += 1
2245
+
2246
+ if valid == 0:
2247
+ zero = node_feat.sum() * 0.0
2248
+ return zero, zero, y_pred_by_graph
2249
+ return (total / valid).clamp(0.0, 10.0), (pos_log / valid).clamp(0.0, 10.0), y_pred_by_graph
2250
+
2251
+ # --------------------------------------------------------
2252
+ # Forward
2253
+ # --------------------------------------------------------
2254
+ def forward(self, data, epoch: int = 0):
2255
+ train_stage = getattr(self.args, "train_stage", "joint")
2256
+ node_feat = self.gnn(data)
2257
+ graph_feat = self.pool(node_feat, data.batch)
2258
+ n_graphs = int(data.num_graphs)
2259
+ y_pred_by_graph: List[Optional[torch.Tensor]] = [None for _ in range(n_graphs)]
2260
+ zero = node_feat.sum() * 0.0
2261
+ stage1_loss, ab_reg = zero, zero
2262
+ stage_loss, pos_loss = zero, zero
2263
+
2264
+ if train_stage in ["gnn", "joint"]:
2265
+ stage1_loss, ab_reg = self.compute_stage1_loss(data, node_feat, graph_feat)
2266
+
2267
+ if train_stage == "coarse":
2268
+ stage_loss, pos_loss, y_pred_by_graph = self.compute_coarse_loss(data, node_feat, graph_feat)
2269
+ elif train_stage == "refine":
2270
+ stage_loss, pos_loss, y_pred_by_graph = self.compute_refine_loss(data, node_feat, graph_feat)
2271
+ elif train_stage == "geom":
2272
+ stage_loss, pos_loss, y_pred_by_graph = self.compute_geom_loss(data, node_feat, graph_feat)
2273
+ elif train_stage == "multimode":
2274
+ stage_loss, pos_loss, y_pred_by_graph = self.compute_multimode_loss(data, node_feat, graph_feat)
2275
+ elif train_stage == "decoder":
2276
+ stage_loss, pos_loss, y_pred_by_graph = self.compute_coarse_loss(data, node_feat, graph_feat)
2277
+ elif train_stage == "joint":
2278
+ stage_loss, pos_loss, y_pred_by_graph = self.compute_geom_loss(data, node_feat, graph_feat)
2279
+ elif train_stage == "gnn":
2280
+ pass
2281
+ else:
2282
+ raise ValueError(f"Unknown train_stage={train_stage}")
2283
+
2284
+ pred_pos = self._collect_pred_pos(data, y_pred_by_graph)
2285
+ lambda_global = float(getattr(self.args, "lambda_global", 1.0))
2286
+
2287
+ if train_stage == "gnn":
2288
+ mani_loss = lambda_global * stage1_loss + ab_reg
2289
+ total_loss = mani_loss
2290
+ pos_loss = zero
2291
+ elif train_stage in ["coarse", "refine", "geom", "multimode", "decoder"]:
2292
+ mani_loss = stage_loss
2293
+ total_loss = stage_loss
2294
+ else:
2295
+ mani_loss = lambda_global * stage1_loss + ab_reg + stage_loss
2296
+ total_loss = mani_loss
2297
+
2298
+ return pred_pos, graph_feat, pos_loss, mani_loss, total_loss, []
2299
+
2300
+ # --------------------------------------------------------
2301
+ # Inference
2302
+ # --------------------------------------------------------
2303
+ @torch.no_grad()
2304
+ def get_student_probability_map(self, data):
2305
+ node_feat = self.gnn(data)
2306
+ graph_feat = self.pool(node_feat, data.batch)
2307
+ out = []
2308
+ for i in range(int(data.num_graphs)):
2309
+ mask = data.batch == i
2310
+ feat_i = node_feat[mask]
2311
+ edge_i = self._get_local_edge_index(data.edge_index, mask)
2312
+ P, _ = self._predict_P_global(feat_i, edge_i)
2313
+ out.append(P.detach())
2314
+ return out
2315
+
2316
+ @torch.no_grad()
2317
+ def predict_conformer(self, data):
2318
+ return self.predict_conformers(data=data, num_modes=1)
2319
+
2320
+ @torch.no_grad()
2321
+ def predict_conformers(
2322
+ self,
2323
+ data=None,
2324
+ feat_i: Optional[torch.Tensor] = None,
2325
+ edge_index_i: Optional[torch.Tensor] = None,
2326
+ graph_feat_i: Optional[torch.Tensor] = None,
2327
+ num_modes: Optional[int] = None,
2328
+ deterministic: bool = False,
2329
+ ):
2330
+ was_training = self.training
2331
+ self.eval()
2332
+ outputs = []
2333
+ if num_modes is None:
2334
+ num_modes = int(getattr(self.args, "num_modes", self.num_modes))
2335
+
2336
+ if feat_i is not None:
2337
+ if feat_i.size(0) <= 1:
2338
+ return outputs
2339
+ P_global, pair_feat = self._predict_P_global(feat_i, edge_index_i)
2340
+ if graph_feat_i is None:
2341
+ graph_feat_i = feat_i.mean(dim=0)
2342
+ for k in range(num_modes):
2343
+ z = self._make_z(k, P_global.device, P_global.dtype, deterministic)
2344
+ y0, yT, traj = self.decode_with_z(P_global, feat_i, z, edge_index_i)
2345
+ outputs.append({
2346
+ "P": P_global.detach(),
2347
+ "Y0": y0.detach(),
2348
+ "Y": yT.detach(),
2349
+ "traj": [t.detach() for t in traj]
2350
+ })
2351
+ if was_training:
2352
+ self.train()
2353
+ return outputs
2354
+
2355
+ if data is None:
2356
+ raise ValueError("predict_conformers requires either data or feat_i.")
2357
+ node_feat = self.gnn(data)
2358
+ graph_feat = self.pool(node_feat, data.batch)
2359
+ for i in range(int(data.num_graphs)):
2360
+ mask = data.batch == i
2361
+ feat_g = node_feat[mask]
2362
+ if feat_g.size(0) <= 1:
2363
+ continue
2364
+ edge_g = self._get_local_edge_index(data.edge_index, mask)
2365
+ outs = self.predict_conformers(
2366
+ feat_i=feat_g,
2367
+ edge_index_i=edge_g,
2368
+ graph_feat_i=graph_feat[i],
2369
+ num_modes=num_modes,
2370
+ deterministic=deterministic,
2371
+ )
2372
+ outputs.extend(outs)
2373
+ if was_training:
2374
+ self.train()
2375
+ return outputs