test_outer_product_mean.py 3.28 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
from openfold.model.outer_product_mean import OuterProductMean
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
22
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
30


class TestOuterProductMean(unittest.TestCase):
    def test_shape(self):
31
        c = 31
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
32

33
        opm = OuterProductMean(consts.c_m, consts.c_z, c)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
34

35
36
37
38
39
40
        m = torch.rand(
            (consts.batch_size, consts.n_seq, consts.n_res, consts.c_m)
        )
        mask = torch.randint(
            0, 2, size=(consts.batch_size, consts.n_seq, consts.n_res)
        )
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
41
        m = opm(m, mask=mask, chunk_size=None)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
42

43
        self.assertTrue(
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
44
45
            m.shape == 
            (consts.batch_size, consts.n_res, consts.n_res, consts.c_z)
46
47
48
        )

    @compare_utils.skip_unless_alphafold_installed()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
49
    def test_opm_compare(self):
50
51
52
53
        def run_opm(msa_act, msa_mask):
            config = compare_utils.get_alphafold_config()
            c_evo = config.model.embeddings_and_evoformer.evoformer
            opm = alphafold.model.modules.OuterProductMean(
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
54
                c_evo.outer_product_mean,
55
56
57
58
59
                config.model.global_config,
                consts.c_z,
            )
            act = opm(act=msa_act, mask=msa_mask)
            return act
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
60

61
62
63
64
65
        f = hk.transform(run_opm)

        n_res = consts.n_res
        n_seq = consts.n_seq
        c_m = consts.c_m
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
66

67
        msa_act = np.random.rand(n_seq, n_res, c_m).astype(np.float32) * 100
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
68
69
70
71
        msa_mask = np.random.randint(low=0, high=2, size=(n_seq, n_res)).astype(
            np.float32
        )

72
73
        # Fetch pretrained parameters (but only from one block)]
        params = compare_utils.fetch_alphafold_module_weights(
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
74
75
            "alphafold/alphafold_iteration/evoformer/"
            + "evoformer_iteration/outer_product_mean"
76
77
78
        )
        params = tree_map(lambda n: n[0], params, jax.numpy.DeviceArray)

Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
79
        out_gt = f.apply(params, None, msa_act, msa_mask).block_until_ready()
80
        out_gt = torch.as_tensor(np.array(out_gt))
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
81

82
        model = compare_utils.get_global_pretrained_openfold()
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
83
        out_repro = (
84
            model.evoformer.blocks[0].core
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
85
86
            .outer_product_mean(
                torch.as_tensor(msa_act).cuda(),
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
87
                chunk_size=4,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
88
89
90
91
92
                mask=torch.as_tensor(msa_mask).cuda(),
            )
            .cpu()
        )

93
94
        # Even when correct, OPM has large, precision-related errors. It gets
        # a special pass from consts.eps.
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
95
        self.assertTrue(torch.max(torch.abs(out_gt - out_repro)) < 5e-4)
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
96
97
98
99


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