Skip to content

Commit 1f77e58

Browse files
author
benoit-cty
committed
fix test
1 parent 893883d commit 1f77e58

1 file changed

Lines changed: 64 additions & 12 deletions

File tree

tests/test_gpu.py

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ def setup_method(self):
5656
):
5757
sys.modules.pop(module_name, None)
5858

59+
import codecarbon.core
60+
61+
for attr in ["gpu", "gpu_nvidia", "gpu_amd", "gpu_device"]:
62+
if hasattr(codecarbon.core, attr):
63+
delattr(codecarbon.core, attr)
64+
65+
import codecarbon.core
66+
67+
for attr in ["gpu", "gpu_nvidia", "gpu_amd", "gpu_device"]:
68+
if hasattr(codecarbon.core, attr):
69+
delattr(codecarbon.core, attr)
70+
5971
# Setup the state, strings are returned as bytes
6072
self.DETAILS = {
6173
"handle_0": {
@@ -189,14 +201,18 @@ def test_gpu_no_power_limit(self):
189201
def raiseException(handle):
190202
raise Exception("Some bad exception")
191203

192-
pynvml.nvmlDeviceGetEnforcedPowerLimit = raiseException
193-
alldevices = AllGPUDevices()
204+
original_limit = pynvml.nvmlDeviceGetEnforcedPowerLimit
205+
try:
206+
pynvml.nvmlDeviceGetEnforcedPowerLimit = raiseException
207+
alldevices = AllGPUDevices()
194208

195-
expected_power_limit = deepcopy(self.expected)
196-
expected_power_limit[0]["power_limit"] = None
197-
expected_power_limit[1]["power_limit"] = None
209+
expected_power_limit = deepcopy(self.expected)
210+
expected_power_limit[0]["power_limit"] = None
211+
expected_power_limit[1]["power_limit"] = None
198212

199-
assert alldevices.get_gpu_details() == expected_power_limit
213+
assert alldevices.get_gpu_details() == expected_power_limit
214+
finally:
215+
pynvml.nvmlDeviceGetEnforcedPowerLimit = original_limit
200216

201217
def test_gpu_not_ready(self):
202218
import pynvml
@@ -206,14 +222,18 @@ def test_gpu_not_ready(self):
206222
def raise_exception(handle):
207223
raise pynvml.NVMLError("System is not in ready state")
208224

209-
pynvml.nvmlDeviceGetTotalEnergyConsumption = raise_exception
210-
alldevices = AllGPUDevices()
225+
original_energy = pynvml.nvmlDeviceGetTotalEnergyConsumption
226+
try:
227+
pynvml.nvmlDeviceGetTotalEnergyConsumption = raise_exception
228+
alldevices = AllGPUDevices()
211229

212-
expected = deepcopy(self.expected)
213-
expected[0]["total_energy_consumption"] = None
214-
expected[1]["total_energy_consumption"] = None
230+
expected = deepcopy(self.expected)
231+
expected[0]["total_energy_consumption"] = None
232+
expected[1]["total_energy_consumption"] = None
215233

216-
assert alldevices.get_gpu_details() == expected
234+
assert alldevices.get_gpu_details() == expected
235+
finally:
236+
pynvml.nvmlDeviceGetTotalEnergyConsumption = original_energy
217237

218238
def test_gpu_metadata_total_power(self):
219239
"""
@@ -374,6 +394,18 @@ def setup_method(self):
374394
):
375395
sys.modules.pop(module_name, None)
376396

397+
import codecarbon.core
398+
399+
for attr in ["gpu", "gpu_nvidia", "gpu_amd", "gpu_device"]:
400+
if hasattr(codecarbon.core, attr):
401+
delattr(codecarbon.core, attr)
402+
403+
import codecarbon.core
404+
405+
for attr in ["gpu", "gpu_nvidia", "gpu_amd", "gpu_device"]:
406+
if hasattr(codecarbon.core, attr):
407+
delattr(codecarbon.core, attr)
408+
377409
import pynvml
378410

379411
pynvml.INIT_MOCK.side_effect = pynvml.NVMLError("NVML Shared Library Not Found")
@@ -892,6 +924,16 @@ def _exec_gpu_module(self, import_func, check_output):
892924
"codecarbon.core.gpu_device",
893925
):
894926
sys.modules.pop(module_name, None)
927+
import codecarbon.core
928+
929+
for attr in ["gpu", "gpu_nvidia", "gpu_amd", "gpu_device"]:
930+
if hasattr(codecarbon.core, attr):
931+
delattr(codecarbon.core, attr)
932+
import codecarbon.core
933+
934+
for attr in ["gpu", "gpu_nvidia", "gpu_amd", "gpu_device"]:
935+
if hasattr(codecarbon.core, attr):
936+
delattr(codecarbon.core, attr)
895937
with (
896938
mock.patch("subprocess.check_output", side_effect=check_output),
897939
mock.patch.object(builtins, "__import__", new=import_func),
@@ -1035,6 +1077,16 @@ def test_is_nvidia_system_fail(self, mock_subprocess):
10351077

10361078

10371079
class TestGpuTracking:
1080+
def setup_method(self):
1081+
for module_name in list(sys.modules.keys()):
1082+
if module_name.startswith("codecarbon.core.gpu"):
1083+
sys.modules.pop(module_name, None)
1084+
import codecarbon.core
1085+
1086+
for attr in ["gpu", "gpu_nvidia", "gpu_amd", "gpu_device"]:
1087+
if hasattr(codecarbon.core, attr):
1088+
delattr(codecarbon.core, attr)
1089+
10381090
@mock.patch("codecarbon.core.gpu.is_rocm_system", return_value=True)
10391091
@mock.patch("codecarbon.core.gpu.is_nvidia_system", return_value=False)
10401092
@mock.patch("codecarbon.core.gpu_amd.subprocess.check_output")

0 commit comments

Comments
 (0)