Download src/deoldify/_device.py from Ashrafb/Bwcdocke: direct link, hf CLI and curl.
- Browser
- Download file 833 Bytes
-
https://huggingface.co/spaces/Ashrafb/Bwcdocke/resolve/main/src/deoldify/_device.py
- Command line
-
hf download hf://spaces/Ashrafb/Bwcdocke/src/deoldify/_device.py
-
curl -L -o _device.py https://huggingface.co/spaces/Ashrafb/Bwcdocke/resolve/main/src/deoldify/_device.py
833 Bytes
| import os | |
| from enum import Enum | |
| from .device_id import DeviceId | |
| #NOTE: This must be called first before any torch imports in order to work properly! | |
| class DeviceException(Exception): | |
| pass | |
| class _Device: | |
| def __init__(self): | |
| self.set(DeviceId.CPU) | |
| def is_gpu(self): | |
| ''' Returns `True` if the current device is GPU, `False` otherwise. ''' | |
| return self.current() is not DeviceId.CPU | |
| def current(self): | |
| return self._current_device | |
| def set(self, device:DeviceId): | |
| if device == DeviceId.CPU: | |
| os.environ['CUDA_VISIBLE_DEVICES']='' | |
| else: | |
| os.environ['CUDA_VISIBLE_DEVICES']=str(device.value) | |
| import torch | |
| torch.backends.cudnn.benchmark=False | |
| self._current_device = device | |
| return device |