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

Inconsistent usage of the linear layer in the SABlock #7992

Closed
KumoLiu opened this issue Aug 5, 2024 · 2 comments · Fixed by #7996
Closed

Inconsistent usage of the linear layer in the SABlock #7992

KumoLiu opened this issue Aug 5, 2024 · 2 comments · Fixed by #7996

Comments

@KumoLiu
Copy link
Contributor

KumoLiu commented Aug 5, 2024

There are three linear in the original generative repo which now is combined within one linear layer. We need add an argument to make it consistent with before.

https://github.com/Project-MONAI/GenerativeModels/blob/main/generative/networks/nets/diffusion_model_unet.py#L379-L381

self.qkv = nn.Linear(self.hidden_input_size, self.inner_dim * 3, bias=qkv_bias)

@ericspod
Copy link
Member

ericspod commented Aug 5, 2024

Perhaps we change the current implementation to match this instead? I don't know if it's significant but making this an option will be problematic with Torchscript which doesn't like class variables being optional.

@ericspod ericspod mentioned this issue Aug 5, 2024
7 tasks
KumoLiu added a commit to KumoLiu/MONAI that referenced this issue Aug 6, 2024
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@KumoLiu
Copy link
Contributor Author

KumoLiu commented Aug 6, 2024

I did a quick test, looks the outcome shows significantly different. In this PR, I utilized identity to get around the torchscript conversion issue. Could you please help review it, thanks!

import torch.nn as nn
import torch
from monai.utils import set_determinism

set_determinism(0)

hidden_input_size = 360
inner_dim = 360
qkv_bias = True
qkv = nn.Linear(hidden_input_size, inner_dim * 3, bias=qkv_bias)


q = nn.Linear(hidden_input_size, inner_dim, bias=qkv_bias)
k = nn.Linear(hidden_input_size, inner_dim, bias=qkv_bias)
v = nn.Linear(hidden_input_size, inner_dim, bias=qkv_bias)

input = torch.rand(2, 512, 360)
qkv_out = qkv(input)
q_out = q(input)
k_out = k(input)
v_out = v(input)

print((qkv_out - torch.cat([q_out, k_out, v_out], dim=-1)).abs().sum().item()) --> 410444.28125

@KumoLiu KumoLiu closed this as completed in 069519d Aug 9, 2024
rcremese pushed a commit to rcremese/MONAI that referenced this issue Sep 2, 2024
…roject-MONAI#7996)

Fixes Project-MONAI#7991
Fixes Project-MONAI#7992

### Description
Add `include_fc` and `use_combined_linear` argument in the `SABlock`.


### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
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

Successfully merging a pull request may close this issue.

2 participants