Skip to content

Instantly share code, notes, and snippets.

@cjams
Created January 8, 2026 02:37
Show Gist options
  • Select an option

  • Save cjams/fa490c245bc00bb239970ecfd6737591 to your computer and use it in GitHub Desktop.

Select an option

Save cjams/fa490c245bc00bb239970ecfd6737591 to your computer and use it in GitHub Desktop.
LayerNorm
class LayerNorm():
def __init__(self, device, num_features):
self.out = None
self.gamma = torch.ones(num_features, device=device)
self.bias = torch.zeros(num_features, device=device)
def __call__(self, x: torch.Tensor):
assert x.ndim == 2
H = x.shape[1]
avg = x.mean(dim=1, keepdim=True)
std = torch.sqrt(1 / H * ((x - avg)**2).sum(dim=1, keepdim=True))
self.out = (x - avg) / std * self.gamma + self.bias
return self.out
def params(self):
return [self.gamma, self.bias]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment