MCPcopy Create free account
hub / github.com/TencentARC/MotionCtrl / timestep_embedding

Function timestep_embedding

lvdm/models/utils_diffusion.py:9–29  ·  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

7
8
9def timestep_embedding(timesteps, dim, max_period=10000, repeat_only=False):
10 """
11 Create sinusoidal timestep embeddings.
12 :param timesteps: a 1-D Tensor of N indices, one per batch element.
13 These may be fractional.
14 :param dim: the dimension of the output.
15 :param max_period: controls the minimum frequency of the embeddings.
16 :return: an [N x dim] Tensor of positional embeddings.
17 """
18 if not repeat_only:
19 half = dim // 2
20 freqs = torch.exp(
21 -math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half
22 ).to(device=timesteps.device)
23 args = timesteps[:, None].float() * freqs[None]
24 embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1)
25 if dim % 2:
26 embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1)
27 else:
28 embedding = repeat(timesteps, 'b -> b d', d=dim)
29 return embedding
30
31
32def make_beta_schedule(schedule, n_timestep, linear_start=1e-4, linear_end=2e-2, cosine_s=8e-3):

Callers 2

selfattn_forward_unetFunction · 0.90
forwardMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected