device
learning.device
"Device Manager, converting modules and data tensors to device
AttributeDataParallel
Bases: DataParallel
DataParallel class with attribute get method.
You can now use expressions like:
Possible with origial DataParallel class.
... self.network(x) ... # Not possible with origial DataParallel class. ... self.network.feature_extractor(x)
...where self.network is a AttributeDataParallel class instance.
DeviceManager
DeviceManager(device: DeviceType = 'cpu', multi_gpu: bool = False)
converting torch.nn.modules and torch.tensors to selected device.
Attributes:
-
device(device) –name of target device for learning.
-
multi_gpu(bool) –whether use multi-gpu learning.
initializing DeviceManager
Parameters:
-
device(Optional[Union[device, str, int]], default:'cpu') –user defined device. Defaults to None.
-
multi_gpu(bool, default:False) –set to class attribute. Defaults to False.
Source code in SaigeToolkit/learning/device.py
set_gpu
set_gpu(device: DeviceType = 'cpu') -> None
If user gives device string, use the defined device.
Parameters:
-
device(Optional[Union[device, str, int]], default:'cpu') –user defined device. Defaults to None.
Source code in SaigeToolkit/learning/device.py
module_to_device
converting torch modules to device
Parameters:
-
module(object) –task specific modules # XXX: isinstance of BaseModule?
Returns:
-
List[Optional[Module]]–List[torch.nn.Module]: network module list which needs to be reset after iteration step.
Source code in SaigeToolkit/learning/device.py
data_to_device
convetring tensor-type data to device
Parameters:
-
data(dict, default:{}) –data with tensors and also non-tensors.
Raises:
-
AssertionError–input data should be dict type.
Returns:
-
dict(dict) –data dict with tensors converted to device
Source code in SaigeToolkit/learning/device.py
get_module_device
cuda_empty_cache
cuda_empty_cache(device: DeviceType) -> None
CUDA device의 torch cache를 비웁니다 Multi-GPU 환경에서 0번이 아닌 device에 대해 empty_cache 호출 시 0번 device에 메모리 올리는 이슈를 해결
Source code in SaigeToolkit/learning/device.py
self_device_context
method에 self.device context 적용
Source code in SaigeToolkit/learning/device.py
apply_self_device_context
class의 모든 method에 self.device context 적용 (staticmethod, classmethod 및 기본으로 정의된 method 제외)
Note: 모든 메소드에 적용하기 때문에 퍼포먼스 이슈가 발생할 수 있습니다