|
| 1 | +# Adapted from ModelTC https://github.com/ModelTC/lightllm |
| 2 | +import torch |
| 3 | +import triton |
| 4 | +import triton.language as tl |
| 5 | + |
| 6 | + |
| 7 | +@triton.jit |
| 8 | +def _rotary_kernel( |
| 9 | + q, |
| 10 | + input_scale, |
| 11 | + output_scale, |
| 12 | + Cos, |
| 13 | + Sin, |
| 14 | + q_bs_stride, |
| 15 | + q_h_stride, |
| 16 | + q_d_stride, |
| 17 | + cos_bs_stride, |
| 18 | + cos_d_stride, |
| 19 | + total_len, |
| 20 | + HEAD_NUM: tl.constexpr, |
| 21 | + BLOCK_HEAD: tl.constexpr, |
| 22 | + BLOCK_SEQ: tl.constexpr, |
| 23 | + HEAD_DIM: tl.constexpr, |
| 24 | +): |
| 25 | + current_head_index = tl.program_id(0) |
| 26 | + current_seq_index = tl.program_id(1) |
| 27 | + |
| 28 | + dim_range0 = tl.arange(0, HEAD_DIM // 2) |
| 29 | + dim_range1 = tl.arange(HEAD_DIM // 2, HEAD_DIM) |
| 30 | + |
| 31 | + current_head_range = current_head_index * BLOCK_HEAD + tl.arange(0, BLOCK_HEAD) |
| 32 | + current_seq_range = current_seq_index * BLOCK_SEQ + tl.arange(0, BLOCK_SEQ) |
| 33 | + |
| 34 | + off_q0 = ( |
| 35 | + current_seq_range[:, None, None] * q_bs_stride |
| 36 | + + current_head_range[None, :, None] * q_h_stride |
| 37 | + + dim_range0[None, None, :] * q_d_stride |
| 38 | + ) |
| 39 | + off_q1 = ( |
| 40 | + current_seq_range[:, None, None] * q_bs_stride |
| 41 | + + current_head_range[None, :, None] * q_h_stride |
| 42 | + + dim_range1[None, None, :] * q_d_stride |
| 43 | + ) |
| 44 | + |
| 45 | + off_dimcos_sin = current_seq_range[:, None, None] * cos_bs_stride + dim_range0[None, None, :] * cos_d_stride |
| 46 | + |
| 47 | + q0 = tl.load( |
| 48 | + q + off_q0, |
| 49 | + mask=(current_seq_range[:, None, None] < total_len) & (current_head_range[None, :, None] < HEAD_NUM), |
| 50 | + other=0.0, |
| 51 | + ) |
| 52 | + q1 = tl.load( |
| 53 | + q + off_q1, |
| 54 | + mask=(current_seq_range[:, None, None] < total_len) & (current_head_range[None, :, None] < HEAD_NUM), |
| 55 | + other=0.0, |
| 56 | + ) |
| 57 | + |
| 58 | + cos = tl.load(Cos + off_dimcos_sin, mask=current_seq_range[:, None, None] < total_len, other=0.0) |
| 59 | + sin = tl.load(Sin + off_dimcos_sin, mask=current_seq_range[:, None, None] < total_len, other=0.0) |
| 60 | + in_scale = tl.load(input_scale) |
| 61 | + o_scale = tl.load(output_scale) |
| 62 | + |
| 63 | + q0 = q0.to(tl.float32) * in_scale |
| 64 | + q1 = q1.to(tl.float32) * in_scale |
| 65 | + |
| 66 | + out0 = (q0 * cos - q1 * sin) / o_scale |
| 67 | + out1 = (q0 * sin + q1 * cos) / o_scale |
| 68 | + |
| 69 | + # out0 = out0.to(tl.int8) |
| 70 | + # out1 = out1.to(tl.int8) |
| 71 | + |
| 72 | + tl.store( |
| 73 | + q + off_q0, |
| 74 | + out0, |
| 75 | + mask=(current_seq_range[:, None, None] < total_len) & (current_head_range[None, :, None] < HEAD_NUM), |
| 76 | + ) |
| 77 | + tl.store( |
| 78 | + q + off_q1, |
| 79 | + out1, |
| 80 | + mask=(current_seq_range[:, None, None] < total_len) & (current_head_range[None, :, None] < HEAD_NUM), |
| 81 | + ) |
| 82 | + |
| 83 | + return |
| 84 | + |
| 85 | + |
| 86 | +@torch.no_grad() |
| 87 | +def int8_rotary_embedding_fwd(q, cos, sin, input_scale, output_scale): |
| 88 | + total_len = q.shape[0] |
| 89 | + head_num = q.shape[1] |
| 90 | + head_dim = q.shape[2] |
| 91 | + assert q.shape[0] == cos.shape[0] and q.shape[0] == sin.shape[0], f"q shape {q.shape} cos shape {cos.shape}" |
| 92 | + BLOCK_HEAD = 4 |
| 93 | + BLOCK_SEQ = 32 |
| 94 | + grid = (triton.cdiv(head_num, BLOCK_HEAD), triton.cdiv(total_len, BLOCK_SEQ)) |
| 95 | + if head_dim >= 128: |
| 96 | + num_warps = 8 |
| 97 | + else: |
| 98 | + num_warps = 4 |
| 99 | + |
| 100 | + _rotary_kernel[grid]( |
| 101 | + q, |
| 102 | + input_scale, |
| 103 | + output_scale, |
| 104 | + cos, |
| 105 | + sin, |
| 106 | + q.stride(0), |
| 107 | + q.stride(1), |
| 108 | + q.stride(2), |
| 109 | + cos.stride(0), |
| 110 | + cos.stride(1), |
| 111 | + total_len, |
| 112 | + HEAD_NUM=head_num, |
| 113 | + BLOCK_HEAD=BLOCK_HEAD, |
| 114 | + BLOCK_SEQ=BLOCK_SEQ, |
| 115 | + HEAD_DIM=head_dim, |
| 116 | + num_warps=num_warps, |
| 117 | + num_stages=1, |
| 118 | + ) |
| 119 | + return |
0 commit comments