import torch import os import importlib def find(cls_string): module_string = ".".join(cls_string.split(".")[:-1]) cls_name = cls_string.split(".")[-1] module = importlib.import_module(module_string, package=None) cls = getattr(module, cls_name) return cls def get_rank(): # SLURM_PROCID can be set even if SLURM is not managing the multiprocessing, # therefore LOCAL_RANK needs to be checked first rank_keys = ("RANK", "LOCAL_RANK", "SLURM_PROCID", "JSM_NAMESPACE_RANK") for key in rank_keys: rank = os.environ.get(key) if rank is not None: return int(rank) return 0 def get_device(): return torch.device(f"cuda:{get_rank()}")