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 SNAP_SHOT_PARSER_H
12 : #define SNAP_SHOT_PARSER_H
13 :
14 : #include <hccl/hccl_types.h>
15 : #include "hccl_params_pub.h"
16 : #include "topo_info.h"
17 : #include "rank_table_info.h"
18 : #include "ccu_rank_group.h"
19 :
20 : namespace Hccl {
21 : constexpr u32 SNAPSHOT_VERSION_SIZE = 128;
22 : constexpr u32 GROUP_NAME_SIZE = 128;
23 :
24 : struct SnapShotDynamic {
25 : vector<std::pair<u32, RankId>> levelRankPairs{};
26 : vector<std::pair<LinkGroup, u32>> linkGroupPair{};
27 : u32 submittedOpCnt{0};
28 : u32 opMode{0};
29 : OpExecuteConfig opExecuteConfig; // 存储算子粒度和通信域加速模式
30 : OpExecuteConfig commExecuteConfig;
31 : bool isLoadOp{false}; // 是否已加载过算子
32 : };
33 :
34 : struct SnapShotPub {
35 : char snapshotVersion[SNAPSHOT_VERSION_SIZE]; // 快照版本 一般不超过 128 Byte
36 : char cannVersion[SNAPSHOT_VERSION_SIZE]; // cann版本 一般不超过 128 Byte
37 : char hcclVersion[SNAPSHOT_VERSION_SIZE]; // hccl版本 一般不超过 128 Byte
38 : uint32_t step;
39 : };
40 :
41 : struct SnapShotComm {
42 : HcclCommConfig config;
43 : Hccl::CommParams commParams;
44 : RankTableInfo rankTableInfo;
45 : TopoInfo topoInfo;
46 : vector<std::pair<u32, RankId>>
47 : levelRankPairs; /* 该信息通过算法层提供的接口获取,用于获取建链时使用的linkData数据——GetPaths(u32 level, RankId
48 : sRankId, RankId dRankId)-> LinkData(const NetInstance::Path &path) */
49 : u32 submittedOpCnt;
50 : u32 opMode{0};
51 : vector<std::pair<LinkGroup, u32>> linkGroupPair{};
52 : OpExecuteConfig opExecuteConfig; // 存储算子粒度和通信域加速模式
53 : OpExecuteConfig commExecuteConfig;
54 : bool isLoadOp{false}; // 是否已加载过算子
55 : };
56 : struct Snapshot {
57 : uint32_t size;
58 : char groupName[GROUP_NAME_SIZE]; // 通信域名称 一般不超过 128 Byte
59 : SnapShotComm snapShotComm; // comm save(size, name, tembinary)
60 : };
61 :
62 : struct SnapShotSubComm {
63 : HcclCommConfig config;
64 : Hccl::CommParams commParams;
65 : vector<RankId> rankIds; /* 子通信域中rank在全局通信域中的rank id组成的vector */
66 : vector<std::pair<u32, RankId>> levelRankPairs;
67 : u32 submittedOpCnt;
68 : u32 opMode{0};
69 : vector<std::pair<LinkGroup, u32>> linkGroupPair{};
70 : OpExecuteConfig opExecuteConfig; // 存储算子粒度和通信域加速模式
71 : OpExecuteConfig commExecuteConfig;
72 : bool isLoadOp{false}; // 是否已加载过算子
73 : };
74 : struct SubSnapshot {
75 : uint32_t size;
76 : char groupName[GROUP_NAME_SIZE]; // 子通信域名称 一般不超过 128 Byte
77 : SnapShotSubComm snapShotSubComm; // comm save(size, name, tembinary)
78 : };
79 :
80 : struct CcuStatusSnapshot {
81 : std::vector<std::array<char, GROUP_NAME_SIZE>> useMsCommIds{};
82 : std::vector<std::array<char, GROUP_NAME_SIZE>> useSchedCommIds{};
83 : };
84 :
85 : struct SnapShotBuf {
86 : SnapShotPub snapShotPub; /* 公共区域 */
87 : uint32_t groupNum; /* 子通信域数量 */
88 : Snapshot snapshot; /* 全局通信域 */
89 : vector<SubSnapshot> subSnapshot; /* 子通信域 */
90 : CcuStatusSnapshot ccuStatusSnapshot; /* ccu使用情况 */
91 : };
92 :
93 : /**
94 : * @brief 快照数据序列化和反序列化 单例
95 : * @note
96 : */
97 : class SnapShotParser {
98 : public:
99 : SnapShotParser(const SnapShotParser&) = delete;
100 : SnapShotParser& operator=(const SnapShotParser&) = delete;
101 :
102 : static SnapShotParser& GetInstance();
103 :
104 : // 获取存储的完整流
105 : BinaryStream& GetSnapShotBuf();
106 :
107 : // 恢复通信域,recorver调用,把传入的备份流流反序列化到本地结构
108 : HcclResult ParseSnapshotToLocalBuff(void* snapshotBuf, uint32_t snapshotBufSize, SnapShotBuf& localBuff);
109 :
110 : // 生成 全局通信域 静态短流
111 : void SerializeCommonInfo(
112 : const CommParams& commParams, const HcclCommConfig& config, std::unique_ptr<RankTableInfo> ranktableInfo,
113 : std::shared_ptr<TopoInfo>& topoInfo, BinaryStream& binStream) const;
114 :
115 : // 生成 子局通信域 静态短流
116 : void SerializeSubCommInfo(
117 : const CommParams& commParams, const HcclCommConfig& subConfig, const std::vector<u32>& rankId,
118 : BinaryStream& binStream) const;
119 : // 生成 单个通信域 动态短流
120 : HcclResult SerializeDynamicInfo(
121 : const std::vector<std::pair<u32, RankId>>& levelRankPairs, u32 submittedOpCnt, BinaryStream& binStream) const;
122 : // 序列化 公共版本信息
123 : void SerializeCommVersionInfo(BinaryStream& binStream) const;
124 :
125 : // 计算BinaryStream的CRC值
126 : HcclResult CalcBufCrc32(BinaryStream& buf, u32& crcValue) const;
127 :
128 : // 快照恢复完成后,需要下发算子才算一个完整的流程
129 : void SetIsNeedLoadOp(bool status);
130 : bool GetIsNeedLoadOp() const;
131 :
132 : private:
133 : // 全局通信域反序列化
134 : HcclResult DeserializeCommInfo(BinaryStream& binaryStream, Snapshot& snapShot);
135 :
136 : // 单个子通信域反序列化
137 : HcclResult DeserializeSubCommInfo(BinaryStream& stream, SubSnapshot& subSnapShot);
138 : // 单个通信域动态buf解析
139 : HcclResult DeSnapShotDynamicBuf(BinaryStream& buf, SnapShotDynamic& dynamicInfo) const;
140 : // 解析 所有通信域 快照动态buf
141 : HcclResult DeAllSnapShotDynamicBuf(BinaryStream& buf, SnapShotBuf& localBuff);
142 : // 解析 所有通信域 快照静态buf
143 : HcclResult DeAllSnapShotStaticBuf(BinaryStream& buf, SnapShotBuf& localBuff);
144 : // 解析 ccuStatus
145 : void DeserializeCcuStatusBuf(BinaryStream& buf, SnapShotBuf& localBuff) const;
146 :
147 : // 全局通信域静态信息的序列化
148 : void SerializeParamsInfo(const CommParams& commParams, BinaryStream& binStream) const;
149 : void SerializeCommConfigInfo(const HcclCommConfig& config, BinaryStream& binStream) const;
150 : void SerializeRankTableInfo(std::unique_ptr<RankTableInfo> ranktableInfo, BinaryStream& binStream) const;
151 : void SerializeTopoInfo(const std::shared_ptr<TopoInfo>& topoInfo, BinaryStream& binStream) const;
152 :
153 : // 全局通信域静态信息的反序列化
154 : HcclResult DeserializeParamsInfo(BinaryStream& binaryStream, Hccl::CommParams& commParams) const;
155 : HcclResult DeserializeCommConfigInfo(BinaryStream& binaryStream, HcclCommConfig& config) const;
156 : HcclResult DeserializeRankTableInfo(BinaryStream& binaryStream, RankTableInfo& rankTableInfo) const;
157 : HcclResult DeserializeTopoInfo(BinaryStream& binaryStream, TopoInfo& topoInfo) const;
158 :
159 : // 子通信域静态信息的序列化
160 : void SerializeSubCommParamsInfo(const CommParams& commParams, BinaryStream& binStream) const;
161 : void SerializeSubCommConfigInfo(const HcclCommConfig& subConfig, BinaryStream& binStream) const;
162 : void SerializeRankIds(const std::vector<u32>& rankIds, BinaryStream& binStream) const;
163 :
164 : // 子通信域静态信息的反序列化
165 : HcclResult DeserializeSubCommParamsInfo(BinaryStream& binaryStream, CommParams& subCommParam) const;
166 : HcclResult DeserializeSubCommConfigInfo(BinaryStream& binaryStream, HcclCommConfig& subConfig) const;
167 : HcclResult DeserializeRankIds(BinaryStream& binaryStream, vector<RankId>& rankIds) const;
168 :
169 : // 公共版本信息序列化和反序列化
170 : HcclResult DeserializeCommVersionInfo(BinaryStream& binaryStream, SnapShotPub& snapshotPub) const;
171 :
172 3 : SnapShotParser() {};
173 3 : ~SnapShotParser() {};
174 :
175 : // 校验BinaryStream的CRC值
176 : HcclResult CheckBufCrc32(BinaryStream& buf, const u32 otherCrcValue) const;
177 :
178 : private:
179 : BinaryStream snapShotStream_;
180 : bool isNeedLoadOp{false};
181 : };
182 :
183 : } // namespace Hccl
184 :
185 : #endif // SNAP_SHOT_PARSER_H
|