def get_1d_sine_cosine_positional_embedding_from_positions(
    embed_dim: int, pos: NDArray[np.float32]
) -> NDArray[np.float32]:
    """Generates 1D sine-cosine positional embedding from positions.

    Code follows: https://github.com/facebookresearch/mae/blob/main/util/pos_embed.py

    Args:
        embed_dim:
            Embedding dimension.
        pos:
            Positions to be encoded with shape (N, M).
    Returns:
        Positional embedding with shape (N * M, embed_dim).
    """
    assert embed_dim % 2 == 0
    omega = np.arange(embed_dim // 2, dtype=np.float32)
    omega /= embed_dim / 2.0
    omega = 1.0 / 10000**omega  # (embed_dim/2,)

    pos = pos.reshape(-1)  # (N*M,)
    out = np.einsum("m,d->md", pos, omega)  # (N*M, embed_dim/2), outer product

    emb_sin = np.sin(out)  # (N*M, embed_dim/2)
    emb_cos = np.cos(out)  # (N*M, embed_dim/2)

    emb = np.concatenate([emb_sin, emb_cos], axis=1)  # (N*M, embed_dim)
    return emb
Generates 1D sine-cosine positional embedding from positions.

    Code follows: https://github.com/facebookresearch/mae/blob/main/util/pos_embed.py

    Args:
        embed_dim:
            Embedding dimension.
        pos:
            Positions to be encoded with shape (N, M).
    Returns:
        Positional embedding with shape (N * M, embed_dim).
    
lightly/models/utils.py