|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +import gc |
| 5 | + |
| 6 | +import pytest |
| 7 | +import torch |
| 8 | +from torch.torch_version import TorchVersion |
| 9 | + |
| 10 | +from vllm import LLM, SamplingParams |
| 11 | +from vllm.config.compilation import DynamicShapesType |
| 12 | + |
| 13 | + |
| 14 | +def cleanup_gpu_memory(): |
| 15 | + """Clean up GPU memory after each test""" |
| 16 | + gc.collect() # Clear Python objects |
| 17 | + torch.cuda.empty_cache() # Clear PyTorch GPU memory cache |
| 18 | + torch.cuda.synchronize() # Wait for all GPU operations to complete |
| 19 | + |
| 20 | + |
| 21 | +def get_test_models(): |
| 22 | + """Get list of models to test based on PyTorch version""" |
| 23 | + # Parse PyTorch version |
| 24 | + result = ["microsoft/DialoGPT-small", "gpt2", "facebook/opt-125m"] |
| 25 | + # Handle alpha versions by removing pre-release suffixes |
| 26 | + version_parts = torch.__version__.split('+')[0].split('a')[0] |
| 27 | + clean_version = version_parts.split('b')[0].split('rc')[0] |
| 28 | + if TorchVersion(clean_version) >= TorchVersion("2.10"): |
| 29 | + |
| 30 | + # Requires some fixes only available in PyTorch 2.10+ |
| 31 | + result.append("Qwen/Qwen2-1.5B-Instruct") |
| 32 | + result.append("Qwen/Qwen2-7B-Instruct") |
| 33 | + result.append("openlm-research/open_llama_13b") |
| 34 | + |
| 35 | + return result |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("model_name", get_test_models()) |
| 39 | +def test_dynamic_shapes_compilation(monkeypatch, model_name): |
| 40 | + """Test that all dynamic shapes types produce compiles""" |
| 41 | + print(f"\nTesting model: {model_name}") |
| 42 | + |
| 43 | + # monkeypatch.setenv("VLLM_ENABLE_V1_MULTIPROCESSING", "0") |
| 44 | + monkeypatch.setenv("TOKENIZERS_PARALLELISM", "true") |
| 45 | + |
| 46 | + prompt = "Hello, my name is" |
| 47 | + results = {} |
| 48 | + |
| 49 | + print("Testing EAGER (no compilation) baseline...") |
| 50 | + cleanup_gpu_memory() |
| 51 | + |
| 52 | + eager_model = LLM( |
| 53 | + model=model_name, |
| 54 | + compilation_config={ |
| 55 | + "level": 0, # NO_COMPILATION - eager mode |
| 56 | + }, |
| 57 | + # gpu_memory_utilization=0.2, |
| 58 | + ) |
| 59 | + |
| 60 | + # Generate text with deterministic sampling parameters |
| 61 | + sampling_params = SamplingParams( |
| 62 | + max_tokens=10, |
| 63 | + temperature=0.0, # Deterministic generation |
| 64 | + seed=42, # Fixed seed for consistency |
| 65 | + ) |
| 66 | + eager_output = eager_model.generate(prompt, |
| 67 | + sampling_params=sampling_params) |
| 68 | + results["EAGER"] = eager_output[0].outputs[0].text |
| 69 | + |
| 70 | + # Cleanup model |
| 71 | + del eager_model |
| 72 | + cleanup_gpu_memory() |
| 73 | + |
| 74 | + # Test all dynamic shapes types with compilation |
| 75 | + for shapes_type in [ |
| 76 | + DynamicShapesType.BACKED, DynamicShapesType.UNBACKED, |
| 77 | + DynamicShapesType.BACKED_SIZE_OBLIVIOUS |
| 78 | + ]: |
| 79 | + print(f"Testing {shapes_type.name} dynamic shapes...") |
| 80 | + |
| 81 | + # Initialize the model with specific dynamic shapes configuration |
| 82 | + model = LLM( |
| 83 | + model=model_name, |
| 84 | + compilation_config={ |
| 85 | + "level": 3, # PIECEWISE compilation |
| 86 | + "dynamic_shapes_config": { |
| 87 | + "dynamic_shapes_type": shapes_type.value, |
| 88 | + "eval_dynamo_ds_guards": False, |
| 89 | + }, |
| 90 | + }, |
| 91 | + # gpu_memory_utilization=0.2, |
| 92 | + ) |
| 93 | + |
| 94 | + output = model.generate(prompt, sampling_params=sampling_params) |
| 95 | + |
| 96 | + # Store results for comparison |
| 97 | + results[shapes_type.name] = output[0].outputs[0].text |
| 98 | + |
| 99 | + # Cleanup model |
| 100 | + del model |
| 101 | + cleanup_gpu_memory() |
| 102 | + |
| 103 | + # Verify all results are non-empty strings |
| 104 | + for shape_type, result in results.items(): |
| 105 | + assert isinstance(result, str), f"{shape_type} should return a string" |
| 106 | + assert len( |
| 107 | + result.strip()) > 0, f"{shape_type} should generate non-empty text" |
| 108 | + |
| 109 | + # Print results |
| 110 | + for shape_type, result in results.items(): |
| 111 | + print(f"{shape_type}: '{result}'") |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + """Run the test directly as a Python script""" |
| 116 | + import os |
| 117 | + |
| 118 | + print("Running dynamic shapes compilation test...") |
| 119 | + |
| 120 | + # Get test models based on PyTorch version |
| 121 | + test_models = get_test_models() |
| 122 | + print(f"Testing {len(test_models)} models: {test_models}") |
| 123 | + |
| 124 | + # Create a mock monkeypatch object for environment variables |
| 125 | + class MockMonkeypatch: |
| 126 | + |
| 127 | + def setenv(self, key, value): |
| 128 | + os.environ[key] = value |
| 129 | + |
| 130 | + monkeypatch = MockMonkeypatch() |
| 131 | + |
| 132 | + # Run test for each model |
| 133 | + for model_name in test_models: |
| 134 | + try: |
| 135 | + print(f"\n{'='*60}") |
| 136 | + print(f"Testing model: {model_name}") |
| 137 | + print(f"{'='*60}") |
| 138 | + |
| 139 | + test_dynamic_shapes_compilation(monkeypatch, model_name) |
| 140 | + |
| 141 | + print(f"✅ Test passed for {model_name}") |
| 142 | + |
| 143 | + except Exception as e: |
| 144 | + print(f"❌ Test failed for {model_name}: {e}") |
| 145 | + raise |
| 146 | + |
| 147 | + print("\n🎉 All tests completed successfully!") |
0 commit comments