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