(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor)
| 467 | |
| 468 | |
| 469 | def apply_rope(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor: |
| 470 | head_size = x.size(-1) |
| 471 | x1 = x[..., : head_size // 2] # (B, nh, T, hs/2) |
| 472 | x2 = x[..., head_size // 2 :] # (B, nh, T, hs/2) |
| 473 | rotated = torch.cat((-x2, x1), dim=-1) # (B, nh, T, hs) |
| 474 | roped = (x * cos) + (rotated * sin) |
| 475 | return roped.type_as(x) |