Coordinate mapping translates a computation’s logical position into the memory offsets needed to read and write data. It matters when an algorithm’s access pattern does not line up directly with the data layout, as in transposition, convolution, or paired-feature operations. section 3.5 section 3.7
In a Triton RoPE kernel, each program instance handles one sequence position and a vector of feature pairs. It calculates separate offsets for the paired input features and the cosine and sine values: the input row uses stride feature_dim, while the angle row uses stride feature_dim // 2. Masks protect partial feature blocks; a guard is also needed if the launch grid can exceed seq_len. section 3.5.3
Listing 3.3 Shared-frequency RoPE kernel (LLaMA-style)
@triton.jit
def rope_kernel(
x_ptr,
output_ptr,
cos_ptr,
sin_ptr,
seq_len,
feature_dim,
BLOCK_SIZE: tl.constexpr,
):
pid = tl.program_id(0) #1
half_dim = feature_dim // 2 #2
local_idx = tl.arange(0, BLOCK_SIZE)
mask = local_idx < half_dim
row_start = pid * feature_dim
first_offset = row_start + local_idx
second_offset = row_start + local_idx + half_dim #3
x1 = tl.load(x_ptr + first_offset, mask=mask, other=0.0)
x2 = tl.load(x_ptr + second_offset, mask=mask, other=0.0)
angle_row_start = pid * half_dim #4
angle_offset = angle_row_start + local_idx
cos_val = tl.load(cos_ptr + angle_offset, mask=mask, other=0.0) #5
sin_val = tl.load(sin_ptr + angle_offset, mask=mask, other=0.0)
out1 = x1 * cos_val - x2 * sin_val #6
out2 = x2 * cos_val + x1 * sin_val
tl.store(output_ptr + first_offset, out1, mask=mask)
tl.store(output_ptr + second_offset, out2, mask=mask)
Inspect the Triton kernel’s paired-feature offsets, angle offsets, masks, and rotation calculations. section 3.5.3
