From 623549b0a9888fb8544154e6c04d753d9e444438 Mon Sep 17 00:00:00 2001 From: taking-lying-flat <1615405@qq.com> Date: Sat, 5 Sep 2026 08:44:00 +0800 Subject: [PATCH] Fix optional dependency imports in CI tests --- swift/template/templates/gemma.py | 4 ++-- tests/general/test_gemma3_template.py | 5 +++-- tests/llm/test_web_ui.py | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/swift/template/templates/gemma.py b/swift/template/templates/gemma.py index 0f0e66a76a..9ce1203bfc 100644 --- a/swift/template/templates/gemma.py +++ b/swift/template/templates/gemma.py @@ -105,12 +105,12 @@ def replace_tag(self, media_type: Literal['image', 'video', 'audio'], index: int return [''] def _encode(self, inputs: StdTemplateInputs) -> Dict[str, Any]: - from transformers.models.gemma3.processing_gemma3 import Gemma3ProcessorKwargs - encoded = super()._encode(inputs) # Keep text-only rows aligned with multimodal rows in mixed batches. encoded['token_type_ids'] = [0] * len(encoded['input_ids']) if inputs.images: + from transformers.models.gemma3.processing_gemma3 import Gemma3ProcessorKwargs + input_ids = encoded['input_ids'] labels = encoded['labels'] loss_scale = encoded.get('loss_scale', None) diff --git a/tests/general/test_gemma3_template.py b/tests/general/test_gemma3_template.py index db48dc5034..0c7469afdc 100644 --- a/tests/general/test_gemma3_template.py +++ b/tests/general/test_gemma3_template.py @@ -12,7 +12,8 @@ def test_text_only_encode_has_token_type_ids(self): encoded = {'input_ids': [2, 10, 20], 'labels': [-100, 10, 20]} inputs = SimpleNamespace(images=[]) - with patch.object(Gemma3Template, '_encode', return_value=encoded): - result = template._encode(inputs) + with patch.dict('sys.modules', {'transformers.models.gemma3.processing_gemma3': None}): + with patch.object(Gemma3Template, '_encode', return_value=encoded): + result = template._encode(inputs) self.assertEqual(result['token_type_ids'], [0, 0, 0]) diff --git a/tests/llm/test_web_ui.py b/tests/llm/test_web_ui.py index bd64e704c9..71b759ffbd 100644 --- a/tests/llm/test_web_ui.py +++ b/tests/llm/test_web_ui.py @@ -1,5 +1,5 @@ -from swift.arguments import WebUIArguments -from swift.ui import webui_main - if __name__ == '__main__': + from swift.arguments import WebUIArguments + from swift.ui import webui_main + webui_main(WebUIArguments())