-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
Flatten out_proj in MultiHeadAttention #126568
base: main
Are you sure you want to change the base?
Conversation
The MHA has an explicit `nn.Linear` layer for output projections, which is not consistent with the rest of the implementation (s.a. input projections). In addition to that this makes the `nn.MultiHeadAttention` dependent on the linear implementation, as well as making it a nested module. ## Changes: 1. Remove `MultiHeadAttention.out_proj` 2. Add `MultiHeadAttention.out_proj_weight`, `MultiHeadAttention.out_proj_bias`. Add the functional linear for forward 3. Add initialization 4. Change expected string to hide the `out_proj` 5. Adds forward compatibility to be able to load old models ## Potential issues: * Initialization: `nn.Linear` initilizes its weight as uniform Kaiming, while this PR uses uniform Xavier. In addition to that, bias in the `nn.Linear` is uniform based on fan-in/fan-out, while here it is constant 0. This means that numerically this will be different from the original implementation. * *Option 1: Accept current change* -- this is more consistent with the rest of the implementation * *Option 2: Duplicate initialization logic from Linear* -- this is consistent with the initialization from before this PR ## Tests There are no new tests, as no new logic or change in functionality is introduced.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/126568
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 245d312 with merge base 31ea829 (): This comment was automatically generated by Dr. CI and updates every 15 minutes. |
cc @jerryzh168 @HDCharles I don't think there is a need for the |
This is BC-breaking, no? |
@jbschlosser It might be for chckpoints: in case a checkpoint is created post-PR, and being loaded into pre-PR. I can add BC-compatibility by saving the weights as |
The MHA has an explicit
nn.Linear
layer for output projections, which is not consistent with the rest of the implementation (s.a. input projections). In addition to that this makes thenn.MultiHeadAttention
dependent on the linear implementation, as well as making it a nested module.Changes:
MultiHeadAttention.out_proj
MultiHeadAttention.out_proj_weight
,MultiHeadAttention.out_proj_bias
. Add the functional linear for forwardout_proj
Potential issues:
nn.Linear
initilizes its weight as uniform Kaiming, while this PR uses uniform Xavier. In addition to that, bias in thenn.Linear
is uniform based on fan-in/fan-out, while here it is constant 0. This means that numerically this will be different from the original implementation.Tests
There are no new tests, as no new logic or change in functionality is introduced.
Potentially fixes #60165
cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @penguinwu @fegin @XilunWu @wanchaol @fduwjj @wz337 @tianyu-l @wconstab @yf225 @chauhang @d4l3k