Download bbflow_base/tests/test_sampler_frame_determinism.py from hanghang214/data_transfer: direct link, hf CLI and curl.
- Browser
- Download file 3.46 kB
-
https://huggingface.co/datasets/hanghang214/data_transfer/resolve/main/bbflow_base/tests/test_sampler_frame_determinism.py
- Command line
-
hf download hf://datasets/hanghang214/data_transfer/bbflow_base/tests/test_sampler_frame_determinism.py
-
curl -L -o test_sampler_frame_determinism.py https://huggingface.co/datasets/hanghang214/data_transfer/resolve/main/bbflow_base/tests/test_sampler_frame_determinism.py
3.46 kB
| """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 | |
| 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) | |