|
| 1 | +import pytest |
| 2 | +import json |
| 3 | +import sys |
| 4 | +import importlib.util |
| 5 | +from pathlib import Path |
| 6 | +from io import StringIO |
| 7 | + |
| 8 | + |
| 9 | +def create_mock_result_file(tmp_path): |
| 10 | + """Create a mock result JSON file.""" |
| 11 | + result_data = { |
| 12 | + "max_concurrency": 10, |
| 13 | + "model_id": "test-model", |
| 14 | + "total_token_throughput": 1000.0, |
| 15 | + "output_throughput": 400.0, |
| 16 | + "ttft_ms": 50.0, |
| 17 | + "tpot_ms": 20.0 |
| 18 | + } |
| 19 | + result_file = tmp_path / "test_result.json" |
| 20 | + with open(result_file, 'w') as f: |
| 21 | + json.dump(result_data, f) |
| 22 | + return result_file |
| 23 | + |
| 24 | + |
| 25 | +def run_process_result_script(tmp_path): |
| 26 | + """Helper to run process_result.py and return the output data.""" |
| 27 | + # Create mock result file |
| 28 | + create_mock_result_file(tmp_path) |
| 29 | + |
| 30 | + # Get script path relative to this test file |
| 31 | + script_path = Path(__file__).parent / "process_result.py" |
| 32 | + spec = importlib.util.spec_from_file_location("process_result", script_path) |
| 33 | + module = importlib.util.module_from_spec(spec) |
| 34 | + |
| 35 | + # Capture stdout |
| 36 | + old_stdout = sys.stdout |
| 37 | + sys.stdout = StringIO() |
| 38 | + |
| 39 | + try: |
| 40 | + spec.loader.exec_module(module) |
| 41 | + output = sys.stdout.getvalue() |
| 42 | + return json.loads(output) |
| 43 | + finally: |
| 44 | + sys.stdout = old_stdout |
| 45 | + |
| 46 | + |
| 47 | +def test_disagg_true_when_both_env_vars_set(tmp_path, monkeypatch): |
| 48 | + """Test that disagg=true when both PREFILL_GPUS and DECODE_GPUS are set.""" |
| 49 | + # Set environment variables |
| 50 | + monkeypatch.setenv('RUNNER_TYPE', 'h200') |
| 51 | + monkeypatch.setenv('TP', '8') |
| 52 | + monkeypatch.setenv('EP_SIZE', '1') |
| 53 | + monkeypatch.setenv('PREFILL_GPUS', '4') |
| 54 | + monkeypatch.setenv('DECODE_GPUS', '4') |
| 55 | + monkeypatch.setenv('DP_ATTENTION', 'false') |
| 56 | + monkeypatch.setenv('RESULT_FILENAME', 'test_result') |
| 57 | + monkeypatch.setenv('FRAMEWORK', 'vllm') |
| 58 | + monkeypatch.setenv('PRECISION', 'fp8') |
| 59 | + |
| 60 | + # Change to tmp_path directory |
| 61 | + monkeypatch.chdir(tmp_path) |
| 62 | + |
| 63 | + # Run the script and get output |
| 64 | + data = run_process_result_script(tmp_path) |
| 65 | + |
| 66 | + # Check that disagg is true |
| 67 | + assert data['disagg'] is True |
| 68 | + # Check that num_prefill_gpu and num_decode_gpu are present |
| 69 | + assert data['num_prefill_gpu'] == 4 |
| 70 | + assert data['num_decode_gpu'] == 4 |
| 71 | + |
| 72 | + |
| 73 | +def test_disagg_false_when_prefill_gpus_not_set(tmp_path, monkeypatch): |
| 74 | + """Test that disagg=false when PREFILL_GPUS is not set.""" |
| 75 | + # Set environment variables (without PREFILL_GPUS) |
| 76 | + monkeypatch.setenv('RUNNER_TYPE', 'h200') |
| 77 | + monkeypatch.setenv('TP', '8') |
| 78 | + monkeypatch.setenv('EP_SIZE', '1') |
| 79 | + monkeypatch.setenv('DECODE_GPUS', '4') |
| 80 | + monkeypatch.setenv('DP_ATTENTION', 'false') |
| 81 | + monkeypatch.setenv('RESULT_FILENAME', 'test_result') |
| 82 | + monkeypatch.setenv('FRAMEWORK', 'vllm') |
| 83 | + monkeypatch.setenv('PRECISION', 'fp8') |
| 84 | + |
| 85 | + # Change to tmp_path directory |
| 86 | + monkeypatch.chdir(tmp_path) |
| 87 | + |
| 88 | + # Run the script and get output |
| 89 | + data = run_process_result_script(tmp_path) |
| 90 | + |
| 91 | + # Check that disagg is false |
| 92 | + assert data['disagg'] is False |
| 93 | + # Check that num_prefill_gpu and num_decode_gpu are NOT present |
| 94 | + assert 'num_prefill_gpu' not in data |
| 95 | + assert 'num_decode_gpu' not in data |
| 96 | + |
| 97 | + |
| 98 | +def test_disagg_false_when_decode_gpus_not_set(tmp_path, monkeypatch): |
| 99 | + """Test that disagg=false when DECODE_GPUS is not set.""" |
| 100 | + # Set environment variables (without DECODE_GPUS) |
| 101 | + monkeypatch.setenv('RUNNER_TYPE', 'h200') |
| 102 | + monkeypatch.setenv('TP', '8') |
| 103 | + monkeypatch.setenv('EP_SIZE', '1') |
| 104 | + monkeypatch.setenv('PREFILL_GPUS', '4') |
| 105 | + monkeypatch.setenv('DP_ATTENTION', 'false') |
| 106 | + monkeypatch.setenv('RESULT_FILENAME', 'test_result') |
| 107 | + monkeypatch.setenv('FRAMEWORK', 'vllm') |
| 108 | + monkeypatch.setenv('PRECISION', 'fp8') |
| 109 | + |
| 110 | + # Change to tmp_path directory |
| 111 | + monkeypatch.chdir(tmp_path) |
| 112 | + |
| 113 | + # Run the script and get output |
| 114 | + data = run_process_result_script(tmp_path) |
| 115 | + |
| 116 | + # Check that disagg is false |
| 117 | + assert data['disagg'] is False |
| 118 | + # Check that num_prefill_gpu and num_decode_gpu are NOT present |
| 119 | + assert 'num_prefill_gpu' not in data |
| 120 | + assert 'num_decode_gpu' not in data |
| 121 | + |
| 122 | + |
| 123 | +def test_disagg_false_when_both_env_vars_empty_strings(tmp_path, monkeypatch): |
| 124 | + """Test that disagg=false when both PREFILL_GPUS and DECODE_GPUS are empty strings.""" |
| 125 | + # Set environment variables with empty strings |
| 126 | + monkeypatch.setenv('RUNNER_TYPE', 'h200') |
| 127 | + monkeypatch.setenv('TP', '8') |
| 128 | + monkeypatch.setenv('EP_SIZE', '1') |
| 129 | + monkeypatch.setenv('PREFILL_GPUS', '') |
| 130 | + monkeypatch.setenv('DECODE_GPUS', '') |
| 131 | + monkeypatch.setenv('DP_ATTENTION', 'false') |
| 132 | + monkeypatch.setenv('RESULT_FILENAME', 'test_result') |
| 133 | + monkeypatch.setenv('FRAMEWORK', 'vllm') |
| 134 | + monkeypatch.setenv('PRECISION', 'fp8') |
| 135 | + |
| 136 | + # Change to tmp_path directory |
| 137 | + monkeypatch.chdir(tmp_path) |
| 138 | + |
| 139 | + # Run the script and get output |
| 140 | + data = run_process_result_script(tmp_path) |
| 141 | + |
| 142 | + # Check that disagg is false |
| 143 | + assert data['disagg'] is False |
| 144 | + # Check that num_prefill_gpu and num_decode_gpu are NOT present |
| 145 | + assert 'num_prefill_gpu' not in data |
| 146 | + assert 'num_decode_gpu' not in data |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + pytest.main([__file__, "-v"]) |
0 commit comments