LCOV - code coverage report
Current view: top level - legacy/ascend910/platform/common/unfold_cache - op_unfold_key.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 20 20
Test Date: 2026-08-04 10:52:23 Functions: 100.0 % 1 1

            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              : #ifndef __OP_UNFOLD_KEY_H__
      12              : #define __OP_UNFOLD_KEY_H__
      13              : 
      14              : #include <cstdint>
      15              : #include <functional>
      16              : #include <string>
      17              : 
      18              : #include "hccl_types.h"
      19              : #include "workflow_pub.h"
      20              : 
      21              : namespace hccl {
      22              : 
      23              : // 作为展开算子的标识符
      24              : // 注意: 由于给定通信域下, 相同数据量相同算子的算法选择是固定的, 不需要在标识符中维护algType或algName
      25              : // 注意: 图模式或单算子模式是全局固定的, 不需要在标识符中维护
      26              : struct OpUnfoldKey{
      27              :     explicit OpUnfoldKey();
      28              :     explicit OpUnfoldKey(const OpUnfoldKey& other); // 拷贝构造函数 (make_pair需要)
      29              : 
      30              :     HcclResult Init(const HcclCMDType curOpType, const HcclDataType curDataType, const HcclReduceOp curReduceType, const bool curIsZeroCopy,
      31              :         const uint64_t curInputSize, const bool curIsInplacePreSync, const HcclWorkflowMode curWorkflowMode, const bool curIsCapture,
      32              :         const uint32_t curRoot = 0);
      33              : 
      34              :     // 用于debug
      35              :     std::string GetKeyString() const;
      36              : 
      37              :     bool operator==(const OpUnfoldKey& other) const; // 重载operator==用于std::unordered_map中相等比较
      38              :     const OpUnfoldKey& operator=(const OpUnfoldKey& other); // 拷贝赋值操作符
      39              : 
      40              :     HcclCMDType opType;
      41              :     HcclDataType dataType;
      42              :     HcclReduceOp reduceType;
      43              :     bool isZeroCopy;
      44              : 
      45              :     // inputSize和outputSize是由totalCount(由rankSize+count决定)+dataType决定的, 给定通信域rankSize是固定的
      46              :     // 因为已经维护dataType, 所以inputSize/outputSize/totalCount中只需要维护任意一个即可
      47              :     // 注意: 对于alltoallv算子, cache查询不依赖具体数据量, inputSize用来区分isBigCount (0: false; 1: true)
      48              :     uint64_t inputSize;
      49              : 
      50              :     // ReduceScatter和AllReduce在开启重执行、in-place update、UserInMem > HcclBuffSize的时候,会触发前同步 (与正常算子展开逻辑不同)
      51              :     bool isInplacePreSync;
      52              : 
      53              :     // 是否为图模式 (可能存在同一个通信域下的同一个算子, 既执行图模式又执行单算子模式下的算法)
      54              :     HcclWorkflowMode workflowMode; // 0: 图模式; 1: 单算子模式
      55              : 
      56              :     // 是否为aclgraph (aclgraph资源在graph销毁时释放)
      57              :     bool isCapture;
      58              : 
      59              :     // 算子的root参数 (仅对 scatter / broadcast / reduce 三类算子生效)
      60              :     // 注意: 这三类算子在不同root下, SQE模板内的远端rank id / link句柄 / notify id不同, 必须把root纳入key
      61              :     //      其他算子root字段无意义, 固定为0即可
      62              :     // 历史兼容性: 默认值0与"root=0"语义重合, 不影响非root算子的cache命中行为
      63              :     uint32_t root;
      64              : };
      65              : 
      66              : }; // namespace hccl
      67              : 
      68              : namespace std {
      69              : 
      70              : // 全特化std::hash<OpUnfoldKey>, 用于std::unordered_map中计算哈希值
      71              : template<>
      72              : struct hash<hccl::OpUnfoldKey> {
      73            4 :     size_t operator()(const hccl::OpUnfoldKey& key) const {
      74              :         // 使用std::hash计算key中每个字段的哈希值
      75              :         std::hash<bool> hashBool;
      76              :         std::hash<uint8_t> hashUint8;
      77              :         std::hash<uint64_t> hashUint64;
      78              : 
      79              :         // 假设opType/dataType/reduceType <= 255
      80            4 :         const size_t opTypeHashValue = hashUint8(static_cast<uint8_t>(key.opType));
      81            4 :         const size_t dataTypeHashValue = hashUint8(static_cast<uint8_t>(key.dataType));
      82            4 :         const size_t reduceTypeHashValue = hashUint8(static_cast<uint8_t>(key.reduceType));
      83              : 
      84            4 :         const size_t isZeroCopyHashValue = hashBool(key.isZeroCopy);
      85            4 :         const size_t inputSizeHashValue = hashUint64(key.inputSize);
      86            4 :         const size_t isInplacePreSyncHashValue = hashBool(key.isInplacePreSync);
      87              : 
      88              :         // 假设workflowMode <= 255
      89            4 :         const size_t workflowModeHashValue = hashUint8(static_cast<uint8_t>(key.workflowMode));
      90              : 
      91            4 :         const size_t isCaptureHashValue = hashBool(key.isCapture);
      92              : 
      93              :         // 加入root字段的哈希: 避免不同root (scatter/broadcast/reduce) 在同一桶内冲突
      94            4 :         const size_t rootHashValue = std::hash<uint32_t>{}(key.root);
      95              : 
      96              :         // 简单的哈希混合
      97            4 :         size_t hashValue = opTypeHashValue;
      98            4 :         hashValue ^= dataTypeHashValue;
      99            4 :         hashValue ^= reduceTypeHashValue;
     100            4 :         hashValue ^= isZeroCopyHashValue;
     101            4 :         hashValue ^= inputSizeHashValue;
     102            4 :         hashValue ^= isInplacePreSyncHashValue;
     103            4 :         hashValue ^= workflowModeHashValue;
     104            4 :         hashValue ^= isCaptureHashValue;
     105            4 :         hashValue ^= rootHashValue;
     106              : 
     107            4 :         return hashValue;
     108              :     }
     109              : };
     110              : 
     111              : } // namespace std
     112              : 
     113              : #endif // __OP_UNFOLD_KEY_H__
        

Generated by: LCOV version 2.0-1