import torch


def merge_masks(shape, masks, offsets):
    """
    Merge all masks in one mask.

    Parameters
    ----------
    shape : tuple
        The width and height of the mask.
    masks : Tensor
        A tensor of masks.
    offsets : Tensor
        A tensor of offsets from the masks.

    Return
    ------
    mask : Tensor
        The merged mask.
    """

    height, width = shape
    mask = torch.zeros((height, width), dtype=torch.uint8)

    for m, offset in zip(masks, offsets):
        h, w = m.shape[1:]
        x, y = offset

        # Merge the sub masks
        mask[y:y+h, x:x+w] |= m.squeeze().type(torch.uint8)

    return mask.type(torch.float32)