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