-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74704d2
commit caf9657
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains 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,65 @@ | ||
# SPDX-FileCopyrightText: © 2024 Tenstorrent AI ULC | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
import pytest | ||
import torch | ||
from transformers import AutoProcessor, LlavaForConditionalGeneration | ||
|
||
import forge | ||
from forge.verify.verify import verify | ||
|
||
from .utils import load_inputs | ||
Check failure on line 13 in forge/test/models/pytorch/multimodal/llava/test_llava.py
|
||
from test.models.utils import Framework, Source, Task, build_module_name | ||
|
||
|
||
class Wrapper(torch.nn.Module): | ||
def __init__(self, model): | ||
super().__init__() | ||
self.model = model | ||
|
||
def forward(self, input_ids, attention_mask, pixel_values): | ||
inputs = {"input_ids": input_ids, "attention_mask": attention_mask, "pixel_values": pixel_values} | ||
output = self.model(**inputs) | ||
return output.logits | ||
|
||
|
||
def load_model(variant): | ||
processor = AutoProcessor.from_pretrained(variant) | ||
model = LlavaForConditionalGeneration.from_pretrained(variant) | ||
model = Wrapper(model) | ||
return model, processor | ||
|
||
|
||
variants = ["llava-hf/llava-1.5-7b-hf"] | ||
|
||
|
||
@pytest.mark.nightly | ||
@pytest.mark.parametrize("variant", variants, ids=variants) | ||
def test_llava(record_forge_property, variant): | ||
# Build Module Name | ||
module_name = build_module_name( | ||
framework=Framework.PYTORCH, | ||
model="llava", | ||
variant=variant, | ||
task=Task.CONDITIONAL_GENERATION, | ||
source=Source.HUGGINGFACE, | ||
) | ||
|
||
# Record Forge Property | ||
record_forge_property("model_name", module_name) | ||
|
||
framework_model, processor = load_model(variant) | ||
image = "https://www.ilankelman.org/stopsigns/australia.jpg" | ||
text = "What’s shown in this image?" | ||
|
||
# Input sample | ||
input_ids, attn_mask, pixel_values = load_inputs(image, text, processor) | ||
inputs = [input_ids, attn_mask, pixel_values] | ||
|
||
# Forge compile framework model | ||
compiled_model = forge.compile(framework_model, sample_inputs=inputs, module_name=module_name) | ||
|
||
# Model Verification | ||
verify(inputs, framework_model, compiled_model) |
This file contains 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,4 @@ | ||
# SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from .utils import load_inputs, load_model |
This file contains 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,39 @@ | ||
# SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import re | ||
|
||
import requests | ||
from PIL import Image | ||
|
||
|
||
def is_url(url): | ||
regex = r"^(https?)://[^\s/$.?#].[^\s]*$" | ||
return bool(re.match(regex, url)) | ||
|
||
|
||
def load_inputs(inp_image, text, processor): | ||
conversation = [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "image"}, | ||
{"type": "text", "text": text}, | ||
], | ||
} | ||
] | ||
text_prompt = processor.apply_chat_template(conversation, padding=True, add_generation_prompt=True) | ||
if is_url(inp_image): | ||
image = Image.open(requests.get(inp_image, stream=True).raw) | ||
else: | ||
if os.path.isfile(inp_image): | ||
image = Image.open(inp_image) | ||
else: | ||
raise ValueError("Input is neither a valid URL nor a valid file path.") | ||
|
||
inputs = processor(images=image, text=text_prompt, return_tensors="pt") | ||
input_ids = inputs["input_ids"] | ||
attn_mask = inputs["attention_mask"] | ||
pixel_values = inputs["pixel_values"] | ||
|
||
return input_ids, attn_mask, pixel_values |
This file contains 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