File size: 1,260 Bytes
80a72c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import copy
import json
import subprocess
import torch

import logging
LOG = logging.getLogger(__name__)


def load_json(jsonfile):
    with open(jsonfile, "r") as jf:
        res = json.load(jf)
    return res


def save_config(config, filepath):
    with open(filepath, "w") as outfile:    
        json.dump(config, outfile, indent=4)


def apply_diff(base_cfg, diff):
    cfg = copy.deepcopy(base_cfg)
    for k, v in diff.items():
        if isinstance(v, dict):
            cfg[k] = apply_diff(base_cfg[k], v)
        else:
            cfg[k] = v
    return cfg


def execute_bash_command(bash_command_string):
    bash_return = subprocess.run(bash_command_string.split(), timeout=20)
    return bash_return


def get_torch_device(force_cpu=False, use_mps_if_available=False):
    try:
        if torch.cuda.is_available() and not force_cpu:
            device_string = "cuda"
        elif use_mps_if_available and torch.backends.mps.is_available() and not force_cpu:
            device_string = "mps"
        else:
            device_string = "cpu"
    except Exception as exc:
        LOG.error(f'Exception: {exc}')
        device_string = "cpu"
    LOG.info(f'Using device: {device_string}')
    device = torch.device(device_string)
    return device