File size: 4,352 Bytes
13e95c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import torch
import torch.nn as nn

from src.utils.license_register import register_license
from src.model.modules.primitives import Linear
from src.utils.data.constants import BOND_TYPE
from src.api.model_interface import (
    PairFormerOutput,
    PairwiseOutput,
)

@register_license('bytedance2024')
class DistogramHead(nn.Module):
    """Implements Algorithm 1 [Line17] in AF3
    Computes a distogram probability distribution.
    For use in computation of distogram loss, subsection 1.9.8 (AF2)
    """

    def __init__(self, c_z: int = 128, no_bins: int = 64) -> None:
        """
        Args:
            c_z (int, optional): hidden dim [for pair embedding]. Defaults to 128.
            no_bins (int, optional): Number of distogram bins. Defaults to 64.
        """
        super(DistogramHead, self).__init__()

        self.c_z = c_z
        self.no_bins = no_bins

        self.linear = Linear(
            in_features=self.c_z, out_features=self.no_bins, initializer="zeros"
        )

    def forward(self, z: torch.Tensor) -> torch.Tensor:  # [*, N, N, C_z]
        """
        Args:
            z (torch.Tensor): pairformer output
                [*, N_token, N_token, C_z]

        Returns:
            torch.Tensor: distogram probability distribution
                [*, N_token, N_token, no_bins]
        """
        # [*, N, N, no_bins]
        logits = self.linear(z)
        logits = logits + logits.transpose(-2, -3)
        return logits

@register_license('odesign2025')
class BondTypeHead(nn.Module):
    """Implements a bond type classifier head.
    Computes bond type probability distribution.
    """

    def __init__(self, c_z: int = 1, c_hidden: int = 128, no_bond_types: int = len(BOND_TYPE)) -> None:
        """
        Args:
            c_in (int, optional): hidden dim [for pair embedding]. Defaults to 1.
            c_hidden (int, optional): hidden dim. Defaults to 128.
            no_bond_types (int, optional): Number of bond types. Defaults to len(BOND_TYPE).
        """
        super(BondTypeHead, self).__init__()

        self.c_in = c_z
        self.c_hidden = c_hidden
        self.no_bond_types = no_bond_types

        self.classifier = nn.Sequential(
            Linear(self.c_in, self.c_hidden),
            Linear(self.c_hidden, no_bond_types),
        )

    def forward(self, z: torch.Tensor) -> torch.Tensor:
        """
        Args:
            z (torch.Tensor): pairformer output
                [*, N_token, N_token, c_z]

        Returns:
            torch.Tensor: bond type probability distribution
                [*, N_token, N_token, no_bond_types]
        """
        # [*, N_token, N_token, no_bond_types]
        logits = self.classifier(z.unsqueeze(0))
        return logits

@register_license('odesign2025')
class PairwiseHead(nn.Module):
    """Implements a pairwise head.
    Computes pairwise probability distribution.
    """

    def __init__(
        self, 
        c_z: int = 128,
        no_bond_types: int = len(BOND_TYPE), 
        no_bins: int = 64,
        bond_reconstruction = True
    ) -> PairwiseOutput:
        """
        Args:
            c_in (int, optional): hidden dim [for pair embedding]. Defaults to 1.
            c_hidden (int, optional): hidden dim. Defaults to 128.
            no_bond_types (int, optional): Number of bond types. Defaults to len(BOND_TYPE).
        """
        super(PairwiseHead, self).__init__()

        self.distogram_head = DistogramHead(c_z=c_z, no_bins=no_bins)
        self.bond_reconstruction = bond_reconstruction
        if bond_reconstruction:
            self.bond_type_head = BondTypeHead(c_z=c_z, no_bond_types=no_bond_types)

    def forward(self, input_embedding: PairFormerOutput) -> PairwiseOutput:
        """
        Args:
            input (PairFormerOutput): pairformer output
                [*, N_token, N_token, c_z]

        Returns:
            PairwiseOutput: bond type probability distribution
                [*, N_token, N_token, no_bond_types]
        """
        # [*, N_token, N_token, no_bond_types]
        distogram = self.distogram_head(input_embedding.z)
        if self.bond_reconstruction:
            token_bond_type_logits = self.bond_type_head(input_embedding.z)
        else:
            token_bond_type_logits = None
        return PairwiseOutput(distogram, token_bond_type_logits)