Merged
fix(Conv): Skip split reshape for group conv1d with group_reshape_no_split flag #6348
fix(Conv): Skip split reshape for group conv1d with group_reshape_no_split flag #6348
Merged
xinweiliuAdd on Sep 15
Total: 8 files changed+108-2
@@ -198,6 +198,9 @@ Tensor ConstructTensorGraphNZ2NZ(Function* functionPtr, std::vector<LogicalTenso
198 {finalRes3DimTensor.GetStorage()});198 {finalRes3DimTensor.GetStorage()});
199 TileShape::Current().SetVecTile(savedVecTile);199 TileShape::Current().SetVecTile(savedVecTile);
200 reshapeResOp.SetAttribute(OpAttributeKey::isConv, true);200 reshapeResOp.SetAttribute(OpAttributeKey::isConv, true);
201+ if (convAttrParam.groups > 1) {
202+ reshapeResOp.SetAttribute(OpAttributeKey::groupReshapeNoSplit, true);
203+ }
201 return finalRes3DimTensor;204 return finalRes3DimTensor;
202 }205 }
203 return finalResTensor;206 return finalResTensor;
@@ -239,6 +242,10 @@ Tensor ConstructTensorGraph(const Tensor& inputTensor, const Tensor& weightTenso
239 242 
240 reshapeFmapOp.SetAttribute(OpAttributeKey::isConv, true);243 reshapeFmapOp.SetAttribute(OpAttributeKey::isConv, true);
241 reshapeWeightOp.SetAttribute(OpAttributeKey::isConv, true);244 reshapeWeightOp.SetAttribute(OpAttributeKey::isConv, true);
245+ if (convAttrParam.groups > 1) {
246+ reshapeFmapOp.SetAttribute(OpAttributeKey::groupReshapeNoSplit, true);
247+ reshapeWeightOp.SetAttribute(OpAttributeKey::groupReshapeNoSplit, true);
248+ }
242 operandVecIn = {fmap4DimTensor.GetStorage(), weigth4DimTensor.GetStorage()};249 operandVecIn = {fmap4DimTensor.GetStorage(), weigth4DimTensor.GetStorage()};
243 // conv1d case, squeeze output to NCL250 // conv1d case, squeeze output to NCL
244 std::vector<int64_t> res4DimShape{inputTensor.GetShape()[NCHW_N_IDX], weightTensor.GetShape()[NCHW_N_IDX], 1,251 std::vector<int64_t> res4DimShape{inputTensor.GetShape()[NCHW_N_IDX], weightTensor.GetShape()[NCHW_N_IDX], 1,
@@ -272,6 +279,9 @@ Tensor ConstructTensorGraph(const Tensor& inputTensor, const Tensor& weightTenso
272 auto& reshapeResOp = functionPtr->AddOperation(Opcode::OP_RESHAPE, operandVecOut, {resTensor.GetStorage()});279 auto& reshapeResOp = functionPtr->AddOperation(Opcode::OP_RESHAPE, operandVecOut, {resTensor.GetStorage()});
273 TileShape::Current().SetVecTile(savedVecTile);280 TileShape::Current().SetVecTile(savedVecTile);
274 reshapeResOp.SetAttribute(OpAttributeKey::isConv, true);281 reshapeResOp.SetAttribute(OpAttributeKey::isConv, true);
282+ if (convAttrParam.groups > 1) {
283+ reshapeResOp.SetAttribute(OpAttributeKey::groupReshapeNoSplit, true);
284+ }
275 }285 }
276 return resTensor;286 return resTensor;
277}287}
@@ -139,6 +139,7 @@ const std::string OpAttributeKey::l0cValidMN = "L0C_VALID_MN";
139const std::string OpAttributeKey::rmwMode = "op_attr_rmw_mode";139const std::string OpAttributeKey::rmwMode = "op_attr_rmw_mode";
140const std::string OpAttributeKey::transDataOffset = "TRANSDATA_OFFSET";140const std::string OpAttributeKey::transDataOffset = "TRANSDATA_OFFSET";
141const std::string OpAttributeKey::isConv = "isConv";141const std::string OpAttributeKey::isConv = "isConv";
142+const std::string OpAttributeKey::groupReshapeNoSplit = "group_reshape_no_split";
142const std::string OpAttributeKey::hwk = "ConvBP_HWK";143const std::string OpAttributeKey::hwk = "ConvBP_HWK";
143const std::string OpAttributeKey::kL0Size = "ConvBP_kL0Size";144const std::string OpAttributeKey::kL0Size = "ConvBP_kL0Size";
144const std::string OpAttributeKey::nL0Size = "ConvBP_nL0Size";145const std::string OpAttributeKey::nL0Size = "ConvBP_nL0Size";
@@ -156,6 +156,7 @@ public:
156 static const std::string rmwMode;156 static const std::string rmwMode;
157 static const std::string transDataOffset;157 static const std::string transDataOffset;
158 static const std::string isConv;158 static const std::string isConv;
159+ static const std::string groupReshapeNoSplit;
159 static const std::string hwk;160 static const std::string hwk;
160 static const std::string kL0Size;161 static const std::string kL0Size;
161 static const std::string nL0Size;162 static const std::string nL0Size;
@@ -1802,10 +1802,11 @@ static int64_t CalculateCapacity(const std::vector<int64_t>& shape)
1802}1802}
1803 1803 
1804void TiledInnerReshape(Function& function, const LogicalTensorPtr& operand, const LogicalTensorPtr& result,1804void TiledInnerReshape(Function& function, const LogicalTensorPtr& operand, const LogicalTensorPtr& result,
1805- const bool isInplace = false)1805+ const bool isInplace = false, const bool groupReshapeNoSplit = false)
1806{1806{
1807 auto& op = function.AddOperation("TILE_RESHAPE", {operand}, {result});1807 auto& op = function.AddOperation("TILE_RESHAPE", {operand}, {result});
1808 op.SetAttribute(OP_ATTR_PREFIX + "isInplace", isInplace);1808 op.SetAttribute(OP_ATTR_PREFIX + "isInplace", isInplace);
1809+ op.SetAttribute(OpAttributeKey::groupReshapeNoSplit, groupReshapeNoSplit);
1809 op.SetAttribute(OP_ATTR_PREFIX + "validShape", result->GetDynValidShape());1810 op.SetAttribute(OP_ATTR_PREFIX + "validShape", result->GetDynValidShape());
1810 op.oOperand.front()->SetIsDummy();1811 op.oOperand.front()->SetIsDummy();
1811}1812}
@@ -2209,7 +2210,9 @@ void ExpandOperationInto(Function& function, const TileShape& tileShape, Opcode
2209 case Opcode::OP_RESHAPE: {2210 case Opcode::OP_RESHAPE: {
2210 bool isInplace = false;2211 bool isInplace = false;
2211 op.GetAttr(OP_ATTR_PREFIX + "isInplace", isInplace);2212 op.GetAttr(OP_ATTR_PREFIX + "isInplace", isInplace);
2212- TiledInnerReshape(function, iOperand[0], oOperand[0], isInplace);2213+ bool groupReshapeNoSplit = false;
2214+ op.GetAttr(OpAttributeKey::groupReshapeNoSplit, groupReshapeNoSplit);
2215+ TiledInnerReshape(function, iOperand[0], oOperand[0], isInplace, groupReshapeNoSplit);
2213 break;2216 break;
2214 }2217 }
2215 case Opcode::OP_MAX_POOL: {2218 case Opcode::OP_MAX_POOL: {
@@ -54,6 +54,9 @@ INLINE void TStoreConv2DNZ2NZ(T& dst, U& src, const OffsetInfo& offsetInfo, cons
54 using strideDim = pto::Stride<-1, -1, -1, -1, -1>;54 using strideDim = pto::Stride<-1, -1, -1, -1, -1>;
55 using globalData = pto::GlobalTensor<typename T::Type, shapeDim, strideDim, pto::Layout::NC1HWC0>;55 using globalData = pto::GlobalTensor<typename T::Type, shapeDim, strideDim, pto::Layout::NC1HWC0>;
56 // 分块搬出,每次搬出cutW大小的数据56 // 分块搬出,每次搬出cutW大小的数据
57+ if (realCutW <= 0) {
58+ return;
59+ }
57 for (int64_t loopH = 0; loopH < (realM / realCutW); loopH++) {60 for (int64_t loopH = 0; loopH < (realM / realCutW); loopH++) {
58 globalData dstGlobal((__gm__ typename T::Type*)(dst.GetAddr() + gmOffset),61 globalData dstGlobal((__gm__ typename T::Type*)(dst.GetAddr() + gmOffset),
59 shapeDim(dstShapeC1, dstShapeH, dstShapeW),62 shapeDim(dstShapeC1, dstShapeH, dstShapeW),
@@ -173,6 +173,7 @@ Status SplitReshape::Init()
173{173{
174 assembleOutToInput_.clear();174 assembleOutToInput_.clear();
175 reshapeSources_.clear();175 reshapeSources_.clear();
176+ groupReshapeNoSplitRawMagics_.clear();
176 mapOffset_.clear();177 mapOffset_.clear();
177 mapAssembleOpMagic_.clear();178 mapAssembleOpMagic_.clear();
178 reshapeDynOutput_.clear();179 reshapeDynOutput_.clear();
@@ -456,6 +457,9 @@ Status SplitReshape::CollectReshapeInfo(const Operation& op)
456 if (op.GetAttr(OP_ATTR_VALID_SHAPE, dynOutput)) {457 if (op.GetAttr(OP_ATTR_VALID_SHAPE, dynOutput)) {
457 reshapeDynOutput_[output->GetRawTensor()->GetRawMagic()] = dynOutput;458 reshapeDynOutput_[output->GetRawTensor()->GetRawMagic()] = dynOutput;
458 }459 }
460+ if (op.HasAttr(OpAttributeKey::groupReshapeNoSplit) && op.GetBoolAttribute(OpAttributeKey::groupReshapeNoSplit)) {
461+ groupReshapeNoSplitRawMagics_.insert(output->GetRawTensor()->GetRawMagic());
462+ }
459 reshapeSources_[output->GetRawTensor()->GetRawMagic()] = input;463 reshapeSources_[output->GetRawTensor()->GetRawMagic()] = input;
460 reshapeOpPtrs_[output->GetMagic()] = &op;464 reshapeOpPtrs_[output->GetMagic()] = &op;
461 return SUCCESS;465 return SUCCESS;
@@ -1278,6 +1282,11 @@ Status SplitReshape::CheckReshapeSkip(const LogicalTensorPtr& input, const Logic
1278 APASS_LOG_DEBUG_F(Elements::Tensor, "View op has no preceding reshape op.");1282 APASS_LOG_DEBUG_F(Elements::Tensor, "View op has no preceding reshape op.");
1279 return WARNING;1283 return WARNING;
1280 }1284 }
1285+ if (groupReshapeNoSplitRawMagics_.count(inputRawMagic) > 0) {
1286+ APASS_LOG_DEBUG_F(Elements::Tensor, "Skip splitreshape because reshape op has %s flag.",
1287+ OpAttributeKey::groupReshapeNoSplit.c_str());
1288+ return WARNING;
1289+ }
1281 checkOutputParam.reshapeSource = reshapeSourceIter->second;1290 checkOutputParam.reshapeSource = reshapeSourceIter->second;
1282 int assembleLikeOpMagic = -1;1291 int assembleLikeOpMagic = -1;
1283 if (HasAssembleLikeInputProducedByReduceAcc(checkOutputParam.reshapeSource, assembleLikeOpMagic)) {1292 if (HasAssembleLikeInputProducedByReduceAcc(checkOutputParam.reshapeSource, assembleLikeOpMagic)) {
@@ -212,6 +212,7 @@ private:
212 std::unordered_map<std::pair<int, int>, std::vector<int64_t>, PairHash> mapOffset_;212 std::unordered_map<std::pair<int, int>, std::vector<int64_t>, PairHash> mapOffset_;
213 std::unordered_map<std::pair<int, int>, int, PairHash> mapAssembleOpMagic_;213 std::unordered_map<std::pair<int, int>, int, PairHash> mapAssembleOpMagic_;
214 std::unordered_map<int, LogicalTensorPtr> reshapeSources_;214 std::unordered_map<int, LogicalTensorPtr> reshapeSources_;
215+ std::unordered_set<int> groupReshapeNoSplitRawMagics_;
215 std::unordered_map<int, std::vector<SymbolicScalar>> reshapeDynOutput_;216 std::unordered_map<int, std::vector<SymbolicScalar>> reshapeDynOutput_;
216 std::vector<AssembleOp> assembles_;217 std::vector<AssembleOp> assembles_;
217 std::unordered_map<unsigned long, std::shared_ptr<ReshapeOp>> reshapes_;218 std::unordered_map<unsigned long, std::shared_ptr<ReshapeOp>> reshapes_;
@@ -2823,5 +2823,83 @@ TEST_F(TestSplitReshapePass, TestSplitReshapeLargeScaleRunTimeLimit)
2823 EXPECT_LT(elapsed.count(), maxElapsedMs);2823 EXPECT_LT(elapsed.count(), maxElapsedMs);
2824}2824}
2825 2825 
2826+TEST_F(TestSplitReshapePass, TestSkipSplitReshapeWithGroupReshapeNoSplitFlag)
2827+{
2828+ std::vector<int64_t> origShape = {kNumOne, kExpSix};
2829+ std::vector<int64_t> reshapeShape = {kNumOne, kNumOne, kExpSix};
2830+ std::vector<int64_t> tiledShape1 = {kNumOne, kExpFour};
2831+ std::vector<int64_t> tiledShape2 = {kNumTwo, kNumOne, kNumTen};
2832+ 
2833+ Tensor input(DT_FP32, origShape, "input");
2834+ Tensor output(DT_FP32, reshapeShape, "output");
2835+ 
2836+ FUNCTION("TestSkipSplitReshapeWithGroupNoSplitFlag")
2837+ {
2838+ TileShape::Current().SetVecTile(tiledShape1);
2839+ Tensor exp = Exp(input);
2840+ Tensor reshape = Reshape(exp, reshapeShape);
2841+ TileShape::Current().SetVecTile(tiledShape2);
2842+ output = Exp(reshape);
2843+ }
2844+ 
2845+ Function* func = Program::GetInstance().GetFunctionByRawName("TENSOR_TestSkipSplitReshapeWithGroupNoSplitFlag");
2846+ 
2847+ RunPassStra(*func, PassName::EXPAND_FUNCTION);
2848+ for (auto& op : func->Operations()) {
2849+ if (op.GetOpcode() == Opcode::OP_RESHAPE) {
2850+ op.SetAttribute(OpAttributeKey::groupReshapeNoSplit, true);
2851+ }
2852+ }
2853+ int reshapeCountBefore = CountReshapeOps(func);
2854+ RunPassStra(*func, PassName::SPLIT_RESHAPE);
2855+ EXPECT_EQ(CountReshapeOps(func), reshapeCountBefore);
2856+}
2857+ 
2858+// 标志位在 tensor 图(ExpandFunction 之前)设置,验证展开后标志位不丢失且 SplitReshape 跳过拆分。
2859+TEST_F(TestSplitReshapePass, TestGroupReshapeNoSplitFlagSurviveExpandFunction)
2860+{
2861+ std::vector<int64_t> origShape = {kNumOne, kExpSix};
2862+ std::vector<int64_t> reshapeShape = {kNumOne, kNumOne, kExpSix};
2863+ std::vector<int64_t> tiledShape1 = {kNumOne, kExpFour};
2864+ std::vector<int64_t> tiledShape2 = {kNumTwo, kNumOne, kNumTen};
2865+ 
2866+ Tensor input(DT_FP32, origShape, "input");
2867+ Tensor output(DT_FP32, reshapeShape, "output");
2868+ 
2869+ FUNCTION("TestGroupNoSplitFlagBeforeExpand")
2870+ {
2871+ TileShape::Current().SetVecTile(tiledShape1);
2872+ Tensor exp = Exp(input);
2873+ Tensor reshape = Reshape(exp, reshapeShape);
2874+ TileShape::Current().SetVecTile(tiledShape2);
2875+ output = Exp(reshape);
2876+ }
2877+ 
2878+ Function* func = Program::GetInstance().GetFunctionByRawName("TENSOR_TestGroupNoSplitFlagBeforeExpand");
2879+ 
2880+ for (auto& op : func->Operations()) {
2881+ if (op.GetOpcode() == Opcode::OP_RESHAPE) {
2882+ op.SetAttribute(OpAttributeKey::groupReshapeNoSplit, true);
2883+ }
2884+ }
2885+ int reshapeCountBeforeExpand = CountReshapeOps(func);
2886+ 
2887+ RunPassStra(*func, PassName::EXPAND_FUNCTION);
2888+ 
2889+ int flagCount = 0;
2890+ for (auto& op : func->Operations()) {
2891+ if (op.GetOpcode() == Opcode::OP_RESHAPE) {
2892+ EXPECT_TRUE(op.HasAttr(OpAttributeKey::groupReshapeNoSplit));
2893+ EXPECT_TRUE(op.GetBoolAttribute(OpAttributeKey::groupReshapeNoSplit));
2894+ flagCount++;
2895+ }
2896+ }
2897+ EXPECT_EQ(flagCount, reshapeCountBeforeExpand);
2898+ 
2899+ int reshapeCountBefore = CountReshapeOps(func);
2900+ RunPassStra(*func, PassName::SPLIT_RESHAPE);
2901+ EXPECT_EQ(CountReshapeOps(func), reshapeCountBefore);
2902+}
2903+ 
2826} // namespace tile_fwk2904} // namespace tile_fwk
2827} // namespace npu2905} // namespace npu