Skip to content

Commit

Permalink
new strategy for repeat consecutive, to get ready for more efficient …
Browse files Browse the repository at this point in the history
…local attention atom transformer biasing
  • Loading branch information
lucidrains committed May 28, 2024
1 parent 74f062a commit 8f9e41d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
32 changes: 21 additions & 11 deletions alphafold3_pytorch/alphafold3.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,7 @@ def repeat_consecutive_with_lens(
lens: Int['b n'],
) -> Float['b m ...'] | Bool['b m']:

is_bool = feats.dtype == torch.bool
feats = feats.float()

device = feats.device
device, dtype = feats.device, feats.dtype

batch, seq, *dims = feats.shape

Expand All @@ -174,25 +171,38 @@ def repeat_consecutive_with_lens(
# create output tensor + a sink position on the very right (index max_len)

total_lens = lens.sum(dim = -1)
output_mask = lens_to_mask(total_lens)

max_len = total_lens.amax()

output = torch.zeros((batch, max_len + 1, *dims), device = device)
output_indices = torch.zeros((batch, max_len + 1), device = device, dtype = torch.long)

indices.masked_fill_(~mask, max_len) # scatter to sink position for padding
indices = rearrange(indices, 'b n w -> b (n w)')

feats = repeat(feats, 'b n ... -> b (n w) ...', w = window_size)

# scatter

output = einx.set_at('b [m] ..., b nw, b nw ... -> b [m] ...', output, indices, feats)
seq_arange = torch.arange(seq, device = device)
seq_arange = repeat(seq_arange, 'n -> (n w)', w = window_size)

output_indices = einx.set_at('b [m], b nw, nw -> b [m]', output_indices, indices, seq_arange)

# remove sink

output = output[:, :-1]
output_indices = output_indices[:, :-1]

# gather

output = einx.get_at('b [n] ..., b m -> b m ...', feats, output_indices)

# final mask

mask_value = False if dtype == torch.bool else 0

if is_bool:
output = output.bool()
output = einx.where(
'b n, b n ..., -> b n ...',
output_mask, output, mask_value
)

return output

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.0.48"
version = "0.0.49"
description = "Alphafold 3 - Pytorch"
authors = [
{ name = "Phil Wang", email = "[email protected]" }
Expand Down

0 comments on commit 8f9e41d

Please sign in to comment.