|
| 1 | +// Copyright (c) 2024 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 | +#include "speculate_step_helper.h" |
| 16 | + |
| 17 | +void SpeculateStepPaddleBase( |
| 18 | + const paddle::Tensor &stop_flags, |
| 19 | + const paddle::Tensor &seq_lens_this_time, |
| 20 | + const paddle::Tensor &ori_seq_lens_encoder, |
| 21 | + const paddle::optional<paddle::Tensor> &ori_seq_lens_decoder, |
| 22 | + const paddle::Tensor &seq_lens_encoder, |
| 23 | + const paddle::Tensor &seq_lens_decoder, |
| 24 | + const paddle::Tensor &block_tables, // [bsz, block_num_per_seq] |
| 25 | + const paddle::Tensor &encoder_block_lens, |
| 26 | + const paddle::Tensor &is_block_step, |
| 27 | + const paddle::Tensor &step_block_list, |
| 28 | + const paddle::Tensor &step_lens, |
| 29 | + const paddle::Tensor &recover_block_list, |
| 30 | + const paddle::Tensor &recover_lens, |
| 31 | + const paddle::Tensor &need_block_list, |
| 32 | + const paddle::Tensor &need_block_len, |
| 33 | + const paddle::Tensor &used_list_len, |
| 34 | + const paddle::Tensor &free_list, |
| 35 | + const paddle::Tensor &free_list_len, |
| 36 | + const paddle::Tensor &input_ids, |
| 37 | + const paddle::Tensor &pre_ids, |
| 38 | + const paddle::Tensor &step_idx, |
| 39 | + const paddle::Tensor &next_tokens, |
| 40 | + const paddle::Tensor &first_token_ids, |
| 41 | + const paddle::Tensor &accept_num, |
| 42 | + const int block_size, |
| 43 | + const int encoder_decoder_block_num, |
| 44 | + const int max_draft_tokens) { |
| 45 | + namespace api = baidu::xpu::api; |
| 46 | + phi::XPUPlace place(phi::backends::xpu::GetXPUCurrentDeviceId()); |
| 47 | + auto dev_ctx = paddle::experimental::DeviceContextPool::Instance().Get(place); |
| 48 | + auto xpu_ctx = static_cast<const phi::XPUContext *>(dev_ctx); |
| 49 | + api::Context *ctx = xpu_ctx->x_context(); |
| 50 | + if (seq_lens_this_time.is_cpu()) { |
| 51 | + ctx = new api::Context(api::kCPU); |
| 52 | + } |
| 53 | + const int bsz = seq_lens_this_time.shape()[0]; |
| 54 | + PADDLE_ENFORCE_LE( |
| 55 | + bsz, |
| 56 | + 640, |
| 57 | + phi::errors::InvalidArgument( |
| 58 | + "Only support bsz <= 640, but received bsz is %d", bsz)); |
| 59 | + const int block_num_per_seq = block_tables.shape()[1]; |
| 60 | + const int length = input_ids.shape()[1]; |
| 61 | + const int pre_id_length = pre_ids.shape()[1]; |
| 62 | + const int max_decoder_block_num = pre_id_length / block_size; |
| 63 | + int r = baidu::xpu::api::plugin::speculate_free_and_dispatch_block( |
| 64 | + ctx, |
| 65 | + const_cast<bool *>(stop_flags.data<bool>()), |
| 66 | + const_cast<int *>(seq_lens_this_time.data<int>()), |
| 67 | + const_cast<int *>(seq_lens_decoder.data<int>()), |
| 68 | + const_cast<int *>(block_tables.data<int>()), |
| 69 | + const_cast<int *>(encoder_block_lens.data<int>()), |
| 70 | + const_cast<bool *>(is_block_step.data<bool>()), |
| 71 | + const_cast<int *>(step_block_list.data<int>()), |
| 72 | + const_cast<int *>(step_lens.data<int>()), |
| 73 | + const_cast<int *>(recover_block_list.data<int>()), |
| 74 | + const_cast<int *>(recover_lens.data<int>()), |
| 75 | + const_cast<int *>(need_block_list.data<int>()), |
| 76 | + const_cast<int *>(need_block_len.data<int>()), |
| 77 | + const_cast<int *>(used_list_len.data<int>()), |
| 78 | + const_cast<int *>(free_list.data<int>()), |
| 79 | + const_cast<int *>(free_list_len.data<int>()), |
| 80 | + const_cast<int64_t *>(first_token_ids.data<int64_t>()), |
| 81 | + const_cast<int *>(accept_num.data<int>()), |
| 82 | + bsz, |
| 83 | + block_size, |
| 84 | + block_num_per_seq, |
| 85 | + max_decoder_block_num, |
| 86 | + max_draft_tokens); |
| 87 | + PD_CHECK(r == 0, "speculate_free_and_dispatch_block failed."); |
| 88 | + auto recover_lens_cpu = recover_lens.copy_to(paddle::CPUPlace(), false); |
| 89 | + int recover_lens_cpu_data = recover_lens_cpu.data<int>()[0]; |
| 90 | + if (recover_lens_cpu_data > 0) { |
| 91 | + r = baidu::xpu::api::plugin::speculate_recover_block( |
| 92 | + ctx, |
| 93 | + const_cast<int *>(recover_block_list.data<int>()), |
| 94 | + const_cast<int *>(recover_lens.data<int>()), |
| 95 | + const_cast<bool *>(stop_flags.data<bool>()), |
| 96 | + const_cast<int *>(seq_lens_this_time.data<int>()), |
| 97 | + ori_seq_lens_encoder.data<int>(), |
| 98 | + ori_seq_lens_decoder ? ori_seq_lens_decoder.get_ptr()->data<int>() |
| 99 | + : nullptr, |
| 100 | + const_cast<int *>(seq_lens_encoder.data<int>()), |
| 101 | + const_cast<int *>(seq_lens_decoder.data<int>()), |
| 102 | + const_cast<int *>(block_tables.data<int>()), |
| 103 | + const_cast<int *>(free_list.data<int>()), |
| 104 | + const_cast<int *>(free_list_len.data<int>()), |
| 105 | + const_cast<int64_t *>(input_ids.data<int64_t>()), |
| 106 | + pre_ids.data<int64_t>(), |
| 107 | + step_idx.data<int64_t>(), |
| 108 | + encoder_block_lens.data<int>(), |
| 109 | + used_list_len.data<int>(), |
| 110 | + next_tokens.data<int64_t>(), |
| 111 | + first_token_ids.data<int64_t>(), |
| 112 | + bsz, |
| 113 | + block_num_per_seq, |
| 114 | + length, |
| 115 | + pre_id_length); |
| 116 | + PD_CHECK(r == 0, "speculate_recover_block failed."); |
| 117 | + } |
| 118 | +} |
0 commit comments