Merged
feat(codegen): Support inline multi-axis broadcast for binary ops in for-axis hoisting #3335
feat(codegen): Support inline multi-axis broadcast for binary ops in for-axis hoisting #3335
Merged
HujunAdd on May 18
Total: 4 files changed+42-21
@@ -167,6 +167,12 @@ const std::map<MemoryType, OperandType> OPERAND_TYPE_TO_MEMORY_TYPE{
167 {MemoryType::MEM_L0BMX, BUF_L0BMX},167 {MemoryType::MEM_L0BMX, BUF_L0BMX},
168};168};
169 169 
170+enum class BroadcastOperand : int64_t {
171+ NONE = 0,
172+ LEFT_OPERAND = 1,
173+ RIGHT_OPERAND = 2,
174+};
175+ 
170} // namespace npu::tile_fwk176} // namespace npu::tile_fwk
171 177 
172#endif // CODEGEN_COMMON_H178#endif // CODEGEN_COMMON_H
@@ -209,12 +209,8 @@ std::string CodeGenOpNPU::PrintBinaryTileTensor() const
209 templateParamList.emplace_back(lastUse);209 templateParamList.emplace_back(lastUse);
210 }210 }
211 if (needBrcinline) {211 if (needBrcinline) {
212- if (brcOperand.size() < SHAPE_DIM5) {212+ FillVecWithDummyInHead<int64_t>(brcOperand, MAX_DIM - brcOperand.size(), 0);
213- brcOperand.insert(brcOperand.begin(), SHAPE_DIM5 - brcOperand.size(), 0);213+ FillParamWithFullInput(templateParamList, brcOperand);
214- }
215- for (auto operandIdx : brcOperand) {
216- templateParamList.emplace_back(std::to_string(operandIdx));
217- }
218 }214 }
219 215 
220 std::ostringstream oss;216 std::ostringstream oss;
@@ -343,12 +339,8 @@ std::string CodeGenOpNPU::GenAxpyOp() const
343 << ") diverged — upstream pass must keep them synchronized";339 << ") diverged — upstream pass must keep them synchronized";
344 }340 }
345 if (needBrcinline) {341 if (needBrcinline) {
346- if (brcOperand.size() < SHAPE_DIM5) {342+ FillVecWithDummyInHead<int64_t>(brcOperand, MAX_DIM - brcOperand.size(), 0);
347- brcOperand.insert(brcOperand.begin(), SHAPE_DIM5 - brcOperand.size(), 0);343+ FillParamWithFullInput(templateParamList, brcOperand);
348- }
349- for (auto operandIdx : brcOperand) {
350- templateParamList.emplace_back(std::to_string(operandIdx));
351- }
352 }344 }
353 345 
354 std::string dtypeStr = DataType2CCEStr(extOperandVal.GetDataType());346 std::string dtypeStr = DataType2CCEStr(extOperandVal.GetDataType());
@@ -20,12 +20,6 @@
20#include <vector>20#include <vector>
21 21 
22namespace npu::tile_fwk {22namespace npu::tile_fwk {
23-enum class BroadcastOperand : int64_t {
24- NONE = 0,
25- LEFT_OPERAND = 1,
26- RIGHT_OPERAND = 2,
27-};
28- 
29struct PrintScatterElemParam {23struct PrintScatterElemParam {
30 const int axis;24 const int axis;
31 const int scatterMode;25 const int scatterMode;
@@ -21,6 +21,7 @@
21 21 
22namespace npu::tile_fwk {22namespace npu::tile_fwk {
23const std::string DEFAULT_TENSOR_OFFSET = "tileOffsets";23const std::string DEFAULT_TENSOR_OFFSET = "tileOffsets";
24+constexpr const int BINARY_OP_INPUT_CNT = 2;
24 25 
25std::string ForNode::Print() const26std::string ForNode::Print() const
26{27{
@@ -195,14 +196,40 @@ bool IsExpandOrBrcAxes(const std::vector<int64_t>& rawShape, const std::vector<i
195 return rawShape[axis] == 1 || std::find(axes.begin(), axes.end(), axis) != axes.end();196 return rawShape[axis] == 1 || std::find(axes.begin(), axes.end(), axis) != axes.end();
196}197}
197 198 
198-std::vector<int> GetNormalizedExpandAxes(unsigned originalDimSize, const Operation& oper)199+std::vector<int> GetNormalizedExpandAxes(unsigned originalDimSize, int tensorMagic, const Operation& oper)
199{200{
200 std::vector<int> normalizedExpandAxes;201 std::vector<int> normalizedExpandAxes;
201 if (oper.GetOpcode() == Opcode::OP_EXPAND) {202 if (oper.GetOpcode() == Opcode::OP_EXPAND) {
202 auto axes = oper.GetVectorIntAttribute(OpAttributeKey::expandDims);203 auto axes = oper.GetVectorIntAttribute(OpAttributeKey::expandDims);
203 CODEGEN_LOGI("expandDims is : %s", IntVecToStr(axes).c_str());204 CODEGEN_LOGI("expandDims is : %s", IntVecToStr(axes).c_str());
204 normalizedExpandAxes = NormalizeExpandAxes(axes, originalDimSize, SHAPE_DIM5);205 normalizedExpandAxes = NormalizeExpandAxes(axes, originalDimSize, SHAPE_DIM5);
206+ return normalizedExpandAxes;
205 }207 }
208+ 
209+ auto brcOperand = oper.GetVectorIntAttribute(OpAttributeKey::brcOperand);
210+ CODEGEN_LOGI("brcOperand is : %s", IntVecToStr(brcOperand).c_str());
211+ if (brcOperand.empty()) {
212+ return normalizedExpandAxes;
213+ }
214+ 
215+ // brcOperand [0, 1, 1, 2, 2] means:
216+ // axis 0: brcOperand value is 0, means BroadcastOperand::NONE, do not need broadcast,
217+ // axis 1, 2: brcOperand value is 1, means BroadcastOperand::LEFT_OPERAND, left operand need broadcast,
218+ // axis 3, 4: brcOperand value is 2, means BroadcastOperand::RIGHT_OPERAND, right operand need broadcast
219+ FillVecWithDummyInHead<int64_t>(brcOperand, MAX_DIM - brcOperand.size(), 0);
220+ 
221+ const auto& inputs = oper.GetIOperands();
222+ ASSERT(OperErr::OPERAND_COUNT_NOT_MATCHED, inputs.size() >= BINARY_OP_INPUT_CNT)
223+ << "GetIOperands size is " << inputs.size() << ", op is " << oper.Dump();
224+ 
225+ for (size_t i = 0; i < brcOperand.size(); ++i) {
226+ if ((tensorMagic == inputs[0]->GetMagic() && brcOperand[i] == ToUnderlying(BroadcastOperand::LEFT_OPERAND)) ||
227+ (tensorMagic == inputs[1]->GetMagic() && brcOperand[i] == ToUnderlying(BroadcastOperand::RIGHT_OPERAND))) {
228+ // if current axis need broadcast, record axis in normalizedExpandAxes
229+ normalizedExpandAxes.push_back(i);
230+ }
231+ }
232+ 
206 return normalizedExpandAxes;233 return normalizedExpandAxes;
207}234}
208 235 
@@ -216,8 +243,10 @@ OffsetInLoop ForBlockManager::BuildOffsetInLoop(int tensorMagic, const Operation
216 auto rawShape = tileTensor->rawShape;243 auto rawShape = tileTensor->rawShape;
217 FillVecWithDummyInHead<int64_t>(rawShape, SHAPE_DIM5 - rawShape.size(), 1);244 FillVecWithDummyInHead<int64_t>(rawShape, SHAPE_DIM5 - rawShape.size(), 1);
218 245 
219- // GetNormalizedExpandAxes need originalDimSize to normalize axes246+ // normalize axes up to MAX_DIM(e.g. SHAPE_DIM5)
220- std::vector<int> normalizedExpandAxes = GetNormalizedExpandAxes(tileTensor->rawShape.size(), oper);247+ // e.g. expand [2, 1, 1, 6] to [2, 8, 4, 6] axes is [1, 2], after normalizedExpandAxes is [2, 3].
248+ // Hoisted loop axes is [0, 1, 2], so axis ‘2’ is in normalizedExpandAxes and newOffset[2] should be set to zero.
249+ std::vector<int> normalizedExpandAxes = GetNormalizedExpandAxes(tileTensor->rawShape.size(), tensorMagic, oper);
221 250 
222 CODEGEN_LOGI(251 CODEGEN_LOGI(
223 "rawShape is : %s, normalizedExpandAxes is : %s", IntVecToStr(rawShape).c_str(),252 "rawShape is : %s, normalizedExpandAxes is : %s", IntVecToStr(rawShape).c_str(),