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)
| 67 | |
| 68 | |
| 69 | def 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 | |
| 92 | def zero_module(module): |
no outgoing calls
no test coverage detected