Merged
fix(operation): Bound FillPad padding to tile columns #6513
bynshardAdd on Sep 20
fix(operation): Bound FillPad padding to tile columns #6513
Merged
Merged frombynshard/pyptofix-fillpad-9.2.0intocann/pypto9.2.0
Total: 3 files changed+16-9
| @@ -1282,12 +1282,15 @@ std::string CodeGenOpNPU::PrintPadTileTensor() const | |||
| 1282 | std::vector<std::string> tileOpParamList = GetTileOpParamsByOrder(); | 1282 | std::vector<std::string> tileOpParamList = GetTileOpParamsByOrder(); |
| 1283 | std::ostringstream oss; | 1283 | std::ostringstream oss; |
| 1284 | 1284 | ||
| 1285 | + const std::string fillPadCols = originalOp.GetOpcode() == Opcode::OP_FILLPAD ? | ||
| 1286 | + ", " + std::to_string(shape[0].back()) : | ||
| 1287 | + ""; | ||
| 1285 | bool isFloatType = (dstDtype == DT_FP32 || dstDtype == DT_FP16 || dstDtype == DT_BF16); | 1288 | bool isFloatType = (dstDtype == DT_FP32 || dstDtype == DT_FP16 || dstDtype == DT_BF16); |
| 1286 | 1289 | ||
| 1287 | if (isFloatType) { | 1290 | if (isFloatType) { |
| 1288 | auto c = extOperandVal.Cast<float>(); | 1291 | auto c = extOperandVal.Cast<float>(); |
| 1289 | if (std::isinf(c)) { | 1292 | if (std::isinf(c)) { |
| 1290 | - oss << tileOpName << "<" << (c < 0 ? "pto::PadValue::Min" : "pto::PadValue::Max") << ">" | 1293 | + oss << tileOpName << "<" << (c < 0 ? "pto::PadValue::Min" : "pto::PadValue::Max") << fillPadCols << ">" |
| 1291 | << WrapParamByParentheses(tileOpParamList) << STMT_END; | 1294 | << WrapParamByParentheses(tileOpParamList) << STMT_END; |
| 1292 | return oss.str(); | 1295 | return oss.str(); |
| 1293 | } | 1296 | } |
| @@ -1305,8 +1308,8 @@ std::string CodeGenOpNPU::PrintPadTileTensor() const | |||
| 1305 | } | 1308 | } |
| 1306 | 1309 | ||
| 1307 | std::string padValueArg = "(" + std::string(DataType2CCEStr(dstDtype)) + ")" + padValueStr; | 1310 | std::string padValueArg = "(" + std::string(DataType2CCEStr(dstDtype)) + ")" + padValueStr; |
| 1308 | - oss << tileOpName << "<pto::PadValueCustom(" << padValueArg << ")>" << WrapParamByParentheses(tileOpParamList) | 1311 | + oss << tileOpName << "<pto::PadValueCustom(" << padValueArg << ")" << fillPadCols << ">" |
| 1309 | - << STMT_END; | 1312 | + << WrapParamByParentheses(tileOpParamList) << STMT_END; |
| 1310 | return oss.str(); | 1313 | return oss.str(); |
| 1311 | } | 1314 | } |
| 1312 | 1315 | ||
| @@ -19,7 +19,7 @@ | |||
| 19 | #include "utils/layout.h" | 19 | #include "utils/layout.h" |
| 20 | #include "utils/tile_tensor.h" | 20 | #include "utils/tile_tensor.h" |
| 21 | 21 | ||
| 22 | -template <pto::PadValue padValue, typename DstTensor, typename SrcTensor> | 22 | +template <pto::PadValue padValue, size_t dstCols, typename DstTensor, typename SrcTensor> |
| 23 | TILEOP void TFillPad(DstTensor dst, SrcTensor src) | 23 | TILEOP void TFillPad(DstTensor dst, SrcTensor src) |
| 24 | { | 24 | { |
| 25 | constexpr auto dstShapeSize = Std::tuple_size<typename DstTensor::Shape>::value; | 25 | constexpr auto dstShapeSize = Std::tuple_size<typename DstTensor::Shape>::value; |
| @@ -52,6 +52,8 @@ TILEOP void TFillPad(DstTensor dst, SrcTensor src) | |||
| 52 | constexpr auto srcTileH = TileOp::GetTensorTileShapeDim<SrcTensor, DIM_4TH, MAX_DIMS>(); | 52 | constexpr auto srcTileH = TileOp::GetTensorTileShapeDim<SrcTensor, DIM_4TH, MAX_DIMS>(); |
| 53 | constexpr auto srcTileW = TileOp::GetTensorTileShapeDim<SrcTensor, DIM_5TH, MAX_DIMS>(); | 53 | constexpr auto srcTileW = TileOp::GetTensorTileShapeDim<SrcTensor, DIM_5TH, MAX_DIMS>(); |
| 54 | constexpr size_t PAD_TILE_INNER_SIZE = 512; | 54 | constexpr size_t PAD_TILE_INNER_SIZE = 512; |
| 55 | + constexpr size_t blockElements = 32 / sizeof(DstDtype); | ||
| 56 | + constexpr size_t rowTileW = (dstCols + blockElements - 1) / blockElements * blockElements; | ||
| 55 | using DstTileType = pto::Tile<pto::TileType::Vec, DstDtype, dstTileH, dstTileW, pto::BLayout::RowMajor, -1, -1, | 57 | using DstTileType = pto::Tile<pto::TileType::Vec, DstDtype, dstTileH, dstTileW, pto::BLayout::RowMajor, -1, -1, |
| 56 | pto::SLayout::NoneBox, PAD_TILE_INNER_SIZE, padValue>; | 58 | pto::SLayout::NoneBox, PAD_TILE_INNER_SIZE, padValue>; |
| 57 | using SrcTileType = pto::Tile<pto::TileType::Vec, SrcDtype, srcTileH, srcTileW, pto::BLayout::RowMajor, -1, -1>; | 59 | using SrcTileType = pto::Tile<pto::TileType::Vec, SrcDtype, srcTileH, srcTileW, pto::BLayout::RowMajor, -1, -1>; |
| @@ -61,7 +63,7 @@ TILEOP void TFillPad(DstTensor dst, SrcTensor src) | |||
| 61 | for (LoopVar n2Index = 0; n2Index < dstShape2; ++n2Index) { | 63 | for (LoopVar n2Index = 0; n2Index < dstShape2; ++n2Index) { |
| 62 | auto dstOffset = n0Index * dstStride0 + n1Index * dstStride1 + n2Index * dstStride2; | 64 | auto dstOffset = n0Index * dstStride0 + n1Index * dstStride1 + n2Index * dstStride2; |
| 63 | auto srcOffset = n0Index * srcStride0 + n1Index * srcStride1 + n2Index * srcStride2; | 65 | auto srcOffset = n0Index * srcStride0 + n1Index * srcStride1 + n2Index * srcStride2; |
| 64 | - if constexpr (dstTileH >= srcTileH && dstTileW >= srcTileW) { | 66 | + if constexpr (dstTileH >= srcTileH && dstTileW >= srcTileW && dstTileW == rowTileW) { |
| 65 | DstTileType dstTile(dstShape3, dstShape4); | 67 | DstTileType dstTile(dstShape3, dstShape4); |
| 66 | auto dstAddr = dst.GetAddr() + dstOffset * sizeof(DstDtype); | 68 | auto dstAddr = dst.GetAddr() + dstOffset * sizeof(DstDtype); |
| 67 | pto::TASSIGN(dstTile, dstAddr); | 69 | pto::TASSIGN(dstTile, dstAddr); |
| @@ -70,9 +72,11 @@ TILEOP void TFillPad(DstTensor dst, SrcTensor src) | |||
| 70 | pto::TASSIGN(srcTile, srcAddr); | 72 | pto::TASSIGN(srcTile, srcAddr); |
| 71 | pto::TFILLPAD_EXPAND(dstTile, srcTile); | 73 | pto::TFILLPAD_EXPAND(dstTile, srcTile); |
| 72 | } else { | 74 | } else { |
| 73 | - using DstRowTileType = pto::Tile<pto::TileType::Vec, DstDtype, 1, dstTileW, pto::BLayout::RowMajor, | 75 | + // A subview keeps the parent stride, but may only pad its own columns. |
| 76 | + // TFILLPAD_EXPAND pads to static Cols, irrespective of the valid columns. | ||
| 77 | + using DstRowTileType = pto::Tile<pto::TileType::Vec, DstDtype, 1, rowTileW, pto::BLayout::RowMajor, | ||
| 74 | -1, -1, pto::SLayout::NoneBox, PAD_TILE_INNER_SIZE, padValue>; | 78 | -1, -1, pto::SLayout::NoneBox, PAD_TILE_INNER_SIZE, padValue>; |
| 75 | - using SrcRowTileType = pto::Tile<pto::TileType::Vec, SrcDtype, 1, dstTileW, pto::BLayout::RowMajor, | 79 | + using SrcRowTileType = pto::Tile<pto::TileType::Vec, SrcDtype, 1, rowTileW, pto::BLayout::RowMajor, |
| 76 | -1, -1>; | 80 | -1, -1>; |
| 77 | for (LoopVar row = 0; row < dstShape3; ++row) { | 81 | for (LoopVar row = 0; row < dstShape3; ++row) { |
| 78 | auto srcValidRow = row < srcShape3 ? 1 : 0; | 82 | auto srcValidRow = row < srcShape3 ? 1 : 0; |
| @@ -259,7 +259,7 @@ TEST_F(TestCodegenDynUna, TestFillPadDynamicBF16) | |||
| 259 | HIDDEN_FUNC_SUFFIX); | 259 | HIDDEN_FUNC_SUFFIX); |
| 260 | 260 | ||
| 261 | const std::string res = GenCodeByFunction(*function); | 261 | const std::string res = GenCodeByFunction(*function); |
| 262 | - std::string expect = R"!!!(TFillPad<pto::PadValueCustom((bfloat16_t)0.f)>(ubTensor_2, ubTensor_0);)!!!"; | 262 | + std::string expect = R"!!!(TFillPad<pto::PadValueCustom((bfloat16_t)0.f), 20>(ubTensor_2, ubTensor_0);)!!!"; |
| 263 | CheckStringExist(expect, res); | 263 | CheckStringExist(expect, res); |
| 264 | } | 264 | } |
| 265 | 265 | ||
| @@ -290,7 +290,7 @@ TEST_F(TestCodegenDynUna, TestFillPadDynamic) | |||
| 290 | HIDDEN_FUNC_SUFFIX); | 290 | HIDDEN_FUNC_SUFFIX); |
| 291 | 291 | ||
| 292 | const std::string res = GenCodeByFunction(*function); | 292 | const std::string res = GenCodeByFunction(*function); |
| 293 | - std::string expect = R"!!!(TFillPad<pto::PadValueCustom((float)0.f)>(ubTensor_2, ubTensor_0);)!!!"; | 293 | + std::string expect = R"!!!(TFillPad<pto::PadValueCustom((float)0.f), 20>(ubTensor_2, ubTensor_0);)!!!"; |
| 294 | CheckStringExist(expect, res); | 294 | CheckStringExist(expect, res); |
| 295 | } | 295 | } |
| 296 | 296 | ||