code-HFD_lzk / tests /test_third_party_gpu_telemetry.py
Tinghong-Ye22's picture
Add files using upload-large-folder tool
db172f6 verified
Raw History Blame Contribute Delete
2.25 kB
import importlib.util
import sys
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
_spec = importlib.util.spec_from_file_location(
"hfd_gpu_telemetry", Path(__file__).resolve().parents[1]
/ "scripts/train/third_party_gpu_telemetry.py")
telemetry = importlib.util.module_from_spec(_spec)
sys.modules[_spec.name] = telemetry
_spec.loader.exec_module(telemetry)
@pytest.mark.parametrize("ticks,argv,expected", [
(42, [str(telemetry.PROJECT / "scripts/train/third_party_run.py")], True),
(43, [str(telemetry.PROJECT / "scripts/train/third_party_run.py")], False),
(42, ["unrelated.py"], False),
])
def test_launcher_identity(monkeypatch, ticks, argv, expected):
monkeypatch.setattr(telemetry, "identity", lambda pid: SimpleNamespace(
start_ticks=ticks, argv=argv))
assert telemetry.launcher_alive({"launcher_pid": 123, "launcher_start_ticks": 42}) is expected
def test_dead_launcher(monkeypatch):
monkeypatch.setattr(telemetry, "identity", lambda pid: None)
assert not telemetry.launcher_alive({"launcher_pid": 123, "launcher_start_ticks": 42})
def test_read_only_gpu_three(monkeypatch):
run = Mock(return_value=SimpleNamespace(stdout="54, 43865, 390.3\n"))
monkeypatch.setattr(telemetry.subprocess, "run", run)
assert telemetry.sample_gpu() == {"gpu_utilization_percent": 54.0,
"memory_used_mib": 43865.0, "power_watts": 390.3}
command = run.call_args.args[0]
assert command[:3] == ["nvidia-smi", "-i", "3"]
assert command[3].startswith("--query-gpu=")
def test_read_only_selected_gpu_zero(monkeypatch):
run = Mock(return_value=SimpleNamespace(stdout="54, 43865, 390.3\n"))
monkeypatch.setattr(telemetry.subprocess, "run", run)
telemetry.sample_gpu(0)
assert run.call_args.args[0][:3] == ["nvidia-smi", "-i", "0"]
with pytest.raises(ValueError):
telemetry.sample_gpu(1)
@pytest.mark.parametrize("stdout", ["54, 43865\n", "54, 43865, N/A\n", ""])
def test_bad_measurement_rejected(monkeypatch, stdout):
monkeypatch.setattr(telemetry.subprocess, "run", Mock(
return_value=SimpleNamespace(stdout=stdout)))
with pytest.raises(ValueError):
telemetry.sample_gpu()