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