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 : #include "operator_kernel_model_enqueue_buff.h"
11 :
12 : #include "aicpusd_status.h"
13 : #include "aicpusd_model_execute.h"
14 : #include "aicpusd_resource_manager.h"
15 : #include "operator_kernel_common.h"
16 :
17 : namespace AicpuSchedule {
18 : namespace {
19 : const std::string KERNEL_MODEL_ENQUEUE_BUFF = "modelEnqueueBuff";
20 : constexpr uint32_t ENQUEUED_TIMEOUT = 1000U;
21 : } // namespace
22 :
23 3 : int32_t OperatorKernelModelEnqueueBuff::Compute(const AicpuTaskInfo& kernelTaskInfo, const RunContext& taskContext)
24 : {
25 3 : auto bufInfo = reinterpret_cast<BufEnQueueBuffInfo*>(static_cast<uintptr_t>(kernelTaskInfo.paraBase));
26 3 : if (bufInfo == nullptr) {
27 1 : aicpusd_err(
28 : "ModelEnqueue kernelTaskInfo paramBase is null, modelId[%u], streamId[%u], taskId[%u]", taskContext.modelId,
29 : taskContext.streamId, kernelTaskInfo.taskID);
30 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
31 : }
32 2 : const auto ret = ModelEnqueueBuff(*bufInfo, taskContext);
33 2 : return ret;
34 : }
35 :
36 5 : int32_t OperatorKernelModelEnqueueBuff::ModelEnqueueBuff(
37 : BufEnQueueBuffInfo& bufInfo, const RunContext& taskContext) const
38 : {
39 5 : const auto model = AicpuModelManager::GetInstance().GetModel(taskContext.modelId);
40 5 : if ((model != nullptr) && (model->GetModelRetCode() != 0)) {
41 1 : aicpusd_info(
42 : "Model execution was not successful, no need to enqueue. modelId=%u, modelRetCode=%d.", taskContext.modelId,
43 : model->GetModelRetCode());
44 1 : return AICPU_SCHEDULE_OK;
45 : }
46 :
47 4 : auto mBufPptr = reinterpret_cast<Mbuf**>(static_cast<uintptr_t>(bufInfo.mBufPtr));
48 4 : if (mBufPptr == nullptr) {
49 1 : aicpusd_err("param mBufPptr is null.");
50 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
51 : }
52 3 : if (*mBufPptr == nullptr) {
53 0 : aicpusd_err("param *mBufPptr is null.");
54 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
55 : }
56 3 : const auto guardRet = BufManager::GetInstance().GuardBuf(*mBufPptr, taskContext.modelId);
57 3 : if (guardRet != AICPU_SCHEDULE_OK) {
58 0 : aicpusd_err("BufManager guard dequeue failed, modelId[%u], ret[%d].", taskContext.modelId, guardRet);
59 0 : return guardRet;
60 : }
61 3 : uint32_t headSize = 0U;
62 3 : void* headBuf = nullptr;
63 3 : const auto ret = halMbufGetPrivInfo(*mBufPptr, &headBuf, &headSize);
64 3 : if (ret != DRV_ERROR_NONE) {
65 1 : aicpusd_err("Failed to get head info in input information, ret[%d].", ret);
66 1 : return AICPU_SCHEDULE_ERROR_FROM_DRV;
67 : }
68 2 : SetMbufRetCode(taskContext.modelId, headBuf, headSize);
69 2 : SetMbufEndOfSequence(taskContext.modelId, headBuf, headSize);
70 :
71 2 : const uint32_t queueId = bufInfo.queueID;
72 2 : const uint32_t deviceId = static_cast<uint32_t>(bufInfo.deviceId);
73 2 : const auto drvRet = QueueEnQueueBuff(deviceId, queueId, *mBufPptr, headBuf, headSize);
74 2 : if (drvRet != DRV_ERROR_NONE) {
75 1 : aicpusd_err("Failed to enqueue on queueId[%u], drvRet[%d].", queueId, drvRet);
76 1 : return AICPU_SCHEDULE_ERROR_FROM_DRV;
77 : }
78 :
79 1 : OperatorKernelCommon::TraceQueueData(taskContext, headBuf, headSize, "EnqueuedBuff");
80 1 : return AICPU_SCHEDULE_OK;
81 : }
82 :
83 2 : int32_t OperatorKernelModelEnqueueBuff::QueueEnQueueBuff(
84 : const uint32_t deviceId, const uint32_t queueId, Mbuf* const mbuf, void* const headBuf,
85 : const uint32_t headSize) const
86 : {
87 2 : uint64_t mbufLen = 0UL;
88 2 : int32_t ret = halMbufGetDataLen(mbuf, &mbufLen);
89 2 : if (ret != DRV_ERROR_NONE) {
90 1 : aicpusd_err("Get mbuf datalen failed. deviceId[%u], queueId[%u]", deviceId, queueId);
91 1 : return AICPU_SCHEDULE_ERROR_FROM_DRV;
92 : }
93 1 : constexpr size_t totalLen = sizeof(struct buff_iovec) + sizeof(struct iovec_info);
94 1 : std::unique_ptr<char_t[]> vecUniquePtr(new (std::nothrow) char_t[totalLen], std::default_delete<char_t[]>());
95 1 : if (vecUniquePtr == nullptr) {
96 0 : aicpusd_err("failed to alloc memory for buffIovec, size[%zu].", totalLen);
97 0 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
98 : }
99 :
100 1 : void* dataAddrPtr = nullptr;
101 1 : ret = OperatorKernelCommon::GetMbufDataPtr(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(&mbuf)), &dataAddrPtr);
102 1 : if (ret != AICPU_SCHEDULE_OK) {
103 0 : aicpusd_err("Failed to get mbuf data addr. ret is [%d]", ret);
104 0 : return ret;
105 : }
106 :
107 1 : buff_iovec* const buffIovec = reinterpret_cast<buff_iovec*>(vecUniquePtr.get());
108 1 : buffIovec->context_base = headBuf;
109 1 : buffIovec->context_len = headSize;
110 1 : buffIovec->count = 1U;
111 1 : buffIovec->ptr[0U].iovec_base = dataAddrPtr;
112 1 : buffIovec->ptr[0U].len = mbufLen;
113 1 : const auto drvRet = halQueueEnQueueBuff(deviceId, queueId, buffIovec, ENQUEUED_TIMEOUT);
114 1 : if (drvRet != DRV_ERROR_NONE) {
115 0 : aicpusd_err(
116 : "halQueueEnQueueBuff to queue[%u] in device[%u] failed, error[%d]", queueId, deviceId,
117 : static_cast<int32_t>(drvRet));
118 0 : return AICPU_SCHEDULE_ERROR_FROM_DRV;
119 : }
120 1 : return AICPU_SCHEDULE_OK;
121 1 : }
122 :
123 6 : REGISTER_OPERATOR_KERNEL(KERNEL_MODEL_ENQUEUE_BUFF, OperatorKernelModelEnqueueBuff);
124 : } // namespace AicpuSchedule
|