import torch


def recover_labels(padded_labels: torch.Tensor, lengths: list) -> torch.Tensor:
    """Utility function to remove the padded labels and retrieve the ground truth labels.

    Args:
        padded_labels (torch.Tensor): Vector of labels including -1's.
        lengths (list): Lengths of original sequences in terms of tokens.

    Returns:
        torch.Tensor: Unpadded sequence of labels.
    """
    # extract labels
    max_len = max(lengths)
    labels_vector = padded_labels.reshape(-1)
    i = 0
    labels_list = []
    for length in lengths:
        start = i * max_len
        stop = start + length
        labels_list.append(labels_vector[start:stop])
        i += 1
    return torch.cat(labels_list)