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,511 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Near-miss local torsion corrector training.
|
|
3
|
+
|
|
4
|
+
This branch directly targets the observed bottleneck: many Manimol candidates
|
|
5
|
+
land in the 0.75-1.25 A band but do not cross the tight 0.75 A threshold. It
|
|
6
|
+
builds train/valid-only pairs:
|
|
7
|
+
|
|
8
|
+
generated near-miss candidate -> nearest reference conformer
|
|
9
|
+
|
|
10
|
+
and fine-tunes the denoiser to predict the local torsion correction. Test
|
|
11
|
+
references are never used.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import argparse
|
|
17
|
+
import os
|
|
18
|
+
import random
|
|
19
|
+
from argparse import Namespace
|
|
20
|
+
from typing import Dict, List
|
|
21
|
+
|
|
22
|
+
import torch
|
|
23
|
+
from torch_geometric.loader import DataLoader
|
|
24
|
+
from tqdm import tqdm
|
|
25
|
+
|
|
26
|
+
import infer_base_conformers
|
|
27
|
+
import infer_torsional_denoiser
|
|
28
|
+
from dataset import QM9Dataset
|
|
29
|
+
from models.model0 import (
|
|
30
|
+
_torsion_rows,
|
|
31
|
+
apply_torsion_residuals,
|
|
32
|
+
periodic_angle_delta,
|
|
33
|
+
safe_heavy_kabsch_rmsd,
|
|
34
|
+
torsion_angles,
|
|
35
|
+
)
|
|
36
|
+
from train_torsional_denoiser import NoiseTorsionDenoiser, graph_forward, load_base_model, local_torsion_index, noised_conformer
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def parse_args():
|
|
40
|
+
p = argparse.ArgumentParser(description="Train denoiser as a near-miss local torsion corrector")
|
|
41
|
+
p.add_argument("--fixed_dump_name", action="store_true", help="Accepted for YAML compatibility; ignored.")
|
|
42
|
+
p.add_argument("--dataset", type=str, default="Drugs")
|
|
43
|
+
p.add_argument("--data_root", type=str, default="data")
|
|
44
|
+
p.add_argument("--raw_prefix", type=str, default="geom_drugs")
|
|
45
|
+
p.add_argument("--base_checkpoint", type=str, required=True)
|
|
46
|
+
p.add_argument("--denoiser_checkpoint", type=str, required=True)
|
|
47
|
+
p.add_argument("--output_dir", type=str, default="dump/drugs_torsion_limit/drugs_stage2_torsion_denoise_nearmiss_limit200")
|
|
48
|
+
p.add_argument("--cache_path", type=str, default="")
|
|
49
|
+
p.add_argument("--device", type=str, default="auto")
|
|
50
|
+
p.add_argument("--max_confs", type=int, default=5,
|
|
51
|
+
help="Stage-I capacity; 0 reads it from --base_checkpoint.")
|
|
52
|
+
p.add_argument("--epochs", type=int, default=25)
|
|
53
|
+
p.add_argument("--batch_size", type=int, default=8)
|
|
54
|
+
p.add_argument("--num_workers", type=int, default=0)
|
|
55
|
+
p.add_argument("--lr", type=float, default=2e-4)
|
|
56
|
+
p.add_argument("--seed", type=int, default=0)
|
|
57
|
+
p.add_argument("--limit_train_molecules", type=int, default=200)
|
|
58
|
+
p.add_argument("--limit_valid_molecules", type=int, default=50)
|
|
59
|
+
|
|
60
|
+
p.add_argument("--sample_multiplier", type=float, default=2.0)
|
|
61
|
+
p.add_argument("--pseudo_oversample_factor", type=float, default=12.0)
|
|
62
|
+
p.add_argument("--pseudo_per_mol", type=int, default=16)
|
|
63
|
+
p.add_argument("--targets_per_mol_per_epoch", type=int, default=3)
|
|
64
|
+
p.add_argument("--near_miss_min", type=float, default=0.72)
|
|
65
|
+
p.add_argument("--near_miss_max", type=float, default=1.25)
|
|
66
|
+
p.add_argument("--oracle_threshold", type=float, default=0.75, help="Deprecated compatibility arg; ignored.")
|
|
67
|
+
p.add_argument("--num_prototypes", type=int, default=0, help="Deprecated compatibility arg; ignored.")
|
|
68
|
+
p.add_argument("--prototype_dim", type=int, default=0, help="Deprecated compatibility arg; ignored.")
|
|
69
|
+
p.add_argument(
|
|
70
|
+
"--fallback_best_per_mol",
|
|
71
|
+
type=int,
|
|
72
|
+
default=4,
|
|
73
|
+
help="If too few near-miss samples exist, add this many best candidates for the molecule.",
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
p.add_argument("--hidden_dim", type=int, default=128)
|
|
77
|
+
p.add_argument("--max_delta", type=float, default=0.8)
|
|
78
|
+
p.add_argument("--denoiser_backbone", type=str, default="mlp", choices=["mlp", "transformer"])
|
|
79
|
+
p.add_argument("--transformer_layers", type=int, default=2)
|
|
80
|
+
p.add_argument("--transformer_heads", type=int, default=4)
|
|
81
|
+
p.add_argument("--transformer_dropout", type=float, default=0.0)
|
|
82
|
+
p.add_argument("--torsion_mode_max", type=int, default=64)
|
|
83
|
+
|
|
84
|
+
p.add_argument("--denoise_steps", type=int, default=10)
|
|
85
|
+
p.add_argument("--denoise_sigma_max", type=float, default=0.75)
|
|
86
|
+
p.add_argument("--denoise_sigma_min", type=float, default=0.05)
|
|
87
|
+
p.add_argument("--denoise_step_scale", type=float, default=1.0)
|
|
88
|
+
p.add_argument("--denoise_initial_noise", type=float, default=0.75)
|
|
89
|
+
p.add_argument("--torsion_base_method", type=str, default="etkdg")
|
|
90
|
+
p.add_argument("--torsion_base_confs", type=int, default=0)
|
|
91
|
+
|
|
92
|
+
p.add_argument("--noise_min", type=float, default=0.03)
|
|
93
|
+
p.add_argument("--noise_max", type=float, default=0.45)
|
|
94
|
+
p.add_argument("--bridge_noise_min", type=float, default=0.05)
|
|
95
|
+
p.add_argument("--bridge_noise_max", type=float, default=0.65)
|
|
96
|
+
p.add_argument("--local_reconstruct_weight", type=float, default=0.15)
|
|
97
|
+
p.add_argument("--bridge_weight", type=float, default=1.4)
|
|
98
|
+
p.add_argument(
|
|
99
|
+
"--direct_bridge_weight",
|
|
100
|
+
type=float,
|
|
101
|
+
default=0.0,
|
|
102
|
+
help=(
|
|
103
|
+
"Extra loss that starts exactly from a generated candidate and "
|
|
104
|
+
"unrolls the denoiser for several small post-correction steps."
|
|
105
|
+
),
|
|
106
|
+
)
|
|
107
|
+
p.add_argument("--direct_unroll_steps", type=int, default=2)
|
|
108
|
+
p.add_argument("--direct_sigma", type=float, default=0.015)
|
|
109
|
+
p.add_argument("--direct_step_scale", type=float, default=0.22)
|
|
110
|
+
p.add_argument("--lambda_rmsd", type=float, default=1.2)
|
|
111
|
+
p.add_argument("--lambda_torsion", type=float, default=0.4)
|
|
112
|
+
p.add_argument("--lambda_tight", type=float, default=1.0)
|
|
113
|
+
p.add_argument("--tight_threshold", type=float, default=0.75)
|
|
114
|
+
return p.parse_args()
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def resolve_device(device_name: str) -> torch.device:
|
|
118
|
+
if str(device_name).lower() == "auto":
|
|
119
|
+
return torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
120
|
+
return torch.device(device_name)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def make_sampling_args(args) -> Namespace:
|
|
124
|
+
return Namespace(
|
|
125
|
+
num_modes=10,
|
|
126
|
+
max_confs=int(args.max_confs),
|
|
127
|
+
deterministic_modes=True,
|
|
128
|
+
denoise_steps=int(args.denoise_steps),
|
|
129
|
+
denoise_sigma_max=float(args.denoise_sigma_max),
|
|
130
|
+
denoise_sigma_min=float(args.denoise_sigma_min),
|
|
131
|
+
denoise_step_scale=float(args.denoise_step_scale),
|
|
132
|
+
denoise_initial_noise=float(args.denoise_initial_noise),
|
|
133
|
+
torsion_mode_max=int(args.torsion_mode_max),
|
|
134
|
+
torsion_base_method=str(args.torsion_base_method),
|
|
135
|
+
torsion_base_confs=int(args.torsion_base_confs),
|
|
136
|
+
use_etkdg=True,
|
|
137
|
+
etkdg_candidate_pool=False,
|
|
138
|
+
use_mmff=False,
|
|
139
|
+
use_torsion_refine=False,
|
|
140
|
+
use_egnn_refine=False,
|
|
141
|
+
mirror_ratio=0.0,
|
|
142
|
+
mirror_axis="x",
|
|
143
|
+
selection_metric="centered_rmsd",
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def load_old_denoise_wrapper(args, device):
|
|
148
|
+
exp_args = Namespace(
|
|
149
|
+
base_checkpoint=str(args.base_checkpoint),
|
|
150
|
+
denoiser_checkpoint=str(args.denoiser_checkpoint),
|
|
151
|
+
)
|
|
152
|
+
wrapper = infer_torsional_denoiser.patched_load_model_factory(exp_args)(
|
|
153
|
+
str(args.base_checkpoint),
|
|
154
|
+
device,
|
|
155
|
+
make_sampling_args(args),
|
|
156
|
+
)
|
|
157
|
+
wrapper.num_modes = 10
|
|
158
|
+
return wrapper
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def build_ref_groups(args, split: str, dataset, subset):
|
|
162
|
+
ref_path = infer_base_conformers.raw_reference_path(
|
|
163
|
+
Namespace(raw_reference_file=None, data_root=args.data_root, raw_prefix=args.raw_prefix, split=split),
|
|
164
|
+
dataset,
|
|
165
|
+
)
|
|
166
|
+
return infer_base_conformers.build_raw_reference_groups(ref_path, subset)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def select_nearmiss_pairs(
|
|
170
|
+
pred_confs,
|
|
171
|
+
ref_confs,
|
|
172
|
+
heavy_mask,
|
|
173
|
+
near_min,
|
|
174
|
+
near_max,
|
|
175
|
+
pseudo_per_mol,
|
|
176
|
+
fallback_best_per_mol,
|
|
177
|
+
):
|
|
178
|
+
rmsd = infer_base_conformers.compute_rmsd_matrix(pred_confs, ref_confs, heavy_mask=heavy_mask)
|
|
179
|
+
nearest = rmsd.min(dim=1).values
|
|
180
|
+
nearest_ref = torch.argmin(rmsd, dim=1)
|
|
181
|
+
mask = (nearest >= float(near_min)) & (nearest <= float(near_max))
|
|
182
|
+
idx = torch.nonzero(mask, as_tuple=False).view(-1)
|
|
183
|
+
if idx.numel() > 0:
|
|
184
|
+
# Prefer candidates just outside the tight threshold, not very loose
|
|
185
|
+
# ones. This keeps the correction local and learnable.
|
|
186
|
+
target_band = torch.abs(nearest[idx] - float(near_min))
|
|
187
|
+
order = idx[torch.argsort(target_band)]
|
|
188
|
+
else:
|
|
189
|
+
order = torch.argsort(nearest)[: max(0, int(fallback_best_per_mol))]
|
|
190
|
+
if order.numel() == 0:
|
|
191
|
+
return []
|
|
192
|
+
keep = order[: min(int(pseudo_per_mol), int(order.numel()))]
|
|
193
|
+
pairs = []
|
|
194
|
+
for cand_idx in keep.detach().cpu().tolist():
|
|
195
|
+
cand_idx = int(cand_idx)
|
|
196
|
+
ref_idx = int(nearest_ref[cand_idx].detach().cpu().item())
|
|
197
|
+
pairs.append(
|
|
198
|
+
{
|
|
199
|
+
"start": pred_confs[cand_idx].detach().cpu().float(),
|
|
200
|
+
"target": ref_confs[ref_idx].detach().cpu().float(),
|
|
201
|
+
"rmsd": float(nearest[cand_idx].detach().cpu().item()),
|
|
202
|
+
}
|
|
203
|
+
)
|
|
204
|
+
return pairs
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def build_pseudo_bank(args, split: str, limit_molecules: int, device: torch.device) -> Dict[str, List[dict]]:
|
|
208
|
+
dataset = QM9Dataset(name=args.dataset, root=args.data_root, max_confs=int(args.max_confs), raw_prefix=args.raw_prefix)
|
|
209
|
+
subset = dataset[infer_base_conformers.get_split_indices(dataset, split)]
|
|
210
|
+
ref_groups = build_ref_groups(args, split, dataset, subset)
|
|
211
|
+
loader = DataLoader(subset, batch_size=1, shuffle=False, num_workers=0)
|
|
212
|
+
old_model = load_old_denoise_wrapper(args, device)
|
|
213
|
+
sampling_args = make_sampling_args(args)
|
|
214
|
+
bank: Dict[str, List[dict]] = {}
|
|
215
|
+
processed = set()
|
|
216
|
+
|
|
217
|
+
for batch in tqdm(loader, desc=f"Build pseudo {split} bank"):
|
|
218
|
+
batch = batch.to(device)
|
|
219
|
+
key = infer_base_conformers.get_batch_mol_key(batch, 0)
|
|
220
|
+
if key in processed:
|
|
221
|
+
continue
|
|
222
|
+
ref_group = ref_groups.get(key)
|
|
223
|
+
if ref_group is None:
|
|
224
|
+
continue
|
|
225
|
+
ref_count = len(ref_group["confs"])
|
|
226
|
+
target_count = max(1, int(torch.ceil(torch.tensor(float(args.sample_multiplier) * ref_count)).item()))
|
|
227
|
+
gen_count = max(target_count, int(torch.ceil(torch.tensor(target_count * float(args.pseudo_oversample_factor))).item()))
|
|
228
|
+
sampled = infer_torsional_denoiser.sample_denoise_conformers(
|
|
229
|
+
model=old_model,
|
|
230
|
+
batch=batch,
|
|
231
|
+
graph_idx=0,
|
|
232
|
+
args=sampling_args,
|
|
233
|
+
model_count=gen_count,
|
|
234
|
+
etkdg_count=0,
|
|
235
|
+
)
|
|
236
|
+
if sampled is None:
|
|
237
|
+
continue
|
|
238
|
+
pred_confs = sampled[0].detach().cpu().float()
|
|
239
|
+
ref_confs = torch.stack(ref_group["confs"], dim=0).float()
|
|
240
|
+
pairs = select_nearmiss_pairs(
|
|
241
|
+
pred_confs=pred_confs,
|
|
242
|
+
ref_confs=ref_confs,
|
|
243
|
+
heavy_mask=ref_group.get("heavy_mask"),
|
|
244
|
+
near_min=float(args.near_miss_min),
|
|
245
|
+
near_max=float(args.near_miss_max),
|
|
246
|
+
pseudo_per_mol=int(args.pseudo_per_mol),
|
|
247
|
+
fallback_best_per_mol=int(args.fallback_best_per_mol),
|
|
248
|
+
)
|
|
249
|
+
if pairs:
|
|
250
|
+
bank[key] = pairs
|
|
251
|
+
processed.add(key)
|
|
252
|
+
if int(limit_molecules) > 0 and len(processed) >= int(limit_molecules):
|
|
253
|
+
break
|
|
254
|
+
if not bank:
|
|
255
|
+
raise RuntimeError(f"No near-miss pairs built for split={split}")
|
|
256
|
+
return bank
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def load_or_build_banks(args, device):
|
|
260
|
+
if args.cache_path and os.path.exists(args.cache_path):
|
|
261
|
+
cache = torch.load(args.cache_path, map_location="cpu")
|
|
262
|
+
print(f"Loaded near-miss pair cache: {args.cache_path}")
|
|
263
|
+
return cache["train"], cache["valid"]
|
|
264
|
+
train_bank = build_pseudo_bank(args, "train", int(args.limit_train_molecules), device)
|
|
265
|
+
valid_bank = build_pseudo_bank(args, "valid", int(args.limit_valid_molecules), device)
|
|
266
|
+
if args.cache_path:
|
|
267
|
+
os.makedirs(os.path.dirname(args.cache_path), exist_ok=True)
|
|
268
|
+
torch.save({"train": train_bank, "valid": valid_bank}, args.cache_path)
|
|
269
|
+
print(f"Saved near-miss pair cache: {args.cache_path}")
|
|
270
|
+
return train_bank, valid_bank
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def pseudo_batch_loss(args, base_model, denoiser, batch, bank, train=True):
|
|
274
|
+
total = batch.pos.new_tensor(0.0)
|
|
275
|
+
valid = 0
|
|
276
|
+
|
|
277
|
+
def one_step_loss(start, target, sigma, feat_i, graph_feat_i, edge_i, P, torsion_index, rows_all, atom_x):
|
|
278
|
+
noisy, _ = noised_conformer(start, torsion_index, edge_i, sigma, int(args.torsion_mode_max))
|
|
279
|
+
delta = denoiser(
|
|
280
|
+
node_feat=feat_i,
|
|
281
|
+
graph_feat=graph_feat_i,
|
|
282
|
+
coords=noisy,
|
|
283
|
+
P=P,
|
|
284
|
+
torsion_index=torsion_index,
|
|
285
|
+
sigma=sigma,
|
|
286
|
+
max_torsions=int(args.torsion_mode_max),
|
|
287
|
+
)
|
|
288
|
+
pred = apply_torsion_residuals(noisy, torsion_index, delta, edge_index=edge_i, max_torsions=int(args.torsion_mode_max))
|
|
289
|
+
rmsd = safe_heavy_kabsch_rmsd(pred, target, atom_x).clamp(0.0, 10.0)
|
|
290
|
+
rows = rows_all[: min(rows_all.size(0), delta.size(0))].to(target.device).long()
|
|
291
|
+
current_t = torsion_angles(noisy, rows)
|
|
292
|
+
target_t = torsion_angles(target, rows).detach()
|
|
293
|
+
target_delta = periodic_angle_delta(target_t, current_t)
|
|
294
|
+
torsion_loss = (1.0 - torch.cos(delta[: rows.size(0)] - target_delta)).mean().clamp(0.0, 10.0)
|
|
295
|
+
tight = torch.relu(rmsd - rmsd.new_tensor(float(args.tight_threshold)))
|
|
296
|
+
return float(args.lambda_rmsd) * rmsd + float(args.lambda_torsion) * torsion_loss + float(args.lambda_tight) * tight * tight
|
|
297
|
+
|
|
298
|
+
def unrolled_bridge_loss(start, target, feat_i, graph_feat_i, edge_i, P, torsion_index, rows_all, atom_x):
|
|
299
|
+
y = start
|
|
300
|
+
deltas = []
|
|
301
|
+
steps = max(1, int(args.direct_unroll_steps))
|
|
302
|
+
for _ in range(steps):
|
|
303
|
+
delta = denoiser(
|
|
304
|
+
node_feat=feat_i,
|
|
305
|
+
graph_feat=graph_feat_i,
|
|
306
|
+
coords=y,
|
|
307
|
+
P=P,
|
|
308
|
+
torsion_index=torsion_index,
|
|
309
|
+
sigma=float(args.direct_sigma),
|
|
310
|
+
max_torsions=int(args.torsion_mode_max),
|
|
311
|
+
)
|
|
312
|
+
deltas.append(delta)
|
|
313
|
+
y = apply_torsion_residuals(
|
|
314
|
+
y,
|
|
315
|
+
torsion_index,
|
|
316
|
+
delta * float(args.direct_step_scale),
|
|
317
|
+
edge_index=edge_i,
|
|
318
|
+
max_torsions=int(args.torsion_mode_max),
|
|
319
|
+
)
|
|
320
|
+
rmsd = safe_heavy_kabsch_rmsd(y, target, atom_x).clamp(0.0, 10.0)
|
|
321
|
+
rows = rows_all[: min(rows_all.size(0), deltas[-1].size(0))].to(target.device).long()
|
|
322
|
+
current_t = torsion_angles(start, rows)
|
|
323
|
+
target_t = torsion_angles(target, rows).detach()
|
|
324
|
+
target_delta = periodic_angle_delta(target_t, current_t)
|
|
325
|
+
pred_total = torch.stack([d[: rows.size(0)] for d in deltas], dim=0).sum(dim=0) * float(args.direct_step_scale)
|
|
326
|
+
torsion_loss = (1.0 - torch.cos(pred_total - target_delta)).mean().clamp(0.0, 10.0)
|
|
327
|
+
tight = torch.relu(rmsd - rmsd.new_tensor(float(args.tight_threshold)))
|
|
328
|
+
# The direct bridge is meant to improve precision without destroying
|
|
329
|
+
# already good candidates. The tight hinge puts extra pressure exactly
|
|
330
|
+
# around the <0.75 A target.
|
|
331
|
+
return (
|
|
332
|
+
float(args.lambda_rmsd) * rmsd
|
|
333
|
+
+ float(args.lambda_torsion) * torsion_loss
|
|
334
|
+
+ float(args.lambda_tight) * tight * tight
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
for graph_idx in range(batch.num_graphs):
|
|
338
|
+
key = infer_base_conformers.get_batch_mol_key(batch, graph_idx)
|
|
339
|
+
pairs = bank.get(key)
|
|
340
|
+
if not pairs:
|
|
341
|
+
continue
|
|
342
|
+
mask = batch.batch == graph_idx
|
|
343
|
+
atom_x = batch.x[mask] if hasattr(batch, "x") else None
|
|
344
|
+
torsion_index = local_torsion_index(base_model, batch, graph_idx)
|
|
345
|
+
rows_all = _torsion_rows(torsion_index)
|
|
346
|
+
if rows_all is None or rows_all.numel() == 0:
|
|
347
|
+
continue
|
|
348
|
+
feat_i, graph_feat_i, edge_i, P, _ = graph_forward(base_model, batch, graph_idx)
|
|
349
|
+
n_targets = min(int(args.targets_per_mol_per_epoch), len(pairs))
|
|
350
|
+
chosen = random.sample(pairs, n_targets) if train and len(pairs) > n_targets else pairs[:n_targets]
|
|
351
|
+
for pair in chosen:
|
|
352
|
+
if isinstance(pair, dict):
|
|
353
|
+
start_cpu = pair["start"]
|
|
354
|
+
target_cpu = pair["target"]
|
|
355
|
+
else:
|
|
356
|
+
# Backward-compatible fallback for old caches: treat entry as
|
|
357
|
+
# both start and target.
|
|
358
|
+
start_cpu = pair
|
|
359
|
+
target_cpu = pair
|
|
360
|
+
start = start_cpu.to(device=batch.pos.device, dtype=batch.pos.dtype)
|
|
361
|
+
target = target_cpu.to(device=batch.pos.device, dtype=batch.pos.dtype)
|
|
362
|
+
if target.size(0) != int(mask.sum().item()):
|
|
363
|
+
continue
|
|
364
|
+
if start.size(0) != target.size(0):
|
|
365
|
+
start = batch.pos[mask]
|
|
366
|
+
|
|
367
|
+
if float(args.local_reconstruct_weight) > 0.0:
|
|
368
|
+
sigma = float(torch.empty((), device=batch.pos.device).uniform_(float(args.noise_min), float(args.noise_max)).item())
|
|
369
|
+
total = total + float(args.local_reconstruct_weight) * one_step_loss(
|
|
370
|
+
start=target,
|
|
371
|
+
target=target,
|
|
372
|
+
sigma=sigma,
|
|
373
|
+
feat_i=feat_i,
|
|
374
|
+
graph_feat_i=graph_feat_i,
|
|
375
|
+
edge_i=edge_i,
|
|
376
|
+
P=P,
|
|
377
|
+
torsion_index=torsion_index,
|
|
378
|
+
rows_all=rows_all,
|
|
379
|
+
atom_x=atom_x,
|
|
380
|
+
)
|
|
381
|
+
valid += 1
|
|
382
|
+
|
|
383
|
+
if float(args.bridge_weight) > 0.0:
|
|
384
|
+
sigma = float(torch.empty((), device=batch.pos.device).uniform_(float(args.bridge_noise_min), float(args.bridge_noise_max)).item())
|
|
385
|
+
total = total + float(args.bridge_weight) * one_step_loss(
|
|
386
|
+
start=start,
|
|
387
|
+
target=target,
|
|
388
|
+
sigma=sigma,
|
|
389
|
+
feat_i=feat_i,
|
|
390
|
+
graph_feat_i=graph_feat_i,
|
|
391
|
+
edge_i=edge_i,
|
|
392
|
+
P=P,
|
|
393
|
+
torsion_index=torsion_index,
|
|
394
|
+
rows_all=rows_all,
|
|
395
|
+
atom_x=atom_x,
|
|
396
|
+
)
|
|
397
|
+
valid += 1
|
|
398
|
+
if float(args.direct_bridge_weight) > 0.0:
|
|
399
|
+
total = total + float(args.direct_bridge_weight) * unrolled_bridge_loss(
|
|
400
|
+
start=start,
|
|
401
|
+
target=target,
|
|
402
|
+
feat_i=feat_i,
|
|
403
|
+
graph_feat_i=graph_feat_i,
|
|
404
|
+
edge_i=edge_i,
|
|
405
|
+
P=P,
|
|
406
|
+
torsion_index=torsion_index,
|
|
407
|
+
rows_all=rows_all,
|
|
408
|
+
atom_x=atom_x,
|
|
409
|
+
)
|
|
410
|
+
valid += 1
|
|
411
|
+
if valid == 0:
|
|
412
|
+
return batch.pos.sum() * 0.0, 0, 0.0
|
|
413
|
+
return total / valid, valid, float(total.detach().cpu().item()) / valid
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def run_epoch(args, base_model, denoiser, loader, bank, opt=None, desc="train"):
|
|
417
|
+
train = opt is not None
|
|
418
|
+
denoiser.train(train)
|
|
419
|
+
total_loss = 0.0
|
|
420
|
+
total_valid = 0
|
|
421
|
+
seen_keys = set()
|
|
422
|
+
bank_keys = set(bank.keys())
|
|
423
|
+
for batch in tqdm(loader, desc=desc):
|
|
424
|
+
batch = batch.to(args.device)
|
|
425
|
+
batch_has_key = False
|
|
426
|
+
for graph_idx in range(batch.num_graphs):
|
|
427
|
+
key = infer_base_conformers.get_batch_mol_key(batch, graph_idx)
|
|
428
|
+
if key in bank_keys:
|
|
429
|
+
batch_has_key = True
|
|
430
|
+
seen_keys.add(key)
|
|
431
|
+
if not batch_has_key:
|
|
432
|
+
if bank_keys and len(seen_keys) >= len(bank_keys):
|
|
433
|
+
break
|
|
434
|
+
continue
|
|
435
|
+
loss, valid, loss_value = pseudo_batch_loss(args, base_model, denoiser, batch, bank, train=train)
|
|
436
|
+
if valid == 0:
|
|
437
|
+
if bank_keys and len(seen_keys) >= len(bank_keys):
|
|
438
|
+
break
|
|
439
|
+
continue
|
|
440
|
+
if train:
|
|
441
|
+
opt.zero_grad(set_to_none=True)
|
|
442
|
+
loss.backward()
|
|
443
|
+
torch.nn.utils.clip_grad_norm_(denoiser.parameters(), 3.0)
|
|
444
|
+
opt.step()
|
|
445
|
+
total_loss += loss_value * valid
|
|
446
|
+
total_valid += valid
|
|
447
|
+
if bank_keys and len(seen_keys) >= len(bank_keys):
|
|
448
|
+
break
|
|
449
|
+
return total_loss / max(1, total_valid), total_valid
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def main():
|
|
453
|
+
args = parse_args()
|
|
454
|
+
if int(args.max_confs) == 0:
|
|
455
|
+
args.max_confs = infer_base_conformers.checkpoint_max_confs(args.base_checkpoint)
|
|
456
|
+
print(f"Auto-resolved max_confs={args.max_confs} from base checkpoint.")
|
|
457
|
+
random.seed(int(args.seed))
|
|
458
|
+
torch.manual_seed(int(args.seed))
|
|
459
|
+
device = resolve_device(args.device)
|
|
460
|
+
args.device = str(device)
|
|
461
|
+
print(f"device={device}")
|
|
462
|
+
|
|
463
|
+
train_bank, valid_bank = load_or_build_banks(args, device)
|
|
464
|
+
print(f"near-miss pair molecules: train={len(train_bank)}, valid={len(valid_bank)}")
|
|
465
|
+
|
|
466
|
+
dataset = QM9Dataset(name=args.dataset, root=args.data_root, max_confs=int(args.max_confs), raw_prefix=args.raw_prefix)
|
|
467
|
+
train_subset = dataset[infer_base_conformers.get_split_indices(dataset, "train")]
|
|
468
|
+
valid_subset = dataset[infer_base_conformers.get_split_indices(dataset, "valid")]
|
|
469
|
+
train_loader = DataLoader(train_subset, batch_size=int(args.batch_size), shuffle=True, num_workers=int(args.num_workers))
|
|
470
|
+
valid_loader = DataLoader(valid_subset, batch_size=int(args.batch_size), shuffle=False, num_workers=int(args.num_workers))
|
|
471
|
+
|
|
472
|
+
base_args = Namespace(checkpoint=args.base_checkpoint, max_confs=args.max_confs)
|
|
473
|
+
base_model = load_base_model(base_args, device)
|
|
474
|
+
denoiser = infer_torsional_denoiser.load_denoiser(args.denoiser_checkpoint, device)
|
|
475
|
+
opt = torch.optim.AdamW(denoiser.parameters(), lr=float(args.lr), weight_decay=1e-4)
|
|
476
|
+
|
|
477
|
+
os.makedirs(args.output_dir, exist_ok=True)
|
|
478
|
+
best = float("inf")
|
|
479
|
+
for epoch in range(int(args.epochs)):
|
|
480
|
+
tr, tr_valid = run_epoch(args, base_model, denoiser, train_loader, train_bank, opt=opt, desc=f"Train E[{epoch}]")
|
|
481
|
+
va, va_valid = run_epoch(args, base_model, denoiser, valid_loader, valid_bank, opt=None, desc=f"Valid E[{epoch}]")
|
|
482
|
+
print(f"epoch={epoch:03d} train_loss={tr:.5f} train_valid={tr_valid} valid_loss={va:.5f} valid_valid={va_valid}")
|
|
483
|
+
if va < best:
|
|
484
|
+
best = va
|
|
485
|
+
path = os.path.join(args.output_dir, "best.pth")
|
|
486
|
+
torch.save(
|
|
487
|
+
{
|
|
488
|
+
"model_state_dict": {k: v.detach().cpu().clone() for k, v in denoiser.state_dict().items()},
|
|
489
|
+
"hidden_dim": int(args.hidden_dim),
|
|
490
|
+
"max_delta": float(args.max_delta),
|
|
491
|
+
"denoiser_backbone": str(args.denoiser_backbone),
|
|
492
|
+
"transformer_layers": int(args.transformer_layers),
|
|
493
|
+
"transformer_heads": int(args.transformer_heads),
|
|
494
|
+
"transformer_dropout": float(args.transformer_dropout),
|
|
495
|
+
"torsion_mode_max": int(args.torsion_mode_max),
|
|
496
|
+
"source_checkpoint": args.denoiser_checkpoint,
|
|
497
|
+
"near_miss_correction": {
|
|
498
|
+
"near_miss_min": float(args.near_miss_min),
|
|
499
|
+
"near_miss_max": float(args.near_miss_max),
|
|
500
|
+
"pseudo_oversample_factor": float(args.pseudo_oversample_factor),
|
|
501
|
+
"pseudo_per_mol": int(args.pseudo_per_mol),
|
|
502
|
+
"fallback_best_per_mol": int(args.fallback_best_per_mol),
|
|
503
|
+
},
|
|
504
|
+
},
|
|
505
|
+
path,
|
|
506
|
+
)
|
|
507
|
+
print(f"New best valid {best:.5f}; saved {path}")
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
if __name__ == "__main__":
|
|
511
|
+
main()
|