Skip to content
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

fix config saving when check on misplaced args broken #966

Merged
merged 13 commits into from
Nov 13, 2024

Conversation

eaidova
Copy link
Collaborator

@eaidova eaidova commented Oct 24, 2024

What does this PR do?

some model configs does not expect be loaded without arguments that required for checking misplaced arguments in 4.45 release (example https://huggingface.co/OpenGVLab/InternVL2-1B). This fix allows ignoring such cases for loading and saving config files

from pathlib import Path
import requests
from optimum.intel.openvino import OVModelForVisualCausalLM
from PIL import Image
from transformers import AutoTokenizer


model_id = "OpenGVLab/InternVL2-1B"
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model_path = Path(".models/InternVL2-1B/FP32")
if not model_path.exists():
    model = OVModelForVisualCausalLM.from_pretrained(model_id, trust_remote_code=True)
    model.save_pretrained(model_path)
    tokenizer.save_pretrained(model_path)
model = OVModelForVisualCausalLM.from_pretrained(model_path, trust_remote_code=True)


image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
raw_image = Image.open(requests.get(image_file, stream=True).raw).convert("RGB")

question = "Please describe the image shortly."
inputs = model.preprocess_inputs(
    tokenizer=tokenizer,
    image=raw_image,
    text=question,
)
generation_output = model.generate(**inputs, max_new_tokens=10)

print(tokenizer.batch_decode(generation_output[:, inputs["input_ids"].shape[1]:]))

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

@eaidova eaidova force-pushed the ea/fix_config_save branch 2 times, most recently from 978b6f7 to f10cf64 Compare October 24, 2024 10:49
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@eaidova eaidova force-pushed the ea/fix_config_save branch 3 times, most recently from 9978afd to f5a4f40 Compare October 24, 2024 11:01
@eaidova eaidova force-pushed the ea/fix_config_save branch 3 times, most recently from 9f36a32 to c9954c0 Compare November 5, 2024 13:02
@AlexKoff88
Copy link
Collaborator

I am fine with the code in the main part but the test is blowing off due to the need to replicate the model-specific preprocessing. I wonder if there is a more elegant solution for that?

Copy link
Collaborator

@nikita-savelyevv nikita-savelyevv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please provide an example in the PR description on how to run the InternVL2 model? Similar examples for (nano)llava and minicpmv were quite useful.

optimum/exporters/openvino/utils.py Outdated Show resolved Hide resolved
optimum/intel/openvino/modeling_base_seq2seq.py Outdated Show resolved Hide resolved
optimum/exporters/openvino/convert.py Outdated Show resolved Hide resolved
optimum/intel/openvino/modeling_base.py Outdated Show resolved Hide resolved
optimum/exporters/openvino/utils.py Outdated Show resolved Hide resolved
@eaidova
Copy link
Collaborator Author

eaidova commented Nov 11, 2024

Could you please provide an example in the PR description on how to run the InternVL2 model? Similar examples for (nano)llava and minicpmv were quite useful.

example of model inference added in description

optimum/exporters/openvino/utils.py Outdated Show resolved Hide resolved
optimum/intel/openvino/modeling_visual_language.py Outdated Show resolved Hide resolved
tests/openvino/test_modeling.py Outdated Show resolved Hide resolved
@AlexKoff88
Copy link
Collaborator

@eaidova, LGTM. Please fix the test: tests/openvino/test_modeling.py::OVModelForVisualCausalLMIntegrationTest::test_compare_to_transformers_4_internvl2

@eaidova
Copy link
Collaborator Author

eaidova commented Nov 12, 2024

@eaidova, LGTM. Please fix the test: tests/openvino/test_modeling.py::OVModelForVisualCausalLMIntegrationTest::test_compare_to_transformers_4_internvl2

@AlexKoff88 it is fixed on model code level https://huggingface.co/katuni4ka/tiny-random-internvl2/commit/35d8860bba682fdbc190914c38f8af37eb46196c

Copy link
Collaborator

@nikita-savelyevv nikita-savelyevv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Could you please update the code example in the description with the following one?

from pathlib import Path
import requests
from optimum.intel.openvino import OVModelForVisualCausalLM
from PIL import Image
from transformers import AutoTokenizer


model_id = "OpenGVLab/InternVL2-1B"
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model_path = Path(".models/InternVL2-1B/FP32")
if not model_path.exists():
    model = OVModelForVisualCausalLM.from_pretrained(model_id, trust_remote_code=True)
    model.save_pretrained(model_path)
    tokenizer.save_pretrained(model_path)
model = OVModelForVisualCausalLM.from_pretrained(model_path, trust_remote_code=True)


image_file = "http://images.cocodataset.org/val2017/000000039769.jpg"
raw_image = Image.open(requests.get(image_file, stream=True).raw).convert("RGB")

question = "Please describe the image shortly."
inputs = model.preprocess_inputs(
    tokenizer=tokenizer,
    image=raw_image,
    text=question,
)
generation_output = model.generate(**inputs, max_new_tokens=10)

print(tokenizer.batch_decode(generation_output[:, inputs["input_ids"].shape[1]:]))

optimum/intel/openvino/modeling_visual_language.py Outdated Show resolved Hide resolved
@AlexKoff88 AlexKoff88 merged commit 5c879b9 into huggingface:main Nov 13, 2024
21 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants