-
Notifications
You must be signed in to change notification settings - Fork 1
/
slurm_gpustat.py
841 lines (725 loc) · 31.4 KB
/
slurm_gpustat.py
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
"""Credit to https://github.com/albanie/slurm_gpustat with minor modifications for web app.
A simple tool for summarising GPU statistics on a slurm cluster.
The tool can be used in two ways:
1. To simply query the current usage of GPUs on the cluster.
2. To launch a daemon which will log usage over time. This can then later be queried
to provide simple usage statistics.
"""
import os
import re
import ast
import sys
import time
import atexit
import signal
import argparse
import functools
import subprocess
from typing import Optional
from pathlib import Path
from datetime import datetime
from collections import defaultdict
import numpy as np
import colored
import seaborn as sns
import humanize
import humanfriendly as hf
from beartype import beartype
from django.utils.functional import lazy
# SLURM states which indicate that the node is not available for submitting jobs
INACCESSIBLE = {"drain*", "down*", "drng", "drain", "down"}
INTERACTIVE_CMDS = {"bash", "zsh", "sh"}
class Daemon:
"""A Generic linux daemon base class for python 3.x.
This code is a Python3 port of Sander Marechal's Daemon module:
http://web.archive.org/web/20131017130434/http://www.jejik.com/articles/
2007/02/a_simple_unix_linux_daemon_in_python/
It's a little difficult to credit the author of the Python3 port, since the code was
published anonymously. The original can be found here:
http://web.archive.org/web/20131101191715/http://www.jejik.com/files/examples/
daemon3x.py
"""
def __init__(self, pidfile):
self.pidfile = pidfile
def daemonize(self):
"""Deamonize class. UNIX double fork mechanism."""
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError as err:
sys.stderr.write('fork #1 failed: {0}\n'.format(err))
sys.exit(1)
# decouple from parent environment
os.chdir('/')
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError as err:
sys.stderr.write('fork #2 failed: {0}\n'.format(err))
sys.exit(1)
# redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = open(os.devnull, 'r')
so = open(os.devnull, 'a+')
se = open(os.devnull, 'a+')
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
# write pidfile
atexit.register(self.delpid)
signal.signal(signal.SIGTERM, lambda signum, stack_frame: exit())
pid = str(os.getpid())
with open(self.pidfile, 'w+') as f:
f.write(pid + '\n')
def delpid(self):
os.remove(self.pidfile)
def start(self):
"""Start the daemon."""
# Check for a pidfile to see if the daemon already runs
try:
with open(self.pidfile, 'r') as pf:
pid = int(pf.read().strip())
except IOError:
pid = None
if pid:
message = "pidfile {0} already exists. Is the daemon already running?\n"
sys.stderr.write(message.format(self.pidfile))
sys.exit(1)
# Start the daemon
self.daemonize()
self.run()
def stop(self):
"""Stop the daemon."""
# Get the pid from the pidfile
try:
with open(self.pidfile, 'r') as pf:
pid = int(pf.read().strip())
except IOError:
pid = None
if not pid:
message = "pidfile {0} does not exist. Daemon not running?\n"
sys.stderr.write(message.format(self.pidfile))
return # not an error in a restart
# Try killing the daemon process
try:
while 1:
os.kill(pid, signal.SIGTERM)
time.sleep(0.1)
except OSError as err:
e = str(err.args)
if e.find("No such process") > 0:
if os.path.exists(self.pidfile):
os.remove(self.pidfile)
else:
print(str(err.args))
sys.exit(1)
def restart(self):
"""Restart the daemon."""
self.stop()
self.start()
def run(self):
"""You should override this method when you subclass Daemon.
It will be called after the process has been daemonized by
start() or restart()."""
raise NotImplementedError("Must override this class")
class GPUStatDaemon(Daemon):
"""A lightweight daemon which intermittently logs gpu usage to a text file.
"""
timestamp_format = "%Y-%m-%d_%H:%M:%S"
def __init__(self, pidfile, log_path, log_interval):
"""Create the daemon.
Args:
pidfile (str): the location of the daemon pid file.
log_path (str): the location where the historical log will be stored.
log_interval (int): the time interval (in seconds) at which gpu usage will
be stored to the log.
"""
Path(pidfile).parent.mkdir(exist_ok=True, parents=True)
super().__init__(pidfile=pidfile)
Path(log_path).parent.mkdir(exist_ok=True, parents=True)
self.log_interval = log_interval
self.log_path = log_path
def serialize_usage(self, usage):
"""Convert data structure into an appropriate string for serialization.
Args:
usage (a dict-like structure): a data-structure which has the form of a
dictionary, but may contain variants with length string representations
(e.g. defaultdict, OrderedDict etc.)
Returns:
(str): a string representation of the usage data strcture.
"""
for user, gpu_dict in usage.items():
for key, subdict in gpu_dict.items():
usage[user][key] = dict(subdict)
usage = dict(usage)
return usage.__repr__()
@staticmethod
def deserialize_usage(log_path):
"""Parse the `usage` data structure by reading in the contents of the text-based
log file and deserializing.
Args:
log_path (str): the location of the log file.
Returns:
(list[dict]): a list of dicts, where each dict contains the time stamp
associated with a set of usage statistics, together with the statistics
themselves.
"""
if not Path(log_path).exists():
raise ValueError("No historical log found. Did you start the daemon?")
with open(log_path, "r") as f:
rows = f.read().splitlines()
data = []
for row in rows:
ts, usage = row.split(maxsplit=1)
dt = datetime.strptime(ts, GPUStatDaemon.timestamp_format)
usage = ast.literal_eval(usage)
data.append({"timestamp": dt, "usage": usage})
return data
def run(self):
"""Run the daemon - will intermittently log gpu usage to disk.
"""
while True:
resources = parse_all_gpus()
usage = gpu_usage(resources)
log_row = self.serialize_usage(usage)
timestamp = datetime.now().strftime(GPUStatDaemon.timestamp_format)
with open(self.log_path, "a") as f:
f.write(f"{timestamp} {log_row}\n")
time.sleep(self.log_interval)
def historical_summary(data):
"""Print a short summary of the historical gpu usage logged by the daemon.
Args:
data (list): the data structure deserialized from the daemon log file (this is
the output of the GPUStatDaemon.deserialize_usage() function.)
"""
first_ts, last_ts = data[0]["timestamp"], data[-1]["timestamp"]
print(f"Historical data contains {len(data)} samples ({first_ts} to {last_ts})")
latest_usage = data[-1]["usage"]
users, gpu_types = set(), set()
for user, resources in latest_usage.items():
users.add(user)
gpu_types.update(set(resources.keys()))
history = {}
for row in data:
for user, subdict in row["usage"].items():
if user not in history:
history[user] = {gpu_type: [] for gpu_type in gpu_types}
type_counts = {key: sum(val.values()) for key, val in subdict.items()}
for gpu_type in gpu_types:
history[user][gpu_type].append(type_counts.get(gpu_type, 0))
for user, subdict in history.items():
print(f"GPU usage for {user}:")
total = 0
for gpu_type, counts in subdict.items():
counts = np.array(counts)
if counts.sum() == 0:
continue
print(f"{gpu_type:5s} > avg: {int(counts.mean())}, max: {np.max(counts)}")
total += counts.mean()
print(f"total > avg: {int(total)}\n")
def split_node_str(node_str):
"""Split SLURM node specifications into node_specs. Here a node_spec defines a range
of nodes that share the same naming scheme (and are grouped together using square
brackets). E.g. 'node[1-3,4,6-9]' represents a single node_spec.
Examples:
A `node_str` of the form 'node[001-003]' will be returned as a single element
list: ['node[001-003]']
A `node_str` of the form 'node[001-002],node004' will be split into
['node[001-002]', 'node004']
Args:
node_str (str): a SLURM-formatted list of nodes
Returns:
(list[str]): SLURM node specs.
"""
node_str = node_str.strip()
breakpoints, stack = [0], []
for ii, char in enumerate(node_str):
if char == "[":
stack.append(char)
elif char == "]":
stack.pop()
elif not stack and char == ",":
breakpoints.append(ii + 1)
end = len(node_str) + 1
return [node_str[i: j - 1] for i, j in zip(breakpoints, breakpoints[1:] + [end])]
def parse_node_names(node_str):
"""Parse the node list produced by the SLURM tools into separate node names.
Examples:
A slurm `node_str` of the form 'node[001-003]' will be split into a list of the
form ['node001', 'node002', 'node003'].
A `node_str` of the form 'node[001-002],node004' will be split into
['node001', 'node002', 'node004']
Args:
node_str (str): a SLURM-formatted list of nodes
Returns:
(list[str]): a list of separate node names.
"""
names = []
node_specs = split_node_str(node_str)
for node_spec in node_specs:
if "[" not in node_spec:
names.append(node_spec)
else:
head, tail = node_spec.index("["), node_spec.index("]")
prefix = node_spec[:head]
subspecs = node_spec[head + 1:tail].split(",")
for subspec in subspecs:
if "-" not in subspec:
subnames = [f"{prefix}{subspec}"]
else:
start, end = subspec.split("-")
num_digits = len(start)
subnames = [f"{prefix}{str(x).zfill(num_digits)}"
for x in range(int(start), int(end) + 1)]
names.extend(subnames)
return names
def parse_cmd(cmd, split=True):
"""Parse the output of a shell command...
and if split set to true: split into a list of strings, one per line of output.
Args:
cmd (str): the shell command to be executed.
split (bool): whether to split the output per line
Returns:
(list[str]): the strings from each output line.
"""
output = subprocess.check_output(cmd, shell=True).decode("utf-8")
if split:
output = [x for x in output.split("\n") if x]
return output
@beartype
def node_states(partition: Optional[str] = None) -> dict:
"""Query SLURM for the state of each managed node.
Args:
partition: the partition/queue (or multiple, comma separated) of interest.
By default None, which queries all available partitions.
Returns:
a mapping between node names and SLURM states.
"""
cmd = "sinfo --noheader"
if partition:
cmd += f" --partition={partition}"
rows = parse_cmd(cmd)
states = {}
for row in rows:
tokens = row.split()
state, names = tokens[4], tokens[5]
node_names = parse_node_names(names)
states.update({name: state for name in node_names})
return states
@beartype
def get_gpu_partitions(keywords=['gpu', 'ddp']) -> list:
"""Query SLURM for the supported partitions.
Args:
keywords: the keywords to indicate gpu partitions (comma separated) of interest.
Returns:
a list of requested SLURM partitions.
"""
cmd = "sinfo --noheader"
rows = parse_cmd(cmd)
partitions = set()
for row in rows:
par = row.split()[0]
if any([k in par for k in keywords]):
partitions.add(par)
return sorted(list(partitions))
@functools.lru_cache(maxsize=64, typed=True)
def occupancy_stats_for_node(node: str) -> dict:
"""Query SLURM for the occupancy of a given node.
Args:
(node): the name of the node to query
Returns:
a mapping between node names and occupancy stats.
"""
cmd = f"scontrol show node {node}"
rows = [x.strip() for x in parse_cmd(cmd)]
keys = ("AllocTRES", "CfgTRES")
metrics = {}
for row in rows:
for key in keys:
if row.startswith(key):
row = row.replace(f"{key}=", "")
tokens = row.split(",")
if tokens == [""]:
# SLURM sometimes omits information, so we alert the user to its
# its exclusion and report nothing for this node
print(f"Missing information for {node}: {key}, skipping....")
metrics[key] = {}
else:
metrics[key] = {x.split("=")[0]: x.split("=")[1] for x in tokens}
occupancy = {}
for metric, alloc_val in metrics["AllocTRES"].items():
cfg_val = metrics["CfgTRES"][metric]
if metric == "mem":
# SLURM appears to sometimes misformat large numbers, producing summary strings
# like 68G/257669M, rather than 68G/258G. The humanfriendly library provides
# a more reliable number parser, and the humanize library provides a nice
# formatter.
alloc_val = format_size(hf.parse_size(alloc_val,binary=True))
cfg_val = format_size(hf.parse_size(cfg_val,binary=True))
occupancy[metric] = f"{alloc_val}/{cfg_val}"
return occupancy
def format_size(size_in_bytes):
size_in_gb = size_in_bytes / (1024 ** 3)
if size_in_gb < 1024:
return f"{int(size_in_gb)} G"
else:
size_in_tb = size_in_gb / 1024
formatted_tb = f"{size_in_tb:.1f}".rstrip('0').rstrip('.')
return f"{formatted_tb} T"
def lru_cache_time(seconds, maxsize=None):
"""
Adds time aware caching to lru_cache.
https://stackoverflow.com/questions/31771286/python-in-memory-cache-with-time-to-live
"""
def wrapper(func):
# Lazy function that makes sure the lru_cache() invalidate after X secs
ttl_hash = lazy(lambda: round(time.time() / seconds), int)()
@functools.lru_cache(maxsize)
def time_aware(__ttl, *args, **kwargs):
"""
Main wrapper, note that the first argument ttl is not passed down.
This is because no function should bother to know this that
this is here.
"""
def wrapping(*args, **kwargs):
return func(*args, **kwargs)
return wrapping(*args, **kwargs)
return functools.update_wrapper(functools.partial(time_aware, ttl_hash), func)
return wrapper
@lru_cache_time(seconds=10)
def avail_stats_for_node(node: str) -> dict:
"""Query SLURM for the availability of a given node.
Args:
(node): the name of the node to query
Returns:
a mapping between node names and availability stats.
"""
cmd = f"scontrol show node {node}"
rows = [x.strip() for x in parse_cmd(cmd)]
keys = ("AllocTRES", "CfgTRES")
metrics = {}
for row in rows:
for key in keys:
if row.startswith(key):
row = row.replace(f"{key}=", "")
tokens = row.split(",")
if tokens == [""]:
# SLURM sometimes omits information, so we alert the user to its
# its exclusion and report nothing for this node
# print(f"Missing information for {node}: {key}, skipping....")
metrics[key] = {}
else:
metrics[key] = {x.split("=")[0]: x.split("=")[1] for x in tokens}
occupancy = {}
for metric, cfg_val in metrics["CfgTRES"].items():
try:
alloc_val = metrics["AllocTRES"][metric]
except:
alloc_val = '0'
if metric == "mem":
# SLURM appears to sometimes misformat large numbers, producing summary strings
# like 68G/257669M, rather than 68G/258G. The humanfriendly library provides
# a more reliable number parser, and the humanize library provides a nice
# formatter.
avail_val = format_size(hf.parse_size(cfg_val,binary=True) - hf.parse_size(alloc_val,binary=True))
cfg_val = format_size(hf.parse_size(cfg_val,binary=True))
occupancy[metric] = f"{avail_val} / {cfg_val}"
else:
occupancy[metric] = f"{hf.parse_size(cfg_val)-hf.parse_size(alloc_val)} / {hf.parse_size(cfg_val)}"
return occupancy
@beartype
def parse_all_gpus(partition: Optional[str] = None,
default_gpus: int = 4,
default_gpu_name: str = "NONAME_GPU") -> dict:
"""Query SLURM for the number and types of GPUs under management.
Args:
partition: the partition/queue (or multiple, comma separated) of interest.
By default None, which queries all available partitions.
default_gpus: The number of GPUs estimated for nodes that have incomplete SLURM
meta data.
default_gpu_name: The name of the GPU for nodes that have incomplete SLURM meta
data.
Returns:
a mapping between node names and a list of the GPUs that they have available.
"""
cmd = "sinfo -o '%1000N|%1000G' --noheader"
if partition:
cmd += f" --partition={partition}"
rows = parse_cmd(cmd)
resources = defaultdict(list)
# Debug the regular expression below at
# https://regex101.com/r/RHYM8Z/3
p = re.compile(r'gpu:(?:(\w*):)?(\d*)(?:\(\S*\))?\s*')
for row in rows:
node_str, resource_strs = row.split("|")
for resource_str in resource_strs.split(","):
if not resource_str.startswith("gpu"):
continue
match = p.search(resource_str)
gpu_type = match.group(1) if match.group(1) is not None else default_gpu_name
# if the number of GPUs is not specified, we assume it is `default_gpus`
gpu_count = int(match.group(2)) if match.group(2) != "" else default_gpus
node_names = parse_node_names(node_str)
for name in node_names:
resources[name].append({"type": gpu_type, "count": gpu_count})
return resources
@beartype
def resource_by_type(resources: dict) -> dict:
"""Determine the cluster capacity by gpu type
Args:
resources: a summary of the cluster resources, organised by node name.
Returns:
resources: a summary of the cluster resources, organised by gpu type
"""
by_type = defaultdict(list)
for node, specs in resources.items():
for spec in specs:
by_type[spec["type"]].append({"node": node, "count": spec["count"]})
return by_type
@beartype
def summary_by_type(resources: dict, tag: str):
"""Print out out a summary of cluster resources, organised by gpu type.
Args:
resources (dict): a summary of cluster resources, organised by node name.
tag (str): a term that will be included in the printed summary.
"""
by_type = resource_by_type(resources)
total = sum(x["count"] for sublist in by_type.values() for x in sublist)
agg_str = []
for key, val in sorted(by_type.items(), key=lambda x: sum(y["count"] for y in x[1])):
gpu_count = sum(x["count"] for x in val)
agg_str.append(f"{gpu_count} {key} gpus")
print(f"There are a total of {total} gpus [{tag}]")
print("\n".join(agg_str))
@beartype
def summary(mode: str, resources: dict = None, states: dict = None):
"""Generate a printed summary of the cluster resources.
Args:
mode (str): the kind of resources to query (must be one of 'accessible', 'up').
resources (dict :: None): a summary of cluster resources, organised by node name.
states (dict[str: str] :: None): a mapping between node names and SLURM states.
"""
if not resources:
resources = parse_all_gpus()
if not states:
states = node_states()
if mode == "accessible":
res = {key: val for key, val in resources.items()
if states.get(key, "down") not in INACCESSIBLE}
elif mode == "up":
res = resources
else:
raise ValueError(f"Unknown mode: {mode}")
summary_by_type(res, tag=mode)
@beartype
def gpu_usage(resources: dict, partition: Optional[str] = "gpu-a40,gpu-v100,gpu-a100-80,gpu-a100-40,gpu-a6000,interactive-rtx3090,interactive-rtx2080") -> dict:
"""Build a data structure of the cluster resource usage, organised by user.
Args:
resources (dict :: None): a summary of cluster resources, organised by node name.
Returns:
(dict): a summary of resources organised by user (and also by node name).
"""
version_cmd = "sinfo -V"
slurm_version = parse_cmd(version_cmd, split=False).split(" ")[1]
if slurm_version.startswith("17"):
resource_flag = "gres"
else:
resource_flag = "tres-per-node"
if int(slurm_version[0:2]) >= 21:
gpu_identifier = 'gres/gpu'
else:
gpu_identifier = 'gpu'
cmd = f"squeue -O {resource_flag}:100,nodelist:100,username:100,jobid:100 --noheader"
if partition:
cmd += f" --partition={partition}"
detailed_job_cmd = "scontrol show jobid -dd %s"
rows = parse_cmd(cmd)
usage = defaultdict(dict)
for row in rows:
tokens = row.split()
# ignore pending jobs
if len(tokens) < 4 or not tokens[0].startswith(gpu_identifier):
continue
gpu_count_str, node_str, user, jobid = tokens
gpu_count_tokens = gpu_count_str.split(":")
if not gpu_count_tokens[-1].isdigit():
gpu_count_tokens.append("1")
num_gpus = int(gpu_count_tokens[-1])
# get detailed job information, to check if using bash
detailed_output = parse_cmd(detailed_job_cmd % jobid, split=False)
is_bash = any([f'Command={x}\n' in detailed_output for x in INTERACTIVE_CMDS])
num_bash_gpus = num_gpus * is_bash
node_names = parse_node_names(node_str)
for node_name in node_names:
# If a node still has jobs running but is draining, it will not be present
# in the "available" resources, so we ignore it
if node_name not in resources:
continue
node_gpu_types = [x["type"] for x in resources[node_name]]
if (len(gpu_count_tokens) == 2) or (int(slurm_version[0:2]) >= 21):
gpu_type = None
elif len(gpu_count_tokens) == 3:
gpu_type = gpu_count_tokens[1]
if gpu_type is None:
if len(node_gpu_types) != 1:
gpu_type = sorted(
resources[node_name],
key=lambda k: k['count'],
reverse=True
)[0]['type']
msg = (f"cannot determine node gpu type for {user} on {node_name}"
f" (guessing {gpu_type})")
print(f"WARNING >>> {msg}")
else:
gpu_type = node_gpu_types[0]
if gpu_type in usage[user]:
usage[user][gpu_type][node_name]['n_gpu'] += num_gpus
usage[user][gpu_type][node_name]['bash_gpu'] += num_bash_gpus
else:
usage[user][gpu_type] = defaultdict(lambda: {'n_gpu': 0, 'bash_gpu': 0})
usage[user][gpu_type][node_name]['n_gpu'] += num_gpus
usage[user][gpu_type][node_name]['bash_gpu'] += num_bash_gpus
return usage
@beartype
def in_use(resources: dict = None, partition: Optional[str] = None):
"""Print a short summary of the resources that are currently used by each user.
Args:
resources: a summary of cluster resources, organised by node name.
"""
if not resources:
resources = parse_all_gpus()
usage = gpu_usage(resources, partition=partition)
aggregates = {}
for user, subdict in usage.items():
aggregates[user] = {}
aggregates[user]['n_gpu'] = {key: sum([x['n_gpu'] for x in val.values()])
for key, val in subdict.items()}
aggregates[user]['bash_gpu'] = {key: sum([x['bash_gpu'] for x in val.values()])
for key, val in subdict.items()}
print("Usage by user:")
for user, subdict in sorted(aggregates.items(),
key=lambda x: sum(x[1]['n_gpu'].values())):
total = (f"total: {str(sum(subdict['n_gpu'].values())):2s} "
f"(interactive: {str(sum(subdict['bash_gpu'].values())):2s})")
summary_str = ", ".join([f"{key}: {val}" for key, val in subdict['n_gpu'].items()])
print(f"{user:10s} [{total}] {summary_str}")
@beartype
def available(
resources: dict = None,
states: dict = None,
verbose: bool = False,
):
"""Print a short summary of resources available on the cluster.
Args:
resources: a summary of cluster resources, organised by node name.
states: a mapping between node names and SLURM states.
verbose: whether to output a more verbose summary of the cluster state.
NOTES: Some systems allow users to share GPUs. The logic below amounts to a
conservative estimate of how many GPUs are available. The algorithm is:
For each user that requests a GPU on a node, we assume that a new GPU is allocated
until all GPUs on the server are assigned. If more GPUs than this are listed as
allocated by squeue, we assume any further GPU usage occurs by sharing GPUs.
"""
if not resources:
resources = parse_all_gpus()
if not states:
states = node_states()
res = {key: val for key, val in resources.items()
if states.get(key, "down") not in INACCESSIBLE}
usage = gpu_usage(resources=res)
for subdict in usage.values():
for gpu_type, node_dicts in subdict.items():
for node_name, user_gpu_count in node_dicts.items():
resource_idx = [x["type"] for x in res[node_name]].index(gpu_type)
count = res[node_name][resource_idx]["count"]
count = max(count - user_gpu_count['n_gpu'], 0)
res[node_name][resource_idx]["count"] = count
by_type = resource_by_type(res)
total = sum(x["count"] for sublist in by_type.values() for x in sublist)
print(f"There are {total} gpus available:")
for key, counts_for_gpu_type in by_type.items():
gpu_count = sum(x["count"] for x in counts_for_gpu_type)
tail = ""
if verbose:
summary_strs = []
for x in counts_for_gpu_type:
node, count = x["node"], x["count"]
if count:
occupancy = occupancy_stats_for_node(node)
users = [user for user in usage if node in usage[user].get(key, [])]
details = [f"{key}: {val}" for key, val in sorted(occupancy.items())]
details = f"[{', '.join(details)}] [{','.join(users)}]"
summary_strs.append(f"\n -> {node}: {count} {key} {details}")
tail = " ".join(summary_strs)
print(f"{key}: {gpu_count} available {tail}")
def all_info(color: int, verbose: bool, partition: Optional[str] = None):
divider, slurm_str = "---------------------------------", "SLURM"
# Use only verified color names from `colored` library
colors = ["red", "green", "yellow", "blue", "magenta", "cyan", "white", "black"]
if color:
divider = colored.fg(colors[7]) + divider + colored.attr("reset")
slurm_str = colored.fg(colors[0]) + slurm_str + colored.attr("reset")
print(divider)
print(f"Under {slurm_str} management")
print(divider)
# Remaining function logic...
resources = parse_all_gpus(partition=partition)
states = node_states(partition=partition)
for mode in ("up", "accessible"):
summary(mode=mode, resources=resources, states=states)
print(divider)
in_use(resources, partition=partition)
print(divider)
available(resources=resources, states=states, verbose=verbose)
print(divider)
def main():
parser = argparse.ArgumentParser(description="slurm_gpus tool")
parser.add_argument("--action", default="current",
choices=["current", "history", "daemon-start", "daemon-stop"],
help=("The function performed by slurm_gpustat: `current` will"
" provide a summary of current usage, 'history' will "
"provide statistics from historical data (provided that the"
"logging daemon has been running). 'daemon-start' and"
"'daemon-stop' will start and stop the daemon, resp."))
parser.add_argument("-p", "--partition", default=None,
help=("the partition/queue (or multiple, comma separated) of"
" interest. By default set to all available partitions."))
parser.add_argument("--log_path",
default=Path.home() / "data/daemons/logs/slurm_gpustat.log",
help="the location where daemon log files will be stored")
parser.add_argument("--gpustat_pid",
default=Path.home() / "data/daemons/pids/slurm_gpustat.pid",
help="the location where the daemon PID file will be stored")
parser.add_argument("--daemon_log_interval", type=int, default=43200,
help="time interval (secs) between stat logging (default 12 hrs)")
parser.add_argument("--color", type=int, default=1, help="color output")
parser.add_argument("--verbose", action="store_true",
help="provide a more detailed breakdown of resources")
args = parser.parse_args()
if args.action == "current":
all_info(color=args.color, verbose=args.verbose, partition=args.partition)
elif args.action == "history":
data = GPUStatDaemon.deserialize_usage(args.log_path)
historical_summary(data)
elif args.action.startswith("daemon"):
daemon = GPUStatDaemon(
log_path=args.log_path,
pidfile=args.gpustat_pid,
log_interval=args.daemon_log_interval,
)
if args.action == "daemon-start":
print("Starting daemon")
daemon.start()
elif args.action == "daemon-stop":
print("Stopping daemon")
daemon.stop()
if __name__ == "__main__":
main()