import torch


def _pairwise_distances(x, x2, y, y2):
    """
    Computes pairwise squared l2 distances between tensors x and y.
    Args:
    x: [n, feature_dim].
    y: [m, feature_dim].
    Returns:
    d: [n, m].
    """
    xs = x2
    ys = y2

    xs = xs.unsqueeze(1)
    ys = ys.unsqueeze(0)
    d = xs + ys - 2. * torch.matmul(x, torch.t(y))
    return d