Merged
fix(operation): Fix TopK RadixSelect Temp Workspace #6342
刘奕哲Add on Sep 15
fix(operation): Fix TopK RadixSelect Temp Workspace #6342
Merged
Merged fromCreeeeper/pyptotopk-tmp-fix-9.2.0intocann/pypto9.2.0
Total: 5 files changed+75-46
| @@ -39,7 +39,7 @@ cbrt(input: Tensor) -> Tensor | |||
| 39 | ## 约束说明 | 39 | ## 约束说明 |
| 40 | 40 | ||
| 41 | 1. 输入Tensor和输出Tensor类型应该相同。 | 41 | 1. 输入Tensor和输出Tensor类型应该相同。 |
| 42 | -2. 在综合考虑输入、输出及临时存储空间占用的前提下,TileShape大小需满足额外约束条件。假设TileShape为\[a,b,c,d\],记$d_{align}=CeilAlign(d, 32/sizeof\(dt\))$,则总的UB空间占用为: | 42 | +2. 在综合考虑输入、输出及临时存储空间占用的前提下,TileShape大小需满足额外约束条件。假设TileShape为\[a,b,c,d\],记$d_{align}=CeilAlign(d, 32/sizeof(dt))$,则总的UB空间占用为: |
| 43 | 43 | ||
| 44 | $$ | 44 | $$ |
| 45 | 4*a*b*c*d_{align}*sizeof(dt) <= UB | 45 | 4*a*b*c*d_{align}*sizeof(dt) <= UB |
| @@ -44,7 +44,15 @@ topk(input: Tensor, k: int, dim: Optional[int] = None, largest: bool = True, alg | |||
| 44 | 44 | ||
| 45 | 1. 只支持对尾轴进行topk操作; | 45 | 1. 只支持对尾轴进行topk操作; |
| 46 | 2. 选用MERGE_SORT算法时,TileShape尾轴需要小于22KB\(TileShape\[-1\]\*4 < 22KB\); | 46 | 2. 选用MERGE_SORT算法时,TileShape尾轴需要小于22KB\(TileShape\[-1\]\*4 < 22KB\); |
| 47 | -3. 选用RADIX_SELECT算法时,记TileShape的次尾轴为tileH(若不存在则为1),尾轴为tileW,tileW对齐到128记为tileAlign,则需要临时空间:blockNum\*tileH\*tileAlign(当输入数据类型为UINT64/INT64时blockNum为46,其余情况为26),临时空间加上输入输出的tile块不能超过UB大小; | 47 | +3. 选用RADIX_SELECT算法时,由于存在临时内存使用,设置TileShape时需保证输入Tile、输出Tile及临时空间的总占用小于可用UB。记TileShape的次尾轴为tileH(若不存在则为1),尾轴为tileW,tileW对齐到128记为tileWAlign,各数据类型对应的临时空间如下: |
| 48 | + | ||
| 49 | + | 输入数据类型 | 临时空间大小(字节) | | ||
| 50 | + |--------------|----------------------| | ||
| 51 | + | DT_INT8、DT_UINT8、DT_INT16、DT_UINT16、DT_FP16、DT_BF16 | `tileH * max(6 * tileWAlign + max(3072, 8 * tileWAlign), 22 * tileWAlign)` | | ||
| 52 | + | DT_FP32、DT_INT32、DT_UINT32 | `tileH * max(12 * tileWAlign + max(3072, 8 * tileWAlign), 26 * tileWAlign)` | | ||
| 53 | + | DT_INT64、DT_UINT64 | `tileH * max(22 * tileWAlign + max(3072, 8 * tileWAlign), 34 * tileWAlign)` | | ||
| 54 | + | ||
| 55 | + 表格中的max\(a,b\)表示取a和b中的最大值。 | ||
| 48 | 4. 选用RADIX_SELECT算法时,尾轴不可切分,TileShape\[-1\]必须大于等于input.shape\[-1\]; | 56 | 4. 选用RADIX_SELECT算法时,尾轴不可切分,TileShape\[-1\]必须大于等于input.shape\[-1\]; |
| 49 | 5. k <= TileShape\[-1\] && k <= input.shape\[-1\]; | 57 | 5. k <= TileShape\[-1\] && k <= input.shape\[-1\]; |
| 50 | 6. RADIX_SELECT算法在不同型号的支持度: | 58 | 6. RADIX_SELECT算法在不同型号的支持度: |
| @@ -48,13 +48,16 @@ constexpr int32_t NUM_VALUE_7 = 7; | |||
| 48 | constexpr int32_t NUM_VALUE_8 = 8; | 48 | constexpr int32_t NUM_VALUE_8 = 8; |
| 49 | constexpr int32_t NUM_VALUE_9 = 9; | 49 | constexpr int32_t NUM_VALUE_9 = 9; |
| 50 | constexpr int32_t NUM_VALUE_10 = 10; | 50 | constexpr int32_t NUM_VALUE_10 = 10; |
| 51 | +constexpr int32_t NUM_VALUE_12 = 12; | ||
| 51 | constexpr int32_t NUM_VALUE_16 = 16; | 52 | constexpr int32_t NUM_VALUE_16 = 16; |
| 53 | +constexpr int32_t NUM_VALUE_22 = 22; | ||
| 52 | constexpr int32_t NUM_VALUE_26 = 26; | 54 | constexpr int32_t NUM_VALUE_26 = 26; |
| 53 | constexpr int32_t NUM_VALUE_31 = 31; | 55 | constexpr int32_t NUM_VALUE_31 = 31; |
| 54 | constexpr int32_t NUM_VALUE_32 = 32; | 56 | constexpr int32_t NUM_VALUE_32 = 32; |
| 55 | -constexpr int32_t NUM_VALUE_46 = 46; | 57 | +constexpr int32_t NUM_VALUE_34 = 34; |
| 56 | constexpr int32_t NUM_VALUE_64 = 64; | 58 | constexpr int32_t NUM_VALUE_64 = 64; |
| 57 | constexpr int32_t NUM_VALUE_128 = 128; | 59 | constexpr int32_t NUM_VALUE_128 = 128; |
| 60 | +constexpr int32_t NUM_VALUE_256 = 256; | ||
| 58 | constexpr double NUM_VALUE_0_5 = 0.5; | 61 | constexpr double NUM_VALUE_0_5 = 0.5; |
| 59 | constexpr double NUM_VALUE_EPS = 1e-9; | 62 | constexpr double NUM_VALUE_EPS = 1e-9; |
| 60 | 63 | ||
| @@ -466,6 +466,36 @@ void TiledTopK(Function& function, const TileShape& tileShape, size_t cur, Input | |||
| 466 | } | 466 | } |
| 467 | } | 467 | } |
| 468 | 468 | ||
| 469 | +int64_t RadixSelectGetSortTempBlockCount(int64_t bytesOfInput) | ||
| 470 | +{ | ||
| 471 | + switch (bytesOfInput) { | ||
| 472 | + case NUM_VALUE_1: | ||
| 473 | + case NUM_VALUE_2: | ||
| 474 | + return NUM_VALUE_22; | ||
| 475 | + case NUM_VALUE_4: | ||
| 476 | + return NUM_VALUE_26; | ||
| 477 | + case NUM_VALUE_8: | ||
| 478 | + return NUM_VALUE_34; | ||
| 479 | + default: | ||
| 480 | + return 0; | ||
| 481 | + } | ||
| 482 | +} | ||
| 483 | + | ||
| 484 | +int64_t RadixSelectGetCalcTopKTempBlockCount(int64_t bytesOfInput) | ||
| 485 | +{ | ||
| 486 | + switch (bytesOfInput) { | ||
| 487 | + case NUM_VALUE_1: | ||
| 488 | + case NUM_VALUE_2: | ||
| 489 | + return NUM_VALUE_6; | ||
| 490 | + case NUM_VALUE_4: | ||
| 491 | + return NUM_VALUE_12; | ||
| 492 | + case NUM_VALUE_8: | ||
| 493 | + return NUM_VALUE_22; | ||
| 494 | + default: | ||
| 495 | + return 0; | ||
| 496 | + } | ||
| 497 | +} | ||
| 498 | + | ||
| 469 | void TiledTopKRadixSelect(Function& function, const TileShape& tileShape, size_t cur, Input& input, | 499 | void TiledTopKRadixSelect(Function& function, const TileShape& tileShape, size_t cur, Input& input, |
| 470 | const LogicalTensorPtr& valueResult, const LogicalTensorPtr& indexResult, int axis, int k, | 500 | const LogicalTensorPtr& valueResult, const LogicalTensorPtr& indexResult, int axis, int k, |
| 471 | int isLargest, int64_t ubSize) | 501 | int isLargest, int64_t ubSize) |
| @@ -477,11 +507,15 @@ void TiledTopKRadixSelect(Function& function, const TileShape& tileShape, size_t | |||
| 477 | input.tileInfo.shape[cur] = valueResult->shape[cur]; | 507 | input.tileInfo.shape[cur] = valueResult->shape[cur]; |
| 478 | auto valueTile = valueResult->View(function, input.tileInfo.shape, input.tileInfo.offset); | 508 | auto valueTile = valueResult->View(function, input.tileInfo.shape, input.tileInfo.offset); |
| 479 | auto indexTile = indexResult->View(function, input.tileInfo.shape, input.tileInfo.offset); | 509 | auto indexTile = indexResult->View(function, input.tileInfo.shape, input.tileInfo.offset); |
| 480 | - int64_t blockNum = NUM_VALUE_26; | 510 | + int64_t tileAlign = AlignUp(tileShape.GetVecTile()[cur], NUM_VALUE_128); |
| 481 | - if (BytesOf(input.tensor.GetDataType()) == BytesOf(DT_INT64)) { | 511 | + int64_t bytesOfInput = BytesOf(input.tensor.GetDataType()); |
| 482 | - blockNum = NUM_VALUE_46; | 512 | + int64_t calcTopKWorkspace1 = tileAlign * RadixSelectGetCalcTopKTempBlockCount(bytesOfInput); |
| 483 | - } | 513 | + int64_t calcTopKBranch1Workspace = NUM_VALUE_256 * BytesOf(DT_INT32) * NUM_VALUE_3; |
| 484 | - std::vector<int64_t> tmpShape = {static_cast<int64_t>(AlignUp(lastDim, NUM_VALUE_128) * blockNum)}; | 514 | + int64_t calcTopKBranch2Workspace = tileAlign * BytesOf(DT_INT32) * NUM_VALUE_2; |
| 515 | + int64_t calcTopKWorkspace2 = std::max(calcTopKBranch1Workspace, calcTopKBranch2Workspace); | ||
| 516 | + int64_t calcTopKWorkspace = calcTopKWorkspace1 + calcTopKWorkspace2; | ||
| 517 | + int64_t sortWorkspace = tileAlign * RadixSelectGetSortTempBlockCount(bytesOfInput); | ||
| 518 | + std::vector<int64_t> tmpShape = {std::max(calcTopKWorkspace, sortWorkspace)}; | ||
| 485 | if (cur > 0) { | 519 | if (cur > 0) { |
| 486 | tmpShape[0] *= input.tileInfo.shape[cur - 1]; | 520 | tmpShape[0] *= input.tileInfo.shape[cur - 1]; |
| 487 | } | 521 | } |
Mframework/tests/ut/codegen/src/test_dynamic/test_codegen_dyn_vector/test_codegen_dyn_sort.cpp+22-38
| @@ -232,49 +232,15 @@ TEST_F(TestCodegenDynSort, TestDynExtractSingle) | |||
| 232 | EXPECT_EQ(res, expect); | 232 | EXPECT_EQ(res, expect); |
| 233 | } | 233 | } |
| 234 | 234 | ||
| 235 | -TEST_F(TestCodegenDynSort, TestRadixSelectFP32) | 235 | +void TestRadixSelect(DataType inputDataType, std::string const& funcName, std::string const& expect) |
| 236 | -{ | ||
| 237 | - config::SetCodeGenConfig(KEY_CODEGEN_SUPPORT_TILE_TENSOR, true); | ||
| 238 | - | ||
| 239 | - std::vector<int64_t> srcShape = {64, 64}; | ||
| 240 | - std::vector<int64_t> dstShape = {64, 16}; | ||
| 241 | - std::vector<int64_t> tmpShape = {64, 1024}; | ||
| 242 | - std::vector<SymbolicScalar> srcDynValidShape = {64, 64}; | ||
| 243 | - std::vector<SymbolicScalar> dstDynValidShape = {64, 16}; | ||
| 244 | - std::vector<SymbolicScalar> tmpDynValidShape = {64, 1024}; | ||
| 245 | - TileShape::Current().SetVecTile({64, 64}); | ||
| 246 | - auto function = GenMockFuncDyn("TestRadixSelectFP32"); | ||
| 247 | - auto localTensorInput = CreateLogicalTensor( | ||
| 248 | - {*function, DataType::DT_FP32, MemoryType::MEM_UB, srcShape, srcDynValidShape}); | ||
| 249 | - auto localTensorValue = CreateLogicalTensor( | ||
| 250 | - {*function, DataType::DT_FP32, MemoryType::MEM_UB, dstShape, dstDynValidShape}); | ||
| 251 | - auto localTensorIndex = CreateLogicalTensor( | ||
| 252 | - {*function, DataType::DT_INT32, MemoryType::MEM_UB, dstShape, dstDynValidShape}); | ||
| 253 | - auto localTensorTmp = CreateLogicalTensor( | ||
| 254 | - {*function, DataType::DT_UINT8, MemoryType::MEM_UB, tmpShape, tmpDynValidShape}); | ||
| 255 | - auto& op = function->AddOperation(Opcode::OP_RADIX_SELECT, {localTensorInput}, | ||
| 256 | - {localTensorValue, localTensorIndex, localTensorTmp}); | ||
| 257 | - op.SetAttribute(OP_ATTR_PREFIX + "kvalue", 16); | ||
| 258 | - op.SetAttribute(OP_ATTR_PREFIX + "order", 1); | ||
| 259 | - | ||
| 260 | - std::string res = GenOpCodeFromOp(*function, op); | ||
| 261 | - std::string expect = | ||
| 262 | - R"!!!(TRadixSelect<16, 1>(ubTensor_0, ubTensor_1, ubTensor_2, ubTensor_3); | ||
| 263 | -)!!!"; | ||
| 264 | - CheckStringExist(expect, res); | ||
| 265 | -} | ||
| 266 | - | ||
| 267 | -TEST_F(TestCodegenDynSort, TestRadixSelectInt64) | ||
| 268 | { | 236 | { |
| 269 | config::SetCodeGenConfig(KEY_CODEGEN_SUPPORT_TILE_TENSOR, true); | 237 | config::SetCodeGenConfig(KEY_CODEGEN_SUPPORT_TILE_TENSOR, true); |
| 270 | auto npuArch = Platform::Instance().GetSoc().GetNPUArch(); | 238 | auto npuArch = Platform::Instance().GetSoc().GetNPUArch(); |
| 271 | Platform::Instance().GetSoc().SetNPUArch(NPUArch::DAV_3510); | 239 | Platform::Instance().GetSoc().SetNPUArch(NPUArch::DAV_3510); |
| 272 | std::vector<int64_t> shape = {16, 16}; | 240 | std::vector<int64_t> shape = {16, 16}; |
| 273 | TileShape::Current().SetVecTile({16, 16}); | 241 | TileShape::Current().SetVecTile({16, 16}); |
| 274 | - Tensor input_a(DataType::DT_INT64, shape, "input"); | 242 | + Tensor input_a(inputDataType, shape, "input"); |
| 275 | - auto output = std::make_tuple(Tensor(DataType::DT_INT64, shape, "value"), | 243 | + auto output = std::make_tuple(Tensor(inputDataType, shape, "value"), Tensor(DataType::DT_INT32, shape, "index")); |
| 276 | - Tensor(DataType::DT_INT32, shape, "index")); | ||
| 277 | - std::string funcName = "RadixSelectInt64"; | ||
| 278 | FUNCTION(funcName, {input_a, std::get<0>(output), std::get<1>(output)}) | 244 | FUNCTION(funcName, {input_a, std::get<0>(output), std::get<1>(output)}) |
| 279 | { | 245 | { |
| 280 | LOOP(funcName, FunctionType::DYNAMIC_LOOP, i, LoopRange(1)) | 246 | LOOP(funcName, FunctionType::DYNAMIC_LOOP, i, LoopRange(1)) |
| @@ -286,8 +252,26 @@ TEST_F(TestCodegenDynSort, TestRadixSelectInt64) | |||
| 286 | auto function = Program::GetInstance().GetFunctionByRawName(FUNCTION_PREFIX + funcName + SUB_FUNC_SUFFIX + | 252 | auto function = Program::GetInstance().GetFunctionByRawName(FUNCTION_PREFIX + funcName + SUB_FUNC_SUFFIX + |
| 287 | HIDDEN_FUNC_SUFFIX); | 253 | HIDDEN_FUNC_SUFFIX); |
| 288 | std::string res = GenCodeByFunction(*function); | 254 | std::string res = GenCodeByFunction(*function); |
| 289 | - std::string expect = R"(TRadixSelect<16, 1>(ubTensor_2, ubTensor_3, ubTensor_4, ubTensor_0);)"; | ||
| 290 | CheckStringExist(expect, res); | 255 | CheckStringExist(expect, res); |
| 291 | Platform::Instance().GetSoc().SetNPUArch(npuArch); | 256 | Platform::Instance().GetSoc().SetNPUArch(npuArch); |
| 292 | } | 257 | } |
| 258 | + | ||
| 259 | +TEST_F(TestCodegenDynSort, TestRadixSelectInt16) | ||
| 260 | +{ | ||
| 261 | + const std::string expect = R"(TRadixSelect<16, 1>(ubTensor_2, ubTensor_3, ubTensor_4, ubTensor_0);)"; | ||
| 262 | + TestRadixSelect(DataType::DT_INT16, "RadixSelectINT16", expect); | ||
| 263 | +} | ||
| 264 | + | ||
| 265 | +TEST_F(TestCodegenDynSort, TestRadixSelectFP32) | ||
| 266 | +{ | ||
| 267 | + const std::string expect = R"(TRadixSelect<16, 1>(ubTensor_2, ubTensor_3, ubTensor_4, ubTensor_0);)"; | ||
| 268 | + TestRadixSelect(DataType::DT_FP32, "RadixSelectFP32", expect); | ||
| 269 | +} | ||
| 270 | + | ||
| 271 | +TEST_F(TestCodegenDynSort, TestRadixSelectInt64) | ||
| 272 | +{ | ||
| 273 | + const std::string expect = R"(TRadixSelect<16, 1>(ubTensor_2, ubTensor_3, ubTensor_4, ubTensor_0);)"; | ||
| 274 | + TestRadixSelect(DataType::DT_INT64, "RadixSelectINT64", expect); | ||
| 275 | +} | ||
| 276 | + | ||
| 293 | } // namespace npu::tile_fwk | 277 | } // namespace npu::tile_fwk |