LCOV - code coverage report
Current view: top level - legacy/ascend950/unified_platform/ccu/ccu_microcode - ccu_assist.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.7 % 106 94
Test Date: 2026-07-28 12:11:00 Functions: 100.0 % 15 15

            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 "ccu_assist.h"
      12              : 
      13              : #include "orion_adapter_rts.h"
      14              : #include "exception_util.h"
      15              : #include "ccu_api_exception.h"
      16              : 
      17              : #include "ccu_microcode.h"
      18              : 
      19              : namespace Hccl {
      20              : namespace CcuRep {
      21              : 
      22              : constexpr uint64_t SetBits(uint16_t start, uint16_t end)
      23              : {
      24              :     return ((uint64_t(1) << (end - start + 1)) - uint64_t(1)) << start;
      25              : }
      26              : 
      27          743 : constexpr uint64_t SetBits(uint16_t end)
      28              : {
      29          743 :     return ((uint64_t(1) << (end + 1)) - uint64_t(1));
      30              : }
      31              : 
      32              : // 辅助函数
      33           28 : uint64_t GetMaxLoopIterNum()
      34              : {
      35           28 :     constexpr uint16_t loopNumBitNum = 12;
      36           28 :     return SetBits(loopNumBitNum);
      37              : }
      38              : 
      39           75 : uint64_t GetLoopParam(uint64_t loopCtxId, uint64_t gsaOffset, uint64_t loopIterNum)
      40              : {
      41           75 :     constexpr uint16_t ctxIdBitNum     = 8;
      42           75 :     constexpr uint16_t ctxIdShiftBit   = 45;
      43           75 :     constexpr uint16_t gsaBitNum       = 32;
      44           75 :     constexpr uint16_t gsaShiftBit     = 13;
      45           75 :     constexpr uint16_t loopNumBitNum   = 13;
      46           75 :     constexpr uint16_t loopNumShiftBit = 0;
      47           75 :     return ((loopCtxId & SetBits(ctxIdBitNum)) << ctxIdShiftBit) | ((gsaOffset & SetBits(gsaBitNum)) << gsaShiftBit)
      48           75 :            | ((loopIterNum & SetBits(loopNumBitNum)) << loopNumShiftBit);
      49              : }
      50              : 
      51           45 : uint64_t GetParallelParam(uint64_t repeatNum, uint64_t repeatLoopIndex, uint64_t totalLoopNum)
      52              : {
      53           45 :     constexpr uint16_t repeatBitNum       = 7;
      54           45 :     constexpr uint16_t repeatNumShiftBit  = 55;
      55           45 :     constexpr uint16_t repeatLoopBitNum   = 7;
      56           45 :     constexpr uint16_t repeatLoopShiftBit = 48;
      57           45 :     constexpr uint16_t totalLoopBitNum    = 7;
      58           45 :     constexpr uint16_t totalLoopShiftBit  = 41;
      59           45 :     return ((repeatNum & SetBits(repeatBitNum)) << repeatNumShiftBit)
      60           45 :            | ((repeatLoopIndex & SetBits(repeatLoopBitNum)) << repeatLoopShiftBit)
      61           45 :            | ((totalLoopNum & SetBits(totalLoopBitNum)) << totalLoopShiftBit);
      62              : }
      63              : 
      64           13 : uint16_t ParseRepeatNumFromParallelParam(uint64_t parallelParam)
      65              : {
      66           13 :     constexpr uint16_t repeatBitNum       = 7; // 7: repeat num 占 7 bits
      67           13 :     constexpr uint16_t repeatNumShiftBit  = 55; // 55: repeat num占[61:55]位置
      68           13 :     return ( parallelParam >> repeatNumShiftBit) & SetBits(repeatBitNum);
      69              : }
      70              : 
      71           39 : uint64_t GetOffsetParam(uint64_t gsaOffset, uint64_t msOffset, uint64_t ckeOffset)
      72              : {
      73           39 :     constexpr uint16_t gsaBitNum   = 32;
      74           39 :     constexpr uint16_t gsaShiftBit = 21;
      75           39 :     constexpr uint16_t msBitNum    = 11;
      76           39 :     constexpr uint16_t msShiftBit  = 10;
      77           39 :     constexpr uint16_t ckeBitNum   = 10;
      78           39 :     constexpr uint16_t ckeShiftBit = 0;
      79           39 :     return ((gsaOffset & SetBits(gsaBitNum)) << gsaShiftBit) | ((msOffset & SetBits(msBitNum)) << msShiftBit)
      80           39 :            | ((ckeOffset & SetBits(ckeBitNum)) << ckeShiftBit);
      81              : }
      82              : 
      83           75 : uint64_t GetToken(uint64_t tokenId, uint64_t tokenValue, uint64_t tokenValid)
      84              : {
      85           75 :     constexpr uint16_t tokenValidBitNum   = 1;
      86           75 :     constexpr uint16_t tokenValidShiftBit = 52;
      87           75 :     constexpr uint16_t tokenIdBitNum      = 20;
      88           75 :     constexpr uint16_t tokenIdShiftBit    = 32;
      89           75 :     constexpr uint16_t tokenValueBitNum   = 32;
      90           75 :     constexpr uint16_t tokenValueShiftBit = 0;
      91           75 :     return ((tokenValid & SetBits(tokenValidBitNum)) << tokenValidShiftBit)
      92           75 :            | ((tokenId & SetBits(tokenIdBitNum)) << tokenIdShiftBit)
      93           75 :            | ((tokenValue & SetBits(tokenValueBitNum)) << tokenValueShiftBit);
      94              : }
      95              : 
      96            1 : uint64_t GetExpansionParam(uint64_t expansionNum)
      97              : {
      98            1 :     constexpr uint64_t expansionNum2        = 2;
      99            1 :     constexpr uint64_t expansionNumShiftBit = 53;
     100            1 :     return (expansionNum == expansionNum2 ? uint64_t(1) : uint64_t(2)) << expansionNumShiftBit; // Bit[53-54], 00: 1, 01: 2, 10: 4
     101              : }
     102              : 
     103           31 : uint16_t GetCcuReduceType(ReduceOp reduceOp)
     104              : {
     105              :     static std::map<ReduceOp, uint16_t> ccuReduceTypeMap = {
     106              :         {ReduceOp::SUM, CCU_REDUCE_SUM},
     107              :         {ReduceOp::MAX, CCU_REDUCE_MAX},
     108              :         {ReduceOp::MIN, CCU_REDUCE_MIN},
     109           33 :     };
     110              : 
     111           31 :     if (ccuReduceTypeMap.find(reduceOp) == ccuReduceTypeMap.end()) {
     112            1 :         THROW<CcuApiException>("Unsupported ReduceOp[%s] for Ccu", reduceOp.Describe().c_str());
     113              :     }
     114              : 
     115           30 :     return ccuReduceTypeMap[reduceOp];
     116              : }
     117              : 
     118           20 : uint16_t GetCcuDataType(DataType dataType, ReduceOp reduceOp)
     119              : {
     120              :     static std::map<DataType, uint16_t> ccuSumDataTypeMap = {
     121              :         {DataType::FP32, 0},    {DataType::FP16, 1}, {DataType::BFP16, 2}, {DataType::HIF8, 3},  {DataType::FP8E4M3, 4},
     122              :         {DataType::FP8E5M2, 5}, {DataType::INT8, 6}, {DataType::UINT8, 7}, {DataType::INT16, 8}, {DataType::INT32, 9},
     123           22 :     };
     124              : 
     125              :     static std::map<DataType, uint16_t> ccuMaxMinDataTypeMap = {
     126              :         {DataType::FP32, 0},  {DataType::FP16, 1},  {DataType::BFP16, 2}, {DataType::INT8, 6},
     127              :         {DataType::UINT8, 7}, {DataType::INT16, 8}, {DataType::INT32, 9},
     128              : 
     129           22 :     };
     130              : 
     131           20 :     uint16_t ccuReduceType = GetCcuReduceType(reduceOp);
     132           20 :     if (ccuReduceType == CCU_REDUCE_SUM) {
     133           12 :         if (ccuSumDataTypeMap.find(dataType) == ccuSumDataTypeMap.end()) {
     134            2 :             THROW<CcuApiException>("Unsupported DataType[%s] for Ccu SUM", dataType.Describe().c_str());
     135              :         }
     136           11 :         return ccuSumDataTypeMap[dataType];
     137              :     }
     138              : 
     139            8 :     if (ccuReduceType == CCU_REDUCE_MAX || ccuReduceType == CCU_REDUCE_MIN) {
     140            8 :         if (ccuMaxMinDataTypeMap.find(dataType) == ccuMaxMinDataTypeMap.end()) {
     141            2 :             THROW<CcuApiException>("Unsupported DataType[%s] for Ccu MAX/MIN", dataType.Describe().c_str());
     142              :         }
     143            6 :         return ccuMaxMinDataTypeMap[dataType];
     144              :     }
     145              : 
     146            0 :     return ccuSumDataTypeMap[dataType];
     147              : }
     148              : 
     149            5 : uint16_t GetUBReduceType(ReduceOp reduceOp)
     150              : {
     151              :     static std::map<ReduceOp, uint16_t> ubReduceTypeMap = {
     152              :         {ReduceOp::SUM, 10},
     153              :         {ReduceOp::MAX, 8},
     154              :         {ReduceOp::MIN, 9},
     155            7 :     };
     156              : 
     157            5 :     if (ubReduceTypeMap.find(reduceOp) == ubReduceTypeMap.end()) {
     158            1 :         THROW<CcuApiException>("Unsupported reduceOp[%s] for UB Reduce", reduceOp.Describe().c_str());
     159              :     }
     160              : 
     161            4 :     return ubReduceTypeMap[reduceOp];
     162              : }
     163              : 
     164            5 : uint16_t GetUBDataType(DataType dataType)
     165              : {
     166              :     static std::map<DataType, uint16_t> ubDataTypeMap = {
     167              :         {DataType::FP32, 7},  {DataType::FP16, 6},  {DataType::BFP16, 8},  {DataType::INT8, 0},  {DataType::UINT8, 3},
     168            7 :         {DataType::INT16, 1}, {DataType::INT32, 2}, {DataType::UINT16, 4}, {DataType::UINT32, 5}};
     169              : 
     170            5 :     if (ubDataTypeMap.find(dataType) == ubDataTypeMap.end()) {
     171            1 :         THROW<CcuApiException>("Unsupported DataType[%s] for UB Reduce", dataType.Describe().c_str());
     172              :     }
     173            4 :     return ubDataTypeMap[dataType];
     174              : }
     175              : 
     176            5 : uint32_t GetReduceExpansionNum(ReduceOp reduceOp, DataType dataType, DataType outputDataType)
     177              : {
     178            5 :     uint32_t expansionNum = 1;
     179              : 
     180            5 :     if (reduceOp == ReduceOp::SUM && outputDataType == DataType::INVALID) {
     181            0 :         outputDataType = dataType;
     182              : 
     183              :         // 低精度数据格式可指定输出数据类型:fp32\bf16\fp16,如果没有指定,默认fp32
     184            0 :         if ((dataType == DataType::HIF8) || (dataType == DataType::FP8E4M3) || (dataType == DataType::FP8E5M2)
     185            0 :             || (dataType == DataType::INT8)) {
     186            0 :             outputDataType = DataType::FP32;
     187              :         }
     188              :     }
     189            5 :     expansionNum = DataTypeSizeGet(outputDataType) / DataTypeSizeGet(dataType);
     190           15 :     HCCL_INFO("Ccu low precision, expansionNum = %u", expansionNum);
     191              : 
     192            5 :     return expansionNum;
     193              : }
     194              : 
     195            9 : std::string GetReduceTypeStr(DataType dataType, ReduceOp opType)
     196              : {
     197              :     static std::map<DataType, std::string> ccuRepDataTypeStr = {
     198            0 :         {DataType::FP32, "fp32"},   {DataType::FP16, "fp16"},       {DataType::BFP16, "bf16"},
     199            0 :         {DataType::HIF8, "hif8"},   {DataType::FP8E4M3, "fp8e4m3"}, {DataType::FP8E5M2, "fp8e5m2"},
     200            0 :         {DataType::INT8, "int8"},   {DataType::UINT8, "uint8"},     {DataType::INT16, "int16"},
     201            0 :         {DataType::INT32, "int32"},
     202           21 :     };
     203              : 
     204              :     static std::map<ReduceOp, std::string> ccuRepOpTypeStr = {
     205            0 :         {ReduceOp::SUM, "sum"},
     206            0 :         {ReduceOp::MAX, "max"},
     207            0 :         {ReduceOp::MIN, "min"},
     208           14 :     };
     209              : 
     210           18 :     return ccuRepDataTypeStr[dataType] + "_" + ccuRepOpTypeStr[opType];
     211            2 : }
     212              : 
     213            7 : uint64_t GetTokenInfo(uint64_t va, uint64_t size)
     214              : {
     215              :     rtMemUbTokenInfo info;
     216            7 :     info.va   = va;
     217            7 :     info.size = size;
     218            7 :     HrtUbDevQueryInfo(QUERY_PROCESS_TOKEN, &info);
     219           14 :     return CcuRep::GetToken(info.tokenId, info.tokenValue, 1);
     220              : }
     221              : 
     222              : }; // namespace CcuRep
     223              : }; // namespace Hccl
        

Generated by: LCOV version 2.0-1