import torch


def _draw_over(tensor_a, tensor_b, mask=None):
    """
    Draw the contents of tensor_b over the contents of tensor_a as if it were a canvas, replacing information
    that is already there at all pixels where tensor_b (or mask if provided) is non-zero.
    :return:
    """
    # Compute an alpha mask or tensor b
    if mask is None:
        mask = tensor_b.sum(1, keepdims=True) > 0
    mask = mask.float().clamp(0.0, 1.0)
    out = tensor_a * (1 - mask.type(torch.float32)) + tensor_b * mask
    return out