Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "operator_kernel_dyn_output_post_process.h"
12 :
13 : #include "aicpusd_status.h"
14 : #include "aicpusd_monitor.h"
15 : #include "aicpusd_model_execute.h"
16 : #include "aicpusd_resource_manager.h"
17 : #include "operator_kernel_common.h"
18 :
19 :
20 : namespace AicpuSchedule {
21 : namespace {
22 : const std::string KERNEL_DYN_OUTPUT_POST_PROCESS = "dynOutputPostProcess";
23 : } // namespace
24 :
25 4 : int32_t OperatorKernelDynOutputPostProcess::Compute(const AicpuTaskInfo &kernelTaskInfo, const RunContext &taskContext)
26 : {
27 4 : ProcessOutputInfo * const info = reinterpret_cast<ProcessOutputInfo *>(
28 4 : static_cast<uintptr_t>(kernelTaskInfo.paraBase));
29 4 : if (info == nullptr) {
30 1 : aicpusd_err("ModelDynPrepareOut kernelTaskInfo paramBase is null, modelId[%u], streamId[%u], taskId[%u]",
31 : taskContext.modelId, taskContext.streamId, kernelTaskInfo.taskID);
32 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
33 : }
34 3 : return DoCompute(*info, taskContext);
35 : }
36 :
37 3 : int32_t OperatorKernelDynOutputPostProcess::DoCompute(const ProcessOutputInfo &outputInfo,
38 : const RunContext &taskContext) const
39 : {
40 : // point to Mbuf * address
41 3 : Mbuf * const * const inMBuf = reinterpret_cast<Mbuf **>(static_cast<uintptr_t>(outputInfo.inMBuf));
42 3 : Mbuf ** const outMBuf = reinterpret_cast<Mbuf **>(static_cast<uintptr_t>(outputInfo.outMBuf));
43 3 : const RuntimeTensorDesc * const srcTensorDesc =
44 3 : reinterpret_cast<RuntimeTensorDesc *>(static_cast<uintptr_t>(outputInfo.srcPtr));
45 3 : const bool inOrOutMbufIsNull = ((inMBuf == nullptr) || (outMBuf == nullptr) || (srcTensorDesc == nullptr));
46 3 : if (inOrOutMbufIsNull) {
47 1 : aicpusd_err("ProcessOutput param inMBuf, outMBuf or srcPtr is null, inMBuf[%llx], outMbuf[%llx], srcPtr[%llx].",
48 : outputInfo.inMBuf, outputInfo.outMBuf, outputInfo.srcPtr);
49 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
50 : }
51 :
52 : // parse tensor desc and calculate shape size
53 2 : uint32_t dataSize = 0U;
54 2 : AicpuModel * const model = AicpuModelManager::GetInstance().GetModel(taskContext.modelId);
55 2 : if (model == nullptr) {
56 1 : aicpusd_err("Model[%u] prepare dynamic output task failed, no model found.", taskContext.modelId);
57 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
58 : }
59 :
60 1 : if (!model->IsEndOfSequence()) {
61 1 : const int32_t ret = OperatorKernelCommon::ParseTensorDescAndCalcDataSize(srcTensorDesc, dataSize);
62 1 : if ((ret != AICPU_SCHEDULE_OK) && !model->AbnormalNeedEnqueue()) {
63 0 : aicpusd_err("Model[%u] prepare dynamic output task failed, ret[%d]", taskContext.modelId, ret);
64 0 : return ret;
65 : }
66 : }
67 :
68 : // alloc data buffer
69 1 : *outMBuf = BufManager::GetInstance().MallocAndGuardBuf(dataSize + static_cast<uint32_t>(sizeof(RuntimeTensorDesc)),
70 1 : taskContext.modelId);
71 1 : if (*outMBuf == nullptr) {
72 0 : aicpusd_err("Failed to alloc mbuf, dataSize[%u], modelId[%u].", dataSize, taskContext.modelId);
73 0 : AicpuMonitor::GetInstance().SendKillMsgToTsd();
74 0 : return AICPU_SCHEDULE_ERROR_FROM_DRV;
75 : }
76 :
77 : // copy mbuf head info
78 1 : void *inputHeaderBuf = nullptr;
79 1 : uint32_t inputHeadSize = 0U;
80 1 : const auto drvRet = halMbufGetPrivInfo(*inMBuf, &inputHeaderBuf, &inputHeadSize);
81 1 : if (drvRet != DRV_ERROR_NONE) {
82 0 : aicpusd_err("Failed to get head info in input information, ret[%d].", drvRet);
83 0 : return AICPU_SCHEDULE_ERROR_FROM_DRV;
84 : }
85 1 : const int32_t ret = OperatorKernelCommon::CopyMbufHeadInfo(inputHeaderBuf, inputHeadSize, *outMBuf);
86 1 : if (ret != AICPU_SCHEDULE_OK) {
87 0 : aicpusd_err("Failed copy mbuf head info, ret[%d].", ret);
88 0 : return ret;
89 : }
90 : // copy tensor desc and data buffer
91 1 : return CopyTensorDescAndDataBuf(srcTensorDesc, dataSize, *outMBuf,
92 1 : dataSize + static_cast<uint32_t>(sizeof(RuntimeTensorDesc)));
93 : }
94 :
95 4 : int32_t OperatorKernelDynOutputPostProcess::CopyTensorDescAndDataBuf(const RuntimeTensorDesc * const srcTensorDesc,
96 : const uint32_t srcDataSize, Mbuf * const outMBuf,
97 : const uint32_t dstdataSize) const
98 : {
99 4 : void *basePtr = nullptr;
100 4 : const auto ret = halMbufGetBuffAddr(outMBuf, &basePtr);
101 4 : if ((ret != DRV_ERROR_NONE) || (basePtr == nullptr)) {
102 1 : aicpusd_err("Failed to call halMbufGetBuffAddr, ret[%d].", ret);
103 1 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
104 : }
105 :
106 3 : errno_t eRet = memcpy_s(basePtr, static_cast<size_t>(dstdataSize), srcTensorDesc, sizeof(RuntimeTensorDesc));
107 3 : if (eRet != EOK) {
108 1 : aicpusd_err("Failed to memcpy_s for tensor description, ret[%d].", eRet);
109 1 : return AICPU_SCHEDULE_ERROR_SAFE_FUNCTION_ERR;
110 : }
111 2 : if (srcDataSize != 0U) {
112 2 : uint8_t * const dataPtr = static_cast<uint8_t *>(basePtr) + sizeof(RuntimeTensorDesc);
113 4 : eRet = memcpy_s(dataPtr, static_cast<size_t>(dstdataSize) - sizeof(RuntimeTensorDesc),
114 2 : reinterpret_cast<void *>(static_cast<uintptr_t>(srcTensorDesc->dataAddr)),
115 : static_cast<size_t>(srcDataSize));
116 2 : if (eRet != EOK) {
117 1 : aicpusd_err("Failed to memcpy_s for data buffer, ret[%d].", eRet);
118 1 : return AICPU_SCHEDULE_ERROR_SAFE_FUNCTION_ERR;
119 : }
120 : }
121 1 : return AICPU_SCHEDULE_OK;
122 : }
123 :
124 6 : REGISTER_OPERATOR_KERNEL(KERNEL_DYN_OUTPUT_POST_PROCESS, OperatorKernelDynOutputPostProcess);
125 : } // namespace AicpuSchedule
|