blog icon indicating copy to clipboard operation
blog copied to clipboard

Is the implementation of SinusoidalPositionEmbeddings in diffusion tutorial correct ?

Open ketvector opened this issue 1 year ago • 1 comments

class SinusoidalPositionEmbeddings(nn.Module):
    def __init__(self, dim):
        super().__init__()
        self.dim = dim

    def forward(self, time):
        device = time.device
        half_dim = self.dim // 2
        embeddings = math.log(10000) / (half_dim - 1)
        embeddings = torch.exp(torch.arange(half_dim, device=device) * -embeddings)
        embeddings = time[:, None] * embeddings[None, :]
        embeddings = torch.cat((embeddings.sin(), embeddings.cos()), dim=-1)
        return embeddings

In the tutorial : https://github.com/huggingface/blog/blob/main/annotated-diffusion.md

for a given time-step, will this implementation not place all the sines first, and all the cosines later. (due to the line embeddings = torch.cat((embeddings.sin(), embeddings.cos()), dim=-1))

should it not be alternating sines and cosines like sin , cos, sin, cos ... and so on ?

ketvector avatar Jul 02 '24 11:07 ketvector

cc @NielsRogge @kashif

osanseviero avatar Jul 03 '24 12:07 osanseviero