Delete rotary.py
Browse files
rotary.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 1 |
-
# Copyright (c) 2023, Tri Dao.
|
| 2 |
-
|
| 3 |
-
from typing import Optional, Union
|
| 4 |
-
|
| 5 |
-
import torch
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
def apply_rotary_emb_torch(
|
| 9 |
-
x,
|
| 10 |
-
cos,
|
| 11 |
-
sin,
|
| 12 |
-
interleaved=False,
|
| 13 |
-
inplace=False,
|
| 14 |
-
seqlen_offsets=0,
|
| 15 |
-
cu_seqlens=None,
|
| 16 |
-
max_seqlen=None,
|
| 17 |
-
):
|
| 18 |
-
# Only supports the basic (not interleaved, not variable-length) case.
|
| 19 |
-
rotary_dim = cos.shape[1] * 2
|
| 20 |
-
x1 = x[..., :rotary_dim]
|
| 21 |
-
x2 = x[..., rotary_dim:]
|
| 22 |
-
|
| 23 |
-
# Split [even, odd] pairs
|
| 24 |
-
x1_1, x1_2 = x1[..., ::2], x1[..., 1::2] # (..., rotary_dim/2)
|
| 25 |
-
|
| 26 |
-
# Reshape cos/sin for broadcasting
|
| 27 |
-
# x: [batch, seqlen, nheads, rotary_dim]
|
| 28 |
-
# cos/sin: [seqlen, rotary_dim/2]
|
| 29 |
-
# reshape to [1, seqlen, 1, rotary_dim/2] to broadcast
|
| 30 |
-
cos = cos.unsqueeze(0).unsqueeze(2)
|
| 31 |
-
sin = sin.unsqueeze(0).unsqueeze(2)
|
| 32 |
-
|
| 33 |
-
rot_x1 = x1_1 * cos - x1_2 * sin
|
| 34 |
-
rot_x2 = x1_1 * sin + x1_2 * cos
|
| 35 |
-
# Interleave last dimension: (..., rotary_dim/2, 2) -> (..., rotary_dim)
|
| 36 |
-
rot_x = torch.stack([rot_x1, rot_x2], dim=-1).reshape_as(x1)
|
| 37 |
-
out = torch.cat([rot_x, x2], dim=-1)
|
| 38 |
-
return out
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def apply_rotary_emb(
|
| 42 |
-
x,
|
| 43 |
-
cos,
|
| 44 |
-
sin,
|
| 45 |
-
interleaved=False,
|
| 46 |
-
inplace=False,
|
| 47 |
-
seqlen_offsets: Union[int, torch.Tensor] = 0,
|
| 48 |
-
cu_seqlens: Optional[torch.Tensor] = None,
|
| 49 |
-
max_seqlen: Optional[int] = None,
|
| 50 |
-
):
|
| 51 |
-
"""
|
| 52 |
-
Arguments:
|
| 53 |
-
x: (batch_size, seqlen, nheads, headdim) if cu_seqlens is None
|
| 54 |
-
else (total_seqlen, nheads, headdim)
|
| 55 |
-
cos, sin: (seqlen_rotary, rotary_dim / 2)
|
| 56 |
-
interleaved: if True, rotate pairs of even and odd dimensions (GPT-J style) instead
|
| 57 |
-
of 1st half and 2nd half (GPT-NeoX style).
|
| 58 |
-
inplace: if True, apply rotary embedding in-place.
|
| 59 |
-
seqlen_offsets: (batch_size,) or int. Each sequence in x is shifted by this amount.
|
| 60 |
-
Most commonly used in inference when we have KV cache.
|
| 61 |
-
cu_seqlens: (batch + 1,) or None
|
| 62 |
-
max_seqlen: int
|
| 63 |
-
Return:
|
| 64 |
-
out: (batch_size, seqlen, nheads, headdim) if cu_seqlens is None
|
| 65 |
-
else (total_seqlen, nheads, headdim)
|
| 66 |
-
rotary_dim must be <= headdim
|
| 67 |
-
Apply rotary embedding to the first rotary_dim of x.
|
| 68 |
-
"""
|
| 69 |
-
# We are forcing the use of the pure PyTorch implementation (`apply_rotary_emb_torch`)
|
| 70 |
-
# for all devices. The custom Triton kernel (`ApplyRotaryEmb`) was causing a graph
|
| 71 |
-
# break in `torch.compile`, pushing expensive operations to the CPU.
|
| 72 |
-
# By using the pure PyTorch version, `torch.compile` can create a single, fully-optimized
|
| 73 |
-
# graph, which should resolve the CPU bottleneck and improve GPU utilization.
|
| 74 |
-
return apply_rotary_emb_torch(
|
| 75 |
-
x, cos, sin, interleaved, inplace, seqlen_offsets, cu_seqlens, max_seqlen
|
| 76 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|