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_model_prepare_non_zero_copy_input.h"
12 :
13 : #include <vector>
14 : #include "securec.h"
15 : #include "aicpusd_status.h"
16 : #include "operator_kernel_common.h"
17 :
18 :
19 : namespace AicpuSchedule {
20 : namespace {
21 : const std::string KERNEL_MODEL_PREPARE_NON_ZERO_COPY_INPUT = "modelPrepareNonZeroCopyInput";
22 : } // namespace
23 :
24 5 : int32_t OperatorKernelModelPrepareNonZeroCopyInput::Compute(const AicpuTaskInfo &kernelTaskInfo,
25 : const RunContext &taskContext)
26 : {
27 5 : InputCopyAddrMapInfo *mapInfo = PtrToPtr<void, InputCopyAddrMapInfo>(ValueToPtr(kernelTaskInfo.paraBase));
28 5 : if (mapInfo == nullptr) {
29 1 : aicpusd_err("Model prepare non-zero copy input para is nullptr, modelId[%u], streamId[%u], taskId[%u]",
30 : taskContext.modelId, taskContext.streamId, kernelTaskInfo.taskID);
31 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
32 : }
33 :
34 4 : return DoCompute(*mapInfo, taskContext);
35 : }
36 :
37 4 : uint32_t OperatorKernelModelPrepareNonZeroCopyInput::DoCompute(const InputCopyAddrMapInfo &mapInfo,
38 : const RunContext &taskContext) const
39 : {
40 4 : const uint64_t *srcAddrList = PtrToPtr<void, uint64_t>(ValueToPtr(mapInfo.srcAddrList));
41 4 : const uint64_t *dstAddrList = PtrToPtr<void, uint64_t>(ValueToPtr(mapInfo.dstAddrList));
42 4 : const uint64_t *dstAddrLenList = PtrToPtr<void, uint64_t>(ValueToPtr(mapInfo.dstAddrLenList));
43 4 : const int32_t *srcFusionOffsetList = PtrToPtr<void, int32_t>(ValueToPtr(mapInfo.srcFusionOffsetList));
44 4 : if ((srcAddrList == nullptr) || (dstAddrList == nullptr) || (dstAddrLenList == nullptr)) {
45 1 : aicpusd_err("Failed to non-zero copy by nullptr, modelId[%u], streamId[%u]",
46 : taskContext.modelId, taskContext.streamId);
47 1 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
48 : }
49 :
50 3 : std::vector<int32_t> srcFusionOffsets(mapInfo.addrNum);
51 3 : if (srcFusionOffsetList != nullptr) {
52 3 : for (uint32_t i = 0U; i < mapInfo.addrNum; i++) {
53 2 : srcFusionOffsets[i] = srcFusionOffsetList[i];
54 : }
55 : }
56 :
57 7 : for (uint32_t i = 0U; i < mapInfo.addrNum; i++) {
58 5 : void *srcDataPtr = nullptr;
59 5 : uint64_t totalOffset = 0UL;
60 5 : int32_t ret = OperatorKernelCommon::GetMbufDataPtr(srcAddrList[i], &srcDataPtr);
61 5 : if (ret != AICPU_SCHEDULE_OK) {
62 1 : aicpusd_err("Failed to get mbuf data addr. modelId[%u], addrNum[%u].", taskContext.modelId, i);
63 1 : return ret;
64 : }
65 :
66 4 : if (srcFusionOffsets[i] > 0) {
67 1 : ret = OperatorKernelCommon::UpdateDataPtr(srcAddrList[i], srcFusionOffsets[i], srcDataPtr, totalOffset);
68 1 : if (ret != AICPU_SCHEDULE_OK) {
69 0 : aicpusd_err("Failed to update data addr. fusion offset = %d.", srcFusionOffsets[i]);
70 0 : return ret;
71 : }
72 : }
73 :
74 4 : const auto mbufPptr = reinterpret_cast<Mbuf **>(static_cast<uintptr_t>(srcAddrList[i]));
75 4 : uint64_t srcDataLen = 0UL;
76 4 : ret = halMbufGetDataLen(*mbufPptr, &srcDataLen);
77 4 : if (ret != DRV_ERROR_NONE) {
78 0 : aicpusd_err("Get mbuf datalen failed. modelId[%u], addrNum[%u], ret[%d]", taskContext.modelId, i, ret);
79 0 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
80 : }
81 4 : if (srcDataLen < sizeof(RuntimeTensorDesc)) {
82 0 : aicpusd_err("The mbuf datalen is less than tensor desc. modelId[%u], srcDataLen[%lu], addrNum[%u]",
83 : taskContext.modelId, srcDataLen, i);
84 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
85 : }
86 :
87 4 : uint32_t dataSize = 0U;
88 4 : const RuntimeTensorDesc * const srcTensorDesc = PtrToPtr<void, RuntimeTensorDesc>(srcDataPtr);
89 4 : ret = OperatorKernelCommon::ParseTensorDescAndCalcDataSize(srcTensorDesc, dataSize);
90 4 : if (ret != AICPU_SCHEDULE_OK) {
91 0 : aicpusd_err("Parse runtime tensor desc failed, ret[%d]", ret);
92 0 : return ret;
93 : }
94 :
95 4 : if (srcDataLen < (sizeof(RuntimeTensorDesc) + totalOffset + dataSize)) {
96 0 : aicpusd_err("The mbuf datalen is invalid. modelId[%u], srcDataLen[%lu], addrNum[%u], "
97 : "dataSize[%u], totalOffset[%lu].",
98 : taskContext.modelId, srcDataLen, i, dataSize, totalOffset);
99 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
100 : }
101 :
102 4 : srcDataPtr = ValueToPtr(PtrToValue(srcDataPtr) + sizeof(RuntimeTensorDesc));
103 4 : const int32_t eRet = memcpy_s(ValueToPtr(dstAddrList[i]), dstAddrLenList[i], srcDataPtr, dataSize);
104 4 : if (eRet != EOK) {
105 0 : aicpusd_err("Data copy failed. modelId[%u], addrNum[%u], dstAddrLen[%lu], dataSize[%lu], ret[%d]",
106 : taskContext.modelId, i, dstAddrLenList[i], dataSize, eRet);
107 0 : return AICPU_SCHEDULE_ERROR_SAFE_FUNCTION_ERR;
108 : }
109 : }
110 :
111 2 : return AICPU_SCHEDULE_OK;
112 3 : }
113 :
114 6 : REGISTER_OPERATOR_KERNEL(KERNEL_MODEL_PREPARE_NON_ZERO_COPY_INPUT, OperatorKernelModelPrepareNonZeroCopyInput);
115 : } // namespace AicpuSchedule
|