test_pair_transition.py 3.06 KB
Newer Older
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Copyright 2021 AlQuraishi Laboratory
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch
import numpy as np
import unittest
18
19
20
21
22
from openfold.model.pair_transition import PairTransition
from openfold.utils.tensor_utils import tree_map
import tests.compare_utils as compare_utils
from tests.config import consts

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
23
if compare_utils.alphafold_is_installed():
24
25
26
    alphafold = compare_utils.import_alphafold()
    import jax
    import haiku as hk
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
27
28
29


class TestPairTransition(unittest.TestCase):
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
30
    def test_shape(self):
31
        c_z = consts.c_z
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
32
33
34
35
        n = 4

        pt = PairTransition(c_z, n)

36
37
        batch_size = consts.batch_size
        n_res = consts.n_res
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
38
39
40
41
42
43
44
45
46

        z = torch.rand((batch_size, n_res, n_res, c_z))
        mask = torch.randint(0, 2, size=(batch_size, n_res, n_res))
        shape_before = z.shape
        z = pt(z, mask)
        shape_after = z.shape

        self.assertTrue(shape_before == shape_after)

47
48
49
50
51
52
    @compare_utils.skip_unless_alphafold_installed()
    def test_compare(self):
        def run_pair_transition(pair_act, pair_mask):
            config = compare_utils.get_alphafold_config()
            c_e = config.model.embeddings_and_evoformer.evoformer
            pt = alphafold.model.modules.Transition(
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
53
                c_e.pair_transition,
54
                config.model.global_config,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
55
                name="pair_transition",
56
57
58
            )
            act = pt(act=pair_act, mask=pair_mask)
            return act
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
59

60
        f = hk.transform(run_pair_transition)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
61

62
        n_res = consts.n_res
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
63

64
        pair_act = np.random.rand(n_res, n_res, consts.c_z).astype(np.float32)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
65
66
        pair_mask = np.ones((n_res, n_res)).astype(np.float32)  # no mask

67
68
        # Fetch pretrained parameters (but only from one block)]
        params = compare_utils.fetch_alphafold_module_weights(
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
69
70
            "alphafold/alphafold_iteration/evoformer/evoformer_iteration/"
            + "pair_transition"
71
72
        )
        params = tree_map(lambda n: n[0], params, jax.numpy.DeviceArray)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
73
74

        out_gt = f.apply(params, None, pair_act, pair_mask).block_until_ready()
75
        out_gt = torch.as_tensor(np.array(out_gt.block_until_ready()))
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
76

77
        model = compare_utils.get_global_pretrained_openfold()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
78
79
80
81
82
83
84
85
        out_repro = (
            model.evoformer.blocks[0]
            .pair_transition(
                torch.as_tensor(pair_act, dtype=torch.float32).cuda(),
                mask=torch.as_tensor(pair_mask, dtype=torch.float32).cuda(),
            )
            .cpu()
        )
86

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
87
        self.assertTrue(torch.max(torch.abs(out_gt - out_repro) < consts.eps))
88

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
89
90
91

if __name__ == "__main__":
    unittest.main()