[docs]classComputationalConfig(ClinicaDLConfig):""" Configuration class to define computational parameters. Parameters ---------- gpu : bool, default=False Whether to use a GPU. non_blocking : bool, default=True Behavior to adopt when sending data or the model to a GPU: "When ``non_blocking`` is set to ``True``, [...] attempts to perform the conversion asynchronously with respect to the host, if possible. This asynchronous behavior applies to both pinned and pageable memory." (see :torch:`PyTorch documentation <generated/torch.Tensor.to.html>`) amp : bool, default=True Whether to use :py:mod:`Automatic Mixed Precision <torch.amp>`. channels_last : bool, default=True Whether to use `Channels Last Memory Format <https://docs.pytorch.org/tutorials/intermediate/memory_format_tutorial.html>`_ when possible. seed : Optional[NonNegativeInt], default=None Global seed to control the randomness. If ``None``, ``ComputationalConfig`` will look for a global seed set with :py:func:`clinicadl.utils.seed.seed_everything`. If a seed is passed here, it will override any global seed. deterministic : Optional[bool], default=None Whether to configure PyTorch's operations in deterministic mode. If ``None``, ``ComputationalConfig`` will look for a global configuration set with :py:func:`clinicadl.utils.seed.seed_everything`. If passed here, it will override any global configuration. """gpu:bool=Truenon_blocking:bool=Trueamp:bool=Truechannels_last:bool=Trueseed:Optional[NonNegativeInt]=Nonedeterministic:Optional[bool]=Nonedefcheck_device(self)->None:""" Checks that the requested device is available. """ifself.gpu:asserttorch.cuda.is_available(),"No GPU with CUDA available."@field_validator("seed",mode="after")@classmethoddef_check_global_seed(cls,seed:Optional[NonNegativeInt])->Optional[NonNegativeInt]:"""If no seed, look for a global seed."""ifseedisNoneand(glob_seed:=os.environ.get(GLOBAL_SEED))isnotNone:returnint(glob_seed)returnseed@field_validator("deterministic",mode="after")@classmethoddef_check_deterministic(cls,deterministic:Optional[bool])->bool:"""If no deterministic arg, look for a global configuration."""ifdeterministicisNone:returnbool(os.environ.get(DETERMINISTIC))returndeterministic@propertydefdevice(self):""" The device, represented as a :py:class:`torch.device`. """returntorch.device("cuda")ifself.gpuelsetorch.device("cpu")defget_scaler(self)->GradScaler:""" To get the :py:class:`torch.amp.GradScaler`. Returns ------- GradScaler The gradient scaler. """returnGradScaler(device=self.device.type,enabled=self.amp)