|
| 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 | +#include <stdio.h> |
| 16 | +#include <string.h> |
| 17 | +#include <sys/ipc.h> |
| 18 | +#include <sys/msg.h> |
| 19 | +#include <sys/types.h> |
| 20 | +#include "paddle/extension.h" |
| 21 | + |
| 22 | +#ifndef PD_BUILD_STATIC_OP |
| 23 | +#define PD_BUILD_STATIC_OP(name) PD_BUILD_OP(static_op_##name) |
| 24 | +#endif |
| 25 | + |
| 26 | +#define MAX_BSZ 128 |
| 27 | +#define K 5 |
| 28 | +// #define SAVE_WITH_OUTPUT_DEBUG |
| 29 | + |
| 30 | +struct msgdata { |
| 31 | + long mtype; |
| 32 | + int mtext[MAX_BSZ * (K + 1) + 2]; // stop_flag, bsz, tokens |
| 33 | + float mtext_f[MAX_BSZ * (K + 1)]; // score |
| 34 | + int mtext_ranks[MAX_BSZ]; // ranks |
| 35 | +}; |
| 36 | + |
| 37 | +void SaveOutMmsgTopK(const paddle::Tensor& x, |
| 38 | + const paddle::Tensor& logprob_token_ids, // [bsz, k+1] |
| 39 | + const paddle::Tensor& logprob_scores, // [bsz, k+1] |
| 40 | + const paddle::Tensor& ranks, |
| 41 | + const paddle::Tensor& not_need_stop, |
| 42 | + int64_t rank_id) { |
| 43 | + if (rank_id > 0) { |
| 44 | + return; |
| 45 | + } |
| 46 | + auto x_cpu = x.copy_to(paddle::CPUPlace(), false); |
| 47 | + auto logprob_token_ids_cpu = |
| 48 | + logprob_token_ids.copy_to(paddle::CPUPlace(), false); |
| 49 | + auto logprob_scores_cpu = logprob_scores.copy_to(paddle::CPUPlace(), false); |
| 50 | + auto ranks_cpu = ranks.copy_to(paddle::CPUPlace(), false); |
| 51 | + int64_t* x_data = x_cpu.data<int64_t>(); |
| 52 | + int64_t* logprob_token_ids_data = logprob_token_ids_cpu.data<int64_t>(); |
| 53 | + float* logprob_scores_data = logprob_scores_cpu.data<float>(); |
| 54 | + int64_t* ranks_data = ranks_cpu.data<int64_t>(); |
| 55 | + static struct msgdata msg_sed; |
| 56 | + int msg_queue_id = 1; |
| 57 | + if (const char* inference_msg_queue_id_env_p = |
| 58 | + std::getenv("INFERENCE_MSG_QUEUE_ID")) { |
| 59 | + std::string inference_msg_queue_id_env_str(inference_msg_queue_id_env_p); |
| 60 | + int inference_msg_queue_id_from_env = |
| 61 | + std::stoi(inference_msg_queue_id_env_str); |
| 62 | + msg_queue_id = inference_msg_queue_id_from_env; |
| 63 | +#ifdef SAVE_WITH_OUTPUT_DEBUG |
| 64 | + std::cout << "Your INFERENCE_MSG_QUEUE_ID is: " |
| 65 | + << inference_msg_queue_id_from_env << std::endl; |
| 66 | +#endif |
| 67 | + } else { |
| 68 | +#ifdef SAVE_WITH_OUTPUT_DEBUG |
| 69 | + std::cout << "Failed to got INFERENCE_MSG_QUEUE_ID at env, use default." |
| 70 | + << std::endl; |
| 71 | +#endif |
| 72 | + } |
| 73 | + int inference_msg_id_from_env = 1; |
| 74 | + if (const char* inference_msg_id_env_p = std::getenv("INFERENCE_MSG_ID")) { |
| 75 | + std::string inference_msg_id_env_str(inference_msg_id_env_p); |
| 76 | + inference_msg_id_from_env = std::stoi(inference_msg_id_env_str); |
| 77 | + if (inference_msg_id_from_env == 2) { |
| 78 | + // 2 and -2 is preserve for no-output indication. |
| 79 | + throw std::runtime_error( |
| 80 | + " INFERENCE_MSG_ID cannot be 2, please use other number."); |
| 81 | + } |
| 82 | + if (inference_msg_id_from_env < 0) { |
| 83 | + throw std::runtime_error( |
| 84 | + " INFERENCE_MSG_ID cannot be negative, please use other " |
| 85 | + "number."); |
| 86 | + } |
| 87 | +#ifdef SAVE_WITH_OUTPUT_DEBUG |
| 88 | + std::cout << "Your INFERENCE_MSG_ID is: " << inference_msg_id_from_env |
| 89 | + << std::endl; |
| 90 | +#endif |
| 91 | + } else { |
| 92 | +#ifdef SAVE_WITH_OUTPUT_DEBUG |
| 93 | + std::cout << "Failed to got INFERENCE_MSG_ID at env, use (int)1 as default." |
| 94 | + << std::endl; |
| 95 | +#endif |
| 96 | + } |
| 97 | + static key_t key = ftok("/dev/shm", msg_queue_id); |
| 98 | + static int msgid = msgget(key, IPC_CREAT | 0666); |
| 99 | +#ifdef SAVE_WITH_OUTPUT_DEBUG |
| 100 | + std::cout << "save_output_key: " << key << std::endl; |
| 101 | + std::cout << "save msgid: " << msgid << std::endl; |
| 102 | +#endif |
| 103 | + msg_sed.mtype = 1; |
| 104 | + bool not_need_stop_data = not_need_stop.data<bool>()[0]; |
| 105 | + msg_sed.mtext[0] = not_need_stop_data ? inference_msg_id_from_env |
| 106 | + : -inference_msg_id_from_env; |
| 107 | + int bsz = x.shape()[0]; |
| 108 | + int max_num_logprobs = logprob_token_ids.shape()[1]; |
| 109 | + msg_sed.mtext[1] = bsz; |
| 110 | + for (int i = 0; i < bsz; i++) { |
| 111 | + for (int j = 0; j < K + 1; j++) { |
| 112 | + const int64_t offset = i * (K + 1) + j; |
| 113 | + if (j == 0) { |
| 114 | + msg_sed.mtext[offset + 2] = (int)x_data[i]; |
| 115 | + msg_sed.mtext_f[offset] = logprob_scores_data[i * max_num_logprobs + j]; |
| 116 | + } else if (j < max_num_logprobs) { |
| 117 | + msg_sed.mtext[offset + 2] = |
| 118 | + (int)logprob_token_ids_data[i * max_num_logprobs + j]; |
| 119 | + msg_sed.mtext_f[offset] = logprob_scores_data[i * max_num_logprobs + j]; |
| 120 | + } else { |
| 121 | + msg_sed.mtext[offset + 2] = -1; |
| 122 | + msg_sed.mtext_f[offset] = 0.0; |
| 123 | + } |
| 124 | + } |
| 125 | + msg_sed.mtext_ranks[i] = (int)ranks_data[i]; |
| 126 | + } |
| 127 | +#ifdef SAVE_WITH_OUTPUT_DEBUG |
| 128 | + std::cout << "msg data: "; |
| 129 | + for (int i = 0; i < bsz; i++) { |
| 130 | + std::cout << " " << (int)x_data[i]; |
| 131 | + } |
| 132 | + std::cout << std::endl; |
| 133 | +#endif |
| 134 | + |
| 135 | + size_t msg_len = (MAX_BSZ * (K + 1) + 2) * sizeof(int) + |
| 136 | + (MAX_BSZ * (K + 1)) * sizeof(float) + MAX_BSZ * sizeof(int); |
| 137 | + |
| 138 | + if ((msgsnd(msgid, &msg_sed, msg_len, 0)) == -1) { |
| 139 | + printf("full msg buffer\n"); |
| 140 | + } |
| 141 | + return; |
| 142 | +} |
| 143 | + |
| 144 | +PD_BUILD_STATIC_OP(save_output_topk) |
| 145 | + .Inputs({"x", "topk_ids", "logprob_scores", "ranks", "not_need_stop"}) |
| 146 | + .Attrs({"rank_id: int64_t"}) |
| 147 | + .Outputs({"x_out"}) |
| 148 | + .SetInplaceMap({{"x", "x_out"}}) |
| 149 | + .SetKernelFn(PD_KERNEL(SaveOutMmsgTopK)); |
0 commit comments