Created
November 22, 2020 19:48
-
-
Save 2torus/7b8ff845fdc97a88eeb6f65e24a0292d to your computer and use it in GitHub Desktop.
nvidia-smi gpu info as a dict in python https://gist.github.com/takuseno/2958caf1cb5e74314a9b5971999182b2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gpu info as a dict | |
# from https://gist.github.com/takuseno/2958caf1cb5e74314a9b5971999182b2 | |
from subprocess import Popen, PIPE | |
from xml.etree.ElementTree import fromstring | |
def nvidia_smi_xml(): | |
p = Popen(['nvidia-smi', '-q', '-x'], stdout=PIPE) | |
outs, errors = p.communicate() | |
return fromstring(outs) | |
def xml_dict(el, tags): | |
return {node.tag: node.text for node in el if node.tag in tags} | |
def main(): | |
xml = nvidia_smi_xml() | |
num_gpus = int(xml.findtext('attached_gpus')) | |
results = [] | |
for gpu_id, gpu in enumerate(xml.iter('gpu')): | |
gpu_data = {} | |
name = gpu.findtext('product_name') | |
gpu_data['name'] = name | |
# get memory | |
memory_usage = gpu.find('fb_memory_usage') | |
gpu_data['memory'] = xml_dict(memory_usage, ['total', 'used', 'free']) | |
# get utilization | |
utilization = gpu.find('utilization') | |
gpu_data['utilization'] = xml_dict(utilization, ['gpu_util', 'memory_util']) | |
# processes | |
processes = gpu.find('processes').iter('process_info') | |
gpu_data['processes'] = [xml_dict(process, ['pid', 'process_name', 'used_memory']) for process in processes] | |
results.append(gpu_data) | |
return results | |
if __name__ == '__main__': | |
print(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment