-
Notifications
You must be signed in to change notification settings - Fork 724
Add Llava model to examples #2576
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
21 commits
Select commit
Hold shift + click to select a range
dc5db81
Add Llava model to examples
45f3438
Update base for Update on "Add Llava model to examples"
50004e1
Update on "Add Llava model to examples"
0cfbd85
Update base for Update on "Add Llava model to examples"
e993541
Update on "Add Llava model to examples"
e06d093
Update base for Update on "Add Llava model to examples"
be00ba5
Update on "Add Llava model to examples"
94d169e
Update base for Update on "Add Llava model to examples"
acae83d
Update on "Add Llava model to examples"
c7f60d9
Update base for Update on "Add Llava model to examples"
98da31a
Update on "Add Llava model to examples"
9e89a28
Update base for Update on "Add Llava model to examples"
7c66ea8
Update on "Add Llava model to examples"
6c22b4e
Update base for Update on "Add Llava model to examples"
fa2565c
Update on "Add Llava model to examples"
00b98b3
Update base for Update on "Add Llava model to examples"
86d1208
Update on "Add Llava model to examples"
b09b05e
Update base for Update on "Add Llava model to examples"
5510639
Update on "Add Llava model to examples"
d9c23af
Update base for Update on "Add Llava model to examples"
afde0c3
Update on "Add Llava model to examples"
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
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
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,20 @@ | ||
| ## Summary | ||
| In this example, we initiate the process of running multi modality through ExecuTorch. | ||
| - Demonstrate how to export the image encoder model in the [LLava](https://github.com/haotian-liu/LLaVA) multimodal model. | ||
| - Provide TODO steps on how to use the exported .pte file and the existing [exported Llama2 model](https://github.com/pytorch/executorch/tree/main/examples/models/llama2), to build the multimodal pipeline. | ||
|
|
||
| ## Instructions | ||
| Note that this folder does not host the pretrained LLava model. | ||
| - To have Llava available, follow the [Install instructions](https://github.com/haotian-liu/LLaVA?tab=readme-ov-file#install) in the LLava github. Follow the licence in the specific repo when using L | ||
| - Since the pytorch model version may not be updated, `cd executorch`, run `./install_requirements.sh`. | ||
| - If there is numpy compatibility issue, run `pip install bitsandbytes -I`. | ||
| - Alternatively, run `examples/models/llava_encoder/install_requirements.sh`, to replace the steps above. | ||
| - Run `python3 -m examples.portable.scripts.export --model_name="llava_encoder"`. The llava_encoder.pte file will be generated. | ||
| - Run `./cmake-out/executor_runner --model_path ./llava_encoder.pte` to verify the exported model with ExecuTorch runtime with portable kernels. Note that the portable kernels are not performance optimized. Please refer to other examples like those in llama2 folder for optimization. | ||
|
|
||
| ## TODO | ||
| - Write the pipeline in cpp | ||
| - Have image and text prompts as inputs. | ||
| - Call image processing functions to preprocess the image tensor. | ||
| - Load the llava_encoder.pte model, run it using the image tensor. | ||
| - The output of the encoder can be combined with the prompt, as inputs to the llama model. Call functions in llama_runner.cpp to run the llama model and get outputs. The ExecuTorch end to end flow for the llama model is located at `examples/models/llama2`. |
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,11 @@ | ||
| # 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 LlavaModel | ||
|
|
||
| __all__ = [ | ||
| LlavaModel, | ||
| ] |
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,22 @@ | ||
| #!/bin/bash | ||
| # 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. | ||
|
|
||
| # install llava from the submodule | ||
| pip install --force-reinstall -e examples/third-party/LLaVA | ||
|
|
||
| # not included in the pip install package, but needed in llava | ||
| pip install protobuf | ||
|
|
||
| # bitsandbytes depends on numpy 1.x, which is not compatible with numpy 2.x. | ||
| # Reinstall bitsandbytes to make it compatible. | ||
| pip install bitsandbytes -I | ||
|
|
||
| # The deps of llava can have different versions than deps of ExecuTorch. | ||
| # For example, torch version required from llava is older than ExecuTorch. | ||
| # To make both work, recover ExecuTorch's original dependencies by rerunning | ||
| # the install_requirements.sh. | ||
| ./install_requirements.sh |
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,52 @@ | ||
| # 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 torch | ||
|
|
||
| from examples.models.model_base import EagerModelBase | ||
| from llava.eval.run_llava import load_images, process_images | ||
| from llava.mm_utils import get_model_name_from_path | ||
|
|
||
| from llava.model.builder import load_pretrained_model | ||
| from torch import nn | ||
|
|
||
|
|
||
| class EncoderModel(nn.Module): | ||
| def __init__(self, llava_model): | ||
| super().__init__() | ||
| self.model_ = llava_model | ||
|
|
||
| def forward(self, images_tensor): | ||
| features = self.model_.get_model().get_vision_tower()(images_tensor) | ||
| features = self.model_.get_model().mm_projector(features) | ||
| return features | ||
|
|
||
|
|
||
| class LlavaModel(EagerModelBase): | ||
| def __init__(self): | ||
| model_path = "liuhaotian/llava-v1.5-7b" | ||
| tokenizer, self.model_, self.image_processor_, context_len = ( | ||
| load_pretrained_model( | ||
| model_path=model_path, | ||
| model_base=None, | ||
| model_name=get_model_name_from_path(model_path), | ||
| ) | ||
| ) | ||
| self.device = "cpu" | ||
| self.dtype = torch.float32 | ||
| self.model_.to(device=self.device, dtype=self.dtype) | ||
|
|
||
| def get_eager_model(self): | ||
| model = EncoderModel(self.model_) | ||
| return model | ||
|
|
||
| def get_example_inputs(self): | ||
| image_file = "https://llava-vl.github.io/static/images/view.jpg" | ||
| images = load_images([image_file]) | ||
| images_tensor = process_images( | ||
| images, self.image_processor_, self.model_.config | ||
| ).to(self.model_.device) | ||
| return (images_tensor,) |
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.
To run this on PR, please look for
model_should_run_on_eventand addllava_encoderto the list there. This is the list of models we want to cover on PR, the rest are only run in trunk after committing (to save some CI resources a while ago)