Utils
distributed
- lis2.utils.distributed.get_device_ids(gpus)[source]
Returns the appropriate list of GPU IDs or ‘cpu’ based on the input argument.
- Parameters:
gpus (int, list of int, str) –
An integer representing a single GPU ID.
A list of integers representing multiple GPU IDs.
The string ‘auto’ to use all available GPUs.
- Returns:
List of GPU IDs or ‘cpu’ if no GPUs are available.
- Return type:
list of int or str
- Raises:
ValueError – If the gpus argument is not valid (not an int, list of ints, or ‘auto’).
IndexError – If the GPU ID in the list exceeds the available GPU count.
- lis2.utils.distributed.get_rank()[source]
Get the rank of the current process. :returns: ‘cpu’ if CUDA is not available, ‘cuda’ if there is a single GPU, or ‘cuda:{rank}’ for distributed GPUs. :rtype: str
- lis2.utils.distributed.get_rank_num()[source]
Get the rank number of the current process. :returns: 0 if CUDA is not available or there is a single GPU, or the rank number for distributed GPUs. :rtype: int
- lis2.utils.distributed.get_world_size()[source]
Get the total number of processes in distributed training. :returns: 1 if CUDA is not available or not in distributed mode, or the total number of processes in distributed training. :rtype: int
- lis2.utils.distributed.is_main_gpu()[source]
Check if the current process is running on the main GPU. :returns: True if CUDA is not available or there is a single GPU, or if the current process is the main process in distributed training. :rtype: bool
- lis2.utils.distributed.reduce_sum(value)[source]
Perform distributed sum reduction on the input tensor. :param tensor: Input tensor to be summed across all processes. :type tensor: torch.Tensor
- Returns:
Resulting tensor after the sum reduction.
- Return type:
torch.Tensor
Normalizer
Normalizing function
- lis2.utils.normalizer.normalize_adapt_histo(x)[source]
Normalize the data with a histogram equalization TODO: to improve
- Parameters:
x (np.ndarray) – The data
- Return type:
A normalized copy of the data
- lis2.utils.normalizer.normalize_direct(x, xmin=None, zmax=None)[source]
Normalize the data by directly projecting them on [0, 1]
- Parameters:
x (np.ndarray) – The data
xmin (float, optional) – The minimum value to normalize by, by default compute from data.
zmax (float, optional) – The maximum value to normalize by, by default compute from data.
- Return type:
A normalized copy of the data
Schema
- lis2.utils.schema.generate_config_schema(input_class: Type[Any]) dict[str, Schema][source]
Automatically generates a configuration schema for a given optimizer by inspecting its __init__ parameters.
- Parameters:
input_class (Type[Optimizer]) – The input class to generate the schema for.
- Returns:
A dictionary mapping parameter names to their corresponding schema.
- Return type:
dict[str, Schema]
- lis2.utils.schema.get_primary_type(default_value: Union) Any[source]
Infer one of the type inside a union of types
- Parameters:
default_value (Union) – The default value to infer the type from.
- Returns:
The inferred type.
- Return type:
Any