import torch


def compute_intersection(a, b):
    """Compute intersection between boxes.

    Args:
        a: tensor (m, 4)
        b: tensor (n, 4)

    Returns:
        tensor (m, n)
    """
    # (m, 1, 2)
    a_min = a[:, 0:2].unsqueeze(1)
    # (1, n, 2)
    b_min = b[:, 0:2].unsqueeze(0)
    # (m, n, 2)
    lb = torch.max(a_min, b_min)

    # (m, 1, 2)
    a_max = a[:, 2:4].unsqueeze(1)
    # (1, n, 2)
    b_max = b[:, 2:4].unsqueeze(0)
    # (m, n, 2)
    ub = torch.min(a_max, b_max)

    # (m, n)
    inter = (ub - lb).clamp(min=0).prod(2)
    return inter