-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[transformer] add rope for transformer/conformer (#2458)
* [transformer] add rope for transformers/conformer * it works * rm comment * fix typo * fix init * add assert attn type for transformer * fix pos_emb in transformer * llama rope and google rope work!
- Loading branch information
Showing
6 changed files
with
212 additions
and
14 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
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,45 @@ | ||
import torch | ||
|
||
|
||
# copy from:https://github.com/google/gemma_pytorch/blob/main/gemma/model.py#L84 | ||
def precompute_freqs_cis(dim: int, | ||
end: int, | ||
theta: float = 10000.0) -> torch.Tensor: | ||
"""Precomputes the frequency cis.""" | ||
freqs = 1.0 / (theta**(torch.arange(0, dim, 2)[:(dim // 2)].float() / dim)) | ||
t = torch.arange(end, device=freqs.device) | ||
freqs = torch.outer(t, freqs).float() | ||
freqs_cis = torch.polar(torch.ones_like(freqs), freqs) # complex64 | ||
return freqs_cis | ||
|
||
|
||
# modified from: | ||
# https://github.com/google/gemma_pytorch/blob/main/gemma/model.py#L95 | ||
def google_apply_rotary_emb(x: torch.Tensor, | ||
freqs_cis: torch.Tensor) -> torch.Tensor: | ||
"""Applies the rotary embedding to the query and key tensors.""" | ||
x_ = torch.view_as_complex( | ||
torch.stack(torch.chunk(x.float(), 2, dim=-1), dim=-1)) | ||
x_out = torch.view_as_real(x_ * freqs_cis).type_as(x) | ||
x_out = torch.cat(torch.chunk(x_out, 2, dim=-1), dim=-2) | ||
x_out = x_out.reshape(x_out.shape[0], x_out.shape[1], x_out.shape[2], -1) | ||
return x_out | ||
|
||
|
||
def reshape_for_broadcast(freqs_cis: torch.Tensor, x: torch.Tensor): | ||
ndim = x.ndim | ||
assert 0 <= 1 < ndim | ||
assert freqs_cis.shape[2:] == (x.shape[1], x.shape[-1]) | ||
# 2 is seq_len in wenet | ||
shape = [ | ||
d if i == 2 or i == ndim - 1 else 1 for i, d in enumerate(x.shape) | ||
] | ||
return freqs_cis.view(*shape) | ||
|
||
|
||
def llama_apply_rotary_emb(x: torch.Tensor, | ||
freqs_cis: torch.Tensor) -> torch.Tensor: | ||
x_ = torch.view_as_complex(x.float().reshape(*x.shape[:-1], -1, 2)) | ||
freqs_cis = reshape_for_broadcast(freqs_cis, x_) | ||
x_out = torch.view_as_real(x_ * freqs_cis).flatten(3) | ||
return x_out.type_as(x) |