Skip to content

Added example script for InternVL - #269

Merged
ochougul merged 10 commits into
quic:mainfrom
quic-dhirajku:intern_example
Feb 28, 2025
Merged

Added example script for InternVL#269
ochougul merged 10 commits into
quic:mainfrom
quic-dhirajku:intern_example

Conversation

@quic-dhirajku

Copy link
Copy Markdown
Contributor

No description provided.

Comment thread examples/internvl_inference.py Outdated

## STEP 2 -- EXPORT & COMPILE THE MODEL

model.compile(num_cores=14, num_devices=4, mxfp6_matmul=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please update the num_cores to 16. We should always by default set the num_cores to 16 only.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pass num_patches here that you will be using at the time of processor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated, thanks.

Comment thread examples/internvl_inference.py Outdated
## STEP 3 -- SETUP THE PROCESSOR

# InternVL doesn't have an AutoProcessor yet, so we will use our own processor class "InternProcessor"
qeff_pt_model = model.model

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there any need of this line?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed accrodingly.

Comment thread examples/internvl_inference.py Outdated
pixel_values = internProcessor.load_image(image, max_num=12)
question = "<image>\n" + prompt
query = internProcessor(pixel_values, question, messages, roles)
inputs = tokenizer(query, return_tensors="pt", padding="max_length", max_length=3840, padding_side="right")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How you are setting max_length to 3840 if you are not setting the prefil_seq_length at the time of compilation?
There is two thing you can do-

  1. Pass prefill seq as a arg to compile, so you will know the max_length value what to use at the time of tokenizer.
  2. Else don't pass anything here, by default padding will be done till the 3840 as its the default value at the time of compilation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added prefill_seq_len as an added arguement now, thanks.


# Resizing the image to have num_crops=13
# We can fix a different size and the compile the model with appropriate `num_crops`
image = image.resize((1000, 747))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@quic-dhirajku How user will know which shape is best to resize to achive exact num_crops that we have used at the time of compilation? It should be taken care inside the processor as we are using own processor and by just passing the num_patches (As its already known because we have used at the time of compilation) and image (Same as you are doing currently) it should resize the image in function dynamic_preprocess. Please modify the function dynamic_preprocess to incorporate this logic. Let me know if you still have doubt.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Already discussed, keeping this as it is for now.

Comment thread examples/internvl_inference.py Outdated

## STEP 2 -- EXPORT & COMPILE THE MODEL

model.compile(num_cores=14, num_devices=4, mxfp6_matmul=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pass num_patches here that you will be using at the time of processor.

image = image.resize((1000, 747))

# preprocess the resized image
pixel_values = internProcessor.load_image(image, max_num=12)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Change max_num to num_patches.



# Processor class for InternVL models
class InternProcessor:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

write a docstring explaining this is taken from model card on hf and summarize the changes done to original code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks

Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
Comment thread README.md Outdated
---

*Latest news* :fire: <br>
- [02/2025] [VLMs support](https://github.com/quic/efficient-transformers/pull/267) added for the models [InternVL-1B](https://huggingface.co/OpenGVLab/InternVL2_5-1B), [Llava](https://huggingface.co/llava-hf/llava-1.5-7b-hf) (Single QPC) and [Mllama](https://huggingface.co/meta-llama/Llama-3.2-11B-Vision-Instruct) (Single + Dual QPCs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Single/dual qpc is internal terminology. We expose this via kv_offload variable.
Remove reference to single/dual qpc term

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changes incorporated

@quic-amitraj
quic-amitraj marked this pull request as draft February 21, 2025 06:19
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
@quic-dhirajku
quic-dhirajku marked this pull request as ready for review February 26, 2025 09:12
image = Image.open(BytesIO(img.content)).convert("RGB")

# Resizing the image to have num_crops=13
# We can fix a different size and the compile the model with appropriate `num_crops`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

num_patches

Comment thread examples/vlm_inference.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

rename to text_to_image_inference.py

InternVL model only has an AutoTokenizer so this class performs the processing tasks similar to an AutoProcessor.
The methods used here are borrowed from the original InternVL modelling files.
"https://huggingface.co/OpenGVLab/InternVL2_5-1B/"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

remove which line?
I didn't get it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

empty line

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

49

query = get_prompt(messages)
if verbose and pixel_values is not None:
image_bs = pixel_values.shape[0]
print(f"dynamic ViT batch size: {image_bs}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why? use logger.info if needed.
Avoid using print statements

pixel_values = internProcessor.load_image(image, max_num=12)
question = "<image>\n" + prompt
query = internProcessor(pixel_values, question, messages, roles)
# inputs = tokenizer(query, return_tensors="pt", padding="max_length", padding_side="right")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

commented code?

# Inputs for the model
prompt = "Please describe the image in detail."
image_url = "https://image.slidesharecdn.com/azureintroduction-191206101932/75/Introduction-to-Microsoft-Azure-Cloud-1-2048.jpg"
prefill_seq_len = 3840

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add the explanation of why this is put as 3840

Comment thread examples/vlm_inference.py Outdated
## STEP - 1 Load the Processor and Model

processor = AutoProcessor.from_pretrained(model_name, token=token)
# `kv_offload` is used to decide if we wish to run Single QPC or 2 QPC setup

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

single/ dual qpc is internal term. can you explain properly here

Comment thread examples/vlm_inference.py
img_size=img_size,
num_cores=num_cores,
num_devices=num_devices,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the expected output for this model? can you put that here?

Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
Comment thread examples/intern_example/internvl_inference.py Outdated
Comment thread examples/intern_example/internvl_inference.py
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
ctx_len = 512
img_size = 560
num_cores = 16
num_devices = 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you please define generation_len here and pass it to run_model ?
everything else LGTM.
Will merge after this change

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure, will do, thanks.

Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
@ochougul
ochougul merged commit 040dab4 into quic:main Feb 28, 2025
quic-hemagnih pushed a commit to quic-hemagnih/efficient-transformers that referenced this pull request Mar 12, 2025
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
Signed-off-by: Hem Agnihotri <quic_hemagnih@quicinc.com>
quic-hemagnih pushed a commit to quic-hemagnih/efficient-transformers that referenced this pull request Mar 12, 2025
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
Signed-off-by: Hem Agnihotri <quic_hemagnih@quicinc.com>
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