File size: 3,743 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
108
109
import torch

from scripts.probe_condition_quality_vs_md import build_condition_batch


class _FakeLatent:
    def __init__(self):
        self.sample_batch_sizes = []

    def sample_prior_z(self, trans, rot, seq, mask):
        batch, num_res = trans.shape[:2]
        self.sample_batch_sizes.append(batch)
        coeff = torch.randn(batch, 2, device=trans.device)
        node = coeff[:, None, :].repeat(1, num_res, 1)
        edge = torch.einsum("bik,bjk->bijk", node, node)
        disp = torch.cat([coeff, coeff[:, :1]], dim=-1)[:, None, :].repeat(1, num_res, 1)
        disp_edge = torch.einsum("bid,bjd->bij", disp, disp)[..., None]
        return {
            "mode_coefficients": coeff,
            "node_cond": node,
            "edge_cond": edge,
            "disp_node_cond": disp,
            "disp_edge_cond": disp_edge,
        }

    @staticmethod
    def repeat_condition(condition, batch_size):
        return {
            key: value.repeat(batch_size, *([1] * (value.dim() - 1)))
            if torch.is_tensor(value)
            else value
            for key, value in condition.items()
        }

    def forward_prior(self, trans, rot, seq, mask):
        batch, num_res = trans.shape[:2]
        return {
            "node_cond": torch.ones(batch, num_res, 2),
            "edge_cond": torch.ones(batch, num_res, num_res, 2),
            "disp_node_cond": torch.ones(batch, num_res, 3),
            "disp_edge_cond": torch.ones(batch, num_res, num_res, 1),
        }, None


class _FakeModule:
    latent_encoder = _FakeLatent()


def _inputs():
    trans = torch.zeros(1, 4, 3)
    rot = torch.eye(3).reshape(1, 1, 3, 3).repeat(1, 4, 1, 1)
    seq = torch.zeros(1, 4, 21)
    mask = torch.ones(1, 4)
    return trans, rot, seq, mask


def test_real_condition_draws_independent_coefficients_per_sample_reproducibly():
    trans, rot, seq, mask = _inputs()
    first = build_condition_batch(
        _FakeModule(), trans, rot, seq, mask, 5, "real", 123
    )
    second = build_condition_batch(
        _FakeModule(), trans, rot, seq, mask, 5, "real", 123
    )

    assert first["mode_coefficients"].shape == (5, 2)
    assert first["mode_coefficients"].std(dim=0).min() > 0
    assert torch.allclose(first["mode_coefficients"], second["mode_coefficients"])


def test_zero_control_uses_matched_coefficients_but_zero_injection():
    trans, rot, seq, mask = _inputs()
    real = build_condition_batch(
        _FakeModule(), trans, rot, seq, mask, 3, "real", 456
    )
    zero = build_condition_batch(
        _FakeModule(), trans, rot, seq, mask, 3, "zero", 456
    )

    assert torch.allclose(real["mode_coefficients"], zero["mode_coefficients"])
    for key in ["node_cond", "edge_cond", "disp_node_cond", "disp_edge_cond"]:
        assert torch.count_nonzero(zero[key]) == 0


def test_static_control_samples_one_coefficient_and_repeats_it_across_batch():
    trans, rot, seq, mask = _inputs()
    module = _FakeModule()
    module.latent_encoder = _FakeLatent()

    static = build_condition_batch(
        module, trans, rot, seq, mask, 5, "static", 789
    )

    assert module.latent_encoder.sample_batch_sizes == [1]
    assert static["mode_coefficients"].shape == (5, 2)
    assert torch.allclose(
        static["mode_coefficients"],
        static["mode_coefficients"][0:1].expand(5, -1),
    )


def test_static_control_reuses_the_same_seed_across_sampling_chunks():
    from scripts.probe_condition_quality_vs_md import condition_seed_for_chunk

    seeds = [condition_seed_for_chunk(2026, index, "static") for index in range(4)]
    assert seeds == [2026, 2026, 2026, 2026]
    real_seeds = [condition_seed_for_chunk(2026, index, "real") for index in range(4)]
    assert len(set(real_seeds)) == 4