Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Fix] fix SiLU activation #447

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ repos:
rev: 0.7.9
hooks:
- id: mdformat
language_version: python3.7
args: ["--number", "--table-width", "200"]
additional_dependencies:
- mdformat-openmmlab
Expand Down
53 changes: 28 additions & 25 deletions mmgen/models/architectures/ddpm/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,39 @@ def forward(self, x, y):
return x


@ACTIVATION_LAYERS.register_module()
class SiLU(nn.Module):
r"""Applies the Sigmoid Linear Unit (SiLU) function, element-wise.
The SiLU function is also known as the swish function.
Args:
input (bool, optional): Use inplace operation or not.
Defaults to `False`.
"""
if 'SiLU' not in ACTIVATION_LAYERS:

def __init__(self, inplace=False):
super().__init__()
if digit_version(
torch.__version__) < digit_version('1.7.0') and inplace:
mmcv.print_log('Inplace version of \'SiLU\' is not supported for '
f'torch < 1.7.0, found \'{torch.version}\'.')
self.inplace = inplace

def forward(self, x):
"""Forward function for SiLU.
@ACTIVATION_LAYERS.register_module()
class SiLU(nn.Module):
r"""Applies the Sigmoid Linear Unit (SiLU) function, element-wise.
The SiLU function is also known as the swish function.
Args:
x (torch.Tensor): Input tensor.

Returns:
torch.Tensor: Tensor after activation.
input (bool, optional): Use inplace operation or not.
Defaults to `False`.
"""

if digit_version(torch.__version__) < digit_version('1.7.0'):
return x * torch.sigmoid(x)
def __init__(self, inplace=False):
super().__init__()
if digit_version(
torch.__version__) < digit_version('1.7.0') and inplace:
mmcv.print_log('Inplace version of \'SiLU\' is not supported '
'for torch < 1.7.0, found '
f'\'{torch.version}\'.')
self.inplace = inplace

def forward(self, x):
"""Forward function for SiLU.
Args:
x (torch.Tensor): Input tensor.

Returns:
torch.Tensor: Tensor after activation.
"""

if digit_version(torch.__version__) < digit_version('1.7.0'):
return x * torch.sigmoid(x)

return F.silu(x, inplace=self.inplace)
return F.silu(x, inplace=self.inplace)


@MODULES.register_module()
Expand Down