Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is the implementation of SinusoidalPositionEmbeddings in diffusion tutorial correct ? #2183

Open
ketvector opened this issue Jul 2, 2024 · 1 comment

Comments

@ketvector
Copy link

ketvector commented Jul 2, 2024

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 ?

@osanseviero
Copy link
Contributor

cc @NielsRogge @kashif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants