|
| 1 | +// Copyright (c) 2026 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 <paddle/phi/backends/xpu/xpu_context.h> |
| 16 | +#include "paddle/extension.h" |
| 17 | +#include "xpu/plugin.h" |
| 18 | + |
| 19 | +#ifndef PD_BUILD_STATIC_OP |
| 20 | +#define PD_BUILD_STATIC_OP(name) PD_BUILD_OP(static_op_##name) |
| 21 | +#endif |
| 22 | + |
| 23 | +namespace api = baidu::xpu::api; |
| 24 | + |
| 25 | +std::vector<paddle::Tensor> SpeculatePreProcess( |
| 26 | + const int64_t cpu_token_num, |
| 27 | + const paddle::Tensor &input_ids, |
| 28 | + const paddle::Tensor &seq_len, |
| 29 | + const paddle::Tensor &draft_tokens, |
| 30 | + const paddle::Tensor &seq_lens_encoder, |
| 31 | + const paddle::Tensor &seq_lens_decoder) { |
| 32 | + phi::XPUPlace place(phi::backends::xpu::GetXPUCurrentDeviceId()); |
| 33 | + auto dev_ctx = paddle::experimental::DeviceContextPool::Instance().Get(place); |
| 34 | + auto xpu_ctx = static_cast<const phi::XPUContext *>(dev_ctx); |
| 35 | + api::Context *ctx = xpu_ctx->x_context(); |
| 36 | + |
| 37 | + // just for ut to run base line |
| 38 | + std::unique_ptr<baidu::xpu::api::Context> cpu_ctx; |
| 39 | + if (input_ids.place().GetType() == phi::AllocationType::CPU) { |
| 40 | + cpu_ctx = std::make_unique<baidu::xpu::api::Context>(baidu::xpu::api::kCPU); |
| 41 | + ctx = cpu_ctx.get(); |
| 42 | + } |
| 43 | + |
| 44 | + std::vector<int64_t> input_ids_shape = input_ids.shape(); |
| 45 | + const int bsz = seq_len.shape()[0]; |
| 46 | + const int max_seq_len = input_ids_shape[1]; |
| 47 | + const int token_num_data = cpu_token_num; |
| 48 | + auto ids_remove_padding = paddle::empty( |
| 49 | + {token_num_data}, paddle::DataType::INT64, input_ids.place()); |
| 50 | + auto batch_id_per_token = paddle::empty( |
| 51 | + {token_num_data}, paddle::DataType::INT32, input_ids.place()); |
| 52 | + auto cu_seqlens_q = |
| 53 | + paddle::empty({bsz + 1}, paddle::DataType::INT32, input_ids.place()); |
| 54 | + auto cu_seqlens_k = |
| 55 | + paddle::empty({bsz + 1}, paddle::DataType::INT32, input_ids.place()); |
| 56 | + const int max_draft_tokens_per_batch = draft_tokens.shape()[1]; |
| 57 | + |
| 58 | + auto seq_lens_output = |
| 59 | + paddle::empty({bsz}, paddle::DataType::INT32, input_ids.place()); |
| 60 | + auto cu_seq_lens_q_output = |
| 61 | + paddle::empty({bsz + 1}, paddle::DataType::INT32, input_ids.place()); |
| 62 | + auto batch_id_per_token_output = |
| 63 | + paddle::empty({bsz * max_draft_tokens_per_batch}, |
| 64 | + paddle::DataType::INT32, |
| 65 | + input_ids.place()); |
| 66 | + auto real_output_token_num = |
| 67 | + paddle::empty({1}, paddle::DataType::INT32, input_ids.place()); |
| 68 | + if (token_num_data == 0) { |
| 69 | + return {ids_remove_padding, |
| 70 | + batch_id_per_token, |
| 71 | + cu_seqlens_q, |
| 72 | + cu_seqlens_k, |
| 73 | + cu_seq_lens_q_output, |
| 74 | + batch_id_per_token_output, |
| 75 | + real_output_token_num}; |
| 76 | + } |
| 77 | + |
| 78 | + int64_t *ids_remove_padding_ptr = ids_remove_padding.data<int64_t>(); |
| 79 | + int *batch_id_per_token_ptr = batch_id_per_token.data<int>(); |
| 80 | + int *cu_seqlens_q_ptr = cu_seqlens_q.data<int>(); |
| 81 | + int *cu_seqlens_k_ptr = cu_seqlens_k.data<int>(); |
| 82 | + int *seq_lens_output_ptr = seq_lens_output.data<int>(); |
| 83 | + int *cu_seq_lens_q_output_ptr = cu_seq_lens_q_output.data<int>(); |
| 84 | + int *batch_id_per_token_output_ptr = batch_id_per_token_output.data<int>(); |
| 85 | + int *real_output_token_num_ptr = real_output_token_num.data<int>(); |
| 86 | + const int64_t *input_data_ptr = input_ids.data<int64_t>(); |
| 87 | + const int *seq_len_ptr = seq_len.data<int>(); |
| 88 | + const int64_t *draft_tokens_ptr = draft_tokens.data<int64_t>(); |
| 89 | + const int *seq_lens_encoder_ptr = seq_lens_encoder.data<int>(); |
| 90 | + |
| 91 | + int r = |
| 92 | + fastdeploy::plugin::speculate_preprocess(ctx, |
| 93 | + ids_remove_padding_ptr, |
| 94 | + batch_id_per_token_ptr, |
| 95 | + cu_seqlens_q_ptr, |
| 96 | + cu_seqlens_k_ptr, |
| 97 | + seq_lens_output_ptr, |
| 98 | + cu_seq_lens_q_output_ptr, |
| 99 | + batch_id_per_token_output_ptr, |
| 100 | + real_output_token_num_ptr, |
| 101 | + input_data_ptr, |
| 102 | + seq_len_ptr, |
| 103 | + draft_tokens_ptr, |
| 104 | + seq_lens_encoder_ptr, |
| 105 | + max_seq_len, |
| 106 | + max_draft_tokens_per_batch, |
| 107 | + token_num_data, |
| 108 | + bsz); |
| 109 | + |
| 110 | + return {ids_remove_padding, |
| 111 | + batch_id_per_token, |
| 112 | + cu_seqlens_q, |
| 113 | + cu_seqlens_k, |
| 114 | + cu_seq_lens_q_output, |
| 115 | + batch_id_per_token_output, |
| 116 | + real_output_token_num}; |
| 117 | +} |
| 118 | + |
| 119 | +PD_BUILD_STATIC_OP(speculate_pre_process) |
| 120 | + .Inputs({"input_ids", |
| 121 | + "seq_len", |
| 122 | + "draft_tokens", |
| 123 | + "seq_lens_encoder", |
| 124 | + "seq_lens_decoder"}) |
| 125 | + .Outputs({"ids_remove_padding", |
| 126 | + "batch_id_per_token", |
| 127 | + "cu_seqlens_q", |
| 128 | + "cu_seqlens_k", |
| 129 | + "cu_seq_lens_q_output", |
| 130 | + "batch_id_per_token_output", |
| 131 | + "real_output_token_num"}) |
| 132 | + .Attrs({"cpu_token_num: int64_t"}) |
| 133 | + .SetKernelFn(PD_KERNEL(SpeculatePreProcess)); |
0 commit comments