|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test CUDA version comparison fix in preflight checks.""" |
| 3 | + |
| 4 | +import unittest |
| 5 | +from unittest.mock import Mock, patch, MagicMock |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +# Add parent directory to path |
| 10 | +sys.path.insert(0, str(Path(__file__).parent.parent)) |
| 11 | + |
| 12 | +from whisperjav.utils.preflight_check import PreflightChecker, CheckStatus |
| 13 | + |
| 14 | + |
| 15 | +class TestCudaVersionComparison(unittest.TestCase): |
| 16 | + """Test the CUDA version comparison logic.""" |
| 17 | + |
| 18 | + def test_cuda_version_match_integer_to_string(self): |
| 19 | + """Test that integer compiled CUDA version matches string runtime version.""" |
| 20 | + with patch('whisperjav.utils.preflight_check.sys') as mock_sys: |
| 21 | + mock_sys.version_info = (3, 10, 0) |
| 22 | + |
| 23 | + # Create mock torch module |
| 24 | + mock_torch = MagicMock() |
| 25 | + mock_torch.cuda.is_available.return_value = True |
| 26 | + mock_torch.cuda.device_count.return_value = 1 |
| 27 | + mock_torch.cuda.get_device_name.return_value = "Test GPU" |
| 28 | + |
| 29 | + # This is the key part: compiled version is int 12090, runtime is string "12.9" |
| 30 | + mock_torch._C._cuda_getCompiledVersion.return_value = 12090 |
| 31 | + mock_torch.version.cuda = "12.9" |
| 32 | + |
| 33 | + # Mock CUDA operations |
| 34 | + mock_tensor = MagicMock() |
| 35 | + mock_torch.zeros.return_value = mock_tensor |
| 36 | + mock_tensor.cuda.return_value = mock_tensor |
| 37 | + |
| 38 | + with patch.dict('sys.modules', {'torch': mock_torch}): |
| 39 | + checker = PreflightChecker(verbose=False) |
| 40 | + checker._check_pytorch_cuda() |
| 41 | + |
| 42 | + # Find the PyTorch CUDA Build result |
| 43 | + cuda_build_result = None |
| 44 | + for result in checker.results: |
| 45 | + if result.name == "PyTorch CUDA Build": |
| 46 | + cuda_build_result = result |
| 47 | + break |
| 48 | + |
| 49 | + self.assertIsNotNone(cuda_build_result, "PyTorch CUDA Build check should exist") |
| 50 | + self.assertEqual(cuda_build_result.status, CheckStatus.PASS, |
| 51 | + "CUDA version 12090 (int) should match '12.9' (string)") |
| 52 | + self.assertIn("12.9", cuda_build_result.message) |
| 53 | + |
| 54 | + def test_cuda_version_mismatch(self): |
| 55 | + """Test that actual CUDA version mismatches are still detected.""" |
| 56 | + with patch('whisperjav.utils.preflight_check.sys') as mock_sys: |
| 57 | + mock_sys.version_info = (3, 10, 0) |
| 58 | + |
| 59 | + # Create mock torch module |
| 60 | + mock_torch = MagicMock() |
| 61 | + mock_torch.cuda.is_available.return_value = True |
| 62 | + mock_torch.cuda.device_count.return_value = 1 |
| 63 | + mock_torch.cuda.get_device_name.return_value = "Test GPU" |
| 64 | + |
| 65 | + # Different versions: 11080 -> "11.8", but runtime is "12.1" |
| 66 | + mock_torch._C._cuda_getCompiledVersion.return_value = 11080 |
| 67 | + mock_torch.version.cuda = "12.1" |
| 68 | + |
| 69 | + # Mock CUDA operations |
| 70 | + mock_tensor = MagicMock() |
| 71 | + mock_torch.zeros.return_value = mock_tensor |
| 72 | + mock_tensor.cuda.return_value = mock_tensor |
| 73 | + |
| 74 | + with patch.dict('sys.modules', {'torch': mock_torch}): |
| 75 | + checker = PreflightChecker(verbose=False) |
| 76 | + checker._check_pytorch_cuda() |
| 77 | + |
| 78 | + # Find the PyTorch CUDA Build result |
| 79 | + cuda_build_result = None |
| 80 | + for result in checker.results: |
| 81 | + if result.name == "PyTorch CUDA Build": |
| 82 | + cuda_build_result = result |
| 83 | + break |
| 84 | + |
| 85 | + self.assertIsNotNone(cuda_build_result, "PyTorch CUDA Build check should exist") |
| 86 | + self.assertEqual(cuda_build_result.status, CheckStatus.WARN, |
| 87 | + "Different CUDA versions should produce a warning") |
| 88 | + self.assertIn("11.8", str(cuda_build_result.details)) |
| 89 | + self.assertIn("12.1", str(cuda_build_result.details)) |
| 90 | + |
| 91 | + def test_cuda_version_conversion_edge_cases(self): |
| 92 | + """Test various CUDA version integer conversions.""" |
| 93 | + test_cases = [ |
| 94 | + (12090, "12.9"), # From the issue screenshot |
| 95 | + (12010, "12.1"), |
| 96 | + (11080, "11.8"), |
| 97 | + (12000, "12.0"), |
| 98 | + (13020, "13.2"), |
| 99 | + ] |
| 100 | + |
| 101 | + for compiled_int, expected_str in test_cases: |
| 102 | + with self.subTest(compiled=compiled_int, expected=expected_str): |
| 103 | + major = compiled_int // 1000 |
| 104 | + minor = (compiled_int % 1000) // 10 |
| 105 | + result = f"{major}.{minor}" |
| 106 | + self.assertEqual(result, expected_str, |
| 107 | + f"Integer {compiled_int} should convert to '{expected_str}'") |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + unittest.main() |
0 commit comments