1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
"""Copyright (C) 2016-2024 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cop-
ies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM-
PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNE-
CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os
from pathlib import Path as path
import csv
from decimal import Decimal
def get_csv_val(datastr, title, gpu=0):
"""
Parse the csv value for 'title' from 'datastr'. Ensures that the 'datastr' is for the 'gpu' passed.
:param: datastr: The data as output by amd-smi in csv format, see example:
gpu,total_vram,used_vram,free_vram,total_visible_vram,used_visible_vram,free_visible_vram,total_gtt,used_gtt,free_gtt
0,16368,905,15463,256,26,230,32119,755,31364
:param: title: The title in the datastr to parse for, example for above output: "total_vram", would get "16368" as output.
:param: gpu: The gpu# we are searching for, it will be validated that 'gpu' is the same as 'gpu' in the datastr, otherwise
this function will return "error"
"""
reader = csv.reader(datastr.split('\n'))
try:
header = next(reader)
data = next(reader)
if int(data[header.index('gpu')]) != gpu:
return "error"
return data[header.index(title)]
except:
return "error"
def _subprocess_helper(cmd, *args, **kwargs):
import subprocess
import tempfile
fout = tempfile.TemporaryFile(mode="w+")
ferr = tempfile.TemporaryFile(mode="w+")
cout = ""
success = False
try:
p = subprocess.Popen(cmd, stdout=fout, stderr=ferr, *args, **kwargs)
p.wait()
fout.seek(0)
cout = fout.read()
success = True
except subprocess.CalledProcessError:
pass
except FileNotFoundError:
pass
return success, cout
def get_smi_exec(cuda):
if cuda:
return "nvidia-smi"
else:
return "/opt/rocm/bin/amd-smi"
def getgfx(devicenum, cuda):
if cuda:
return "N/A"
else:
cmd = ["/opt/rocm/bin/rocm_agent_enumerator"]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
# Add 1 to devicenum since rocm-agent-enum always prints gfx000 first
return cout.splitlines()[devicenum+1]
# Get the hostname
def gethostname():
import socket
hostname = socket.gethostname()
return hostname
# Get the host cpu information
def getcpu():
cmd = ["lscpu"]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
cpulist = ""
searchstr = "Model name:"
for line in cout.split("\n"):
if line.startswith(searchstr):
cpulist += line[len(searchstr):].strip()
return cpulist
# Get the kernel version
def getkernel():
import subprocess
cmd = ["uname", "-r"]
import tempfile
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
return cout.strip()
# Get the host ram size
def getram():
import re
cmd = ["lshw", "-class", "memory"]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
searchstr = "size:"
for line in cout.split("\n"):
m = re.search(searchstr, line)
if not m == None:
return line.strip()[len(searchstr):].strip()
# Get the Linux distro information
def getdistro():
cmd = ["lsb_release", "-a"]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
searchstr = "Description:"
for line in cout.split("\n"):
if line.startswith(searchstr):
return line[len(searchstr):].strip()
# Get the version number for rocm
def getrocmversion():
if os.path.isfile("/opt/rocm/.info/version-utils"):
rocm_info = path("/opt/rocm/.info/version-utils").read_text()
elif os.path.isfile("/opt/rocm/.info/version"):
rocm_info = path("/opt/rocm/.info/version").read_text()
else:
return "N/A"
return rocm_info.strip()
# Get the vbios version for the specified device
def getvbios(devicenum, cuda):
cmd = [get_smi_exec(cuda), "static", "-g", str(devicenum), "-V", "--csv"]
if cuda:
cmd = [get_smi_exec(cuda), "--query-gpu=vbios_version", "--format=csv,noheader", "-i", str(devicenum)]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
if cuda:
return cout
return get_csv_val(cout, 'version', devicenum)
def getgpuid(devicenum, cuda):
import re
name = ""
cmd = [get_smi_exec(cuda), "static", "-g", str(devicenum), "-a", "--csv"]
if cuda:
cmd = [get_smi_exec(cuda), "--query-gpu=name", "--format=csv,noheader", "-i", str(devicenum)]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
if cuda:
return cout
return get_csv_val(cout, 'market_name', devicenum)
# Get the name of the device from lshw which has index devicenum
def getdeviceinfo(devicenum, cuda):
import re
cmd = ["lshw", "-C", "video"]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
searchstr = "-display"
indices = []
name = ""
for idx, line in enumerate(cout.split("\n")):
if re.search(searchstr, line) != None:
indices.append(idx)
for idx, line in enumerate(cout.split("\n")):
if idx >= indices[devicenum]:
searchstr = "product:"
if re.search(searchstr, line) != None:
pos = line.find(":")
name += line[pos+1:].strip()
name += " " + getgpuid(devicenum, cuda)
return name
# Get the vram for the specified device
def getvram(devicenum, cuda):
import re
cmd = [get_smi_exec(cuda), "static", "-g", str(devicenum), "-v", "--csv"]
if cuda:
cmd = [get_smi_exec(cuda), "--query-gpu=memory.total", "--format=csv,noheader", "-i", str(devicenum)]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
if cuda:
return cout
val = get_csv_val(cout, 'size', devicenum)
if(val == 'error'):
val = get_csv_val(cout, 'vram_size_mb', devicenum) # ROCm 6.0 output
return val + " MB"
# Get the memory clock for the specified device
def getmclk(devicenum, cuda):
import re
cmd = [get_smi_exec(cuda), "metric", "-g", str(devicenum), "-c", "--csv"]
if cuda:
cmd = [get_smi_exec(cuda), "--query-gpu=clocks.mem", "--format=csv,noheader", "-i", str(devicenum)]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
if cuda:
return cout
val = get_csv_val(cout, 'MEM_clk', devicenum)
if val == 'error':
val = get_csv_val(cout, 'MEM_cur_clk', devicenum) # ROCm 6.0 output
# Get the system clock for the specified device
def getsclk(devicenum, cuda):
import re
cmd = [get_smi_exec(cuda), "metric", "-g", str(devicenum), "-c", "--csv"]
if cuda:
cmd = [get_smi_exec(cuda), "--query-gpu=clocks.sm", "--format=csv,noheader", "-i", str(devicenum)]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
if cuda:
return cout
val = get_csv_val(cout, 'clk', devicenum)
if val == 'error':
val = get_csv_val(cout, 'cur_clk', devicenum) # ROCm 6.0 output
return val
def listdevices(cuda):
if cuda:
cmd = [get_smi_exec(cuda), "--query-gpu=count", "--format=csv,noheader", "-i", '0']
success, cout = _subprocess_helper(cmd)
if not success:
return []
return list(range(0, int(cout)))
else:
cmd = [get_smi_exec(cuda), "list", "--csv"]
success, cout = _subprocess_helper(cmd)
return list(range(0, cout.count('\n') - 2))
def getbus(devicenum, cuda):
if cuda:
cmd = [get_smi_exec(cuda), "--query-gpu=pci.bus_id", "--format=csv,noheader", "-i", str(devicenum)]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
return cout
else:
cmd = [get_smi_exec(cuda), "static", "-g", str(devicenum), "-b", "--csv"]
success, cout = _subprocess_helper(cmd)
return get_csv_val(cout, 'bdf', devicenum)
def getprofile(devicenum, cuda):
import re
if cuda:
return "N/A"
else:
cmd = [get_smi_exec(cuda), "metric", "-g", str(devicenum), "-p", "--csv"]
success, cout = _subprocess_helper(cmd)
return get_csv_val(cout, 'power_management', devicenum)
def getfanspeedpercent(devicenum, cuda):
if cuda:
cmd = [get_smi_exec(cuda), "--query-gpu=fan.speed", "--format=csv,noheader", "-i", str(devicenum)]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
return str(cout)
else:
cmd = [get_smi_exec(cuda), "metric", "-g", str(devicenum), "-f", "--csv"]
success, cout = _subprocess_helper(cmd)
return get_csv_val(cout, 'usage', devicenum)
def validclocknames(cuda):
if cuda:
return ["graphics", "sm", "memory", "video"]
else:
host_rocm_ver = Decimal('.'.join(getrocmversion().split('.')[0:2])) # get host's rocm major.minor version
rocm_6_1_ver = Decimal('6.1')
if rocm_6_1_ver.compare(host_rocm_ver) == 1:
return ["cur_clk", "MEM_cur_clk", "VCLK0_cur_clk"] # For versions below ROCm 6.1
else:
return ["clk", "MEM_clk", "VCLK0_clk"]
def getcurrentclockfreq(devicenum, clock, cuda):
import re
if cuda:
cmd = [get_smi_exec(cuda), "--query-gpu=clocks.current." + clock, "--format=csv,noheader", "-i", str(devicenum)]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
return cout
else:
cmd = [get_smi_exec(cuda), "metric", "-g", str(devicenum), "-c", "--csv"]
success, cout = _subprocess_helper(cmd)
return get_csv_val(cout, clock, devicenum)
def validmemtypes(cuda):
if cuda:
return ["vram"]
else:
return ["vram", "vram", "gtt"]
def getmeminfo(devicenum, mem_type, cuda):
if cuda:
if mem_type == 'vram':
cmd = [get_smi_exec(cuda), "--query-gpu=memory.total", "--format=csv,noheader", "-i", str(devicenum)]
success, cout_total = _subprocess_helper(cmd)
if not success:
return "N/A"
cmd = [get_smi_exec(cuda), "--query-gpu=memory.used", "--format=csv,noheader", "-i", str(devicenum)]
success, cout_used = _subprocess_helper(cmd)
if not success:
return "N/A"
return cout_used, cout_total
else:
return "N/A"
else:
cmd = [get_smi_exec(cuda), "metric", "-g", str(devicenum), "-m", "--csv"]
success, cout = _subprocess_helper(cmd)
return get_csv_val(cout, "used_" + mem_type, devicenum), get_csv_val(cout, "total_" + mem_type, devicenum)
def validversioncomponents(cuda):
# currently only driver according to /opt/rocm/bin/rocm_smi.py
# driver corresponds to 0 in /opt/rocm/bin/rocm_smi.py
if cuda:
return ['driver']
else:
# currently only driver according to /opt/rocm/bin/rocm_smi.py
return ['driver']
def getversion(devicenum, component, cuda):
if cuda:
if component == 'driver':
cmd = [get_smi_exec(cuda), "--query-gpu=driver_version", "--format=csv,noheader", "-i", str(devicenum)]
success, cout = _subprocess_helper(cmd)
if not success:
return "N/A"
return cout
else:
return "N/A"
elif component == 'driver':
cmd = [get_smi_exec(cuda), "static", "-g", str(devicenum), "-d", "--csv"]
success, cout = _subprocess_helper(cmd)
val = get_csv_val(cout, 'version', devicenum)
if val == 'error':
val = get_csv_val(cout, 'driver_version', devicenum) # ROCm 6.0 output
return val
else:
return "N/A"
|