import torch


def pdist2(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
    """Compute squared pairwise distances (x-y)*(x-y)

    :param x: n by d matrix of d-dimensional values
    :param y: m by d matrix of d-dimensional values
    :return: n by m matrix of squared pairwise distances
    """
    n, d = x.size()
    if y.size()[-1] != d:
        raise ValueError("x and y must have same second dimension")
    xx = torch.sum(x*x, dim=1)
    yy = torch.sum(y*y, dim=1)
    xy = torch.einsum('nd,md->nm', x, y)
    # Using (x-y)*(x-y) = x*x + y+y - 2*x*y
    return xx[:, None] + yy[None, :] - 2 * xy