-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from reginabarzilaygroup/v1.2.2_dev
V1.2.2 dev
- Loading branch information
Showing
7 changed files
with
213 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import itertools | ||
import os | ||
from typing import Union | ||
|
||
import torch | ||
|
||
|
||
def get_default_device(): | ||
if torch.cuda.is_available(): | ||
return get_most_free_gpu() | ||
elif torch.backends.mps.is_available(): | ||
# Not all operations implemented in MPS yet | ||
use_mps = os.environ.get("PYTORCH_ENABLE_MPS_FALLBACK", "0") == "1" | ||
if use_mps: | ||
return torch.device('mps') | ||
else: | ||
return torch.device('cpu') | ||
else: | ||
return torch.device('cpu') | ||
|
||
|
||
def get_available_devices(num_devices=None, max_devices=None): | ||
device = get_default_device() | ||
if device.type == "cuda": | ||
num_gpus = torch.cuda.device_count() | ||
if max_devices is not None: | ||
num_gpus = min(num_gpus, max_devices) | ||
gpu_list = [get_device(i) for i in range(num_gpus)] | ||
if num_devices is not None: | ||
cycle_gpu_list = itertools.cycle(gpu_list) | ||
gpu_list = [next(cycle_gpu_list) for _ in range(num_devices)] | ||
return gpu_list | ||
else: | ||
num_devices = num_devices if num_devices else torch.multiprocessing.cpu_count() | ||
num_devices = min(num_devices, max_devices) if max_devices is not None else num_devices | ||
return [device]*num_devices | ||
|
||
|
||
def get_device(gpu_id: int): | ||
if gpu_id is not None and torch.cuda.is_available() and gpu_id < torch.cuda.device_count(): | ||
return torch.device(f'cuda:{gpu_id}') | ||
else: | ||
return None | ||
|
||
|
||
def get_device_mem_info(device: Union[int, torch.device]): | ||
if not torch.cuda.is_available(): | ||
return None | ||
|
||
free_mem, total_mem = torch.cuda.mem_get_info(device=device) | ||
return free_mem, total_mem | ||
|
||
|
||
def get_most_free_gpu(): | ||
""" | ||
Get the GPU with the most free memory | ||
If system has no GPUs (or CUDA not available), return None | ||
""" | ||
if not torch.cuda.is_available(): | ||
return None | ||
|
||
num_gpus = torch.cuda.device_count() | ||
if num_gpus == 0: | ||
return None | ||
|
||
most_free_idx, most_free_val = -1, -1 | ||
for i in range(num_gpus): | ||
free_mem, total_mem = get_device_mem_info(i) | ||
if free_mem > most_free_val: | ||
most_free_idx, most_free_val = i, free_mem | ||
|
||
return torch.device(f'cuda:{most_free_idx}') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import logging | ||
import os | ||
|
||
|
||
LOGGER_NAME = "sybil" | ||
LOGLEVEL_KEY = "LOG_LEVEL" | ||
|
||
|
||
def _get_formatter(loglevel="INFO"): | ||
warn_fmt = "[%(asctime)s] %(levelname)s -%(message)s" | ||
debug_fmt = "[%(asctime)s] [%(filename)s:%(lineno)d] %(levelname)s - %(message)s" | ||
fmt = debug_fmt if loglevel.upper() in {"DEBUG"} else warn_fmt | ||
return logging.Formatter( | ||
fmt=fmt, | ||
datefmt="%Y-%b-%d %H:%M:%S %Z", | ||
) | ||
|
||
|
||
def remove_all_handlers(logger): | ||
while logger.hasHandlers(): | ||
logger.removeHandler(logger.handlers[0]) | ||
|
||
|
||
def configure_logger(loglevel=None, logger_name=LOGGER_NAME, logfile=None): | ||
"""Do basic logger configuration and set our main logger""" | ||
|
||
# Set as environment variable so other processes can retrieve it | ||
if loglevel is None: | ||
loglevel = os.environ.get(LOGLEVEL_KEY, "WARNING") | ||
else: | ||
os.environ[LOGLEVEL_KEY] = loglevel | ||
|
||
logger = logging.getLogger(logger_name) | ||
logger.setLevel(loglevel) | ||
remove_all_handlers(logger) | ||
logger.propagate = False | ||
|
||
formatter = _get_formatter(loglevel) | ||
def _prep_handler(handler): | ||
for ex_handler in logger.handlers: | ||
if type(ex_handler) == type(handler): | ||
# Remove old handler, don't want to double-handle | ||
logger.removeHandler(ex_handler) | ||
handler.setLevel(loglevel) | ||
handler.setFormatter(formatter) | ||
logger.addHandler(handler) | ||
|
||
sh = logging.StreamHandler() | ||
_prep_handler(sh) | ||
|
||
if logfile is not None: | ||
fh = logging.FileHandler(logfile, mode="a") | ||
_prep_handler(fh) | ||
|
||
return logger | ||
|
||
|
||
def get_logger(base_name=LOGGER_NAME): | ||
""" | ||
Return a logger. | ||
Use a different logger in each subprocess, though they should all have the same log level. | ||
""" | ||
pid = os.getpid() | ||
logger_name = f"{base_name}-process-{pid}" | ||
logger = logging.getLogger(logger_name) | ||
if not logger.hasHandlers(): | ||
configure_logger(logger_name=logger_name) | ||
return logger |