2021-03-18 11:32:26 +01:00
|
|
|
#####################################################
|
|
|
|
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.03 #
|
|
|
|
#####################################################
|
2021-03-18 13:15:50 +01:00
|
|
|
import torch
|
2021-03-17 11:06:29 +01:00
|
|
|
import torch.nn as nn
|
|
|
|
|
2021-03-18 11:32:26 +01:00
|
|
|
import math
|
|
|
|
from typing import Optional, Union
|
|
|
|
|
|
|
|
import spaces
|
2021-03-18 13:15:50 +01:00
|
|
|
from .super_module import SuperModule
|
|
|
|
from .super_module import SuperRunMode
|
2021-03-18 11:32:26 +01:00
|
|
|
|
|
|
|
IntSpaceType = Union[int, spaces.Integer, spaces.Categorical]
|
|
|
|
BoolSpaceType = Union[bool, spaces.Categorical]
|
2021-03-18 08:04:14 +01:00
|
|
|
|
2021-03-18 09:02:55 +01:00
|
|
|
|
|
|
|
class SuperLinear(SuperModule):
|
|
|
|
"""Applies a linear transformation to the incoming data: :math:`y = xA^T + b`"""
|
2021-03-18 08:04:14 +01:00
|
|
|
|
2021-03-18 11:32:26 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
in_features: IntSpaceType,
|
|
|
|
out_features: IntSpaceType,
|
|
|
|
bias: BoolSpaceType = True,
|
|
|
|
) -> None:
|
2021-03-18 09:02:55 +01:00
|
|
|
super(SuperLinear, self).__init__()
|
2021-03-18 11:32:26 +01:00
|
|
|
|
|
|
|
# the raw input args
|
|
|
|
self._in_features = in_features
|
|
|
|
self._out_features = out_features
|
|
|
|
self._bias = bias
|
2021-03-18 13:44:22 +01:00
|
|
|
# weights to be optimized
|
2021-03-18 13:15:50 +01:00
|
|
|
self._super_weight = torch.nn.Parameter(
|
2021-03-18 11:32:26 +01:00
|
|
|
torch.Tensor(self.out_features, self.in_features)
|
|
|
|
)
|
2021-03-18 13:15:50 +01:00
|
|
|
if self.bias:
|
|
|
|
self._super_bias = torch.nn.Parameter(torch.Tensor(self.out_features))
|
2021-03-18 08:04:14 +01:00
|
|
|
else:
|
2021-03-18 11:32:26 +01:00
|
|
|
self.register_parameter("_super_bias", None)
|
2021-03-18 08:04:14 +01:00
|
|
|
self.reset_parameters()
|
|
|
|
|
2021-03-18 11:32:26 +01:00
|
|
|
@property
|
|
|
|
def in_features(self):
|
|
|
|
return spaces.get_max(self._in_features)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def out_features(self):
|
|
|
|
return spaces.get_max(self._out_features)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def bias(self):
|
|
|
|
return spaces.has_categorical(self._bias, True)
|
|
|
|
|
2021-03-18 13:15:50 +01:00
|
|
|
def abstract_search_space(self):
|
2021-03-18 13:44:22 +01:00
|
|
|
root_node = spaces.VirtualNode(id(self))
|
|
|
|
if not spaces.is_determined(self._in_features):
|
2021-03-18 13:46:50 +01:00
|
|
|
root_node.append("_in_features", self._in_features)
|
2021-03-18 13:44:22 +01:00
|
|
|
if not spaces.is_determined(self._out_features):
|
2021-03-18 13:46:50 +01:00
|
|
|
root_node.append("_out_features", self._out_features)
|
2021-03-18 13:44:22 +01:00
|
|
|
if not spaces.is_determined(self._bias):
|
2021-03-18 13:46:50 +01:00
|
|
|
root_node.append("_bias", self._bias)
|
2021-03-18 13:44:22 +01:00
|
|
|
return root_node
|
2021-03-18 13:15:50 +01:00
|
|
|
|
2021-03-18 08:04:14 +01:00
|
|
|
def reset_parameters(self) -> None:
|
2021-03-18 11:32:26 +01:00
|
|
|
nn.init.kaiming_uniform_(self._super_weight, a=math.sqrt(5))
|
|
|
|
if self.bias:
|
|
|
|
fan_in, _ = nn.init._calculate_fan_in_and_fan_out(self._super_weight)
|
2021-03-18 08:04:14 +01:00
|
|
|
bound = 1 / math.sqrt(fan_in)
|
2021-03-18 11:32:26 +01:00
|
|
|
nn.init.uniform_(self._super_bias, -bound, bound)
|
2021-03-18 08:04:14 +01:00
|
|
|
|
2021-03-18 13:15:50 +01:00
|
|
|
def forward_raw(self, input: torch.Tensor) -> torch.Tensor:
|
2021-03-18 11:32:26 +01:00
|
|
|
return F.linear(input, self._super_weight, self._super_bias)
|
2021-03-18 08:04:14 +01:00
|
|
|
|
|
|
|
def extra_repr(self) -> str:
|
2021-03-18 09:02:55 +01:00
|
|
|
return "in_features={:}, out_features={:}, bias={:}".format(
|
2021-03-18 11:32:26 +01:00
|
|
|
self.in_features, self.out_features, self.bias
|
2021-03-18 08:04:14 +01:00
|
|
|
)
|
|
|
|
|
2021-03-18 09:02:55 +01:00
|
|
|
|
2021-03-18 08:04:14 +01:00
|
|
|
class SuperMLP(nn.Module):
|
2021-03-18 09:02:55 +01:00
|
|
|
# MLP: FC -> Activation -> Drop -> FC -> Drop
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
in_features,
|
|
|
|
hidden_features: Optional[int] = None,
|
|
|
|
out_features: Optional[int] = None,
|
|
|
|
act_layer=nn.GELU,
|
|
|
|
drop: Optional[float] = None,
|
|
|
|
):
|
|
|
|
super(MLP, self).__init__()
|
|
|
|
out_features = out_features or in_features
|
|
|
|
hidden_features = hidden_features or in_features
|
|
|
|
self.fc1 = nn.Linear(in_features, hidden_features)
|
|
|
|
self.act = act_layer()
|
|
|
|
self.fc2 = nn.Linear(hidden_features, out_features)
|
|
|
|
self.drop = nn.Dropout(drop or 0)
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
x = self.fc1(x)
|
|
|
|
x = self.act(x)
|
|
|
|
x = self.drop(x)
|
|
|
|
x = self.fc2(x)
|
|
|
|
x = self.drop(x)
|
|
|
|
return x
|