import torch


def calc_dihedral(a_coord: torch.Tensor, b_coord: torch.Tensor,
                  c_coord: torch.Tensor,
                  d_coord: torch.Tensor) -> torch.Tensor:
    """
    Calculate a dihedral between tensors of coords
    """
    b1 = a_coord - b_coord
    b2 = b_coord - c_coord
    b3 = c_coord - d_coord

    n1 = torch.cross(b1, b2)
    n1 = torch.div(n1, n1.norm(dim=-1, keepdim=True))
    n2 = torch.cross(b2, b3)
    n2 = torch.div(n2, n2.norm(dim=-1, keepdim=True))
    m1 = torch.cross(n1, torch.div(b2, b2.norm(dim=-1, keepdim=True)))

    dihedral = torch.atan2((m1 * n2).sum(-1), (n1 * n2).sum(-1))

    return dihedral