def positional_encoding_2d(condition_vector: int, height: int, width: int) -> torch.Tensor:
    """Create 2D positional encoding using sine and cosine functions.

    Creates an embedding to store relative position of feature vectors using
    sinusoidal functions at different frequencies.

    Args:
        condition_vector (int): Length of the condition vector (must be multiple
            of 4)
        height (int): Height of the positions grid
        width (int): Width of the positions grid

    Raises:
        ValueError: If ``condition_vector`` is not a multiple of 4

    Returns:
        torch.Tensor: Position encoding of shape
            ``(condition_vector, height, width)``

    Example:
        >>> encoding = positional_encoding_2d(128, 32, 32)
        >>> encoding.shape
        torch.Size([128, 32, 32])
    """
    if condition_vector % 4 != 0:
        msg = f"Cannot use sin/cos positional encoding with odd dimension (got dim={condition_vector})"
        raise ValueError(msg)
    pos_encoding = torch.zeros(condition_vector, height, width)
    # Each dimension use half of condition_vector
    condition_vector = condition_vector // 2
    div_term = torch.exp(torch.arange(0.0, condition_vector, 2) * -(math.log(1e4) / condition_vector))
    pos_w = torch.arange(0.0, width).unsqueeze(1)
    pos_h = torch.arange(0.0, height).unsqueeze(1)
    pos_encoding[0:condition_vector:2, :, :] = (
        torch.sin(pos_w * div_term).transpose(0, 1).unsqueeze(1).repeat(1, height, 1)
    )
    pos_encoding[1:condition_vector:2, :, :] = (
        torch.cos(pos_w * div_term).transpose(0, 1).unsqueeze(1).repeat(1, height, 1)
    )
    pos_encoding[condition_vector::2, :, :] = (
        torch.sin(pos_h * div_term).transpose(0, 1).unsqueeze(2).repeat(1, 1, width)
    )
    pos_encoding[condition_vector + 1 :: 2, :, :] = (
        torch.cos(pos_h * div_term).transpose(0, 1).unsqueeze(2).repeat(1, 1, width)
    )
    return pos_encoding
Create 2D positional encoding using sine and cosine functions.

    Creates an embedding to store relative position of feature vectors using
    sinusoidal functions at different frequencies.

    Args:
        condition_vector (int): Length of the condition vector (must be multiple
            of 4)
        height (int): Height of the positions grid
        width (int): Width of the positions grid

    Raises:
        ValueError: If ``condition_vector`` is not a multiple of 4

    Returns:
        torch.Tensor: Position encoding of shape
            ``(condition_vector, height, width)``

    Example:
        >>> encoding = positional_encoding_2d(128, 32, 32)
        >>> encoding.shape
        torch.Size([128, 32, 32])
    
src/anomalib/models/image/cflow/utils.py