| 287 | physical = offsets // (heads * dim) |
| 288 | head = offsets // dim % heads |
| 289 | column = offsets % dim |
| 290 | start, sink, recent = tl.load(metadata), tl.load(metadata + 1), tl.load(metadata + 2) |
| 291 | logical = tl.where(physical < sink, physical, sink + (physical - sink - recent + capacity - sink) % (capacity - sink)) |
| 292 | if split_half: |
| 293 | partner = tl.where(column < rotary_dim // 2, column + rotary_dim // 2, column - rotary_dim // 2) |
| 294 | frequency = column % (rotary_dim // 2) |
| 295 | first = column < rotary_dim // 2 |
| 296 | else: |
| 297 | partner = column ^ 1 |
| 298 | frequency = column // 2 |
| 299 | first = column % 2 == 0 |
| 300 | rotate = valid & (logical < visible) & (column < rotary_dim) |
| 301 | cosine = tl.load(freqs + logical * rotary_dim + frequency, rotate, 0).to(tl.float32) |
| 302 | sine = tl.load(freqs + logical * rotary_dim + rotary_dim // 2 + frequency, rotate, 0).to(tl.float32) |
| 303 | key = tl.load(raw_key + offsets, valid, 0).to(tl.float32) |
| 304 | other = tl.load(raw_key + (physical * heads + head) * dim + partner, rotate, 0).to(tl.float32) |
| 305 | if rope_fma: |
| 306 | rotated = tl.where(first, tl.fma(key, cosine, -other * sine), tl.fma(key, cosine, other * sine)) |
| 307 | else: |
| 308 | rotated = tl.where(first, key * cosine - other * sine, key * cosine + other * sine) |
| 309 | tl.store(rotated_key + offsets, tl.where(column < rotary_dim, rotated, key), valid) |
| 310 | query_token = logical - start |
| 311 | current = valid & (logical < visible) & (query_token >= 0) & (query_token < query_tokens) |
| 312 | address = query_token * query_token_stride + head * query_head_stride |
| 313 | q = tl.load(query + address + column * query_dim_stride, current, 0).to(tl.float32) |
| 314 | q_other = tl.load(query + address + partner * query_dim_stride, current & (column < rotary_dim), 0).to(tl.float32) |
| 315 | if rope_fma: |
| 316 | q_rotated = tl.where(first, tl.fma(q, cosine, -q_other * sine), tl.fma(q, cosine, q_other * sine)) |
| 317 | else: |
| 318 | q_rotated = tl.where(first, q * cosine - q_other * sine, q * cosine + q_other * sine) |
| 319 | tl.store(query + address + column * query_dim_stride, tl.where(column < rotary_dim, q_rotated, q), current) |
| 320 | |
| 321 | |
| 322 | def rotate_ring_qk( |
| 323 | query: torch.Tensor, |
| 324 | raw_key: torch.Tensor, |
| 325 | rotated_key: torch.Tensor, |
| 326 | freqs: torch.Tensor, |
| 327 | metadata: torch.Tensor, |
| 328 | *, |
| 329 | rotary_dim=None, |
| 330 | rope_layout="interleaved", |
| 331 | rope_fma=True, |
| 332 | ) -> None: |
| 333 | """Rotate Q and physical-order K using each slot's current logical position.""" |
| 334 | if torch.compiler.is_compiling(): |
| 335 | _rotate_ring_qk_op(query, raw_key, rotated_key, freqs, metadata, rotary_dim or raw_key.shape[-1], rope_layout == "split_half", rope_fma) |
| 336 | return |
| 337 | rotary_dim = rotary_dim or raw_key.shape[-1] |
| 338 | if rotary_dim != raw_key.shape[-1] or rope_layout == "split_half" or not rope_fma: |
| 339 | # Split-half partners can live in different warps. Produce Q out of |
| 340 | # place before copying back, so one warp cannot overwrite another |
| 341 | # warp's input while it is still reading its rotation partner. |
| 342 | if query.numel(): |
| 343 | query.copy_(rotate_token_rope(query, freqs, metadata, rotary_dim=rotary_dim, split_half=rope_layout == "split_half", rope_fma=rope_fma)) |
| 344 | _rotate_ring_qk_partial[(triton.cdiv(rotated_key.numel(), 512),)]( |
| 345 | query, |
| 346 | raw_key, |