MCPcopy Create free account
hub / github.com/CompVis/diff2flow / timestep_embedding

Function timestep_embedding

diff2flow/models/unet/util.py:69–89  ·  view source on GitHub ↗

Create sinusoidal timestep embeddings. :param timesteps: a 1-D Tensor of N indices, one per batch element. These may be fractional. :param dim: the dimension of the output. :param max_period: controls the minimum frequency of the embeddings. :return: an [N

(timesteps, dim, max_period=10000, repeat_only=False)

Source from the content-addressed store, hash-verified

67
68
69def timestep_embedding(timesteps, dim, max_period=10000, repeat_only=False):
70 """
71 Create sinusoidal timestep embeddings.
72 :param timesteps: a 1-D Tensor of N indices, one per batch element.
73 These may be fractional.
74 :param dim: the dimension of the output.
75 :param max_period: controls the minimum frequency of the embeddings.
76 :return: an [N x dim] Tensor of positional embeddings.
77 """
78 if not repeat_only:
79 half = dim // 2
80 freqs = torch.exp(
81 -math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half
82 ).to(device=timesteps.device)
83 args = timesteps[:, None].float() * freqs[None]
84 embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1)
85 if dim % 2:
86 embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1)
87 else:
88 embedding = repeat(timesteps, 'b -> b d', d=dim)
89 return embedding
90
91
92def zero_module(module):

Callers 3

forwardMethod · 0.90
forwardMethod · 0.90
get_midblock_featuresMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected