|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +from vlmeval.config import supported_VLM, qwen3_8_series |
| 6 | +from vlmeval.api.adapters import build_adapter |
| 7 | +from vlmeval.vlm.qwen3_vl.model import is_moe_model |
| 8 | + |
| 9 | + |
| 10 | +class TestAllQwen3_8Models(unittest.TestCase): |
| 11 | + |
| 12 | + def test_all_15_models_configuration(self): |
| 13 | + """Verify each of the 15 Qwen 3.8 models has correct configuration in supported_VLM.""" |
| 14 | + self.assertEqual(len(qwen3_8_series), 15) |
| 15 | + for name, partial_func in qwen3_8_series.items(): |
| 16 | + self.assertIn(name, supported_VLM, f"{name} not found in supported_VLM") |
| 17 | + func = partial_func.func |
| 18 | + keywords = partial_func.keywords |
| 19 | + print(f"Verified config for {name} -> {func.__name__} (keys: {list(keywords.keys())})") |
| 20 | + |
| 21 | + @patch('transformers.AutoModelForImageTextToText.from_pretrained') |
| 22 | + @patch('transformers.AutoProcessor.from_pretrained') |
| 23 | + @patch('vlmeval.vlm.qwen3_vl.model.get_gpu_memory', return_value=[80000]) |
| 24 | + @patch('vlmeval.vlm.qwen3_vl.model.torch.cuda.device_count', return_value=1) |
| 25 | + def test_all_open_weights_models_pipeline(self, mock_gpu_count, mock_gpu_mem, mock_proc, mock_model): |
| 26 | + """Test instantiation, prompt generation, and inference pipeline for all open-weights models.""" |
| 27 | + open_weight_models = [ |
| 28 | + "Qwen3.8-27B", |
| 29 | + "Qwen3.8-27B-Thinking", |
| 30 | + "Qwen3.8-27B-Instruct", |
| 31 | + "Qwen3.8-27B-FP8", |
| 32 | + "Qwen3.8-2.4T-A95B", |
| 33 | + "Qwen3.8-Flash-Next", |
| 34 | + "Qwen3.8-Flash-Next-FP8", |
| 35 | + ] |
| 36 | + |
| 37 | + mock_processor_instance = MagicMock() |
| 38 | + mock_processor_instance.apply_chat_template.return_value = "<mock_prompt>" |
| 39 | + mock_processor_instance.tokenizer.batch_decode.return_value = ["A single red apple."] |
| 40 | + mock_proc.return_value = mock_processor_instance |
| 41 | + |
| 42 | + mock_model_instance = MagicMock() |
| 43 | + mock_output = MagicMock() |
| 44 | + mock_model_instance.generate.return_value = [[1, 2, 3, 4]] |
| 45 | + mock_model.return_value = mock_model_instance |
| 46 | + |
| 47 | + img_path = os.path.abspath('assets/apple.jpg') |
| 48 | + test_messages = [ |
| 49 | + {'type': 'image', 'value': img_path}, |
| 50 | + {'type': 'text', 'value': 'Describe what is in this image.'} |
| 51 | + ] |
| 52 | + |
| 53 | + for name in open_weight_models: |
| 54 | + builder = supported_VLM[name] |
| 55 | + # Override use_vllm=False for testing transformers generation pipeline |
| 56 | + model = builder(use_vllm=False) |
| 57 | + model.set_dump_image(lambda l: img_path) |
| 58 | + |
| 59 | + # Test prompt building for MMMU, MCQ, Y/N, VQA |
| 60 | + line = {'question': 'Is this an apple?', 'A': 'Yes', 'B': 'No'} |
| 61 | + mmmu_prompt = model.build_prompt(line, dataset='MMMU_DEV_VAL') |
| 62 | + self.assertEqual(mmmu_prompt[0]['type'], 'image') |
| 63 | + self.assertEqual(mmmu_prompt[1]['type'], 'text') |
| 64 | + |
| 65 | + mcq_prompt = model.build_prompt(line, dataset='MMBench_DEV_EN') |
| 66 | + self.assertIn('Answer with the option letter only.', mcq_prompt[1]['value']) |
| 67 | + |
| 68 | + yorn_prompt = model.build_prompt(line, dataset='MME') |
| 69 | + self.assertIn('Please answer yes or no.', yorn_prompt[1]['value']) |
| 70 | + |
| 71 | + vqa_prompt = model.build_prompt(line, dataset='DocVQA_VAL') |
| 72 | + self.assertIn('Please answer concisely', vqa_prompt[1]['value']) |
| 73 | + |
| 74 | + # Test generation through VLMEvalKit generate() entrypoint |
| 75 | + with patch('qwen_vl_utils.process_vision_info', return_value=(None, None, None)): |
| 76 | + out = model.generate(test_messages) |
| 77 | + self.assertEqual(out, "A single red apple.") |
| 78 | + print(f"[PASSED] Open-weights model pipeline: {name}") |
| 79 | + |
| 80 | + @patch('urllib.request.urlopen') |
| 81 | + def test_all_api_models_pipeline(self, mock_urlopen): |
| 82 | + """Test instantiation and payload construction for all LMDeploy / vLLM server API models.""" |
| 83 | + api_models = [ |
| 84 | + "Qwen3.8-27B_api", |
| 85 | + "Qwen3.8-27B_ThinkMode_api", |
| 86 | + "Qwen3.8-27B_InstructMode_api", |
| 87 | + "Qwen3.8-2.4T-A95B_api", |
| 88 | + "Qwen3.8-Flash-Next_api", |
| 89 | + ] |
| 90 | + |
| 91 | + for name in api_models: |
| 92 | + builder = supported_VLM[name] |
| 93 | + model = builder() |
| 94 | + self.assertEqual(model.api_base, "http://0.0.0.0:8000/v1/chat/completions") |
| 95 | + self.assertTrue(hasattr(model, 'generate')) |
| 96 | + print(f"[PASSED] Server API model configuration: {name}") |
| 97 | + |
| 98 | + def test_all_dashscope_api_models_pipeline(self): |
| 99 | + """Test instantiation and message preparation for all DashScope cloud API models.""" |
| 100 | + dashscope_models = [ |
| 101 | + "Qwen3.8-Max", |
| 102 | + "Qwen3.8-27B-API", |
| 103 | + "Qwen3.8-Flash-Next-API", |
| 104 | + ] |
| 105 | + |
| 106 | + img_path = os.path.abspath('assets/apple.jpg') |
| 107 | + test_inputs = [ |
| 108 | + {'type': 'image', 'value': img_path}, |
| 109 | + {'type': 'text', 'value': 'What is this?'} |
| 110 | + ] |
| 111 | + |
| 112 | + for name in dashscope_models: |
| 113 | + builder = supported_VLM[name] |
| 114 | + # Initialize with dummy test key to verify structure |
| 115 | + model = builder(key='mock-dashscope-key') |
| 116 | + self.assertTrue(model.is_api) |
| 117 | + prepared = model._prepare_content(test_inputs) |
| 118 | + self.assertEqual(prepared[0]['type'], 'image') |
| 119 | + self.assertEqual(prepared[1]['type'], 'text') |
| 120 | + print(f"[PASSED] DashScope API model: {name} (target model: {model.model})") |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == '__main__': |
| 124 | + unittest.main() |
0 commit comments