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.6 % 105 93
Test Date: 2026-08-18 17:47:01 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          672 :     constexpr uint64_t SetBits(uint16_t end) { return ((uint64_t(1) << (end + 1)) - uint64_t(1)); }
      28              : 
      29              :     // 辅助函数
      30           26 :     uint64_t GetMaxLoopIterNum()
      31              :     {
      32           26 :         constexpr uint16_t loopNumBitNum = 12;
      33           26 :         return SetBits(loopNumBitNum);
      34              :     }
      35              : 
      36           63 :     uint64_t GetLoopParam(uint64_t loopCtxId, uint64_t gsaOffset, uint64_t loopIterNum)
      37              :     {
      38           63 :         constexpr uint16_t ctxIdBitNum = 8;
      39           63 :         constexpr uint16_t ctxIdShiftBit = 45;
      40           63 :         constexpr uint16_t gsaBitNum = 32;
      41           63 :         constexpr uint16_t gsaShiftBit = 13;
      42           63 :         constexpr uint16_t loopNumBitNum = 13;
      43           63 :         constexpr uint16_t loopNumShiftBit = 0;
      44           63 :         return ((loopCtxId & SetBits(ctxIdBitNum)) << ctxIdShiftBit) | ((gsaOffset & SetBits(gsaBitNum)) << gsaShiftBit)
      45           63 :                | ((loopIterNum & SetBits(loopNumBitNum)) << loopNumShiftBit);
      46              :     }
      47              : 
      48           41 :     uint64_t GetParallelParam(uint64_t repeatNum, uint64_t repeatLoopIndex, uint64_t totalLoopNum)
      49              :     {
      50           41 :         constexpr uint16_t repeatBitNum = 7;
      51           41 :         constexpr uint16_t repeatNumShiftBit = 55;
      52           41 :         constexpr uint16_t repeatLoopBitNum = 7;
      53           41 :         constexpr uint16_t repeatLoopShiftBit = 48;
      54           41 :         constexpr uint16_t totalLoopBitNum = 7;
      55           41 :         constexpr uint16_t totalLoopShiftBit = 41;
      56           41 :         return ((repeatNum & SetBits(repeatBitNum)) << repeatNumShiftBit)
      57           41 :                | ((repeatLoopIndex & SetBits(repeatLoopBitNum)) << repeatLoopShiftBit)
      58           41 :                | ((totalLoopNum & SetBits(totalLoopBitNum)) << totalLoopShiftBit);
      59              :     }
      60              : 
      61           13 :     uint16_t ParseRepeatNumFromParallelParam(uint64_t parallelParam)
      62              :     {
      63           13 :         constexpr uint16_t repeatBitNum = 7;       // 7: repeat num 占 7 bits
      64           13 :         constexpr uint16_t repeatNumShiftBit = 55; // 55: repeat num占[61:55]位置
      65           13 :         return (parallelParam >> repeatNumShiftBit) & SetBits(repeatBitNum);
      66              :     }
      67              : 
      68           35 :     uint64_t GetOffsetParam(uint64_t gsaOffset, uint64_t msOffset, uint64_t ckeOffset)
      69              :     {
      70           35 :         constexpr uint16_t gsaBitNum = 32;
      71           35 :         constexpr uint16_t gsaShiftBit = 21;
      72           35 :         constexpr uint16_t msBitNum = 11;
      73           35 :         constexpr uint16_t msShiftBit = 10;
      74           35 :         constexpr uint16_t ckeBitNum = 10;
      75           35 :         constexpr uint16_t ckeShiftBit = 0;
      76           35 :         return ((gsaOffset & SetBits(gsaBitNum)) << gsaShiftBit) | ((msOffset & SetBits(msBitNum)) << msShiftBit)
      77           35 :                | ((ckeOffset & SetBits(ckeBitNum)) << ckeShiftBit);
      78              :     }
      79              : 
      80           72 :     uint64_t GetToken(uint64_t tokenId, uint64_t tokenValue, uint64_t tokenValid)
      81              :     {
      82           72 :         constexpr uint16_t tokenValidBitNum = 1;
      83           72 :         constexpr uint16_t tokenValidShiftBit = 52;
      84           72 :         constexpr uint16_t tokenIdBitNum = 20;
      85           72 :         constexpr uint16_t tokenIdShiftBit = 32;
      86           72 :         constexpr uint16_t tokenValueBitNum = 32;
      87           72 :         constexpr uint16_t tokenValueShiftBit = 0;
      88           72 :         return ((tokenValid & SetBits(tokenValidBitNum)) << tokenValidShiftBit)
      89           72 :                | ((tokenId & SetBits(tokenIdBitNum)) << tokenIdShiftBit)
      90           72 :                | ((tokenValue & SetBits(tokenValueBitNum)) << tokenValueShiftBit);
      91              :     }
      92              : 
      93            1 :     uint64_t GetExpansionParam(uint64_t expansionNum)
      94              :     {
      95            1 :         constexpr uint64_t expansionNum2 = 2;
      96            1 :         constexpr uint64_t expansionNumShiftBit = 53;
      97              :         return (expansionNum == expansionNum2 ? uint64_t(1) : uint64_t(2))
      98            1 :                << expansionNumShiftBit; // Bit[53-54], 00: 1, 01: 2, 10: 4
      99              :     }
     100              : 
     101           25 :     uint16_t GetCcuReduceType(ReduceOp reduceOp)
     102              :     {
     103              :         static std::map<ReduceOp, uint16_t> ccuReduceTypeMap = {
     104              :             {ReduceOp::SUM, CCU_REDUCE_SUM},
     105              :             {ReduceOp::MAX, CCU_REDUCE_MAX},
     106              :             {ReduceOp::MIN, CCU_REDUCE_MIN},
     107           27 :         };
     108              : 
     109           25 :         if (ccuReduceTypeMap.find(reduceOp) == ccuReduceTypeMap.end()) {
     110            1 :             THROW<CcuApiException>("Unsupported ReduceOp[%s] for Ccu", reduceOp.Describe().c_str());
     111              :         }
     112              : 
     113           24 :         return ccuReduceTypeMap[reduceOp];
     114              :     }
     115              : 
     116           16 :     uint16_t GetCcuDataType(DataType dataType, ReduceOp reduceOp)
     117              :     {
     118              :         static std::map<DataType, uint16_t> ccuSumDataTypeMap = {
     119              :             {DataType::FP32, 0},    {DataType::FP16, 1},    {DataType::BFP16, 2}, {DataType::HIF8, 3},
     120              :             {DataType::FP8E4M3, 4}, {DataType::FP8E5M2, 5}, {DataType::INT8, 6},  {DataType::UINT8, 7},
     121              :             {DataType::INT16, 8},   {DataType::INT32, 9},
     122           18 :         };
     123              : 
     124              :         static std::map<DataType, uint16_t> ccuMaxMinDataTypeMap = {
     125              :             {DataType::FP32, 0},  {DataType::FP16, 1},  {DataType::BFP16, 2}, {DataType::INT8, 6},
     126              :             {DataType::UINT8, 7}, {DataType::INT16, 8}, {DataType::INT32, 9},
     127              : 
     128           18 :         };
     129              : 
     130           16 :         uint16_t ccuReduceType = GetCcuReduceType(reduceOp);
     131           16 :         if (ccuReduceType == CCU_REDUCE_SUM) {
     132            8 :             if (ccuSumDataTypeMap.find(dataType) == ccuSumDataTypeMap.end()) {
     133            2 :                 THROW<CcuApiException>("Unsupported DataType[%s] for Ccu SUM", dataType.Describe().c_str());
     134              :             }
     135            7 :             return ccuSumDataTypeMap[dataType];
     136              :         }
     137              : 
     138            8 :         if (ccuReduceType == CCU_REDUCE_MAX || ccuReduceType == CCU_REDUCE_MIN) {
     139            8 :             if (ccuMaxMinDataTypeMap.find(dataType) == ccuMaxMinDataTypeMap.end()) {
     140            2 :                 THROW<CcuApiException>("Unsupported DataType[%s] for Ccu MAX/MIN", dataType.Describe().c_str());
     141              :             }
     142            6 :             return ccuMaxMinDataTypeMap[dataType];
     143              :         }
     144              : 
     145            0 :         return ccuSumDataTypeMap[dataType];
     146              :     }
     147              : 
     148            5 :     uint16_t GetUBReduceType(ReduceOp reduceOp)
     149              :     {
     150              :         static std::map<ReduceOp, uint16_t> ubReduceTypeMap = {
     151              :             {ReduceOp::SUM, 10},
     152              :             {ReduceOp::MAX, 8},
     153              :             {ReduceOp::MIN, 9},
     154            7 :         };
     155              : 
     156            5 :         if (ubReduceTypeMap.find(reduceOp) == ubReduceTypeMap.end()) {
     157            1 :             THROW<CcuApiException>("Unsupported reduceOp[%s] for UB Reduce", reduceOp.Describe().c_str());
     158              :         }
     159              : 
     160            4 :         return ubReduceTypeMap[reduceOp];
     161              :     }
     162              : 
     163            5 :     uint16_t GetUBDataType(DataType dataType)
     164              :     {
     165              :         static std::map<DataType, uint16_t> ubDataTypeMap
     166              :             = {{DataType::FP32, 7},  {DataType::FP16, 6},   {DataType::BFP16, 8},
     167              :                {DataType::INT8, 0},  {DataType::UINT8, 3},  {DataType::INT16, 1},
     168            7 :                {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            3 :     uint32_t GetReduceExpansionNum(ReduceOp reduceOp, DataType dataType, DataType outputDataType)
     177              :     {
     178            3 :         uint32_t expansionNum = 1;
     179              : 
     180            3 :         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            3 :         expansionNum = DataTypeSizeGet(outputDataType) / DataTypeSizeGet(dataType);
     190            9 :         HCCL_INFO("Ccu low precision, expansionNum = %u", expansionNum);
     191              : 
     192            3 :         return expansionNum;
     193              :     }
     194              : 
     195            5 :     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           17 :         };
     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           10 :         };
     209              : 
     210           10 :         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