TensorTonic
Problems
Rotary Position Embedding in Triton
TransformersGPU Programming
Medium

Implement Rotary Position Embedding (RoPE) in Triton. Given an input xxx of shape (N,D)(N, D)(N,D) with DDD even, and pre-computed cos⁡\coscos and sin⁡\sinsin tables of shape (N,D/2)(N, D/2)(N,D/2), write the rotated output. For each row iii and pair index jjj:

out[i,2j]=x[i,2j]⋅cos⁡[i,j]−x[i,2j+1]⋅sin⁡[i,j]\texttt{out}[i, 2j] = x[i, 2j] \cdot \cos[i, j] - x[i, 2j+1] \cdot \sin[i, j]out[i,2j]=x[i,2j]⋅cos[i,j]−x[i,2j+1]⋅sin[i,j] out[i,2j+1]=x[i,2j]⋅sin⁡[i,j]+x[i,2j+1]⋅cos⁡[i,j]\texttt{out}[i, 2j+1] = x[i, 2j] \cdot \sin[i, j] + x[i, 2j+1] \cdot \cos[i, j]out[i,2j+1]=x[i,2j]⋅sin[i,j]+x[i,2j+1]⋅cos[i,j]

RoPE encodes absolute positions by rotating pairs of activation channels in 2D by a position-dependent angle. The pattern is a per-row map with no reduction: each row is independent, and within a row each pair is independent. One program per token row keeps the kernel simple and the launch overhead negligible.

Loading visualization...

Examples

Input: x = [[1.0, 2.0, 3.0, 4.0]], cos = [[1.0, 0.0]], sin = [[0.0, 1.0]]

Output: [[1.0, 2.0, -4.0, 3.0]]

Input: x = [[1.0, 0.0], [0.0, 1.0]], cos = [[1.0], [0.0]], sin = [[0.0], [1.0]]

Output: [[1.0, 0.0], [-1.0, 0.0]]

Hint 1

One program per row. Address xxx at offsets 2j2j2j and 2j+12j+12j+1 across the pair axis; the same column mask works for both halves.

Hint 2

Load cos⁡\coscos and sin⁡\sinsin from the row of the precomputed tables and reuse them for both output halves. No transcendentals are computed inside the kernel.

Hint 3

Form out_even=xeven⋅c−xodd⋅s\text{out\_even} = x_\text{even} \cdot c - x_\text{odd} \cdot sout_even=xeven​⋅c−xodd​⋅s and out_odd=xeven⋅s+xodd⋅c\text{out\_odd} = x_\text{even} \cdot s + x_\text{odd} \cdot cout_odd=xeven​⋅s+xodd​⋅c in registers, then store each at its own offset.

Requirements

  • Use one Triton program per token row; iterate over the D/2D/2D/2 pair axis inside the program
  • Apply the 2D rotation in registers using the loaded cos⁡\coscos and sin⁡\sinsin for the row
  • Mask the pair-axis tail so a D/2D/2D/2 not divisible by the block size is safe
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤10241 \le N \le 10241≤N≤1024, DDD even with 2≤D≤2562 \le D \le 2562≤D≤256.
  • All tensors are torch.float32 on cuda.
  • Tolerance: atol=1e-3.
  • No batch, single head per call.
Try Similar Problems
Scaled Dot-Product Attention in TritonMediumOnline Softmax Kernel in TritonMediumKV Cache Append in TritonEasyFlashAttention Forward Kernel in TritonHardScaled Dot-Product Attention in CUDAHardRow-Wise LogSumExp in TritonMediumBlock-Pointer Matmul in TritonMediumSingle-Pass Mean and Variance in TritonMediumAutotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumLayerNorm Forward in TritonMediumFused Row-Wise Softmax in TritonMedium

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Rotary Position Embedding in Triton

Rotary Position Embedding in Triton

TransformersGPU Programming
Medium

Implement Rotary Position Embedding (RoPE) in Triton. Given an input xxx of shape (N,D)(N, D)(N,D) with DDD even, and pre-computed cos⁡\coscos and sin⁡\sinsin tables of shape (N,D/2)(N, D/2)(N,D/2), write the rotated output. For each row iii and pair index jjj:

out[i,2j]=x[i,2j]⋅cos⁡[i,j]−x[i,2j+1]⋅sin⁡[i,j]\texttt{out}[i, 2j] = x[i, 2j] \cdot \cos[i, j] - x[i, 2j+1] \cdot \sin[i, j]out[i,2j]=x[i,2j]⋅cos[i,j]−x[i,2j+1]⋅sin[i,j] out[i,2j+1]=x[i,2j]⋅sin⁡[i,j]+x[i,2j+1]⋅cos⁡[i,j]\texttt{out}[i, 2j+1] = x[i, 2j] \cdot \sin[i, j] + x[i, 2j+1] \cdot \cos[i, j]out[i,2j+1]=x[i,2j]⋅sin[i,j]+x[i,2j+1]⋅cos[i,j]

RoPE encodes absolute positions by rotating pairs of activation channels in 2D by a position-dependent angle. The pattern is a per-row map with no reduction: each row is independent, and within a row each pair is independent. One program per token row keeps the kernel simple and the launch overhead negligible.

Loading visualization...

Examples

Input: x = [[1.0, 2.0, 3.0, 4.0]], cos = [[1.0, 0.0]], sin = [[0.0, 1.0]]

Output: [[1.0, 2.0, -4.0, 3.0]]

Input: x = [[1.0, 0.0], [0.0, 1.0]], cos = [[1.0], [0.0]], sin = [[0.0], [1.0]]

Output: [[1.0, 0.0], [-1.0, 0.0]]

Hint 1

One program per row. Address xxx at offsets 2j2j2j and 2j+12j+12j+1 across the pair axis; the same column mask works for both halves.

Hint 2

Load cos⁡\coscos and sin⁡\sinsin from the row of the precomputed tables and reuse them for both output halves. No transcendentals are computed inside the kernel.

Hint 3

Form out_even=xeven⋅c−xodd⋅s\text{out\_even} = x_\text{even} \cdot c - x_\text{odd} \cdot sout_even=xeven​⋅c−xodd​⋅s and out_odd=xeven⋅s+xodd⋅c\text{out\_odd} = x_\text{even} \cdot s + x_\text{odd} \cdot cout_odd=xeven​⋅s+xodd​⋅c in registers, then store each at its own offset.

Requirements

  • Use one Triton program per token row; iterate over the D/2D/2D/2 pair axis inside the program
  • Apply the 2D rotation in registers using the loaded cos⁡\coscos and sin⁡\sinsin for the row
  • Mask the pair-axis tail so a D/2D/2D/2 not divisible by the block size is safe
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤N≤10241 \le N \le 10241≤N≤1024, DDD even with 2≤D≤2562 \le D \le 2562≤D≤256.
  • All tensors are torch.float32 on cuda.
  • Tolerance: atol=1e-3.
  • No batch, single head per call.
Try Similar Problems
Scaled Dot-Product Attention in TritonMediumOnline Softmax Kernel in TritonMediumKV Cache Append in TritonEasyFlashAttention Forward Kernel in TritonHardScaled Dot-Product Attention in CUDAHardRow-Wise LogSumExp in TritonMediumBlock-Pointer Matmul in TritonMediumSingle-Pass Mean and Variance in TritonMediumAutotuned Matrix Multiplication in TritonMediumTiled Transpose in TritonMediumLayerNorm Forward in TritonMediumFused Row-Wise Softmax in TritonMedium

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1