Skip to content

Commit

Permalink
use the latest layernorm without bias in pytorch if available
Browse files Browse the repository at this point in the history
  • Loading branch information
lucidrains committed Jan 5, 2024
1 parent 7916859 commit bb4d8fb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name = 'x-transformers',
packages = find_packages(exclude=['examples']),
version = '1.27.0',
version = '1.27.1',
license='MIT',
description = 'X-Transformers - Pytorch',
author = 'Phil Wang',
Expand Down
5 changes: 4 additions & 1 deletion x_transformers/x_transformers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import math
from random import random
from typing import Dict
from packaging import version

import torch
from torch import nn, einsum, Tensor
Expand Down Expand Up @@ -502,7 +503,6 @@ class LayerNorm(nn.Module):
def __init__(self, dim):
"""
bias-less layernorm has been shown to be more stable. most newer models have moved towards rmsnorm, also bias-less
latest pytorch actually has a way to turn this off in nn.LayerNorm
"""
super().__init__()
self.gamma = nn.Parameter(torch.ones(dim))
Expand All @@ -511,6 +511,9 @@ def __init__(self, dim):
def forward(self, x):
return F.layer_norm(x, x.shape[-1:], self.gamma, self.beta)

if version.parse(torch.__version__) >= version.parse('2.1.0'):
LayerNorm = partial(nn.LayerNorm, bias = False)

class RMSNorm(nn.Module):
def __init__(self, dim):
super().__init__()
Expand Down

0 comments on commit bb4d8fb

Please sign in to comment.