"googlemock/msvc/vscode:/vscode.git/clone" did not exist on "87348c217f603b915dd3307ee99dff0a0e23e9a8"
test_outer_product_mean.py 3.2 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
23
24
25
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
if(compare_utils.alphafold_is_installed()):
    alphafold = compare_utils.import_alphafold()
    import jax
    import haiku as hk
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
26
27
28
29


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

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

34
35
36
37
38
39
        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
40
41
        m = opm(m, mask)

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
        self.assertTrue(
            m.shape == (consts.batch_size, consts.n_res, consts.n_res, consts.c_z)
        )

    @compare_utils.skip_unless_alphafold_installed()
    def test_opm_compare(self):    
        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(
                c_evo.outer_product_mean, 
                config.model.global_config,
                consts.c_z,
            )
            act = opm(act=msa_act, mask=msa_mask)
            return act
        
        f = hk.transform(run_opm)

        n_res = consts.n_res
        n_seq = consts.n_seq
        c_m = consts.c_m
    
        msa_act = np.random.rand(n_seq, n_res, c_m).astype(np.float32) * 100
        msa_mask = np.random.randint(
            low=0, high=2, size=(n_seq, n_res)
        ).astype(np.float32)
    
        # Fetch pretrained parameters (but only from one block)]
        params = compare_utils.fetch_alphafold_module_weights(
            "alphafold/alphafold_iteration/evoformer/" +
            "evoformer_iteration/outer_product_mean"
        )
        params = tree_map(lambda n: n[0], params, jax.numpy.DeviceArray)

        out_gt = f.apply(
            params, None, msa_act, msa_mask
        ).block_until_ready()
        out_gt = torch.as_tensor(np.array(out_gt))
   
        model = compare_utils.get_global_pretrained_openfold()
        out_repro = model.evoformer.blocks[0].outer_product_mean(
            torch.as_tensor(msa_act).cuda(), 
            mask=torch.as_tensor(msa_mask).cuda(), 
        ).cpu()
   
        # Even when correct, OPM has large, precision-related errors. It gets
        # a special pass from consts.eps.
        self.assertTrue(torch.max(torch.abs(out_gt - out_repro) < 5e-4))
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
91
92
93
94


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