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