MCPcopy Create free account
hub / github.com/VisionXLab/OF-Diff / get_timestep_embedding

Function get_timestep_embedding

ldm/modules/diffusionmodules/model.py:27–45  ·  view source on GitHub ↗

This matches the implementation in Denoising Diffusion Probabilistic Models: From Fairseq. Build sinusoidal embeddings. This matches the implementation in tensor2tensor, but differs slightly from the description in Section 3.5 of "Attention Is All You Need".

(timesteps, embedding_dim)

Source from the content-addressed store, hash-verified

25
26
27def get_timestep_embedding(timesteps, embedding_dim):
28 """
29 This matches the implementation in Denoising Diffusion Probabilistic Models:
30 From Fairseq.
31 Build sinusoidal embeddings.
32 This matches the implementation in tensor2tensor, but differs slightly
33 from the description in Section 3.5 of "Attention Is All You Need".
34 """
35 assert len(timesteps.shape) == 1
36
37 half_dim = embedding_dim // 2
38 emb = math.log(10000) / (half_dim - 1)
39 emb = torch.exp(torch.arange(half_dim, dtype=torch.float32) * -emb)
40 emb = emb.to(device=timesteps.device)
41 emb = timesteps.float()[:, None] * emb[None, :]
42 emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
43 if embedding_dim % 2 == 1: # zero pad
44 emb = torch.nn.functional.pad(emb, (0,1,0,0))
45 return emb
46
47
48def nonlinearity(x):

Callers 1

forwardMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected