Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion monai/networks/blocks/selfattention.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ def __init__(

self.num_heads = num_heads
self.hidden_input_size = hidden_input_size if hidden_input_size else hidden_size
self.out_proj = nn.Linear(self.inner_dim, self.hidden_input_size)
self.out_proj: Union[nn.Linear, nn.Identity]
if include_fc:
self.out_proj = nn.Linear(self.inner_dim, self.hidden_input_size)
else:
self.out_proj = nn.Identity()

self.qkv: Union[nn.Linear, nn.Identity]
self.to_q: Union[nn.Linear, nn.Identity]
Expand Down
6 changes: 3 additions & 3 deletions monai/networks/nets/diffusion_model_unet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1847,9 +1847,9 @@ def load_old_state_dict(self, old_state_dict: dict, verbose=False) -> None:
new_state_dict[f"{block}.attn.to_v.bias"] = old_state_dict.pop(f"{block}.to_v.bias")

# projection
new_state_dict[f"{block}.attn.out_proj.weight"] = old_state_dict.pop(f"{block}.proj_attn.weight")
new_state_dict[f"{block}.attn.out_proj.bias"] = old_state_dict.pop(f"{block}.proj_attn.bias")

if f"{block}.attn.out_proj.weight" in new_state_dict and f"{block}.attn.out_proj.bias" in new_state_dict:
new_state_dict[f"{block}.attn.out_proj.weight"] = old_state_dict.pop(f"{block}.proj_attn.weight")
new_state_dict[f"{block}.attn.out_proj.bias"] = old_state_dict.pop(f"{block}.proj_attn.bias")
# fix the cross attention blocks
cross_attention_blocks = [
k.replace(".out_proj.weight", "")
Expand Down
21 changes: 21 additions & 0 deletions tests/networks/blocks/test_selfattention.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,27 @@ def test_flash_attention(self):
out_2 = block_wo_flash_attention(test_data)
assert_allclose(out_1, out_2, atol=1e-4)

@parameterized.expand([[True], [False]])
def test_no_extra_weights_if_no_fc(self, include_fc):
input_param = {
"hidden_size": 360,
"num_heads": 4,
"dropout_rate": 0.0,
"rel_pos_embedding": None,
"input_size": (16, 32),
"include_fc": include_fc,
"use_combined_linear": use_combined_linear,
}
net = SABlock(**input_param)
if not include_fc:
self.assertNotIn("out_proj.weight", net.state_dict())
self.assertNotIn("out_proj.bias", net.state_dict())
self.assertIsInstance(net.out_proj, torch.nn.Identity)
else:
self.assertIn("out_proj.weight", net.state_dict())
self.assertIn("out_proj.bias", net.state_dict())
self.assertIsInstance(net.out_proj, torch.nn.Linear)


if __name__ == "__main__":
unittest.main()
Loading