Feature Extraction
Transformers
PyTorch
Safetensors
boltz2_automodel
protein-language-model
fastplms
custom_code
Instructions to use Synthyra/Boltz2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Synthyra/Boltz2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="Synthyra/Boltz2", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Synthyra/Boltz2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload vb_layers_pairformer.py with huggingface_hub
Browse files- vb_layers_pairformer.py +337 -337
vb_layers_pairformer.py
CHANGED
|
@@ -1,337 +1,337 @@
|
|
| 1 |
-
from typing import Optional, Tuple
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
-
from torch import Tensor, nn
|
| 5 |
-
|
| 6 |
-
from . import vb_const as const
|
| 7 |
-
from .vb_layers_attention import AttentionPairBias
|
| 8 |
-
from .vb_layers_attentionv2 import AttentionPairBias as AttentionPairBiasV2
|
| 9 |
-
from .vb_layers_dropout import get_dropout_mask
|
| 10 |
-
from .vb_layers_transition import Transition
|
| 11 |
-
from .vb_tri_attn_attention import (
|
| 12 |
-
TriangleAttentionEndingNode,
|
| 13 |
-
TriangleAttentionStartingNode,
|
| 14 |
-
)
|
| 15 |
-
from .vb_layers_triangular_mult import (
|
| 16 |
-
TriangleMultiplicationIncoming,
|
| 17 |
-
TriangleMultiplicationOutgoing,
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
class PairformerLayer(nn.Module):
|
| 22 |
-
"""Pairformer module."""
|
| 23 |
-
|
| 24 |
-
def __init__(
|
| 25 |
-
self,
|
| 26 |
-
token_s: int,
|
| 27 |
-
token_z: int,
|
| 28 |
-
num_heads: int = 16,
|
| 29 |
-
dropout: float = 0.25,
|
| 30 |
-
pairwise_head_width: int = 32,
|
| 31 |
-
pairwise_num_heads: int = 4,
|
| 32 |
-
post_layer_norm: bool = False,
|
| 33 |
-
v2: bool = False,
|
| 34 |
-
) -> None:
|
| 35 |
-
super().__init__()
|
| 36 |
-
self.token_z = token_z
|
| 37 |
-
self.dropout = dropout
|
| 38 |
-
self.num_heads = num_heads
|
| 39 |
-
self.post_layer_norm = post_layer_norm
|
| 40 |
-
|
| 41 |
-
self.pre_norm_s = nn.LayerNorm(token_s)
|
| 42 |
-
if v2:
|
| 43 |
-
self.attention = AttentionPairBiasV2(token_s, token_z, num_heads)
|
| 44 |
-
else:
|
| 45 |
-
self.attention = AttentionPairBias(token_s, token_z, num_heads)
|
| 46 |
-
|
| 47 |
-
self.tri_mul_out = TriangleMultiplicationOutgoing(token_z)
|
| 48 |
-
self.tri_mul_in = TriangleMultiplicationIncoming(token_z)
|
| 49 |
-
|
| 50 |
-
self.tri_att_start = TriangleAttentionStartingNode(
|
| 51 |
-
token_z, pairwise_head_width, pairwise_num_heads, inf=1e9
|
| 52 |
-
)
|
| 53 |
-
self.tri_att_end = TriangleAttentionEndingNode(
|
| 54 |
-
token_z, pairwise_head_width, pairwise_num_heads, inf=1e9
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
self.transition_s = Transition(token_s, token_s * 4)
|
| 58 |
-
self.transition_z = Transition(token_z, token_z * 4)
|
| 59 |
-
|
| 60 |
-
self.s_post_norm = (
|
| 61 |
-
nn.LayerNorm(token_s) if self.post_layer_norm else nn.Identity()
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
def forward(
|
| 65 |
-
self,
|
| 66 |
-
s: Tensor,
|
| 67 |
-
z: Tensor,
|
| 68 |
-
mask: Tensor,
|
| 69 |
-
pair_mask: Tensor,
|
| 70 |
-
chunk_size_tri_attn: Optional[int] = None,
|
| 71 |
-
use_kernels: bool = False,
|
| 72 |
-
use_cuequiv_mul: bool = False,
|
| 73 |
-
use_cuequiv_attn: bool = False,
|
| 74 |
-
) -> Tuple[Tensor, Tensor]:
|
| 75 |
-
# Compute pairwise stack
|
| 76 |
-
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 77 |
-
z = z + dropout * self.tri_mul_out(
|
| 78 |
-
z, mask=pair_mask, use_kernels=use_cuequiv_mul or use_kernels
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 82 |
-
z = z + dropout * self.tri_mul_in(
|
| 83 |
-
z, mask=pair_mask, use_kernels=use_cuequiv_mul or use_kernels
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 87 |
-
z = z + dropout * self.tri_att_start(
|
| 88 |
-
z,
|
| 89 |
-
mask=pair_mask,
|
| 90 |
-
chunk_size=chunk_size_tri_attn,
|
| 91 |
-
use_kernels=use_cuequiv_attn or use_kernels,
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
dropout = get_dropout_mask(self.dropout, z, self.training, columnwise=True)
|
| 95 |
-
z = z + dropout * self.tri_att_end(
|
| 96 |
-
z,
|
| 97 |
-
mask=pair_mask,
|
| 98 |
-
chunk_size=chunk_size_tri_attn,
|
| 99 |
-
use_kernels=use_cuequiv_attn or use_kernels,
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
z = z + self.transition_z(z)
|
| 103 |
-
|
| 104 |
-
# Compute sequence stack
|
| 105 |
-
with torch.autocast("cuda", enabled=False):
|
| 106 |
-
s_normed = self.pre_norm_s(s.float())
|
| 107 |
-
s = s.float() + self.attention(
|
| 108 |
-
s=s_normed, z=z.float(), mask=mask.float(), k_in=s_normed
|
| 109 |
-
)
|
| 110 |
-
s = s + self.transition_s(s)
|
| 111 |
-
s = self.s_post_norm(s)
|
| 112 |
-
|
| 113 |
-
return s, z
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
class PairformerModule(nn.Module):
|
| 117 |
-
"""Pairformer module."""
|
| 118 |
-
|
| 119 |
-
def __init__(
|
| 120 |
-
self,
|
| 121 |
-
token_s: int,
|
| 122 |
-
token_z: int,
|
| 123 |
-
num_blocks: int,
|
| 124 |
-
num_heads: int = 16,
|
| 125 |
-
dropout: float = 0.25,
|
| 126 |
-
pairwise_head_width: int = 32,
|
| 127 |
-
pairwise_num_heads: int = 4,
|
| 128 |
-
post_layer_norm: bool = False,
|
| 129 |
-
activation_checkpointing: bool = False,
|
| 130 |
-
v2: bool = False,
|
| 131 |
-
**kwargs,
|
| 132 |
-
) -> None:
|
| 133 |
-
super().__init__()
|
| 134 |
-
self.token_z = token_z
|
| 135 |
-
self.num_blocks = num_blocks
|
| 136 |
-
self.dropout = dropout
|
| 137 |
-
self.num_heads = num_heads
|
| 138 |
-
self.post_layer_norm = post_layer_norm
|
| 139 |
-
self.activation_checkpointing = activation_checkpointing
|
| 140 |
-
|
| 141 |
-
self.layers = nn.ModuleList()
|
| 142 |
-
for _ in range(num_blocks):
|
| 143 |
-
self.layers.append(
|
| 144 |
-
PairformerLayer(
|
| 145 |
-
token_s,
|
| 146 |
-
token_z,
|
| 147 |
-
num_heads,
|
| 148 |
-
dropout,
|
| 149 |
-
pairwise_head_width,
|
| 150 |
-
pairwise_num_heads,
|
| 151 |
-
post_layer_norm,
|
| 152 |
-
v2,
|
| 153 |
-
),
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
def forward(
|
| 157 |
-
self,
|
| 158 |
-
s: Tensor,
|
| 159 |
-
z: Tensor,
|
| 160 |
-
mask: Tensor,
|
| 161 |
-
pair_mask: Tensor,
|
| 162 |
-
use_kernels: bool = False,
|
| 163 |
-
) -> Tuple[Tensor, Tensor]:
|
| 164 |
-
"""Perform the forward pass.
|
| 165 |
-
|
| 166 |
-
Parameters
|
| 167 |
-
----------
|
| 168 |
-
s : Tensor
|
| 169 |
-
The sequence stack.
|
| 170 |
-
z : Tensor
|
| 171 |
-
The pairwise stack.
|
| 172 |
-
mask : Tensor
|
| 173 |
-
The mask.
|
| 174 |
-
pair_mask : Tensor
|
| 175 |
-
The pairwise mask.
|
| 176 |
-
use_kernels : bool
|
| 177 |
-
Whether to use kernels.
|
| 178 |
-
|
| 179 |
-
"""
|
| 180 |
-
if not self.training:
|
| 181 |
-
if z.shape[1] > const.chunk_size_threshold:
|
| 182 |
-
chunk_size_tri_attn = 128
|
| 183 |
-
else:
|
| 184 |
-
chunk_size_tri_attn = 512
|
| 185 |
-
else:
|
| 186 |
-
chunk_size_tri_attn = None
|
| 187 |
-
|
| 188 |
-
for layer in self.layers:
|
| 189 |
-
if self.activation_checkpointing:
|
| 190 |
-
s, z = torch.utils.checkpoint.checkpoint(
|
| 191 |
-
layer,
|
| 192 |
-
s,
|
| 193 |
-
z,
|
| 194 |
-
mask,
|
| 195 |
-
pair_mask,
|
| 196 |
-
chunk_size_tri_attn,
|
| 197 |
-
use_kernels,
|
| 198 |
-
use_reentrant=False,
|
| 199 |
-
)
|
| 200 |
-
else:
|
| 201 |
-
s, z = layer(s, z, mask, pair_mask, chunk_size_tri_attn, use_kernels)
|
| 202 |
-
return s, z
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
class PairformerNoSeqLayer(nn.Module):
|
| 206 |
-
"""Pairformer module without sequence track."""
|
| 207 |
-
|
| 208 |
-
def __init__(
|
| 209 |
-
self,
|
| 210 |
-
token_z: int,
|
| 211 |
-
dropout: float = 0.25,
|
| 212 |
-
pairwise_head_width: int = 32,
|
| 213 |
-
pairwise_num_heads: int = 4,
|
| 214 |
-
post_layer_norm: bool = False,
|
| 215 |
-
) -> None:
|
| 216 |
-
super().__init__()
|
| 217 |
-
self.token_z = token_z
|
| 218 |
-
self.dropout = dropout
|
| 219 |
-
self.post_layer_norm = post_layer_norm
|
| 220 |
-
|
| 221 |
-
self.tri_mul_out = TriangleMultiplicationOutgoing(token_z)
|
| 222 |
-
self.tri_mul_in = TriangleMultiplicationIncoming(token_z)
|
| 223 |
-
|
| 224 |
-
self.tri_att_start = TriangleAttentionStartingNode(
|
| 225 |
-
token_z, pairwise_head_width, pairwise_num_heads, inf=1e9
|
| 226 |
-
)
|
| 227 |
-
self.tri_att_end = TriangleAttentionEndingNode(
|
| 228 |
-
token_z, pairwise_head_width, pairwise_num_heads, inf=1e9
|
| 229 |
-
)
|
| 230 |
-
|
| 231 |
-
self.transition_z = Transition(token_z, token_z * 4)
|
| 232 |
-
|
| 233 |
-
def forward(
|
| 234 |
-
self,
|
| 235 |
-
z: Tensor,
|
| 236 |
-
pair_mask: Tensor,
|
| 237 |
-
chunk_size_tri_attn: Optional[int] = None,
|
| 238 |
-
use_kernels: bool = False,
|
| 239 |
-
use_cuequiv_mul: bool = False,
|
| 240 |
-
use_cuequiv_attn: bool = False,
|
| 241 |
-
) -> Tensor:
|
| 242 |
-
# Compute pairwise stack
|
| 243 |
-
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 244 |
-
z = z + dropout * self.tri_mul_out(
|
| 245 |
-
z, mask=pair_mask, use_kernels=use_cuequiv_mul or use_kernels
|
| 246 |
-
)
|
| 247 |
-
|
| 248 |
-
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 249 |
-
z = z + dropout * self.tri_mul_in(
|
| 250 |
-
z, mask=pair_mask, use_kernels=use_cuequiv_mul or use_kernels
|
| 251 |
-
)
|
| 252 |
-
|
| 253 |
-
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 254 |
-
z = z + dropout * self.tri_att_start(
|
| 255 |
-
z,
|
| 256 |
-
mask=pair_mask,
|
| 257 |
-
chunk_size=chunk_size_tri_attn,
|
| 258 |
-
use_kernels=use_cuequiv_attn or use_kernels,
|
| 259 |
-
)
|
| 260 |
-
|
| 261 |
-
dropout = get_dropout_mask(self.dropout, z, self.training, columnwise=True)
|
| 262 |
-
z = z + dropout * self.tri_att_end(
|
| 263 |
-
z,
|
| 264 |
-
mask=pair_mask,
|
| 265 |
-
chunk_size=chunk_size_tri_attn,
|
| 266 |
-
use_kernels=use_cuequiv_attn or use_kernels,
|
| 267 |
-
)
|
| 268 |
-
|
| 269 |
-
z = z + self.transition_z(z)
|
| 270 |
-
return z
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
class PairformerNoSeqModule(nn.Module):
|
| 274 |
-
"""Pairformer module without sequence track."""
|
| 275 |
-
|
| 276 |
-
def __init__(
|
| 277 |
-
self,
|
| 278 |
-
token_z: int,
|
| 279 |
-
num_blocks: int,
|
| 280 |
-
dropout: float = 0.25,
|
| 281 |
-
pairwise_head_width: int = 32,
|
| 282 |
-
pairwise_num_heads: int = 4,
|
| 283 |
-
post_layer_norm: bool = False,
|
| 284 |
-
activation_checkpointing: bool = False,
|
| 285 |
-
**kwargs,
|
| 286 |
-
) -> None:
|
| 287 |
-
super().__init__()
|
| 288 |
-
self.token_z = token_z
|
| 289 |
-
self.num_blocks = num_blocks
|
| 290 |
-
self.dropout = dropout
|
| 291 |
-
self.post_layer_norm = post_layer_norm
|
| 292 |
-
self.activation_checkpointing = activation_checkpointing
|
| 293 |
-
|
| 294 |
-
self.layers = nn.ModuleList()
|
| 295 |
-
for i in range(num_blocks):
|
| 296 |
-
self.layers.append(
|
| 297 |
-
PairformerNoSeqLayer(
|
| 298 |
-
token_z,
|
| 299 |
-
dropout,
|
| 300 |
-
pairwise_head_width,
|
| 301 |
-
pairwise_num_heads,
|
| 302 |
-
post_layer_norm,
|
| 303 |
-
),
|
| 304 |
-
)
|
| 305 |
-
|
| 306 |
-
def forward(
|
| 307 |
-
self,
|
| 308 |
-
z: Tensor,
|
| 309 |
-
pair_mask: Tensor,
|
| 310 |
-
use_kernels: bool = False,
|
| 311 |
-
) -> Tensor:
|
| 312 |
-
if not self.training:
|
| 313 |
-
if z.shape[1] > const.chunk_size_threshold:
|
| 314 |
-
chunk_size_tri_attn = 128
|
| 315 |
-
else:
|
| 316 |
-
chunk_size_tri_attn = 512
|
| 317 |
-
else:
|
| 318 |
-
chunk_size_tri_attn = None
|
| 319 |
-
|
| 320 |
-
for layer in self.layers:
|
| 321 |
-
if self.activation_checkpointing:
|
| 322 |
-
z = torch.utils.checkpoint.checkpoint(
|
| 323 |
-
layer,
|
| 324 |
-
z,
|
| 325 |
-
pair_mask,
|
| 326 |
-
chunk_size_tri_attn,
|
| 327 |
-
use_kernels,
|
| 328 |
-
use_reentrant=False,
|
| 329 |
-
)
|
| 330 |
-
else:
|
| 331 |
-
z = layer(
|
| 332 |
-
z,
|
| 333 |
-
pair_mask,
|
| 334 |
-
chunk_size_tri_attn,
|
| 335 |
-
use_kernels,
|
| 336 |
-
)
|
| 337 |
-
return z
|
|
|
|
| 1 |
+
from typing import Optional, Tuple
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
from torch import Tensor, nn
|
| 5 |
+
|
| 6 |
+
from . import vb_const as const
|
| 7 |
+
from .vb_layers_attention import AttentionPairBias
|
| 8 |
+
from .vb_layers_attentionv2 import AttentionPairBias as AttentionPairBiasV2
|
| 9 |
+
from .vb_layers_dropout import get_dropout_mask
|
| 10 |
+
from .vb_layers_transition import Transition
|
| 11 |
+
from .vb_tri_attn_attention import (
|
| 12 |
+
TriangleAttentionEndingNode,
|
| 13 |
+
TriangleAttentionStartingNode,
|
| 14 |
+
)
|
| 15 |
+
from .vb_layers_triangular_mult import (
|
| 16 |
+
TriangleMultiplicationIncoming,
|
| 17 |
+
TriangleMultiplicationOutgoing,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class PairformerLayer(nn.Module):
|
| 22 |
+
"""Pairformer module."""
|
| 23 |
+
|
| 24 |
+
def __init__(
|
| 25 |
+
self,
|
| 26 |
+
token_s: int,
|
| 27 |
+
token_z: int,
|
| 28 |
+
num_heads: int = 16,
|
| 29 |
+
dropout: float = 0.25,
|
| 30 |
+
pairwise_head_width: int = 32,
|
| 31 |
+
pairwise_num_heads: int = 4,
|
| 32 |
+
post_layer_norm: bool = False,
|
| 33 |
+
v2: bool = False,
|
| 34 |
+
) -> None:
|
| 35 |
+
super().__init__()
|
| 36 |
+
self.token_z = token_z
|
| 37 |
+
self.dropout = dropout
|
| 38 |
+
self.num_heads = num_heads
|
| 39 |
+
self.post_layer_norm = post_layer_norm
|
| 40 |
+
|
| 41 |
+
self.pre_norm_s = nn.LayerNorm(token_s)
|
| 42 |
+
if v2:
|
| 43 |
+
self.attention = AttentionPairBiasV2(token_s, token_z, num_heads)
|
| 44 |
+
else:
|
| 45 |
+
self.attention = AttentionPairBias(token_s, token_z, num_heads)
|
| 46 |
+
|
| 47 |
+
self.tri_mul_out = TriangleMultiplicationOutgoing(token_z)
|
| 48 |
+
self.tri_mul_in = TriangleMultiplicationIncoming(token_z)
|
| 49 |
+
|
| 50 |
+
self.tri_att_start = TriangleAttentionStartingNode(
|
| 51 |
+
token_z, pairwise_head_width, pairwise_num_heads, inf=1e9
|
| 52 |
+
)
|
| 53 |
+
self.tri_att_end = TriangleAttentionEndingNode(
|
| 54 |
+
token_z, pairwise_head_width, pairwise_num_heads, inf=1e9
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
self.transition_s = Transition(token_s, token_s * 4)
|
| 58 |
+
self.transition_z = Transition(token_z, token_z * 4)
|
| 59 |
+
|
| 60 |
+
self.s_post_norm = (
|
| 61 |
+
nn.LayerNorm(token_s) if self.post_layer_norm else nn.Identity()
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
def forward(
|
| 65 |
+
self,
|
| 66 |
+
s: Tensor,
|
| 67 |
+
z: Tensor,
|
| 68 |
+
mask: Tensor,
|
| 69 |
+
pair_mask: Tensor,
|
| 70 |
+
chunk_size_tri_attn: Optional[int] = None,
|
| 71 |
+
use_kernels: bool = False,
|
| 72 |
+
use_cuequiv_mul: bool = False,
|
| 73 |
+
use_cuequiv_attn: bool = False,
|
| 74 |
+
) -> Tuple[Tensor, Tensor]:
|
| 75 |
+
# Compute pairwise stack
|
| 76 |
+
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 77 |
+
z = z + dropout * self.tri_mul_out(
|
| 78 |
+
z, mask=pair_mask, use_kernels=use_cuequiv_mul or use_kernels
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 82 |
+
z = z + dropout * self.tri_mul_in(
|
| 83 |
+
z, mask=pair_mask, use_kernels=use_cuequiv_mul or use_kernels
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 87 |
+
z = z + dropout * self.tri_att_start(
|
| 88 |
+
z,
|
| 89 |
+
mask=pair_mask,
|
| 90 |
+
chunk_size=chunk_size_tri_attn,
|
| 91 |
+
use_kernels=use_cuequiv_attn or use_kernels,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
dropout = get_dropout_mask(self.dropout, z, self.training, columnwise=True)
|
| 95 |
+
z = z + dropout * self.tri_att_end(
|
| 96 |
+
z,
|
| 97 |
+
mask=pair_mask,
|
| 98 |
+
chunk_size=chunk_size_tri_attn,
|
| 99 |
+
use_kernels=use_cuequiv_attn or use_kernels,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
z = z + self.transition_z(z)
|
| 103 |
+
|
| 104 |
+
# Compute sequence stack
|
| 105 |
+
with torch.autocast("cuda", enabled=False):
|
| 106 |
+
s_normed = self.pre_norm_s(s.float())
|
| 107 |
+
s = s.float() + self.attention(
|
| 108 |
+
s=s_normed, z=z.float(), mask=mask.float(), k_in=s_normed
|
| 109 |
+
)
|
| 110 |
+
s = s + self.transition_s(s)
|
| 111 |
+
s = self.s_post_norm(s)
|
| 112 |
+
|
| 113 |
+
return s, z
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
class PairformerModule(nn.Module):
|
| 117 |
+
"""Pairformer module."""
|
| 118 |
+
|
| 119 |
+
def __init__(
|
| 120 |
+
self,
|
| 121 |
+
token_s: int,
|
| 122 |
+
token_z: int,
|
| 123 |
+
num_blocks: int,
|
| 124 |
+
num_heads: int = 16,
|
| 125 |
+
dropout: float = 0.25,
|
| 126 |
+
pairwise_head_width: int = 32,
|
| 127 |
+
pairwise_num_heads: int = 4,
|
| 128 |
+
post_layer_norm: bool = False,
|
| 129 |
+
activation_checkpointing: bool = False,
|
| 130 |
+
v2: bool = False,
|
| 131 |
+
**kwargs,
|
| 132 |
+
) -> None:
|
| 133 |
+
super().__init__()
|
| 134 |
+
self.token_z = token_z
|
| 135 |
+
self.num_blocks = num_blocks
|
| 136 |
+
self.dropout = dropout
|
| 137 |
+
self.num_heads = num_heads
|
| 138 |
+
self.post_layer_norm = post_layer_norm
|
| 139 |
+
self.activation_checkpointing = activation_checkpointing
|
| 140 |
+
|
| 141 |
+
self.layers = nn.ModuleList()
|
| 142 |
+
for _ in range(num_blocks):
|
| 143 |
+
self.layers.append(
|
| 144 |
+
PairformerLayer(
|
| 145 |
+
token_s,
|
| 146 |
+
token_z,
|
| 147 |
+
num_heads,
|
| 148 |
+
dropout,
|
| 149 |
+
pairwise_head_width,
|
| 150 |
+
pairwise_num_heads,
|
| 151 |
+
post_layer_norm,
|
| 152 |
+
v2,
|
| 153 |
+
),
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
def forward(
|
| 157 |
+
self,
|
| 158 |
+
s: Tensor,
|
| 159 |
+
z: Tensor,
|
| 160 |
+
mask: Tensor,
|
| 161 |
+
pair_mask: Tensor,
|
| 162 |
+
use_kernels: bool = False,
|
| 163 |
+
) -> Tuple[Tensor, Tensor]:
|
| 164 |
+
"""Perform the forward pass.
|
| 165 |
+
|
| 166 |
+
Parameters
|
| 167 |
+
----------
|
| 168 |
+
s : Tensor
|
| 169 |
+
The sequence stack.
|
| 170 |
+
z : Tensor
|
| 171 |
+
The pairwise stack.
|
| 172 |
+
mask : Tensor
|
| 173 |
+
The mask.
|
| 174 |
+
pair_mask : Tensor
|
| 175 |
+
The pairwise mask.
|
| 176 |
+
use_kernels : bool
|
| 177 |
+
Whether to use kernels.
|
| 178 |
+
|
| 179 |
+
"""
|
| 180 |
+
if not self.training:
|
| 181 |
+
if z.shape[1] > const.chunk_size_threshold:
|
| 182 |
+
chunk_size_tri_attn = 128
|
| 183 |
+
else:
|
| 184 |
+
chunk_size_tri_attn = 512
|
| 185 |
+
else:
|
| 186 |
+
chunk_size_tri_attn = None
|
| 187 |
+
|
| 188 |
+
for layer in self.layers:
|
| 189 |
+
if self.activation_checkpointing:
|
| 190 |
+
s, z = torch.utils.checkpoint.checkpoint(
|
| 191 |
+
layer,
|
| 192 |
+
s,
|
| 193 |
+
z,
|
| 194 |
+
mask,
|
| 195 |
+
pair_mask,
|
| 196 |
+
chunk_size_tri_attn,
|
| 197 |
+
use_kernels,
|
| 198 |
+
use_reentrant=False,
|
| 199 |
+
)
|
| 200 |
+
else:
|
| 201 |
+
s, z = layer(s, z, mask, pair_mask, chunk_size_tri_attn, use_kernels)
|
| 202 |
+
return s, z
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
class PairformerNoSeqLayer(nn.Module):
|
| 206 |
+
"""Pairformer module without sequence track."""
|
| 207 |
+
|
| 208 |
+
def __init__(
|
| 209 |
+
self,
|
| 210 |
+
token_z: int,
|
| 211 |
+
dropout: float = 0.25,
|
| 212 |
+
pairwise_head_width: int = 32,
|
| 213 |
+
pairwise_num_heads: int = 4,
|
| 214 |
+
post_layer_norm: bool = False,
|
| 215 |
+
) -> None:
|
| 216 |
+
super().__init__()
|
| 217 |
+
self.token_z = token_z
|
| 218 |
+
self.dropout = dropout
|
| 219 |
+
self.post_layer_norm = post_layer_norm
|
| 220 |
+
|
| 221 |
+
self.tri_mul_out = TriangleMultiplicationOutgoing(token_z)
|
| 222 |
+
self.tri_mul_in = TriangleMultiplicationIncoming(token_z)
|
| 223 |
+
|
| 224 |
+
self.tri_att_start = TriangleAttentionStartingNode(
|
| 225 |
+
token_z, pairwise_head_width, pairwise_num_heads, inf=1e9
|
| 226 |
+
)
|
| 227 |
+
self.tri_att_end = TriangleAttentionEndingNode(
|
| 228 |
+
token_z, pairwise_head_width, pairwise_num_heads, inf=1e9
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
self.transition_z = Transition(token_z, token_z * 4)
|
| 232 |
+
|
| 233 |
+
def forward(
|
| 234 |
+
self,
|
| 235 |
+
z: Tensor,
|
| 236 |
+
pair_mask: Tensor,
|
| 237 |
+
chunk_size_tri_attn: Optional[int] = None,
|
| 238 |
+
use_kernels: bool = False,
|
| 239 |
+
use_cuequiv_mul: bool = False,
|
| 240 |
+
use_cuequiv_attn: bool = False,
|
| 241 |
+
) -> Tensor:
|
| 242 |
+
# Compute pairwise stack
|
| 243 |
+
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 244 |
+
z = z + dropout * self.tri_mul_out(
|
| 245 |
+
z, mask=pair_mask, use_kernels=use_cuequiv_mul or use_kernels
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 249 |
+
z = z + dropout * self.tri_mul_in(
|
| 250 |
+
z, mask=pair_mask, use_kernels=use_cuequiv_mul or use_kernels
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
dropout = get_dropout_mask(self.dropout, z, self.training)
|
| 254 |
+
z = z + dropout * self.tri_att_start(
|
| 255 |
+
z,
|
| 256 |
+
mask=pair_mask,
|
| 257 |
+
chunk_size=chunk_size_tri_attn,
|
| 258 |
+
use_kernels=use_cuequiv_attn or use_kernels,
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
dropout = get_dropout_mask(self.dropout, z, self.training, columnwise=True)
|
| 262 |
+
z = z + dropout * self.tri_att_end(
|
| 263 |
+
z,
|
| 264 |
+
mask=pair_mask,
|
| 265 |
+
chunk_size=chunk_size_tri_attn,
|
| 266 |
+
use_kernels=use_cuequiv_attn or use_kernels,
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
z = z + self.transition_z(z)
|
| 270 |
+
return z
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
class PairformerNoSeqModule(nn.Module):
|
| 274 |
+
"""Pairformer module without sequence track."""
|
| 275 |
+
|
| 276 |
+
def __init__(
|
| 277 |
+
self,
|
| 278 |
+
token_z: int,
|
| 279 |
+
num_blocks: int,
|
| 280 |
+
dropout: float = 0.25,
|
| 281 |
+
pairwise_head_width: int = 32,
|
| 282 |
+
pairwise_num_heads: int = 4,
|
| 283 |
+
post_layer_norm: bool = False,
|
| 284 |
+
activation_checkpointing: bool = False,
|
| 285 |
+
**kwargs,
|
| 286 |
+
) -> None:
|
| 287 |
+
super().__init__()
|
| 288 |
+
self.token_z = token_z
|
| 289 |
+
self.num_blocks = num_blocks
|
| 290 |
+
self.dropout = dropout
|
| 291 |
+
self.post_layer_norm = post_layer_norm
|
| 292 |
+
self.activation_checkpointing = activation_checkpointing
|
| 293 |
+
|
| 294 |
+
self.layers = nn.ModuleList()
|
| 295 |
+
for i in range(num_blocks):
|
| 296 |
+
self.layers.append(
|
| 297 |
+
PairformerNoSeqLayer(
|
| 298 |
+
token_z,
|
| 299 |
+
dropout,
|
| 300 |
+
pairwise_head_width,
|
| 301 |
+
pairwise_num_heads,
|
| 302 |
+
post_layer_norm,
|
| 303 |
+
),
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
def forward(
|
| 307 |
+
self,
|
| 308 |
+
z: Tensor,
|
| 309 |
+
pair_mask: Tensor,
|
| 310 |
+
use_kernels: bool = False,
|
| 311 |
+
) -> Tensor:
|
| 312 |
+
if not self.training:
|
| 313 |
+
if z.shape[1] > const.chunk_size_threshold:
|
| 314 |
+
chunk_size_tri_attn = 128
|
| 315 |
+
else:
|
| 316 |
+
chunk_size_tri_attn = 512
|
| 317 |
+
else:
|
| 318 |
+
chunk_size_tri_attn = None
|
| 319 |
+
|
| 320 |
+
for layer in self.layers:
|
| 321 |
+
if self.activation_checkpointing:
|
| 322 |
+
z = torch.utils.checkpoint.checkpoint(
|
| 323 |
+
layer,
|
| 324 |
+
z,
|
| 325 |
+
pair_mask,
|
| 326 |
+
chunk_size_tri_attn,
|
| 327 |
+
use_kernels,
|
| 328 |
+
use_reentrant=False,
|
| 329 |
+
)
|
| 330 |
+
else:
|
| 331 |
+
z = layer(
|
| 332 |
+
z,
|
| 333 |
+
pair_mask,
|
| 334 |
+
chunk_size_tri_attn,
|
| 335 |
+
use_kernels,
|
| 336 |
+
)
|
| 337 |
+
return z
|