import torch
from typing import List


def _decode(loc: torch.Tensor, priors: torch.Tensor, variances: List[float]) -> torch.Tensor:
    """Decode locations from predictions using priors to undo the encoding we did for offset regression at train
    time.

    Args:
        loc:location predictions for loc layers. Shape: [num_priors,4].
        priors: Prior boxes in center-offset form. Shape: [num_priors,4].
        variances: (list[float]) Variances of priorboxes.

    Return:
        Tensor containing decoded bounding box predictions.
    """
    boxes = torch.cat((
        priors[:, 0:2] + loc[:, 0:2] * variances[0] * priors[:, 2:4],
        priors[:, 2:4] * torch.exp(loc[:, 2:4] * variances[1]),
        priors[:, 0:2] + loc[:, 4:6] * variances[0] * priors[:, 2:4],
        priors[:, 0:2] + loc[:, 6:8] * variances[0] * priors[:, 2:4],
        priors[:, 0:2] + loc[:, 8:10] * variances[0] * priors[:, 2:4],
        priors[:, 0:2] + loc[:, 10:12] * variances[0] * priors[:, 2:4],
        priors[:, 0:2] + loc[:, 12:14] * variances[0] * priors[:, 2:4]), 1)
    # prepare final output
    tmp = boxes[:, 0:2] - boxes[:, 2:4] / 2
    return torch.cat((tmp, boxes[:, 2:4] + tmp, boxes[:, 4:]), dim=-1)