Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions serve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3435,6 +3435,12 @@ def gpu_speed_scores(indices: list[int]) -> dict[int, float] | None:
return got if all(i in got and got[i] > 0 for i in indices) else None



def _read_lines(path: str):
"""The lines of `path`, with the file closed when the caller is done (the HIP properties probe reads two)."""
with open(path) as fh:
return fh.readlines()

def hip_speed_scores(indices: list[int], root: str = "/sys/class/kfd/kfd/topology/nodes") -> dict[int, float] | None:
"""AMD on Linux: SIMDs x max engine clock of each card, from the KFD topology (the order setup and the engine use
for "gpu" there: the GPU nodes in node order). None when the files are not there (Windows, no ROCm driver)."""
Expand All @@ -3445,7 +3451,7 @@ def hip_speed_scores(indices: list[int], root: str = "/sys/class/kfd/kfd/topolog
cards = []
for n in nodes:
try:
props = dict(line.split() for line in open(os.path.join(root, str(n), "properties")) if len(line.split()) == 2)
props = dict(line.split() for line in _read_lines(os.path.join(root, str(n), "properties")) if len(line.split()) == 2)
simd, clk = int(props.get("simd_count", 0)), int(props.get("max_engine_clk_fcompute", 0))
except (OSError, ValueError):
continue
Expand Down Expand Up @@ -3482,7 +3488,7 @@ def hip_gpu_archs(root: str = "/sys/class/kfd/kfd/topology/nodes") -> dict[int,
out: dict[int, str] = {}
for n in nodes:
try:
props = dict(line.split() for line in open(os.path.join(root, str(n), "properties")) if len(line.split()) == 2)
props = dict(line.split() for line in _read_lines(os.path.join(root, str(n), "properties")) if len(line.split()) == 2)
ver, simd = int(props.get("gfx_target_version", 0)), int(props.get("simd_count", 0))
except (OSError, ValueError):
continue
Expand Down
16 changes: 10 additions & 6 deletions serve/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ def _cpu_topology(root: str = "/sys/devices/system"):
packages = set()
for f in glob.glob(os.path.join(root, "cpu", "cpu[0-9]*", "topology", "physical_package_id")):
try:
packages.add(open(f, encoding="utf-8").read().strip())
with open(f, encoding="utf-8") as fh:
packages.add(fh.read().strip())
except OSError:
continue
nodes = len(glob.glob(os.path.join(root, "node", "node[0-9]*")))
Expand All @@ -473,9 +474,10 @@ def _cpu_name():
except OSError:
pass
elif os.path.exists("/proc/cpuinfo"):
for line in open("/proc/cpuinfo", encoding="utf-8", errors="replace"):
if line.startswith("model name"):
return line.split(":", 1)[1].strip()
with open("/proc/cpuinfo", encoding="utf-8", errors="replace") as fh:
for line in fh:
if line.startswith("model name"):
return line.split(":", 1)[1].strip()
return platform.processor() or None


Expand All @@ -492,7 +494,8 @@ def _times(self):
return idle.value, kern.value + user.value # kernel time includes idle
return None
try:
f = [int(x) for x in open("/proc/stat").readline().split()[1:]]
with open("/proc/stat") as fh:
f = [int(x) for x in fh.readline().split()[1:]]
return f[3] + f[4], sum(f)
except (OSError, ValueError):
return None
Expand All @@ -519,7 +522,8 @@ class MS(ctypes.Structure):
return m.ullTotalPhys - m.ullAvailPhys, m.ullTotalPhys
return None, None
try:
info = dict(line.split(":", 1) for line in open("/proc/meminfo"))
with open("/proc/meminfo") as fh:
info = dict(line.split(":", 1) for line in fh)
total = int(info["MemTotal"].split()[0]) * 1024
avail = int(info["MemAvailable"].split()[0]) * 1024
return total - avail, total
Expand Down
Loading