LCOV - code coverage report
Current view: top level - base_comm/resources/ccu/ccu_microcode - ccu_assist.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 74.7 % 87 65
Test Date: 2026-07-28 12:11:00 Functions: 83.3 % 12 10

            Line data    Source code
       1              : /*
       2              :  * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
       3              :  * Description: ccu context header file
       4              :  * Create: 2025-02-18
       5              :  */
       6              : 
       7              : #include "ccu_assist_v1.h"
       8              : 
       9              : #include "ccu_microcode_v1.h"
      10              : 
      11              : #include "hcomm_adapter_rts.h"
      12              : 
      13              : #include "exception_util.h" // todo: 需要统一整改为不抛异常
      14              : #include "ccu_api_exception.h"
      15              : 
      16              : namespace hcomm {
      17              : namespace CcuRep {
      18              : 
      19              : constexpr uint64_t SetBits(uint16_t start, uint16_t end)
      20              : {
      21              :     return ((uint64_t(1) << (end - start + 1)) - uint64_t(1)) << start;
      22              : }
      23              : 
      24         2871 : constexpr uint64_t SetBits(uint16_t end)
      25              : {
      26         2871 :     return ((uint64_t(1) << (end + 1)) - uint64_t(1));
      27              : }
      28              : 
      29           19 : uint64_t GetLoopParam(uint64_t loopCtxId, uint64_t gsaOffset, uint64_t loopIterNum)
      30              : {
      31           19 :     constexpr uint16_t ctxIdBitNum     = 8;
      32           19 :     constexpr uint16_t ctxIdShiftBit   = 45;
      33           19 :     constexpr uint16_t gsaBitNum       = 32;
      34           19 :     constexpr uint16_t gsaShiftBit     = 13;
      35           19 :     constexpr uint16_t loopNumBitNum   = 13;
      36           19 :     constexpr uint16_t loopNumShiftBit = 0;
      37           19 :     return ((loopCtxId & SetBits(ctxIdBitNum)) << ctxIdShiftBit) | ((gsaOffset & SetBits(gsaBitNum)) << gsaShiftBit)
      38           19 :            | ((loopIterNum & SetBits(loopNumBitNum)) << loopNumShiftBit);
      39              : }
      40              : 
      41           10 : uint64_t GetParallelParam(uint64_t repeatNum, uint64_t repeatLoopIndex, uint64_t totalLoopNum)
      42              : {
      43           10 :     constexpr uint16_t repeatBitNum       = 7;
      44           10 :     constexpr uint16_t repeatNumShiftBit  = 55;
      45           10 :     constexpr uint16_t repeatLoopBitNum   = 7;
      46           10 :     constexpr uint16_t repeatLoopShiftBit = 48;
      47           10 :     constexpr uint16_t totalLoopBitNum    = 7;
      48           10 :     constexpr uint16_t totalLoopShiftBit  = 41;
      49           10 :     return ((repeatNum & SetBits(repeatBitNum)) << repeatNumShiftBit)
      50           10 :            | ((repeatLoopIndex & SetBits(repeatLoopBitNum)) << repeatLoopShiftBit)
      51           10 :            | ((totalLoopNum & SetBits(totalLoopBitNum)) << totalLoopShiftBit);
      52              : }
      53              : 
      54            0 : uint64_t GetParallelParamV2(uint64_t repeatNum, uint64_t repeatLoopIndex, uint64_t totalLoopNum)
      55              : {
      56            0 :     constexpr uint16_t loopNumBitNum      = 10;
      57            0 :     constexpr uint16_t loopNumShiftBit    = 0;
      58            0 :     constexpr uint16_t repeatLoopBitNum   = 9;
      59            0 :     constexpr uint16_t repeatLoopShiftBit = 10;
      60            0 :     constexpr uint16_t extendBitNum       = 9;
      61            0 :     constexpr uint16_t extendShiftBit     = 19;
      62            0 :     return ((totalLoopNum & SetBits(loopNumBitNum)) << loopNumShiftBit)
      63            0 :            | ((repeatLoopIndex & SetBits(repeatLoopBitNum)) << repeatLoopShiftBit)
      64            0 :            | ((repeatNum & SetBits(extendBitNum)) << extendShiftBit);
      65              : }
      66              : 
      67           10 : uint64_t GetOffsetParam(uint64_t gsaOffset, uint64_t msOffset, uint64_t ckeOffset)
      68              : {
      69           10 :     constexpr uint16_t gsaBitNum   = 32;
      70           10 :     constexpr uint16_t gsaShiftBit = 21;
      71           10 :     constexpr uint16_t msBitNum    = 11;
      72           10 :     constexpr uint16_t msShiftBit  = 10;
      73           10 :     constexpr uint16_t ckeBitNum   = 10;
      74           10 :     constexpr uint16_t ckeShiftBit = 0;
      75           10 :     return ((gsaOffset & SetBits(gsaBitNum)) << gsaShiftBit) | ((msOffset & SetBits(msBitNum)) << msShiftBit)
      76           10 :            | ((ckeOffset & SetBits(ckeBitNum)) << ckeShiftBit);
      77              : }
      78              : 
      79          918 : uint64_t GetToken(uint64_t tokenId, uint64_t tokenValue, uint64_t tokenValid)
      80              : {
      81          918 :     constexpr uint16_t tokenValidBitNum   = 1;
      82          918 :     constexpr uint16_t tokenValidShiftBit = 52;
      83          918 :     constexpr uint16_t tokenIdBitNum      = 20;
      84          918 :     constexpr uint16_t tokenIdShiftBit    = 32;
      85          918 :     constexpr uint16_t tokenValueBitNum   = 32;
      86          918 :     constexpr uint16_t tokenValueShiftBit = 0;
      87          918 :     return ((tokenValid & SetBits(tokenValidBitNum)) << tokenValidShiftBit)
      88          918 :            | ((tokenId & SetBits(tokenIdBitNum)) << tokenIdShiftBit)
      89          918 :            | ((tokenValue & SetBits(tokenValueBitNum)) << tokenValueShiftBit);
      90              : }
      91              : 
      92            0 : uint64_t CcuCombineTokenInfo(uint64_t tokenId, uint64_t tokenValue, uint64_t tokenValid)
      93              : {
      94            0 :     return GetToken(tokenId, tokenValue, tokenValid);
      95              : }
      96              : 
      97            6 : uint16_t GetCcuReduceType(Hccl::ReduceOp reduceOp)
      98              : {
      99              :     static std::map<Hccl::ReduceOp, uint16_t> ccuReduceTypeMap = {
     100              :         {Hccl::ReduceOp::SUM, CCU_REDUCE_SUM},
     101              :         {Hccl::ReduceOp::MAX, CCU_REDUCE_MAX},
     102              :         {Hccl::ReduceOp::MIN, CCU_REDUCE_MIN},
     103            8 :     };
     104              : 
     105            6 :     if (ccuReduceTypeMap.find(reduceOp) == ccuReduceTypeMap.end()) {
     106            0 :         Hccl::THROW<Hccl::CcuApiException>("Unsupported ReduceOp[%s] for Ccu", reduceOp.Describe().c_str());
     107              :     }
     108              : 
     109            6 :     return ccuReduceTypeMap[reduceOp];
     110              : }
     111              : 
     112            4 : uint16_t GetCcuDataType(Hccl::DataType dataType, Hccl::ReduceOp reduceOp)
     113              : {
     114              :     static std::map<Hccl::DataType, uint16_t> ccuSumDataTypeMap = {
     115              :         {Hccl::DataType::FP32, 0},    {Hccl::DataType::FP16, 1}, {Hccl::DataType::BFP16, 2}, {Hccl::DataType::HIF8, 3},  {Hccl::DataType::FP8E4M3, 4},
     116              :         {Hccl::DataType::FP8E5M2, 5}, {Hccl::DataType::INT8, 6}, {Hccl::DataType::UINT8, 7}, {Hccl::DataType::INT16, 8}, {Hccl::DataType::INT32, 9},
     117            6 :     };
     118              : 
     119              :     static std::map<Hccl::DataType, uint16_t> ccuMaxMinDataTypeMap = {
     120              :         {Hccl::DataType::FP32, 0},  {Hccl::DataType::FP16, 1},  {Hccl::DataType::BFP16, 2}, {Hccl::DataType::INT8, 6},
     121              :         {Hccl::DataType::UINT8, 7}, {Hccl::DataType::INT16, 8}, {Hccl::DataType::INT32, 9},
     122              : 
     123            6 :     };
     124              : 
     125            4 :     uint16_t ccuReduceType = GetCcuReduceType(reduceOp);
     126            4 :     if (ccuReduceType == CCU_REDUCE_SUM) {
     127            4 :         if (ccuSumDataTypeMap.find(dataType) == ccuSumDataTypeMap.end()) {
     128            0 :             Hccl::THROW<Hccl::CcuApiException>("Unsupported Hccl::DataType[%s] for Ccu SUM", dataType.Describe().c_str());
     129              :         }
     130            4 :         return ccuSumDataTypeMap[dataType];
     131              :     }
     132              : 
     133            0 :     if (ccuReduceType == CCU_REDUCE_MAX || ccuReduceType == CCU_REDUCE_MIN) {
     134            0 :         if (ccuMaxMinDataTypeMap.find(dataType) == ccuMaxMinDataTypeMap.end()) {
     135            0 :             Hccl::THROW<Hccl::CcuApiException>("Unsupported Hccl::DataType[%s] for Ccu MAX/MIN", dataType.Describe().c_str());
     136              :         }
     137            0 :         return ccuMaxMinDataTypeMap[dataType];
     138              :     }
     139              : 
     140            0 :     return ccuSumDataTypeMap[dataType];
     141              : }
     142              : 
     143            2 : uint16_t GetUBReduceType(Hccl::ReduceOp reduceOp)
     144              : {
     145              :     static std::map<Hccl::ReduceOp, uint16_t> ubReduceTypeMap = {
     146              :         {Hccl::ReduceOp::SUM, 10},
     147              :         {Hccl::ReduceOp::MAX, 8},
     148              :         {Hccl::ReduceOp::MIN, 9},
     149            4 :     };
     150              : 
     151            2 :     if (ubReduceTypeMap.find(reduceOp) == ubReduceTypeMap.end()) {
     152            0 :         Hccl::THROW<Hccl::CcuApiException>("Unsupported reduceOp[%s] for UB Reduce", reduceOp.Describe().c_str());
     153              :     }
     154              : 
     155            2 :     return ubReduceTypeMap[reduceOp];
     156              : }
     157              : 
     158            2 : uint16_t GetUBDataType(Hccl::DataType dataType)
     159              : {
     160              :     static std::map<Hccl::DataType, uint16_t> ubDataTypeMap = {
     161              :         {Hccl::DataType::FP32, 7},  {Hccl::DataType::FP16, 6},  {Hccl::DataType::BFP16, 8},  {Hccl::DataType::INT8, 0},  {Hccl::DataType::UINT8, 3},
     162            4 :         {Hccl::DataType::INT16, 1}, {Hccl::DataType::INT32, 2}, {Hccl::DataType::UINT16, 4}, {Hccl::DataType::UINT32, 5}};
     163              : 
     164            2 :     if (ubDataTypeMap.find(dataType) == ubDataTypeMap.end()) {
     165            0 :         Hccl::THROW<Hccl::CcuApiException>("Unsupported Hccl::DataType[%s] for UB Reduce", dataType.Describe().c_str());
     166              :     }
     167            2 :     return ubDataTypeMap[dataType];
     168              : }
     169              : 
     170           54 : uint64_t GetTokenInfo(uint64_t va, uint64_t size)
     171              : {
     172           54 :     rtMemUbTokenInfo info{};
     173           54 :     info.va   = va;
     174           54 :     info.size = size;
     175           54 :     if (RtsUbDevQueryInfo(QUERY_PROCESS_TOKEN, info) != HcclResult::HCCL_SUCCESS) {
     176            0 :         Hccl::THROW<Hccl::CcuApiException>("failed to query tokenInfo.");
     177              :     }
     178          108 :     return CcuRep::GetToken(info.tokenId, info.tokenValue, 1);
     179              : }
     180              : 
     181              : }; // namespace CcuRep
     182              : }; // namespace hcomm
        

Generated by: LCOV version 2.0-1