(dim, position)
| 209 | |
| 210 | |
| 211 | def sinusoidal_embedding_1d(dim, position): |
| 212 | # preprocess |
| 213 | assert dim % 2 == 0 |
| 214 | half = dim // 2 |
| 215 | position = position.type(torch.float64) |
| 216 | |
| 217 | # calculation |
| 218 | sinusoid = torch.outer( |
| 219 | position, torch.pow(10000, -torch.arange(half).to(position).div(half))) |
| 220 | x = torch.cat([torch.cos(sinusoid), torch.sin(sinusoid)], dim=1) |
| 221 | return x |
| 222 | |
| 223 | |
| 224 | @amp.autocast(enabled=False, device_type="cuda") |