Skip to content

Commit 42eeebc

Browse files
Martin Yuanfacebook-github-bot
authored andcommitted
Add example of LLava encoder (#2375)
Summary: Pull Request resolved: #2375 Add the example to start enabling LLava, one multimodal model in generative AI area. In this example, we initiate the process of running LLava through ExecuTorch. Refer to the added README.md for details. bypass-github-export-checks Reviewed By: cccclai Differential Revision: D54812717 fbshipit-source-id: 57f79a925f40594d6c0714b77aefb6193ee2890a
1 parent 4fea983 commit 42eeebc

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

examples/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"ic4": ("inception_v4", "InceptionV4Model"),
2727
"resnet18": ("resnet", "ResNet18Model"),
2828
"resnet50": ("resnet", "ResNet50Model"),
29+
"llava": ("llava", "LlavaModel"),
2930
}
3031

3132
__all__ = [
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Summary
2+
In this example, we initiate the process of running multi modality through ExecuTorch.
3+
- Demonstrate how to export the image encoder model in the [LLava](https://github.com/haotian-liu/LLaVA) multimodal model.
4+
- 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.
5+
6+
## Instructions
7+
Note that this folder does not host the pretrained LLava model.
8+
- 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
9+
- Since the pytorch model version may not be updated, `cd executorch`, run `./install_requirements.sh`.
10+
- Run `python3 -m examples.portable.scripts.export --model_name="llava_encoder"`. The llava_encoder.pte file will be generated.
11+
12+
## TODO
13+
- Write the pipeline in cpp
14+
- Have image and text prompts as inputs.
15+
- Call image processing functions to preprocess the image tensor.
16+
- Load the llava_encoder.pte model, run it using the image tensor.
17+
- 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.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
from .model import LlavaModel
8+
9+
__all__ = [
10+
LlavaModel,
11+
]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
import torch
8+
9+
from examples.models.model_base import EagerModelBase
10+
from llava.eval.run_llava import load_images, process_images
11+
from llava.mm_utils import get_model_name_from_path
12+
13+
from llava.model.builder import load_pretrained_model
14+
from torch import nn
15+
16+
17+
class EncoderModel(nn.Module):
18+
def __init__(self, llava_model):
19+
super().__init__()
20+
self.model_ = llava_model
21+
22+
def forward(self, images_tensor):
23+
features = self.model_.get_model().get_vision_tower()(images_tensor)
24+
features = self.model_.get_model().mm_projector(features)
25+
return features
26+
27+
28+
class LlavaModel(EagerModelBase):
29+
def __init__(self):
30+
model_path = "liuhaotian/llava-v1.5-7b"
31+
tokenizer, self.model_, self.image_processor_, context_len = (
32+
load_pretrained_model(
33+
model_path=model_path,
34+
model_base=None,
35+
model_name=get_model_name_from_path(model_path),
36+
)
37+
)
38+
self.device = "cpu"
39+
self.model_.to(self.device)
40+
self.dtype = torch.float32
41+
42+
def get_eager_model(self):
43+
model = EncoderModel(self.model_)
44+
return model
45+
46+
def get_example_inputs(self):
47+
image_file = "https://llava-vl.github.io/static/images/view.jpg"
48+
images = load_images([image_file])
49+
images_tensor = process_images(
50+
images, self.image_processor_, self.model_.config
51+
).to(self.model_.device, dtype=torch.float32)
52+
return (images_tensor,)

0 commit comments

Comments
 (0)