Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,002 Bytes
1ea89dd |
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 |
import torch
import torch.nn as nn
from unik3d.utils.misc import default
from .activation import SwiGLU
class MLP(nn.Module):
def __init__(
self,
input_dim: int,
expansion: int = 4,
dropout: float = 0.0,
gated: bool = False,
output_dim: int | None = None,
):
super().__init__()
if gated:
expansion = int(expansion * 2 / 3)
hidden_dim = int(input_dim * expansion)
output_dim = default(output_dim, input_dim)
self.norm = nn.LayerNorm(input_dim)
self.proj1 = nn.Linear(input_dim, hidden_dim)
self.proj2 = nn.Linear(hidden_dim, output_dim)
self.act = nn.GELU() if not gated else SwiGLU()
self.dropout = nn.Dropout(dropout) if dropout > 0.0 else nn.Identity()
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.norm(x)
x = self.proj1(x)
x = self.act(x)
x = self.proj2(x)
x = self.dropout(x)
return x
|