Skip to content

Make sure CLIP resized pos_embed is contiguous #1986

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

Merged
merged 1 commit into from
Nov 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def test_tile_resize_position_embedding(self, params):
embedding, tgt_max_num_tiles
)

assert resized_pos_embed.is_contiguous()
assert_expected(resized_pos_embed, expected_output, atol=1e-3, rtol=1e-4)

@pytest.mark.parametrize("params", local_pos_emb_test_cases)
Expand All @@ -215,6 +216,7 @@ def test_resize_local_position_embedding(self, params):
)
)

assert resized_pos_embed.is_contiguous()
assert_expected(resized_pos_embed, expected_output, atol=1e-3, rtol=1e-4)

@pytest.mark.parametrize("params", global_pos_emb_test_cases)
Expand All @@ -230,6 +232,7 @@ def test_resize_global_position_embedding(self, params):
)
)

assert resized_pos_embed.is_contiguous()
assert_expected(resized_pos_embed, expected_output, atol=1e-3, rtol=1e-4)

@pytest.mark.parametrize(
Expand Down
6 changes: 3 additions & 3 deletions torchtune/models/clip/_position_embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def _resize_local_position_embedding(
# add cls token back in
local_pos_embed = torch.cat([cls_token, local_pos_embed], dim=0)

return local_pos_embed
return local_pos_embed.contiguous()

# TODO: Switch to public method after 2.5 is stable
@staticmethod
Expand Down Expand Up @@ -436,7 +436,7 @@ def _resize_global_position_embedding(
# add cls token back in
global_pos_embed = torch.cat([cls_embed, pos_embed], dim=2)

return global_pos_embed
return global_pos_embed.contiguous()

def forward(self, x: torch.Tensor, aspect_ratio: torch.Tensor) -> torch.Tensor:
"""
Expand Down Expand Up @@ -633,7 +633,7 @@ def _resize_position_embedding(
)
# permute to the original shape
embedding = embedding.permute(2, 3, 0, 1)
return embedding
return embedding.contiguous()

def forward(self, x: torch.Tensor, aspect_ratio: torch.Tensor) -> torch.Tensor:
"""
Expand Down
Loading