File size: 3,463 Bytes
24599ef | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | """The CMFM group's MD frames must be reproducible from the seed.
``seed`` used to control only the protein permutation (a ``torch.Generator``
feeding ``randperm``); which conformations landed in a group came from the
process-global ``np.random`` state. Two runs with identical configs therefore
trained on different frames, so any comparison between loss weights or
normalizers was confounded by a sampling lottery that nothing recorded.
"""
import numpy as np
import pandas as pd
import pytest
from bbflow.data.pdb_dataloader_bbflow import LengthBatcherBBFlow
class _SamplerCfg:
max_conformations = 100
max_batch_size = 8
max_num_res_squared = 1_000_000
def _metadata_csv(num_proteins=4):
return pd.DataFrame({
"modeled_seq_len": [100] * num_proteins,
"num_conformations": [100] * num_proteins,
})
def _batcher(seed=123, rank=0, num_replicas=1, group_size=8):
return LengthBatcherBBFlow(
sampler_cfg=_SamplerCfg(),
metadata_csv=_metadata_csv(),
seed=seed,
shuffle=True,
ensemble_group_size=group_size,
num_replicas=num_replicas,
rank=rank,
)
def _frames_for_epochs(batcher, num_epochs=2):
"""Collect (protein, trajectory, conformation) triples per epoch."""
epochs = []
for _ in range(num_epochs):
rows = np.concatenate(list(iter(batcher)), axis=0)
epochs.append(rows[:, :3].tolist())
return epochs
def test_same_seed_reproduces_the_same_md_frames():
first = _frames_for_epochs(_batcher(seed=123))
second = _frames_for_epochs(_batcher(seed=123))
assert first == second
def test_different_seeds_draw_different_md_frames():
first = _frames_for_epochs(_batcher(seed=123), num_epochs=1)
other = _frames_for_epochs(_batcher(seed=124), num_epochs=1)
assert first != other
def test_frames_advance_across_epochs():
# Determinism must not collapse into replaying one epoch forever.
epochs = _frames_for_epochs(_batcher(seed=123), num_epochs=2)
assert epochs[0] != epochs[1]
def test_replicas_do_not_draw_identical_frames():
# Keying the frame RNG on the seed alone would make every rank train on the
# same conformations, silently cutting the ensemble seen per step.
rank0 = _frames_for_epochs(_batcher(seed=123, rank=0, num_replicas=2), num_epochs=1)
rank1 = _frames_for_epochs(_batcher(seed=123, rank=1, num_replicas=2), num_epochs=1)
assert rank0 != rank1
def test_global_numpy_state_does_not_affect_the_draw():
"""The whole point: no dependence on the ambient np.random stream."""
np.random.seed(0)
first = _frames_for_epochs(_batcher(seed=123), num_epochs=1)
np.random.seed(999)
np.random.random(1000)
second = _frames_for_epochs(_batcher(seed=123), num_epochs=1)
assert first == second
@pytest.mark.parametrize("draw", [0, 1, 2])
def test_augmentation_rounds_draw_fresh_frames(draw):
"""``_create_batches`` loops until it has ``_num_batches``.
Each round must draw new conformations; a rng keyed only on (seed, epoch,
rank) would replay the first round's frames every time.
"""
batcher = _batcher(seed=123)
baseline = np.concatenate(batcher._replica_epoch_batches(0, draw=0), axis=0)
repeat = np.concatenate(batcher._replica_epoch_batches(0, draw=draw), axis=0)
if draw == 0:
assert np.array_equal(baseline, repeat)
else:
assert not np.array_equal(baseline, repeat)
|