Merged
refactor(codegen): Use rawShape instead of shape in some places #1884
refactor(codegen): Use rawShape instead of shape in some places #1884
Merged
HujunAdd on Mar 23
Total: 19 files changed+171-223
@@ -41,14 +41,14 @@
414. **分析日志**414. **分析日志**
42 - 对于FRAMEWORK(F60XXX)、OPERATION_ADAPTER(F61XXX)类错误,一般为上游数据异常导致,需要结合PASS日志分析42 - 对于FRAMEWORK(F60XXX)、OPERATION_ADAPTER(F61XXX)类错误,一般为上游数据异常导致,需要结合PASS日志分析
43 - 其他类型错误需要结合上下文进行分析43 - 其他类型错误需要结合上下文进行分析
44- 44+<br>
45 45 
46 46 
47### 场景举例47### 场景举例
48 48 
49注意:所有场景日志分析均基于上述排查步骤中获取的日志为基础。49注意:所有场景日志分析均基于上述排查步骤中获取的日志为基础。
50 50 
51-#### 生成kernel代码中某个TileOP调用参数不符合预期 {#生成kernel代码中某个TileOP调用参数不符合预期}51+#### 生成kernel代码中某个TileOP调用参数不符合预期
52 52 
531. 在kernel代码中找到不符合预期的TileOp调用,例如:531. 在kernel代码中找到不符合预期的TileOp调用,例如:
54 54 
@@ -65,6 +65,7 @@
65 ```65 ```
66 66 
67 其中!10010即该OP的唯一标识码,可以此为关键字在PASS的图或日志中搜索获取相关信息,PASS定位指导详见[pass trouble shooting](./pass.md)67 其中!10010即该OP的唯一标识码,可以此为关键字在PASS的图或日志中搜索获取相关信息,PASS定位指导详见[pass trouble shooting](./pass.md)
68+<br>
68 69
69 70 
70#### 错误码 F62014:SYMBOL_NOT_FOUND71#### 错误码 F62014:SYMBOL_NOT_FOUND
@@ -76,13 +77,13 @@
76该类错误场景日志上下文会包含"UNDEFINED_VAR"关键字。77该类错误场景日志上下文会包含"UNDEFINED_VAR"关键字。
77CodeGen需要以来Operation中的need_alloc属性生成变量定义语句,若该属性缺失,则会导致变量定义语句缺失从而报错。78CodeGen需要以来Operation中的need_alloc属性生成变量定义语句,若该属性缺失,则会导致变量定义语句缺失从而报错。
78可结合前文[生成kernel代码中某个TileOP调用参数不符合预期](#生成kernel代码中某个TileOP调用参数不符合预期)步骤,找到缺失属性的Operation联合PASS继续定位。79可结合前文[生成kernel代码中某个TileOP调用参数不符合预期](#生成kernel代码中某个TileOP调用参数不符合预期)步骤,找到缺失属性的Operation联合PASS继续定位。
79- 80+<br>
80 81 
81#### 错误码 F63001:COMPILE_CODE_FAILED82#### 错误码 F63001:COMPILE_CODE_FAILED
82 83 
83kernel代码编译错误可能有多种原因导致,后续将根据不同场景完善排查指导。84kernel代码编译错误可能有多种原因导致,后续将根据不同场景完善排查指导。
84 85 
85-##### 堆栈溢出86+##### 场景1: 堆栈溢出
86 87 
87报错关键字样例:88报错关键字样例:
88 89 
@@ -92,7 +93,7 @@ error: stack frame size (*****) exceeds limit (32768) in function
92 93 
93可参考: [算子编译报堆栈溢出错误](../tutorials/appendix/faq.md#算子编译报堆栈溢出错误)94可参考: [算子编译报堆栈溢出错误](../tutorials/appendix/faq.md#算子编译报堆栈溢出错误)
94 95 
95-##### PTO指令数据类型不匹配96+##### 场景2: PTO指令数据类型不匹配
96 97 
97报错关键字"maybe need a type",样例:98报错关键字"maybe need a type",样例:
98 99 
@@ -105,7 +106,7 @@ copy_matrix_cc_to_gm(dstGlobalAddr, srcTileAddr, xmReg, xtReg);
105- 前端调用Operation接口参数传递错误,参考:[执行代码有pto相关报错](https://gitcode.com/cann/pypto/issues/705)106- 前端调用Operation接口参数传递错误,参考:[执行代码有pto相关报错](https://gitcode.com/cann/pypto/issues/705)
106- 使用了PTO-ISA不支持的数据类型,需要重新分析用例场景,使用硬件支持的数据类型107- 使用了PTO-ISA不支持的数据类型,需要重新分析用例场景,使用硬件支持的数据类型
107 108 
108-##### 生成PTO指令和二进制编译参数指定的硬件平台不匹配109+##### 场景3: 生成PTO指令和二进制编译参数指定的硬件平台不匹配
109 110 
110报错关键字"does not support the given target feature",样例:111报错关键字"does not support the given target feature",样例:
111 112 
@@ -121,7 +122,7 @@ error: function type 'void (__cbuf__ void *, __gm__ void *, unsigned char, unsig
121- CodeGen使用Cube或Vector的编译参数依据为Function::IsCube()接口,需要PASS确认对不同子图,该接口设置的值是否正确。122- CodeGen使用Cube或Vector的编译参数依据为Function::IsCube()接口,需要PASS确认对不同子图,该接口设置的值是否正确。
122 123 
123 124 
124-##### 变量未定义125+##### 场景4: 变量未定义
125 126 
1261. 报错关键字包含sym_***:1271. 报错关键字包含sym_***:
127 128 
@@ -131,8 +132,10 @@ UBTileTensorBF16Dim2_1 ubTensor_1((uint64_t)UB_S0_E512_T, (Shape2Dim(sym_209_dim
131```132```
132 133 
133此类变量用于运行时动态获取Shape、Offset大小,数据来源于Function::GetDynParamTable接口,可在报错日志中往前搜索首个出现的"subprogram id"关键字,找到子图ID并告知PASS,由PASS继续分析变量缺失原因。134此类变量用于运行时动态获取Shape、Offset大小,数据来源于Function::GetDynParamTable接口,可在报错日志中往前搜索首个出现的"subprogram id"关键字,找到子图ID并告知PASS,由PASS继续分析变量缺失原因。
135+<br>
134 136 
135-##### 二进制编译时长统计137+ 
138+#### 二进制编译时长统计
136 139 
137CodeGen模块耗时可通过执行算子后在屏幕输出中观察Compiler Monitor提供的统计结果,样例如下:140CodeGen模块耗时可通过执行算子后在屏幕输出中观察Compiler Monitor提供的统计结果,样例如下:
138 141 
@@ -257,7 +257,7 @@ void CodeGenCloudNPU::GenCode(
257 };257 };
258 tasks.push_back(task);258 tasks.push_back(task);
259 }259 }
260- unsigned threadNum = ConfigManager::Instance().GetCodeGenConfig(KEY_PARALLEL_COMPILE, 1u);260+ unsigned threadNum = GetCGThreadNum();
261 ParallelExecuteAndWait(threadNum, tasks);261 ParallelExecuteAndWait(threadNum, tasks);
262 262 
263#ifdef BUILD_WITH_CANN263#ifdef BUILD_WITH_CANN
@@ -739,7 +739,7 @@ void CodeGenCloudNPU::ExecuteParallelCompile(const Function &topFunc) {
739 std::ostringstream makeCmd;739 std::ostringstream makeCmd;
740 makeCmd << "make -j";740 makeCmd << "make -j";
741 741 
742- unsigned parallelJobs = ConfigManager::Instance().GetCodeGenConfig(KEY_PARALLEL_COMPILE, 1u);742+ unsigned parallelJobs = GetCGThreadNum();
743 makeCmd << std::to_string(parallelJobs);743 makeCmd << std::to_string(parallelJobs);
744 744 
745 std::string makefilePath = GetOutputDir();745 std::string makefilePath = GetOutputDir();
@@ -75,14 +75,7 @@ std::string CodeGenOpCloudNPU::GenCubeOp(bool zeroC) const {
75 if (isSupportLayout) {75 if (isSupportLayout) {
76 return PrintMatmulTileTensor(!zeroC);76 return PrintMatmulTileTensor(!zeroC);
77 }77 }
78- // shape: dst, src0, src178+ 
79- bool isShapeValid = (shape[ID0][ID0] == shape[ID1][ID0]) &&
80- (shape[ID0][ID1] == shape[ID2][ID1] || shape[ID0][ID1] == shape[ID2][ID0]) &&
81- (shape[ID1][ID1] == shape[ID2][ID1] || shape[ID1][ID1] == shape[ID2][ID0]);
82- ASSERT(GenCodeErr::TENSOR_SHAPE_MISMATCHED, isShapeValid) << "CUBE: m k n is invalid.";
83- int64_t m = shape[ID0][ID0];
84- int64_t k = shape[ID1][ID1]; // NEXTNEXT assume A is not transposed for now
85- int64_t n = shape[ID0][ID1];
86 unsigned uf = 0;79 unsigned uf = 0;
87 80 
88 std::string aVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);81 std::string aVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
@@ -119,8 +112,11 @@ std::string CodeGenOpCloudNPU::GenCubeOp(bool zeroC) const {
119 << ", " << SymbolicExpressionTable::BuildExpression(l0cShapeDyn[ID0]) << ", "112 << ", " << SymbolicExpressionTable::BuildExpression(l0cShapeDyn[ID0]) << ", "
120 << SymbolicExpressionTable::BuildExpression(l0cShapeDyn[ID1]) << ");\n";113 << SymbolicExpressionTable::BuildExpression(l0cShapeDyn[ID1]) << ");\n";
121 } else { // static function114 } else { // static function
115+ int64_t m = originShape[ID0][ID0];
116+ int64_t k = originShape[ID1][ID1]; // NEXTNEXT assume A is not transposed for now
117+ int64_t n = originShape[ID0][ID1];
122 oss << tileOpName << "<" << cDtypeStr << ", " << aDtypeStr << ", " << bDtypeStr << ", " << offset[ID0][ID0]118 oss << tileOpName << "<" << cDtypeStr << ", " << aDtypeStr << ", " << bDtypeStr << ", " << offset[ID0][ID0]
123- << ", " << offset[ID0][ID1] << ", " << shape[ID0][ID0] << ", " << shape[ID0][ID1] << ">"119+ << ", " << offset[ID0][ID1] << ", " << m << ", " << n << ">"
124 << "((" << GetAddrTypeByOperandType(operandType[ID0]) << " " << cDtypeStr << "*)" << cVar << ", "120 << "((" << GetAddrTypeByOperandType(operandType[ID0]) << " " << cDtypeStr << "*)" << cVar << ", "
125 << "(" << GetAddrTypeByOperandType(operandType[ID1]) << " " << aDtypeStr << "*)" << aVar << ", "121 << "(" << GetAddrTypeByOperandType(operandType[ID1]) << " " << aDtypeStr << "*)" << aVar << ", "
126 << "(" << GetAddrTypeByOperandType(operandType[ID2]) << " " << bDtypeStr << "*)" << bVar << ", " << m122 << "(" << GetAddrTypeByOperandType(operandType[ID2]) << " " << bDtypeStr << "*)" << bVar << ", " << m
@@ -28,7 +28,7 @@ DynamicParamPackMTE CodeGenOpCloudNPU::PrepareDynamicShapeInfoForMTE(
28 DynamicParamPackMTE pack;28 DynamicParamPackMTE pack;
29 int dim = static_cast<int>(rawShape[dynShapeIdx].size());29 int dim = static_cast<int>(rawShape[dynShapeIdx].size());
30 if (isGmSpill) {30 if (isGmSpill) {
31- for (auto s : shape[dynShapeIdx]) {31+ for (auto s : shapeFromAttr[dynShapeIdx]) {
32 pack.gmShapeExpr.emplace_back(std::to_string(s));32 pack.gmShapeExpr.emplace_back(std::to_string(s));
33 }33 }
34 } else {34 } else {
@@ -194,10 +194,10 @@ std::string CodeGenOpCloudNPU::GenMemL1ToL0() const {
194 194 
195 std::string paramStr = GenParamsStr();195 std::string paramStr = GenParamsStr();
196 196 
197- std::vector<int64_t> l1Shape = this->rawShape[ID1];197+ std::vector<int64_t> l1Shape = rawShape[ID1];
198 CODEGEN_LOGI("GenMemL1ToL0 %s, l1Shape is %s", tileOpName.c_str(), IntVecToStr(l1Shape).c_str());198 CODEGEN_LOGI("GenMemL1ToL0 %s, l1Shape is %s", tileOpName.c_str(), IntVecToStr(l1Shape).c_str());
199 199 
200- std::vector<int64_t> l0Shape = this->rawShape[ID0];200+ std::vector<int64_t> l0Shape = rawShape[ID0];
201 CODEGEN_LOGI("GenMemL1ToL0 %s, l0Shape is %s", tileOpName.c_str(), IntVecToStr(l0Shape).c_str());201 CODEGEN_LOGI("GenMemL1ToL0 %s, l0Shape is %s", tileOpName.c_str(), IntVecToStr(l0Shape).c_str());
202 202 
203 unsigned srcOffset0 = 0;203 unsigned srcOffset0 = 0;
@@ -393,7 +393,7 @@ std::string CodeGenOpCloudNPU::GenUBToL1TileTensor() const {
393 if (!isSupportLayout) {393 if (!isSupportLayout) {
394 return "";394 return "";
395 }395 }
396- std::vector<int64_t> dstOffset = this->offset[ID0];396+ std::vector<int64_t> dstOffset = offset[ID0];
397 std::string coordCp = WrapParamByParentheses(dstOffset);397 std::string coordCp = WrapParamByParentheses(dstOffset);
398 // e.g. Coord4Dim((RUNTIME_COA_GET_PARAM_OFFSET(2, 136, 0)),(RUNTIME_COA_GET_PARAM_OFFSET(2, 136, 1)))398 // e.g. Coord4Dim((RUNTIME_COA_GET_PARAM_OFFSET(2, 136, 0)),(RUNTIME_COA_GET_PARAM_OFFSET(2, 136, 1)))
399 std::string coord = PrintCoord(rawShape[ID0].size(), coordCp);399 std::string coord = PrintCoord(rawShape[ID0].size(), coordCp);
@@ -443,9 +443,9 @@ std::string CodeGenOpCloudNPU::GenMemCopyVar(bool isCopyLocalToGM, bool isSpillT
443 addrTypeHead[gmIdx] = GetAddrTypeByOperandType(BUF_DDR);443 addrTypeHead[gmIdx] = GetAddrTypeByOperandType(BUF_DDR);
444 addrTypeHead[localIdx] = GetAddrTypeByOperandType(localType);444 addrTypeHead[localIdx] = GetAddrTypeByOperandType(localType);
445 445 
446- std::vector<int64_t> gmShape = this->rawShape[gmIdx];446+ std::vector<int64_t> gmShape = rawShape[gmIdx];
447 CODEGEN_LOGI("gmShape is %s", IntVecToStr(gmShape).c_str());447 CODEGEN_LOGI("gmShape is %s", IntVecToStr(gmShape).c_str());
448- std::vector<int64_t> localRawShape = this->rawShape[localIdx];448+ std::vector<int64_t> localRawShape = rawShape[localIdx];
449 CODEGEN_LOGI("localRawShape is %s", IntVecToStr(localRawShape).c_str());449 CODEGEN_LOGI("localRawShape is %s", IntVecToStr(localRawShape).c_str());
450 450 
451 std::vector<std::string> addrExpr(ID2);451 std::vector<std::string> addrExpr(ID2);
@@ -809,7 +809,7 @@ std::string CodeGenOpCloudNPU::PrintMemCopyWithL1Static(const PrintMemCopyWithL1
809 ASSERT(GenCodeErr::PRINT_FAILED, printRet >= 0)809 ASSERT(GenCodeErr::PRINT_FAILED, printRet >= 0)
810 << "sprintf_s failed in genMemCopyVar, return value:" << printRet;810 << "sprintf_s failed in genMemCopyVar, return value:" << printRet;
811 } else {811 } else {
812- std::vector<SymbolicScalar> gmOffset = this->offsetFromAttr[gmIdx];812+ std::vector<SymbolicScalar> gmOffset = offsetFromAttr[gmIdx];
813 printRet = sprintf_s(addrBuffer, BUFFER_SIZE_1024, "%s", addrExpr[ID1].c_str());813 printRet = sprintf_s(addrBuffer, BUFFER_SIZE_1024, "%s", addrExpr[ID1].c_str());
814 ASSERT(GenCodeErr::PRINT_FAILED, printRet >= 0)814 ASSERT(GenCodeErr::PRINT_FAILED, printRet >= 0)
815 << "sprintf_s failed in PrintMemCopyWithL1Static, return value:" << printRet;815 << "sprintf_s failed in PrintMemCopyWithL1Static, return value:" << printRet;
@@ -45,7 +45,7 @@ std::string CodeGenOpCloudNPU::GenMemL1CopyInConv() const {
45 GetAttr(Conv::LoadStoreConvOpAttributeKey::isFmap, isFmap);45 GetAttr(Conv::LoadStoreConvOpAttributeKey::isFmap, isFmap);
46 GetAttr(Conv::LoadStoreConvOpAttributeKey::isConv3D, isConv3D);46 GetAttr(Conv::LoadStoreConvOpAttributeKey::isConv3D, isConv3D);
47 auto dynOffset = offsetFromAttr[ToUnderlying(MISOIdx::SRC0_IDX)];47 auto dynOffset = offsetFromAttr[ToUnderlying(MISOIdx::SRC0_IDX)];
48- auto srcShape = shape[ToUnderlying(MISOIdx::SRC0_IDX)];48+ auto srcShape = shapeFromAttr[ToUnderlying(MISOIdx::SRC0_IDX)];
49 if (isConv3D) {49 if (isConv3D) {
50 ASSERT(ConvCodenGenError::CODEGEN_CHECK_DIM_INVALID, dynOffset.size() == SHAPE_DIM5)50 ASSERT(ConvCodenGenError::CODEGEN_CHECK_DIM_INVALID, dynOffset.size() == SHAPE_DIM5)
51 << "GenMemL1CopyInConv offset should be 5-dim!";51 << "GenMemL1CopyInConv offset should be 5-dim!";
@@ -108,7 +108,7 @@ std::string CodeGenOpCloudNPU::GenMemL1CopyOutConv() const {
108 int64_t realM = 0, realN = 0;108 int64_t realM = 0, realN = 0;
109 int64_t offsetN = 0, offsetC = 0, offsetD = 0, offsetH = 0, offsetW = 0;109 int64_t offsetN = 0, offsetC = 0, offsetD = 0, offsetH = 0, offsetW = 0;
110 GetAttr(Conv::LoadStoreConvOpAttributeKey::isConv3D, isConv3D);110 GetAttr(Conv::LoadStoreConvOpAttributeKey::isConv3D, isConv3D);
111- auto realShape = shape[ToUnderlying(MISOIdx::DST_IDX)];111+ auto realShape = shapeFromAttr[ToUnderlying(MISOIdx::DST_IDX)];
112 ASSERT(ConvCodenGenError::CODEGEN_CHECK_DIM_INVALID, realShape.size() == SHAPE_DIM2)112 ASSERT(ConvCodenGenError::CODEGEN_CHECK_DIM_INVALID, realShape.size() == SHAPE_DIM2)
113 << "GenMemL1CopyOutConv valid shape should be 2-dim!";113 << "GenMemL1CopyOutConv valid shape should be 2-dim!";
114 realM = realShape[ID0];114 realM = realShape[ID0];
@@ -154,7 +154,7 @@ std::string CodeGenOpCloudNPU::GenMemL1ToL0Load3D() const {
154 paramList.emplace_back(mPos);154 paramList.emplace_back(mPos);
155 paramList.emplace_back(kPos);155 paramList.emplace_back(kPos);
156 156 
157- std::vector<int64_t> fmapL1Shape = this->rawShape[ID1];157+ std::vector<int64_t> fmapL1Shape = rawShape[ID1];
158 CODEGEN_LOGI("GenMemL1ToL0Load3D %s, fmapL1Shape is %s", tileOpName.c_str(), IntVecToStr(fmapL1Shape).c_str());158 CODEGEN_LOGI("GenMemL1ToL0Load3D %s, fmapL1Shape is %s", tileOpName.c_str(), IntVecToStr(fmapL1Shape).c_str());
159 159 
160 int64_t padLeft = 0, padRight = 0, padTop = 0, padBottom = 0, padValue = 0;160 int64_t padLeft = 0, padRight = 0, padTop = 0, padBottom = 0, padValue = 0;
@@ -187,7 +187,7 @@ std::string CodeGenOpCloudNPU::GenMemL1ToL0Load3D() const {
187 paramList.emplace_back(strideH);187 paramList.emplace_back(strideH);
188 paramList.emplace_back(strideW);188 paramList.emplace_back(strideW);
189 189 
190- std::vector<int64_t> fmapL0Shape = this->rawShape[ID0];190+ std::vector<int64_t> fmapL0Shape = rawShape[ID0];
191 CODEGEN_LOGI("GenMemL1ToL0Load3D %s, fmapL0Shape is %s", tileOpName.c_str(), IntVecToStr(fmapL0Shape).c_str());191 CODEGEN_LOGI("GenMemL1ToL0Load3D %s, fmapL0Shape is %s", tileOpName.c_str(), IntVecToStr(fmapL0Shape).c_str());
192 ASSERT(ConvCodenGenError::CODEGEN_CHECK_DIM_INVALID, fmapL0Shape.size() == SHAPE_DIM2)192 ASSERT(ConvCodenGenError::CODEGEN_CHECK_DIM_INVALID, fmapL0Shape.size() == SHAPE_DIM2)
193 << "GenMemL1ToL0Load3D L0 fmap only support 2-dim!";193 << "GenMemL1ToL0Load3D L0 fmap only support 2-dim!";
@@ -215,10 +215,10 @@ std::string CodeGenOpCloudNPU::GenMemL1ToL0Load2D() const {
215 paramList.emplace_back(kPos);215 paramList.emplace_back(kPos);
216 paramList.emplace_back(nPos);216 paramList.emplace_back(nPos);
217 217 
218- std::vector<int64_t> weightL1Shape = this->rawShape[ID1];218+ std::vector<int64_t> weightL1Shape = rawShape[ID1];
219 CODEGEN_LOGI("GenMemL1ToL0Load2D %s, weightL1Shape is %s", tileOpName.c_str(), IntVecToStr(weightL1Shape).c_str());219 CODEGEN_LOGI("GenMemL1ToL0Load2D %s, weightL1Shape is %s", tileOpName.c_str(), IntVecToStr(weightL1Shape).c_str());
220 220 
221- std::vector<int64_t> weightL0Shape = this->rawShape[ID0];221+ std::vector<int64_t> weightL0Shape = rawShape[ID0];
222 CODEGEN_LOGI("GenMemL1ToL0Load2D %s, weightL0Shape is %s", tileOpName.c_str(), IntVecToStr(weightL0Shape).c_str());222 CODEGEN_LOGI("GenMemL1ToL0Load2D %s, weightL0Shape is %s", tileOpName.c_str(), IntVecToStr(weightL0Shape).c_str());
223 ASSERT(ConvCodenGenError::CODEGEN_CHECK_DIM_INVALID, weightL0Shape.size() == SHAPE_DIM2)223 ASSERT(ConvCodenGenError::CODEGEN_CHECK_DIM_INVALID, weightL0Shape.size() == SHAPE_DIM2)
224 << "GenMemL1ToL0Load2D L0 weight only support 2-dim!";224 << "GenMemL1ToL0Load2D L0 weight only support 2-dim!";
@@ -173,8 +173,8 @@ void HelpNormalize(std::vector<size_t> &index, int axis, int paramDim) {
173 index.insert(index.begin() + axis, delNum);173 index.insert(index.begin() + axis, delNum);
174}174}
175std::string CodeGenOpCloudNPU::PrintGatherDynamicUnaligned() const {175std::string CodeGenOpCloudNPU::PrintGatherDynamicUnaligned() const {
176- std::vector dstShape = this->rawShape[0];176+ std::vector dstShape = rawShape[0];
177- std::vector src0Shape = this->rawShape[1];177+ std::vector src0Shape = rawShape[1];
178 178 
179 std::string resultDtypeStr = DataType2CCEStr(operandDtype[ID0]);179 std::string resultDtypeStr = DataType2CCEStr(operandDtype[ID0]);
180 std::string paramDtypeStr = DataType2CCEStr(operandDtype[ID1]);180 std::string paramDtypeStr = DataType2CCEStr(operandDtype[ID1]);
@@ -325,8 +325,8 @@ std::string CodeGenOpCloudNPU::PrintGatherInUBLayout() const {
325 return oss.str();325 return oss.str();
326}326}
327std::string CodeGenOpCloudNPU::PrintGatherInUBDynamicUnaligned() const {327std::string CodeGenOpCloudNPU::PrintGatherInUBDynamicUnaligned() const {
328- std::vector dstShape = this->rawShape[0];328+ std::vector dstShape = rawShape[0];
329- std::vector src0Shape = this->rawShape[1];329+ std::vector src0Shape = rawShape[1];
330 330 
331 std::string resultDtypeStr = DataType2CCEStr(operandDtype[ID0]);331 std::string resultDtypeStr = DataType2CCEStr(operandDtype[ID0]);
332 std::string paramDtypeStr = DataType2CCEStr(operandDtype[ID1]);332 std::string paramDtypeStr = DataType2CCEStr(operandDtype[ID1]);
@@ -55,10 +55,10 @@ std::string CodeGenOpCloudNPU::GenIndexOutCastOp() const {
55 std::vector<std::string> addrExpr(ID2);55 std::vector<std::string> addrExpr(ID2);
56 addrExpr[gmIdx] = GenGmParamVar(gmIdx);56 addrExpr[gmIdx] = GenGmParamVar(gmIdx);
57 57 
58- std::vector<int64_t> src0OriginShape = this->originShape[ID1];58+ std::vector<int64_t> src0OriginShape = originShape[ID1];
59- std::vector<int64_t> src1OriginShape = this->originShape[ID2];59+ std::vector<int64_t> src1OriginShape = originShape[ID2];
60- std::vector<int64_t> src0RawShape = this->rawShape[ID1];60+ std::vector<int64_t> src0RawShape = rawShape[ID1];
61- std::vector<int64_t> src1RawShape = this->rawShape[ID2];61+ std::vector<int64_t> src1RawShape = rawShape[ID2];
62 62 
63 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID1]);63 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID1]);
64 std::string src0DtypeStr = DataType2CCEStr(operandDtype[ID1]);64 std::string src0DtypeStr = DataType2CCEStr(operandDtype[ID1]);
@@ -71,7 +71,7 @@ std::string CodeGenOpCloudNPU::GenIndexOutCastOp() const {
71 {localIdx, std::ref(s0Var)}71 {localIdx, std::ref(s0Var)}
72 });72 });
73 73 
74- std::vector gmShape = this->rawShape[gmIdx];74+ std::vector gmShape = rawShape[gmIdx];
75 CODEGEN_LOGI("genIndexOutCastOp gm shape: %s", IntVecToStr(gmShape).c_str());75 CODEGEN_LOGI("genIndexOutCastOp gm shape: %s", IntVecToStr(gmShape).c_str());
76 76 
77 std::vector<int64_t> s0os = NormalizeShape(src0OriginShape, SHAPE_DIM4);77 std::vector<int64_t> s0os = NormalizeShape(src0OriginShape, SHAPE_DIM4);
@@ -224,7 +224,7 @@ CodeGenOpCloudNPU::CodeGenOpCloudNPU(const CodeGenOpCloudNPUCtx &ctx)
224 // hypot op224 // hypot op
225 {Opcode::OP_HYPOT, [this]() { return GenHypotOp(); }},225 {Opcode::OP_HYPOT, [this]() { return GenHypotOp(); }},
226 {Opcode::OP_PAD, [this]() { return GenPadOp(); }},226 {Opcode::OP_PAD, [this]() { return GenPadOp(); }},
227- {Opcode::OP_FILLPAD, [this]() { return GenPadOp(); }}, 227+ {Opcode::OP_FILLPAD, [this]() { return GenPadOp(); }},
228 }),228 }),
229 sortOps_({229 sortOps_({
230 // sort230 // sort
@@ -528,10 +528,10 @@ void CodeGenOpCloudNPU::UpdateTileTensorShapeAndStride(
528 // gm tensor528 // gm tensor
529 if (tileTensor.bufType == OperandType::BUF_DDR) {529 if (tileTensor.bufType == OperandType::BUF_DDR) {
530 if (isSpillToGm) {530 if (isSpillToGm) {
531- for (auto s : shape[paramIdx]) {531+ for (auto s : shapeFromAttr[paramIdx]) {
532 tileTensor.shape.emplace_back(std::to_string(s));532 tileTensor.shape.emplace_back(std::to_string(s));
533 }533 }
534- tileTensor.stride = BuildStride(shape[paramIdx]);534+ tileTensor.stride = BuildStride(shapeFromAttr[paramIdx]);
535 } else {535 } else {
536 tileTensor.shape = GenGetParamMacroPacked(paramIdx, tileTensor.dim, PREFIX_STR_RAW_SHAPE);536 tileTensor.shape = GenGetParamMacroPacked(paramIdx, tileTensor.dim, PREFIX_STR_RAW_SHAPE);
537 tileTensor.stride = GenGetParamMacroPacked(paramIdx, tileTensor.dim, PREFIX_STR_STRIDE);537 tileTensor.stride = GenGetParamMacroPacked(paramIdx, tileTensor.dim, PREFIX_STR_STRIDE);
@@ -17,6 +17,8 @@
17#define CODEGEN_OP_CLOUDNPU_H17#define CODEGEN_OP_CLOUDNPU_H
18 18 
19#include <utility>19#include <utility>
20+#include <map>
21+#include <functional>
20#include <unordered_set>22#include <unordered_set>
21 23 
22#include "op_print_param_def.h"24#include "op_print_param_def.h"
@@ -135,6 +137,7 @@ public:
135 std::string GenMrgSortOp() const;137 std::string GenMrgSortOp() const;
136 std::string GenExtractOp() const;138 std::string GenExtractOp() const;
137 std::string GenTiledMrgSortOp() const;139 std::string GenTiledMrgSortOp() const;
140+ std::string GenSortOpWithParams(const std::set<int> &idx) const;
138 std::string GenSortOp() const;141 std::string GenSortOp() const;
139 std::string GenCompareAndSwapOp() const;142 std::string GenCompareAndSwapOp() const;
140 std::string GenMergeOp() const;143 std::string GenMergeOp() const;
@@ -259,6 +262,7 @@ private:
259 tempKey = 0;262 tempKey = 0;
260 AppendLocalBufVarOffsetInOrderImpl<T>(args...);263 AppendLocalBufVarOffsetInOrderImpl<T>(args...);
261 }264 }
265+ 
262 void AppendLocalBufferVarOffset(const std::map<unsigned, std::reference_wrapper<std::string>> &vars) const;266 void AppendLocalBufferVarOffset(const std::map<unsigned, std::reference_wrapper<std::string>> &vars) const;
263 267 
264 // get start offset in total block268 // get start offset in total block
@@ -29,10 +29,10 @@ std::string CodeGenOpCloudNPU::GenCastOp() const {
29 std::string s0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);29 std::string s0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
30 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);30 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
31 31 
32- std::vector srcShape = this->rawShape[ID1];32+ std::vector srcShape = rawShape[ID1];
33 CODEGEN_LOGI("genCastOp %s, srcShape is %s", tileOpName.c_str(), IntVecToStr(srcShape).c_str());33 CODEGEN_LOGI("genCastOp %s, srcShape is %s", tileOpName.c_str(), IntVecToStr(srcShape).c_str());
34 34 
35- std::vector dstShape = this->rawShape[ID0];35+ std::vector dstShape = rawShape[ID0];
36 CODEGEN_LOGI("genCastOp %s, dstShape is %s", tileOpName.c_str(), IntVecToStr(dstShape).c_str());36 CODEGEN_LOGI("genCastOp %s, dstShape is %s", tileOpName.c_str(), IntVecToStr(dstShape).c_str());
37 37 
38 std::string srcDtypeStr = DataType2CCEStr(operandDtype[ID1]);38 std::string srcDtypeStr = DataType2CCEStr(operandDtype[ID1]);
@@ -183,9 +183,9 @@ std::string CodeGenOpCloudNPU::GenTransposeDataMove() const {
183 std::string localVar = sm->QueryVarNameByTensorMagic(operandWithMagic[localIdx]);183 std::string localVar = sm->QueryVarNameByTensorMagic(operandWithMagic[localIdx]);
184 std::string gmVar = GenGmParamVar(gmIdx);184 std::string gmVar = GenGmParamVar(gmIdx);
185 185 
186- std::vector<int64_t> srcShape = this->rawShape[localIdx];186+ std::vector<int64_t> srcShape = rawShape[localIdx];
187 CODEGEN_LOGI("GenTransposeDataMove: srcShape is %s", IntVecToStr(srcShape).c_str());187 CODEGEN_LOGI("GenTransposeDataMove: srcShape is %s", IntVecToStr(srcShape).c_str());
188- std::vector<int64_t> gmShape = this->rawShape[gmIdx];188+ std::vector<int64_t> gmShape = rawShape[gmIdx];
189 CODEGEN_LOGI("GenTransposeDataMove: gmShape is %s", IntVecToStr(gmShape).c_str());189 CODEGEN_LOGI("GenTransposeDataMove: gmShape is %s", IntVecToStr(gmShape).c_str());
190 190 
191 AppendLocalBufferVarOffset({191 AppendLocalBufferVarOffset({
@@ -383,8 +383,8 @@ std::string CodeGenOpCloudNPU::PrintGatherStatic(const PrintGatherParam &param)
383 const std::string &s0Var = param.s0Var;383 const std::string &s0Var = param.s0Var;
384 const std::string &s1Var = param.s1Var;384 const std::string &s1Var = param.s1Var;
385 385 
386- std::vector dstShape = this->rawShape[ID0];386+ std::vector dstShape = rawShape[ID0];
387- std::vector src0Shape = this->rawShape[ID1];387+ std::vector src0Shape = rawShape[ID1];
388 388 
389 std::vector<int64_t> dos = NormalizeShape(originShape[ID0], SHAPE_DIM4);389 std::vector<int64_t> dos = NormalizeShape(originShape[ID0], SHAPE_DIM4);
390 std::vector<int64_t> ss = NormalizeShape(src0Shape, SHAPE_DIM4);390 std::vector<int64_t> ss = NormalizeShape(src0Shape, SHAPE_DIM4);
@@ -423,9 +423,9 @@ std::string CodeGenOpCloudNPU::PrintGatherDynamicUnaligned(const PrintGatherPara
423 const std::string &src0DtypeStr = param.src0DtypeStr;423 const std::string &src0DtypeStr = param.src0DtypeStr;
424 const std::string &src1DtypeStr = param.src1DtypeStr;424 const std::string &src1DtypeStr = param.src1DtypeStr;
425 const int64_t axis = param.axis;425 const int64_t axis = param.axis;
426- std::vector dstShape = this->rawShape[ID0];426+ std::vector dstShape = rawShape[ID0];
427- std::vector src0Shape = this->rawShape[ID1];427+ std::vector src0Shape = rawShape[ID1];
428- std::vector src1Shape = this->rawShape[ID2];428+ std::vector src1Shape = rawShape[ID2];
429 429 
430 auto mul = [](uint32_t data, const int64_t in) { return data * in; };430 auto mul = [](uint32_t data, const int64_t in) { return data * in; };
431 std::vector<int64_t> indexShape = NormalizeShape(src1Shape, SHAPE_DIM4);431 std::vector<int64_t> indexShape = NormalizeShape(src1Shape, SHAPE_DIM4);
@@ -492,12 +492,11 @@ std::string CodeGenOpCloudNPU::GenGatherFromUBOp() const {
492 << "GenGatherOp: There is nop axis attribute here";492 << "GenGatherOp: There is nop axis attribute here";
493 const int64_t axis = AnyCast<int64_t>(opAttrs.at("op_attr_axis"));493 const int64_t axis = AnyCast<int64_t>(opAttrs.at("op_attr_axis"));
494 // shape: dst, src0, src1494 // shape: dst, src0, src1
495- int src0Rank = shape[ID1].size();495+ int dim = rawShape[ID1].size();
496- ASSERT(GenCodeErr::TENSOR_SHAPE_INVALID, src0Rank <= RANK4) << "GenGatherOp: src0 shape rank is not supported!";496+ ASSERT(GenCodeErr::TENSOR_SHAPE_INVALID, dim <= SHAPE_DIM4) << "GenGatherOp: dim is not supported: " << dim;
497 497 
498- std::vector dstShape = this->rawShape[0];498+ std::vector dstShape = rawShape[ID0];
499- 499+ std::vector src0Shape = rawShape[ID1];
500- std::vector src0Shape = this->rawShape[1];
501 CODEGEN_LOGI(500 CODEGEN_LOGI(
502 "GenGatherOp, src0 Shape is [%ld,%ld]", static_cast<long>(src0Shape[0]), static_cast<long>(src0Shape[1]));501 "GenGatherOp, src0 Shape is [%ld,%ld]", static_cast<long>(src0Shape[0]), static_cast<long>(src0Shape[1]));
503 502 
@@ -602,9 +601,9 @@ std::string CodeGenOpCloudNPU::GenGatherElementOp() const {
602 std::string s1Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3]);601 std::string s1Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3]);
603 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);602 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
604 603 
605- std::vector dstShape = this->rawShape[ID0];604+ std::vector dstShape = rawShape[ID0];
606- std::vector src0Shape = this->rawShape[ID2];605+ std::vector src0Shape = rawShape[ID2];
607- std::vector src1Shape = this->rawShape[ID3];606+ std::vector src1Shape = rawShape[ID3];
608 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);607 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);
609 std::string src0DtypeStr = DataType2CCEStr(operandDtype[ID2]);608 std::string src0DtypeStr = DataType2CCEStr(operandDtype[ID2]);
610 std::string src1DtypeStr = DataType2CCEStr(operandDtype[ID3]);609 std::string src1DtypeStr = DataType2CCEStr(operandDtype[ID3]);
@@ -724,8 +723,8 @@ std::string CodeGenOpCloudNPU::GenIndexPutOp() const {
724 std::string s2VarTemp = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3 + i]);723 std::string s2VarTemp = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3 + i]);
725 s2Var.emplace_back(s2VarTemp);724 s2Var.emplace_back(s2VarTemp);
726 }725 }
727- std::vector gmShape = this->rawShape[ID0];726+ std::vector gmShape = rawShape[ID0];
728- std::vector src1RawShape = this->rawShape[ID2];727+ std::vector src1RawShape = rawShape[ID2];
729 728 
730 std::vector<std::string> dataTypeExpr;729 std::vector<std::string> dataTypeExpr;
731 for (int i = 0; i < NUM4; i++) {730 for (int i = 0; i < NUM4; i++) {
@@ -886,8 +885,8 @@ std::string CodeGenOpCloudNPU::GenIndexAddOp() const {
886 std::string srcVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3]);885 std::string srcVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3]);
887 std::string indicesVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID4]);886 std::string indicesVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID4]);
888 887 
889- std::vector dstRawShape = this->rawShape[ID0];888+ std::vector dstRawShape = rawShape[ID0];
890- std::vector srcRawShape = this->rawShape[ID3];889+ std::vector srcRawShape = rawShape[ID3];
891 890 
892 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);891 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);
893 std::string srcDtypeStr = DataType2CCEStr(operandDtype[ID3]);892 std::string srcDtypeStr = DataType2CCEStr(operandDtype[ID3]);
@@ -958,7 +957,7 @@ std::string CodeGenOpCloudNPU::GenCumSumOp() const {
958 std::string dstVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);957 std::string dstVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
959 std::string inputVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);958 std::string inputVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
960 959 
961- std::vector inputRawShape = this->rawShape[ID1];960+ std::vector inputRawShape = rawShape[ID1];
962 961 
963 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);962 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);
964 std::string inputDtypeStr = DataType2CCEStr(operandDtype[ID1]);963 std::string inputDtypeStr = DataType2CCEStr(operandDtype[ID1]);
@@ -1007,23 +1006,20 @@ std::string CodeGenOpCloudNPU::GenTriULOp() const {
1007}1006}
1008 1007 
1009std::string CodeGenOpCloudNPU::PrintScatterElementSOpStatic(const PrintScatterElemParam &param) const {1008std::string CodeGenOpCloudNPU::PrintScatterElementSOpStatic(const PrintScatterElemParam &param) const {
1010- // Static only support 2Dim
1011- int dstRank = shape[ToUnderlying(MISOIdx::DST_IDX)].size();
1012- int src1Rank = shape[ToUnderlying(MISOIdx::SRC1_IDX)].size();
1013- ASSERT(GenCodeErr::TENSOR_SHAPE_INVALID, src1Rank == RANK2)
1014- << "GenScatterElementSOp: src1 shape rank is not supported!";
1015- ASSERT(GenCodeErr::TENSOR_SHAPE_INVALID, dstRank == RANK2)
1016- << "GenScatterElementSOp: dst shape rank is not supported!";
1017- 
1018 const std::string &dstVar = param.dVar;1009 const std::string &dstVar = param.dVar;
1019 const std::string &src0Var = param.s0Var;1010 const std::string &src0Var = param.s0Var;
1020 const std::string &src1Var = param.s1Var;1011 const std::string &src1Var = param.s1Var;
1021 std::vector<int64_t> &dstShape = param.dstRawShape;1012 std::vector<int64_t> &dstShape = param.dstRawShape;
1022 std::vector<int64_t> &src1RawShape = param.src1RawShape;1013 std::vector<int64_t> &src1RawShape = param.src1RawShape;
1014+ // Static only support 2Dim
1015+ ASSERT(GenCodeErr::TENSOR_DIM_UNSUPPORTED, dstShape.size() == SHAPE_DIM2)
1016+ << "dst only support 2 Dim, current is : " << dstShape.size();
1017+ ASSERT(GenCodeErr::TENSOR_DIM_UNSUPPORTED, src1RawShape.size() == SHAPE_DIM2)
1018+ << "src1 only support 2 Dim, current is : " << src1RawShape.size();
1023 const std::vector<std::string> &dataTypeExpr = param.dataTypeExpr;1019 const std::vector<std::string> &dataTypeExpr = param.dataTypeExpr;
1024 const Element &scala = extOperandVal;1020 const Element &scala = extOperandVal;
1025 1021 
1026- std::vector src1Shape = this->originShape[ToUnderlying(MISOIdx::SRC1_IDX)];1022+ std::vector src1Shape = originShape[ToUnderlying(MISOIdx::SRC1_IDX)];
1027 std::vector<int64_t> s1os = NormalizeShape(src1Shape, SHAPE_DIM2);1023 std::vector<int64_t> s1os = NormalizeShape(src1Shape, SHAPE_DIM2);
1028 std::vector<int64_t> s1rs = NormalizeShape(src1RawShape, SHAPE_DIM2);1024 std::vector<int64_t> s1rs = NormalizeShape(src1RawShape, SHAPE_DIM2);
1029 std::vector<int64_t> drs = NormalizeShape(dstShape, SHAPE_DIM2);1025 std::vector<int64_t> drs = NormalizeShape(dstShape, SHAPE_DIM2);
@@ -1125,8 +1121,8 @@ std::string CodeGenOpCloudNPU::GenScatterElementSOp() const {
1125 std::string src1Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(MISOIdx::SRC1_IDX)]);1121 std::string src1Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(MISOIdx::SRC1_IDX)]);
1126 std::string dstVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(MISOIdx::DST_IDX)]);1122 std::string dstVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(MISOIdx::DST_IDX)]);
1127 1123 
1128- std::vector dstRawShape = this->rawShape[ToUnderlying(MISOIdx::DST_IDX)];1124+ std::vector dstRawShape = rawShape[ToUnderlying(MISOIdx::DST_IDX)];
1129- std::vector src1RawShape = this->rawShape[ToUnderlying(MISOIdx::SRC1_IDX)];1125+ std::vector src1RawShape = rawShape[ToUnderlying(MISOIdx::SRC1_IDX)];
1130 1126 
1131 std::string dstDtypeStr = DataType2CCEStr(dstDtype);1127 std::string dstDtypeStr = DataType2CCEStr(dstDtype);
1132 std::string src0DtypeStr = DataType2CCEStr(src0Dtype);1128 std::string src0DtypeStr = DataType2CCEStr(src0Dtype);
@@ -1221,9 +1217,9 @@ std::string CodeGenOpCloudNPU::GenScatterOp() const {
1221 std::string src1Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3]);1217 std::string src1Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3]);
1222 std::string src2Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID4]);1218 std::string src2Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID4]);
1223 1219 
1224- std::vector dstRawShape = this->rawShape[ID0];1220+ std::vector dstRawShape = rawShape[ID0];
1225- std::vector src1RawShape = this->rawShape[ID3];1221+ std::vector src1RawShape = rawShape[ID3];
1226- std::vector src2RawShape = this->rawShape[ID4];1222+ std::vector src2RawShape = rawShape[ID4];
1227 1223 
1228 std::string dstDtypeStr = DataType2CCEStr(dstDtype);1224 std::string dstDtypeStr = DataType2CCEStr(dstDtype);
1229 std::string src1DtypeStr = DataType2CCEStr(src1Dtype);1225 std::string src1DtypeStr = DataType2CCEStr(src1Dtype);
@@ -1292,9 +1288,9 @@ WhereParam CodeGenOpCloudNPU::PrepareWhereParam() const {
1292 std::vector<std::string> varExpr;1288 std::vector<std::string> varExpr;
1293 std::vector<std::string> dataTypeExpr;1289 std::vector<std::string> dataTypeExpr;
1294 GetWhereVarAndType(varExpr, dataTypeExpr);1290 GetWhereVarAndType(varExpr, dataTypeExpr);
1295- std::vector<int64_t> ds = NormalizeShape(this->rawShape[ToUnderlying(WhereOpIdx::resIdx)], SHAPE_DIM4);1291+ std::vector<int64_t> ds = NormalizeShape(rawShape[ToUnderlying(WhereOpIdx::resIdx)], SHAPE_DIM4);
1296- std::vector<int64_t> c0s = NormalizeShape(this->rawShape[ToUnderlying(WhereOpIdx::condIdx)], SHAPE_DIM4);1292+ std::vector<int64_t> c0s = NormalizeShape(rawShape[ToUnderlying(WhereOpIdx::condIdx)], SHAPE_DIM4);
1297- std::vector<int64_t> s0s = NormalizeShape(this->rawShape[ToUnderlying(WhereOpIdx::src0Idx)], SHAPE_DIM4);1293+ std::vector<int64_t> s0s = NormalizeShape(rawShape[ToUnderlying(WhereOpIdx::src0Idx)], SHAPE_DIM4);
1298 std::vector<std::string> templateList;1294 std::vector<std::string> templateList;
1299 templateList.emplace_back(dataTypeExpr[ToUnderlying(WhereOpIdx::resIdx)]);1295 templateList.emplace_back(dataTypeExpr[ToUnderlying(WhereOpIdx::resIdx)]);
1300 templateList.emplace_back(dataTypeExpr[ToUnderlying(WhereOpIdx::condIdx)]);1296 templateList.emplace_back(dataTypeExpr[ToUnderlying(WhereOpIdx::condIdx)]);
@@ -1438,8 +1434,8 @@ std::string CodeGenOpCloudNPU::GenLogicalNotOp() const {
1438 std::string tmpVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(OpIdx::tmpIdx)]);1434 std::string tmpVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(OpIdx::tmpIdx)]);
1439 std::string srcVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(OpIdx::srcIdx)]);1435 std::string srcVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(OpIdx::srcIdx)]);
1440 1436 
1441- std::vector dstShape = this->rawShape[ToUnderlying(OpIdx::resIdx)];1437+ std::vector dstShape = rawShape[ToUnderlying(OpIdx::resIdx)];
1442- std::vector srcShape = this->rawShape[ToUnderlying(OpIdx::srcIdx)];1438+ std::vector srcShape = rawShape[ToUnderlying(OpIdx::srcIdx)];
1443 1439 
1444 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ToUnderlying(OpIdx::resIdx)]);1440 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ToUnderlying(OpIdx::resIdx)]);
1445 std::string tmpDtypeStr = DataType2CCEStr(operandDtype[ToUnderlying(OpIdx::tmpIdx)]);1441 std::string tmpDtypeStr = DataType2CCEStr(operandDtype[ToUnderlying(OpIdx::tmpIdx)]);
@@ -1707,9 +1703,9 @@ std::string CodeGenOpCloudNPU::GenLogicalAndOp() const {
1707 std::string srcVar0 = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(OpIdx::srcIdx0)]);1703 std::string srcVar0 = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(OpIdx::srcIdx0)]);
1708 std::string srcVar1 = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(OpIdx::srcIdx1)]);1704 std::string srcVar1 = sm->QueryVarNameByTensorMagic(operandWithMagic[ToUnderlying(OpIdx::srcIdx1)]);
1709 1705 
1710- std::vector dstShape = this->rawShape[ToUnderlying(OpIdx::resIdx)];1706+ std::vector dstShape = rawShape[ToUnderlying(OpIdx::resIdx)];
1711- std::vector srcShape0 = this->rawShape[ToUnderlying(OpIdx::srcIdx0)];1707+ std::vector srcShape0 = rawShape[ToUnderlying(OpIdx::srcIdx0)];
1712- std::vector srcShape1 = this->rawShape[ToUnderlying(OpIdx::srcIdx1)];1708+ std::vector srcShape1 = rawShape[ToUnderlying(OpIdx::srcIdx1)];
1713 1709 
1714 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ToUnderlying(OpIdx::resIdx)]);1710 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ToUnderlying(OpIdx::resIdx)]);
1715 std::string tmpDtypeStr = DataType2CCEStr(operandDtype[ToUnderlying(OpIdx::tmpIdx)]);1711 std::string tmpDtypeStr = DataType2CCEStr(operandDtype[ToUnderlying(OpIdx::tmpIdx)]);
@@ -183,7 +183,7 @@ std::string CodeGenOpCloudNPU::GenBinaryOp() const {
183 std::string s0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);183 std::string s0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
184 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);184 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
185 185 
186- std::vector src0RawShape = this->rawShape[ID1];186+ std::vector src0RawShape = rawShape[ID1];
187 CODEGEN_LOGI("genBinaryOp %s, src0RawShape is %s", tileOpName.c_str(), IntVecToStr(src0RawShape).c_str());187 CODEGEN_LOGI("genBinaryOp %s, src0RawShape is %s", tileOpName.c_str(), IntVecToStr(src0RawShape).c_str());
188 188 
189 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);189 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);
@@ -364,8 +364,8 @@ std::string CodeGenOpCloudNPU::GenBinaryWithBrc() const {
364 std::string s0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID2]);364 std::string s0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID2]);
365 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);365 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
366 366 
367- std::vector src0RawShape = this->rawShape[ID2];367+ std::vector src0RawShape = rawShape[ID2];
368- std::vector src1RawShape = this->rawShape[ID3];368+ std::vector src1RawShape = rawShape[ID3];
369 CODEGEN_LOGI("GenBinaryWithBrc %s, src0RawShape is %s", tileOpName.c_str(), IntVecToStr(src0RawShape).c_str());369 CODEGEN_LOGI("GenBinaryWithBrc %s, src0RawShape is %s", tileOpName.c_str(), IntVecToStr(src0RawShape).c_str());
370 370 
371 char buffer[256] = "CG_ERROR";371 char buffer[256] = "CG_ERROR";
@@ -401,8 +401,8 @@ std::string CodeGenOpCloudNPU::PrintBinaryScalarStatic(const PrintBinaryScalarPa
401 const std::string &dVar = param.dVar;401 const std::string &dVar = param.dVar;
402 const std::string &s0Var = param.s0Var;402 const std::string &s0Var = param.s0Var;
403 403 
404- std::vector dstShape = this->rawShape[0];404+ std::vector dstShape = rawShape[0];
405- std::vector src0Shape = this->rawShape[1];405+ std::vector src0Shape = rawShape[1];
406 406 
407 std::vector<int64_t> os0 = NormalizeShape(originShape[1], SHAPE_DIM3);407 std::vector<int64_t> os0 = NormalizeShape(originShape[1], SHAPE_DIM3);
408 std::vector<int64_t> ss = NormalizeShape(src0Shape, SHAPE_DIM3);408 std::vector<int64_t> ss = NormalizeShape(src0Shape, SHAPE_DIM3);
@@ -442,8 +442,8 @@ std::string CodeGenOpCloudNPU::PrintBinaryScalarDynamicUnaligned(const PrintBina
442 const std::string &dVar = param.dVar;442 const std::string &dVar = param.dVar;
443 const std::string &s0Var = param.s0Var;443 const std::string &s0Var = param.s0Var;
444 444 
445- std::vector dstShape = this->rawShape[0];445+ std::vector dstShape = rawShape[0];
446- std::vector src0Shape = this->rawShape[1];446+ std::vector src0Shape = rawShape[1];
447 447 
448 std::vector<int64_t> ss = NormalizeShape(src0Shape, SHAPE_DIM3);448 std::vector<int64_t> ss = NormalizeShape(src0Shape, SHAPE_DIM3);
449 std::vector<int64_t> ds = NormalizeShape(dstShape, SHAPE_DIM3);449 std::vector<int64_t> ds = NormalizeShape(dstShape, SHAPE_DIM3);
@@ -577,8 +577,8 @@ std::string CodeGenOpCloudNPU::GenVectorScalarOpByMode(VecScalMode mode) const {
577 577 
578 AppendLocalBufVarOffsetInOrder(dVar, s0Var);578 AppendLocalBufVarOffsetInOrder(dVar, s0Var);
579 579 
580- std::vector src0RawShape = this->rawShape[1];580+ std::vector src0RawShape = rawShape[1];
581- std::vector dstRawShape = this->rawShape[0];581+ std::vector dstRawShape = rawShape[0];
582 std::vector<int64_t> os0 = NormalizeShape(originShape[1], SHAPE_DIM4);582 std::vector<int64_t> os0 = NormalizeShape(originShape[1], SHAPE_DIM4);
583 std::vector<int64_t> s0 = NormalizeShape(rawShape[1], SHAPE_DIM4);583 std::vector<int64_t> s0 = NormalizeShape(rawShape[1], SHAPE_DIM4);
584 std::vector<int64_t> ds = NormalizeShape(rawShape[0], SHAPE_DIM4);584 std::vector<int64_t> ds = NormalizeShape(rawShape[0], SHAPE_DIM4);
@@ -202,9 +202,9 @@ SortParam CodeGenOpCloudNPU::PrepareSortParam() const {
202 std::string tmpVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);202 std::string tmpVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
203 std::string dstVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);203 std::string dstVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
204 204 
205- std::vector dstShape = this->rawShape[0];205+ std::vector dstShape = rawShape[0];
206- std::vector tmpShape = this->rawShape[1];206+ std::vector tmpShape = rawShape[1];
207- std::vector src0Shape = this->rawShape[2];207+ std::vector src0Shape = rawShape[2];
208 std::vector<int64_t> ds = NormalizeShape(dstShape, SHAPE_DIM4);208 std::vector<int64_t> ds = NormalizeShape(dstShape, SHAPE_DIM4);
209 std::vector<int64_t> ts = NormalizeShape(tmpShape, SHAPE_DIM4);209 std::vector<int64_t> ts = NormalizeShape(tmpShape, SHAPE_DIM4);
210 std::vector<int64_t> ss = NormalizeShape(src0Shape, SHAPE_DIM4);210 std::vector<int64_t> ss = NormalizeShape(src0Shape, SHAPE_DIM4);
@@ -236,16 +236,16 @@ std::string CodeGenOpCloudNPU::PrintExtractStatic() const {
236 SymbolManager::AllocRecord src0, dst;236 SymbolManager::AllocRecord src0, dst;
237 std::string s0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);237 std::string s0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
238 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);238 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
239- std::vector src0RawShape = this->rawShape[1];239+ std::vector src0RawShape = rawShape[1];
240 unsigned tShape0 = 0;240 unsigned tShape0 = 0;
241 unsigned tShape1 = 0;241 unsigned tShape1 = 0;
242 242 
243 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);243 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);
244 std::string src0DtypeStr = DataType2CCEStr(operandDtype[ID1]);244 std::string src0DtypeStr = DataType2CCEStr(operandDtype[ID1]);
245- std::vector src0Shape = this->rawShape[0];245+ std::vector src0Shape = rawShape[0];
246 AppendLocalBufVarOffsetInOrder(dVar, s0Var);246 AppendLocalBufVarOffsetInOrder(dVar, s0Var);
247 constexpr unsigned defaultDim = 1u;247 constexpr unsigned defaultDim = 1u;
248- if (this->rawShape[1].size() == 1) {248+ if (rawShape[1].size() == 1) {
249 tShape1 = std::min(src0RawShape[0], shape[0][0]);249 tShape1 = std::min(src0RawShape[0], shape[0][0]);
250 tShape0 = defaultDim;250 tShape0 = defaultDim;
251 } else {251 } else {
@@ -273,7 +273,7 @@ std::string CodeGenOpCloudNPU::PrintExtractDynamicUnaligned() const {
273 SymbolManager::AllocRecord src0, dst;273 SymbolManager::AllocRecord src0, dst;
274 std::string s0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);274 std::string s0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
275 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);275 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
276- std::vector src0RawShape = this->rawShape[1];276+ std::vector src0RawShape = rawShape[1];
277 277 
278 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);278 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);
279 std::string src0DtypeStr = DataType2CCEStr(operandDtype[ID1]);279 std::string src0DtypeStr = DataType2CCEStr(operandDtype[ID1]);
@@ -281,7 +281,7 @@ std::string CodeGenOpCloudNPU::PrintExtractDynamicUnaligned() const {
281 281 
282 std::vector<std::string> paramList;282 std::vector<std::string> paramList;
283 paramList.insert(paramList.end(), {dstDtypeStr, src0DtypeStr});283 paramList.insert(paramList.end(), {dstDtypeStr, src0DtypeStr});
284- std::vector dstShape = this->rawShape[0];284+ std::vector dstShape = rawShape[0];
285 std::vector<int64_t> ds = NormalizeShape(dstShape, SHAPE_DIM4);285 std::vector<int64_t> ds = NormalizeShape(dstShape, SHAPE_DIM4);
286 paramList.insert(paramList.end(), {std::to_string(ds[ID1]), std::to_string(ds[ID2]), std::to_string(ds[ID3])});286 paramList.insert(paramList.end(), {std::to_string(ds[ID1]), std::to_string(ds[ID2]), std::to_string(ds[ID3])});
287 287 
@@ -339,9 +339,9 @@ TiledSortParam CodeGenOpCloudNPU::PrepareTiledSortParam() const {
339 std::string src2Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID4]);339 std::string src2Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID4]);
340 std::string src3Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID5]);340 std::string src3Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID5]);
341 341 
342- std::vector dstShape = this->rawShape[ID0];342+ std::vector dstShape = rawShape[ID0];
343- std::vector src0Shape = this->rawShape[ID2];343+ std::vector src0Shape = rawShape[ID2];
344- std::vector src3Shape = this->rawShape[ID5];344+ std::vector src3Shape = rawShape[ID5];
345 std::vector<int64_t> ds = NormalizeShape(dstShape, SHAPE_DIM4);345 std::vector<int64_t> ds = NormalizeShape(dstShape, SHAPE_DIM4);
346 std::vector<int64_t> s0 = NormalizeShape(src0Shape, SHAPE_DIM4);346 std::vector<int64_t> s0 = NormalizeShape(src0Shape, SHAPE_DIM4);
347 std::vector<int64_t> s3 = NormalizeShape(src3Shape, SHAPE_DIM4);347 std::vector<int64_t> s3 = NormalizeShape(src3Shape, SHAPE_DIM4);
@@ -378,35 +378,40 @@ std::string CodeGenOpCloudNPU::GenTiledMrgSortOp() const {
378 return PrintTiledMrgSortDynamicUnaligned(tiledSortParm);378 return PrintTiledMrgSortDynamicUnaligned(tiledSortParm);
379}379}
380 380 
381-std::string CodeGenOpCloudNPU::GenSortOp() const {381+std::string CodeGenOpCloudNPU::GenSortOpWithParams(const std::set<int> &idx) const {
382 std::string xDtypeStr = DataType2CCEStr(operandDtype[ID0]);382 std::string xDtypeStr = DataType2CCEStr(operandDtype[ID0]);
383 std::string idxDtypeStr = DataType2CCEStr(operandDtype[ID1]);383 std::string idxDtypeStr = DataType2CCEStr(operandDtype[ID1]);
384 384 
385- std::string yVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);385+ std::vector<std::string> varNames;
386- std::string yIdxVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);386+ for (int i = 0; i < operandCnt; ++i) {
387- std::string tmpVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID2]);387+ varNames.emplace_back(sm->QueryVarNameByTensorMagic(operandWithMagic[i]));
388- std::string xVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3]);388+ }
389- AppendLocalBufVarOffsetInOrder(yVar, yIdxVar, tmpVar, xVar);
390 389 
391- auto xShape = this->rawShape[ID0];390+ std::map<unsigned, std::reference_wrapper<std::string>> varsMap;
392- auto idxShape = this->rawShape[ID1];391+ for (int i = 0; i < operandCnt; ++i) {
392+ varsMap.emplace(i, std::ref(varNames[i]));
393+ }
394+ AppendLocalBufferVarOffset(varsMap);
395+ 
396+ auto xShape = rawShape[ID0];
397+ auto idxShape = rawShape[ID1];
393 398 
394 std::vector<std::string> paramList;399 std::vector<std::string> paramList;
395 paramList.emplace_back(xDtypeStr);400 paramList.emplace_back(xDtypeStr);
396 paramList.emplace_back(idxDtypeStr);401 paramList.emplace_back(idxDtypeStr);
397- paramList.emplace_back(std::to_string(xShape[0]));402+ paramList.emplace_back(std::to_string(xShape[ID0]));
398- paramList.emplace_back(std::to_string(xShape[1]));403+ paramList.emplace_back(std::to_string(xShape[ID1]));
399- paramList.emplace_back(std::to_string(idxShape[0]));404+ paramList.emplace_back(std::to_string(idxShape[ID0]));
400- paramList.emplace_back(std::to_string(idxShape[1]));405+ paramList.emplace_back(std::to_string(idxShape[ID1]));
401 std::string templateParam = JoinString(paramList, CONN_COMMA);406 std::string templateParam = JoinString(paramList, CONN_COMMA);
402 templateParam += GenOpAttr();407 templateParam += GenOpAttr();
403 408 
404 paramList.clear();409 paramList.clear();
405- std::string y = "(" + GetAddrTypeByOperandType(operandType[ID0]) + " " + xDtypeStr + "*)" + yVar;410+ for (int i = 0; i < operandCnt; ++i) {
406- std::string yIdx = "(" + GetAddrTypeByOperandType(operandType[ID1]) + " " + idxDtypeStr + "*)" + yIdxVar;411+ std::string dtypeStr = idx.count(i) ? idxDtypeStr : xDtypeStr;
407- std::string tmp = "(" + GetAddrTypeByOperandType(operandType[ID2]) + " " + xDtypeStr + "*)" + tmpVar;412+ std::string param = "(" + GetAddrTypeByOperandType(operandType[i]) + " " + dtypeStr + "*)" + varNames[i];
408- std::string x = "(" + GetAddrTypeByOperandType(operandType[ID3]) + " " + xDtypeStr + "*)" + xVar;413+ paramList.emplace_back(param);
409- paramList.insert(paramList.end(), {y, yIdx, tmp, x});414+ }
410 std::string tileOpParam = JoinString(paramList, CONN_COMMA);415 std::string tileOpParam = JoinString(paramList, CONN_COMMA);
411 416 
412 std::ostringstream os;417 std::ostringstream os;
@@ -414,86 +419,16 @@ std::string CodeGenOpCloudNPU::GenSortOp() const {
414 return os.str();419 return os.str();
415}420}
416 421 
422+std::string CodeGenOpCloudNPU::GenSortOp() const {
423+ return GenSortOpWithParams({ID1});
424+}
425+ 
417std::string CodeGenOpCloudNPU::GenMergeOp() const {426std::string CodeGenOpCloudNPU::GenMergeOp() const {
418- std::string xDtypeStr = DataType2CCEStr(operandDtype[ID0]);427+ return GenSortOpWithParams({ID1, ID4});
419- std::string idxDtypeStr = DataType2CCEStr(operandDtype[ID1]);
420- 
421- std::string yVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
422- std::string yIdxVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
423- std::string tmpVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID2]);
424- std::string xVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3]);
425- std::string idxVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID4]);
426- AppendLocalBufVarOffsetInOrder(yVar, yIdxVar, tmpVar, xVar, idxVar);
427- 
428- auto xShape = this->rawShape[ID0];
429- auto idxShape = this->rawShape[ID1];
430- 
431- std::vector<std::string> paramList;
432- paramList.emplace_back(xDtypeStr);
433- paramList.emplace_back(idxDtypeStr);
434- paramList.emplace_back(std::to_string(xShape[0]));
435- paramList.emplace_back(std::to_string(xShape[1]));
436- paramList.emplace_back(std::to_string(idxShape[0]));
437- paramList.emplace_back(std::to_string(idxShape[1]));
438- std::string templateParam = JoinString(paramList, CONN_COMMA);
439- templateParam += GenOpAttr();
440- 
441- paramList.clear();
442- std::string y = "(" + GetAddrTypeByOperandType(operandType[ID0]) + " " + xDtypeStr + "*)" + yVar;
443- std::string yIdx = "(" + GetAddrTypeByOperandType(operandType[ID1]) + " " + idxDtypeStr + "*)" + yIdxVar;
444- std::string tmp = "(" + GetAddrTypeByOperandType(operandType[ID2]) + " " + xDtypeStr + "*)" + tmpVar;
445- std::string x = "(" + GetAddrTypeByOperandType(operandType[ID3]) + " " + xDtypeStr + "*)" + xVar;
446- std::string idx = "(" + GetAddrTypeByOperandType(operandType[ID4]) + " " + idxDtypeStr + "*)" + idxVar;
447- paramList.insert(paramList.end(), {y, yIdx, tmp, x, idx});
448- std::string tileOpParam = JoinString(paramList, CONN_COMMA);
449- 
450- std::ostringstream os;
451- os << tileOpName.c_str() << "<" << templateParam << ">" << "(" << tileOpParam << ");\n";
452- return os.str();
453}428}
454 429 
455std::string CodeGenOpCloudNPU::GenCompareAndSwapOp() const {430std::string CodeGenOpCloudNPU::GenCompareAndSwapOp() const {
456- std::string xDtypeStr = DataType2CCEStr(operandDtype[ID0]);431+ return GenSortOpWithParams({ID1, ID3, ID5, ID7});
457- std::string idxDtypeStr = DataType2CCEStr(operandDtype[ID1]);
458- 
459- std::string y0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
460- std::string yIdx0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
461- std::string y1Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID2]);
462- std::string yIdx1Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID3]);
463- std::string x0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID4]);
464- std::string idx0Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID5]);
465- std::string x1Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID6]);
466- std::string idx1Var = sm->QueryVarNameByTensorMagic(operandWithMagic[ID7]);
467- AppendLocalBufVarOffsetInOrder(y0Var, yIdx0Var, y1Var, yIdx1Var, x0Var, idx0Var, x1Var, idx1Var);
468- 
469- auto xShape = this->rawShape[ID0];
470- auto idxShape = this->rawShape[ID1];
471- 
472- std::vector<std::string> paramList;
473- paramList.emplace_back(xDtypeStr);
474- paramList.emplace_back(idxDtypeStr);
475- paramList.emplace_back(std::to_string(xShape[0]));
476- paramList.emplace_back(std::to_string(xShape[1]));
477- paramList.emplace_back(std::to_string(idxShape[0]));
478- paramList.emplace_back(std::to_string(idxShape[1]));
479- std::string templateParam = JoinString(paramList, CONN_COMMA);
480- templateParam += GenOpAttr();
481- 
482- paramList.clear();
483- std::string y0 = "(" + GetAddrTypeByOperandType(operandType[ID0]) + " " + xDtypeStr + "*)" + y0Var;
484- std::string yIdx0 = "(" + GetAddrTypeByOperandType(operandType[ID1]) + " " + idxDtypeStr + "*)" + yIdx0Var;
485- std::string y1 = "(" + GetAddrTypeByOperandType(operandType[ID2]) + " " + xDtypeStr + "*)" + y1Var;
486- std::string yIdx1 = "(" + GetAddrTypeByOperandType(operandType[ID3]) + " " + idxDtypeStr + "*)" + yIdx1Var;
487- std::string x0 = "(" + GetAddrTypeByOperandType(operandType[ID4]) + " " + xDtypeStr + "*)" + x0Var;
488- std::string idx0 = "(" + GetAddrTypeByOperandType(operandType[ID5]) + " " + idxDtypeStr + "*)" + idx0Var;
489- std::string x1 = "(" + GetAddrTypeByOperandType(operandType[ID6]) + " " + xDtypeStr + "*)" + x1Var;
490- std::string idx1 = "(" + GetAddrTypeByOperandType(operandType[ID7]) + " " + idxDtypeStr + "*)" + idx1Var;
491- paramList.insert(paramList.end(), {y0, yIdx0, y1, yIdx1, x0, idx0, x1, idx1});
492- std::string tileOpParam = JoinString(paramList, CONN_COMMA);
493- 
494- std::ostringstream os;
495- os << tileOpName.c_str() << "<" << templateParam << ">" << "(" << tileOpParam << ");\n";
496- return os.str();
497}432}
498 433 
499std::string CodeGenOpCloudNPU::GenTopKSortOp() const {434std::string CodeGenOpCloudNPU::GenTopKSortOp() const {
@@ -513,7 +448,7 @@ std::string CodeGenOpCloudNPU::GenTopKSortOp() const {
513 startIdx = SymbolicExpressionTable::BuildExpression(scalarExpr);448 startIdx = SymbolicExpressionTable::BuildExpression(scalarExpr);
514 }449 }
515 450 
516- auto xShape = this->rawShape[ID2];451+ auto xShape = rawShape[ID2];
517 452 
518 std::vector<std::string> paramList;453 std::vector<std::string> paramList;
519 paramList.emplace_back(xDtypeStr);454 paramList.emplace_back(xDtypeStr);
@@ -550,7 +485,7 @@ std::string CodeGenOpCloudNPU::GenTopKMergeOp() const {
550 std::string xVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);485 std::string xVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
551 AppendLocalBufVarOffsetInOrder(yVar, xVar);486 AppendLocalBufVarOffsetInOrder(yVar, xVar);
552 487 
553- auto xShape = this->rawShape[ID0];488+ auto xShape = rawShape[ID0];
554 489 
555 std::vector<std::string> paramList;490 std::vector<std::string> paramList;
556 paramList.emplace_back(xDtypeStr);491 paramList.emplace_back(xDtypeStr);
@@ -578,8 +513,8 @@ std::string CodeGenOpCloudNPU::GenTopKExtractOp() const {
578 std::string xVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);513 std::string xVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
579 AppendLocalBufVarOffsetInOrder(yVar, xVar);514 AppendLocalBufVarOffsetInOrder(yVar, xVar);
580 515 
581- auto yShape = this->rawShape[ID0];516+ auto yShape = rawShape[ID0];
582- auto xShape = this->rawShape[ID1];517+ auto xShape = rawShape[ID1];
583 518 
584 std::vector<std::string> paramList;519 std::vector<std::string> paramList;
585 paramList.emplace_back(yDtypeStr);520 paramList.emplace_back(yDtypeStr);
@@ -617,8 +552,8 @@ std::string CodeGenOpCloudNPU::PrintSortUBDynamicUnaligned(bool containDstType)
617 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);552 std::string dstDtypeStr = DataType2CCEStr(operandDtype[ID0]);
618 std::string srcDtypeStr = DataType2CCEStr(operandDtype[ID1]);553 std::string srcDtypeStr = DataType2CCEStr(operandDtype[ID1]);
619 554 
620- std::vector<int64_t> ds = NormalizeShape(this->rawShape[ID0], SHAPE_DIM4);555+ std::vector<int64_t> ds = NormalizeShape(rawShape[ID0], SHAPE_DIM4);
621- std::vector<int64_t> ss = NormalizeShape(this->rawShape[ID1], SHAPE_DIM4);556+ std::vector<int64_t> ss = NormalizeShape(rawShape[ID1], SHAPE_DIM4);
622 557 
623 auto dynSrcShape = dynamicValidShape[ID1];558 auto dynSrcShape = dynamicValidShape[ID1];
624 FillIntVecWithDummyInHead<SymbolicScalar>(dynSrcShape, SHAPE_DIM4 - dynamicValidShape[ID1].size(), 1);559 FillIntVecWithDummyInHead<SymbolicScalar>(dynSrcShape, SHAPE_DIM4 - dynamicValidShape[ID1].size(), 1);
@@ -478,10 +478,10 @@ std::string CodeGenOpCloudNPU::GenUnaryOpWithTmpBuff() const {
478 std::string tmpVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);478 std::string tmpVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID1]);
479 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);479 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
480 480 
481- std::vector srcShape = this->rawShape[2];481+ std::vector srcShape = rawShape[2];
482 CODEGEN_LOGI("GenUnaryOpWithTmpBuff %s src raw shape: %s", tileOpName.c_str(), IntVecToStr(srcShape).c_str());482 CODEGEN_LOGI("GenUnaryOpWithTmpBuff %s src raw shape: %s", tileOpName.c_str(), IntVecToStr(srcShape).c_str());
483 483 
484- std::vector dstShape = this->rawShape[0];484+ std::vector dstShape = rawShape[0];
485 CODEGEN_LOGI("GenUnaryOpWithTmpBuff %s dst raw shape: %s", tileOpName.c_str(), IntVecToStr(dstShape).c_str());485 CODEGEN_LOGI("GenUnaryOpWithTmpBuff %s dst raw shape: %s", tileOpName.c_str(), IntVecToStr(dstShape).c_str());
486 486 
487 std::string srcDtypeStr = DataType2CCEStr(operandDtype[ID2]);487 std::string srcDtypeStr = DataType2CCEStr(operandDtype[ID2]);
@@ -101,12 +101,12 @@ void CodeGenOp::UpdateShape(
101 101 
102 Opcode opcode = oper.GetOpcode();102 Opcode opcode = oper.GetOpcode();
103 if (logicalTensor.GetMemoryTypeOriginal() == MEM_DEVICE_DDR && IsOpShapeFromAttr(opcode)) {103 if (logicalTensor.GetMemoryTypeOriginal() == MEM_DEVICE_DDR && IsOpShapeFromAttr(opcode)) {
104- // used for spilling GM scene
105 std::shared_ptr<CopyOpAttribute> attr = std::static_pointer_cast<CopyOpAttribute>(oper.GetOpAttribute());104 std::shared_ptr<CopyOpAttribute> attr = std::static_pointer_cast<CopyOpAttribute>(oper.GetOpAttribute());
106 ASSERT(OperErr::ATTRIBUTE_INVALID, attr != nullptr) << ": missing OpAttr in copy op: \n" << oper.Dump();105 ASSERT(OperErr::ATTRIBUTE_INVALID, attr != nullptr) << ": missing OpAttr in copy op: \n" << oper.Dump();
107- shape[operandIdx] = attr->GetSpecifiedShape(1);106+ // 1. for spilling GM scene 2. for conv
108- CODEGEN_LOGI("attrShape(from op CopyOpAttribute) = %s", IntVecToStr(shape[operandIdx]).c_str());107+ shapeFromAttr[operandIdx] = attr->GetSpecifiedShape(1);
109- } else { // Local Tensor shape just use shape from LogicalTensor108+ CODEGEN_LOGI("attrShape(from op CopyOpAttribute) = %s", IntVecToStr(shapeFromAttr[operandIdx]).c_str());
109+ } else { // Tile Shape from LogicalTensor (Only used in extremely special cases)
110 shape[operandIdx] = logicalTensor.shape;110 shape[operandIdx] = logicalTensor.shape;
111 }111 }
112 if ((opCode == Opcode::OP_L0C_TO_L1) && (operandIdx == 0)) {112 if ((opCode == Opcode::OP_L0C_TO_L1) && (operandIdx == 0)) {
@@ -81,6 +81,7 @@ protected:
81 std::string GenOpAttr(bool hasExistingParam = true) const;81 std::string GenOpAttr(bool hasExistingParam = true) const;
82 82 
83 const Operation &originalOp;83 const Operation &originalOp;
84+ const FunctionType functionType;
84 std::string opCodeStr;85 std::string opCodeStr;
85 Opcode opCode{Opcode::OP_UNKNOWN};86 Opcode opCode{Opcode::OP_UNKNOWN};
86 std::string aliasOp; // alias op name87 std::string aliasOp; // alias op name
@@ -93,6 +94,8 @@ protected:
93 Element extOperandVal;94 Element extOperandVal;
94 SymbolicScalar extSymbolicScalar;95 SymbolicScalar extSymbolicScalar;
95 std::vector<Element> extScalarVec;96 std::vector<Element> extScalarVec;
97+ bool isPartialMem[MAX_OPERANDS] = {};
98+ 
96 std::vector<int64_t> offset[MAX_OPERANDS] = {};99 std::vector<int64_t> offset[MAX_OPERANDS] = {};
97 std::vector<int64_t> shape[MAX_OPERANDS] = {};100 std::vector<int64_t> shape[MAX_OPERANDS] = {};
98 std::vector<int64_t> rawShape[MAX_OPERANDS] = {};101 std::vector<int64_t> rawShape[MAX_OPERANDS] = {};
@@ -101,8 +104,9 @@ protected:
101 std::vector<int64_t> originShape[MAX_OPERANDS] = {};104 std::vector<int64_t> originShape[MAX_OPERANDS] = {};
102 std::vector<SymbolicScalar> dynamicOffset[MAX_OPERANDS] = {};105 std::vector<SymbolicScalar> dynamicOffset[MAX_OPERANDS] = {};
103 std::vector<SymbolicScalar> dynamicValidShape[MAX_OPERANDS] = {}; // valid shape106 std::vector<SymbolicScalar> dynamicValidShape[MAX_OPERANDS] = {}; // valid shape
104- std::vector<SymbolicScalar> offsetFromAttr[MAX_OPERANDS] = {}; // for spilling to GM scene107+ 
105- bool isPartialMem[MAX_OPERANDS] = {};108+ std::vector<int64_t> shapeFromAttr[MAX_OPERANDS] = {}; // 1.for spilling to GM scene 2.for conv
109+ std::vector<SymbolicScalar> offsetFromAttr[MAX_OPERANDS] = {}; // for spilling to GM scene
106 std::vector<SymbolicScalar> dynValidShapeFromOpAttr[MAX_OPERANDS] = {};110 std::vector<SymbolicScalar> dynValidShapeFromOpAttr[MAX_OPERANDS] = {};
107 // if operand is an variable, record its related argument location111 // if operand is an variable, record its related argument location
108 // In COA(Call Operation Attribute), 0-index is the callee's cce info. So the tensor list starts from 1.112 // In COA(Call Operation Attribute), 0-index is the callee's cce info. So the tensor list starts from 1.
@@ -118,14 +122,13 @@ protected:
118 std::map<std::string, Any> opAttrs;122 std::map<std::string, Any> opAttrs;
119 123 
120 std::shared_ptr<SymbolManager> sm{nullptr};124 std::shared_ptr<SymbolManager> sm{nullptr};
125+ const std::map<int, int> &paramLocToParamListOffset{};
121 126 
122- const FunctionType functionType;
123 std::string tileOpName;127 std::string tileOpName;
124 bool isInputForceCombineAxis{false};128 bool isInputForceCombineAxis{false};
125 bool isSupportDynamicAligned{false}; // NEXTNEXT delete after all TileOp is changed to TileTensor Mode129 bool isSupportDynamicAligned{false}; // NEXTNEXT delete after all TileOp is changed to TileTensor Mode
126 bool isDynamicFunction{false};130 bool isDynamicFunction{false};
127 bool isSupportLayout{false};131 bool isSupportLayout{false};
128- const std::map<int, int> &paramLocToParamListOffset{};
129 bool isUnderDynamicFunction{false};132 bool isUnderDynamicFunction{false};
130 int operandCnt{0};133 int operandCnt{0};
131 bool isMainBlock{false};134 bool isMainBlock{false};
@@ -17,8 +17,11 @@
17 17 
18#include <cstring>18#include <cstring>
19#include <algorithm>19#include <algorithm>
20+#include <thread>
20#include <unistd.h>21#include <unistd.h>
21 22 
23+#include "interface/configs/config_manager.h"
24+ 
22#include "codegen/codegen_common.h"25#include "codegen/codegen_common.h"
23#include "interface/utils/log.h"26#include "interface/utils/log.h"
24 27 
@@ -125,4 +128,13 @@ void PrintIndent(std::ostringstream &os, int scopeLevel) {
125 }128 }
126}129}
127 130 
131+unsigned GetCGThreadNum() {
132+ unsigned threadNum = ConfigManager::Instance().GetCodeGenConfig(KEY_PARALLEL_COMPILE, 1u);
133+ unsigned cpuCores = std::thread::hardware_concurrency();
134+ if (cpuCores != 0 && threadNum > cpuCores * 2) {
135+ return cpuCores * 2;
136+ }
137+ return threadNum;
138+}
139+ 
128} // namespace npu::tile_fwk140} // namespace npu::tile_fwk
@@ -188,5 +188,7 @@ std::string CopyOutModeToString(Matrix::CopyOutMode copyMode);
188 188 
189std::string PaddingModeToString(Matrix::PaddingMode paddingMode);189std::string PaddingModeToString(Matrix::PaddingMode paddingMode);
190 190 
191+unsigned GetCGThreadNum();
192+ 
191} // namespace npu::tile_fwk193} // namespace npu::tile_fwk
192#endif194#endif
@@ -61,11 +61,7 @@ void TaskRunner(ThreadSafeTaskQueue &taskQueue) {
61}; // namespace61}; // namespace
62 62 
63void ParallelExecuteAndWait(unsigned threadNum, std::deque<Task> tasks) {63void ParallelExecuteAndWait(unsigned threadNum, std::deque<Task> tasks) {
64- constexpr unsigned threshold = 128;64+ if (threadNum == 0) {
65- if (threadNum > threshold) {
66- CODEGEN_LOGI("ParallelExecute: threadNum is too large and thus ignored. Reset to %ud.", threshold);
67- threadNum = threshold;
68- } else if (threadNum == 0) {
69 threadNum = 1;65 threadNum = 1;
70 }66 }
71 67 
@@ -18,6 +18,7 @@
18#include "tilefwk/tilefwk.h"18#include "tilefwk/tilefwk.h"
19#include "codegen/codegen.h"19#include "codegen/codegen.h"
20#include "codegen/utils/parallel_execute.h"20#include "codegen/utils/parallel_execute.h"
21+#include "codegen/utils/codegen_utils.h"
21#include "interface/inner/tilefwk.h"22#include "interface/inner/tilefwk.h"
22#include "interface/program/program.h"23#include "interface/program/program.h"
23#include "interface/operation/operation.h"24#include "interface/operation/operation.h"
@@ -893,7 +894,7 @@ static void CompileDyndevFunction(Function *function, FunctionCache &cache, [[ma
893 tasks.push_back(task);894 tasks.push_back(task);
894 }895 }
895 896
896- unsigned threadNum = ConfigManager::Instance().GetCodeGenConfig(KEY_PARALLEL_COMPILE, 1u);897+ unsigned threadNum = GetCGThreadNum();
897 ParallelExecuteAndWait(threadNum, tasks);898 ParallelExecuteAndWait(threadNum, tasks);
898 899 
899 struct EncodeDevAscendFunctionParam encodeDevAscendFunctionParam = {};900 struct EncodeDevAscendFunctionParam encodeDevAscendFunctionParam = {};