create torch device Args: device_name (str): cpu, gpu, gpu:0, cuda:0 etc.
(device_name)
| 83 | |
| 84 | |
| 85 | def create_device(device_name): |
| 86 | """ create torch device |
| 87 | |
| 88 | Args: |
| 89 | device_name (str): cpu, gpu, gpu:0, cuda:0 etc. |
| 90 | """ |
| 91 | import torch |
| 92 | device_type, device_id = verify_device(device_name) |
| 93 | use_cuda = False |
| 94 | if device_type == Devices.gpu: |
| 95 | use_cuda = True |
| 96 | if not torch.cuda.is_available(): |
| 97 | logger.info('cuda is not available, using cpu instead.') |
| 98 | use_cuda = False |
| 99 | |
| 100 | if use_cuda: |
| 101 | device = torch.device(f'cuda:{device_id}') |
| 102 | else: |
| 103 | device = torch.device('cpu') |
| 104 | |
| 105 | return device |
| 106 | |
| 107 | |
| 108 | def get_device(): |
searching dependent graphs…