fastdev.utils.struct¶
Module Contents¶
- fastdev.utils.struct.list_to_padded(x: List[torch.Tensor] | Tuple[torch.Tensor], pad_size: Sequence[int] | None = None, pad_value: float | int = 0.0, equisized: bool = False) torch.Tensor [source]¶
Transforms a list of N tensors each of shape (Si_0, Si_1, … Si_D) into: - a single tensor of shape (N, pad_size(0), pad_size(1), …, pad_size(D))
if pad_size is provided
or a tensor of shape (N, max(Si_0), max(Si_1), …, max(Si_D)) if pad_size is None.
- Parameters:
x (Union[List[torch.Tensor], Tuple[torch.Tensor]]) – list of Tensors
pad_size (Union[Sequence[int], None]) – list(int) specifying the size of the padded tensor. If None (default), the largest size of each dimension is set as the pad_size.
pad_value (Union[float, int]) – float value to be used to fill the padded tensor
equisized (bool) – bool indicating whether the items in x are of equal size (sometimes this is known and if provided saves computation)
- Returns:
- tensor consisting of padded input tensors stored
over the newly allocated memory.
- Return type:
x_padded
- fastdev.utils.struct.padded_to_list(x: torch.Tensor, split_size: Sequence[int] | None = None, dim: int = 0) List[torch.Tensor] [source]¶
Transforms a padded tensor of shape (N, S_1, S_2, …, S_D) into a list of N tensors of shape: - (Si_1, Si_2, …, Si_D) where (Si_1, Si_2, …, Si_D) is specified in split_size(i) - or (S_1, S_2, …, S_D) if split_size is None - or (Si_1, S_2, …, S_D) if split_size(i) is an integer.
- Parameters:
x (torch.Tensor) – tensor
split_size (Union[Sequence[int], None]) – optional 1D list/tuple of ints defining the number of items for each tensor.
dim (int)
- Returns:
a list of tensors sharing the memory with the input.
- Return type:
x_list
- fastdev.utils.struct.list_to_packed(x: List[torch.Tensor]) Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor] [source]¶
Transforms a list of N tensors each of shape (Mi, K, …) into a single tensor of shape (sum(Mi), K, …).
- Parameters:
x (List[torch.Tensor]) – list of tensors.
- Returns:
4-element tuple containing
x_packed: tensor consisting of packed input tensors along the 1st dimension.
num_items: tensor of shape N containing Mi for each element in x.
item_packed_first_idx: tensor of shape N indicating the index of the first item belonging to the same element in the original list.
item_packed_to_list_idx: tensor of shape sum(Mi) containing the index of the element in the list the item belongs to.
- Return type:
Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]
- fastdev.utils.struct.packed_to_list(x: torch.Tensor, split_size: Sequence[int] | int) List[torch.Tensor] [source]¶
Transforms a tensor of shape (sum(Mi), K, L, …) to N set of tensors of shape (Mi, K, L, …) where Mi’s are defined in split_size
- Parameters:
x (torch.Tensor) – tensor
split_size (Union[Sequence[int], int]) – list, tuple or int defining the number of items for each tensor in the output list.
- Returns:
A list of Tensors
- Return type:
x_list
- fastdev.utils.struct.list_to_padded_numpy(x: List[numpy.ndarray], pad_size: Sequence[int] | None = None, pad_value: float | int = 0.0, equisized: bool = False) numpy.ndarray [source]¶
Transforms a list of N numpy arrays each of shape (Si_0, Si_1, … Si_D) into: - a single array of shape (N, pad_size(0), pad_size(1), …, pad_size(D))
if pad_size is provided
or an array of shape (N, max(Si_0), max(Si_1), …, max(Si_D)) if pad_size is None.
- Parameters:
x (List[numpy.ndarray]) – list of numpy arrays
pad_size (Union[Sequence[int], None]) – list(int) specifying the size of the padded array. If None (default), the largest size of each dimension is set as the pad_size.
pad_value (Union[float, int]) – float/int value to be used to fill the padded array
equisized (bool) – bool indicating whether the items in x are of equal size
- Returns:
numpy array consisting of padded input arrays
- Return type:
x_padded
- fastdev.utils.struct.list_to_packed_numpy(x: List[numpy.ndarray]) Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray] [source]¶
Transforms a list of N numpy arrays each of shape (Mi, K, …) into a single array of shape (sum(Mi), K, …).
- Parameters:
x (List[numpy.ndarray]) – list of numpy arrays.
- Returns:
4-element tuple containing
x_packed: array consisting of packed input arrays along the 1st dimension.
num_items: array of shape N containing Mi for each element in x.
item_packed_first_idx: array of shape N indicating the index of the first item belonging to the same element in the original list.
item_packed_to_list_idx: array of shape sum(Mi) containing the index of the element in the list the item belongs to.
- Return type:
Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray]