-
Notifications
You must be signed in to change notification settings - Fork 636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] Adding a conv MLP, following VAN #321
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
30b8dca
Adding a conv MLP, following VAN
blefaudeux f942caf
Renaming to Conv2DFeedforward, more specific I believe
blefaudeux 5a45e84
Catch FF requiring squared context length
blefaudeux 1b13a83
Adding a reference in the README
blefaudeux 6e230ae
removing dead code
blefaudeux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. | ||
# | ||
# This source code is licensed under the BSD license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
|
||
# CREDITS: Largely reusing the code from the reference VAN implementation | ||
# see https://github.com/Visual-Attention-Network | ||
|
||
import math | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
import torch.nn as nn | ||
|
||
from xformers.components import Activation, build_activation | ||
from xformers.components.feedforward import Feedforward, FeedforwardConfig | ||
|
||
from . import register_feedforward | ||
|
||
|
||
@dataclass | ||
class ConvMlpConfig(FeedforwardConfig): | ||
hidden_layer_multiplier: int | ||
dim_model: int | ||
dim_model_out: Optional[int] | ||
act_layer: Activation | ||
dropout: float | ||
|
||
|
||
@register_feedforward("Conv2DFeedforward", ConvMlpConfig) | ||
class Conv2DFeedforward(Feedforward): | ||
""" | ||
A Convolutional feed-forward network, as proposed in VAN_ (Vision Attention Network, Guo et al.) | ||
|
||
.. _VAN: https://arxiv.org/pdf/2202.09741.pdf | ||
""" | ||
|
||
def __init__( | ||
self, | ||
dim_model: int, | ||
hidden_layer_multiplier: int = 1, | ||
dim_model_out: Optional[int] = None, | ||
activation: Activation = Activation.GeLU, | ||
dropout=0.0, | ||
*args, | ||
**kwargs, | ||
): | ||
super().__init__() | ||
out_features = dim_model_out or dim_model | ||
hidden_features = hidden_layer_multiplier * dim_model | ||
|
||
self.conv_mlp = nn.Sequential( | ||
nn.Conv2d(dim_model, hidden_features, 1), | ||
nn.Conv2d( | ||
hidden_features, | ||
hidden_features, | ||
3, | ||
1, | ||
1, | ||
bias=True, | ||
groups=hidden_features, | ||
), | ||
build_activation(activation), | ||
nn.Conv2d(hidden_features, out_features, 1), | ||
nn.Dropout(dropout), | ||
) | ||
|
||
# This feedforward requires a context length which is squared, often due to 2D pooling | ||
self.requires_squared_context = True | ||
|
||
def init_weights(self, **kwargs): | ||
# Follow the original init, but also make it possible to initialize from the outside | ||
def init_module(m: nn.Module): | ||
if isinstance(m, nn.Conv2d): | ||
fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels | ||
fan_out //= m.groups | ||
m.weight.data.normal_(0, math.sqrt(2.0 / fan_out)) | ||
if m.bias is not None: | ||
m.bias.data.zero_() | ||
|
||
self.apply(init_module) | ||
|
||
def forward(self, x): | ||
# The conv layers expect NCHW, we have NLC by default | ||
B, L, C = x.shape | ||
HW = int(math.sqrt(x.shape[-2])) | ||
assert HW**2 == L, "Conv2DFeedforward requires squared context lengths" | ||
|
||
x = x.reshape((B, HW, HW, C)).swapdims(1, -1) | ||
|
||
# The actual FW, including the 2d convolutions | ||
x = self.conv_mlp(x) | ||
|
||
# back to NLC | ||
x = x.transpose(1, -1) | ||
return x.flatten(1, 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does 2D convolutions, meaning that the layer needs to be able to go from [Batch x Context x Embedding] to [Batch x H x W x Embedding]. A solution which is not too intrusive is to force the use of sequences being squared numbers, meaning essentially that we only work with square pictures. It's pretty common in vision codebases, I think that another solution would be to keep track of the original H and W prior to flattening this dimension.