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

Fixing gradient in sincos positional encoding in monai/networks/blocks/patchembedding.py #7564

Merged
merged 3 commits into from
Mar 27, 2024
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
1 change: 1 addition & 0 deletions monai/networks/blocks/patchembedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def __init__(
with torch.no_grad():
pos_embeddings = build_sincos_position_embedding(grid_size, hidden_size, spatial_dims)
KumoLiu marked this conversation as resolved.
Show resolved Hide resolved
self.position_embeddings.data.copy_(pos_embeddings.float())
self.position_embeddings.requires_grad = False
else:
raise ValueError(f"pos_embed_type {self.pos_embed_type} not supported.")

Expand Down
2 changes: 1 addition & 1 deletion monai/networks/blocks/pos_embed_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def build_sincos_position_embedding(
temperature (float): The temperature for the sin-cos position embedding.

Returns:
pos_embed (nn.Parameter): The sin-cos position embedding as a learnable parameter.
pos_embed (nn.Parameter): The sin-cos position embedding as a fixed parameter.
"""

if spatial_dims == 2:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_patchembedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ def test_shape(self, input_param, input_shape, expected_shape):
result = net(torch.randn(input_shape))
self.assertEqual(result.shape, expected_shape)

def test_sincos_pos_embed(self):
net = PatchEmbeddingBlock(
in_channels=1,
img_size=(32, 32, 32),
patch_size=(8, 8, 8),
hidden_size=96,
num_heads=8,
pos_embed_type="sincos",
dropout_rate=0.5,
)

self.assertEqual(net.position_embeddings.requires_grad, False)

def test_learnable_pos_embed(self):
net = PatchEmbeddingBlock(
in_channels=1,
img_size=(32, 32, 32),
patch_size=(8, 8, 8),
hidden_size=96,
num_heads=8,
pos_embed_type="learnable",
dropout_rate=0.5,
)

self.assertEqual(net.position_embeddings.requires_grad, True)

def test_ill_arg(self):
with self.assertRaises(ValueError):
PatchEmbeddingBlock(
Expand Down
Loading