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

Flatten out_proj in MultiHeadAttention #126568

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

z-a-f
Copy link
Contributor

@z-a-f z-a-f commented May 17, 2024

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.

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

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.
Copy link

pytorch-bot bot commented May 17, 2024

🔗 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 Failures

As of commit 245d312 with merge base 31ea829 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added oncall: distributed Add this issue/PR to distributed oncall triage queue release notes: distributed (fsdp) release notes category labels May 17, 2024
@z-a-f
Copy link
Contributor Author

z-a-f commented May 17, 2024

cc @jerryzh168 @HDCharles I don't think there is a need for the NonDynamicallyQuantizableLinear anymore. However, it is worth checking for quantization logic changes.

@jbschlosser
Copy link
Contributor

This is BC-breaking, no?

@z-a-f
Copy link
Contributor Author

z-a-f commented May 17, 2024

@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 out_proj.weight and out_proj.bias with a deprecation warning, but that would mean the checkpoint state-dict will have structure inconsistent with the MHA, wdyt?

@albanD albanD removed their request for review May 20, 2024 15:11
@drisspg drisspg added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
oncall: distributed Add this issue/PR to distributed oncall triage queue open source release notes: distributed (fsdp) release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

torch.load non backwards compatible on Transformer between 1.8.1 and 1.9.0
4 participants