Skip to content

Commit

Permalink
fix funnel transformer so that downsample is query-only
Browse files Browse the repository at this point in the history
  • Loading branch information
lucidrains committed Nov 6, 2020
1 parent 65cd8ec commit f2063a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name = 'x-transformers',
packages = find_packages(exclude=['examples']),
version = '0.0.24',
version = '0.0.25',
license='MIT',
description = 'X-Transformers - Pytorch',
author = 'Phil Wang',
Expand Down
17 changes: 12 additions & 5 deletions x_transformers/funnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ def fn(x, *args, **kwargs):

class AttentionWithDownsample(Attention):
def forward(self, x, num_memory_tokens = 0, downsample = False, **kwargs):
mask = None

if downsample:
b, n, *_ = x.shape
b, n, *_, orig_x = *x.shape, x
is_odd = (n % 2) == 1

mem, x = x[:, :num_memory_tokens], x[:, num_memory_tokens:]
Expand All @@ -33,14 +31,23 @@ def forward(self, x, num_memory_tokens = 0, downsample = False, **kwargs):
x = torch.cat((mem, x, remainder), dim = 1)

mask = kwargs.pop('mask', None)
orig_mask = mask

if exists(mask):
mask = mask[:, num_memory_tokens:]
mask = F.pad(mask, (0, 1), value = False) if is_odd else mask
mask = mask.reshape(b, -1, 2).any(dim = -1)
mask = F.pad(mask, (num_memory_tokens, 0), value = True)
kwargs.update(mask = mask)

return super().forward(x, **kwargs), mask
return super().forward(
x,
mask = mask,
context = orig_x,
context_mask = orig_mask,
**kwargs
), mask

return super().forward(x, **kwargs), None

class FunnelEncoder(nn.Module):
def __init__(self, dim, depths, heads = 8, use_scalenorm = False, rel_pos_bias = False, num_memory_tokens = 0, **kwargs):
Expand Down

0 comments on commit f2063a4

Please sign in to comment.