test_primitives.py 1.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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 unittest

from openfold.model.primitives import (
19
20
    lecun_normal_init_,
    Attention,
21
22
)
from tests.config import consts
23
from tests.data_utils import random_attention_inputs
24
25
26
27


class TestLMA(unittest.TestCase):
    def test_lma_vs_attention(self):
28
        c_hidden = 32
29
30
        no_heads = 4

31
32
33
34
35
36
37
38
39
        q, kv, _, biases = random_attention_inputs(batch_size=consts.batch_size,
                                                   n_seq=consts.n_seq,
                                                   n=2**12,
                                                   no_heads=no_heads,
                                                   c_hidden=c_hidden)

        a = Attention(
            c_hidden, c_hidden, c_hidden, c_hidden, no_heads
        ).cuda()
40
41

        with torch.no_grad():
42
43
44
45
46
            lecun_normal_init_(a.linear_g.weight)
            lecun_normal_init_(a.linear_o.weight)

            l = a(q, kv, biases=biases, use_lma=True).cpu()
            real = a(q, kv, biases=biases).cpu()
47
48
49
       
        err = torch.max(torch.abs(l - real))
        self.assertTrue(err < consts.eps, f'Error: {err}')
50
51
52
53

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