LCOV - code coverage report
Current view: top level - aicpu_schedule/core/operator_kernel/preprocess - operator_kernel_model_prepare_non_zero_copy_input.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 78.6 % 56 44
Test Date: 2026-08-12 11:05:02 Functions: 100.0 % 3 3

            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              : namespace AicpuSchedule {
      19              : namespace {
      20              : const std::string KERNEL_MODEL_PREPARE_NON_ZERO_COPY_INPUT = "modelPrepareNonZeroCopyInput";
      21              : } // namespace
      22              : 
      23            5 : int32_t OperatorKernelModelPrepareNonZeroCopyInput::Compute(
      24              :     const AicpuTaskInfo& kernelTaskInfo, const RunContext& taskContext)
      25              : {
      26            5 :     InputCopyAddrMapInfo* mapInfo = PtrToPtr<void, InputCopyAddrMapInfo>(ValueToPtr(kernelTaskInfo.paraBase));
      27            5 :     if (mapInfo == nullptr) {
      28            1 :         aicpusd_err(
      29              :             "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(
      38              :     const InputCopyAddrMapInfo& mapInfo, 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(
      46              :             "Failed to non-zero copy by nullptr, modelId[%u], streamId[%u]", 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(
      83              :                 "The mbuf datalen is less than tensor desc. modelId[%u], srcDataLen[%lu], addrNum[%u]",
      84              :                 taskContext.modelId, srcDataLen, i);
      85            0 :             return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
      86              :         }
      87              : 
      88            4 :         uint32_t dataSize = 0U;
      89            4 :         const RuntimeTensorDesc* const srcTensorDesc = PtrToPtr<void, RuntimeTensorDesc>(srcDataPtr);
      90            4 :         ret = OperatorKernelCommon::ParseTensorDescAndCalcDataSize(srcTensorDesc, dataSize);
      91            4 :         if (ret != AICPU_SCHEDULE_OK) {
      92            0 :             aicpusd_err("Parse runtime tensor desc failed, ret[%d]", ret);
      93            0 :             return ret;
      94              :         }
      95              : 
      96            4 :         if (srcDataLen < (sizeof(RuntimeTensorDesc) + totalOffset + dataSize)) {
      97            0 :             aicpusd_err(
      98              :                 "The mbuf datalen is invalid. modelId[%u], srcDataLen[%lu], addrNum[%u], "
      99              :                 "dataSize[%u], totalOffset[%lu].",
     100              :                 taskContext.modelId, srcDataLen, i, dataSize, totalOffset);
     101            0 :             return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
     102              :         }
     103              : 
     104            4 :         srcDataPtr = ValueToPtr(PtrToValue(srcDataPtr) + sizeof(RuntimeTensorDesc));
     105            4 :         const int32_t eRet = memcpy_s(ValueToPtr(dstAddrList[i]), dstAddrLenList[i], srcDataPtr, dataSize);
     106            4 :         if (eRet != EOK) {
     107            0 :             aicpusd_err(
     108              :                 "Data copy failed. modelId[%u], addrNum[%u], dstAddrLen[%lu], dataSize[%lu], ret[%d]",
     109              :                 taskContext.modelId, i, dstAddrLenList[i], dataSize, eRet);
     110            0 :             return AICPU_SCHEDULE_ERROR_SAFE_FUNCTION_ERR;
     111              :         }
     112              :     }
     113              : 
     114            2 :     return AICPU_SCHEDULE_OK;
     115            3 : }
     116              : 
     117            6 : REGISTER_OPERATOR_KERNEL(KERNEL_MODEL_PREPARE_NON_ZERO_COPY_INPUT, OperatorKernelModelPrepareNonZeroCopyInput);
     118              : } // namespace AicpuSchedule
        

Generated by: LCOV version 2.0-1