diff --git a/swift/template/templates/internvl.py b/swift/template/templates/internvl.py index 444a161a92..faac371da5 100644 --- a/swift/template/templates/internvl.py +++ b/swift/template/templates/internvl.py @@ -30,6 +30,7 @@ def init_env_args(self): def replace_tag(self, media_type: Literal['image', 'video', 'audio'], index: int, inputs: StdTemplateInputs) -> List[Context]: if self.mode == 'vllm': + inputs.mm_processor_kwargs.setdefault('max_dynamic_patch', self.max_num) image_context = ['\n'] else: image_context = ['', [-100], '\n'] diff --git a/tests/general/test_internvl_template.py b/tests/general/test_internvl_template.py new file mode 100644 index 0000000000..1661128fe3 --- /dev/null +++ b/tests/general/test_internvl_template.py @@ -0,0 +1,44 @@ +import unittest +from types import SimpleNamespace + +from swift.template.templates.internvl import InternvlTemplate + + +class TestInternvlTemplate(unittest.TestCase): + + @staticmethod + def _template(mode): + template = object.__new__(InternvlTemplate) + template.mode = mode + template.max_num = 6 + return template + + def test_vllm_receives_max_dynamic_patch(self): + template = self._template('vllm') + inputs = SimpleNamespace(mm_processor_kwargs={}) + + context = template.replace_tag('image', 0, inputs) + + self.assertEqual(context, ['\n']) + self.assertEqual(inputs.mm_processor_kwargs, {'max_dynamic_patch': 6}) + + def test_vllm_preserves_explicit_max_dynamic_patch(self): + template = self._template('vllm') + inputs = SimpleNamespace(mm_processor_kwargs={'max_dynamic_patch': 4}) + + template.replace_tag('image', 0, inputs) + + self.assertEqual(inputs.mm_processor_kwargs, {'max_dynamic_patch': 4}) + + def test_transformers_does_not_receive_vllm_processor_kwargs(self): + template = self._template('transformers') + inputs = SimpleNamespace(mm_processor_kwargs={}) + + context = template.replace_tag('image', 0, inputs) + + self.assertEqual(context, ['', [-100], '\n']) + self.assertEqual(inputs.mm_processor_kwargs, {}) + + +if __name__ == '__main__': + unittest.main()