Skip to content

Added support of 2qpcs for internvl and llava - #279

Merged
quic-rishinr merged 7 commits into
quic:mainfrom
mohiso22:intern_llava_2qpcs
Feb 28, 2025
Merged

Added support of 2qpcs for internvl and llava#279
quic-rishinr merged 7 commits into
quic:mainfrom
mohiso22:intern_llava_2qpcs

Conversation

@mohiso22

Copy link
Copy Markdown
Contributor

No description provided.

@mohiso22 mohiso22 self-assigned this Feb 24, 2025
Comment thread QEfficient/transformers/models/modeling_auto.py Outdated
Comment thread QEfficient/transformers/models/modeling_auto.py Outdated
@quic-amitraj
quic-amitraj marked this pull request as ready for review February 24, 2025 15:46
@ochougul

Copy link
Copy Markdown
Contributor

How do we test this? Can you add a test file specific to this file?

Comment thread QEfficient/transformers/models/modeling_auto.py Outdated
Comment thread QEfficient/transformers/models/modeling_auto.py Outdated
Comment thread QEfficient/transformers/models/internvl/modeling_internvl.py Outdated
Comment thread QEfficient/transformers/models/modeling_auto.py Outdated
Comment thread QEfficient/transformers/models/modeling_auto.py Outdated
@ochougul

Copy link
Copy Markdown
Contributor

Fix DCO.

@ochougul ochougul left a comment

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.

Rebase

from QEfficient.utils.logging_utils import logger


class QEffInternVisionEncoder2QPC(nn.Module):

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 QEffInternVisionEncoderWrapper 2QPC is internal terminology.

return vit_embeds


class QEffInternLanguageDecoder2QPC(nn.Module):

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 better


output_names = {}
if kv_offload:
lang_output_names.pop(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.

instead of popping. insert the value based on kv_offload flag.
This makes code more intuitive and readable

output_names["vision"] = vision_output_names
output_names["lang"] = lang_output_names
else:
lang_output_names.pop(2)

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.

same as above.

Comment thread QEfficient/transformers/models/internvl/modeling_internvl.py

output_names = {}
if kv_offload:
lang_output_names.pop(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.

same comment as for intern

Comment on lines 428 to +465
super().__init__(model)
self.model = model.get_qeff_language_decoder()

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 to
super().__init__(model.get_qeff_language_decoder())
single line should handle assigning self.model as well

]
_onnx_transforms = [FP16ClipTransform, SplitTensorsTransform]

def __init__(self, model: nn.modules):

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 to
super().__init__(model.get_qeff_vision_encoder())

@quic-amitraj quic-amitraj Feb 27, 2025

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.

Agree, but I think here we cannot do this, as here model is without transform, so model.get_qeff_vision_encoder() will through an error. There is one solution that we can inherit the QEFFBaseModel in _QEffAutoModelForImageTextToTextDualQPC and there init we can call super().__init__(model) which will apply the pytorch transform then there will be no need of calling super().__init__(model) later separately for the vision and language.

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.

Okay, let's keep it like this for now. in both vision and causal classes.
Later we will create Mixin classes to fix this by applying transform in the AutoImageText class

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.

For the vision I had kept the same but for the lang, I had done in one line

self.config = self.model.language_model.config

def forward(self, input_ids, vit_embeds, position_ids, past_key_values):
# TODO: Check if Hardcoding this is okay, i.e. check if this value is common for all intern models

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 are we hardcoding and using magic numbers? How do we handle it in case of single QPC?

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.

This is the image token_id, and it is same for all the Internmodels available so we are directly keeping that here

head_dim = self.config.text_config.hidden_size // self.config.text_config.num_attention_heads
if vis_cfg := getattr(self.config, "vision_config", None):
img_size = getattr(vis_cfg, "image_size", 336)
else:

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 are we using magic numbers in the 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.

U mean to say img_size as 336 ?

}
lang_inputs = {
"input_ids": torch.ones((BS, SEQ_LEN), dtype=torch.int64),
"image_features": torch.ones((BS, 576, self.language_model.config.hidden_size), dtype=torch.float32),

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.

same as above for 576?

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.

So if we fix the img_size to 336, it will create a shape of (BS,576,hs), and if we don't fix 336 as img_size, we might have to run the vision part inside this function to get the shapes of image_features

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.

So let say user changes the img_size in config.json and then execute it. Then hardcoding shape of 576 won't work correct? Shouldn't we discuss and plan for a generic approach rather having support for a very specific config.

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.

Yes you are correct, we can try for a generic approach, and should discuss about it.

Signed-off-by: Mohit Soni <quic_mohisoni@quicinc.com>
Signed-off-by: Mohit Soni <quic_mohisoni@quicinc.com>
Signed-off-by: Mohit Soni <quic_mohisoni@quicinc.com>
Mohit Soni <quic_mohisoni@quicinc.com>

Signed-off-by: Mohit Soni <quic_mohisoni@quicinc.com>
Signed-off-by: Mohit Soni <quic_mohisoni@quicinc.com>
Signed-off-by: Mohit Soni <quic_mohisoni@quicinc.com>
Comment thread QEfficient/transformers/models/internvl/modeling_internvl.py
Signed-off-by: Mohit Soni <quic_mohisoni@quicinc.com>
@quic-rishinr
quic-rishinr merged commit 2b17ebd 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: Mohit Soni <quic_mohisoni@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: Mohit Soni <quic_mohisoni@quicinc.com>
Signed-off-by: Hem Agnihotri <quic_hemagnih@quicinc.com>
qcdipankar pushed a commit to qcdipankar/efficient-transformers that referenced this pull request Apr 1, 2025
Signed-off-by: Mohit Soni <quic_mohisoni@quicinc.com>
Signed-off-by: Dipankar Sarkar <quic_dipankar@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.

5 participants