-
Notifications
You must be signed in to change notification settings - Fork 689
[llama-mm] Onboard Llama3.2 mm vision encoder #6653
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
12 changes: 12 additions & 0 deletions
12
examples/models/llama3_2_vision/vision_encoder/__init__.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,12 @@ | ||
# 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. | ||
|
||
from .model import FlamingoVisionEncoderModel, VisionEncoderConfig | ||
|
||
__all__ = [ | ||
"FlamingoVisionEncoderModel", | ||
"VisionEncoderConfig", | ||
] |
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,88 @@ | ||
# 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. | ||
|
||
from dataclasses import dataclass, field | ||
from typing import Optional | ||
|
||
import torch | ||
|
||
from executorch.examples.models.model_base import EagerModelBase | ||
from executorch.extension.llm.modules._position_embeddings import ( | ||
replace_tile_positional_embedding, | ||
) | ||
from torchtune.models.flamingo._component_builders import flamingo_vision_encoder | ||
|
||
max_seq_len = 8192 | ||
in_channels = 3 | ||
tile_size = 560 | ||
max_num_tiles = 4 | ||
# how many tokens per image generated by the vision encoder | ||
tokens_per_image = 6404 | ||
# how many images to cache in the kv cache in cross attention | ||
kv_cache_image_num = 1 | ||
# maximum number of tokens generated by encoder and thus stored in the kv cache in cross attention | ||
encoder_max_seq_len = tokens_per_image * kv_cache_image_num | ||
|
||
|
||
@dataclass | ||
class VisionEncoderConfig: | ||
patch_size: int = 14 | ||
num_heads: int = 16 | ||
clip_embed_dim: int = 1280 | ||
clip_num_layers: int = 32 | ||
clip_hidden_states: list[int] = field(default_factory=lambda: [3, 7, 15, 23, 30]) | ||
decoder_embed_dim: int = 4096 | ||
num_layers_projection: int = 8 | ||
tile_size: int = 560 | ||
max_num_tiles: int = 4 | ||
in_channels: int = 3 | ||
|
||
|
||
class FlamingoVisionEncoderModel(EagerModelBase): | ||
def __init__(self, config: Optional[VisionEncoderConfig] = None): | ||
super().__init__() | ||
if config is None: | ||
config = VisionEncoderConfig() | ||
self.config = config | ||
self.model = flamingo_vision_encoder( | ||
patch_size=config.patch_size, | ||
num_heads=config.num_heads, | ||
clip_embed_dim=config.clip_embed_dim, | ||
clip_num_layers=config.clip_num_layers, | ||
clip_hidden_states=config.clip_hidden_states, | ||
decoder_embed_dim=config.decoder_embed_dim, | ||
num_layers_projection=config.num_layers_projection, | ||
tile_size=config.tile_size, | ||
max_num_tiles=config.max_num_tiles, | ||
in_channels=config.in_channels, | ||
) | ||
self.model = replace_tile_positional_embedding(self.model) | ||
self.image = torch.randn( | ||
1, 1, 4, 3, self.config.tile_size, self.config.tile_size | ||
) | ||
self.aspect_ratio = torch.tensor([[[1, 2]]]) | ||
self.sample_inputs = ( | ||
self.image, | ||
self.aspect_ratio, | ||
) | ||
|
||
def get_eager_model(self, **kwargs): | ||
return self.model | ||
|
||
def get_example_inputs(self): | ||
return self.sample_inputs | ||
|
||
def get_dynamic_shapes(self): | ||
dim = torch.export.Dim("num_tiles", min=1, max=self.config.max_num_tiles) | ||
image_dynamic_dim = { | ||
0: 1, | ||
1: 1, | ||
2: dim, | ||
3: 3, | ||
4: self.config.tile_size, | ||
5: self.config.tile_size, | ||
} | ||
return (image_dynamic_dim, None) |
Empty file.
44 changes: 44 additions & 0 deletions
44
examples/models/llama3_2_vision/vision_encoder/test/test_vision_encoder.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,44 @@ | ||
# 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. | ||
|
||
# Export and ExecuTorch tests for CLIP vision encoder are covered by test_models.sh. | ||
# Only test AOTI in this file | ||
import os | ||
import tempfile | ||
import unittest | ||
|
||
import torch | ||
|
||
from executorch.examples.models.llama3_2_vision.vision_encoder import ( | ||
FlamingoVisionEncoderModel, | ||
) | ||
from torch._inductor.package import package_aoti | ||
|
||
|
||
class FlamingoVisionEncoderTest(unittest.TestCase): | ||
def setUp(self) -> None: | ||
super().setUp() | ||
|
||
def test_flamingo_vision_encoder(self) -> None: | ||
model = FlamingoVisionEncoderModel() | ||
encoder = model.model | ||
eager_res = encoder.forward(*model.get_example_inputs()) | ||
|
||
# AOTI | ||
so = torch._export.aot_compile( | ||
encoder, | ||
model.get_example_inputs(), | ||
options={"aot_inductor.package": True}, | ||
dynamic_shapes=model.get_dynamic_shapes(), | ||
) | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
path = package_aoti(os.path.join(tmpdir, "vision_encoder.pt2"), so) | ||
print(path) | ||
encoder_aoti = torch._inductor.aoti_load_package(path) | ||
|
||
y = encoder_aoti(*model.get_example_inputs()) | ||
|
||
self.assertTrue(torch.allclose(y, eager_res)) |
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
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.
Need to change to: