Skip to content

Dynamic max_num_tiles for mllama #308

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

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions QEfficient/transformers/models/mllama/modeling_mllama.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,24 +1133,24 @@ def get_dummy_inputs(self, kv_offload: bool = False):

if vis_cfg := getattr(self.config, "vision_config", None):
img_size = getattr(vis_cfg, "image_size", 448)
max_num_img_tiles = getattr(vis_cfg, "max_num_tiles", 4)
max_num_tiles = getattr(vis_cfg, "max_num_tiles", 4)
else:
img_size = 448
max_num_img_tiles = 4
max_num_tiles = 4

# vision inputs
vision_inputs = {
"pixel_values": torch.zeros(
(BS, MAX_NUM_IMG, max_num_img_tiles, NUM_CHANNEL, img_size, img_size), dtype=torch.float32
(BS, MAX_NUM_IMG, max_num_tiles, NUM_CHANNEL, img_size, img_size), dtype=torch.float32
),
"aspect_ratio_ids": torch.ones((BS, MAX_NUM_IMG), dtype=torch.int64),
"aspect_ratio_mask": torch.ones((BS, MAX_NUM_IMG, max_num_img_tiles), dtype=torch.int64),
"aspect_ratio_mask": torch.ones((BS, MAX_NUM_IMG, max_num_tiles), dtype=torch.int64),
}

# lang_inputs
lang_inputs = {
"input_ids": torch.zeros((BS, SEQ_LEN), dtype=torch.int64),
"cross_attention_mask": torch.zeros((BS, SEQ_LEN, MAX_NUM_IMG, max_num_img_tiles), dtype=torch.int64),
"cross_attention_mask": torch.zeros((BS, SEQ_LEN, MAX_NUM_IMG, max_num_tiles), dtype=torch.int64),
"attention_mask": torch.ones((BS, SEQ_LEN), dtype=torch.int64),
}

Expand Down Expand Up @@ -1201,6 +1201,7 @@ def get_specializations(
):
vis_cfg = self.config.vision_config
max_num_images = compiler_options.pop("max_num_images", 1)
max_num_tiles = compiler_options.pop("max_num_tiles", 4)
prefill_seq_len = prefill_seq_len if prefill_seq_len else 32
ctx_len = ctx_len if ctx_len else 128
if img_size is None and hasattr(vis_cfg, "image_size"):
Expand All @@ -1209,20 +1210,29 @@ def get_specializations(
img_size = 448
logger.warning("Setting `img_size=448` as it was neither passed nor found in vision_config")

vision = [{"batch_size": batch_size, "max_num_images": max_num_images, "img_size": img_size}]
vision = [
{
"batch_size": batch_size,
"max_num_images": max_num_images,
"max_num_tiles": max_num_tiles,
"img_size": img_size,
}
]
lang = [
{
"batch_size": batch_size,
"seq_len": prefill_seq_len,
"ctx_len": ctx_len,
"max_num_images": max_num_images,
"max_num_tiles": max_num_tiles,
"img_size": img_size,
},
{
"batch_size": batch_size,
"seq_len": "1",
"ctx_len": ctx_len,
"max_num_images": max_num_images,
"max_num_tiles": max_num_tiles,
"img_size": img_size,
},
]
Expand All @@ -1241,15 +1251,15 @@ def get_onnx_dynamic_axes(self, kv_offload: bool = False):
cross_attention_layers = txt_cfg.cross_attention_layers

vision_dynamic_axes = {
"pixel_values": {0: "batch_size", 1: "max_num_images", 4: "img_size", 5: "img_size"},
"pixel_values": {0: "batch_size", 1: "max_num_images", 2: "max_num_tiles", 4: "img_size", 5: "img_size"},
"aspect_ratio_ids": {0: "batch_size", 1: "max_num_images"},
"aspect_ratio_mask": {0: "batch_size", 1: "max_num_images"},
"aspect_ratio_mask": {0: "batch_size", 1: "max_num_images", 2: "max_num_tiles"},
}

lang_dynamic_axes = {
"input_ids": {0: "batch_size", 1: "seq_len"},
"position_ids": {0: "batch_size", 1: "seq_len"},
"cross_attention_mask": {0: "batch_size", 1: "seq_len", 2: "max_num_images"},
"cross_attention_mask": {0: "batch_size", 1: "seq_len", 2: "max_num_images", 3: "max_num_tiles"},
}

for i in range(num_hidden_layers):
Expand Down