MCPcopy Create free account
hub / github.com/IceClear/StableSR / timestep_embedding

Function timestep_embedding

ldm/modules/diffusionmodules/util.py:151–171  ·  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

149
150
151def timestep_embedding(timesteps, dim, max_period=10000, repeat_only=False):
152 """
153 Create sinusoidal timestep embeddings.
154 :param timesteps: a 1-D Tensor of N indices, one per batch element.
155 These may be fractional.
156 :param dim: the dimension of the output.
157 :param max_period: controls the minimum frequency of the embeddings.
158 :return: an [N x dim] Tensor of positional embeddings.
159 """
160 if not repeat_only:
161 half = dim // 2
162 freqs = torch.exp(
163 -math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half
164 ).to(device=timesteps.device)
165 args = timesteps[:, None].float() * freqs[None]
166 embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1)
167 if dim % 2:
168 embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1)
169 else:
170 embedding = repeat(timesteps, 'b -> b d', d=dim)
171 return embedding
172
173
174def zero_module(module):

Callers 3

forwardMethod · 0.90
forwardMethod · 0.90
forwardMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected