|
| 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 <paddle/phi/backends/xpu/xpu_context.h> |
| 16 | +#include "paddle/extension.h" |
| 17 | +#include "paddle/phi/core/enforce.h" |
| 18 | +#include "xpu/plugin.h" |
| 19 | + |
| 20 | +void TokenPenaltyMultiScores(const paddle::Tensor& pre_ids, |
| 21 | + const paddle::Tensor& logits, |
| 22 | + const paddle::Tensor& penalty_scores, |
| 23 | + const paddle::Tensor& frequency_scores, |
| 24 | + const paddle::Tensor& presence_scores, |
| 25 | + const paddle::Tensor& temperatures, |
| 26 | + const paddle::Tensor& bad_tokens, |
| 27 | + const paddle::Tensor& cur_len, |
| 28 | + const paddle::Tensor& min_len, |
| 29 | + const paddle::Tensor& eos_token_id) { |
| 30 | + phi::XPUPlace place(phi::backends::xpu::GetXPUCurrentDeviceId()); |
| 31 | + auto dev_ctx = paddle::experimental::DeviceContextPool::Instance().Get(place); |
| 32 | + auto xpu_ctx = static_cast<const phi::XPUContext*>(dev_ctx); |
| 33 | + int64_t bs = logits.shape()[0]; |
| 34 | + PADDLE_ENFORCE_LE( |
| 35 | + bs, |
| 36 | + 640, |
| 37 | + phi::errors::InvalidArgument( |
| 38 | + "Only support bsz <= 1024, but received bsz is %d", bs)); |
| 39 | + int64_t length = logits.shape()[1]; |
| 40 | + int64_t length_id = pre_ids.shape()[1]; |
| 41 | + int64_t length_bad_words = bad_tokens.shape()[0]; |
| 42 | + int64_t end_length = eos_token_id.shape()[0]; |
| 43 | + switch (logits.type()) { |
| 44 | + case paddle::DataType::FLOAT16: { |
| 45 | + using XPUType = typename XPUTypeTrait<float16>::Type; |
| 46 | + typedef paddle::float16 data_t; |
| 47 | + int r = baidu::xpu::api::plugin::token_penalty_multi_scores( |
| 48 | + xpu_ctx->x_context(), |
| 49 | + pre_ids.data<int64_t>(), |
| 50 | + reinterpret_cast<XPUType*>( |
| 51 | + const_cast<data_t*>(logits.data<data_t>())), |
| 52 | + reinterpret_cast<const XPUType*>(penalty_scores.data<data_t>()), |
| 53 | + reinterpret_cast<const XPUType*>(frequency_scores.data<data_t>()), |
| 54 | + reinterpret_cast<const XPUType*>(presence_scores.data<data_t>()), |
| 55 | + temperatures.data<float>(), |
| 56 | + cur_len.data<int64_t>(), |
| 57 | + min_len.data<int64_t>(), |
| 58 | + eos_token_id.data<int64_t>(), |
| 59 | + bad_tokens.data<int64_t>(), |
| 60 | + bs, |
| 61 | + length, |
| 62 | + length_id, |
| 63 | + end_length, |
| 64 | + length_bad_words); |
| 65 | + PD_CHECK(r == 0, "xpu::plugin::token_penalty_multi_scores failed."); |
| 66 | + } break; |
| 67 | + case paddle::DataType::FLOAT32: { |
| 68 | + int r = baidu::xpu::api::plugin::token_penalty_multi_scores( |
| 69 | + xpu_ctx->x_context(), |
| 70 | + pre_ids.data<int64_t>(), |
| 71 | + const_cast<float*>(logits.data<float>()), |
| 72 | + penalty_scores.data<float>(), |
| 73 | + frequency_scores.data<float>(), |
| 74 | + presence_scores.data<float>(), |
| 75 | + temperatures.data<float>(), |
| 76 | + cur_len.data<int64_t>(), |
| 77 | + min_len.data<int64_t>(), |
| 78 | + eos_token_id.data<int64_t>(), |
| 79 | + bad_tokens.data<int64_t>(), |
| 80 | + bs, |
| 81 | + length, |
| 82 | + length_id, |
| 83 | + end_length, |
| 84 | + length_bad_words); |
| 85 | + PD_CHECK(r == 0, "xpu::plugin::token_penalty_multi_scores failed."); |
| 86 | + } break; |
| 87 | + default: |
| 88 | + PD_THROW( |
| 89 | + "NOT supported data type. " |
| 90 | + "Only float16 and float32 are supported. "); |
| 91 | + break; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +PD_BUILD_OP(get_token_penalty_multi_scores_v2) |
| 96 | + .Inputs({"pre_ids", |
| 97 | + "logits", |
| 98 | + "penalty_scores", |
| 99 | + "frequency_scores", |
| 100 | + "presence_scores", |
| 101 | + "temperatures", |
| 102 | + "bad_tokens", |
| 103 | + "cur_len", |
| 104 | + "min_len", |
| 105 | + "eos_token_id"}) |
| 106 | + .Outputs({"logits_out"}) |
| 107 | + .SetInplaceMap({{"logits", "logits_out"}}) |
| 108 | + .SetKernelFn(PD_KERNEL(TokenPenaltyMultiScores)); |
0 commit comments