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.
- args_parse.py +593 -0
- candidate_corrector.py +161 -0
- candidate_scorer.py +359 -0
- conformend/__init__.py +36 -0
- conformend/refiner.py +25 -0
- dataset/__init__.py +1 -0
- dataset/drugdataset.py +644 -0
- dataset/manifold.py +237 -0
- dataset/sharded_drugdataset.py +124 -0
- dataset/smiles2graph.py +217 -0
- dualmapref/__init__.py +24 -0
- dualmapref/refiner.py +16 -0
- dualmapref-0.2.8.dist-info/LICENSE +21 -0
- dualmapref-0.2.8.dist-info/METADATA +167 -0
- dualmapref-0.2.8.dist-info/RECORD +186 -0
- dualmapref-0.2.8.dist-info/WHEEL +5 -0
- dualmapref-0.2.8.dist-info/entry_points.txt +10 -0
- dualmapref-0.2.8.dist-info/top_level.txt +29 -0
- exputils.py +289 -0
- infer.py +23 -0
- infer_base_conformers.py +5589 -0
- infer_mean_dispersion_torsion.py +1117 -0
- infer_torsion.py +752 -0
- infer_torsional_denoiser.py +1021 -0
- manimol/__init__.py +30 -0
- manimol/__main__.py +6 -0
- manimol/cli.py +36 -0
- manimol/full_cli.py +41 -0
- manimol/legacy_imports.py +43 -0
- manimol/manifest.py +56 -0
- manimol/postprocess.py +148 -0
- manimol/priors.py +60 -0
- manimol/protocols.py +119 -0
- manimol/refiner.py +645 -0
- manimol/runtime.py +194 -0
- manimol/selection.py +85 -0
- manimol/torsion.py +22 -0
- manimol/train_cli.py +14 -0
- mixture_flow/src/__init__.py +1 -0
- mixture_flow/src/_path.py +7 -0
- mixture_flow/src/infer28_mixture_torsion_flow.py +1093 -0
- mixture_flow/src/mixture_torsion_flow.py +291 -0
- mixture_flow/src/summarize_mixture_debug.py +80 -0
- mixture_flow/src/train28_mixture_torsion_flow.py +583 -0
- mixture_torsion_flow.py +7 -0
- models/ manifold_learning.py +86 -0
- models/__init__.py +8 -0
- models/dist.py +273 -0
- models/dist2coords.py +23 -0
- models/egnn.py +107 -0
- models/experimental_backbones.py +364 -0
- models/gnnconv.py +512 -0
- models/kernel_inversion.py +141 -0
- models/kernels.py +427 -0
- models/losses.py +154 -0
- models/mean_dispersion_bridge.py +119 -0
- models/mean_dispersion_torsion_context.py +155 -0
- models/model.py +73 -0
- models/model0.py +2375 -0
- models/vis.py +128 -0
- pgraph_controls.py +107 -0
- scripts/__init__.py +2 -0
- scripts/analyze_basin_alignment.py +63 -0
- scripts/analyze_hif2a_conformer_aggregation.py +267 -0
- scripts/analyze_hif2a_contact_pharmacophore.py +1111 -0
- scripts/analyze_hif2a_fixed_budget.py +202 -0
- scripts/analyze_hif2a_pharmacophore.py +405 -0
- scripts/analyze_hif2a_pharmacophore_match_metric.py +386 -0
- scripts/analyze_p_control_candidate_flow.py +154 -0
- scripts/analyze_scaffold_basin.py +83 -0
- scripts/audit_dispersion_proposals.py +208 -0
- scripts/audit_etflow_protocol.py +258 -0
- scripts/audit_external_overlap.py +150 -0
- scripts/audit_oldbest_fulltrainval_split.py +82 -0
- scripts/audit_pdbbind_contact_pharmacophores.py +146 -0
- scripts/audit_qm9_geomol_test_split.py +94 -0
- scripts/audit_stage1_stage2_cross.py +131 -0
- scripts/build_geom_drugs_lowref_diagnostic.py +84 -0
- scripts/build_qm9_backfill_manifest.py +99 -0
- scripts/check_etflow_runtime.py +56 -0
- scripts/compare_prior_sources.py +122 -0
- scripts/compose_binding_mode_figure.py +87 -0
- scripts/compose_hif2a_contact_pharmacophore_suppfig.py +255 -0
- scripts/compose_hif2a_functional_geometry_pipeline.py +266 -0
- scripts/compose_manimol_internal_architecture.py +291 -0
- scripts/diagnose_flow_basin_intervention.py +54 -0
- scripts/diagnose_hif2a_pharmacophore_budget.py +185 -0
- scripts/diagnose_route2_p_ranking.py +123 -0
- scripts/discover_binding_mode_inputs.py +30 -0
- scripts/evaluate_functional_prior_stage1.py +230 -0
- scripts/evaluate_geomanimol.py +74 -0
- scripts/evaluate_geomanimol_flow.py +23 -0
- scripts/evaluate_geomanimol_multiscaffold.py +46 -0
- scripts/evaluate_geomanimol_residual.py +45 -0
- scripts/evaluate_geomanimol_torsion.py +40 -0
- scripts/evaluate_hif2a_bioactive_enrichment.py +91 -0
- scripts/evaluate_hif2a_pms_suite.py +440 -0
- scripts/evaluate_pdbbind_contact_pms.py +155 -0
- scripts/evaluate_platinum_external.py +114 -0
- scripts/evaluate_platinum_pool.py +82 -0
- scripts/evaluate_route2_calibrated_p.py +104 -0
- scripts/evaluate_route2_library_components.py +596 -0
- scripts/evaluate_route2_p_basin_budget.py +288 -0
- scripts/evaluate_stage1_checkpoint_only.py +30 -0
- scripts/export_dispersion_bond_audit.py +136 -0
- scripts/export_top20_shards.py +207 -0
- scripts/external_validation_metrics.py +152 -0
- scripts/fit_topology_prior.py +117 -0
- scripts/frozen_dispersion_audit.py +281 -0
- scripts/geomanimol_debug.py +50 -0
- scripts/geomanimol_smoke.py +37 -0
- scripts/infer_etflow_matched.py +524 -0
- scripts/inspect_checkpoint.py +12 -0
- scripts/inspect_conformer_capacity.py +71 -0
- scripts/inspect_pool.py +12 -0
- scripts/make_figure3_selection_loss.py +474 -0
- scripts/make_figure3_selection_summary.py +275 -0
- scripts/make_geom_debug_subset.py +62 -0
- scripts/make_low_reference_test_subset.py +70 -0
- scripts/merge_stage1_into_stage2.py +82 -0
- scripts/place_hif2a_candidates_for_evaluation.py +66 -0
- scripts/plot_hif2a_low_budget_efficiency.py +100 -0
- scripts/plot_prior_validation.py +48 -0
- scripts/precompute_stage2_assignments.py +90 -0
- scripts/prepare_bace_activity.py +98 -0
- scripts/prepare_bace_etkdg_benchmark.py +90 -0
- scripts/prepare_figure5_4x4_cases.py +470 -0
- scripts/prepare_geom_drugs_geomol_top20.py +472 -0
- scripts/prepare_pdbbind_core_raw.py +90 -0
- scripts/prepare_platinum_raw.py +74 -0
- scripts/prepare_pubchem_etkdg.py +54 -0
- scripts/rebuild_hif2a_final_sdf_and_metrics.py +55 -0
- scripts/rebuild_hif2a_fixed_pool_sweep.py +175 -0
- scripts/rebuild_hif2a_strict.py +220 -0
- scripts/recompute_hif2a_bootstrap.py +63 -0
- scripts/reference_free_geometry.py +112 -0
- scripts/render_binding_modes.py +413 -0
- scripts/render_figure5_4x4.py +330 -0
- scripts/render_figure6_pharmacophore_pymol.py +162 -0
- scripts/render_hif2a_contact_pharmacophore_pymol.py +414 -0
- scripts/render_hif2a_figure11.py +397 -0
- scripts/run_bace_2d_baseline.py +73 -0
- scripts/run_drugs_validation_matrix.py +225 -0
- scripts/run_external_reference_free.py +53 -0
- scripts/run_manimol_finalk_sensitivity.py +106 -0
- scripts/run_manimol_oversampling_sensitivity.py +109 -0
- scripts/run_pdisp_matched_intervention.py +116 -0
- scripts/run_stage1_stage2_cross20.py +83 -0
- scripts/score_hif2a_candidates_with_pgraph.py +221 -0
- scripts/summarize_dispersion_intervention.py +88 -0
- scripts/test_mean_dispersion_bridge.py +50 -0
- scripts/train_dispersion_head_diagnostic.py +320 -0
- scripts/train_dispersion_head_streaming.py +519 -0
- scripts/train_geomanimol.py +100 -0
- scripts/train_geomanimol_flow.py +26 -0
- scripts/train_geomanimol_residual.py +70 -0
- scripts/train_geomanimol_torsion.py +50 -0
- scripts/train_quantile_representation_diagnostic.py +204 -0
- scripts/validate_mean_dispersion_prior.py +173 -0
- scripts/validate_packed_shard.py +41 -0
- scripts/validate_stage1_prior.py +221 -0
- scripts/validate_top20_shards.py +36 -0
- scripts/validation/__init__.py +1 -0
- scripts/validation/hif2a_downstream_baselines.py +105 -0
- scripts/validation/hif2a_pairwise_ranker.py +218 -0
- scripts/verify_binding_mode_figure.py +47 -0
- torsion_manifold.py +281 -0
- train.py +78 -0
- train_graph_encoder.py +206 -0
- train_local_torsion_corrector.py +511 -0
- train_mean_dispersion_torsion_prior.py +329 -0
- train_probability_basin.py +235 -0
- train_torsion_generator.py +1631 -0
- train_torsion_manifold_prior.py +258 -0
- train_torsional_denoiser.py +415 -0
- trainer.py +1038 -0
- utils/__init__.py +7 -0
- utils/checkpoint.py +40 -0
- utils/device.py +17 -0
- utils/helpers.py +98 -0
- utils/kabsch.py +29 -0
- utils/lookup_table.py +164 -0
- utils/metrics.py +50 -0
- utils/optuna.py +111 -0
- utils/save_mol.py +366 -0
- utils/util.py +36 -0
|
@@ -0,0 +1,1631 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Experimental torsion-space Stage-2 training entrypoint.
|
|
3
|
+
|
|
4
|
+
This file is intentionally isolated from train.py. It keeps the current
|
|
5
|
+
Stage-1 probability encoder/checkpoint flow, but replaces the Stage-2 Cartesian
|
|
6
|
+
coordinate decoder objective with a torsion-delta objective:
|
|
7
|
+
|
|
8
|
+
base conformer + mode-conditioned torsion deltas -> generated conformers
|
|
9
|
+
|
|
10
|
+
The purpose is to test whether Manimol can escape Cartesian decoder collapse by
|
|
11
|
+
learning in internal-coordinate space. ETKDG is not mixed into a candidate pool
|
|
12
|
+
here; this entrypoint trains only the Manimol torsion head.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import logging
|
|
18
|
+
import math
|
|
19
|
+
import os
|
|
20
|
+
import sys
|
|
21
|
+
import warnings
|
|
22
|
+
from argparse import ArgumentParser
|
|
23
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
24
|
+
|
|
25
|
+
import torch
|
|
26
|
+
import torch.distributed as dist
|
|
27
|
+
import torch.nn as nn
|
|
28
|
+
import torch.nn.functional as F
|
|
29
|
+
from rdkit import RDLogger
|
|
30
|
+
from torch.optim.lr_scheduler import CosineAnnealingLR
|
|
31
|
+
|
|
32
|
+
try:
|
|
33
|
+
from torch.utils.tensorboard import SummaryWriter
|
|
34
|
+
except ModuleNotFoundError:
|
|
35
|
+
class SummaryWriter: # type: ignore[no-redef]
|
|
36
|
+
"""No-op fallback for inference environments without tensorboard."""
|
|
37
|
+
|
|
38
|
+
def __init__(self, *args, **kwargs):
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
def add_scalar(self, *args, **kwargs):
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
def add_histogram(self, *args, **kwargs):
|
|
45
|
+
pass
|
|
46
|
+
|
|
47
|
+
def flush(self):
|
|
48
|
+
pass
|
|
49
|
+
|
|
50
|
+
def close(self):
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
from args_parse import args_parser
|
|
54
|
+
from exputils import get_dump_path, initialize_exp, merge_args_from_paths, set_seed
|
|
55
|
+
from train_graph_encoder import fix_args
|
|
56
|
+
from trainer import Trainer
|
|
57
|
+
from utils.helpers import get_arg
|
|
58
|
+
from models.model0 import (
|
|
59
|
+
_torsion_rows,
|
|
60
|
+
apply_torsion_residuals,
|
|
61
|
+
bond_distance_match_loss,
|
|
62
|
+
cross_entropy_loss,
|
|
63
|
+
heavy_atom_mask_from_x,
|
|
64
|
+
pairwise_distance,
|
|
65
|
+
periodic_angle_delta,
|
|
66
|
+
prob_low_dim,
|
|
67
|
+
prob_low_dim_kernel,
|
|
68
|
+
safe_heavy_kabsch_rmsd,
|
|
69
|
+
signed_volume_supervision_loss,
|
|
70
|
+
torsion_angles,
|
|
71
|
+
torsion_rotation_masks,
|
|
72
|
+
)
|
|
73
|
+
from models.mean_dispersion_torsion_context import mean_dispersion_torsion_context
|
|
74
|
+
from utils.kabsch import kabsch_rmsd
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
78
|
+
RDLogger.DisableLog("rdApp.*")
|
|
79
|
+
|
|
80
|
+
logging.basicConfig(
|
|
81
|
+
level=logging.INFO,
|
|
82
|
+
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
|
83
|
+
)
|
|
84
|
+
logger = logging.getLogger(__name__)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class TorsionModeDecoder(nn.Module):
|
|
88
|
+
"""Predict mode-conditioned torsion deltas for rotatable torsion rows."""
|
|
89
|
+
|
|
90
|
+
def __init__(
|
|
91
|
+
self,
|
|
92
|
+
emb_dim: int = 128,
|
|
93
|
+
mode_dim: int = 32,
|
|
94
|
+
hidden_dim: int = 128,
|
|
95
|
+
max_delta: float = math.pi,
|
|
96
|
+
prototype_dim: int = 0,
|
|
97
|
+
torsion_context_dim: int = 0,
|
|
98
|
+
):
|
|
99
|
+
super().__init__()
|
|
100
|
+
self.max_delta = float(max_delta)
|
|
101
|
+
self.prototype_dim = max(0, int(prototype_dim))
|
|
102
|
+
self.context_dim = max(0, int(torsion_context_dim))
|
|
103
|
+
self.mlp = nn.Sequential(
|
|
104
|
+
# Keep the legacy input width/state-dict layout intact. The new
|
|
105
|
+
# mean-dispersion signal enters through a zero-initialized residual
|
|
106
|
+
# projection instead of perturbing the oldbest decoder at step 0.
|
|
107
|
+
nn.Linear(5 * emb_dim + mode_dim + self.prototype_dim + 3, hidden_dim),
|
|
108
|
+
nn.SiLU(),
|
|
109
|
+
nn.Linear(hidden_dim, hidden_dim),
|
|
110
|
+
nn.SiLU(),
|
|
111
|
+
nn.Linear(hidden_dim, 1),
|
|
112
|
+
)
|
|
113
|
+
self.context_proj = None
|
|
114
|
+
if self.context_dim > 0:
|
|
115
|
+
self.context_proj = nn.Linear(self.context_dim, hidden_dim)
|
|
116
|
+
nn.init.zeros_(self.context_proj.weight)
|
|
117
|
+
nn.init.zeros_(self.context_proj.bias)
|
|
118
|
+
|
|
119
|
+
def forward(
|
|
120
|
+
self,
|
|
121
|
+
node_feat: torch.Tensor,
|
|
122
|
+
graph_feat: torch.Tensor,
|
|
123
|
+
coords: torch.Tensor,
|
|
124
|
+
P: torch.Tensor,
|
|
125
|
+
torsion_index: Optional[torch.Tensor],
|
|
126
|
+
z: torch.Tensor,
|
|
127
|
+
prototype: Optional[torch.Tensor],
|
|
128
|
+
max_torsions: int,
|
|
129
|
+
torsion_context: Optional[torch.Tensor] = None,
|
|
130
|
+
) -> torch.Tensor:
|
|
131
|
+
rows = _torsion_rows(torsion_index)
|
|
132
|
+
if rows is None:
|
|
133
|
+
return coords.new_zeros((0,))
|
|
134
|
+
rows = rows[: min(int(max_torsions), rows.size(0))].to(coords.device).long()
|
|
135
|
+
valid = (rows >= 0).all(dim=1) & (rows < coords.size(0)).all(dim=1)
|
|
136
|
+
rows = rows[valid]
|
|
137
|
+
if rows.numel() == 0:
|
|
138
|
+
return coords.new_zeros((0,))
|
|
139
|
+
|
|
140
|
+
current = torsion_angles(coords, rows)
|
|
141
|
+
graph_context = graph_feat.to(device=coords.device, dtype=node_feat.dtype)
|
|
142
|
+
if graph_context.dim() != 1:
|
|
143
|
+
graph_context = graph_context.view(-1)
|
|
144
|
+
z = z.to(device=coords.device, dtype=node_feat.dtype).view(-1)
|
|
145
|
+
if self.prototype_dim > 0:
|
|
146
|
+
if prototype is None:
|
|
147
|
+
proto = torch.zeros(self.prototype_dim, device=coords.device, dtype=node_feat.dtype)
|
|
148
|
+
else:
|
|
149
|
+
proto = prototype.to(device=coords.device, dtype=node_feat.dtype).view(-1)
|
|
150
|
+
if proto.numel() < self.prototype_dim:
|
|
151
|
+
proto = F.pad(proto, (0, self.prototype_dim - proto.numel()))
|
|
152
|
+
elif proto.numel() > self.prototype_dim:
|
|
153
|
+
proto = proto[: self.prototype_dim]
|
|
154
|
+
else:
|
|
155
|
+
proto = coords.new_zeros((0,), dtype=node_feat.dtype)
|
|
156
|
+
|
|
157
|
+
if self.context_dim > 0:
|
|
158
|
+
if torsion_context is None:
|
|
159
|
+
context = coords.new_zeros((rows.size(0), self.context_dim), dtype=node_feat.dtype)
|
|
160
|
+
else:
|
|
161
|
+
context = torsion_context.to(device=coords.device, dtype=node_feat.dtype)
|
|
162
|
+
context = context[: rows.size(0)]
|
|
163
|
+
if context.size(-1) < self.context_dim:
|
|
164
|
+
context = F.pad(context, (0, self.context_dim - context.size(-1)))
|
|
165
|
+
elif context.size(-1) > self.context_dim:
|
|
166
|
+
context = context[:, : self.context_dim]
|
|
167
|
+
else:
|
|
168
|
+
context = coords.new_zeros((rows.size(0), 0), dtype=node_feat.dtype)
|
|
169
|
+
|
|
170
|
+
feats = []
|
|
171
|
+
for idx, row in enumerate(rows):
|
|
172
|
+
i, j, k, l = row
|
|
173
|
+
feats.append(
|
|
174
|
+
torch.cat(
|
|
175
|
+
[
|
|
176
|
+
node_feat[i],
|
|
177
|
+
node_feat[j],
|
|
178
|
+
node_feat[k],
|
|
179
|
+
node_feat[l],
|
|
180
|
+
graph_context,
|
|
181
|
+
z,
|
|
182
|
+
proto,
|
|
183
|
+
torch.sin(current[idx]).view(1),
|
|
184
|
+
torch.cos(current[idx]).view(1),
|
|
185
|
+
P[j, k].view(1),
|
|
186
|
+
],
|
|
187
|
+
dim=0,
|
|
188
|
+
)
|
|
189
|
+
)
|
|
190
|
+
x = torch.stack(feats, dim=0)
|
|
191
|
+
hidden = self.mlp[0](x)
|
|
192
|
+
if self.context_proj is not None:
|
|
193
|
+
hidden = hidden + self.context_proj(context)
|
|
194
|
+
hidden = self.mlp[1](hidden)
|
|
195
|
+
hidden = self.mlp[2](hidden)
|
|
196
|
+
hidden = self.mlp[3](hidden)
|
|
197
|
+
delta = torch.tanh(self.mlp[4](hidden).view(-1)) * self.max_delta
|
|
198
|
+
return torch.nan_to_num(delta, nan=0.0, posinf=0.0, neginf=0.0)
|
|
199
|
+
|
|
200
|
+
def forward_modes(
|
|
201
|
+
self,
|
|
202
|
+
node_feat: torch.Tensor,
|
|
203
|
+
graph_feat: torch.Tensor,
|
|
204
|
+
coords: torch.Tensor,
|
|
205
|
+
P: torch.Tensor,
|
|
206
|
+
torsion_index: Optional[torch.Tensor],
|
|
207
|
+
z: torch.Tensor,
|
|
208
|
+
prototype: Optional[torch.Tensor],
|
|
209
|
+
max_torsions: int,
|
|
210
|
+
torsion_context: Optional[torch.Tensor] = None,
|
|
211
|
+
) -> torch.Tensor:
|
|
212
|
+
"""Evaluate every mode in one MLP call without changing its features."""
|
|
213
|
+
rows = _torsion_rows(torsion_index)
|
|
214
|
+
if rows is None:
|
|
215
|
+
return coords.new_zeros((z.size(0), 0))
|
|
216
|
+
rows = rows[: min(int(max_torsions), rows.size(0))].to(coords.device).long()
|
|
217
|
+
num_atoms = coords.size(-2)
|
|
218
|
+
valid = (rows >= 0).all(dim=1) & (rows < num_atoms).all(dim=1)
|
|
219
|
+
rows = rows[valid]
|
|
220
|
+
if rows.numel() == 0:
|
|
221
|
+
return coords.new_zeros((z.size(0), 0))
|
|
222
|
+
num_modes, num_torsions = z.size(0), rows.size(0)
|
|
223
|
+
mode_coords = coords.unsqueeze(0).expand(num_modes, -1, -1) if coords.dim() == 2 else coords
|
|
224
|
+
current = torsion_angles_modes(mode_coords, rows)
|
|
225
|
+
atom_context = torch.cat(
|
|
226
|
+
[node_feat[rows[:, column]] for column in range(4)], dim=-1
|
|
227
|
+
)
|
|
228
|
+
graph_context = graph_feat.reshape(1, -1).expand(num_torsions, -1)
|
|
229
|
+
static = torch.cat([atom_context, graph_context], dim=-1)
|
|
230
|
+
static = static.unsqueeze(0).expand(num_modes, -1, -1)
|
|
231
|
+
z_context = z[:, None, :].expand(-1, num_torsions, -1)
|
|
232
|
+
if self.prototype_dim > 0:
|
|
233
|
+
if prototype is None:
|
|
234
|
+
proto_context = static.new_zeros((num_modes, num_torsions, self.prototype_dim))
|
|
235
|
+
else:
|
|
236
|
+
proto_context = prototype[:, None, :].expand(-1, num_torsions, -1)
|
|
237
|
+
else:
|
|
238
|
+
proto_context = static.new_zeros((num_modes, num_torsions, 0))
|
|
239
|
+
pair_context = P[rows[:, 1], rows[:, 2]].reshape(1, num_torsions, 1)
|
|
240
|
+
pair_context = pair_context.expand(num_modes, -1, -1)
|
|
241
|
+
if self.context_dim > 0:
|
|
242
|
+
if torsion_context is None:
|
|
243
|
+
context = static.new_zeros((num_torsions, self.context_dim))
|
|
244
|
+
else:
|
|
245
|
+
context = torsion_context.to(device=coords.device, dtype=static.dtype)[:num_torsions]
|
|
246
|
+
if context.size(-1) < self.context_dim:
|
|
247
|
+
context = F.pad(context, (0, self.context_dim - context.size(-1)))
|
|
248
|
+
elif context.size(-1) > self.context_dim:
|
|
249
|
+
context = context[:, : self.context_dim]
|
|
250
|
+
context = context.unsqueeze(0).expand(num_modes, -1, -1)
|
|
251
|
+
else:
|
|
252
|
+
context = static.new_zeros((num_modes, num_torsions, 0))
|
|
253
|
+
features = torch.cat(
|
|
254
|
+
[
|
|
255
|
+
static,
|
|
256
|
+
z_context,
|
|
257
|
+
proto_context,
|
|
258
|
+
torch.sin(current).unsqueeze(-1),
|
|
259
|
+
torch.cos(current).unsqueeze(-1),
|
|
260
|
+
pair_context,
|
|
261
|
+
],
|
|
262
|
+
dim=-1,
|
|
263
|
+
)
|
|
264
|
+
flat_features = features.reshape(-1, features.size(-1))
|
|
265
|
+
hidden = self.mlp[0](flat_features)
|
|
266
|
+
if self.context_proj is not None:
|
|
267
|
+
hidden = hidden + self.context_proj(context.reshape(-1, self.context_dim))
|
|
268
|
+
hidden = self.mlp[1](hidden)
|
|
269
|
+
hidden = self.mlp[2](hidden)
|
|
270
|
+
hidden = self.mlp[3](hidden)
|
|
271
|
+
delta = torch.tanh(self.mlp[4](hidden))
|
|
272
|
+
delta = delta.reshape(num_modes, num_torsions) * self.max_delta
|
|
273
|
+
return torch.nan_to_num(delta, nan=0.0, posinf=0.0, neginf=0.0)
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def torsion_angles_modes(coords: torch.Tensor, torsions: torch.Tensor, eps: float = 1e-8) -> torch.Tensor:
|
|
277
|
+
"""Vectorized torsion angles for coordinates shaped ``(M, N, 3)``."""
|
|
278
|
+
p0 = coords[:, torsions[:, 0]]
|
|
279
|
+
p1 = coords[:, torsions[:, 1]]
|
|
280
|
+
p2 = coords[:, torsions[:, 2]]
|
|
281
|
+
p3 = coords[:, torsions[:, 3]]
|
|
282
|
+
b1, b2, b3 = p1 - p0, p2 - p1, p3 - p2
|
|
283
|
+
n1 = torch.cross(b1, b2, dim=-1)
|
|
284
|
+
n2 = torch.cross(b2, b3, dim=-1)
|
|
285
|
+
n1 = n1 / torch.norm(n1, dim=-1, keepdim=True).clamp_min(eps)
|
|
286
|
+
n2 = n2 / torch.norm(n2, dim=-1, keepdim=True).clamp_min(eps)
|
|
287
|
+
b2 = b2 / torch.norm(b2, dim=-1, keepdim=True).clamp_min(eps)
|
|
288
|
+
m1 = torch.cross(n1, b2, dim=-1)
|
|
289
|
+
x = (n1 * n2).sum(dim=-1).clamp(-1.0 + eps, 1.0 - eps)
|
|
290
|
+
y = (m1 * n2).sum(dim=-1).clamp(-1.0 + eps, 1.0 - eps)
|
|
291
|
+
return torch.atan2(y, x)
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def apply_torsion_residuals_modes(
|
|
295
|
+
coords: torch.Tensor,
|
|
296
|
+
torsions: Optional[torch.Tensor],
|
|
297
|
+
deltas: torch.Tensor,
|
|
298
|
+
rotation_masks: List[torch.Tensor],
|
|
299
|
+
max_torsions: int,
|
|
300
|
+
) -> torch.Tensor:
|
|
301
|
+
"""Apply sequential torsion updates to all modes in parallel."""
|
|
302
|
+
rows = _torsion_rows(torsions)
|
|
303
|
+
if rows is None:
|
|
304
|
+
return coords
|
|
305
|
+
rows = rows[: min(int(max_torsions), rows.size(0), deltas.size(1))].to(coords.device).long()
|
|
306
|
+
valid = (rows >= 0).all(dim=1) & (rows < coords.size(1)).all(dim=1)
|
|
307
|
+
rows = rows[valid]
|
|
308
|
+
if len(rotation_masks) != rows.size(0):
|
|
309
|
+
raise ValueError("rotation_masks must match valid torsion rows")
|
|
310
|
+
out = coords
|
|
311
|
+
# Copy the small bond-index table once. Calling .cpu() for every torsion
|
|
312
|
+
# introduces a CUDA synchronization inside this sequential geometry loop.
|
|
313
|
+
bond_atoms = rows[:, 1:3].detach().cpu().tolist()
|
|
314
|
+
for index, (j, k) in enumerate(bond_atoms):
|
|
315
|
+
j, k = int(j), int(k)
|
|
316
|
+
side = rotation_masks[index].to(out.device)
|
|
317
|
+
p0, p1 = out[:, j], out[:, k]
|
|
318
|
+
axis = p1 - p0
|
|
319
|
+
axis = axis / torch.norm(axis, dim=-1, keepdim=True).clamp_min(1e-8)
|
|
320
|
+
points = out[:, side]
|
|
321
|
+
relative = points - p0[:, None]
|
|
322
|
+
angle = deltas[:, index]
|
|
323
|
+
cosine = torch.cos(angle)[:, None, None]
|
|
324
|
+
sine = torch.sin(angle)[:, None, None]
|
|
325
|
+
axis_expanded = axis[:, None].expand_as(relative)
|
|
326
|
+
cross = torch.cross(axis_expanded, relative, dim=-1)
|
|
327
|
+
dot = (relative * axis[:, None]).sum(dim=-1, keepdim=True)
|
|
328
|
+
rotated = (
|
|
329
|
+
p0[:, None]
|
|
330
|
+
+ relative * cosine
|
|
331
|
+
+ cross * sine
|
|
332
|
+
+ axis[:, None] * dot * (1.0 - cosine)
|
|
333
|
+
)
|
|
334
|
+
updated = out.clone()
|
|
335
|
+
updated[:, side] = rotated
|
|
336
|
+
out = updated
|
|
337
|
+
return torch.nan_to_num(out, nan=0.0, posinf=0.0, neginf=0.0)
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def pair_distance_loss(pred: torch.Tensor, target: torch.Tensor, atom_x: Optional[torch.Tensor]) -> torch.Tensor:
|
|
341
|
+
"""Heavy-atom pair-distance loss for local geometry tightness."""
|
|
342
|
+
if pred.shape != target.shape or pred.size(0) <= 1:
|
|
343
|
+
return pred.sum() * 0.0
|
|
344
|
+
if atom_x is not None:
|
|
345
|
+
mask = heavy_atom_mask_from_x(atom_x).to(pred.device)
|
|
346
|
+
if int(mask.sum().item()) >= 2:
|
|
347
|
+
pred = pred[mask]
|
|
348
|
+
target = target[mask]
|
|
349
|
+
dp = pairwise_distance(pred)
|
|
350
|
+
dt = pairwise_distance(target).detach()
|
|
351
|
+
n = dp.size(0)
|
|
352
|
+
if n <= 1:
|
|
353
|
+
return pred.sum() * 0.0
|
|
354
|
+
eye = torch.eye(n, dtype=torch.bool, device=dp.device)
|
|
355
|
+
return torch.abs(dp[~eye] - dt[~eye]).mean().clamp(0.0, 10.0)
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def pairwise_heavy_kabsch_rmsd_matrix(
|
|
359
|
+
predictions: torch.Tensor,
|
|
360
|
+
references: torch.Tensor,
|
|
361
|
+
atom_x: Optional[torch.Tensor],
|
|
362
|
+
eps: float = 1e-8,
|
|
363
|
+
) -> torch.Tensor:
|
|
364
|
+
"""Vectorized equivalent of safe_heavy_kabsch_rmsd for two conformer sets.
|
|
365
|
+
|
|
366
|
+
Returns a ``(num_predictions, num_references)`` matrix. The normalization,
|
|
367
|
+
reflection correction and coordinate-wise RMSD match the scalar helper used
|
|
368
|
+
by the legacy Stage-II loss, but all small 3x3 SVDs are submitted together.
|
|
369
|
+
"""
|
|
370
|
+
if predictions.dim() != 3 or references.dim() != 3:
|
|
371
|
+
raise ValueError("Expected conformer tensors with shape (K, N, 3).")
|
|
372
|
+
if predictions.shape[1:] != references.shape[1:] or predictions.size(1) <= 1:
|
|
373
|
+
return predictions.new_zeros((predictions.size(0), references.size(0)))
|
|
374
|
+
mask = heavy_atom_mask_from_x(atom_x)
|
|
375
|
+
if mask is not None and mask.size(0) == predictions.size(1):
|
|
376
|
+
mask = mask.to(predictions.device)
|
|
377
|
+
predictions = predictions[:, mask]
|
|
378
|
+
references = references[:, mask]
|
|
379
|
+
|
|
380
|
+
def normalize(coords: torch.Tensor) -> torch.Tensor:
|
|
381
|
+
coords = torch.nan_to_num(coords, nan=0.0, posinf=0.0, neginf=0.0)
|
|
382
|
+
coords = coords - coords.mean(dim=1, keepdim=True)
|
|
383
|
+
rms = torch.sqrt((coords * coords).mean(dim=(1, 2), keepdim=True) + eps).clamp_min(eps)
|
|
384
|
+
return torch.nan_to_num(coords / rms, nan=0.0, posinf=0.0, neginf=0.0)
|
|
385
|
+
|
|
386
|
+
pred = normalize(predictions)
|
|
387
|
+
ref = normalize(references)
|
|
388
|
+
pred_pair = pred[:, None].expand(-1, ref.size(0), -1, -1)
|
|
389
|
+
ref_pair = ref[None, :].expand(pred.size(0), -1, -1, -1)
|
|
390
|
+
covariance = pred_pair.transpose(-2, -1) @ ref_pair
|
|
391
|
+
u, _, vh = torch.linalg.svd(covariance)
|
|
392
|
+
v = vh.transpose(-2, -1)
|
|
393
|
+
rotation = v @ u
|
|
394
|
+
determinant = torch.linalg.det(rotation)
|
|
395
|
+
correction = torch.ones(
|
|
396
|
+
(*determinant.shape, 3), device=pred.device, dtype=pred.dtype
|
|
397
|
+
)
|
|
398
|
+
correction[..., -1] = torch.where(determinant < 0, -1.0, 1.0)
|
|
399
|
+
v = v * correction.unsqueeze(-2)
|
|
400
|
+
rotation = v @ u
|
|
401
|
+
aligned = pred_pair @ rotation
|
|
402
|
+
result = torch.sqrt(((aligned - ref_pair) ** 2).mean(dim=(-2, -1)) + eps)
|
|
403
|
+
return torch.nan_to_num(result, nan=10.0, posinf=10.0, neginf=0.0)
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
def pair_distance_loss_matrix(
|
|
407
|
+
predictions: torch.Tensor,
|
|
408
|
+
references: torch.Tensor,
|
|
409
|
+
atom_x: Optional[torch.Tensor],
|
|
410
|
+
) -> torch.Tensor:
|
|
411
|
+
"""Vectorized heavy-atom pair-distance loss for mode/reference sets."""
|
|
412
|
+
if predictions.dim() != 3 or references.dim() != 3:
|
|
413
|
+
raise ValueError("Expected conformer tensors with shape (K, N, 3).")
|
|
414
|
+
mask = heavy_atom_mask_from_x(atom_x)
|
|
415
|
+
if mask is not None and mask.size(0) == predictions.size(1):
|
|
416
|
+
mask = mask.to(predictions.device)
|
|
417
|
+
predictions = predictions[:, mask]
|
|
418
|
+
references = references[:, mask]
|
|
419
|
+
n_atoms = predictions.size(1)
|
|
420
|
+
if n_atoms <= 1:
|
|
421
|
+
return predictions.new_zeros((predictions.size(0), references.size(0)))
|
|
422
|
+
pred_dist = torch.cdist(predictions, predictions)
|
|
423
|
+
ref_dist = torch.cdist(references, references).detach()
|
|
424
|
+
off_diag = ~torch.eye(n_atoms, dtype=torch.bool, device=predictions.device)
|
|
425
|
+
delta = torch.abs(pred_dist[:, None] - ref_dist[None, :])
|
|
426
|
+
return delta[..., off_diag].mean(dim=-1).clamp(0.0, 10.0)
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
def local_pair_distance_loss(
|
|
430
|
+
pred: torch.Tensor,
|
|
431
|
+
target: torch.Tensor,
|
|
432
|
+
atom_x: Optional[torch.Tensor],
|
|
433
|
+
cutoff: float = 4.0,
|
|
434
|
+
) -> torch.Tensor:
|
|
435
|
+
"""Near-neighbor heavy-atom distance loss for tight-threshold refinement."""
|
|
436
|
+
if pred.shape != target.shape or pred.size(0) <= 1:
|
|
437
|
+
return pred.sum() * 0.0
|
|
438
|
+
if atom_x is not None:
|
|
439
|
+
mask = heavy_atom_mask_from_x(atom_x).to(pred.device)
|
|
440
|
+
if int(mask.sum().item()) >= 2:
|
|
441
|
+
pred = pred[mask]
|
|
442
|
+
target = target[mask]
|
|
443
|
+
n = pred.size(0)
|
|
444
|
+
if n <= 1:
|
|
445
|
+
return pred.sum() * 0.0
|
|
446
|
+
dp = pairwise_distance(pred)
|
|
447
|
+
dt = pairwise_distance(target).detach()
|
|
448
|
+
eye = torch.eye(n, dtype=torch.bool, device=dp.device)
|
|
449
|
+
local = (dt < float(cutoff)) & (~eye)
|
|
450
|
+
if int(local.sum().item()) == 0:
|
|
451
|
+
return pred.sum() * 0.0
|
|
452
|
+
return torch.abs(dp[local] - dt[local]).mean().clamp(0.0, 10.0)
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
def heavy_clash_loss(
|
|
456
|
+
pred: torch.Tensor,
|
|
457
|
+
atom_x: Optional[torch.Tensor],
|
|
458
|
+
min_distance: float = 1.1,
|
|
459
|
+
) -> torch.Tensor:
|
|
460
|
+
"""Soft heavy-atom clash penalty; keeps generated local geometry from collapsing."""
|
|
461
|
+
if pred.size(0) <= 1:
|
|
462
|
+
return pred.sum() * 0.0
|
|
463
|
+
if atom_x is not None:
|
|
464
|
+
mask = heavy_atom_mask_from_x(atom_x).to(pred.device)
|
|
465
|
+
if int(mask.sum().item()) >= 2:
|
|
466
|
+
pred = pred[mask]
|
|
467
|
+
n = pred.size(0)
|
|
468
|
+
if n <= 1:
|
|
469
|
+
return pred.sum() * 0.0
|
|
470
|
+
dist = pairwise_distance(pred)
|
|
471
|
+
eye = torch.eye(n, dtype=torch.bool, device=dist.device)
|
|
472
|
+
valid = ~eye
|
|
473
|
+
margin = pred.new_tensor(float(min_distance))
|
|
474
|
+
penalty = torch.relu(margin - dist[valid])
|
|
475
|
+
if penalty.numel() == 0:
|
|
476
|
+
return pred.sum() * 0.0
|
|
477
|
+
return (penalty * penalty).mean().clamp(0.0, 10.0)
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
def angle_distance_match_loss(pred: torch.Tensor, target: torch.Tensor, edge_index: Optional[torch.Tensor]) -> torch.Tensor:
|
|
481
|
+
"""Match angles induced by bonded triples i-j-k against the reference conformer."""
|
|
482
|
+
if edge_index is None or edge_index.numel() == 0 or pred.shape != target.shape:
|
|
483
|
+
return pred.sum() * 0.0
|
|
484
|
+
src, dst = edge_index
|
|
485
|
+
neighbors = {}
|
|
486
|
+
for s, d in zip(src.detach().cpu().tolist(), dst.detach().cpu().tolist()):
|
|
487
|
+
s, d = int(s), int(d)
|
|
488
|
+
if 0 <= s < pred.size(0) and 0 <= d < pred.size(0) and s != d:
|
|
489
|
+
neighbors.setdefault(s, [])
|
|
490
|
+
if d not in neighbors[s]:
|
|
491
|
+
neighbors[s].append(d)
|
|
492
|
+
losses = []
|
|
493
|
+
for center in sorted(neighbors.keys()):
|
|
494
|
+
neigh = sorted(neighbors[center])
|
|
495
|
+
if len(neigh) < 2:
|
|
496
|
+
continue
|
|
497
|
+
for a_idx in range(len(neigh)):
|
|
498
|
+
for b_idx in range(a_idx + 1, len(neigh)):
|
|
499
|
+
i, j, k = int(neigh[a_idx]), int(center), int(neigh[b_idx])
|
|
500
|
+
vp1 = pred[i] - pred[j]
|
|
501
|
+
vp2 = pred[k] - pred[j]
|
|
502
|
+
vt1 = target[i] - target[j]
|
|
503
|
+
vt2 = target[k] - target[j]
|
|
504
|
+
cp = torch.sum(vp1 * vp2) / (torch.norm(vp1) * torch.norm(vp2) + 1e-8)
|
|
505
|
+
ct = torch.sum(vt1 * vt2) / (torch.norm(vt1) * torch.norm(vt2) + 1e-8)
|
|
506
|
+
losses.append(F.smooth_l1_loss(cp.clamp(-1.0, 1.0), ct.detach().clamp(-1.0, 1.0)))
|
|
507
|
+
if len(losses) >= 128:
|
|
508
|
+
break
|
|
509
|
+
if len(losses) >= 128:
|
|
510
|
+
break
|
|
511
|
+
if len(losses) >= 128:
|
|
512
|
+
break
|
|
513
|
+
if not losses:
|
|
514
|
+
return pred.sum() * 0.0
|
|
515
|
+
return torch.stack(losses).mean().clamp(0.0, 10.0)
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
def parse_torsion_args():
|
|
519
|
+
parser = ArgumentParser(add_help=False)
|
|
520
|
+
parser.add_argument("--torsion_mode_max", type=int, default=64)
|
|
521
|
+
parser.add_argument("--torsion_delta_scale", type=float, default=3.141592653589793)
|
|
522
|
+
parser.add_argument("--lambda_torsion_delta_direct", type=float, default=0.2)
|
|
523
|
+
parser.add_argument("--lambda_torsion_mode_diversity", type=float, default=0.02)
|
|
524
|
+
parser.add_argument("--torsion_mode_diversity_margin", type=float, default=0.35)
|
|
525
|
+
parser.add_argument("--iterative_torsion_steps", type=int, default=0)
|
|
526
|
+
parser.add_argument("--iterative_torsion_delta_scale", type=float, default=0.8)
|
|
527
|
+
parser.add_argument("--lambda_tight_rmsd", type=float, default=0.0)
|
|
528
|
+
parser.add_argument("--lambda_tight_square", type=float, default=0.0)
|
|
529
|
+
parser.add_argument("--tight_rmsd_threshold", type=float, default=0.75)
|
|
530
|
+
parser.add_argument("--lambda_pair_distance", type=float, default=0.0)
|
|
531
|
+
parser.add_argument("--lambda_local_pair_distance", type=float, default=0.0)
|
|
532
|
+
parser.add_argument("--local_pair_cutoff", type=float, default=4.0)
|
|
533
|
+
parser.add_argument("--lambda_clash", type=float, default=0.0)
|
|
534
|
+
parser.add_argument("--clash_min_distance", type=float, default=1.1)
|
|
535
|
+
parser.add_argument("--train2_base_noise_prob", type=float, default=0.0)
|
|
536
|
+
parser.add_argument("--train2_base_noise_scale", type=float, default=0.0)
|
|
537
|
+
parser.add_argument("--lambda_bond_match", type=float, default=0.0)
|
|
538
|
+
parser.add_argument("--lambda_angle_match", type=float, default=0.0)
|
|
539
|
+
parser.add_argument("--lambda_chiral_volume_torsion", type=float, default=0.0)
|
|
540
|
+
parser.add_argument("--chiral_volume_max_centers", type=int, default=64)
|
|
541
|
+
parser.add_argument("--chiral_volume_ref_abs_min", type=float, default=0.05)
|
|
542
|
+
parser.add_argument(
|
|
543
|
+
"--mode_condition_strategy",
|
|
544
|
+
type=str,
|
|
545
|
+
default="z",
|
|
546
|
+
choices=["z", "learned_prototype", "z_plus_prototype"],
|
|
547
|
+
help=(
|
|
548
|
+
"Stage 2 decoder condition. z keeps the old mode code; learned_prototype "
|
|
549
|
+
"uses a trainable basin embedding per mode; z_plus_prototype concatenates both."
|
|
550
|
+
),
|
|
551
|
+
)
|
|
552
|
+
parser.add_argument(
|
|
553
|
+
"--mode_prototype_dim",
|
|
554
|
+
type=int,
|
|
555
|
+
default=32,
|
|
556
|
+
help="Dimension of the trainable mode/basin prototype embedding when enabled.",
|
|
557
|
+
)
|
|
558
|
+
parser.add_argument(
|
|
559
|
+
"--train2_resume_torsion_checkpoint",
|
|
560
|
+
type=str,
|
|
561
|
+
default="",
|
|
562
|
+
help=(
|
|
563
|
+
"Optional train_torsion_generator checkpoint used to restore torsion_mode_decoder / "
|
|
564
|
+
"torsion_refine_decoder after these modules are created. If empty, "
|
|
565
|
+
"checkpoint_path is also inspected for train_torsion_generator torsion modules."
|
|
566
|
+
),
|
|
567
|
+
)
|
|
568
|
+
parser.add_argument(
|
|
569
|
+
"--train2_freeze_torsion_mode_decoder",
|
|
570
|
+
action="store_true",
|
|
571
|
+
help="Freeze the coarse torsion decoder and train only local refinement modules.",
|
|
572
|
+
)
|
|
573
|
+
parser.add_argument(
|
|
574
|
+
"--disable_stage1_pglobal",
|
|
575
|
+
action="store_true",
|
|
576
|
+
help="Stage-2 ablation: do not use Stage-1 P_global; feed a zero probability map and skip manifold CE.",
|
|
577
|
+
)
|
|
578
|
+
parser.add_argument(
|
|
579
|
+
"--use_mean_dispersion_torsion_context",
|
|
580
|
+
action="store_true",
|
|
581
|
+
help=(
|
|
582
|
+
"Condition the oldbest torsion residual decoder on masked mean/dispersion "
|
|
583
|
+
"summaries extracted from Stage-I pairwise maps."
|
|
584
|
+
),
|
|
585
|
+
)
|
|
586
|
+
parser.add_argument(
|
|
587
|
+
"--train2_unfreeze_gnn",
|
|
588
|
+
action="store_true",
|
|
589
|
+
help="Stage-2 ablation: train the GNN encoder from scratch together with the torsion decoder.",
|
|
590
|
+
)
|
|
591
|
+
parser.add_argument(
|
|
592
|
+
"--mode_assignment_strategy",
|
|
593
|
+
type=str,
|
|
594
|
+
default="min",
|
|
595
|
+
choices=[
|
|
596
|
+
"min",
|
|
597
|
+
"conf_id",
|
|
598
|
+
"cluster",
|
|
599
|
+
"cluster_window",
|
|
600
|
+
"torsion_cluster",
|
|
601
|
+
"torsion_cluster_window",
|
|
602
|
+
"stage1_p_basin",
|
|
603
|
+
"stage1_p_basin_window",
|
|
604
|
+
],
|
|
605
|
+
help="How references supervise modes in torsion Stage 2.",
|
|
606
|
+
)
|
|
607
|
+
parser.add_argument(
|
|
608
|
+
"--mode_assignment_window",
|
|
609
|
+
type=int,
|
|
610
|
+
default=0,
|
|
611
|
+
help="Modes per reference cluster for cluster_window; 0 means auto ceil(num_modes / clusters).",
|
|
612
|
+
)
|
|
613
|
+
parser.add_argument(
|
|
614
|
+
"--vectorized_stage2_loss",
|
|
615
|
+
action="store_true",
|
|
616
|
+
help=(
|
|
617
|
+
"Batch the mathematically identical Kabsch, reference clustering, and "
|
|
618
|
+
"pair-distance calculations on GPU. Disabled by default so legacy runs "
|
|
619
|
+
"remain byte-for-byte configured as before."
|
|
620
|
+
),
|
|
621
|
+
)
|
|
622
|
+
parser.add_argument("--train2_use_refiner", action="store_true")
|
|
623
|
+
parser.add_argument(
|
|
624
|
+
"--train2_base_strategy",
|
|
625
|
+
type=str,
|
|
626
|
+
default="first",
|
|
627
|
+
choices=["first", "conf0"],
|
|
628
|
+
help="Base conformer used for torsion application inside each molecule group.",
|
|
629
|
+
)
|
|
630
|
+
exp_args, remaining = parser.parse_known_args()
|
|
631
|
+
return exp_args, remaining
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
def parse_all_args():
|
|
635
|
+
exp_args, remaining = parse_torsion_args()
|
|
636
|
+
original_argv = sys.argv
|
|
637
|
+
try:
|
|
638
|
+
sys.argv = [original_argv[0]] + remaining
|
|
639
|
+
args = args_parser()
|
|
640
|
+
finally:
|
|
641
|
+
sys.argv = original_argv
|
|
642
|
+
|
|
643
|
+
for key, value in vars(exp_args).items():
|
|
644
|
+
setattr(args, key, value)
|
|
645
|
+
return args
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
def build_writer(logger_path: str):
|
|
649
|
+
tb_dir = os.path.join(logger_path, "tensorboard")
|
|
650
|
+
os.makedirs(tb_dir, exist_ok=True)
|
|
651
|
+
return SummaryWriter(log_dir=tb_dir)
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
class TorsionTrainer(Trainer):
|
|
655
|
+
"""Trainer that swaps Stage-2 loss for torsion-space mode decoding."""
|
|
656
|
+
|
|
657
|
+
def __init__(self, args: Any, writer: SummaryWriter, logger_path: str):
|
|
658
|
+
# Stage-II modules are attached after the base Trainer is initialized.
|
|
659
|
+
# Defer a full progress restore until those modules and their optimizer
|
|
660
|
+
# exist, otherwise strict loading rejects the saved torsion-decoder keys.
|
|
661
|
+
progress_checkpoint = getattr(args, "resume_progress_checkpoint", None)
|
|
662
|
+
if progress_checkpoint:
|
|
663
|
+
args.resume_progress_checkpoint = None
|
|
664
|
+
super().__init__(args=args, writer=writer, logger_path=logger_path)
|
|
665
|
+
if progress_checkpoint:
|
|
666
|
+
args.resume_progress_checkpoint = progress_checkpoint
|
|
667
|
+
|
|
668
|
+
emb_dim = int(getattr(args, "emb_dim", 128))
|
|
669
|
+
hidden_dim = int(getattr(args, "mlp_hidden", getattr(args, "coarse_hidden", 128)))
|
|
670
|
+
mode_dim = int(getattr(args, "mode_dim", getattr(args, "latent_dim", 32)))
|
|
671
|
+
mode_condition_strategy = str(getattr(args, "mode_condition_strategy", "z")).lower()
|
|
672
|
+
prototype_dim = int(getattr(args, "mode_prototype_dim", mode_dim))
|
|
673
|
+
decoder_prototype_dim = prototype_dim if mode_condition_strategy in {"learned_prototype", "z_plus_prototype"} else 0
|
|
674
|
+
torsion_context_dim = 4 if bool(getattr(args, "use_mean_dispersion_torsion_context", False)) else 0
|
|
675
|
+
self.model.torsion_mode_decoder = TorsionModeDecoder(
|
|
676
|
+
emb_dim=emb_dim,
|
|
677
|
+
mode_dim=mode_dim,
|
|
678
|
+
hidden_dim=hidden_dim,
|
|
679
|
+
max_delta=float(getattr(args, "torsion_delta_scale", math.pi)),
|
|
680
|
+
prototype_dim=decoder_prototype_dim,
|
|
681
|
+
torsion_context_dim=torsion_context_dim,
|
|
682
|
+
).to(self.device)
|
|
683
|
+
if int(getattr(args, "iterative_torsion_steps", 0)) > 0:
|
|
684
|
+
self.model.torsion_refine_decoder = TorsionModeDecoder(
|
|
685
|
+
emb_dim=emb_dim,
|
|
686
|
+
mode_dim=mode_dim,
|
|
687
|
+
hidden_dim=hidden_dim,
|
|
688
|
+
max_delta=float(getattr(args, "iterative_torsion_delta_scale", 0.8)),
|
|
689
|
+
prototype_dim=decoder_prototype_dim,
|
|
690
|
+
torsion_context_dim=torsion_context_dim,
|
|
691
|
+
).to(self.device)
|
|
692
|
+
if decoder_prototype_dim > 0:
|
|
693
|
+
self.model.mode_prototype_embedding = nn.Embedding(
|
|
694
|
+
int(getattr(args, "num_modes", 10)),
|
|
695
|
+
decoder_prototype_dim,
|
|
696
|
+
).to(self.device)
|
|
697
|
+
self._load_train2_torsion_modules_if_needed()
|
|
698
|
+
|
|
699
|
+
self._reset_trainable_modules()
|
|
700
|
+
self.opt = self._build_optimizer()
|
|
701
|
+
eta_min = float(getattr(self.cfg, "eta_min", 1e-6))
|
|
702
|
+
if eta_min >= float(self.cfg.lr):
|
|
703
|
+
eta_min = float(self.cfg.lr) * 0.01
|
|
704
|
+
self.scheduler = CosineAnnealingLR(
|
|
705
|
+
self.opt,
|
|
706
|
+
T_max=max(1, int(self.cfg.epoch)),
|
|
707
|
+
eta_min=eta_min,
|
|
708
|
+
)
|
|
709
|
+
if progress_checkpoint:
|
|
710
|
+
self._load_progress_checkpoint_if_needed()
|
|
711
|
+
self.did_optimizer_step = False
|
|
712
|
+
self.empty_supervision_batches = 0
|
|
713
|
+
trainable_count = sum(p.numel() for p in self.model.parameters() if p.requires_grad)
|
|
714
|
+
frozen_count = sum(p.numel() for p in self.model.parameters() if not p.requires_grad)
|
|
715
|
+
logger.info("train_torsion_generator torsion-space branch enabled.")
|
|
716
|
+
logger.info(f"Trainable parameters after train_torsion_generator reset: {trainable_count}")
|
|
717
|
+
logger.info(f"Frozen parameters after train_torsion_generator reset: {frozen_count}")
|
|
718
|
+
|
|
719
|
+
def _trainable_zero_loss(self, reference: torch.Tensor) -> torch.Tensor:
|
|
720
|
+
"""Return a scalar zero connected to every trainable Stage-II parameter."""
|
|
721
|
+
terms = [
|
|
722
|
+
parameter.reshape(-1)[0] * 0.0
|
|
723
|
+
for parameter in self.model.parameters()
|
|
724
|
+
if parameter.requires_grad and parameter.numel() > 0
|
|
725
|
+
]
|
|
726
|
+
if not terms:
|
|
727
|
+
return reference.sum() * 0.0
|
|
728
|
+
return torch.stack(terms).sum()
|
|
729
|
+
|
|
730
|
+
def _load_train2_torsion_modules_if_needed(self):
|
|
731
|
+
checkpoint_path = str(getattr(self.args, "train2_resume_torsion_checkpoint", "") or "")
|
|
732
|
+
if not checkpoint_path:
|
|
733
|
+
checkpoint_path = str(getattr(self.args, "checkpoint_path", "") or "")
|
|
734
|
+
if not checkpoint_path or not os.path.exists(checkpoint_path):
|
|
735
|
+
return
|
|
736
|
+
try:
|
|
737
|
+
ckpt = torch.load(checkpoint_path, map_location=self.device)
|
|
738
|
+
if isinstance(ckpt, dict) and "model_state_dict" in ckpt:
|
|
739
|
+
state = ckpt["model_state_dict"]
|
|
740
|
+
elif isinstance(ckpt, dict) and "state_dict" in ckpt:
|
|
741
|
+
state = ckpt["state_dict"]
|
|
742
|
+
else:
|
|
743
|
+
state = ckpt
|
|
744
|
+
prefixes = ("torsion_mode_decoder.", "torsion_refine_decoder.", "mode_prototype_embedding.")
|
|
745
|
+
model_state = self.model.state_dict()
|
|
746
|
+
compatible = {}
|
|
747
|
+
skipped = []
|
|
748
|
+
for key, value in state.items():
|
|
749
|
+
if not key.startswith(prefixes):
|
|
750
|
+
continue
|
|
751
|
+
if key in model_state and model_state[key].shape == value.shape:
|
|
752
|
+
compatible[key] = value
|
|
753
|
+
else:
|
|
754
|
+
skipped.append(key)
|
|
755
|
+
if not compatible:
|
|
756
|
+
logger.info("No compatible train_torsion_generator torsion modules found in %s.", checkpoint_path)
|
|
757
|
+
return
|
|
758
|
+
model_state.update(compatible)
|
|
759
|
+
self.model.load_state_dict(model_state, strict=True)
|
|
760
|
+
logger.info(
|
|
761
|
+
"Loaded train_torsion_generator torsion modules from %s, loaded=%d, skipped=%d",
|
|
762
|
+
checkpoint_path,
|
|
763
|
+
len(compatible),
|
|
764
|
+
len(skipped),
|
|
765
|
+
)
|
|
766
|
+
if skipped:
|
|
767
|
+
logger.warning("Skipped train_torsion_generator torsion module keys: %s", skipped[:20])
|
|
768
|
+
except Exception:
|
|
769
|
+
logger.exception("加载 train_torsion_generator torsion modules 失败,继续使用新初始化模块")
|
|
770
|
+
|
|
771
|
+
def _reset_trainable_modules(self):
|
|
772
|
+
self._set_requires_grad(self.model, False)
|
|
773
|
+
if not bool(getattr(self.args, "train2_freeze_torsion_mode_decoder", False)):
|
|
774
|
+
self._unfreeze_modules(["torsion_mode_decoder"])
|
|
775
|
+
else:
|
|
776
|
+
logger.info("train2_freeze_torsion_mode_decoder=True:冻结 coarse torsion decoder,只训练局部 corrector。")
|
|
777
|
+
if hasattr(self.model, "mode_prototype_embedding"):
|
|
778
|
+
if not bool(getattr(self.args, "train2_freeze_torsion_mode_decoder", False)):
|
|
779
|
+
self._unfreeze_modules(["mode_prototype_embedding"])
|
|
780
|
+
logger.info(
|
|
781
|
+
"mode_condition_strategy=%s:启用可学习 mode/basin prototype embedding。",
|
|
782
|
+
str(getattr(self.args, "mode_condition_strategy", "z")),
|
|
783
|
+
)
|
|
784
|
+
if int(getattr(self.args, "iterative_torsion_steps", 0)) > 0:
|
|
785
|
+
self._unfreeze_modules(["torsion_refine_decoder"])
|
|
786
|
+
logger.info(
|
|
787
|
+
"iterative_torsion_steps=%s:启用 iterative torsion refinement。",
|
|
788
|
+
int(getattr(self.args, "iterative_torsion_steps", 0)),
|
|
789
|
+
)
|
|
790
|
+
if bool(getattr(self.args, "train2_use_refiner", False)):
|
|
791
|
+
self._unfreeze_modules(["refiner"])
|
|
792
|
+
logger.info("train2_use_refiner=True:torsion 后继续训练轻量 refiner。")
|
|
793
|
+
if bool(getattr(self.args, "use_end2end_pglobal", False)):
|
|
794
|
+
self._unfreeze_modules(["gnn", "global_edge_predictor", "ab_head", "conf_embedding"])
|
|
795
|
+
logger.info("use_end2end_pglobal=True:train_torsion_generator 同时微调 GNN/P_global 分支。")
|
|
796
|
+
elif bool(getattr(self.args, "train2_unfreeze_gnn", False)):
|
|
797
|
+
self._unfreeze_modules(["gnn"])
|
|
798
|
+
logger.info("train2_unfreeze_gnn=True:Stage2-only 消融中训练 GNN encoder,但不启用 Stage1 P_global。")
|
|
799
|
+
|
|
800
|
+
def train_batch(self, batch: Any, epoch: int = 0) -> float:
|
|
801
|
+
batch = batch.to(self.device, non_blocking=(self.device.type == "cuda"))
|
|
802
|
+
self.opt.zero_grad(set_to_none=True)
|
|
803
|
+
|
|
804
|
+
node_feat = self.model.gnn(batch)
|
|
805
|
+
graph_feat = self.model.pool(node_feat, batch.batch)
|
|
806
|
+
loss, pos_loss, _ = self.compute_torsion_mode_loss(batch, node_feat, graph_feat)
|
|
807
|
+
loss = torch.nan_to_num(loss, nan=0.0, posinf=1e6, neginf=0.0)
|
|
808
|
+
|
|
809
|
+
if not torch.isfinite(loss):
|
|
810
|
+
logger.warning("Skip batch because loss is NaN/Inf")
|
|
811
|
+
return 0.0
|
|
812
|
+
|
|
813
|
+
# Full GEOM-Drugs contains molecules without a usable torsion target.
|
|
814
|
+
# Every distributed rank must still make the same collective calls;
|
|
815
|
+
# otherwise one early return leaves the other ranks blocked in all-reduce.
|
|
816
|
+
local_has_supervision = bool(loss.requires_grad)
|
|
817
|
+
active_ranks = 1
|
|
818
|
+
if bool(getattr(self.args, "distributed_stage2", False)):
|
|
819
|
+
active = torch.tensor(
|
|
820
|
+
1 if local_has_supervision else 0,
|
|
821
|
+
dtype=torch.int64,
|
|
822
|
+
device=self.device,
|
|
823
|
+
)
|
|
824
|
+
dist.all_reduce(active, op=dist.ReduceOp.SUM)
|
|
825
|
+
active_ranks = int(active.item())
|
|
826
|
+
if active_ranks == 0:
|
|
827
|
+
self.empty_supervision_batches += 1
|
|
828
|
+
self.opt.zero_grad(set_to_none=True)
|
|
829
|
+
if self.empty_supervision_batches <= 5 or self.empty_supervision_batches % 100 == 0:
|
|
830
|
+
logger.warning(
|
|
831
|
+
"Skip globally empty Stage-II batch (count=%d)",
|
|
832
|
+
self.empty_supervision_batches,
|
|
833
|
+
)
|
|
834
|
+
return 0.0
|
|
835
|
+
|
|
836
|
+
if not local_has_supervision:
|
|
837
|
+
self.empty_supervision_batches += 1
|
|
838
|
+
loss = self._trainable_zero_loss(loss)
|
|
839
|
+
if self.empty_supervision_batches <= 5 or self.empty_supervision_batches % 100 == 0:
|
|
840
|
+
logger.warning(
|
|
841
|
+
"Rank has no valid Stage-II target; contributing zero gradients "
|
|
842
|
+
"to keep distributed collectives aligned (count=%d)",
|
|
843
|
+
self.empty_supervision_batches,
|
|
844
|
+
)
|
|
845
|
+
|
|
846
|
+
loss.backward()
|
|
847
|
+
trainable_params = [p for p in self.model.parameters() if p.requires_grad]
|
|
848
|
+
if bool(getattr(self.args, "distributed_stage2", False)):
|
|
849
|
+
for parameter in trainable_params:
|
|
850
|
+
if parameter.grad is None:
|
|
851
|
+
parameter.grad = torch.zeros_like(parameter)
|
|
852
|
+
dist.all_reduce(parameter.grad, op=dist.ReduceOp.SUM)
|
|
853
|
+
parameter.grad.div_(max(1, active_ranks))
|
|
854
|
+
finite_checks = [torch.isfinite(p.grad).all() for p in trainable_params if p.grad is not None]
|
|
855
|
+
bad_grad = bool(finite_checks) and not bool(torch.stack(finite_checks).all().item())
|
|
856
|
+
if bad_grad:
|
|
857
|
+
logger.warning("Skip optimizer step because gradient is NaN/Inf")
|
|
858
|
+
self.opt.zero_grad(set_to_none=True)
|
|
859
|
+
return 0.0
|
|
860
|
+
torch.nn.utils.clip_grad_norm_(trainable_params, 1.0)
|
|
861
|
+
self.opt.step()
|
|
862
|
+
self.did_optimizer_step = True
|
|
863
|
+
self.total_step += 1
|
|
864
|
+
|
|
865
|
+
loss_value = float(loss.detach().item())
|
|
866
|
+
pos_value = float(pos_loss.detach().item())
|
|
867
|
+
self.writer.add_scalar("loss/total", loss_value, self.total_step)
|
|
868
|
+
self.writer.add_scalar("loss/pos", pos_value, self.total_step)
|
|
869
|
+
self.writer.add_scalar("lr", self.opt.param_groups[0]["lr"], self.total_step)
|
|
870
|
+
return loss_value
|
|
871
|
+
|
|
872
|
+
def _base_graph_index(self, batch: Any, graph_indices: List[int]) -> int:
|
|
873
|
+
if str(getattr(self.args, "train2_base_strategy", "first")).lower() == "conf0" and hasattr(batch, "conf_id"):
|
|
874
|
+
for gi in graph_indices:
|
|
875
|
+
if int(batch.conf_id[gi].item()) == 0:
|
|
876
|
+
return int(gi)
|
|
877
|
+
return int(graph_indices[0])
|
|
878
|
+
|
|
879
|
+
def _mode_code(self, k: int, device, dtype) -> torch.Tensor:
|
|
880
|
+
if hasattr(self.model, "_make_z"):
|
|
881
|
+
return self.model._make_z(
|
|
882
|
+
k,
|
|
883
|
+
device,
|
|
884
|
+
dtype,
|
|
885
|
+
deterministic=bool(getattr(self.args, "deterministic_modes", True)),
|
|
886
|
+
)
|
|
887
|
+
z = torch.zeros(int(getattr(self.args, "mode_dim", 32)), device=device, dtype=dtype)
|
|
888
|
+
z[k % z.numel()] = 1.0
|
|
889
|
+
return z
|
|
890
|
+
|
|
891
|
+
def _mode_inputs(self, k: int, device, dtype) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
|
|
892
|
+
strategy = str(getattr(self.args, "mode_condition_strategy", "z")).lower()
|
|
893
|
+
z = self._mode_code(k, device, dtype)
|
|
894
|
+
prototype = None
|
|
895
|
+
if strategy in {"learned_prototype", "z_plus_prototype"} and hasattr(self.model, "mode_prototype_embedding"):
|
|
896
|
+
idx = torch.tensor(
|
|
897
|
+
int(k) % int(getattr(self.args, "num_modes", 10)),
|
|
898
|
+
device=device,
|
|
899
|
+
dtype=torch.long,
|
|
900
|
+
)
|
|
901
|
+
prototype = self.model.mode_prototype_embedding(idx).to(dtype=dtype)
|
|
902
|
+
if strategy == "learned_prototype":
|
|
903
|
+
z = torch.zeros_like(z)
|
|
904
|
+
return z, prototype
|
|
905
|
+
|
|
906
|
+
def _predict_group_modes(
|
|
907
|
+
self,
|
|
908
|
+
batch: Any,
|
|
909
|
+
node_feat: torch.Tensor,
|
|
910
|
+
graph_feat: torch.Tensor,
|
|
911
|
+
graph_indices: List[int],
|
|
912
|
+
) -> Tuple[List[torch.Tensor], List[torch.Tensor], int, Optional[torch.Tensor]]:
|
|
913
|
+
batch_vec = batch.batch
|
|
914
|
+
base_idx = self._base_graph_index(batch, graph_indices)
|
|
915
|
+
base_mask = batch_vec == base_idx
|
|
916
|
+
feat_i = node_feat[base_mask]
|
|
917
|
+
graph_feat_i = graph_feat[base_idx]
|
|
918
|
+
base_pos = batch.pos[base_mask]
|
|
919
|
+
if feat_i.size(0) <= 1 or base_pos.size(0) != feat_i.size(0):
|
|
920
|
+
return [], [], base_idx, None
|
|
921
|
+
|
|
922
|
+
edge_i = self.model._get_local_edge_index(batch.edge_index, base_mask)
|
|
923
|
+
local_torsion_index = self.model._get_local_torsion_index(
|
|
924
|
+
getattr(batch, "torsion_index", None), base_mask
|
|
925
|
+
)
|
|
926
|
+
rows = _torsion_rows(local_torsion_index)
|
|
927
|
+
if rows is None or rows.numel() == 0:
|
|
928
|
+
return [], [], base_idx, None
|
|
929
|
+
cached_rotation_masks = None
|
|
930
|
+
if bool(getattr(self.args, "vectorized_stage2_loss", False)):
|
|
931
|
+
cached_rotation_masks = torsion_rotation_masks(
|
|
932
|
+
num_atoms=base_pos.size(0),
|
|
933
|
+
torsions=local_torsion_index,
|
|
934
|
+
edge_index=edge_i,
|
|
935
|
+
max_torsions=int(getattr(self.args, "torsion_mode_max", 64)),
|
|
936
|
+
)
|
|
937
|
+
|
|
938
|
+
base_decode = base_pos
|
|
939
|
+
noise_prob = float(getattr(self.args, "train2_base_noise_prob", 0.0))
|
|
940
|
+
noise_scale = float(getattr(self.args, "train2_base_noise_scale", 0.0))
|
|
941
|
+
if self.model.training and noise_prob > 0.0 and noise_scale > 0.0:
|
|
942
|
+
if torch.rand((), device=base_pos.device).item() < min(1.0, max(0.0, noise_prob)):
|
|
943
|
+
noise = torch.randn_like(base_pos) * noise_scale
|
|
944
|
+
base_decode = base_pos + noise
|
|
945
|
+
base_decode = base_decode - base_decode.mean(dim=0, keepdim=True) + base_pos.mean(dim=0, keepdim=True)
|
|
946
|
+
|
|
947
|
+
if bool(getattr(self.args, "disable_stage1_pglobal", False)):
|
|
948
|
+
P_global = feat_i.new_zeros((feat_i.size(0), feat_i.size(0)))
|
|
949
|
+
P_disp = None
|
|
950
|
+
else:
|
|
951
|
+
if bool(getattr(self.args, "use_mean_dispersion_torsion_context", False)):
|
|
952
|
+
P_global, P_disp, _ = self.model._predict_mean_dispersion(feat_i, edge_i)
|
|
953
|
+
else:
|
|
954
|
+
P_global, _ = self.model._predict_P_global(feat_i, edge_i)
|
|
955
|
+
P_disp = None
|
|
956
|
+
P_det = P_global if bool(getattr(self.args, "use_end2end_pglobal", False)) else P_global.detach()
|
|
957
|
+
feat_det = feat_i if bool(getattr(self.args, "use_end2end_pglobal", False)) else feat_i.detach()
|
|
958
|
+
graph_det = graph_feat_i if bool(getattr(self.args, "use_end2end_pglobal", False)) else graph_feat_i.detach()
|
|
959
|
+
torsion_context = None
|
|
960
|
+
if bool(getattr(self.args, "use_mean_dispersion_torsion_context", False)) and P_disp is not None:
|
|
961
|
+
mol = batch.rdmol[base_idx] if hasattr(batch, "rdmol") else None
|
|
962
|
+
torsion_context, _ = mean_dispersion_torsion_context(
|
|
963
|
+
P_det,
|
|
964
|
+
P_disp if bool(getattr(self.args, "use_end2end_pglobal", False)) else P_disp.detach(),
|
|
965
|
+
mol,
|
|
966
|
+
local_torsion_index,
|
|
967
|
+
int(getattr(self.args, "torsion_mode_max", 64)),
|
|
968
|
+
)
|
|
969
|
+
|
|
970
|
+
outputs: List[torch.Tensor] = []
|
|
971
|
+
deltas: List[torch.Tensor] = []
|
|
972
|
+
if bool(getattr(self.args, "vectorized_stage2_loss", False)):
|
|
973
|
+
mode_inputs = [
|
|
974
|
+
self._mode_inputs(k, P_det.device, P_det.dtype)
|
|
975
|
+
for k in range(int(getattr(self.args, "num_modes", 10)))
|
|
976
|
+
]
|
|
977
|
+
z_modes = torch.stack([item[0] for item in mode_inputs], dim=0)
|
|
978
|
+
prototypes = None
|
|
979
|
+
if mode_inputs and mode_inputs[0][1] is not None:
|
|
980
|
+
prototypes = torch.stack([item[1] for item in mode_inputs], dim=0)
|
|
981
|
+
delta_modes = self.model.torsion_mode_decoder.forward_modes(
|
|
982
|
+
node_feat=feat_det,
|
|
983
|
+
graph_feat=graph_det,
|
|
984
|
+
coords=base_decode,
|
|
985
|
+
P=P_det,
|
|
986
|
+
torsion_index=local_torsion_index,
|
|
987
|
+
z=z_modes,
|
|
988
|
+
prototype=prototypes,
|
|
989
|
+
max_torsions=int(getattr(self.args, "torsion_mode_max", 64)),
|
|
990
|
+
torsion_context=torsion_context,
|
|
991
|
+
)
|
|
992
|
+
coordinates = base_decode.unsqueeze(0).expand(z_modes.size(0), -1, -1)
|
|
993
|
+
coordinates = apply_torsion_residuals_modes(
|
|
994
|
+
coordinates,
|
|
995
|
+
torsions=local_torsion_index,
|
|
996
|
+
deltas=delta_modes,
|
|
997
|
+
rotation_masks=cached_rotation_masks or [],
|
|
998
|
+
max_torsions=int(getattr(self.args, "torsion_mode_max", 64)),
|
|
999
|
+
)
|
|
1000
|
+
for _ in range(max(0, int(getattr(self.args, "iterative_torsion_steps", 0)))):
|
|
1001
|
+
if not hasattr(self.model, "torsion_refine_decoder"):
|
|
1002
|
+
break
|
|
1003
|
+
step_modes = self.model.torsion_refine_decoder.forward_modes(
|
|
1004
|
+
node_feat=feat_det,
|
|
1005
|
+
graph_feat=graph_det,
|
|
1006
|
+
coords=coordinates,
|
|
1007
|
+
P=P_det,
|
|
1008
|
+
torsion_index=local_torsion_index,
|
|
1009
|
+
z=z_modes,
|
|
1010
|
+
prototype=prototypes,
|
|
1011
|
+
max_torsions=int(getattr(self.args, "torsion_mode_max", 64)),
|
|
1012
|
+
torsion_context=torsion_context,
|
|
1013
|
+
)
|
|
1014
|
+
coordinates = apply_torsion_residuals_modes(
|
|
1015
|
+
coordinates,
|
|
1016
|
+
torsions=local_torsion_index,
|
|
1017
|
+
deltas=step_modes,
|
|
1018
|
+
rotation_masks=cached_rotation_masks or [],
|
|
1019
|
+
max_torsions=int(getattr(self.args, "torsion_mode_max", 64)),
|
|
1020
|
+
)
|
|
1021
|
+
if bool(getattr(self.args, "train2_use_refiner", False)):
|
|
1022
|
+
refined = []
|
|
1023
|
+
for mode_coords in coordinates:
|
|
1024
|
+
value, _ = self.model.refiner(
|
|
1025
|
+
P=P_det, node_feat=feat_det, y0=mode_coords,
|
|
1026
|
+
edge_index=edge_i, edge_attr=None
|
|
1027
|
+
)
|
|
1028
|
+
refined.append(value)
|
|
1029
|
+
coordinates = torch.stack(refined, dim=0)
|
|
1030
|
+
return list(coordinates.unbind(dim=0)), list(delta_modes.unbind(dim=0)), base_idx, base_decode
|
|
1031
|
+
|
|
1032
|
+
for k in range(int(getattr(self.args, "num_modes", 10))):
|
|
1033
|
+
z, prototype = self._mode_inputs(k, P_det.device, P_det.dtype)
|
|
1034
|
+
delta = self.model.torsion_mode_decoder(
|
|
1035
|
+
node_feat=feat_det,
|
|
1036
|
+
graph_feat=graph_det,
|
|
1037
|
+
coords=base_decode,
|
|
1038
|
+
P=P_det,
|
|
1039
|
+
torsion_index=local_torsion_index,
|
|
1040
|
+
z=z,
|
|
1041
|
+
prototype=prototype,
|
|
1042
|
+
max_torsions=int(getattr(self.args, "torsion_mode_max", 64)),
|
|
1043
|
+
torsion_context=torsion_context,
|
|
1044
|
+
)
|
|
1045
|
+
y = apply_torsion_residuals(
|
|
1046
|
+
base_decode,
|
|
1047
|
+
torsions=local_torsion_index,
|
|
1048
|
+
deltas=delta,
|
|
1049
|
+
edge_index=edge_i,
|
|
1050
|
+
max_torsions=int(getattr(self.args, "torsion_mode_max", 64)),
|
|
1051
|
+
rotation_masks=cached_rotation_masks,
|
|
1052
|
+
)
|
|
1053
|
+
for _ in range(max(0, int(getattr(self.args, "iterative_torsion_steps", 0)))):
|
|
1054
|
+
if not hasattr(self.model, "torsion_refine_decoder"):
|
|
1055
|
+
break
|
|
1056
|
+
step_delta = self.model.torsion_refine_decoder(
|
|
1057
|
+
node_feat=feat_det,
|
|
1058
|
+
graph_feat=graph_det,
|
|
1059
|
+
coords=y,
|
|
1060
|
+
P=P_det,
|
|
1061
|
+
torsion_index=local_torsion_index,
|
|
1062
|
+
z=z,
|
|
1063
|
+
prototype=prototype,
|
|
1064
|
+
max_torsions=int(getattr(self.args, "torsion_mode_max", 64)),
|
|
1065
|
+
torsion_context=torsion_context,
|
|
1066
|
+
)
|
|
1067
|
+
y = apply_torsion_residuals(
|
|
1068
|
+
y,
|
|
1069
|
+
torsions=local_torsion_index,
|
|
1070
|
+
deltas=step_delta,
|
|
1071
|
+
edge_index=edge_i,
|
|
1072
|
+
max_torsions=int(getattr(self.args, "torsion_mode_max", 64)),
|
|
1073
|
+
rotation_masks=cached_rotation_masks,
|
|
1074
|
+
)
|
|
1075
|
+
if bool(getattr(self.args, "train2_use_refiner", False)):
|
|
1076
|
+
y, _ = self.model.refiner(P=P_det, node_feat=feat_det, y0=y, edge_index=edge_i, edge_attr=None)
|
|
1077
|
+
outputs.append(y)
|
|
1078
|
+
deltas.append(delta)
|
|
1079
|
+
return outputs, deltas, base_idx, base_decode
|
|
1080
|
+
|
|
1081
|
+
def _diversity_loss(self, outputs: List[torch.Tensor], atom_x: Optional[torch.Tensor]) -> torch.Tensor:
|
|
1082
|
+
if len(outputs) <= 1:
|
|
1083
|
+
return outputs[0].sum() * 0.0 if outputs else torch.tensor(0.0, device=self.device)
|
|
1084
|
+
margin = float(getattr(self.args, "torsion_mode_diversity_margin", 0.35))
|
|
1085
|
+
if bool(getattr(self.args, "vectorized_stage2_loss", False)):
|
|
1086
|
+
stacked = torch.stack(outputs, dim=0)
|
|
1087
|
+
distances = pairwise_heavy_kabsch_rmsd_matrix(stacked, stacked, atom_x)
|
|
1088
|
+
upper = torch.triu(
|
|
1089
|
+
torch.ones_like(distances, dtype=torch.bool), diagonal=1
|
|
1090
|
+
)
|
|
1091
|
+
return torch.relu(stacked.new_tensor(margin) - distances[upper]).mean().clamp(0.0, 10.0)
|
|
1092
|
+
vals = []
|
|
1093
|
+
for i in range(len(outputs)):
|
|
1094
|
+
for j in range(i + 1, len(outputs)):
|
|
1095
|
+
d = safe_heavy_kabsch_rmsd(outputs[i], outputs[j], atom_x)
|
|
1096
|
+
vals.append(torch.relu(outputs[i].new_tensor(margin) - d))
|
|
1097
|
+
if not vals:
|
|
1098
|
+
return outputs[0].sum() * 0.0
|
|
1099
|
+
return torch.stack(vals).mean().clamp(0.0, 10.0)
|
|
1100
|
+
|
|
1101
|
+
def _cluster_mode_assignments(
|
|
1102
|
+
self,
|
|
1103
|
+
data: Any,
|
|
1104
|
+
graph_indices: List[int],
|
|
1105
|
+
batch_vec: torch.Tensor,
|
|
1106
|
+
num_modes: int,
|
|
1107
|
+
) -> Tuple[Dict[int, int], int]:
|
|
1108
|
+
"""Assign references to geometry clusters and map clusters to mode ids.
|
|
1109
|
+
|
|
1110
|
+
This prevents every reference conformer of the same molecule from
|
|
1111
|
+
supervising the same easiest mode. Farthest-point anchors are enough
|
|
1112
|
+
because each training molecule normally has only a few references.
|
|
1113
|
+
"""
|
|
1114
|
+
if hasattr(data, "stage2_cluster_id") and hasattr(data, "stage2_cluster_count"):
|
|
1115
|
+
cached = {}
|
|
1116
|
+
counts = []
|
|
1117
|
+
for gi in graph_indices:
|
|
1118
|
+
cluster_id = int(data.stage2_cluster_id[int(gi)].item())
|
|
1119
|
+
cluster_count = int(data.stage2_cluster_count[int(gi)].item())
|
|
1120
|
+
if cluster_id < 0 or cluster_count <= 0:
|
|
1121
|
+
cached = {}
|
|
1122
|
+
break
|
|
1123
|
+
cached[int(gi)] = cluster_id
|
|
1124
|
+
counts.append(cluster_count)
|
|
1125
|
+
if len(cached) == len(graph_indices) and counts:
|
|
1126
|
+
return cached, max(counts)
|
|
1127
|
+
valid_graphs: List[int] = []
|
|
1128
|
+
positions: List[torch.Tensor] = []
|
|
1129
|
+
atom_x = None
|
|
1130
|
+
for gi in graph_indices:
|
|
1131
|
+
mask = batch_vec == int(gi)
|
|
1132
|
+
pos = data.pos[mask]
|
|
1133
|
+
if pos.size(0) <= 1:
|
|
1134
|
+
continue
|
|
1135
|
+
valid_graphs.append(int(gi))
|
|
1136
|
+
positions.append(pos.detach())
|
|
1137
|
+
if atom_x is None and hasattr(data, "x"):
|
|
1138
|
+
atom_x = data.x[mask]
|
|
1139
|
+
|
|
1140
|
+
n = len(valid_graphs)
|
|
1141
|
+
if n == 0:
|
|
1142
|
+
return {}, 0
|
|
1143
|
+
if n == 1 or int(num_modes) <= 1:
|
|
1144
|
+
return {valid_graphs[0]: 0}, 1
|
|
1145
|
+
|
|
1146
|
+
if bool(getattr(self.args, "vectorized_stage2_loss", False)):
|
|
1147
|
+
refs = torch.stack(positions, dim=0)
|
|
1148
|
+
dist = pairwise_heavy_kabsch_rmsd_matrix(refs, refs, atom_x).detach()
|
|
1149
|
+
dist = torch.nan_to_num(dist, nan=0.0, posinf=10.0, neginf=0.0).clamp(0.0, 10.0)
|
|
1150
|
+
dist = 0.5 * (dist + dist.transpose(0, 1))
|
|
1151
|
+
dist.fill_diagonal_(0.0)
|
|
1152
|
+
else:
|
|
1153
|
+
dist = positions[0].new_zeros((n, n))
|
|
1154
|
+
for i in range(n):
|
|
1155
|
+
for j in range(i + 1, n):
|
|
1156
|
+
val = safe_heavy_kabsch_rmsd(positions[i], positions[j], atom_x).detach()
|
|
1157
|
+
val = torch.nan_to_num(val, nan=0.0, posinf=10.0, neginf=0.0).clamp(0.0, 10.0)
|
|
1158
|
+
dist[i, j] = val
|
|
1159
|
+
dist[j, i] = val
|
|
1160
|
+
|
|
1161
|
+
k_clusters = max(1, min(int(num_modes), n))
|
|
1162
|
+
anchors = [int(torch.argmax(dist.mean(dim=1)).item())]
|
|
1163
|
+
while len(anchors) < k_clusters:
|
|
1164
|
+
min_to_anchor = dist[:, anchors].min(dim=1).values
|
|
1165
|
+
min_to_anchor[torch.tensor(anchors, device=dist.device, dtype=torch.long)] = -1.0
|
|
1166
|
+
anchors.append(int(torch.argmax(min_to_anchor).item()))
|
|
1167
|
+
|
|
1168
|
+
nearest = torch.argmin(dist[:, anchors], dim=1).detach().cpu().tolist()
|
|
1169
|
+
return {valid_graphs[i]: int(nearest[i]) for i in range(n)}, k_clusters
|
|
1170
|
+
|
|
1171
|
+
def _torsion_cluster_mode_assignments(
|
|
1172
|
+
self,
|
|
1173
|
+
data: Any,
|
|
1174
|
+
graph_indices: List[int],
|
|
1175
|
+
batch_vec: torch.Tensor,
|
|
1176
|
+
torsion_rows: Optional[torch.Tensor],
|
|
1177
|
+
num_modes: int,
|
|
1178
|
+
) -> Tuple[Dict[int, int], int]:
|
|
1179
|
+
"""Assign references to clusters using periodic torsion fingerprints."""
|
|
1180
|
+
if torsion_rows is None or torsion_rows.numel() == 0:
|
|
1181
|
+
return self._cluster_mode_assignments(data, graph_indices, batch_vec, num_modes)
|
|
1182
|
+
|
|
1183
|
+
valid_graphs: List[int] = []
|
|
1184
|
+
torsion_vecs: List[torch.Tensor] = []
|
|
1185
|
+
rows = torsion_rows.long()
|
|
1186
|
+
for gi in graph_indices:
|
|
1187
|
+
mask = batch_vec == int(gi)
|
|
1188
|
+
pos = data.pos[mask]
|
|
1189
|
+
if pos.size(0) <= 1:
|
|
1190
|
+
continue
|
|
1191
|
+
try:
|
|
1192
|
+
valid = (rows >= 0).all(dim=1) & (rows < pos.size(0)).all(dim=1)
|
|
1193
|
+
local_rows = rows[valid]
|
|
1194
|
+
if local_rows.numel() == 0:
|
|
1195
|
+
continue
|
|
1196
|
+
vec = torsion_angles(pos, local_rows).detach()
|
|
1197
|
+
except Exception:
|
|
1198
|
+
continue
|
|
1199
|
+
if vec.numel() == 0:
|
|
1200
|
+
continue
|
|
1201
|
+
valid_graphs.append(int(gi))
|
|
1202
|
+
torsion_vecs.append(vec)
|
|
1203
|
+
|
|
1204
|
+
n = len(valid_graphs)
|
|
1205
|
+
if n == 0:
|
|
1206
|
+
return self._cluster_mode_assignments(data, graph_indices, batch_vec, num_modes)
|
|
1207
|
+
if n == 1 or int(num_modes) <= 1:
|
|
1208
|
+
return {valid_graphs[0]: 0}, 1
|
|
1209
|
+
|
|
1210
|
+
min_len = min(int(v.numel()) for v in torsion_vecs)
|
|
1211
|
+
if min_len <= 0:
|
|
1212
|
+
return self._cluster_mode_assignments(data, graph_indices, batch_vec, num_modes)
|
|
1213
|
+
torsion_vecs = [v[:min_len] for v in torsion_vecs]
|
|
1214
|
+
|
|
1215
|
+
dist = torsion_vecs[0].new_zeros((n, n))
|
|
1216
|
+
for i in range(n):
|
|
1217
|
+
for j in range(i + 1, n):
|
|
1218
|
+
delta = periodic_angle_delta(torsion_vecs[i], torsion_vecs[j])
|
|
1219
|
+
val = torch.sqrt((delta * delta).mean() + 1e-8).detach()
|
|
1220
|
+
val = torch.nan_to_num(val, nan=0.0, posinf=math.pi, neginf=0.0).clamp(0.0, math.pi)
|
|
1221
|
+
dist[i, j] = val
|
|
1222
|
+
dist[j, i] = val
|
|
1223
|
+
|
|
1224
|
+
k_clusters = max(1, min(int(num_modes), n))
|
|
1225
|
+
anchors = [int(torch.argmax(dist.mean(dim=1)).item())]
|
|
1226
|
+
while len(anchors) < k_clusters:
|
|
1227
|
+
min_to_anchor = dist[:, anchors].min(dim=1).values
|
|
1228
|
+
min_to_anchor[torch.tensor(anchors, device=dist.device, dtype=torch.long)] = -1.0
|
|
1229
|
+
anchors.append(int(torch.argmax(min_to_anchor).item()))
|
|
1230
|
+
|
|
1231
|
+
nearest = torch.argmin(dist[:, anchors], dim=1).detach().cpu().tolist()
|
|
1232
|
+
return {valid_graphs[i]: int(nearest[i]) for i in range(n)}, k_clusters
|
|
1233
|
+
|
|
1234
|
+
def _allowed_mode_indices(
|
|
1235
|
+
self,
|
|
1236
|
+
data: Any,
|
|
1237
|
+
gi: int,
|
|
1238
|
+
graph_indices: List[int],
|
|
1239
|
+
cluster_assignments: Dict[int, int],
|
|
1240
|
+
cluster_count: int,
|
|
1241
|
+
num_modes: int,
|
|
1242
|
+
) -> List[int]:
|
|
1243
|
+
strategy = str(getattr(self.args, "mode_assignment_strategy", "min")).lower()
|
|
1244
|
+
num_modes = max(1, int(num_modes))
|
|
1245
|
+
if strategy == "conf_id":
|
|
1246
|
+
cidx = self.model._get_conf_idx(data, int(gi))
|
|
1247
|
+
return [int(cidx) % num_modes]
|
|
1248
|
+
if strategy in {
|
|
1249
|
+
"cluster",
|
|
1250
|
+
"cluster_window",
|
|
1251
|
+
"torsion_cluster",
|
|
1252
|
+
"torsion_cluster_window",
|
|
1253
|
+
"stage1_p_basin",
|
|
1254
|
+
"stage1_p_basin_window",
|
|
1255
|
+
}:
|
|
1256
|
+
cluster_id = int(cluster_assignments.get(int(gi), 0)) % num_modes
|
|
1257
|
+
if strategy in {"cluster", "torsion_cluster", "stage1_p_basin"}:
|
|
1258
|
+
return [cluster_id]
|
|
1259
|
+
cluster_count = max(1, int(cluster_count))
|
|
1260
|
+
window = int(getattr(self.args, "mode_assignment_window", 0))
|
|
1261
|
+
if window <= 0:
|
|
1262
|
+
window = int(math.ceil(float(num_modes) / float(cluster_count)))
|
|
1263
|
+
window = max(1, window)
|
|
1264
|
+
start = (cluster_id * window) % num_modes
|
|
1265
|
+
return [int((start + offset) % num_modes) for offset in range(window)]
|
|
1266
|
+
return list(range(num_modes))
|
|
1267
|
+
|
|
1268
|
+
def compute_torsion_mode_loss(self, data: Any, node_feat: torch.Tensor, graph_feat: torch.Tensor):
|
|
1269
|
+
groups = self.model._group_graph_indices_by_mol_id(data, int(data.num_graphs))
|
|
1270
|
+
total = torch.tensor(0.0, device=node_feat.device)
|
|
1271
|
+
pos_log = torch.tensor(0.0, device=node_feat.device)
|
|
1272
|
+
y_pred_by_graph: List[Optional[torch.Tensor]] = [None for _ in range(int(data.num_graphs))]
|
|
1273
|
+
valid = 0
|
|
1274
|
+
|
|
1275
|
+
lambda_delta = float(getattr(self.args, "lambda_torsion_delta_direct", 0.2))
|
|
1276
|
+
lambda_div = float(getattr(self.args, "lambda_torsion_mode_diversity", 0.02))
|
|
1277
|
+
lambda_ce = float(getattr(self.args, "lambda_ce_manifold", 0.0))
|
|
1278
|
+
lambda_recall = float(getattr(self.args, "lambda_set_recall", 0.0))
|
|
1279
|
+
lambda_tight = float(getattr(self.args, "lambda_tight_rmsd", 0.0))
|
|
1280
|
+
lambda_tight_square = float(getattr(self.args, "lambda_tight_square", 0.0))
|
|
1281
|
+
tight_threshold = float(getattr(self.args, "tight_rmsd_threshold", 0.75))
|
|
1282
|
+
lambda_pair = float(getattr(self.args, "lambda_pair_distance", 0.0))
|
|
1283
|
+
lambda_local_pair = float(getattr(self.args, "lambda_local_pair_distance", 0.0))
|
|
1284
|
+
local_pair_cutoff = float(getattr(self.args, "local_pair_cutoff", 4.0))
|
|
1285
|
+
lambda_clash = float(getattr(self.args, "lambda_clash", 0.0))
|
|
1286
|
+
clash_min_distance = float(getattr(self.args, "clash_min_distance", 1.1))
|
|
1287
|
+
lambda_bond_match = float(getattr(self.args, "lambda_bond_match", 0.0))
|
|
1288
|
+
lambda_angle_match = float(getattr(self.args, "lambda_angle_match", 0.0))
|
|
1289
|
+
lambda_chiral_volume = float(getattr(self.args, "lambda_chiral_volume_torsion", 0.0))
|
|
1290
|
+
chiral_volume_max_centers = int(getattr(self.args, "chiral_volume_max_centers", 64))
|
|
1291
|
+
chiral_volume_ref_abs_min = float(getattr(self.args, "chiral_volume_ref_abs_min", 0.05))
|
|
1292
|
+
mode_assignment_strategy = str(getattr(self.args, "mode_assignment_strategy", "min")).lower()
|
|
1293
|
+
num_modes = int(getattr(self.args, "num_modes", 10))
|
|
1294
|
+
|
|
1295
|
+
for _, graph_indices in groups.items():
|
|
1296
|
+
outputs, deltas, base_idx, base_decode = self._predict_group_modes(data, node_feat, graph_feat, graph_indices)
|
|
1297
|
+
if not outputs:
|
|
1298
|
+
continue
|
|
1299
|
+
base_mask = data.batch == base_idx
|
|
1300
|
+
base_pos = data.pos[base_mask]
|
|
1301
|
+
atom_x_base = data.x[base_mask] if hasattr(data, "x") else None
|
|
1302
|
+
edge_i = self.model._get_local_edge_index(data.edge_index, base_mask)
|
|
1303
|
+
if base_decode is None:
|
|
1304
|
+
base_decode = base_pos
|
|
1305
|
+
local_torsion_index = self.model._get_local_torsion_index(
|
|
1306
|
+
getattr(data, "torsion_index", None), base_mask
|
|
1307
|
+
)
|
|
1308
|
+
rows = _torsion_rows(local_torsion_index)
|
|
1309
|
+
if rows is not None:
|
|
1310
|
+
rows = rows[: min(int(getattr(self.args, "torsion_mode_max", 64)), rows.size(0))].to(base_pos.device).long()
|
|
1311
|
+
|
|
1312
|
+
if lambda_div > 0:
|
|
1313
|
+
total = total + lambda_div * self._diversity_loss(outputs, atom_x_base)
|
|
1314
|
+
|
|
1315
|
+
vectorized = bool(getattr(self.args, "vectorized_stage2_loss", False))
|
|
1316
|
+
vector_ref_rows: Dict[int, int] = {}
|
|
1317
|
+
vector_rmsds = None
|
|
1318
|
+
vector_pair_losses = None
|
|
1319
|
+
if vectorized:
|
|
1320
|
+
ref_graphs = []
|
|
1321
|
+
ref_positions = []
|
|
1322
|
+
for gi in graph_indices:
|
|
1323
|
+
ref_mask = data.batch == int(gi)
|
|
1324
|
+
ref_pos = data.pos[ref_mask]
|
|
1325
|
+
if ref_pos.size(0) == base_pos.size(0):
|
|
1326
|
+
vector_ref_rows[int(gi)] = len(ref_graphs)
|
|
1327
|
+
ref_graphs.append(int(gi))
|
|
1328
|
+
ref_positions.append(ref_pos)
|
|
1329
|
+
if ref_positions:
|
|
1330
|
+
output_stack = torch.stack(outputs, dim=0)
|
|
1331
|
+
reference_stack = torch.stack(ref_positions, dim=0)
|
|
1332
|
+
vector_rmsds = pairwise_heavy_kabsch_rmsd_matrix(
|
|
1333
|
+
output_stack, reference_stack, atom_x_base
|
|
1334
|
+
).transpose(0, 1)
|
|
1335
|
+
vector_rmsds = torch.nan_to_num(
|
|
1336
|
+
vector_rmsds, nan=10.0, posinf=10.0, neginf=0.0
|
|
1337
|
+
).clamp(0.0, 10.0)
|
|
1338
|
+
if lambda_pair > 0:
|
|
1339
|
+
vector_pair_losses = pair_distance_loss_matrix(
|
|
1340
|
+
output_stack, reference_stack, atom_x_base
|
|
1341
|
+
).transpose(0, 1)
|
|
1342
|
+
|
|
1343
|
+
cluster_assignments: Dict[int, int] = {}
|
|
1344
|
+
cluster_count = 0
|
|
1345
|
+
if mode_assignment_strategy in {"cluster", "cluster_window"}:
|
|
1346
|
+
cluster_assignments, cluster_count = self._cluster_mode_assignments(
|
|
1347
|
+
data,
|
|
1348
|
+
graph_indices,
|
|
1349
|
+
data.batch,
|
|
1350
|
+
num_modes=max(1, len(outputs)),
|
|
1351
|
+
)
|
|
1352
|
+
elif mode_assignment_strategy in {"torsion_cluster", "torsion_cluster_window"}:
|
|
1353
|
+
cluster_assignments, cluster_count = self._torsion_cluster_mode_assignments(
|
|
1354
|
+
data,
|
|
1355
|
+
graph_indices,
|
|
1356
|
+
data.batch,
|
|
1357
|
+
torsion_rows=rows,
|
|
1358
|
+
num_modes=max(1, len(outputs)),
|
|
1359
|
+
)
|
|
1360
|
+
elif (
|
|
1361
|
+
mode_assignment_strategy in {"stage1_p_basin", "stage1_p_basin_window"}
|
|
1362
|
+
and hasattr(self, "_stage1_p_cluster_mode_assignments")
|
|
1363
|
+
):
|
|
1364
|
+
try:
|
|
1365
|
+
cluster_assignments, cluster_count = self._stage1_p_cluster_mode_assignments(
|
|
1366
|
+
data=data,
|
|
1367
|
+
graph_indices=graph_indices,
|
|
1368
|
+
batch_vec=data.batch,
|
|
1369
|
+
graph_feat=graph_feat,
|
|
1370
|
+
base_idx=base_idx,
|
|
1371
|
+
num_modes=max(1, len(outputs)),
|
|
1372
|
+
)
|
|
1373
|
+
except Exception:
|
|
1374
|
+
logger.exception("stage1_p_cluster_mode_assignments failed; fallback to geometry clusters.")
|
|
1375
|
+
cluster_assignments, cluster_count = self._cluster_mode_assignments(
|
|
1376
|
+
data,
|
|
1377
|
+
graph_indices,
|
|
1378
|
+
data.batch,
|
|
1379
|
+
num_modes=max(1, len(outputs)),
|
|
1380
|
+
)
|
|
1381
|
+
|
|
1382
|
+
ref_losses = []
|
|
1383
|
+
for gi in graph_indices:
|
|
1384
|
+
ref_mask = data.batch == gi
|
|
1385
|
+
pos = data.pos[ref_mask]
|
|
1386
|
+
if pos.size(0) != base_pos.size(0):
|
|
1387
|
+
continue
|
|
1388
|
+
atom_x = data.x[ref_mask] if hasattr(data, "x") else None
|
|
1389
|
+
|
|
1390
|
+
mode_losses = []
|
|
1391
|
+
mode_rmsds = []
|
|
1392
|
+
vector_row = vector_ref_rows.get(int(gi))
|
|
1393
|
+
for mode_idx, (y, delta) in enumerate(zip(outputs, deltas)):
|
|
1394
|
+
if vector_rmsds is not None and vector_row is not None:
|
|
1395
|
+
rmsd = vector_rmsds[vector_row, mode_idx]
|
|
1396
|
+
else:
|
|
1397
|
+
rmsd = safe_heavy_kabsch_rmsd(y, pos, atom_x).clamp(0.0, 10.0)
|
|
1398
|
+
mode_rmsds.append(rmsd)
|
|
1399
|
+
loss = rmsd
|
|
1400
|
+
if lambda_tight > 0:
|
|
1401
|
+
tight_residual = torch.relu(rmsd - rmsd.new_tensor(tight_threshold)).clamp(0.0, 10.0)
|
|
1402
|
+
loss = loss + lambda_tight * tight_residual
|
|
1403
|
+
if lambda_tight_square > 0:
|
|
1404
|
+
tight_residual = torch.relu(rmsd - rmsd.new_tensor(tight_threshold)).clamp(0.0, 10.0)
|
|
1405
|
+
loss = loss + lambda_tight_square * (tight_residual * tight_residual).clamp(0.0, 10.0)
|
|
1406
|
+
if lambda_pair > 0:
|
|
1407
|
+
if vector_pair_losses is not None and vector_row is not None:
|
|
1408
|
+
pair_loss_value = vector_pair_losses[vector_row, mode_idx]
|
|
1409
|
+
else:
|
|
1410
|
+
pair_loss_value = pair_distance_loss(y, pos, atom_x)
|
|
1411
|
+
loss = loss + lambda_pair * pair_loss_value
|
|
1412
|
+
if lambda_local_pair > 0:
|
|
1413
|
+
loss = loss + lambda_local_pair * local_pair_distance_loss(
|
|
1414
|
+
y,
|
|
1415
|
+
pos,
|
|
1416
|
+
atom_x,
|
|
1417
|
+
cutoff=local_pair_cutoff,
|
|
1418
|
+
)
|
|
1419
|
+
if lambda_clash > 0:
|
|
1420
|
+
loss = loss + lambda_clash * heavy_clash_loss(
|
|
1421
|
+
y,
|
|
1422
|
+
atom_x,
|
|
1423
|
+
min_distance=clash_min_distance,
|
|
1424
|
+
)
|
|
1425
|
+
if lambda_bond_match > 0:
|
|
1426
|
+
loss = loss + lambda_bond_match * bond_distance_match_loss(y, pos, edge_i).clamp(0.0, 10.0)
|
|
1427
|
+
if lambda_angle_match > 0:
|
|
1428
|
+
loss = loss + lambda_angle_match * angle_distance_match_loss(y, pos, edge_i).clamp(0.0, 10.0)
|
|
1429
|
+
if lambda_chiral_volume > 0:
|
|
1430
|
+
loss = loss + lambda_chiral_volume * signed_volume_supervision_loss(
|
|
1431
|
+
y,
|
|
1432
|
+
pos,
|
|
1433
|
+
edge_i,
|
|
1434
|
+
max_centers=chiral_volume_max_centers,
|
|
1435
|
+
ref_abs_min=chiral_volume_ref_abs_min,
|
|
1436
|
+
).clamp(0.0, 10.0)
|
|
1437
|
+
|
|
1438
|
+
if lambda_delta > 0 and rows is not None and rows.numel() > 0 and delta.numel() > 0:
|
|
1439
|
+
try:
|
|
1440
|
+
valid_rows = rows[: min(rows.size(0), delta.size(0))]
|
|
1441
|
+
base_t = torsion_angles(base_decode, valid_rows)
|
|
1442
|
+
ref_t = torsion_angles(pos, valid_rows).detach()
|
|
1443
|
+
target_delta = periodic_angle_delta(ref_t, base_t)
|
|
1444
|
+
loss_delta = 1.0 - torch.cos(delta[: valid_rows.size(0)] - target_delta)
|
|
1445
|
+
loss = loss + lambda_delta * loss_delta.mean().clamp(0.0, 10.0)
|
|
1446
|
+
except Exception:
|
|
1447
|
+
pass
|
|
1448
|
+
|
|
1449
|
+
if lambda_ce > 0 and not bool(getattr(self.args, "disable_stage1_pglobal", False)):
|
|
1450
|
+
try:
|
|
1451
|
+
feat_i = node_feat[base_mask]
|
|
1452
|
+
P_global, _ = self.model._predict_P_global(feat_i, edge_i)
|
|
1453
|
+
cidx = self.model._get_conf_idx(data, gi)
|
|
1454
|
+
a, b = self.model.predict_ab(graph_feat[base_idx], cidx)
|
|
1455
|
+
stage1_kernel = str(getattr(self.args, "stage1_kernel", "umap")).lower()
|
|
1456
|
+
P_pred = prob_low_dim_kernel(
|
|
1457
|
+
y,
|
|
1458
|
+
a.detach(),
|
|
1459
|
+
b.detach(),
|
|
1460
|
+
kernel=stage1_kernel,
|
|
1461
|
+
).clamp(1e-6, 1.0 - 1e-6)
|
|
1462
|
+
loss = loss + lambda_ce * cross_entropy_loss(
|
|
1463
|
+
P_pred, P_global.detach()
|
|
1464
|
+
).clamp(0.0, 10.0)
|
|
1465
|
+
except Exception:
|
|
1466
|
+
pass
|
|
1467
|
+
|
|
1468
|
+
mode_losses.append(loss.clamp(0.0, 10.0))
|
|
1469
|
+
|
|
1470
|
+
if not mode_losses:
|
|
1471
|
+
continue
|
|
1472
|
+
losses_t = torch.stack(mode_losses)
|
|
1473
|
+
rmsds_t = torch.stack(mode_rmsds)
|
|
1474
|
+
allowed = self._allowed_mode_indices(
|
|
1475
|
+
data=data,
|
|
1476
|
+
gi=int(gi),
|
|
1477
|
+
graph_indices=graph_indices,
|
|
1478
|
+
cluster_assignments=cluster_assignments,
|
|
1479
|
+
cluster_count=cluster_count,
|
|
1480
|
+
num_modes=len(outputs),
|
|
1481
|
+
)
|
|
1482
|
+
allowed = [idx for idx in allowed if 0 <= int(idx) < len(outputs)]
|
|
1483
|
+
if not allowed:
|
|
1484
|
+
allowed = list(range(len(outputs)))
|
|
1485
|
+
allowed_t = torch.tensor(allowed, dtype=torch.long, device=rmsds_t.device)
|
|
1486
|
+
local_best = int(torch.argmin(rmsds_t[allowed_t].detach()).item())
|
|
1487
|
+
best = int(allowed_t[local_best].item())
|
|
1488
|
+
total = total + losses_t[best]
|
|
1489
|
+
pos_log = pos_log + rmsds_t[best].detach()
|
|
1490
|
+
y_pred_by_graph[gi] = outputs[best]
|
|
1491
|
+
ref_losses.append(rmsds_t)
|
|
1492
|
+
valid += 1
|
|
1493
|
+
|
|
1494
|
+
if lambda_recall > 0 and ref_losses:
|
|
1495
|
+
# Extra set-cover pressure: every reference should have some generated mode nearby.
|
|
1496
|
+
recall = torch.stack([v.min() for v in ref_losses]).mean()
|
|
1497
|
+
total = total + lambda_recall * recall.clamp(0.0, 10.0)
|
|
1498
|
+
|
|
1499
|
+
if valid == 0:
|
|
1500
|
+
zero = node_feat.sum() * 0.0
|
|
1501
|
+
return zero, zero, y_pred_by_graph
|
|
1502
|
+
return (total / valid).clamp(0.0, 10.0), (pos_log / valid).clamp(0.0, 10.0), y_pred_by_graph
|
|
1503
|
+
|
|
1504
|
+
@torch.no_grad()
|
|
1505
|
+
def _eval_torsion_rmsd_limited(self, loader, max_samples: int = 100, split_name: str = "valid") -> float:
|
|
1506
|
+
self.model.eval()
|
|
1507
|
+
total_rmsd = 0.0
|
|
1508
|
+
total_valid = 0
|
|
1509
|
+
for batch in loader:
|
|
1510
|
+
batch = batch.to(self.device)
|
|
1511
|
+
node_feat = self.model.gnn(batch)
|
|
1512
|
+
graph_feat = self.model.pool(node_feat, batch.batch)
|
|
1513
|
+
groups = self.model._group_graph_indices_by_mol_id(batch, int(batch.num_graphs))
|
|
1514
|
+
for _, graph_indices in groups.items():
|
|
1515
|
+
outputs, _, _, _ = self._predict_group_modes(batch, node_feat, graph_feat, graph_indices)
|
|
1516
|
+
if not outputs:
|
|
1517
|
+
continue
|
|
1518
|
+
for gi in graph_indices:
|
|
1519
|
+
if total_valid >= max_samples:
|
|
1520
|
+
avg = total_rmsd / max(1, total_valid)
|
|
1521
|
+
logger.info(f"RMSD eval {split_name} conformers = {total_valid}, RMSD = {avg:.6f}")
|
|
1522
|
+
return float(avg)
|
|
1523
|
+
mask = batch.batch == gi
|
|
1524
|
+
pos = batch.pos[mask]
|
|
1525
|
+
atom_x = batch.x[mask] if hasattr(batch, "x") else None
|
|
1526
|
+
vals = [safe_heavy_kabsch_rmsd(y, pos, atom_x) for y in outputs if y.shape == pos.shape]
|
|
1527
|
+
if not vals:
|
|
1528
|
+
continue
|
|
1529
|
+
best = torch.stack(vals).min()
|
|
1530
|
+
if torch.isfinite(best):
|
|
1531
|
+
total_rmsd += float(best.detach().cpu())
|
|
1532
|
+
total_valid += 1
|
|
1533
|
+
avg = total_rmsd / max(1, total_valid)
|
|
1534
|
+
logger.info(f"RMSD eval {split_name} conformers = {total_valid}, RMSD = {avg:.6f}")
|
|
1535
|
+
return float(avg)
|
|
1536
|
+
|
|
1537
|
+
@torch.no_grad()
|
|
1538
|
+
def _test_rmsd_limited(self, loader, max_samples: int = 100, split_name: str = "train") -> float:
|
|
1539
|
+
return self._eval_torsion_rmsd_limited(loader, max_samples=max_samples, split_name=split_name)
|
|
1540
|
+
|
|
1541
|
+
@torch.no_grad()
|
|
1542
|
+
def test(self, loader, val: bool) -> float:
|
|
1543
|
+
split_name = "valid" if val else "test"
|
|
1544
|
+
return self._eval_torsion_rmsd_limited(loader, max_samples=100, split_name=split_name)
|
|
1545
|
+
|
|
1546
|
+
|
|
1547
|
+
def main(args=None, optuna_trial=None):
|
|
1548
|
+
if args is None:
|
|
1549
|
+
args = parse_all_args()
|
|
1550
|
+
args = fix_args(args)
|
|
1551
|
+
|
|
1552
|
+
distributed = bool(getattr(args, "distributed_stage2", False))
|
|
1553
|
+
if distributed:
|
|
1554
|
+
if "LOCAL_RANK" not in os.environ:
|
|
1555
|
+
raise RuntimeError("--distributed_stage2 must be launched with torchrun")
|
|
1556
|
+
local_rank = int(os.environ["LOCAL_RANK"])
|
|
1557
|
+
torch.cuda.set_device(local_rank)
|
|
1558
|
+
args.gpu = str(local_rank)
|
|
1559
|
+
dist.init_process_group(backend="nccl")
|
|
1560
|
+
rank = dist.get_rank()
|
|
1561
|
+
args.exp_id = os.path.join(str(getattr(args, "exp_id", "run")), f"rank{rank}")
|
|
1562
|
+
|
|
1563
|
+
try:
|
|
1564
|
+
args = merge_args_from_paths(args, attr_name="paras_path")
|
|
1565
|
+
except Exception:
|
|
1566
|
+
logger.exception("merge_args_from_paths failed, continue with raw args.")
|
|
1567
|
+
args = fix_args(args)
|
|
1568
|
+
|
|
1569
|
+
if args.train_stage != "multimode":
|
|
1570
|
+
raise ValueError("train_torsion_generator.py is an experimental Stage-2 entrypoint; please use --train_stage multimode.")
|
|
1571
|
+
args.deterministic_modes = bool(getattr(args, "deterministic_modes", True))
|
|
1572
|
+
|
|
1573
|
+
initialize_exp(args)
|
|
1574
|
+
set_seed(getattr(args, "random_seed", 0))
|
|
1575
|
+
logger_path = get_dump_path(args)
|
|
1576
|
+
writer = build_writer(logger_path)
|
|
1577
|
+
|
|
1578
|
+
logger.info("=" * 60)
|
|
1579
|
+
logger.info("Experimental Manimol torsion-space Stage-2 training")
|
|
1580
|
+
logger.info(f"train_stage = {args.train_stage}")
|
|
1581
|
+
logger.info(f"metric = {args.metric}")
|
|
1582
|
+
logger.info(f"num_modes = {args.num_modes}")
|
|
1583
|
+
logger.info(f"torsion_mode_max = {args.torsion_mode_max}")
|
|
1584
|
+
logger.info(f"torsion_delta_scale = {args.torsion_delta_scale}")
|
|
1585
|
+
logger.info(f"lambda_torsion_delta_direct = {args.lambda_torsion_delta_direct}")
|
|
1586
|
+
logger.info(f"lambda_torsion_mode_div = {args.lambda_torsion_mode_diversity}")
|
|
1587
|
+
logger.info(f"iterative_torsion_steps = {args.iterative_torsion_steps}")
|
|
1588
|
+
logger.info(f"lambda_tight_rmsd = {args.lambda_tight_rmsd}")
|
|
1589
|
+
logger.info(f"lambda_tight_square = {args.lambda_tight_square}")
|
|
1590
|
+
logger.info(f"tight_rmsd_threshold = {args.tight_rmsd_threshold}")
|
|
1591
|
+
logger.info(f"lambda_pair_distance = {args.lambda_pair_distance}")
|
|
1592
|
+
logger.info(f"lambda_local_pair_distance = {args.lambda_local_pair_distance}")
|
|
1593
|
+
logger.info(f"local_pair_cutoff = {args.local_pair_cutoff}")
|
|
1594
|
+
logger.info(f"lambda_clash = {args.lambda_clash}")
|
|
1595
|
+
logger.info(f"clash_min_distance = {args.clash_min_distance}")
|
|
1596
|
+
logger.info(f"train2_base_noise_prob = {args.train2_base_noise_prob}")
|
|
1597
|
+
logger.info(f"train2_base_noise_scale = {args.train2_base_noise_scale}")
|
|
1598
|
+
logger.info(f"lambda_bond_match = {args.lambda_bond_match}")
|
|
1599
|
+
logger.info(f"lambda_angle_match = {args.lambda_angle_match}")
|
|
1600
|
+
logger.info(f"lambda_chiral_volume_torsion= {args.lambda_chiral_volume_torsion}")
|
|
1601
|
+
logger.info(f"mode_assignment_strategy = {args.mode_assignment_strategy}")
|
|
1602
|
+
logger.info(f"mode_assignment_window = {args.mode_assignment_window}")
|
|
1603
|
+
logger.info(f"vectorized_stage2_loss = {args.vectorized_stage2_loss}")
|
|
1604
|
+
logger.info(f"mode_condition_strategy = {args.mode_condition_strategy}")
|
|
1605
|
+
logger.info(f"mode_prototype_dim = {args.mode_prototype_dim}")
|
|
1606
|
+
logger.info(f"train2_resume_torsion_ckpt = {args.train2_resume_torsion_checkpoint}")
|
|
1607
|
+
logger.info(f"freeze_torsion_mode_decoder = {args.train2_freeze_torsion_mode_decoder}")
|
|
1608
|
+
logger.info(f"train2_use_refiner = {args.train2_use_refiner}")
|
|
1609
|
+
logger.info(f"cuda_available = {torch.cuda.is_available()}")
|
|
1610
|
+
logger.info(f"batch_size = {args.bs}")
|
|
1611
|
+
logger.info(f"num_workers = {args.num_workers}")
|
|
1612
|
+
logger.info(f"pin_memory = {args.pin_memory}")
|
|
1613
|
+
logger.info("=" * 60)
|
|
1614
|
+
|
|
1615
|
+
trainer = TorsionTrainer(args=args, writer=writer, logger_path=logger_path)
|
|
1616
|
+
try:
|
|
1617
|
+
result = trainer.run(optuna_trial=optuna_trial)
|
|
1618
|
+
logger.info("train_torsion_generator torsion-space training finished.")
|
|
1619
|
+
return result
|
|
1620
|
+
finally:
|
|
1621
|
+
try:
|
|
1622
|
+
writer.close()
|
|
1623
|
+
except Exception:
|
|
1624
|
+
pass
|
|
1625
|
+
if distributed and dist.is_initialized():
|
|
1626
|
+
dist.barrier()
|
|
1627
|
+
dist.destroy_process_group()
|
|
1628
|
+
|
|
1629
|
+
|
|
1630
|
+
if __name__ == "__main__":
|
|
1631
|
+
main()
|