Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[transformer] add qk norm #2588

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions wenet/transformer/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,17 @@ def __init__(self,
use_sdpa: bool = False,
n_kv_head: Optional[int] = None,
head_dim: Optional[int] = None,
qk_norm: bool = False,
style='google'):

super().__init__(n_head, n_feat, dropout_rate, query_bias, key_bias,
value_bias, use_sdpa, n_kv_head, head_dim)
self.qk_norm = qk_norm
# https://arxiv.org/pdf/2302.05442
if self.qk_norm:
from wenet.utils.class_utils import WENET_NORM_CLASSES
self.q_norm = WENET_NORM_CLASSES['rms_norm'](self.d_k, eps=1e-6)
self.k_norm = WENET_NORM_CLASSES['rms_norm'](self.d_k, eps=1e-6)
self.style = style

def forward(
Expand Down Expand Up @@ -655,11 +663,15 @@ def forward(
q = self._forward_linearx('query', query, head_first=False)
k = self._forward_linearx('key', key, head_first=False)
v = self._forward_linearx('value', value, head_first=False)

# NOTE(Mddct): In order to make the code easier to read,
# these two lines are not placed in MultiHeadedAttention.
q = WENET_APPLY_ROTARY_EMB[self.style](q, pos_emb)
k = WENET_APPLY_ROTARY_EMB[self.style](k, pos_emb)

if self.qk_norm:
q = self.q_norm(q)
k = self.k_norm(k)
k, v, new_cache = self._update_kv_and_cache(k,
v,
cache,
Expand Down
Loading