blob: 99a5291e02baa08b31f88696e257849d7acccf86 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
"""Normalization layers for transfromers.
Copied from lucidrains:
https://github.com/lucidrains/x-transformers/blob/main/x_transformers/x_transformers.py
"""
from typing import Callable, Dict
import torch
from torch import nn
from torch import Tensor
class Rezero(nn.Module):
def __init__(self, fn: Callable) -> None:
super().__init__()
self.fn = fn
self.g = nn.Parameter(torch.zeros(1))
def forward(self, x: Tensor, **kwargs: Dict) -> Tensor:
x, *rest = self.fn(x, **kwargs)
return (x * self.g, *rest)
|