AR / ar /model /mlp.py
AjayRangoji's picture
Upload AR-4B pretraining codebase, configs, and cluster scripts
2ae6691 verified
Raw History Blame Contribute Delete
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)