LCOV - code coverage report
Current view: top level - aicpu_schedule/statistic - aicpusd_model_statistic.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 146 146
Test Date: 2026-08-31 10:07:09 Functions: 93.3 % 15 14

            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 "aicpusd_model_statistic.h"
      11              : 
      12              : #include <sstream>
      13              : namespace AicpuSchedule {
      14           14 : std::string AicpuSdModelStatistic::FormatShapeToString(const std::vector<int64_t>& shape) const
      15              : {
      16           14 :     bool first = true;
      17           14 :     std::stringstream ss;
      18           14 :     ss << "[";
      19           42 :     for (const int64_t& x : shape) {
      20           14 :         if (first) {
      21           10 :             first = false;
      22           10 :             ss << x;
      23              :         } else {
      24            4 :             ss << ", " << x;
      25              :         }
      26              :     }
      27           14 :     ss << "]";
      28           28 :     return ss.str();
      29           14 : }
      30              : 
      31            2 : std::string AicpuSdModelStatistic::GetRangeString(
      32              :     const std::vector<InAndOutParamStatInfo>& statInfos, size_t startIdx, size_t endIdx) const
      33              : {
      34            2 :     std::stringstream minSizeStream;
      35            2 :     std::stringstream maxSizeStream;
      36            2 :     std::stringstream minShapeStream;
      37            2 :     std::stringstream maxShapeStream;
      38            9 :     for (size_t idx = startIdx; idx <= endIdx; ++idx) {
      39            7 :         const auto& curInfo = statInfos[idx];
      40            7 :         if (idx != startIdx) {
      41            5 :             minSizeStream << ", ";
      42            5 :             maxSizeStream << ", ";
      43            5 :             minShapeStream << ", ";
      44            5 :             maxShapeStream << ", ";
      45              :         }
      46            7 :         minSizeStream << curInfo.minSize;
      47            7 :         maxSizeStream << curInfo.maxSize;
      48            7 :         minShapeStream << FormatShapeToString(curInfo.minShape);
      49            7 :         maxShapeStream << FormatShapeToString(curInfo.maxShape);
      50              :     }
      51            2 :     std::stringstream strStream;
      52            2 :     strStream << "min_size=[" << minSizeStream.str() << "], max_size=[" << maxSizeStream.str() << "], min_shape=["
      53            2 :               << minShapeStream.str() << "], max_shape=[" << maxShapeStream.str() << "]";
      54            4 :     return strStream.str();
      55            2 : }
      56              : 
      57         2050 : AicpuSdModelStatistic::~AicpuSdModelStatistic() {}
      58              : 
      59         2050 : AicpuSdModelStatistic::AicpuSdModelStatistic() {}
      60              : 
      61           66 : AicpuSdModelStatistic& AicpuSdModelStatistic::GetInstance()
      62              : {
      63           66 :     static AicpuSdModelStatistic instance;
      64           66 :     return instance;
      65              : }
      66              : 
      67            4 : void AicpuSdModelStatistic::StatNNModelExecTime(const uint32_t modelId)
      68              : {
      69            4 :     if (modelId >= MAX_MODEL_COUNT) {
      70            1 :         aicpusd_err("invalid modelId:%u, max:%u", modelId, MAX_MODEL_COUNT);
      71            3 :         return;
      72              :     }
      73            3 :     aicpusd_info(
      74              :         "enter curMax(us):%llu, curMin(us):%llu", modelStatArray_[modelId].maxExecTime,
      75              :         modelStatArray_[modelId].minExecTime);
      76            3 :     if (!modelStatArray_[modelId].useFlag) {
      77            2 :         aicpusd_info("only stat static model, dynamic model no need stat");
      78            2 :         return;
      79              :     }
      80            1 :     modelStatArray_[modelId].totalCnt++;
      81            1 :     auto curTime = std::chrono::steady_clock::now();
      82            1 :     auto timeGap = curTime - modelStatArray_[modelId].modelStartTime;
      83              :     const uint64_t curExecTime =
      84            1 :         static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(timeGap).count());
      85            1 :     modelStatArray_[modelId].totalTime += curExecTime;
      86            1 :     AicpuUtil::UpdateMaxAndSubMaxData(
      87            1 :         modelStatArray_[modelId].maxExecTime, modelStatArray_[modelId].subMaxExecTime, curExecTime);
      88            1 :     AicpuUtil::UpdateMinData(modelStatArray_[modelId].minExecTime, curExecTime);
      89            1 :     aicpusd_info(
      90              :         "after update curMax(us):%llu, curMin(us):%llu, curStat(us):%llu", modelStatArray_[modelId].maxExecTime,
      91              :         modelStatArray_[modelId].minExecTime, curExecTime);
      92              : }
      93              : 
      94            4 : void AicpuSdModelStatistic::UpdateStatVecInfo(
      95              :     InAndOutParamStatInfo& dstStat, const uint64_t totalSize, const int64_t* shapes) const
      96              : {
      97            4 :     if ((dstStat.minSize == 0) || (totalSize < dstStat.minSize)) {
      98            2 :         dstStat.minSize = totalSize;
      99            2 :         dstStat.minShape.assign(&(shapes[1]), &(shapes[1]) + shapes[0]);
     100              :     }
     101              : 
     102            4 :     if (totalSize > dstStat.maxSize) {
     103            1 :         dstStat.maxSize = totalSize;
     104            1 :         dstStat.maxShape.assign(&(shapes[1]), &(shapes[1]) + shapes[0]);
     105              :     }
     106            4 : }
     107              : 
     108            5 : void AicpuSdModelStatistic::AddNewDataToStatVec(
     109              :     std::vector<InAndOutParamStatInfo>& delVec, const uint64_t totalSize, const int64_t* shapes) const
     110              : {
     111            5 :     InAndOutParamStatInfo tempNode;
     112            5 :     tempNode.minSize = totalSize;
     113            5 :     tempNode.minShape.assign(&(shapes[1]), &(shapes[1]) + shapes[0]);
     114            5 :     tempNode.maxSize = totalSize;
     115            5 :     tempNode.maxShape.assign(&(shapes[1]), &(shapes[1]) + shapes[0]);
     116            5 :     aicpusd_info(
     117              :         "max size:%llu, min size:%llu, min shape size:%zu, max shape size:%zu", tempNode.maxSize, tempNode.minSize,
     118              :         tempNode.minShape.size(), tempNode.maxShape.size());
     119            5 :     delVec.emplace_back(tempNode);
     120            5 : }
     121              : 
     122            4 : void AicpuSdModelStatistic::StatNNModelInput(
     123              :     const uint32_t modelId, const std::vector<uint64_t>& totalSizeList, std::vector<ModelConfigTensorDesc>& curStatVec)
     124              : {
     125            4 :     if ((modelId >= MAX_MODEL_COUNT) || (totalSizeList.size() != curStatVec.size())) {
     126            1 :         aicpusd_err(
     127              :             "invalid modelId:%u, max:%u, size list:%zu, stat list:%zu", modelId, MAX_MODEL_COUNT, totalSizeList.size(),
     128              :             curStatVec.size());
     129            1 :         return;
     130              :     }
     131            3 :     std::vector<InAndOutParamStatInfo>& delVec = modelStatArray_[modelId].inputStatVec;
     132            3 :     const size_t delVecSize = delVec.size();
     133            3 :     const size_t curVecSize = curStatVec.size();
     134            3 :     const size_t curMinSize = delVecSize < curVecSize ? delVecSize : curVecSize;
     135            5 :     for (size_t index = 0; index < curMinSize; index++) {
     136            2 :         UpdateStatVecInfo(delVec[index], totalSizeList[index], &(curStatVec[index].shape[0]));
     137            2 :         aicpusd_info(
     138              :             "index:%zu max size:%llu, min size:%llu, min shape size:%zu, max shape size:%zu", index,
     139              :             delVec[index].maxSize, delVec[index].minSize, delVec[index].minShape.size(), delVec[index].maxShape.size());
     140              :     }
     141              : 
     142            3 :     if (delVecSize < curVecSize) {
     143            4 :         for (size_t i = curMinSize; i < curVecSize; i++) {
     144            2 :             AddNewDataToStatVec(delVec, totalSizeList[i], &(curStatVec[i].shape[0]));
     145              :         }
     146              :     }
     147              : }
     148              : 
     149            7 : void AicpuSdModelStatistic::InitModelStatInfo()
     150              : {
     151         7175 :     for (uint32_t index = 0; index < MAX_MODEL_COUNT; index++) {
     152         7168 :         modelStatArray_[index].useFlag = false;
     153         7168 :         modelStatArray_[index].maxExecTime = 0UL;
     154         7168 :         modelStatArray_[index].minExecTime = 0UL;
     155         7168 :         modelStatArray_[index].subMaxExecTime = 0UL;
     156         7168 :         modelStatArray_[index].totalTime = 0UL;
     157         7168 :         modelStatArray_[index].totalCnt = 0UL;
     158         7168 :         modelStatArray_[index].inputStatVec.clear();
     159         7168 :         modelStatArray_[index].outputStatVec.clear();
     160              :     }
     161            7 : }
     162              : 
     163           10 : void AicpuSdModelStatistic::MarNNModelStartTime(const uint32_t modelId)
     164              : {
     165           10 :     if (modelId >= MAX_MODEL_COUNT) {
     166            1 :         aicpusd_err("invalid modelId:%u, max:%u", modelId, MAX_MODEL_COUNT);
     167            1 :         return;
     168              :     }
     169            9 :     modelStatArray_[modelId].modelStartTime = std::chrono::steady_clock::now();
     170            9 :     modelStatArray_[modelId].useFlag = true;
     171            9 :     aicpusd_info("mark mode:%u started", modelId);
     172              : }
     173              : 
     174            7 : void AicpuSdModelStatistic::StatNNModelOutput(
     175              :     const uint32_t modelId, const RuntimeTensorDesc* const tensorDesc, const int32_t nextIndex)
     176              : {
     177            7 :     if ((modelId >= MAX_MODEL_COUNT) || (nextIndex <= 0) || (tensorDesc == nullptr)) {
     178            1 :         aicpusd_err("invalid modelId:%u, max:%u, nextIndex:%d", modelId, MAX_MODEL_COUNT, nextIndex);
     179            2 :         return;
     180              :     }
     181            6 :     const uint32_t curIndex = static_cast<uint32_t>(nextIndex) - 1U;
     182            6 :     std::vector<InAndOutParamStatInfo>& delVec = modelStatArray_[modelId].outputStatVec;
     183            6 :     const size_t curDelSize = delVec.size();
     184            6 :     int64_t curTotalSize = 0;
     185              :     const int32_t ret =
     186            6 :         AicpuUtil::CalcDataSizeByShape(&(tensorDesc->shape[1]), tensorDesc->shape[0], tensorDesc->dtype, curTotalSize);
     187            6 :     if ((ret != AICPU_SCHEDULE_OK) || (curTotalSize < 0)) {
     188            1 :         aicpusd_warn("cannot get size from tensordesc, ret[%d], size[%lld]", ret, curTotalSize);
     189            1 :         return;
     190              :     }
     191            5 :     const uint64_t dataSize = static_cast<uint64_t>(curTotalSize);
     192            5 :     if (curIndex < curDelSize) {
     193            2 :         UpdateStatVecInfo(delVec[curIndex], dataSize, &(tensorDesc->shape[0]));
     194            2 :         aicpusd_info(
     195              :             "max size:%llu, min size:%llu, min shape size:%zu, max shape size:%zu", delVec[curIndex].maxSize,
     196              :             delVec[curIndex].minSize, delVec[curIndex].minShape.size(), delVec[curIndex].maxShape.size());
     197            3 :     } else if (curIndex == curDelSize) {
     198            2 :         AddNewDataToStatVec(delVec, dataSize, &(tensorDesc->shape[0]));
     199              :     } else {
     200            1 :         delVec.resize(curIndex);
     201            1 :         AddNewDataToStatVec(delVec, dataSize, &(tensorDesc->shape[0]));
     202              :     }
     203              : }
     204              : 
     205           25 : void AicpuSdModelStatistic::PrintOutModelStatInfo()
     206              : {
     207        25625 :     for (uint32_t modelId = 0U; modelId < MAX_MODEL_COUNT; modelId++) {
     208        25600 :         if (modelStatArray_[modelId].useFlag) {
     209            1 :             DumpOutModelMetrics(modelId);
     210              :         }
     211              :     }
     212           25 : }
     213            1 : void AicpuSdModelStatistic::DumpOutModelMetrics(const uint32_t modelId)
     214              : {
     215            1 :     ModelStatInfo& curModeInfo = modelStatArray_[modelId];
     216            1 :     aicpusd_run_info(
     217              :         "model_metrics:name=%s, min_exec_time=%llu us, max_exec_time=%llu us, "
     218              :         "sub_max_exec_time=%llu us, total_exec_time=%llu us, total_exec_num=%llu.",
     219              :         std::to_string(modelId).c_str(), curModeInfo.minExecTime, curModeInfo.maxExecTime, curModeInfo.subMaxExecTime,
     220              :         curModeInfo.totalTime, curModeInfo.totalCnt);
     221            1 :     constexpr size_t maxCntPrintPreLine = 10;
     222            1 :     const size_t inputSize = curModeInfo.inputStatVec.size();
     223            1 :     const size_t outputSize = curModeInfo.outputStatVec.size();
     224            2 :     for (size_t inputIdx = 0; inputIdx < inputSize; inputIdx += maxCntPrintPreLine) {
     225            1 :         const size_t startIdx = inputIdx;
     226            1 :         const size_t endIdx =
     227            1 :             (inputIdx + maxCntPrintPreLine) > inputSize ? (inputSize - 1) : (inputIdx + maxCntPrintPreLine - 1);
     228            1 :         aicpusd_run_info(
     229              :             "model_metrics:name=%s, input[%zu-%zu], %s.", std::to_string(modelId).c_str(), startIdx, endIdx,
     230              :             GetRangeString(curModeInfo.inputStatVec, startIdx, endIdx).c_str());
     231              :     }
     232              : 
     233            2 :     for (size_t outputIdx = 0; outputIdx < outputSize; outputIdx += maxCntPrintPreLine) {
     234            1 :         const size_t startIdx = outputIdx;
     235            1 :         const size_t endIdx =
     236            1 :             (outputIdx + maxCntPrintPreLine) > outputSize ? (outputSize - 1) : (outputIdx + maxCntPrintPreLine - 1);
     237            1 :         aicpusd_run_info(
     238              :             "model_metrics:name=%s, output[%zu-%zu], %s.", std::to_string(modelId).c_str(), startIdx, endIdx,
     239              :             GetRangeString(curModeInfo.outputStatVec, startIdx, endIdx).c_str());
     240              :     }
     241            1 : }
     242              : } // namespace AicpuSchedule
        

Generated by: LCOV version 2.0-1