Example 1
Arguments: [
[
[
1,
2
]
],
[
0
],
10000
]
Expected result: [
[
1,
2
]
]At position 0, every angle is zero and the row is unchanged.
Tokenization and attention · Medium
Problem 16 of 50 · Python and NumPy
Rotate adjacent feature pairs (0,1), (2,3), … in each token row. For pair index j starting at zero, angle = positions[t] / base^(2j/D). Transform (a,b) to (a*cos(angle)−b*sin(angle), a*sin(angle)+b*cos(angle)).
Use adjacent-pair ordering, not split-half ordering. Positions are real-valued for this numerical exercise; output keeps shape T × D.
def rotary_embeddings(x, positions, base):Arguments: [
[
[
1,
2
]
],
[
0
],
10000
]
Expected result: [
[
1,
2
]
]At position 0, every angle is zero and the row is unchanged.
Arguments: [
[
[
1,
0
]
],
[
1.5707963267948966
],
10000
]
Expected result: [
[
6.123233995736766e-17,
1
]
]A π/2 rotation sends [1,0] to approximately [0,1].