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,364 @@
|
|
|
1
|
+
"""Experimental coordinate backbones for train1.py.
|
|
2
|
+
|
|
3
|
+
This file intentionally does not modify the current production model. The
|
|
4
|
+
modules below keep the same decoder/refiner call signatures used by model0.py,
|
|
5
|
+
so train1.py can swap only the Stage-2 geometry backbone while reusing the
|
|
6
|
+
existing Stage-1 probability graph, losses, dataset, and evaluation logic.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Optional, Tuple
|
|
12
|
+
|
|
13
|
+
import torch
|
|
14
|
+
import torch.nn as nn
|
|
15
|
+
import torch.nn.functional as F
|
|
16
|
+
|
|
17
|
+
from .model0 import GNNEncoder, center_and_rescale
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _symmetrize_probability(P: torch.Tensor) -> torch.Tensor:
|
|
21
|
+
n = P.size(0)
|
|
22
|
+
P = torch.nan_to_num(P, nan=0.0, posinf=1.0, neginf=0.0).clamp(1e-6, 1.0 - 1e-6)
|
|
23
|
+
P = 0.5 * (P + P.t())
|
|
24
|
+
return P * (1.0 - torch.eye(n, device=P.device, dtype=P.dtype))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _node_stats_from_P(P: torch.Tensor) -> torch.Tensor:
|
|
28
|
+
degree = P.sum(dim=-1, keepdim=True)
|
|
29
|
+
mean = P.mean(dim=-1, keepdim=True)
|
|
30
|
+
maxv = P.max(dim=-1, keepdim=True).values
|
|
31
|
+
std = P.std(dim=-1, keepdim=True)
|
|
32
|
+
P_safe = P.clamp(1e-6, 1.0 - 1e-6)
|
|
33
|
+
entropy = -(P_safe * torch.log(P_safe)).sum(dim=-1, keepdim=True)
|
|
34
|
+
return torch.cat([degree, mean, maxv, std, entropy], dim=-1)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _edge_attr_matrix(
|
|
38
|
+
n: int,
|
|
39
|
+
edge_index: Optional[torch.Tensor],
|
|
40
|
+
edge_attr: Optional[torch.Tensor],
|
|
41
|
+
edge_dim: int,
|
|
42
|
+
dtype: torch.dtype,
|
|
43
|
+
device: torch.device,
|
|
44
|
+
) -> torch.Tensor:
|
|
45
|
+
out = torch.zeros(n, n, edge_dim, dtype=dtype, device=device)
|
|
46
|
+
if edge_index is None or edge_attr is None or edge_index.numel() == 0:
|
|
47
|
+
return out
|
|
48
|
+
src, dst = edge_index
|
|
49
|
+
if edge_attr.size(-1) != edge_dim:
|
|
50
|
+
usable = min(edge_dim, edge_attr.size(-1))
|
|
51
|
+
out[src, dst, :usable] = edge_attr[:, :usable].to(dtype=dtype)
|
|
52
|
+
out[dst, src, :usable] = edge_attr[:, :usable].to(dtype=dtype)
|
|
53
|
+
else:
|
|
54
|
+
out[src, dst] = edge_attr.to(dtype=dtype)
|
|
55
|
+
out[dst, src] = edge_attr.to(dtype=dtype)
|
|
56
|
+
return out
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class EquivariantTransformerLayer(nn.Module):
|
|
60
|
+
"""Attention-style scalar messages with equivariant coordinate updates.
|
|
61
|
+
|
|
62
|
+
This is a lightweight Transformer alternative to the current probability
|
|
63
|
+
EGNN layer. Attention logits are conditioned on node features, probability
|
|
64
|
+
graph entries P_ij, distances, and optional chemical edge attributes. The
|
|
65
|
+
coordinate update is a weighted sum of relative directions, which keeps
|
|
66
|
+
translation and rotation equivariance.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
def __init__(self, hidden_dim: int, edge_dim: int = 13, num_heads: int = 4):
|
|
70
|
+
super().__init__()
|
|
71
|
+
self.hidden_dim = int(hidden_dim)
|
|
72
|
+
self.edge_dim = int(edge_dim)
|
|
73
|
+
self.num_heads = int(max(1, num_heads))
|
|
74
|
+
if self.hidden_dim % self.num_heads != 0:
|
|
75
|
+
self.num_heads = 1
|
|
76
|
+
self.head_dim = self.hidden_dim // self.num_heads
|
|
77
|
+
|
|
78
|
+
self.q_proj = nn.Linear(hidden_dim, hidden_dim)
|
|
79
|
+
self.k_proj = nn.Linear(hidden_dim, hidden_dim)
|
|
80
|
+
self.v_proj = nn.Linear(hidden_dim, hidden_dim)
|
|
81
|
+
self.edge_bias = nn.Sequential(
|
|
82
|
+
nn.Linear(edge_dim + 2, hidden_dim),
|
|
83
|
+
nn.SiLU(),
|
|
84
|
+
nn.Linear(hidden_dim, self.num_heads),
|
|
85
|
+
)
|
|
86
|
+
self.coord_gate = nn.Sequential(
|
|
87
|
+
nn.Linear(hidden_dim + edge_dim + 2, hidden_dim),
|
|
88
|
+
nn.SiLU(),
|
|
89
|
+
nn.Linear(hidden_dim, self.num_heads),
|
|
90
|
+
nn.Tanh(),
|
|
91
|
+
)
|
|
92
|
+
self.out_proj = nn.Linear(hidden_dim, hidden_dim)
|
|
93
|
+
self.node_mlp = nn.Sequential(
|
|
94
|
+
nn.Linear(2 * hidden_dim, hidden_dim),
|
|
95
|
+
nn.SiLU(),
|
|
96
|
+
nn.Linear(hidden_dim, hidden_dim),
|
|
97
|
+
)
|
|
98
|
+
self.norm = nn.LayerNorm(hidden_dim)
|
|
99
|
+
|
|
100
|
+
def forward(
|
|
101
|
+
self,
|
|
102
|
+
h: torch.Tensor,
|
|
103
|
+
x: torch.Tensor,
|
|
104
|
+
P: torch.Tensor,
|
|
105
|
+
edge_index: Optional[torch.Tensor] = None,
|
|
106
|
+
edge_attr: Optional[torch.Tensor] = None,
|
|
107
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
108
|
+
n = h.size(0)
|
|
109
|
+
device, dtype = h.device, h.dtype
|
|
110
|
+
P = _symmetrize_probability(P)
|
|
111
|
+
eye = torch.eye(n, device=device, dtype=torch.bool)
|
|
112
|
+
|
|
113
|
+
diff = x.unsqueeze(1) - x.unsqueeze(0)
|
|
114
|
+
dist2 = (diff.square().sum(dim=-1, keepdim=True)).clamp(max=100.0)
|
|
115
|
+
dist = torch.sqrt(dist2 + 1e-8)
|
|
116
|
+
direction = diff / (dist + 1e-8)
|
|
117
|
+
edge_mat = _edge_attr_matrix(n, edge_index, edge_attr, self.edge_dim, dtype, device)
|
|
118
|
+
|
|
119
|
+
q = self.q_proj(h).view(n, self.num_heads, self.head_dim)
|
|
120
|
+
k = self.k_proj(h).view(n, self.num_heads, self.head_dim)
|
|
121
|
+
v = self.v_proj(h).view(n, self.num_heads, self.head_dim)
|
|
122
|
+
|
|
123
|
+
logits = torch.einsum("ihd,jhd->ijh", q, k) / (self.head_dim ** 0.5)
|
|
124
|
+
pair_extra = torch.cat([P.unsqueeze(-1), dist], dim=-1)
|
|
125
|
+
logits = logits + self.edge_bias(torch.cat([edge_mat, pair_extra], dim=-1))
|
|
126
|
+
logits = logits.masked_fill(eye.unsqueeze(-1), -1e4)
|
|
127
|
+
attn = F.softmax(logits, dim=1)
|
|
128
|
+
attn = torch.nan_to_num(attn, nan=0.0, posinf=0.0, neginf=0.0)
|
|
129
|
+
|
|
130
|
+
msg = torch.einsum("ijh,jhd->ihd", attn, v).reshape(n, self.hidden_dim)
|
|
131
|
+
msg = self.out_proj(msg)
|
|
132
|
+
h = self.norm(h + self.node_mlp(torch.cat([h, msg], dim=-1)))
|
|
133
|
+
|
|
134
|
+
gate_in = torch.cat(
|
|
135
|
+
[msg.unsqueeze(1).expand(n, n, -1), edge_mat, P.unsqueeze(-1), dist],
|
|
136
|
+
dim=-1,
|
|
137
|
+
)
|
|
138
|
+
coord_gate = self.coord_gate(gate_in).mean(dim=-1, keepdim=True).clamp(-0.03, 0.03)
|
|
139
|
+
coord_weight = attn.mean(dim=-1, keepdim=True).detach()
|
|
140
|
+
dx = (direction * coord_gate * coord_weight).sum(dim=1)
|
|
141
|
+
dx = torch.nan_to_num(dx, nan=0.0, posinf=0.0, neginf=0.0).clamp(-0.08, 0.08)
|
|
142
|
+
return h, x + dx
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class NequipLiteLayer(nn.Module):
|
|
146
|
+
"""A small NequIP-inspired radial/directional message layer.
|
|
147
|
+
|
|
148
|
+
It is not a full spherical-harmonics/e3nn implementation. Instead, it uses
|
|
149
|
+
l=0 radial filters and l=1 relative directions as equivariant vector
|
|
150
|
+
features. This keeps dependencies minimal and makes it suitable as a first
|
|
151
|
+
controlled test before adding a full e3nn/NequIP dependency.
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
def __init__(self, hidden_dim: int, edge_dim: int = 13, num_radial: int = 8):
|
|
155
|
+
super().__init__()
|
|
156
|
+
self.hidden_dim = int(hidden_dim)
|
|
157
|
+
self.edge_dim = int(edge_dim)
|
|
158
|
+
self.num_radial = int(num_radial)
|
|
159
|
+
centers = torch.linspace(0.0, 4.0, self.num_radial)
|
|
160
|
+
self.register_buffer("rbf_centers", centers)
|
|
161
|
+
self.gamma = 4.0
|
|
162
|
+
self.filter_mlp = nn.Sequential(
|
|
163
|
+
nn.Linear(2 * hidden_dim + edge_dim + self.num_radial + 1, hidden_dim),
|
|
164
|
+
nn.SiLU(),
|
|
165
|
+
nn.Linear(hidden_dim, hidden_dim),
|
|
166
|
+
nn.SiLU(),
|
|
167
|
+
)
|
|
168
|
+
self.scalar_gate = nn.Sequential(nn.Linear(hidden_dim, hidden_dim), nn.SiLU(), nn.Linear(hidden_dim, 1))
|
|
169
|
+
self.node_mlp = nn.Sequential(
|
|
170
|
+
nn.Linear(2 * hidden_dim, hidden_dim),
|
|
171
|
+
nn.SiLU(),
|
|
172
|
+
nn.Linear(hidden_dim, hidden_dim),
|
|
173
|
+
)
|
|
174
|
+
self.norm = nn.LayerNorm(hidden_dim)
|
|
175
|
+
|
|
176
|
+
def _rbf(self, dist: torch.Tensor) -> torch.Tensor:
|
|
177
|
+
centers = self.rbf_centers.to(device=dist.device, dtype=dist.dtype).view(1, 1, -1)
|
|
178
|
+
return torch.exp(-self.gamma * (dist - centers).square())
|
|
179
|
+
|
|
180
|
+
def forward(
|
|
181
|
+
self,
|
|
182
|
+
h: torch.Tensor,
|
|
183
|
+
x: torch.Tensor,
|
|
184
|
+
P: torch.Tensor,
|
|
185
|
+
edge_index: Optional[torch.Tensor] = None,
|
|
186
|
+
edge_attr: Optional[torch.Tensor] = None,
|
|
187
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
188
|
+
n = h.size(0)
|
|
189
|
+
device, dtype = h.device, h.dtype
|
|
190
|
+
P = _symmetrize_probability(P)
|
|
191
|
+
eye = torch.eye(n, device=device, dtype=torch.bool)
|
|
192
|
+
|
|
193
|
+
diff = x.unsqueeze(1) - x.unsqueeze(0)
|
|
194
|
+
dist = torch.sqrt(diff.square().sum(dim=-1, keepdim=True) + 1e-8).clamp(max=10.0)
|
|
195
|
+
direction = diff / (dist + 1e-8)
|
|
196
|
+
rbf = self._rbf(dist)
|
|
197
|
+
edge_mat = _edge_attr_matrix(n, edge_index, edge_attr, self.edge_dim, dtype, device)
|
|
198
|
+
hi = h.unsqueeze(1).expand(n, n, -1)
|
|
199
|
+
hj = h.unsqueeze(0).expand(n, n, -1)
|
|
200
|
+
pair = torch.cat([hi, hj, edge_mat, rbf, P.unsqueeze(-1)], dim=-1)
|
|
201
|
+
filt = self.filter_mlp(pair).masked_fill(eye.unsqueeze(-1), 0.0)
|
|
202
|
+
|
|
203
|
+
weights = F.softmax(P / 0.15, dim=1).unsqueeze(-1).detach()
|
|
204
|
+
msg = (filt * weights).sum(dim=1)
|
|
205
|
+
h = self.norm(h + self.node_mlp(torch.cat([h, msg], dim=-1)))
|
|
206
|
+
|
|
207
|
+
gate = self.scalar_gate(filt).clamp(-0.025, 0.025)
|
|
208
|
+
dx = (direction * gate * weights).sum(dim=1)
|
|
209
|
+
dx = torch.nan_to_num(dx, nan=0.0, posinf=0.0, neginf=0.0).clamp(-0.08, 0.08)
|
|
210
|
+
return h, x + dx
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
class ExperimentalConditionedDecoder(nn.Module):
|
|
214
|
+
def __init__(
|
|
215
|
+
self,
|
|
216
|
+
emb_dim: int = 128,
|
|
217
|
+
hidden_dim: int = 128,
|
|
218
|
+
num_layers: int = 2,
|
|
219
|
+
mode_dim: int = 32,
|
|
220
|
+
edge_dim: int = 13,
|
|
221
|
+
backbone: str = "equivariant_transformer",
|
|
222
|
+
num_heads: int = 4,
|
|
223
|
+
):
|
|
224
|
+
super().__init__()
|
|
225
|
+
self.mode_dim = int(mode_dim)
|
|
226
|
+
self.edge_dim = int(edge_dim)
|
|
227
|
+
self.backbone = str(backbone)
|
|
228
|
+
self.z_proj = nn.Sequential(nn.Linear(mode_dim, hidden_dim), nn.SiLU(), nn.Linear(hidden_dim, hidden_dim))
|
|
229
|
+
self.node_proj = nn.Sequential(
|
|
230
|
+
nn.Linear(emb_dim + 5 + hidden_dim, hidden_dim),
|
|
231
|
+
nn.SiLU(),
|
|
232
|
+
nn.Linear(hidden_dim, hidden_dim),
|
|
233
|
+
)
|
|
234
|
+
self.coord_init = nn.Sequential(nn.Linear(hidden_dim, hidden_dim), nn.SiLU(), nn.Linear(hidden_dim, 3))
|
|
235
|
+
self.layers = nn.ModuleList(
|
|
236
|
+
[
|
|
237
|
+
self._make_layer(hidden_dim, edge_dim, backbone=self.backbone, num_heads=num_heads)
|
|
238
|
+
for _ in range(int(num_layers))
|
|
239
|
+
]
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
@staticmethod
|
|
243
|
+
def _make_layer(hidden_dim: int, edge_dim: int, backbone: str, num_heads: int):
|
|
244
|
+
if backbone in {"transformer", "equivariant_transformer"}:
|
|
245
|
+
return EquivariantTransformerLayer(hidden_dim, edge_dim=edge_dim, num_heads=num_heads)
|
|
246
|
+
if backbone in {"nequip", "nequip_lite", "spherical"}:
|
|
247
|
+
return NequipLiteLayer(hidden_dim, edge_dim=edge_dim)
|
|
248
|
+
raise ValueError(f"Unknown experimental geometry backbone: {backbone}")
|
|
249
|
+
|
|
250
|
+
def _node_stats_from_P(self, P: torch.Tensor) -> torch.Tensor:
|
|
251
|
+
"""Compatibility shim for infer_base_conformers.py's optional EGNN refinement path."""
|
|
252
|
+
return _node_stats_from_P(P)
|
|
253
|
+
|
|
254
|
+
def forward(
|
|
255
|
+
self,
|
|
256
|
+
P_global: torch.Tensor,
|
|
257
|
+
node_feat: torch.Tensor,
|
|
258
|
+
z: torch.Tensor,
|
|
259
|
+
edge_index: Optional[torch.Tensor] = None,
|
|
260
|
+
edge_attr: Optional[torch.Tensor] = None,
|
|
261
|
+
) -> torch.Tensor:
|
|
262
|
+
P_global = _symmetrize_probability(P_global)
|
|
263
|
+
n = P_global.size(0)
|
|
264
|
+
z_feat = self.z_proj(z.view(-1)).unsqueeze(0).expand(n, -1)
|
|
265
|
+
h = self.node_proj(torch.cat([node_feat, _node_stats_from_P(P_global), z_feat], dim=-1))
|
|
266
|
+
x = center_and_rescale(self.coord_init(h), target_rms=1.0)
|
|
267
|
+
for layer in self.layers:
|
|
268
|
+
h, x = layer(h, x, P_global, edge_index=edge_index, edge_attr=edge_attr)
|
|
269
|
+
x = center_and_rescale(x, target_rms=1.0)
|
|
270
|
+
return x
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
class ExperimentalGeometryRefiner(nn.Module):
|
|
274
|
+
def __init__(
|
|
275
|
+
self,
|
|
276
|
+
emb_dim: int = 128,
|
|
277
|
+
hidden_dim: int = 128,
|
|
278
|
+
num_steps: int = 4,
|
|
279
|
+
edge_dim: int = 13,
|
|
280
|
+
backbone: str = "equivariant_transformer",
|
|
281
|
+
num_heads: int = 4,
|
|
282
|
+
):
|
|
283
|
+
super().__init__()
|
|
284
|
+
self.num_steps = int(num_steps)
|
|
285
|
+
self.node_proj = nn.Sequential(
|
|
286
|
+
nn.Linear(emb_dim + 3 + 5, hidden_dim),
|
|
287
|
+
nn.SiLU(),
|
|
288
|
+
nn.Linear(hidden_dim, hidden_dim),
|
|
289
|
+
)
|
|
290
|
+
self.layers = nn.ModuleList(
|
|
291
|
+
[
|
|
292
|
+
ExperimentalConditionedDecoder._make_layer(
|
|
293
|
+
hidden_dim,
|
|
294
|
+
edge_dim,
|
|
295
|
+
backbone=backbone,
|
|
296
|
+
num_heads=num_heads,
|
|
297
|
+
)
|
|
298
|
+
for _ in range(self.num_steps)
|
|
299
|
+
]
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
def forward(
|
|
303
|
+
self,
|
|
304
|
+
P: Optional[torch.Tensor] = None,
|
|
305
|
+
node_feat: Optional[torch.Tensor] = None,
|
|
306
|
+
y0: Optional[torch.Tensor] = None,
|
|
307
|
+
edge_index: Optional[torch.Tensor] = None,
|
|
308
|
+
edge_attr: Optional[torch.Tensor] = None,
|
|
309
|
+
coords: Optional[torch.Tensor] = None,
|
|
310
|
+
edge_index_i: Optional[torch.Tensor] = None,
|
|
311
|
+
):
|
|
312
|
+
if y0 is None and coords is not None:
|
|
313
|
+
y0 = coords
|
|
314
|
+
if y0 is None:
|
|
315
|
+
raise ValueError("ExperimentalGeometryRefiner requires y0 or coords")
|
|
316
|
+
n = y0.size(0)
|
|
317
|
+
if P is None:
|
|
318
|
+
P = torch.ones(n, n, device=y0.device, dtype=y0.dtype)
|
|
319
|
+
P = P * (1.0 - torch.eye(n, device=y0.device, dtype=y0.dtype))
|
|
320
|
+
P = _symmetrize_probability(P)
|
|
321
|
+
if node_feat is None:
|
|
322
|
+
node_dim = self.node_proj[0].in_features - 8
|
|
323
|
+
node_feat = torch.zeros(n, node_dim, device=y0.device, dtype=y0.dtype)
|
|
324
|
+
x = center_and_rescale(y0, target_rms=1.0)
|
|
325
|
+
h = self.node_proj(torch.cat([node_feat, x, _node_stats_from_P(P)], dim=-1))
|
|
326
|
+
traj = []
|
|
327
|
+
for layer in self.layers:
|
|
328
|
+
h, x = layer(h, x, P, edge_index=edge_index, edge_attr=edge_attr)
|
|
329
|
+
x = center_and_rescale(x, target_rms=1.0)
|
|
330
|
+
traj.append(x)
|
|
331
|
+
return x, traj
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
class ExperimentalGNNEncoder(GNNEncoder):
|
|
335
|
+
"""GNNEncoder with Stage-2 decoder/refiner replaced by an experimental backbone."""
|
|
336
|
+
|
|
337
|
+
def __init__(self, args, config=None):
|
|
338
|
+
super().__init__(args=args, config=config)
|
|
339
|
+
backbone = getattr(args, "geometry_backbone", "equivariant_transformer")
|
|
340
|
+
emb_dim = int(getattr(args, "emb_dim", 128))
|
|
341
|
+
hidden_dim = int(getattr(args, "mlp_hidden", getattr(args, "coarse_hidden", 128)))
|
|
342
|
+
edge_dim = int(getattr(args, "edge_dim", 13))
|
|
343
|
+
num_refine_steps = int(getattr(args, "num_refine_steps", getattr(args, "refine_steps", 4)))
|
|
344
|
+
num_decoder_layers = int(getattr(args, "decoder_layers", 2))
|
|
345
|
+
num_heads = int(getattr(args, "attention_heads", 4))
|
|
346
|
+
|
|
347
|
+
self.conditioned_decoder = ExperimentalConditionedDecoder(
|
|
348
|
+
emb_dim=emb_dim,
|
|
349
|
+
hidden_dim=hidden_dim,
|
|
350
|
+
num_layers=num_decoder_layers,
|
|
351
|
+
mode_dim=self.mode_dim,
|
|
352
|
+
edge_dim=edge_dim,
|
|
353
|
+
backbone=backbone,
|
|
354
|
+
num_heads=num_heads,
|
|
355
|
+
)
|
|
356
|
+
self.coarse_decoder = self.conditioned_decoder
|
|
357
|
+
self.refiner = ExperimentalGeometryRefiner(
|
|
358
|
+
emb_dim=emb_dim,
|
|
359
|
+
hidden_dim=hidden_dim,
|
|
360
|
+
num_steps=num_refine_steps,
|
|
361
|
+
edge_dim=edge_dim,
|
|
362
|
+
backbone=backbone,
|
|
363
|
+
num_heads=num_heads,
|
|
364
|
+
)
|