Added example script for InternVL - #269
Conversation
|
|
||
| ## STEP 2 -- EXPORT & COMPILE THE MODEL | ||
|
|
||
| model.compile(num_cores=14, num_devices=4, mxfp6_matmul=True) |
There was a problem hiding this comment.
Please update the num_cores to 16. We should always by default set the num_cores to 16 only.
There was a problem hiding this comment.
Pass num_patches here that you will be using at the time of processor.
There was a problem hiding this comment.
Updated, thanks.
| ## 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 |
There was a problem hiding this comment.
Is there any need of this line?
There was a problem hiding this comment.
Removed accrodingly.
| 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") |
There was a problem hiding this comment.
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-
- Pass prefill seq as a arg to compile, so you will know the
max_lengthvalue what to use at the time of tokenizer. - Else don't pass anything here, by default padding will be done till the
3840as its the default value at the time of compilation.
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
Already discussed, keeping this as it is for now.
|
|
||
| ## STEP 2 -- EXPORT & COMPILE THE MODEL | ||
|
|
||
| model.compile(num_cores=14, num_devices=4, mxfp6_matmul=True) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Change max_num to num_patches.
|
|
||
|
|
||
| # Processor class for InternVL models | ||
| class InternProcessor: |
There was a problem hiding this comment.
write a docstring explaining this is taken from model card on hf and summarize the changes done to original code.
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>
25c7256 to
d6b19bd
Compare
| --- | ||
|
|
||
| *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) |
There was a problem hiding this comment.
Single/dual qpc is internal terminology. We expose this via kv_offload variable.
Remove reference to single/dual qpc term
There was a problem hiding this comment.
Changes incorporated
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>
| 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` |
There was a problem hiding this comment.
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/" | ||
|
|
There was a problem hiding this comment.
remove which line?
I didn't get it.
| 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}") |
There was a problem hiding this comment.
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") |
| # 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 |
There was a problem hiding this comment.
Add the explanation of why this is put as 3840
| ## 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 |
There was a problem hiding this comment.
single/ dual qpc is internal term. can you explain properly here
| img_size=img_size, | ||
| num_cores=num_cores, | ||
| num_devices=num_devices, | ||
| ) |
There was a problem hiding this comment.
What's the expected output for this model? can you put that here?
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>
| ctx_len = 512 | ||
| img_size = 560 | ||
| num_cores = 16 | ||
| num_devices = 1 |
There was a problem hiding this comment.
can you please define generation_len here and pass it to run_model ?
everything else LGTM.
Will merge after this change
There was a problem hiding this comment.
sure, will do, thanks.
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com>
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com> Signed-off-by: Hem Agnihotri <quic_hemagnih@quicinc.com>
Signed-off-by: quic-dhirajku <quic_dhirajku@quicinc.com> Signed-off-by: Hem Agnihotri <quic_hemagnih@quicinc.com>
No description provided.