Datasets:

Formats:
text
ArXiv:
Libraries:
Datasets
File size: 2,656 Bytes
2872543
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2024 ByteDance and/or its affiliates.
#
# 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 math
import time
import unittest

import torch

from protenix.model.modules.transformer import DiffusionTransformer


class TestDiffusionTransformer(unittest.TestCase):
    def setUp(self) -> None:
        self._start_time = time.time()
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        super().setUp()

    def get_model(
        self,
        c_a: int = 128,
        c_s: int = 384,
        c_z: int = 64,
        n_blocks: int = 3,
        n_heads: int = 4,
    ):

        model = DiffusionTransformer(
            c_a=c_a, c_s=c_s, c_z=c_z, n_blocks=n_blocks, n_heads=n_heads
        ).to(self.device)

        return model

    def test_shape(self) -> None:

        n_heads = 2
        c_a = 13 * n_heads
        c_s = 23
        c_z = 17

        N = 45
        bs_dims = (2, 3)

        inputs = {
            "a": torch.rand(size=(*bs_dims, N, c_a)).to(self.device),
            "s": torch.rand(size=(*bs_dims, N, c_s)).to(self.device),
            "z": torch.rand(size=(*bs_dims, N, N, c_z)).to(self.device),
            "n_queries": None,
            "n_keys": None,
        }

        model = self.get_model(c_a=c_a, c_s=c_s, c_z=c_z, n_heads=n_heads)

        out = model(**inputs)
        target_shape = (*bs_dims, N, c_a)
        self.assertEqual(out.shape, out.reshape(target_shape).shape)

        N_q = 32
        N_k = 128
        N_blocks = math.ceil(N / N_q)

        inputs = {
            "a": torch.rand(size=(*bs_dims, N, c_a)).to(self.device),
            "s": torch.rand(size=(*bs_dims, N, c_s)).to(self.device),
            "z": torch.rand(size=(*bs_dims, N_blocks, N_q, N_k, c_z)).to(self.device),
            "n_queries": 32,
            "n_keys": 128,
        }

        out = model(**inputs)
        target_shape = (*bs_dims, N, c_a)
        self.assertEqual(out.shape, out.reshape(target_shape).shape)

    def tearDown(self):
        elapsed_time = time.time() - self._start_time
        print(f"Test {self.id()} took {elapsed_time:.6f}s")


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