|
| 1 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License") |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import unittest |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +import paddle |
| 20 | + |
| 21 | +from fastdeploy.model_executor.ops.gpu import draft_model_update |
| 22 | + |
| 23 | + |
| 24 | +def is_in_end(id, end_ids, length): |
| 25 | + flag = False |
| 26 | + for i in range(length): |
| 27 | + if id == end_ids[i]: |
| 28 | + return True |
| 29 | + return flag |
| 30 | + |
| 31 | + |
| 32 | +# recalculate data offset, offset_new is starting from index 0 |
| 33 | +def get_inter_next_tokens_start_offset(inter_next_tokens, max_seq_len, start_id, offset): |
| 34 | + offset_new = start_id + offset |
| 35 | + return inter_next_tokens[int(offset_new / max_seq_len)][int(offset_new % max_seq_len)] |
| 36 | + |
| 37 | + |
| 38 | +def draft_model_update_kernel( |
| 39 | + inter_next_tokens, |
| 40 | + draft_tokens, |
| 41 | + pre_ids, |
| 42 | + seq_lens_this_time, |
| 43 | + seq_lens_encoder, |
| 44 | + seq_lens_decoder, |
| 45 | + step_idx, |
| 46 | + output_cum_offsets, |
| 47 | + stop_flags, |
| 48 | + not_need_stop, |
| 49 | + max_dec_len, |
| 50 | + end_ids, |
| 51 | + base_model_draft_tokens, |
| 52 | + bsz, |
| 53 | + max_draft_token, |
| 54 | + pre_id_length, |
| 55 | + max_base_model_draft_token, |
| 56 | + end_ids_len, |
| 57 | + max_seq_len, |
| 58 | + substep, |
| 59 | + prefill_one_step_stop, |
| 60 | +): |
| 61 | + stop_sum = 0 |
| 62 | + for tid in range(bsz): |
| 63 | + stop_flag_now_int = 0 |
| 64 | + draft_token_now = draft_tokens[tid] |
| 65 | + pre_ids_now = pre_ids[tid] |
| 66 | + base_model_draft_tokens_now = base_model_draft_tokens[tid] |
| 67 | + next_tokens_start_id = tid * max_seq_len - output_cum_offsets[tid] |
| 68 | + # next_tokens_start = |
| 69 | + seq_len_this_time = seq_lens_this_time[tid] |
| 70 | + seq_len_encoder = seq_lens_encoder[tid] |
| 71 | + seq_len_decoder = seq_lens_decoder[tid] |
| 72 | + |
| 73 | + # 1. update step_idx && seq_lens_dec |
| 74 | + if not stop_flags[tid]: # seq_lens_decoder > 0 or seq_lens_encoder > 0 |
| 75 | + token_this_time = -1 |
| 76 | + # decoder step |
| 77 | + if seq_len_decoder > 0 and seq_len_encoder <= 0: |
| 78 | + seq_lens_decoder[tid] += seq_len_this_time |
| 79 | + token_this_time = get_inter_next_tokens_start_offset( |
| 80 | + inter_next_tokens, max_seq_len, next_tokens_start_id, seq_len_this_time - 1 |
| 81 | + ) |
| 82 | + draft_token_now[0] = token_this_time |
| 83 | + base_model_draft_tokens_now[substep + 1] = token_this_time |
| 84 | + step_idx[tid] += seq_len_this_time |
| 85 | + pre_ids_now[step_idx[tid]] = token_this_time |
| 86 | + else: |
| 87 | + token_this_time = get_inter_next_tokens_start_offset( |
| 88 | + inter_next_tokens, max_seq_len, next_tokens_start_id, 0 |
| 89 | + ) |
| 90 | + |
| 91 | + # seq_lens_decoder[tid] = seq_lens_encoder[tid] |
| 92 | + seq_lens_decoder[tid] = seq_len_encoder + seq_len_decoder |
| 93 | + seq_lens_encoder[tid] = 0 |
| 94 | + pre_ids_now[1] = token_this_time |
| 95 | + step_idx[tid] += 1 |
| 96 | + draft_token_now[0] = token_this_time |
| 97 | + base_model_draft_tokens_now[substep + 1] = token_this_time |
| 98 | + |
| 99 | + # multi_end |
| 100 | + if is_in_end(token_this_time, end_ids, end_ids_len) or prefill_one_step_stop: |
| 101 | + stop_flags[tid] = True |
| 102 | + stop_flag_now_int = 1 |
| 103 | + # max_dec_len |
| 104 | + elif step_idx[tid] >= max_dec_len[tid]: |
| 105 | + stop_flags[tid] = True |
| 106 | + draft_token_now[seq_len_this_time - 1] = end_ids[0] |
| 107 | + base_model_draft_tokens_now[substep + 1] = end_ids[0] |
| 108 | + stop_flag_now_int = 1 |
| 109 | + else: |
| 110 | + draft_token_now[0] = -1 |
| 111 | + base_model_draft_tokens_now[substep + 1] = -1 |
| 112 | + stop_flag_now_int = 1 |
| 113 | + |
| 114 | + # 2. set end |
| 115 | + if not stop_flags[tid]: |
| 116 | + seq_lens_this_time[tid] = 1 |
| 117 | + else: |
| 118 | + seq_lens_this_time[tid] = 0 |
| 119 | + seq_lens_encoder[tid] = 0 |
| 120 | + |
| 121 | + stop_sum = stop_sum + stop_flag_now_int |
| 122 | + not_need_stop[0] = stop_sum < bsz |
| 123 | + |
| 124 | + |
| 125 | +def draft_model_update_ref( |
| 126 | + inter_next_tokens, |
| 127 | + draft_tokens, |
| 128 | + pre_ids, |
| 129 | + seq_lens_this_time, |
| 130 | + seq_lens_encoder, |
| 131 | + seq_lens_decoder, |
| 132 | + step_idx, |
| 133 | + output_cum_offsets, |
| 134 | + stop_flags, |
| 135 | + not_need_stop, |
| 136 | + max_dec_len, |
| 137 | + end_ids, |
| 138 | + base_model_draft_tokens, |
| 139 | + max_seq_len, |
| 140 | + substep, |
| 141 | +): |
| 142 | + seq_lens_this_time_shape = seq_lens_this_time.shape |
| 143 | + real_bsz = seq_lens_this_time_shape[0] |
| 144 | + end_ids_len = end_ids.shape[0] |
| 145 | + max_draft_token = draft_tokens.shape[1] |
| 146 | + pre_id_length = pre_ids.shape[1] |
| 147 | + max_base_model_draft_token = base_model_draft_tokens.shape[1] |
| 148 | + |
| 149 | + prefill_one_step_stop = False |
| 150 | + import os |
| 151 | + |
| 152 | + env = os.getenv("PREFILL_NODE_ONE_STEP_STOP") |
| 153 | + if env == "1": |
| 154 | + prefill_one_step_stop = True |
| 155 | + |
| 156 | + draft_model_update_kernel( |
| 157 | + inter_next_tokens, |
| 158 | + draft_tokens, |
| 159 | + pre_ids, |
| 160 | + seq_lens_this_time, |
| 161 | + seq_lens_encoder, |
| 162 | + seq_lens_decoder, |
| 163 | + step_idx, |
| 164 | + output_cum_offsets, |
| 165 | + stop_flags, |
| 166 | + not_need_stop, |
| 167 | + max_dec_len, |
| 168 | + end_ids, |
| 169 | + base_model_draft_tokens, |
| 170 | + real_bsz, |
| 171 | + max_draft_token, |
| 172 | + pre_id_length, |
| 173 | + max_base_model_draft_token, |
| 174 | + end_ids_len, |
| 175 | + max_seq_len, |
| 176 | + substep, |
| 177 | + prefill_one_step_stop, |
| 178 | + ) |
| 179 | + |
| 180 | + |
| 181 | +class TestDraftModelUpdate(unittest.TestCase): |
| 182 | + def test_draft_model_update(self): |
| 183 | + self._run_paddle_test() |
| 184 | + |
| 185 | + def _run_paddle_test(self): |
| 186 | + np.random.seed(42) |
| 187 | + paddle.seed(42) |
| 188 | + |
| 189 | + max_bsz = 128 |
| 190 | + max_draft_token = 3 |
| 191 | + pre_id_length = 3 |
| 192 | + max_seq_len = 100 |
| 193 | + max_base_model_draft_token = 4 |
| 194 | + substep = 2 |
| 195 | + |
| 196 | + inter_next_tokens = paddle.randint(1, 100, shape=(max_bsz, max_seq_len), dtype="int64") |
| 197 | + draft_tokens = paddle.randint(1, 100, shape=(max_bsz, max_draft_token), dtype="int64") |
| 198 | + pre_ids = paddle.randint(1, 100, shape=(max_bsz, pre_id_length), dtype="int64") |
| 199 | + seq_lens_this_time = paddle.randint(1, 2, shape=(max_bsz,), dtype="int32") |
| 200 | + seq_lens_encoder = paddle.randint(1, 10, shape=(max_bsz,), dtype="int32") |
| 201 | + seq_lens_decoder = paddle.randint(1, 10, shape=(max_bsz,), dtype="int32") |
| 202 | + step_idx = paddle.randint(1, 10, shape=(max_bsz,), dtype="int64") |
| 203 | + output_cum_offsets = paddle.randint(0, 2, shape=(max_bsz,), dtype="int32") |
| 204 | + output_cum_offsets[0] = 0 |
| 205 | + stop_flags = paddle.zeros([max_bsz], dtype="bool") |
| 206 | + not_need_stop = paddle.zeros([1], dtype="bool").to(device=paddle.CPUPlace()) |
| 207 | + max_dec_len = paddle.randint(100, 102, shape=(max_bsz,), dtype="int64") |
| 208 | + end_ids = paddle.to_tensor([2], dtype="int64") |
| 209 | + base_model_draft_tokens = paddle.randint(1, 10, shape=(max_bsz, max_base_model_draft_token), dtype="int64") |
| 210 | + |
| 211 | + inputs = ( |
| 212 | + inter_next_tokens, |
| 213 | + draft_tokens, |
| 214 | + pre_ids, |
| 215 | + seq_lens_this_time, |
| 216 | + seq_lens_encoder, |
| 217 | + seq_lens_decoder, |
| 218 | + step_idx, |
| 219 | + output_cum_offsets, |
| 220 | + stop_flags, |
| 221 | + not_need_stop, |
| 222 | + max_dec_len, |
| 223 | + end_ids, |
| 224 | + base_model_draft_tokens, |
| 225 | + max_seq_len, |
| 226 | + substep, |
| 227 | + ) |
| 228 | + # inplace modify, need to clone inputs |
| 229 | + inputs_clone = [x.clone() if isinstance(x, paddle.Tensor) else x for x in inputs] |
| 230 | + draft_model_update(*inputs) |
| 231 | + draft_model_update_ref(*inputs_clone) |
| 232 | + idx_list = ( |
| 233 | + 1, |
| 234 | + 2, |
| 235 | + 3, |
| 236 | + 4, |
| 237 | + 5, |
| 238 | + 6, |
| 239 | + 8, |
| 240 | + 9, |
| 241 | + 12, |
| 242 | + ) |
| 243 | + for i in idx_list: |
| 244 | + np.testing.assert_allclose(inputs[i].numpy(), inputs_clone[i].numpy()) |
| 245 | + |
| 246 | + |
| 247 | +if __name__ == "__main__": |
| 248 | + unittest.main() |
0 commit comments