Line data Source code
1 : /*
2 : * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
3 : * Description: ccu context header file
4 : * Create: 2025-02-18
5 : */
6 :
7 : #include "ccu_assist_v1.h"
8 :
9 : #include "ccu_microcode_v1.h"
10 :
11 : #include "hcomm_adapter_rts.h"
12 :
13 : #include "exception_util.h" // todo: 需要统一整改为不抛异常
14 : #include "ccu_api_exception.h"
15 :
16 : namespace hcomm {
17 : namespace CcuRep {
18 :
19 : constexpr uint64_t SetBits(uint16_t start, uint16_t end)
20 : {
21 : return ((uint64_t(1) << (end - start + 1)) - uint64_t(1)) << start;
22 : }
23 :
24 8019 : constexpr uint64_t SetBits(uint16_t end)
25 : {
26 8019 : return ((uint64_t(1) << (end + 1)) - uint64_t(1));
27 : }
28 :
29 19 : uint64_t GetLoopParam(uint64_t loopCtxId, uint64_t gsaOffset, uint64_t loopIterNum)
30 : {
31 19 : constexpr uint16_t ctxIdBitNum = 8;
32 19 : constexpr uint16_t ctxIdShiftBit = 45;
33 19 : constexpr uint16_t gsaBitNum = 32;
34 19 : constexpr uint16_t gsaShiftBit = 13;
35 19 : constexpr uint16_t loopNumBitNum = 13;
36 19 : constexpr uint16_t loopNumShiftBit = 0;
37 19 : return ((loopCtxId & SetBits(ctxIdBitNum)) << ctxIdShiftBit) | ((gsaOffset & SetBits(gsaBitNum)) << gsaShiftBit)
38 19 : | ((loopIterNum & SetBits(loopNumBitNum)) << loopNumShiftBit);
39 : }
40 :
41 10 : uint64_t GetParallelParam(uint64_t repeatNum, uint64_t repeatLoopIndex, uint64_t totalLoopNum)
42 : {
43 10 : constexpr uint16_t repeatBitNum = 7;
44 10 : constexpr uint16_t repeatNumShiftBit = 55;
45 10 : constexpr uint16_t repeatLoopBitNum = 7;
46 10 : constexpr uint16_t repeatLoopShiftBit = 48;
47 10 : constexpr uint16_t totalLoopBitNum = 7;
48 10 : constexpr uint16_t totalLoopShiftBit = 41;
49 10 : return ((repeatNum & SetBits(repeatBitNum)) << repeatNumShiftBit)
50 10 : | ((repeatLoopIndex & SetBits(repeatLoopBitNum)) << repeatLoopShiftBit)
51 10 : | ((totalLoopNum & SetBits(totalLoopBitNum)) << totalLoopShiftBit);
52 : }
53 :
54 4 : uint64_t GetParallelParamV2(uint64_t repeatNum, uint64_t repeatLoopIndex, uint64_t totalLoopNum)
55 : {
56 4 : constexpr uint16_t loopNumBitNum = 10;
57 4 : constexpr uint16_t loopNumShiftBit = 0;
58 4 : constexpr uint16_t repeatLoopBitNum = 9;
59 4 : constexpr uint16_t repeatLoopShiftBit = 10;
60 4 : constexpr uint16_t extendBitNum = 9;
61 4 : constexpr uint16_t extendShiftBit = 19;
62 4 : return ((totalLoopNum & SetBits(loopNumBitNum)) << loopNumShiftBit)
63 4 : | ((repeatLoopIndex & SetBits(repeatLoopBitNum)) << repeatLoopShiftBit)
64 4 : | ((repeatNum & SetBits(extendBitNum)) << extendShiftBit);
65 : }
66 :
67 14 : uint64_t GetOffsetParam(uint64_t gsaOffset, uint64_t msOffset, uint64_t ckeOffset)
68 : {
69 14 : constexpr uint16_t gsaBitNum = 32;
70 14 : constexpr uint16_t gsaShiftBit = 21;
71 14 : constexpr uint16_t msBitNum = 11;
72 14 : constexpr uint16_t msShiftBit = 10;
73 14 : constexpr uint16_t ckeBitNum = 10;
74 14 : constexpr uint16_t ckeShiftBit = 0;
75 14 : return ((gsaOffset & SetBits(gsaBitNum)) << gsaShiftBit) | ((msOffset & SetBits(msBitNum)) << msShiftBit)
76 14 : | ((ckeOffset & SetBits(ckeBitNum)) << ckeShiftBit);
77 : }
78 :
79 2626 : uint64_t GetToken(uint64_t tokenId, uint64_t tokenValue, uint64_t tokenValid)
80 : {
81 2626 : constexpr uint16_t tokenValidBitNum = 1;
82 2626 : constexpr uint16_t tokenValidShiftBit = 52;
83 2626 : constexpr uint16_t tokenIdBitNum = 20;
84 2626 : constexpr uint16_t tokenIdShiftBit = 32;
85 2626 : constexpr uint16_t tokenValueBitNum = 32;
86 2626 : constexpr uint16_t tokenValueShiftBit = 0;
87 2626 : return ((tokenValid & SetBits(tokenValidBitNum)) << tokenValidShiftBit)
88 2626 : | ((tokenId & SetBits(tokenIdBitNum)) << tokenIdShiftBit)
89 2626 : | ((tokenValue & SetBits(tokenValueBitNum)) << tokenValueShiftBit);
90 : }
91 :
92 0 : uint64_t CcuCombineTokenInfo(uint64_t tokenId, uint64_t tokenValue, uint64_t tokenValid)
93 : {
94 0 : return GetToken(tokenId, tokenValue, tokenValid);
95 : }
96 :
97 12 : uint16_t GetCcuReduceType(Hccl::ReduceOp reduceOp)
98 : {
99 : static std::map<Hccl::ReduceOp, uint16_t> ccuReduceTypeMap = {
100 : {Hccl::ReduceOp::SUM, CCU_REDUCE_SUM},
101 : {Hccl::ReduceOp::MAX, CCU_REDUCE_MAX},
102 : {Hccl::ReduceOp::MIN, CCU_REDUCE_MIN},
103 14 : };
104 :
105 12 : if (ccuReduceTypeMap.find(reduceOp) == ccuReduceTypeMap.end()) {
106 0 : Hccl::THROW<Hccl::CcuApiException>("Unsupported ReduceOp[%s] for Ccu", reduceOp.Describe().c_str());
107 : }
108 :
109 12 : return ccuReduceTypeMap[reduceOp];
110 : }
111 :
112 8 : uint16_t GetCcuDataType(Hccl::DataType dataType, Hccl::ReduceOp reduceOp)
113 : {
114 : static std::map<Hccl::DataType, uint16_t> ccuSumDataTypeMap = {
115 : {Hccl::DataType::FP32, 0}, {Hccl::DataType::FP16, 1}, {Hccl::DataType::BFP16, 2}, {Hccl::DataType::HIF8, 3}, {Hccl::DataType::FP8E4M3, 4},
116 : {Hccl::DataType::FP8E5M2, 5}, {Hccl::DataType::INT8, 6}, {Hccl::DataType::UINT8, 7}, {Hccl::DataType::INT16, 8}, {Hccl::DataType::INT32, 9},
117 10 : };
118 :
119 : static std::map<Hccl::DataType, uint16_t> ccuMaxMinDataTypeMap = {
120 : {Hccl::DataType::FP32, 0}, {Hccl::DataType::FP16, 1}, {Hccl::DataType::BFP16, 2}, {Hccl::DataType::INT8, 6},
121 : {Hccl::DataType::UINT8, 7}, {Hccl::DataType::INT16, 8}, {Hccl::DataType::INT32, 9},
122 :
123 10 : };
124 :
125 8 : uint16_t ccuReduceType = GetCcuReduceType(reduceOp);
126 8 : if (ccuReduceType == CCU_REDUCE_SUM) {
127 8 : if (ccuSumDataTypeMap.find(dataType) == ccuSumDataTypeMap.end()) {
128 0 : Hccl::THROW<Hccl::CcuApiException>("Unsupported Hccl::DataType[%s] for Ccu SUM", dataType.Describe().c_str());
129 : }
130 8 : return ccuSumDataTypeMap[dataType];
131 : }
132 :
133 0 : if (ccuReduceType == CCU_REDUCE_MAX || ccuReduceType == CCU_REDUCE_MIN) {
134 0 : if (ccuMaxMinDataTypeMap.find(dataType) == ccuMaxMinDataTypeMap.end()) {
135 0 : Hccl::THROW<Hccl::CcuApiException>("Unsupported Hccl::DataType[%s] for Ccu MAX/MIN", dataType.Describe().c_str());
136 : }
137 0 : return ccuMaxMinDataTypeMap[dataType];
138 : }
139 :
140 0 : return ccuSumDataTypeMap[dataType];
141 : }
142 :
143 4 : uint16_t GetUBReduceType(Hccl::ReduceOp reduceOp)
144 : {
145 : static std::map<Hccl::ReduceOp, uint16_t> ubReduceTypeMap = {
146 : {Hccl::ReduceOp::SUM, 10},
147 : {Hccl::ReduceOp::MAX, 8},
148 : {Hccl::ReduceOp::MIN, 9},
149 6 : };
150 :
151 4 : if (ubReduceTypeMap.find(reduceOp) == ubReduceTypeMap.end()) {
152 0 : Hccl::THROW<Hccl::CcuApiException>("Unsupported reduceOp[%s] for UB Reduce", reduceOp.Describe().c_str());
153 : }
154 :
155 4 : return ubReduceTypeMap[reduceOp];
156 : }
157 :
158 4 : uint16_t GetUBDataType(Hccl::DataType dataType)
159 : {
160 : static std::map<Hccl::DataType, uint16_t> ubDataTypeMap = {
161 : {Hccl::DataType::FP32, 7}, {Hccl::DataType::FP16, 6}, {Hccl::DataType::BFP16, 8}, {Hccl::DataType::INT8, 0}, {Hccl::DataType::UINT8, 3},
162 6 : {Hccl::DataType::INT16, 1}, {Hccl::DataType::INT32, 2}, {Hccl::DataType::UINT16, 4}, {Hccl::DataType::UINT32, 5}};
163 :
164 4 : if (ubDataTypeMap.find(dataType) == ubDataTypeMap.end()) {
165 0 : Hccl::THROW<Hccl::CcuApiException>("Unsupported Hccl::DataType[%s] for UB Reduce", dataType.Describe().c_str());
166 : }
167 4 : return ubDataTypeMap[dataType];
168 : }
169 :
170 154 : uint64_t GetTokenInfo(uint64_t va, uint64_t size)
171 : {
172 154 : rtMemUbTokenInfo info{};
173 154 : info.va = va;
174 154 : info.size = size;
175 154 : if (RtsUbDevQueryInfo(QUERY_PROCESS_TOKEN, info) != HcclResult::HCCL_SUCCESS) {
176 0 : Hccl::THROW<Hccl::CcuApiException>("failed to query tokenInfo.");
177 : }
178 308 : return CcuRep::GetToken(info.tokenId, info.tokenValue, 1);
179 : }
180 :
181 : }; // namespace CcuRep
182 : }; // namespace hcomm
|