|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | + |
| 5 | +import vsi.tools.gpu_check |
| 6 | + |
| 7 | + |
| 8 | +# dummy class to represent the loaded libcudart.so library |
| 9 | +class MockLibrary: |
| 10 | + def __init__(self, file): |
| 11 | + self._name = file |
| 12 | + self.cudaGetDeviceCount = MagicMock(return_value="cudaGetDeviceCount") |
| 13 | + |
| 14 | + |
| 15 | +# unit tests |
| 16 | +class GpuCheckTest(unittest.TestCase): |
| 17 | + |
| 18 | + def _check_logs(self, logs, logs_expected): |
| 19 | + '''Check for expected logs''' |
| 20 | + |
| 21 | + # check length |
| 22 | + self.assertEqual(len(logs.output), len(logs_expected), |
| 23 | + f'Logs are of different length\n{logs.output=}') |
| 24 | + |
| 25 | + # check that expected substring is in each log item |
| 26 | + for idx, (item, expected) in enumerate(zip(logs.output, logs_expected)): |
| 27 | + self.assertIn(expected, item, |
| 28 | + f'Log mismatch line {idx}\n{logs.output=}') |
| 29 | + |
| 30 | + |
| 31 | + @patch('vsi.tools.gpu_check.pathlib.Path.rglob') |
| 32 | + @patch('vsi.tools.gpu_check.ctypes.cdll.LoadLibrary', side_effect=MockLibrary) |
| 33 | + def test_find_cudart(self, mock_LoadLibrary, mock_rglob): |
| 34 | + '''Test find_cudart''' |
| 35 | + |
| 36 | + # rglob return for 3 search_dirs |
| 37 | + search_dirs = ['/foo', '/bar', '/path/to'] |
| 38 | + mock_rglob.side_effect = [list(), list(), ['/path/to/liubcudart.so']] |
| 39 | + |
| 40 | + # call function & check return |
| 41 | + result = vsi.tools.gpu_check.find_cudart(search_dirs) |
| 42 | + self.assertEqual(result._name, '/path/to/liubcudart.so') |
| 43 | + |
| 44 | + |
| 45 | + @patch('vsi.tools.gpu_check.pathlib.Path.rglob') |
| 46 | + def test_find_cudart_error(self, mock_rglob): |
| 47 | + '''Test find_cudart without discovering a file''' |
| 48 | + |
| 49 | + # rglob return for 1 search_dirs |
| 50 | + search_dirs = ['/foo'] |
| 51 | + mock_rglob.side_effect = [list()] |
| 52 | + |
| 53 | + # raise OSError |
| 54 | + with self.assertRaises(OSError): |
| 55 | + _ = vsi.tools.gpu_check.find_cudart(search_dirs) |
| 56 | + |
| 57 | + |
| 58 | + @patch('vsi.tools.gpu_check.ctypes.cdll.LoadLibrary', side_effect=MockLibrary) |
| 59 | + def test_load_cudart_file(self, mock_LoadLibrary): |
| 60 | + '''Test load_cudart from file''' |
| 61 | + |
| 62 | + # call function & check return |
| 63 | + result = vsi.tools.gpu_check.load_cudart('/path/to/file') |
| 64 | + self.assertEqual(result._name, '/path/to/file') |
| 65 | + |
| 66 | + |
| 67 | + @patch.dict(os.environ, {"LD_LIBRARY_PATH": ""}) |
| 68 | + @patch('vsi.tools.gpu_check.find_cudart') |
| 69 | + @patch('vsi.tools.gpu_check.pathlib.Path.glob') |
| 70 | + def test_load_cudart_dirs(self, mock_glob, mock_find_cudart): |
| 71 | + '''Test load_cudart from ``/usr/local/cuda``''' |
| 72 | + |
| 73 | + # glob result for ['/usr/local', '/usr'] |
| 74 | + mock_glob.side_effect = [['/usr/local/cuda'], ['/usr/cuda']] |
| 75 | + |
| 76 | + # find_cudart result |
| 77 | + def _find_cudart(search_dirs): |
| 78 | + for search_dir in search_dirs: |
| 79 | + if '/usr/local/cuda' in str(search_dir): |
| 80 | + return MockLibrary('/usr/local/cuda/libcudart.so') |
| 81 | + raise None |
| 82 | + |
| 83 | + mock_find_cudart.side_effect = _find_cudart |
| 84 | + |
| 85 | + # call function & check return |
| 86 | + result = vsi.tools.gpu_check.load_cudart() |
| 87 | + self.assertEqual(result._name, '/usr/local/cuda/libcudart.so') |
| 88 | + |
| 89 | + |
| 90 | + @patch.dict(os.environ, {"LD_LIBRARY_PATH": "/foo:/bar:/path/to"}) |
| 91 | + @patch('vsi.tools.gpu_check.find_cudart') |
| 92 | + @patch('vsi.tools.gpu_check.pathlib.Path.glob') |
| 93 | + def test_load_cudart_ld(self, mock_glob, mock_find_cudart): |
| 94 | + '''Test load_cudart from ``/usr/local/cuda``''' |
| 95 | + |
| 96 | + # glob result for ['/usr/local', '/usr'] |
| 97 | + mock_glob.side_effect = [['/usr/local/cuda'], ['/usr/cuda']] |
| 98 | + |
| 99 | + # find_cudart result |
| 100 | + def _find_cudart(search_dirs): |
| 101 | + for search_dir in search_dirs: |
| 102 | + if '/path/to' in str(search_dir): |
| 103 | + return MockLibrary('/path/to/libcudart.so') |
| 104 | + raise None |
| 105 | + |
| 106 | + mock_find_cudart.side_effect = _find_cudart |
| 107 | + |
| 108 | + # call function & check return |
| 109 | + result = vsi.tools.gpu_check.load_cudart() |
| 110 | + self.assertEqual(result._name, '/path/to/libcudart.so') |
| 111 | + |
| 112 | + |
| 113 | + @patch('vsi.tools.gpu_check.load_cudart', return_value=MockLibrary('libcudart.so')) |
| 114 | + def test_load_nvidia_uvm(self, mock_load_cudart): |
| 115 | + '''Test load_nvidia_uvm''' |
| 116 | + |
| 117 | + # call function & check logs |
| 118 | + with self.assertLogs(level='DEBUG') as logs: |
| 119 | + vsi.tools.gpu_check.load_nvidia_uvm() |
| 120 | + |
| 121 | + logs_expected = [ |
| 122 | + "found libcudart.so : libcudart.so", |
| 123 | + "cudaGetDeviceCount : exit_code='cudaGetDeviceCount', gpu_count=-1", |
| 124 | + ] |
| 125 | + self._check_logs(logs, logs_expected) |
| 126 | + |
| 127 | + |
| 128 | + @patch('vsi.tools.gpu_check.glob.glob', return_value=None) |
| 129 | + def test_gpu_check_no_gpu(self, mock_glob): |
| 130 | + '''Test gpu_check with nothing in /dev/nvidia[0-9]''' |
| 131 | + |
| 132 | + with self.assertLogs(level='DEBUG') as logs: |
| 133 | + vsi.tools.gpu_check.gpu_check() |
| 134 | + |
| 135 | + logs_expected = ["Skip gpu_check : /dev/nvidia[0-9] missing"] |
| 136 | + self._check_logs(logs, logs_expected) |
| 137 | + |
| 138 | + |
| 139 | + @patch('vsi.tools.gpu_check.glob.glob', return_value=True) |
| 140 | + @patch('vsi.tools.gpu_check.os.path.exists', return_value=True) |
| 141 | + def test_gpu_check_uvm_exists(self, mock_exists, mock_glob): |
| 142 | + '''Test gpu_check with existing /dev/nvidia-uvm''' |
| 143 | + |
| 144 | + with self.assertLogs(level='DEBUG') as logs: |
| 145 | + vsi.tools.gpu_check.gpu_check() |
| 146 | + |
| 147 | + logs_expected = ["Skip gpu_check : /dev/nvidia-uvm already loaded"] |
| 148 | + self._check_logs(logs, logs_expected) |
| 149 | + |
| 150 | + |
| 151 | + @patch('vsi.tools.gpu_check.glob.glob', return_value=True) |
| 152 | + @patch('vsi.tools.gpu_check.os.path.exists') |
| 153 | + @patch('vsi.tools.gpu_check.load_nvidia_uvm', return_value=MockLibrary('libcudart.so')) |
| 154 | + def test_gpu_check_uvm_success(self, mock_load_nvidia_uvm, mock_exists, mock_glob): |
| 155 | + '''Test gpu_check with load_nvidia_uvm success''' |
| 156 | + |
| 157 | + # /dev/nvidia-uvm check fails then succeeds |
| 158 | + mock_exists.side_effect = [False, True] |
| 159 | + |
| 160 | + # run test |
| 161 | + with self.assertLogs(level='DEBUG') as logs: |
| 162 | + vsi.tools.gpu_check.gpu_check() |
| 163 | + logs_expected = ["/dev/nvidia-uvm has been successfully loaded"] |
| 164 | + self._check_logs(logs, logs_expected) |
| 165 | + |
0 commit comments