Neural Networks & LLM Components ยท Hard
Rotary Positional Encoding
Rotate paired query or key features with position-dependent frequencies.
Task
Implement rotary_positional_encoding(values: list[list[float]], positions: list[int], base: float = 10000.0) -> list[list[float]]. Apply rotary positional encoding to each row by rotating adjacent feature pairs with position-dependent frequencies.
Requirements
- values has one row per position and an even feature width; positions has one integer per row.
- For pair index i, use angle = position / base ** (2 * i / d_model).
- Rotate each adjacent pair (x_even, x_odd) into (x_even * cos(angle) - x_odd * sin(angle), x_even * sin(angle) + x_odd * cos(angle)).
- Use the same angle for both features in a pair.
- Rotate each row using its corresponding position without mixing rows.
- Do not mutate values or positions.
- Preserve empty input and zero-width rows.
Example
values = [[1.0, 0.0, 1.0, 0.0]]
positions = [1]
rotary_positional_encoding(values, positions)
# [[0.54030231, 0.84147098, 0.99995000, 0.00999983]]