(tensor: torch.Tensor, length: int, pad_value: Union[int, float], dim: int = -1)
| 72 | |
| 73 | |
| 74 | def pad_to_length(tensor: torch.Tensor, length: int, pad_value: Union[int, float], dim: int = -1) -> torch.Tensor: |
| 75 | if tensor.size(dim) >= length: |
| 76 | return tensor |
| 77 | else: |
| 78 | pad_size = list(tensor.shape) |
| 79 | pad_size[dim] = length - tensor.size(dim) |
| 80 | return torch.cat([tensor, pad_value * torch.ones(*pad_size, dtype=tensor.dtype, device=tensor.device)], dim=dim) |
| 81 | |
| 82 | |
| 83 | def all_gather_if_needed(values: torch.Tensor, rank: int, world_size: int) -> torch.Tensor: |
no outgoing calls
no test coverage detected