MCPcopy Create free account
hub / github.com/MotrixLab/MotionDiffuse / timestep_embedding

Function timestep_embedding

text2motion/models/transformer.py:15–32  ·  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)

Source from the content-addressed store, hash-verified

13
14
15def timestep_embedding(timesteps, dim, max_period=10000):
16 """
17 Create sinusoidal timestep embeddings.
18 :param timesteps: a 1-D Tensor of N indices, one per batch element.
19 These may be fractional.
20 :param dim: the dimension of the output.
21 :param max_period: controls the minimum frequency of the embeddings.
22 :return: an [N x dim] Tensor of positional embeddings.
23 """
24 half = dim // 2
25 freqs = torch.exp(
26 -math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half
27 ).to(device=timesteps.device)
28 args = timesteps[:, None].float() * freqs[None]
29 embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1)
30 if dim % 2:
31 embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1)
32 return embedding
33
34
35def set_requires_grad(nets, requires_grad=False):

Callers 1

forwardMethod · 0.85

Calls 1

toMethod · 0.80

Tested by

no test coverage detected