Skip to content

Commit 35d2440

Browse files
ashvink5000quic-ashvkuma
authored andcommitted
Support Qwen 3VL in AdaScale ONNX (#6990)
Signed-off-by: Ashvin Kumar <quic_ashvkuma@quicinc.com> Co-authored-by: Ashvin Kumar <quic_ashvkuma@quicinc.com>
1 parent f8365bb commit 35d2440

5 files changed

Lines changed: 28 additions & 15 deletions

File tree

GenAILab/qai_hub_lm/models/generator.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,10 @@ def prefill(
743743
else [t for layer_kv in past_key_values for t in (layer_kv[0], layer_kv[1])]
744744
}
745745

746-
selected_seq_len = self._select_sequence_length(input_tokens.shape[1])
747746
slices_iter = self.slice_inputs_for_inference(
748747
input_tokens,
749748
attention_mask,
750-
selected_seq_len,
749+
self.sequence_length, # always use max sequence length for slicing in prefill
751750
position_ids,
752751
**kwargs,
753752
)
@@ -767,7 +766,7 @@ def prefill(
767766
input_ids=input_slice if input_ids is not None else None,
768767
attention_mask=attention_mask_slice,
769768
past_key_values=preconsumed_outputs["past_key_values"],
770-
sequence_length=selected_seq_len,
769+
sequence_length=self.sequence_length,
771770
context_length=self.context_length,
772771
pad_token=getattr(self.tokenizer, "eos_token_id", 0),
773772
attention_mask_min=self.attention_mask_min,
@@ -798,7 +797,7 @@ def prefill(
798797
input_ids=input_slice if input_ids is not None else None,
799798
attention_mask=attention_mask_slice,
800799
past_key_values=preconsumed_outputs["past_key_values"],
801-
sequence_length=selected_seq_len,
800+
sequence_length=self.sequence_length,
802801
context_length=self.context_length,
803802
pad_token=getattr(self.tokenizer, "eos_token_id", 0),
804803
attention_mask_min=self.attention_mask_min,

GenAILab/qai_hub_lm/models/qwen2_vl.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,15 @@ def generate_position_ids(
102102
self,
103103
*args,
104104
input_ids: torch.Tensor,
105-
attention_mask: torch.Tensor,
105+
attention_mask: torch.Tensor | None = None,
106106
**kwargs,
107107
):
108108
num_new_tokens = input_ids.shape[1]
109-
attention_mask = attention_mask[:, -num_new_tokens:]
109+
attention_mask = (
110+
attention_mask[:, -num_new_tokens:]
111+
if attention_mask is not None
112+
else torch.ones_like(input_ids, dtype=torch.int32)
113+
)
110114

111115
ctx = PositionIdContext(self.config, modeling_qwen2_5_vl.Qwen2_5_VLModel)
112116
position_ids, *_ = modeling_qwen2_5_vl.Qwen2_5_VLModel.get_rope_index(

GenAILab/qai_hub_lm/models/qwen3_vl.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,15 @@ def generate_position_ids(
165165
self,
166166
*args,
167167
input_ids: torch.Tensor,
168-
attention_mask: torch.Tensor,
168+
attention_mask: torch.Tensor | None = None,
169169
**kwargs,
170170
):
171171
num_new_tokens = input_ids.shape[1]
172-
attention_mask = attention_mask[:, -num_new_tokens:]
172+
attention_mask = (
173+
attention_mask[:, -num_new_tokens:]
174+
if attention_mask is not None
175+
else torch.ones_like(input_ids, dtype=torch.int32)
176+
)
173177

174178
ctx = PositionIdContext(self.config, modeling_qwen3_vl.Qwen3VLModel)
175179
position_ids, *_ = modeling_qwen3_vl.Qwen3VLModel.get_rope_index(

GenAILab/tests/unit/models/test_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,11 +948,11 @@ def test_prefill_uses_large_seq_len(self, multi_seq_gen):
948948
assert isinstance(slices[0], OrderedDict)
949949
assert slices[0]["input_ids"].shape[1] == 8
950950

951-
def test_prefill_single_token_uses_small_seq_len(self, multi_seq_gen):
951+
def test_prefill_single_token_uses_max_seq_len(self, multi_seq_gen):
952952
input_ids = torch.randint(0, 256, (1, 1))
953953
slices = list(multi_seq_gen.prefill(input_ids=input_ids))
954954
assert len(slices) == 1
955-
assert slices[0]["input_ids"].shape[1] == 1
955+
assert slices[0]["input_ids"].shape[1] == max(multi_seq_gen.sequence_lengths)
956956

957957
def test_three_sequence_lengths(self, model, tokenizer):
958958
gen = Generator(

TrainingExtensions/onnx/src/python/aimet_onnx/experimental/adascale/adascale_optimizer.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class AdaScaleModelConfig:
7373
"qwen3": AdaScaleModelConfig(
7474
model_type="qwen3", beta_gamma_lr=1e-3, scales_lr=5e-4
7575
),
76+
"qwen3_vl": AdaScaleModelConfig(
77+
model_type="qwen3", beta_gamma_lr=1e-3, scales_lr=5e-4
78+
),
7679
"phi3": AdaScaleModelConfig(model_type="phi3", beta_gamma_lr=1e-3, scales_lr=5e-4),
7780
"qwen2_5_vl": AdaScaleModelConfig(
7881
model_type="qwen2_5_vl", beta_gamma_lr=1e-3, scales_lr=5e-4
@@ -148,12 +151,15 @@ def apply_adascale(
148151
)
149152

150153
# create a list of common input names to be used for graph slicing and populating input_list
154+
# Exclude primary sequence inputs (consumed upstream by embedding layer)
155+
# and past_key_*/past_value_* (handled per-block below)
151156
common_input_names = []
152157
for name in graph_input_names:
153-
if "attention" in name:
154-
common_input_names.append(name)
155-
if "position" in name:
156-
common_input_names.append(name)
158+
if name == "inputs_embeds" or "input_ids" in name:
159+
continue
160+
if "past_key" in name or "past_value" in name:
161+
continue
162+
common_input_names.append(name)
157163

158164
del sim.session
159165
gc.collect()
@@ -198,7 +204,7 @@ def apply_adascale(
198204
):
199205
block_kv_tensor_names.append(name)
200206

201-
block_input_names = common_input_names
207+
block_input_names = list(common_input_names)
202208
if len(block_kv_tensor_names) > 0:
203209
if len(block_kv_tensor_names) != 2:
204210
raise RuntimeError(

0 commit comments

Comments
 (0)