📘
Rotary Position Embedding
Implement Rotary Position Embedding (RoPE).
RoPE rotates pairs of embedding dimensions by position-dependent angles. For a vector x of dimension D at position pos:
- Split x into pairs: (x[0], x[1]), (x[2], x[3]), ...
- For pair i, compute angle θ_i = pos / 10000^(2i/D)
- Apply rotation: x'[2i] = x[2i] * cos(θ_i) - x[2i+1] * sin(θ_i) x'[2i+1] = x[2i] * sin(θ_i) + x[2i+1] * cos(θ_i)
Input:
- Line 1: pos D (position, dimension)
- Line 2: space-separated floats (the vector)
Output: Rotated vector, values rounded to 4 decimal places.
Example:
Input:
1 4 1.0 0.0 1.0 0.0
Output:
0.5403 0.8415 0.9999 0.0100
Reasoning:
- The input vector is split into pairs: (1.0,0.0) and (1.0,0.0).
- For each pair i, the angle θi is computed: θ0=100002/41=1001 and θ1=100004/41=100001.
- The rotation is applied to each pair:
- For the first pair: x′[0]=1.0⋅cos(1001)−0.0⋅sin(1001) and x′[1]=1.0⋅sin(1001)+0.0⋅cos(1001), resulting in approximately 0.5403 and 0.8415 respectively,
- For the second pair: x′[2]=1.0⋅cos(100001)−0.0⋅sin(100001) and x′[3]=1.0⋅sin(100001)+0.0⋅cos(100001), resulting in approximately 0.9999 and 0.0100 respectively.
- The final output is the rotated vector: 0.5403,0.8415,0.9999,0.0100.
Constraints:
- 0 <= pos <= 100, 2 <= D <= 16 (D is even)
- Use base 10000 for frequency
- Round to 4 decimal places
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.