-
Notifications
You must be signed in to change notification settings - Fork 702
Resizable image positional embeddings #1695
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
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a11433e
pretrained config
4f47b1e
first commit
7e4010c
fix shape bugs
6135b42
add docstrings
ba1578a
unit tests
967d86f
docstrings
54d71b5
small docstring changes
085f981
clip docs
2c90b64
better comments and assertion
d2452ad
delete config
e69fd47
comments + end2end tests
e4cdd82
add todo
59f1996
comment fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
tests/torchtune/models/clip/test_pos_embedding_interpolation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from tests.test_utils import assert_expected | ||
|
|
||
| from torchtune.models.clip._position_embeddings import ( | ||
| TiledTokenPositionalEmbedding, | ||
| TilePositionalEmbedding, | ||
| ) | ||
|
|
||
| # generated comparing vs fairinternal/internal-llama-models | ||
| tile_pos_emb_test_cases = [ | ||
| { | ||
| "tgt_num_tiles": 1, | ||
| # [max_num_tiles, max_num_tiles, -1, embed_dim] -> (2, 2, 2, 3) | ||
| "input_tensor": torch.tensor( | ||
| [ | ||
| [ | ||
| [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]], | ||
| [[6.0, 7.0, 8.0], [9.0, 10.0, 11.0]], | ||
| ], | ||
| [ | ||
| [[12.0, 13.0, 14.0], [15.0, 16.0, 17.0]], | ||
| [[18.0, 19.0, 20.0], [21.0, 22.0, 23.0]], | ||
| ], | ||
| ] | ||
| ), | ||
| "expected_output": torch.tensor([[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]]]), | ||
| }, | ||
| { | ||
| "tgt_num_tiles": 3, | ||
| # [max_num_tiles, max_num_tiles, -1, embed_dim] -> (2, 2, 1, 2) | ||
| "input_tensor": torch.tensor( | ||
| [[[[0.0, 1.0]], [[2.0, 3.0]]], [[[4.0, 5.0]], [[6.0, 7.0]]]] | ||
| ), | ||
| "expected_output": torch.tensor( | ||
| [ | ||
| [[[0.0, 1.0]], [[1.0, 2.0]], [[2.0, 3.0]]], | ||
| [[[2.0, 3.0]], [[3.0, 4.0]], [[4.0, 5.0]]], | ||
| [[[4.0, 5.0]], [[5.0, 6.0]], [[6.0, 7.0]]], | ||
| ] | ||
| ), | ||
| }, | ||
| ] | ||
|
|
||
| local_pos_emb_test_cases = [ | ||
| { | ||
| "target_n_tokens_per_tile": 1, | ||
| # [inpt_n_tokens_per_tile, emb_dim] -> (5, 2) | ||
| "input_tensor": torch.tensor( | ||
| [[0.0, 1.0], [2.0, 3.0], [4.0, 5.0], [6.0, 7.0], [8.0, 9.0]] | ||
| ), | ||
| "expected_output": torch.tensor([[0.0, 1.0], [2.0, 3.0]]), | ||
| }, | ||
| { | ||
| "target_n_tokens_per_tile": 3, | ||
| # [inpt_n_tokens_per_tile, emb_dim] -> (5, 11) | ||
| "input_tensor": torch.tensor([[0.0], [1.0], [2.0], [3.0], [4.0]]), | ||
| "expected_output": torch.tensor( | ||
| [ | ||
| [0.0000], | ||
| [1.0000], | ||
| [1.5000], | ||
| [2.0000], | ||
| [2.0000], | ||
| [2.5000], | ||
| [3.0000], | ||
| [3.0000], | ||
| [3.5000], | ||
| [4.0000], | ||
| ] | ||
| ), | ||
| }, | ||
| ] | ||
|
|
||
| global_pos_emb_test_cases = [ | ||
| { | ||
| "tgt_max_num_tiles": 2, | ||
| "tgt_patch_grid_size": 2, | ||
| # [max_num_tiles, max_num_tiles, num_tokens_per_tile, embed_dim] -> (3, 3, 2, 1) | ||
| "input_tensor": torch.tensor( | ||
| [ | ||
| [[[0.0], [1.0]], [[2.0], [3.0]], [[4.0], [5.0]]], | ||
| [[[6.0], [7.0]], [[8.0], [9.0]], [[10.0], [11.0]]], | ||
| [[[12.0], [13.0]], [[14.0], [15.0]], [[16.0], [17.0]]], | ||
| ] | ||
| ), | ||
| "expected_output": torch.tensor( | ||
| [ | ||
| [ | ||
| [[0.0000], [1.0000], [2.3333], [5.0000], [6.3333]], | ||
| [[4.0000], [3.6667], [5.0000], [7.6667], [9.0000]], | ||
| ], | ||
| [ | ||
| [[12.0000], [9.0000], [10.3333], [13.0000], [14.3333]], | ||
| [[16.0000], [11.6667], [13.0000], [15.6667], [17.0000]], | ||
| ], | ||
| ] | ||
| ), | ||
| }, | ||
| { | ||
| "tgt_max_num_tiles": 1, | ||
| "tgt_patch_grid_size": 1, | ||
| # [max_num_tiles, max_num_tiles, num_tokens_per_tile, embed_dim] -> (1, 1, 5, 2) | ||
| "input_tensor": torch.tensor( | ||
| [[[[0.0, 1.0], [2.0, 3.0], [4.0, 5.0], [6.0, 7.0], [8.0, 9.0]]]] | ||
| ), | ||
| "expected_output": torch.tensor([[[[0.0, 1.0], [2.0, 3.0]]]]), | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| class TestPositionalEmbeddingsInterpolation: | ||
| @pytest.mark.parametrize("params", tile_pos_emb_test_cases) | ||
| def test_dynamic_resize(self, params): | ||
| tgt_num_tiles = params["tgt_num_tiles"] | ||
| expected_output = params["expected_output"] | ||
| embedding = params["input_tensor"] | ||
|
|
||
| resized_pos_embed = TilePositionalEmbedding._resize_position_embedding( | ||
| embedding, tgt_num_tiles | ||
| ) | ||
|
|
||
| assert_expected(resized_pos_embed, expected_output, atol=1e-3, rtol=1e-4) | ||
|
|
||
| @pytest.mark.parametrize("params", local_pos_emb_test_cases) | ||
| def test_resize_local_position_embedding(self, params): | ||
| input_tensor = params["input_tensor"] | ||
| target_n_tokens_per_tile = params["target_n_tokens_per_tile"] | ||
| expected_output = params["expected_output"] | ||
|
|
||
| resized_pos_embed = ( | ||
| TiledTokenPositionalEmbedding._resize_local_position_embedding( | ||
| input_tensor, target_n_tokens_per_tile | ||
| ) | ||
| ) | ||
|
|
||
| assert_expected(resized_pos_embed, expected_output, atol=1e-3, rtol=1e-4) | ||
|
|
||
| @pytest.mark.parametrize("params", global_pos_emb_test_cases) | ||
| def test_resize_global_position_embedding(self, params): | ||
| input_tensor = params["input_tensor"] | ||
| tgt_max_num_tiles = params["tgt_max_num_tiles"] | ||
| tgt_patch_grid_size = params["tgt_patch_grid_size"] | ||
| expected_output = params["expected_output"] | ||
|
|
||
| resized_pos_embed = ( | ||
| TiledTokenPositionalEmbedding._resize_global_position_embedding( | ||
| input_tensor, tgt_max_num_tiles, tgt_patch_grid_size | ||
| ) | ||
| ) | ||
|
|
||
| assert_expected(resized_pos_embed, expected_output, atol=1e-3, rtol=1e-4) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I don't fully follow these comments in each test case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just trying to help the reader understand the dimensions provided. I can make it better. -1 is because the actual pos embedding has dim=1 there, but when i created the tests, i created with 2.