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 : namespace AicpuSchedule {
20 : namespace {
21 : const std::string KERNEL_DYN_OUTPUT_POST_PROCESS = "dynOutputPostProcess";
22 : } // namespace
23 :
24 4 : int32_t OperatorKernelDynOutputPostProcess::Compute(const AicpuTaskInfo& kernelTaskInfo, const RunContext& taskContext)
25 : {
26 4 : ProcessOutputInfo* const info =
27 4 : reinterpret_cast<ProcessOutputInfo*>(static_cast<uintptr_t>(kernelTaskInfo.paraBase));
28 4 : if (info == nullptr) {
29 1 : aicpusd_err(
30 : "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(
38 : const ProcessOutputInfo& outputInfo, 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(
48 : "ProcessOutput param inMBuf, outMBuf or srcPtr is null, inMBuf[%llx], outMbuf[%llx], srcPtr[%llx].",
49 : outputInfo.inMBuf, outputInfo.outMBuf, outputInfo.srcPtr);
50 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
51 : }
52 :
53 : // parse tensor desc and calculate shape size
54 2 : uint32_t dataSize = 0U;
55 2 : AicpuModel* const model = AicpuModelManager::GetInstance().GetModel(taskContext.modelId);
56 2 : if (model == nullptr) {
57 1 : aicpusd_err("Model[%u] prepare dynamic output task failed, no model found.", taskContext.modelId);
58 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
59 : }
60 :
61 1 : if (!model->IsEndOfSequence()) {
62 1 : const int32_t ret = OperatorKernelCommon::ParseTensorDescAndCalcDataSize(srcTensorDesc, dataSize);
63 1 : if ((ret != AICPU_SCHEDULE_OK) && !model->AbnormalNeedEnqueue()) {
64 0 : aicpusd_err("Model[%u] prepare dynamic output task failed, ret[%d]", taskContext.modelId, ret);
65 0 : return ret;
66 : }
67 : }
68 :
69 : // alloc data buffer
70 1 : *outMBuf = BufManager::GetInstance().MallocAndGuardBuf(
71 1 : dataSize + static_cast<uint32_t>(sizeof(RuntimeTensorDesc)), taskContext.modelId);
72 1 : if (*outMBuf == nullptr) {
73 0 : aicpusd_err("Failed to alloc mbuf, dataSize[%u], modelId[%u].", dataSize, taskContext.modelId);
74 0 : AicpuMonitor::GetInstance().SendKillMsgToTsd();
75 0 : return AICPU_SCHEDULE_ERROR_FROM_DRV;
76 : }
77 :
78 : // copy mbuf head info
79 1 : void* inputHeaderBuf = nullptr;
80 1 : uint32_t inputHeadSize = 0U;
81 1 : const auto drvRet = halMbufGetPrivInfo(*inMBuf, &inputHeaderBuf, &inputHeadSize);
82 1 : if (drvRet != DRV_ERROR_NONE) {
83 0 : aicpusd_err("Failed to get head info in input information, ret[%d].", drvRet);
84 0 : return AICPU_SCHEDULE_ERROR_FROM_DRV;
85 : }
86 1 : const int32_t ret = OperatorKernelCommon::CopyMbufHeadInfo(inputHeaderBuf, inputHeadSize, *outMBuf);
87 1 : if (ret != AICPU_SCHEDULE_OK) {
88 0 : aicpusd_err("Failed copy mbuf head info, ret[%d].", ret);
89 0 : return ret;
90 : }
91 : // copy tensor desc and data buffer
92 1 : return CopyTensorDescAndDataBuf(
93 1 : srcTensorDesc, dataSize, *outMBuf, dataSize + static_cast<uint32_t>(sizeof(RuntimeTensorDesc)));
94 : }
95 :
96 4 : int32_t OperatorKernelDynOutputPostProcess::CopyTensorDescAndDataBuf(
97 : const RuntimeTensorDesc* const srcTensorDesc, const uint32_t srcDataSize, Mbuf* const outMBuf,
98 : const uint32_t dstdataSize) const
99 : {
100 4 : void* basePtr = nullptr;
101 4 : const auto ret = halMbufGetBuffAddr(outMBuf, &basePtr);
102 4 : if ((ret != DRV_ERROR_NONE) || (basePtr == nullptr)) {
103 1 : aicpusd_err("Failed to call halMbufGetBuffAddr, ret[%d].", ret);
104 1 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
105 : }
106 :
107 3 : errno_t eRet = memcpy_s(basePtr, static_cast<size_t>(dstdataSize), srcTensorDesc, sizeof(RuntimeTensorDesc));
108 3 : if (eRet != EOK) {
109 1 : aicpusd_err("Failed to memcpy_s for tensor description, ret[%d].", eRet);
110 1 : return AICPU_SCHEDULE_ERROR_SAFE_FUNCTION_ERR;
111 : }
112 2 : if (srcDataSize != 0U) {
113 2 : uint8_t* const dataPtr = static_cast<uint8_t*>(basePtr) + sizeof(RuntimeTensorDesc);
114 4 : eRet = memcpy_s(
115 2 : dataPtr, static_cast<size_t>(dstdataSize) - sizeof(RuntimeTensorDesc),
116 2 : reinterpret_cast<void*>(static_cast<uintptr_t>(srcTensorDesc->dataAddr)), static_cast<size_t>(srcDataSize));
117 2 : if (eRet != EOK) {
118 1 : aicpusd_err("Failed to memcpy_s for data buffer, ret[%d].", eRet);
119 1 : return AICPU_SCHEDULE_ERROR_SAFE_FUNCTION_ERR;
120 : }
121 : }
122 1 : return AICPU_SCHEDULE_OK;
123 : }
124 :
125 6 : REGISTER_OPERATOR_KERNEL(KERNEL_DYN_OUTPUT_POST_PROCESS, OperatorKernelDynOutputPostProcess);
126 : } // namespace AicpuSchedule
|