(dim, position)
| 16 | |
| 17 | |
| 18 | def sinusoidal_embedding_1d(dim, position): |
| 19 | # preprocess |
| 20 | assert dim % 2 == 0 |
| 21 | half = dim // 2 |
| 22 | position = position.type(torch.float64) |
| 23 | |
| 24 | # calculation |
| 25 | sinusoid = torch.outer( |
| 26 | position, torch.pow(10000, -torch.arange(half).to(position).div(half))) |
| 27 | x = torch.cat([torch.cos(sinusoid), torch.sin(sinusoid)], dim=1) |
| 28 | return x |
| 29 | |
| 30 | |
| 31 | @amp.autocast(enabled=False) |
no outgoing calls
no test coverage detected