|
| 1 | +import torch |
| 2 | + |
| 3 | +import triton |
| 4 | +import triton.language as tl |
| 5 | + |
| 6 | + |
| 7 | +@triton.jit |
| 8 | +def _fwd_kernel_scatter( |
| 9 | + next_token_ids, |
| 10 | + req_to_next_token_ids, |
| 11 | + b_req_idx, |
| 12 | + b_mtp_index, |
| 13 | + b_has_out, |
| 14 | + req_to_next_token_ids_stride, |
| 15 | + req_to_next_token_ids_stride_1, |
| 16 | + num_size, |
| 17 | + HAS_OUT_IS_NONE: tl.constexpr, |
| 18 | + BLOCK: tl.constexpr, |
| 19 | +): |
| 20 | + block_index = tl.program_id(0) |
| 21 | + block_range = block_index * BLOCK + tl.arange(0, BLOCK) |
| 22 | + block_mask = block_range < num_size |
| 23 | + |
| 24 | + cur_req_idx = tl.load(b_req_idx + block_range, mask=block_mask) |
| 25 | + cur_mtp_index = tl.load(b_mtp_index + block_range, mask=block_mask) |
| 26 | + cur_next_token_id = tl.load(next_token_ids + block_range, mask=block_mask) |
| 27 | + |
| 28 | + if not HAS_OUT_IS_NONE: |
| 29 | + cur_has_out = tl.load(b_has_out + block_range, mask=block_mask, other=False) |
| 30 | + tl.store( |
| 31 | + req_to_next_token_ids + cur_req_idx * req_to_next_token_ids_stride + cur_mtp_index, |
| 32 | + cur_next_token_id, |
| 33 | + mask=cur_has_out & block_mask, |
| 34 | + ) |
| 35 | + else: |
| 36 | + tl.store( |
| 37 | + req_to_next_token_ids + cur_req_idx * req_to_next_token_ids_stride + cur_mtp_index, |
| 38 | + cur_next_token_id, |
| 39 | + mask=block_mask, |
| 40 | + ) |
| 41 | + |
| 42 | + return |
| 43 | + |
| 44 | + |
| 45 | +@torch.no_grad() |
| 46 | +def scatter_token( |
| 47 | + next_token_ids: torch.Tensor, |
| 48 | + req_to_next_token_ids: torch.Tensor, |
| 49 | + b_req_idx: torch.Tensor, |
| 50 | + b_mtp_index: torch.Tensor, |
| 51 | + b_has_out: torch.Tensor = None, |
| 52 | +): |
| 53 | + """ |
| 54 | + This function is used to scatter the token_info(GPU tensor) to the req_to_token_info(CPU tensor). |
| 55 | + Args: |
| 56 | + next_token_ids: (batch_size,) |
| 57 | + req_to_next_token_ids: (max_req_num, max_mtp_step) |
| 58 | + b_req_idx: (batch_size,) |
| 59 | + b_mtp_index: (batch_size,) |
| 60 | + """ |
| 61 | + assert next_token_ids.shape[0] == b_req_idx.shape[0] |
| 62 | + batch_size = b_req_idx.shape[0] |
| 63 | + BLOCK = 256 |
| 64 | + |
| 65 | + grid = (triton.cdiv(batch_size, BLOCK),) |
| 66 | + num_warps = 1 |
| 67 | + |
| 68 | + _fwd_kernel_scatter[grid]( |
| 69 | + next_token_ids=next_token_ids, |
| 70 | + req_to_next_token_ids=req_to_next_token_ids, |
| 71 | + b_req_idx=b_req_idx, |
| 72 | + b_mtp_index=b_mtp_index, |
| 73 | + b_has_out=b_has_out, |
| 74 | + req_to_next_token_ids_stride=req_to_next_token_ids.stride(0), |
| 75 | + req_to_next_token_ids_stride_1=req_to_next_token_ids.stride(1), |
| 76 | + num_size=batch_size, |
| 77 | + HAS_OUT_IS_NONE=b_has_out is None, |
| 78 | + BLOCK=BLOCK, |
| 79 | + num_warps=num_warps, |
| 80 | + num_stages=1, |
| 81 | + ) |
| 82 | + return |
| 83 | + |
| 84 | + |
| 85 | +@triton.jit |
| 86 | +def _fwd_kernel_gather( |
| 87 | + req_to_next_token_ids, |
| 88 | + req_to_next_token_ids_stride, |
| 89 | + req_to_next_token_ids_stride_1, |
| 90 | + output, |
| 91 | + b_req_idx, |
| 92 | + b_mtp_index, |
| 93 | + num_size, |
| 94 | + BLOCK: tl.constexpr, |
| 95 | +): |
| 96 | + block_index = tl.program_id(0) |
| 97 | + block_range = block_index * BLOCK + tl.arange(0, BLOCK) |
| 98 | + block_mask = block_range < num_size |
| 99 | + cur_req_idx = tl.load(b_req_idx + block_range, mask=block_mask) |
| 100 | + cur_mtp_index = tl.load(b_mtp_index + block_range, mask=block_mask) |
| 101 | + cur_next_token_id = tl.load( |
| 102 | + req_to_next_token_ids + cur_req_idx * req_to_next_token_ids_stride + cur_mtp_index, mask=block_mask |
| 103 | + ) |
| 104 | + tl.store(output + block_range, cur_next_token_id, mask=block_mask) |
| 105 | + return |
| 106 | + |
| 107 | + |
| 108 | +def gather_token(req_to_next_token_ids: torch.Tensor, b_req_idx: torch.Tensor, b_mtp_index: torch.Tensor): |
| 109 | + """ |
| 110 | + This function is used to gather the token_info(CPU tensor) to the token_info(GPU tensor). |
| 111 | + Args: |
| 112 | + req_to_token_info: (max_req_num, max_mtp_step) |
| 113 | + b_req_idx: (batch_size,) |
| 114 | + b_mtp_index: (batch_size,) |
| 115 | + Returns: |
| 116 | + output: (batch_size,) |
| 117 | + """ |
| 118 | + batch_size = b_req_idx.shape[0] |
| 119 | + output = torch.empty(batch_size, dtype=req_to_next_token_ids.dtype, device="cuda") |
| 120 | + BLOCK = 256 |
| 121 | + grid = (triton.cdiv(batch_size, BLOCK),) |
| 122 | + num_warps = 1 |
| 123 | + _fwd_kernel_gather[grid]( |
| 124 | + req_to_next_token_ids=req_to_next_token_ids, |
| 125 | + req_to_next_token_ids_stride=req_to_next_token_ids.stride(0), |
| 126 | + req_to_next_token_ids_stride_1=req_to_next_token_ids.stride(1), |
| 127 | + output=output, |
| 128 | + b_req_idx=b_req_idx, |
| 129 | + b_mtp_index=b_mtp_index, |
| 130 | + num_size=batch_size, |
| 131 | + BLOCK=BLOCK, |
| 132 | + num_warps=num_warps, |
| 133 | + num_stages=1, |
| 134 | + ) |
| 135 | + return output |
| 136 | + |
| 137 | + |
| 138 | +def test_scatter_token_to_cpu(): |
| 139 | + batch_size = 30 |
| 140 | + req_to_token_info = torch.zeros((1000, 1), dtype=torch.float32, pin_memory=True) |
| 141 | + token_info = torch.randn((batch_size,)).cuda() |
| 142 | + req_ids = torch.arange(20, 20 + batch_size, dtype=torch.int32).cuda() |
| 143 | + mtp_index = torch.zeros((batch_size,), dtype=torch.int32).cuda() |
| 144 | + scatter_token(token_info, req_to_token_info, req_ids, mtp_index) |
| 145 | + diff = (req_to_token_info[20 : 20 + batch_size].cuda().view(-1) - token_info).abs().max() |
| 146 | + assert diff < 1e-6 |
| 147 | + print("test_scatter_token_to_cpu passed") |
| 148 | + |
| 149 | + |
| 150 | +def test_gather_token(): |
| 151 | + batch_size = 30 |
| 152 | + req_to_token_info = torch.zeros((1000, 1), dtype=torch.float32, pin_memory=True) |
| 153 | + token_info = torch.randn((batch_size,)).cuda() |
| 154 | + req_ids = torch.arange(20, 20 + batch_size, dtype=torch.int32).cuda() |
| 155 | + mtp_index = torch.zeros((batch_size,), dtype=torch.int32).cuda() |
| 156 | + scatter_token(token_info, req_to_token_info, req_ids, mtp_index) |
| 157 | + output = gather_token(req_to_token_info, req_ids, mtp_index) |
| 158 | + diff = (token_info - output).abs().max() |
| 159 | + assert diff < 1e-6 |
| 160 | + print("test_gather_token passed") |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + test_scatter_token_to_cpu() |
| 165 | + test_gather_token() |
0 commit comments