Spaces:
Runtime error
Runtime error
File size: 1,120 Bytes
e4bd7f9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
from torch import nn, Tensor
from typing import Union, Optional, Tuple
class BaseProjector(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x: Tensor) -> Tensor:
raise NotImplementedError
class LinearProjector(BaseProjector):
def __init__(self, in_dim, out_dim):
super().__init__()
self.fc = nn.Linear(in_dim, out_dim)
def forward(self, x: Tensor) -> Tensor:
return self.fc(x)
class AdapterProjector(BaseProjector):
def __init__(self, in_dim, mid_dim, out_dim):
super().__init__()
self.fc = nn.Sequential(
nn.Linear(in_dim, mid_dim, bias=False),
nn.ReLU(inplace=True),
nn.Linear(mid_dim, out_dim, bias=False),
nn.ReLU(inplace=True)
)
def forward(self, x: Tensor) -> Tensor:
return self.fc(x)
def create_projectors(dims):
if len(dims) == 0:
return nn.Identity()
elif len(dims) == 2:
return LinearProjector(*dims)
elif len(dims) == 3:
return AdapterProjector(*dims)
else:
raise NotImplementedError
|