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

Beit why no k bias in attention? #510

Closed
cqliheping opened this issue Nov 10, 2021 · 1 comment
Closed

Beit why no k bias in attention? #510

cqliheping opened this issue Nov 10, 2021 · 1 comment
Assignees

Comments

@cqliheping
Copy link

cqliheping commented Nov 10, 2021

The k bias is always zero in code. Is there any reason for this? This is different from the normal implement.

qkv_bias = torch.cat((self.q_bias, torch.zeros_like(self.v_bias, requires_grad=False), self.v_bias))

In my test. when finetune, k bias has little affect on performance. But I do not have a test on pretrain.

@donglixp
Copy link
Contributor

Both (i.e., with or without key.bias) are equivalent in terms of calculation results. They are canceled by the softmax function.

Softmax(q,k) = exp(q.weight * key.weight + q.bias * key.weight + q.weight * key.bias + q.bias * key.bias) / Z

Because the query is the same over all the keys, so the term (q.weight * key.bias + q.bias * key.bias) remains the same across all the keys, which in turn can be cancelled without affecting the softmax results.

exp(a)/(exp(a)+ exp(b)) == exp(a+C)/(exp(a+C)+ exp(b+C))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants