-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from lucidrains/rotary
replace shaws with rotary embeddings
- Loading branch information
Showing
4 changed files
with
59 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import torch | ||
from torch import nn, einsum | ||
from einops import rearrange, repeat | ||
|
||
class SinusoidalEmbeddings(nn.Module): | ||
def __init__(self, dim): | ||
super().__init__() | ||
inv_freq = 1. / (10000 ** (torch.arange(0, dim, 2).float() / dim)) | ||
self.register_buffer('inv_freq', inv_freq) | ||
|
||
def forward(self, x): | ||
n = x.shape[-2] | ||
t = torch.arange(n, device = x.device).type_as(self.inv_freq) | ||
sinusoid_inp = torch.einsum('i , j -> i j', t, self.inv_freq) | ||
emb = torch.cat((sinusoid_inp.sin(), sinusoid_inp.cos()), dim=-1) | ||
return emb[None, :, :] | ||
|
||
def rotate_every_two(x): | ||
x = rearrange(x, '... (d j) -> ... d j', j = 2) | ||
x1, x2 = x.unbind(dim = -1) | ||
x = torch.stack((-x2, x1), dim = -1) | ||
return rearrange(x, '... d j -> ... (d j)') | ||
|
||
def apply_rotary_pos_emb(q, k, sinu_pos): | ||
sinu_pos = rearrange(sinu_pos, '() n (j d) -> n j d', j = 2) | ||
sin, cos = sinu_pos.unbind(dim = -2) | ||
sin, cos = map(lambda t: repeat(t, 'b n -> b (n j)', j = 2), (sin, cos)) | ||
q, k = map(lambda t: (t * cos) + (rotate_every_two(t) * sin), (q, k)) | ||
return q, k |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters