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