Skip to content

Commit a22b1f1

Browse files
authored
Merge pull request #39 from meizhong986/copilot/fix-preflight-check-warning
Fix CUDA version type mismatch in preflight check
2 parents 644544c + f7e99d4 commit a22b1f1

2 files changed

Lines changed: 124 additions & 2 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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()

whisperjav/utils/preflight_check.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,18 @@ def _check_pytorch_cuda(self):
149149
compiled_cuda = torch._C._cuda_getCompiledVersion()
150150
runtime_cuda = torch.version.cuda
151151

152-
if compiled_cuda == runtime_cuda:
152+
# Convert compiled_cuda integer to version string for comparison
153+
# compiled_cuda is an integer like 12090 representing CUDA 12.9.0
154+
# We extract major.minor (12.9) to match runtime_cuda format (string "12.9")
155+
# Note: Patch version is intentionally truncated as runtime_cuda doesn't include it
156+
if isinstance(compiled_cuda, int):
157+
major = compiled_cuda // 1000
158+
minor = (compiled_cuda % 1000) // 10
159+
compiled_cuda_str = f"{major}.{minor}"
160+
else:
161+
compiled_cuda_str = str(compiled_cuda)
162+
163+
if compiled_cuda_str == runtime_cuda:
153164
self.results.append(CheckResult(
154165
name="PyTorch CUDA Build",
155166
status=CheckStatus.PASS,
@@ -161,7 +172,7 @@ def _check_pytorch_cuda(self):
161172
status=CheckStatus.WARN,
162173
message="CUDA version mismatch",
163174
details=[
164-
f"PyTorch compiled for: CUDA {compiled_cuda}",
175+
f"PyTorch compiled for: CUDA {compiled_cuda_str}",
165176
f"Runtime CUDA version: {runtime_cuda}",
166177
"This may cause compatibility issues"
167178
]

0 commit comments

Comments
 (0)