Skip to content

Commit 0ef274f

Browse files
authored
Fix patches for Qwen2_5 (#294)
* Fix patch for Qwen2_5 * doc * fix * fix * int2 * attention * fix ut * fix * fix * fix * fix aggregation * mypy * fix * lint * fix * revert * stop * lint
1 parent c371e38 commit 0ef274f

File tree

19 files changed

+490
-50
lines changed

19 files changed

+490
-50
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ jobs:
149149
- name: run tests
150150
run: |
151151
pip install pytest
152-
PYTHONPATH=. UNITTEST_GOING=1 pytest --durations=10 _unittests --ignore _unittests/ut_reference/test_backend_extended_reference_evaluator.py --ignore _unittests/ut_reference/test_backend_onnxruntime_evaluator.py --ignore _unittests/ut_torch_models/test_tiny_llms_bypassed.py --ignore _unittests/ut_tasks/test_tasks_zero_shot_image_classification.py --ignore _unittests/ut_tasks/test_tasks_image_classification.py --ignore _unittests/ut_torch_models/test_validate_whole_models*.py
152+
PYTHONPATH=. UNITTEST_GOING=1 pytest --durations=10 _unittests --ignore _unittests/ut_reference/test_backend_extended_reference_evaluator.py --ignore _unittests/ut_reference/test_backend_onnxruntime_evaluator.py --ignore _unittests/ut_torch_models/test_tiny_llms_bypassed.py --ignore _unittests/ut_tasks/test_tasks_zero_shot_image_classification.py --ignore _unittests/ut_tasks/test_tasks_image_classification.py --ignore-glob _unittests/ut_torch_models/test_validate_whole_models*.py --ignore-glob _unittests/ut_torch_models/test_validate_models*.py
153153
154154
- name: test models
155155
run: |
156+
echo "----"
157+
PYTHONPATH=. UNITTEST_GOING=1 python _unittests/ut_torch_models/test_validate_models.py
156158
echo "----"
157159
PYTHONPATH=. UNITTEST_GOING=1 python _unittests/ut_torch_models/test_validate_whole_models1.py
158160
echo "----"

CHANGELOGS.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ Change Logs
44
0.8.2
55
+++++
66

7-
* :pr:`293`: second series of patches
8-
* :pr:`292`: new patches for Qwen models
7+
* :pr:`292`, :pr:`293`, :pr:`294`: new patches for Qwen models
98

109
0.8.1
1110
+++++

_unittests/ut_export/test_api.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import os
12
import unittest
23
import torch
3-
from onnx_diagnostic.ext_test_case import ExtTestCase, hide_stdout, has_transformers
4+
from onnx_diagnostic.ext_test_case import (
5+
ExtTestCase,
6+
hide_stdout,
7+
has_transformers,
8+
ignore_warnings,
9+
)
410
from onnx_diagnostic.helpers import max_diff
511
from onnx_diagnostic.helpers.torch_helper import torch_deepcopy
612
from onnx_diagnostic.helpers.rt_helper import make_feeds
@@ -36,6 +42,7 @@ def forward(self, x, y):
3642
)
3743

3844
@hide_stdout()
45+
@ignore_warnings(FutureWarning)
3946
def test_tiny_llm_to_onnx(self):
4047
import onnxruntime
4148

@@ -68,6 +75,8 @@ def test_tiny_llm_to_onnx(self):
6875
filename=filename,
6976
)
7077
for exporter, filename in filenames.items():
78+
if not os.path.exists(filename):
79+
continue
7180
with self.subTest(exporter=f"validate-{exporter}"):
7281
sess = onnxruntime.InferenceSession(
7382
filename, providers=["CPUExecutionProvider"]
@@ -90,6 +99,8 @@ def test_tiny_llm_to_onnx(self):
9099

91100
expected = model(**torch_deepcopy(problem))
92101
for exporter, filename in filenames.items():
102+
if not os.path.exists(filename):
103+
continue
93104
with self.subTest(exporter=f"full-mask-{exporter}"):
94105
sess = onnxruntime.InferenceSession(
95106
filename, providers=["CPUExecutionProvider"]

_unittests/ut_helpers/test_helper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,15 @@ def test_size_type_onnx(self):
342342
continue
343343
if name in {"NAME_FIELD_NUMBER"}:
344344
continue
345-
if name not in {"STRING", "UINT4", "INT4", "FLOAT4E2M1"}:
345+
if name not in {"STRING", "UINT4", "INT4", "FLOAT4E2M1", "INT2", "UINT2"}:
346346
size_type(i)
347347

348348
if name not in {
349349
"STRING",
350350
"UINT4",
351351
"INT4",
352+
"INT2",
353+
"UINT2",
352354
"FLOAT4E2M1",
353355
"FLOAT8E5M2FNUZ",
354356
"FLOAT8E5M2",

_unittests/ut_reference/test_backend_onnxruntime_evaluator.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,25 @@ def run_node(cls, node, inputs, device=None, outputs_info=None, **kwargs):
248248
)
249249

250250
if onnx_opset_version() <= 25:
251+
exc = "|".join(
252+
[
253+
"batchnorm_.*_training",
254+
"convinteger_with_padding",
255+
"rms_normalization",
256+
"rotary_embedding_3d",
257+
"rotary_embedding",
258+
# cuda,
259+
"test_Conv3d_dilated.*_cuda",
260+
"test_reduce_.*_empty_set_cuda",
261+
"test_reduce_sum_square_.*_expanded_cuda",
262+
"test_reduce_l1_.*_expanded_cuda",
263+
"test_reduce_l2_.*_expanded_cuda",
264+
"test_reduce_log_sum_.*_expanded_cuda",
265+
]
266+
)
267+
backend_test.exclude(f"({exc})")
268+
269+
if onnx_opset_version() <= 26:
251270
backend_test.exclude(
252271
"(deform_conv"
253272
"|gru"
@@ -261,36 +280,25 @@ def run_node(cls, node, inputs, device=None, outputs_info=None, **kwargs):
261280
"|layer_normalization.*expanded"
262281
"|layer_normalization.*expanded"
263282
"|affine_grid.*expanded"
283+
"|test_attention_4d_diff_heads_mask4d_padded_kv.*"
284+
"|test_convinteger_with_padding"
264285
"|test_rnn_seq"
265286
"|test_roialign_aligned_false"
266287
"|test_roialign_aligned_true"
267288
"|test_roialign_mode_max"
289+
"|test_rotary_embedding_no_position_ids_rotary_dim.*"
290+
"|test_rotary_embedding_with_interleaved_rotary_dim.*"
291+
"|test_rotary_embedding_with_rotary_dim*"
268292
"|test_simple_rnn_batchwise"
269293
"|test_simple_rnn_defaults"
270294
"|test_simple_rnn_with_initial_bias"
295+
"|test_swish*"
296+
"|test_tensorscatter*"
297+
"|test_top_k*"
271298
")"
272299
)
273300

274301

275-
if onnx_opset_version() <= 25:
276-
exc = "|".join(
277-
[
278-
"batchnorm_.*_training",
279-
"convinteger_with_padding",
280-
"rms_normalization",
281-
"rotary_embedding_3d",
282-
"rotary_embedding",
283-
# cuda,
284-
"test_Conv3d_dilated.*_cuda",
285-
"test_reduce_.*_empty_set_cuda",
286-
"test_reduce_sum_square_.*_expanded_cuda",
287-
"test_reduce_l1_.*_expanded_cuda",
288-
"test_reduce_l2_.*_expanded_cuda",
289-
"test_reduce_log_sum_.*_expanded_cuda",
290-
]
291-
)
292-
backend_test.exclude(f"({exc})")
293-
294302
if pv.Version(onnxruntime.__version__) <= pv.Version("1.24"):
295303
backend_test.exclude("(test_attention_4d_with|test_attention_4d_gqa)")
296304

_unittests/ut_tasks/try_tasks.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ def test_imagetext2text_qwen_2_5_vl_instruct(self):
10111011
return_dict:bool
10121012
)
10131013
"""
1014+
import transformers
10141015
from transformers import AutoModel, AutoProcessor
10151016
from qwen_vl_utils import process_vision_info
10161017

@@ -1068,6 +1069,20 @@ def config_reduction(config, task):
10681069
print(f"-- processor {type(processor)}")
10691070
print(f"-- inputs={self.string_type(inputs, with_shape=True, with_min_max=True)}")
10701071

1072+
f_ = transformers.models.qwen2_5_vl.modeling_qwen2_5_vl.apply_multimodal_rotary_pos_emb
1073+
1074+
def _apply_multimodal_rotary_pos_emb(*args, **kwargs):
1075+
print(
1076+
"-- apply_multimodal_rotary_pos_emb:",
1077+
self.string_type(args, with_shape=True),
1078+
self.string_type(kwargs, with_shape=True),
1079+
)
1080+
return f_(*args, **kwargs)
1081+
1082+
transformers.models.qwen2_5_vl.modeling_qwen2_5_vl.apply_multimodal_rotary_pos_emb = (
1083+
_apply_multimodal_rotary_pos_emb
1084+
)
1085+
10711086
print()
10721087
with (
10731088
torch_export_patches(
@@ -1077,7 +1092,7 @@ def config_reduction(config, task):
10771092
verbose=1,
10781093
),
10791094
steal_forward(
1080-
[model, model.visual],
1095+
[model, model.visual, model.visual.blocks[0].attn],
10811096
dump_file=self.get_dump_file("test_imagetext2text_qwen_2_5_vl_instruct.onnx"),
10821097
dump_drop={"attention_mask", "past_key_values", "pixel_values"},
10831098
save_as_external_data=False,

_unittests/ut_torch_export_patches/test_dynamic_class.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
)
2222
from onnx_diagnostic.torch_export_patches.patch_inputs import use_dyn_not_str
2323
from onnx_diagnostic.torch_models.hghub.model_inputs import get_untrained_model_with_inputs
24-
import onnx_diagnostic.torch_export_patches.patches.patch_transformers as patch_transformers
2524

2625

2726
class TestOnnxExportErrors(ExtTestCase):
@@ -344,11 +343,6 @@ def test_phi2_export_interpreter(self):
344343
)
345344

346345
with torch_export_patches(patch_transformers=True, verbose=1):
347-
if masking_utils is not None:
348-
self.assertEqual(
349-
masking_utils.ALL_MASK_ATTENTION_FUNCTIONS["sdpa"],
350-
patch_transformers.patched_sdpa_mask_recent_torch,
351-
)
352346
ep = torch.export.export(
353347
model,
354348
(),

0 commit comments

Comments
 (0)