-
Notifications
You must be signed in to change notification settings - Fork 694
[Model] Support for llava hf #1479
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
Open
smgjch
wants to merge
2
commits into
open-compass:main
Choose a base branch
from
smgjch:dev_support_for_llava-hf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+183
−1
Open
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
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| from .llava import LLaVA, LLaVA_Next, LLaVA_Next2, LLaVA_OneVision, LLaVA_OneVision_HF | ||
| from .llava_xtuner import LLaVA_XTuner | ||
| from .llava_hf import LLaVA_HF | ||
|
|
||
| __all__ = ['LLaVA', 'LLaVA_Next', 'LLaVA_XTuner', 'LLaVA_Next2', 'LLaVA_OneVision', 'LLaVA_OneVision_HF'] | ||
| __all__ = ['LLaVA', 'LLaVA_Next', 'LLaVA_XTuner','LLaVA_HF', 'LLaVA_Next2', 'LLaVA_OneVision', 'LLaVA_OneVision_HF'] |
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,178 @@ | ||
| import torch | ||
| from PIL import Image | ||
| from abc import abstractproperty | ||
| import sys | ||
| import os.path as osp | ||
| from ..base import BaseModel | ||
| from ...smp import * | ||
| from ...dataset import DATASET_TYPE, DATASET_MODALITY | ||
| import copy | ||
| import requests | ||
| from transformers import AutoProcessor, LlavaForConditionalGeneration | ||
| import logging | ||
|
|
||
| class LLaVA_HF(BaseModel): | ||
| INSTALL_REQ = False | ||
| INTERLEAVE = True | ||
|
|
||
| def __init__(self, model_path="llava-hf/llava-1.5-7b-hf", **kwargs): | ||
|
|
||
| self.model_path = model_path | ||
|
|
||
| try: | ||
| self.model = LlavaForConditionalGeneration.from_pretrained( | ||
| model_path, | ||
| torch_dtype=torch.float16, | ||
| low_cpu_mem_usage=True, | ||
| device_map="cuda" | ||
| ) | ||
| self.processor = AutoProcessor.from_pretrained(model_path) | ||
| except Exception as err: | ||
| logging.critical(f"Failed to load Hugging Face LLaVA model from {model_path}.") | ||
| raise err | ||
|
|
||
| kwargs_default = dict( | ||
| do_sample=False, | ||
| temperature=0, | ||
| max_new_tokens=2048, | ||
| top_p=None, | ||
| num_beams=1, | ||
| use_cache=True, | ||
| ) | ||
| kwargs_default.update(kwargs) | ||
|
|
||
| # Hugging Face's generation config doesn't accept temperature=0 with do_sample=False | ||
| if not kwargs_default["do_sample"] and kwargs_default["temperature"] == 0: | ||
| kwargs_default.pop("temperature", None) | ||
| kwargs_default.pop("top_p", None) | ||
|
|
||
| self.kwargs = kwargs_default | ||
| warnings.warn( | ||
| f"Following kwargs received: {self.kwargs}, will use as generation config. " | ||
| ) | ||
|
|
||
| def use_custom_prompt(self, dataset): | ||
| assert dataset is not None | ||
| if DATASET_TYPE(dataset) == "MCQ": | ||
| return True | ||
| return False | ||
|
|
||
| def build_prompt(self, line, dataset=None): | ||
| assert self.use_custom_prompt(dataset) | ||
| assert dataset is None or isinstance(dataset, str) | ||
| tgt_path = self.dump_image(line, dataset) | ||
|
|
||
| question = line["question"] | ||
| hint = line["hint"] if ("hint" in line and not pd.isna(line["hint"])) else None | ||
| if hint is not None: | ||
| question = hint + "\n" + question | ||
|
|
||
| options = { | ||
| cand: line[cand] | ||
| for cand in string.ascii_uppercase | ||
| if cand in line and not pd.isna(line[cand]) | ||
| } | ||
| for key, item in options.items(): | ||
| question += f"\n{key}. {item}" | ||
| prompt = question | ||
|
|
||
| if len(options): | ||
| prompt += ( | ||
| "\n请直接回答选项字母。" | ||
| if cn_string(prompt) | ||
| else "\nAnswer with the option's letter from the given choices directly." | ||
| ) | ||
| else: | ||
| prompt += ( | ||
| "\n请直接回答问题。" | ||
| if cn_string(prompt) | ||
| else "\nAnswer the question directly." | ||
| ) | ||
|
|
||
| message = [dict(type="image", value=s) for s in tgt_path] | ||
| message.append(dict(type="text", value=prompt)) | ||
| return message | ||
|
|
||
| def chat_inner(self, message, dataset=None): | ||
|
|
||
|
|
||
| conversation = [] | ||
| images = [] | ||
|
|
||
| # Convert framework messages to HF Chat Template format | ||
| for utter in message: | ||
| content_list = [] | ||
| for item in utter["content"]: | ||
| if item["type"] == "text": | ||
| content_list.append({"type": "text", "text": item["value"]}) | ||
| elif item["type"] == "image": | ||
| content_list.append({"type": "image"}) | ||
| images.append(Image.open(item["value"]).convert("RGB")) | ||
|
|
||
| conversation.append({ | ||
| "role": utter["role"], | ||
| "content": content_list | ||
| }) | ||
|
|
||
| prompt = self.processor.apply_chat_template(conversation, add_generation_prompt=True) | ||
|
|
||
| inputs = self.processor( | ||
| images=images if images else None, | ||
| text=prompt, | ||
| return_tensors="pt" | ||
| ).to(self.model.device, torch.float16) | ||
|
|
||
| with torch.inference_mode(): | ||
| output_ids = self.model.generate( | ||
| **inputs, | ||
| **self.kwargs | ||
| ) | ||
|
|
||
| # Slice the output to remove the input prompt tokens | ||
| input_len = inputs["input_ids"].shape[1] | ||
| generated_ids = output_ids[0][input_len:] | ||
|
|
||
| output = self.processor.decode(generated_ids, skip_special_tokens=True).strip() | ||
| return output | ||
|
|
||
| def generate_inner(self, message, dataset=None): | ||
| import torch | ||
|
|
||
| content_list = [] | ||
| images = [] | ||
|
|
||
| # Convert single-turn framework message to HF Chat Template format | ||
| for item in message: | ||
| if item["type"] == "text": | ||
| content_list.append({"type": "text", "text": item["value"]}) | ||
| elif item["type"] == "image": | ||
| content_list.append({"type": "image"}) | ||
| images.append(Image.open(item["value"]).convert("RGB")) | ||
|
|
||
| conversation = [ | ||
| { | ||
| "role": "user", | ||
| "content": content_list | ||
| } | ||
| ] | ||
|
|
||
| prompt = self.processor.apply_chat_template(conversation, add_generation_prompt=True) | ||
|
|
||
| inputs = self.processor( | ||
| images=images if images else None, | ||
| text=prompt, | ||
| return_tensors="pt" | ||
| ).to(self.model.device, torch.float16) | ||
|
|
||
| with torch.inference_mode(): | ||
| output_ids = self.model.generate( | ||
| **inputs, | ||
| **self.kwargs | ||
| ) | ||
|
|
||
| # Slice the output to remove the input prompt tokens | ||
| input_len = inputs["input_ids"].shape[1] | ||
| generated_ids = output_ids[0][input_len:] | ||
|
|
||
| output = self.processor.decode(generated_ids, skip_special_tokens=True).strip() | ||
| return output |
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.
Why is the
LLaVA_HFclass not used in the config.py?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.
Thank you for pointing out the typo. It should indeed be LLaVA_HF here.