Download ar/model/mlp.py from AjayRangoji/AR: direct link, hf CLI and curl.
- Browser
- Download file 738 Bytes
-
https://huggingface.co/AjayRangoji/AR/resolve/main/ar/model/mlp.py
- Command line
-
hf download hf://AjayRangoji/AR/ar/model/mlp.py
-
curl -L -o mlp.py https://huggingface.co/AjayRangoji/AR/resolve/main/ar/model/mlp.py
738 Bytes
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| class SwiGLU(nn.Module): | |
| """Gated MLP: one path (with SiLU) gates another, then projects back down.""" | |
| def __init__(self, dim: int, hidden_dim: int): | |
| super().__init__() | |
| # gate and up both widen dim -> hidden_dim; down narrows back | |
| self.gate_proj = nn.Linear(dim, hidden_dim, bias=False) | |
| self.up_proj = nn.Linear(dim, hidden_dim, bias=False) | |
| self.down_proj = nn.Linear(hidden_dim, dim, bias=False) | |
| def forward(self, x: torch.Tensor) -> torch.Tensor: | |
| # SiLU(gate) acts as a soft, per-element valve on the up path | |
| gated = F.silu(self.gate_proj(x)) * self.up_proj(x) | |
| return self.down_proj(gated) |