Skip to content

Commit

Permalink
make attention softmax done in full precision customizable, bfloat16 …
Browse files Browse the repository at this point in the history
…should be ok
  • Loading branch information
lucidrains committed Sep 29, 2024
1 parent 35b6bc1 commit 165d795
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions alphafold3_pytorch/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ def __init__(
num_memory_kv: int = 0,
enable_attn_softclamp = False,
attn_softclamp_value = 50.,
init_gate_bias = -2.
init_gate_bias = -2.,
softmax_full_precision = False
):
super().__init__()
"""
Expand All @@ -201,6 +202,7 @@ def __init__(
window_size = window_size,
enable_attn_softclamp = enable_attn_softclamp,
attn_softclamp_value = attn_softclamp_value,
softmax_full_precision = softmax_full_precision
)

self.split_heads = Rearrange('b n (h d) -> b h n d', h = heads)
Expand Down Expand Up @@ -279,7 +281,8 @@ def __init__(
window_size = None,
scale: float | None = None,
enable_attn_softclamp = False,
attn_softclamp_value = 50.
attn_softclamp_value = 50.,
softmax_full_precision = False
):
super().__init__()
"""
Expand Down Expand Up @@ -309,6 +312,9 @@ def __init__(
self.enable_attn_softclamp = enable_attn_softclamp
self.attn_softclamp_value = attn_softclamp_value

# whether to use full precision for softmax
self.softmax_full_precision = softmax_full_precision

@typecheck
def local_attn(
self,
Expand Down Expand Up @@ -505,9 +511,16 @@ def forward(
mask, sim, max_neg_value(sim)
)

# attention cast float32 - in case there are instabilities with float16

softmax_kwargs = dict()

if self.softmax_full_precision:
softmax_kwargs.update(dtype = torch.float32)

# attention

attn = sim.softmax(dim = -1, dtype = torch.float32)
attn = sim.softmax(dim = -1, **softmax_kwargs)
attn = attn.to(dtype)

attn = self.attn_dropout(attn)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "alphafold3-pytorch"
version = "0.5.49"
version = "0.5.50"
description = "Alphafold 3 - Pytorch"
authors = [
{ name = "Phil Wang", email = "lucidrains@gmail.com" },
Expand Down

0 comments on commit 165d795

Please sign in to comment.