Merged
fix(function): Fix copy attr of inner copyin/out not normalized #6587
fix(function): Fix copy attr of inner copyin/out not normalized #6587
Merged
赵俊涵Add on Sep 21
Total: 3 files changed+84-114
@@ -3184,40 +3184,15 @@ static void MaybeNormalizeValue(std::vector<SymbolicScalar>& valueCoa, SymbolicS
3184 }3184 }
3185}3185}
3186 3186 
3187-static void NormalizeReshapeCopyDynValidShape(Operation* op, std::vector<std::vector<SymbolicScalar>>& coaLists,3187+static std::vector<SymbolicScalar> MaybeNormalizeValue(std::vector<OpImmediate>& opImmList, int coaIndex,
3188- int& coaIndex, bool valueToIndex)3188+ bool valueToIndex)
3189{3189{
3190- Opcode opcode = op->GetOpcode();3190+ std::vector<SymbolicScalar> coaList;
3191- if (opcode != Opcode::OP_RESHAPE_COPY_OUT && opcode != Opcode::OP_RESHAPE_COPY_IN &&3191+ for (auto valueCoa : OpImmediate::ToSpecified(opImmList)) {
3192- opcode != Opcode::OP_L0C_RESHAPE_COPY_OUT && opcode != Opcode::OP_L1_RESHAPE_COPY_IN) {3192+ MaybeNormalizeValue(coaList, valueCoa, coaIndex, valueToIndex);
3193- return;3193+ coaIndex += 1;
3194 }3194 }
3195- auto copyAttr = std::static_pointer_cast<CopyOpAttribute>(op->GetOpAttribute());3195+ return coaList;
3196- FE_ASSERT(FeError::INVALID_PTR, copyAttr != nullptr)
3197- << "Normalize reshape copy dyn valid shape failed: copyAttr is null.\n"
3198- << "Operation: " << op->Dump();
3199- 
3200- bool useToDynValidShape = op->GetOpcode() == Opcode::OP_RESHAPE_COPY_OUT ||
3201- op->GetOpcode() == Opcode::OP_L0C_RESHAPE_COPY_OUT;
3202- const char* dynValidShapeName = useToDynValidShape ? "toDynValidShape" : "fromDynValidShape";
3203- auto opImmList = useToDynValidShape ? copyAttr->GetToDynValidShape() : copyAttr->GetFromDynValidShape();
3204- FE_ASSERT(FeError::INVALID_PTR, !opImmList.empty())
3205- << "Normalize reshape copy dyn valid shape failed: " << dynValidShapeName << " is empty.\n"
3206- << "Operation: " << op->Dump();
3207- 
3208- std::vector<SymbolicScalar> dynValidShape = OpImmediate::ToSpecified(opImmList);
3209- std::vector<SymbolicScalar> valueCoaList;
3210- for (auto& value : dynValidShape) {
3211- MaybeNormalizeValue(valueCoaList, value, coaIndex + static_cast<int>(valueCoaList.size()), valueToIndex);
3212- }
3213- 
3214- if (useToDynValidShape) {
3215- copyAttr->SetToDynValidShape(OpImmediate::Specified(dynValidShape));
3216- } else {
3217- copyAttr->SetFromDynValidShape(OpImmediate::Specified(dynValidShape));
3218- }
3219- coaLists.emplace_back(valueCoaList);
3220- coaIndex += valueCoaList.size();
3221}3196}
3222 3197 
3223static std::vector<SymbolicScalar> NormalizeCopyIn(Operation* op, int coaIndexBase, bool valueToIndex)3198static std::vector<SymbolicScalar> NormalizeCopyIn(Operation* op, int coaIndexBase, bool valueToIndex)
@@ -3240,7 +3215,6 @@ static std::vector<SymbolicScalar> NormalizeCopyIn(Operation* op, int coaIndexBa
3240 // shape to normal3215 // shape to normal
3241 opImmList = copyAttr->GetShape();3216 opImmList = copyAttr->GetShape();
3242 OpImmediate::NormalizeValue(operandCoaList, operandCoaIndex, opImmList, coaIndex, valueToIndex);3217 OpImmediate::NormalizeValue(operandCoaList, operandCoaIndex, opImmList, coaIndex, valueToIndex);
3243- copyAttr->SetShape(opImmList);
3244 operandCoaIndex += dim;3218 operandCoaIndex += dim;
3245 coaIndex += dim;3219 coaIndex += dim;
3246 3220 
@@ -3253,11 +3227,8 @@ static std::vector<SymbolicScalar> NormalizeCopyIn(Operation* op, int coaIndexBa
3253 3227 
3254 opImmList = copyAttr->GetToDynValidShape();3228 opImmList = copyAttr->GetToDynValidShape();
3255 if (op->GetOpcode() == Opcode::OP_L1_COPY_IN_CONV) {3229 if (op->GetOpcode() == Opcode::OP_L1_COPY_IN_CONV) {
3256- std::vector<SymbolicScalar> valueCoaList;3230+ auto valueCoaList = MaybeNormalizeValue(opImmList, coaIndex, valueToIndex);
3257- for (auto validshape : OpImmediate::ToSpecified(opImmList)) {3231+ coaIndex += valueCoaList.size();
3258- MaybeNormalizeValue(valueCoaList, validshape, coaIndex, valueToIndex);
3259- coaIndex += 1;
3260- }
3261 operandCoaList.erase(operandCoaList.end() - valueCoaList.size(), operandCoaList.end());3232 operandCoaList.erase(operandCoaList.end() - valueCoaList.size(), operandCoaList.end());
3262 operandCoaList.insert(operandCoaList.end(), valueCoaList.begin(), valueCoaList.end());3233 operandCoaList.insert(operandCoaList.end(), valueCoaList.begin(), valueCoaList.end());
3263 } else {3234 } else {
@@ -3289,7 +3260,6 @@ static std::vector<SymbolicScalar> NormalizeCopyOut(Operation* op, int coaIndexB
3289 // shape to normals3260 // shape to normals
3290 opImmList = copyAttr->GetShape();3261 opImmList = copyAttr->GetShape();
3291 OpImmediate::NormalizeValue(operandCoaList, operandCoaIndex, opImmList, coaIndex, valueToIndex);3262 OpImmediate::NormalizeValue(operandCoaList, operandCoaIndex, opImmList, coaIndex, valueToIndex);
3292- copyAttr->SetShape(opImmList);
3293 operandCoaIndex += dim;3263 operandCoaIndex += dim;
3294 coaIndex += dim;3264 coaIndex += dim;
3295 3265 
@@ -3302,11 +3272,8 @@ static std::vector<SymbolicScalar> NormalizeCopyOut(Operation* op, int coaIndexB
3302 3272 
3303 opImmList = copyAttr->GetFromDynValidShape();3273 opImmList = copyAttr->GetFromDynValidShape();
3304 if (op->GetOpcode() == Opcode::OP_L0C_COPY_OUT_CONV) {3274 if (op->GetOpcode() == Opcode::OP_L0C_COPY_OUT_CONV) {
3305- std::vector<SymbolicScalar> valueCoaList;3275+ auto valueCoaList = MaybeNormalizeValue(opImmList, coaIndex, valueToIndex);
3306- for (auto validshape : OpImmediate::ToSpecified(opImmList)) {3276+ coaIndex += valueCoaList.size();
3307- MaybeNormalizeValue(valueCoaList, validshape, coaIndex, valueToIndex);
3308- coaIndex += 1;
3309- }
3310 operandCoaList.erase(operandCoaList.end() - valueCoaList.size(), operandCoaList.end());3277 operandCoaList.erase(operandCoaList.end() - valueCoaList.size(), operandCoaList.end());
3311 operandCoaList.insert(operandCoaList.end(), valueCoaList.begin(), valueCoaList.end());3278 operandCoaList.insert(operandCoaList.end(), valueCoaList.begin(), valueCoaList.end());
3312 } else {3279 } else {
@@ -3394,8 +3361,7 @@ std::vector<std::vector<SymbolicScalar>> Function::NormalizeCoa(std::vector<Oper
3394 std::vector<OperandAttribute>& oOpAttr)3361 std::vector<OperandAttribute>& oOpAttr)
3395{3362{
3396 std::unordered_map<int, Operation*> opmagicToOp;3363 std::unordered_map<int, Operation*> opmagicToOp;
3397- std::unordered_map<LogicalTensorPtr, int> processedOperands;3364+ std::unordered_map<LogicalTensorPtr, int> normTensors;
3398- std::unordered_set<Operation*> normalizedOps;
3399 3365 
3400 opmagicToOp.reserve(operations_.size());3366 opmagicToOp.reserve(operations_.size());
3401 for (auto& op : operations_) {3367 for (auto& op : operations_) {
@@ -3405,9 +3371,9 @@ std::vector<std::vector<SymbolicScalar>> Function::NormalizeCoa(std::vector<Oper
3405 int coaIndex = COA_INDEX_BASE;3371 int coaIndex = COA_INDEX_BASE;
3406 std::vector<std::vector<SymbolicScalar>> coaLists;3372 std::vector<std::vector<SymbolicScalar>> coaLists;
3407 coaLists.reserve(incastPosition.size() + outcastPosition.size());3373 coaLists.reserve(incastPosition.size() + outcastPosition.size());
3408- NormalizeCoaForInCasts(iOpAttr, coaLists, coaIndex, processedOperands, normalizedOps, opmagicToOp);3374+ NormalizeCoaForInCasts(iOpAttr, coaLists, coaIndex, normTensors, opmagicToOp);
3409- NormalizeCoaForOutCasts(oOpAttr, coaLists, coaIndex, processedOperands, normalizedOps, opmagicToOp);3375+ NormalizeCoaForOutCasts(oOpAttr, coaLists, coaIndex, normTensors, opmagicToOp);
3410- NormalizeCoaForNormalOperands(coaLists, coaIndex, processedOperands, normalizedOps);3376+ NormalizeCoaForNormalOperands(coaLists, coaIndex, normTensors);
3411 NormalizeCoaForSpecialInfo(coaLists, coaIndex);3377 NormalizeCoaForSpecialInfo(coaLists, coaIndex);
3412 3378 
3413 return coaLists;3379 return coaLists;
@@ -3451,8 +3417,7 @@ static bool IsOutcastCopyOutChain(Operation* op, std::vector<Operation*>& copyOu
3451 3417 
3452void Function::NormalizeCoaForInCasts(std::vector<OperandAttribute>& iOpAttr,3418void Function::NormalizeCoaForInCasts(std::vector<OperandAttribute>& iOpAttr,
3453 std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,3419 std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,
3454- std::unordered_map<LogicalTensorPtr, int>& processedOperands,3420+ std::unordered_map<LogicalTensorPtr, int>& normTensors,
3455- std::unordered_set<Operation*>& normalizedOps,
3456 const std::unordered_map<int, Operation*>& opmagicToOp)3421 const std::unordered_map<int, Operation*>& opmagicToOp)
3457{3422{
3458 bool valueToIndex = parent_->GetFunctionType() == FunctionType::DYNAMIC_LOOP_PATH;3423 bool valueToIndex = parent_->GetFunctionType() == FunctionType::DYNAMIC_LOOP_PATH;
@@ -3460,7 +3425,6 @@ void Function::NormalizeCoaForInCasts(std::vector<OperandAttribute>& iOpAttr,
3460 for (auto [opmagic, k] : incastPosition) {3425 for (auto [opmagic, k] : incastPosition) {
3461 auto op = opmagicToOp.at(opmagic);3426 auto op = opmagicToOp.at(opmagic);
3462 if (op->GetIOpAttrOffset(k) != -1) {3427 if (op->GetIOpAttrOffset(k) != -1) {
3463- normalizedOps.insert(op);
3464 continue;3428 continue;
3465 }3429 }
3466 std::vector<SymbolicScalar> operandCoaList;3430 std::vector<SymbolicScalar> operandCoaList;
@@ -3478,7 +3442,6 @@ void Function::NormalizeCoaForInCasts(std::vector<OperandAttribute>& iOpAttr,
3478 for (auto copyIn : copyInList) {3442 for (auto copyIn : copyInList) {
3479 operandCoaList = NormalizeCopyIn(copyIn, coaIndex, valueToIndex);3443 operandCoaList = NormalizeCopyIn(copyIn, coaIndex, valueToIndex);
3480 copyIn->SetIOpAtt(isReshape ? 0 : k, coaIndex);3444 copyIn->SetIOpAtt(isReshape ? 0 : k, coaIndex);
3481- normalizedOps.insert(copyIn);
3482 if (!isReshape) {3445 if (!isReshape) {
3483 iOpAttr.emplace_back(coaIndex);3446 iOpAttr.emplace_back(coaIndex);
3484 }3447 }
@@ -3492,23 +3455,20 @@ void Function::NormalizeCoaForInCasts(std::vector<OperandAttribute>& iOpAttr,
3492 coaIndex += operandCoaList.size();3455 coaIndex += operandCoaList.size();
3493 coaLists.emplace_back(std::move(operandCoaList));3456 coaLists.emplace_back(std::move(operandCoaList));
3494 }3457 }
3495- normalizedOps.insert(op);
3496 continue;3458 continue;
3497 }3459 }
3498 auto iOperand = op->GetInputOperand(k);3460 auto iOperand = op->GetInputOperand(k);
3499- auto it = processedOperands.find(iOperand);3461+ auto it = normTensors.find(iOperand);
3500- if (it != processedOperands.end()) {3462+ if (it != normTensors.end()) {
3501 op->SetIOpAtt(k, it->second);3463 op->SetIOpAtt(k, it->second);
3502 iOpAttr.emplace_back(it->second);3464 iOpAttr.emplace_back(it->second);
3503- normalizedOps.insert(op);
3504 continue;3465 continue;
3505 }3466 }
3506 operandCoaList = NormalizeTensor(iOperand, coaIndex, false, op->GetOpcode() == Opcode::OP_NOP);3467 operandCoaList = NormalizeTensor(iOperand, coaIndex, false, op->GetOpcode() == Opcode::OP_NOP);
3507- processedOperands.emplace(iOperand, coaIndex);3468+ normTensors.emplace(iOperand, coaIndex);
3508 }3469 }
3509 op->SetIOpAtt(k, coaIndex);3470 op->SetIOpAtt(k, coaIndex);
3510 iOpAttr.emplace_back(coaIndex);3471 iOpAttr.emplace_back(coaIndex);
3511- normalizedOps.insert(op);
3512 coaIndex += operandCoaList.size();3472 coaIndex += operandCoaList.size();
3513 coaLists.emplace_back(std::move(operandCoaList));3473 coaLists.emplace_back(std::move(operandCoaList));
3514 }3474 }
@@ -3516,8 +3476,7 @@ void Function::NormalizeCoaForInCasts(std::vector<OperandAttribute>& iOpAttr,
3516 3476 
3517void Function::NormalizeCoaForOutCasts(std::vector<OperandAttribute>& oOpAttr,3477void Function::NormalizeCoaForOutCasts(std::vector<OperandAttribute>& oOpAttr,
3518 std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,3478 std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,
3519- std::unordered_map<LogicalTensorPtr, int>& processedOperands,3479+ std::unordered_map<LogicalTensorPtr, int>& normTensors,
3520- std::unordered_set<Operation*>& normalizedOps,
3521 const std::unordered_map<int, Operation*>& opmagicToOp)3480 const std::unordered_map<int, Operation*>& opmagicToOp)
3522{3481{
3523 bool valueToIndex = parent_->GetFunctionType() == FunctionType::DYNAMIC_LOOP_PATH;3482 bool valueToIndex = parent_->GetFunctionType() == FunctionType::DYNAMIC_LOOP_PATH;
@@ -3525,7 +3484,6 @@ void Function::NormalizeCoaForOutCasts(std::vector<OperandAttribute>& oOpAttr,
3525 for (auto [opmagic, k] : outcastPosition) {3484 for (auto [opmagic, k] : outcastPosition) {
3526 auto op = opmagicToOp.at(opmagic);3485 auto op = opmagicToOp.at(opmagic);
3527 if (op->GetOOpAttrOffset(k) != -1) {3486 if (op->GetOOpAttrOffset(k) != -1) {
3528- normalizedOps.insert(op);
3529 continue;3487 continue;
3530 }3488 }
3531 bool isAtomic = op->GetOOperands()[0]->HasAttr(OpAttributeKey::writeConflict);3489 bool isAtomic = op->GetOOperands()[0]->HasAttr(OpAttributeKey::writeConflict);
@@ -3535,18 +3493,13 @@ void Function::NormalizeCoaForOutCasts(std::vector<OperandAttribute>& oOpAttr,
3535 operandCoaList = NormalizeCopyOut(op, coaIndex, valueToIndex);3493 operandCoaList = NormalizeCopyOut(op, coaIndex, valueToIndex);
3536 } else if (op->GetIOperands().size() > 0 && !this->IsFromInCast(op->GetIOperands().front()) &&3494 } else if (op->GetIOperands().size() > 0 && !this->IsFromInCast(op->GetIOperands().front()) &&
3537 IsOutcastCopyOutChain(op, copyOutList)) {3495 IsOutcastCopyOutChain(op, copyOutList)) {
3538- // 出口链 COPY_OUT→RESHAPE:参数化链首 COPY_OUT 的 attr(镜像入口侧 COPY_IN→RESHAPE
3539- // 的处理),RESHAPE 出口 tensor 只记录值不回写。折叠在 COPY_OUT attr 里的字面量由此
3540- // 进入参数表而非 hash。
3541 bool isReshape = op->GetOpcode() == Opcode::OP_RESHAPE;3496 bool isReshape = op->GetOpcode() == Opcode::OP_RESHAPE;
3542 for (auto copyOut : copyOutList) {3497 for (auto copyOut : copyOutList) {
3543 if (copyOut->GetOOpAttrOffset(0) != -1) {3498 if (copyOut->GetOOpAttrOffset(0) != -1) {
3544- normalizedOps.insert(copyOut);
3545 continue;3499 continue;
3546 }3500 }
3547 operandCoaList = NormalizeCopyOut(copyOut, coaIndex, valueToIndex);3501 operandCoaList = NormalizeCopyOut(copyOut, coaIndex, valueToIndex);
3548 copyOut->SetOOpAtt(isReshape ? 0 : k, coaIndex, false);3502 copyOut->SetOOpAtt(isReshape ? 0 : k, coaIndex, false);
3549- normalizedOps.insert(copyOut);
3550 if (!isReshape) {3503 if (!isReshape) {
3551 oOpAttr.emplace_back(coaIndex, isAtomic);3504 oOpAttr.emplace_back(coaIndex, isAtomic);
3552 }3505 }
@@ -3560,31 +3513,27 @@ void Function::NormalizeCoaForOutCasts(std::vector<OperandAttribute>& oOpAttr,
3560 coaIndex += operandCoaList.size();3513 coaIndex += operandCoaList.size();
3561 coaLists.emplace_back(std::move(operandCoaList));3514 coaLists.emplace_back(std::move(operandCoaList));
3562 }3515 }
3563- normalizedOps.insert(op);
3564 continue;3516 continue;
3565 } else {3517 } else {
3566 auto oOperand = op->GetOutputOperand(k);3518 auto oOperand = op->GetOutputOperand(k);
3567- auto it = processedOperands.find(oOperand);3519+ auto it = normTensors.find(oOperand);
3568- if (it != processedOperands.end()) {3520+ if (it != normTensors.end()) {
3569 op->SetOOpAtt(k, it->second, isAtomic);3521 op->SetOOpAtt(k, it->second, isAtomic);
3570 oOpAttr.emplace_back(it->second, isAtomic);3522 oOpAttr.emplace_back(it->second, isAtomic);
3571- normalizedOps.insert(op);
3572 continue;3523 continue;
3573 }3524 }
3574 operandCoaList = NormalizeTensor(oOperand, coaIndex, false);3525 operandCoaList = NormalizeTensor(oOperand, coaIndex, false);
3575- processedOperands.emplace(oOperand, coaIndex);3526+ normTensors.emplace(oOperand, coaIndex);
3576 }3527 }
3577 op->SetOOpAtt(k, coaIndex, isAtomic);3528 op->SetOOpAtt(k, coaIndex, isAtomic);
3578 oOpAttr.emplace_back(coaIndex, isAtomic);3529 oOpAttr.emplace_back(coaIndex, isAtomic);
3579- normalizedOps.insert(op);
3580 coaIndex += operandCoaList.size();3530 coaIndex += operandCoaList.size();
3581 coaLists.emplace_back(std::move(operandCoaList));3531 coaLists.emplace_back(std::move(operandCoaList));
3582 }3532 }
3583}3533}
3584 3534 
3585void Function::NormalizeCoaForNormalOperands(std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,3535void Function::NormalizeCoaForNormalOperands(std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,
3586- std::unordered_map<LogicalTensorPtr, int>& processedOperands,3536+ std::unordered_map<LogicalTensorPtr, int>& normTensors)
3587- const std::unordered_set<Operation*>& normalizedOps)
3588{3537{
3589 std::unordered_set<LogicalTensorPtr> inOutCasts;3538 std::unordered_set<LogicalTensorPtr> inOutCasts;
3590 inOutCasts.insert(inCasts_.begin(), inCasts_.end());3539 inOutCasts.insert(inCasts_.begin(), inCasts_.end());
@@ -3595,30 +3544,31 @@ void Function::NormalizeCoaForNormalOperands(std::vector<std::vector<SymbolicSca
3595 outcastRawMagics.insert(outcast->GetRawMagic());3544 outcastRawMagics.insert(outcast->GetRawMagic());
3596 }3545 }
3597 }3546 }
3547+ 
3598 bool valueToIndex = parent_->GetFunctionType() == FunctionType::DYNAMIC_LOOP_PATH;3548 bool valueToIndex = parent_->GetFunctionType() == FunctionType::DYNAMIC_LOOP_PATH;
3599 for (auto& op : operations_) {3549 for (auto& op : operations_) {
3600 if (op->GetOpcode() == Opcode::OP_NOP) {3550 if (op->GetOpcode() == Opcode::OP_NOP) {
3601 continue;3551 continue;
3602 }3552 }
3603- // In/outcast 规范化只覆盖已登记 operand。多输出 op(如 INDEX_ADD 的 tmp)仍要处理剩余操作数,3553+ if ((IsCopyIn(op->GetOpcode()) || OpcodeManager::Inst().IsCopyFromL1(op->GetOpcode())) &&
3604- // 不能因 op 已在 normalizedOps 中就整算子跳过。3554+ !op->GetIOperands().empty() && op->GetIOpAttrOffset(0) == -1 && op->GetOpAttribute() != nullptr) {
3605- if (normalizedOps.count(op.get()) == 0) {3555+ auto operandCoaList = NormalizeCopyIn(op.get(), coaIndex, valueToIndex);
3606- if (IsCopyIn(op->GetOpcode()) && !op->GetIOperands().empty() &&3556+ // Inner copy attr still goes into coaLists; only outcast-raw copies may occupy IOpAtt,
3607- outcastRawMagics.count(op->GetInputOperand(0)->GetRawMagic()) > 0) {3557+ // which InferParamIndex treats as tensor validshape COA.
3608- auto operandCoaList = NormalizeCopyIn(op.get(), coaIndex, valueToIndex);3558+ if (outcastRawMagics.count(op->GetInputOperand(0)->GetRawMagic()) > 0) {
3609 op->SetIOpAtt(0, coaIndex);3559 op->SetIOpAtt(0, coaIndex);
3610- coaIndex += operandCoaList.size();
3611- coaLists.emplace_back(std::move(operandCoaList));
3612- continue;
3613 }3560 }
3614- if (IsCopyOut(op->GetOpcode()) && !op->GetOOperands().empty() &&3561+ coaIndex += operandCoaList.size();
3615- outcastRawMagics.count(op->GetOutputOperand(0)->GetRawMagic()) > 0) {3562+ coaLists.emplace_back(std::move(operandCoaList));
3616- auto operandCoaList = NormalizeCopyOut(op.get(), coaIndex, valueToIndex);3563+ }
3564+ if (IsCopyOut(op->GetOpcode()) && !op->GetOOperands().empty() && op->GetOOpAttrOffset(0) == -1 &&
3565+ op->GetOpAttribute() != nullptr) {
3566+ auto operandCoaList = NormalizeCopyOut(op.get(), coaIndex, valueToIndex);
3567+ if (outcastRawMagics.count(op->GetOutputOperand(0)->GetRawMagic()) > 0) {
3617 op->SetOOpAtt(0, coaIndex, false);3568 op->SetOOpAtt(0, coaIndex, false);
3618- coaIndex += operandCoaList.size();
3619- coaLists.emplace_back(std::move(operandCoaList));
3620- continue;
3621 }3569 }
3570+ coaIndex += operandCoaList.size();
3571+ coaLists.emplace_back(std::move(operandCoaList));
3622 }3572 }
3623 for (size_t i = 0; i < op->GetInputOperandSize(); i++) {3573 for (size_t i = 0; i < op->GetInputOperandSize(); i++) {
3624 auto iOperand = op->GetInputOperand(i);3574 auto iOperand = op->GetInputOperand(i);
@@ -3628,14 +3578,14 @@ void Function::NormalizeCoaForNormalOperands(std::vector<std::vector<SymbolicSca
3628 if (inOutCasts.count(iOperand) > 0) {3578 if (inOutCasts.count(iOperand) > 0) {
3629 continue;3579 continue;
3630 }3580 }
3631- auto it = processedOperands.find(iOperand);3581+ auto it = normTensors.find(iOperand);
3632- if (it != processedOperands.end()) {3582+ if (it != normTensors.end()) {
3633 op->SetIOpAtt(i, it->second);3583 op->SetIOpAtt(i, it->second);
3634 continue;3584 continue;
3635 }3585 }
3636 if (!iOperand->GetDynOffset().empty() || !iOperand->GetDynValidShape().empty()) {3586 if (!iOperand->GetDynOffset().empty() || !iOperand->GetDynValidShape().empty()) {
3637 auto operandCoaList = NormalizeTensor(iOperand, coaIndex, valueToIndex);3587 auto operandCoaList = NormalizeTensor(iOperand, coaIndex, valueToIndex);
3638- processedOperands.emplace(iOperand, coaIndex);3588+ normTensors.emplace(iOperand, coaIndex);
3639 coaIndex += operandCoaList.size();3589 coaIndex += operandCoaList.size();
3640 coaLists.emplace_back(std::move(operandCoaList));3590 coaLists.emplace_back(std::move(operandCoaList));
3641 }3591 }
@@ -3653,14 +3603,14 @@ void Function::NormalizeCoaForNormalOperands(std::vector<std::vector<SymbolicSca
3653 if (oOperand->GetConsumers().empty() && !OpcodeManager::Inst().IsCopyInOrOut(op->GetOpcode())) {3603 if (oOperand->GetConsumers().empty() && !OpcodeManager::Inst().IsCopyInOrOut(op->GetOpcode())) {
3654 continue;3604 continue;
3655 }3605 }
3656- auto it = processedOperands.find(oOperand);3606+ auto it = normTensors.find(oOperand);
3657- if (it != processedOperands.end()) {3607+ if (it != normTensors.end()) {
3658 op->SetOOpAtt(i, it->second, false);3608 op->SetOOpAtt(i, it->second, false);
3659 continue;3609 continue;
3660 }3610 }
3661 if (!oOperand->GetDynOffset().empty() || !oOperand->GetDynValidShape().empty()) {3611 if (!oOperand->GetDynOffset().empty() || !oOperand->GetDynValidShape().empty()) {
3662 auto operandCoaList = NormalizeTensor(oOperand, coaIndex, valueToIndex);3612 auto operandCoaList = NormalizeTensor(oOperand, coaIndex, valueToIndex);
3663- processedOperands.emplace(oOperand, coaIndex);3613+ normTensors.emplace(oOperand, coaIndex);
3664 op->SetOOpAtt(i, coaIndex, false);3614 op->SetOOpAtt(i, coaIndex, false);
3665 coaIndex += operandCoaList.size();3615 coaIndex += operandCoaList.size();
3666 coaLists.emplace_back(std::move(operandCoaList));3616 coaLists.emplace_back(std::move(operandCoaList));
@@ -3691,10 +3641,7 @@ void Function::NormalizeCoaForSpecialInfo(std::vector<std::vector<SymbolicScalar
3691 coaLists.emplace_back(valueCoaList);3641 coaLists.emplace_back(valueCoaList);
3692 coaIndex += 1;3642 coaIndex += 1;
3693 }3643 }
3694- } else if (op->GetOpcode() == Opcode::OP_NCHW2NC1HWC0 || op->GetOpcode() == Opcode::OP_NCHW2Fractal_Z ||3644+ } else if (OpcodeManager::Inst().IsTransData(op->GetOpcode())) {
3695- op->GetOpcode() == Opcode::OP_NC1HWC02NCHW || op->GetOpcode() == Opcode::OP_NCDHW2NDC1HWC0 ||
3696- op->GetOpcode() == Opcode::OP_NCDHW2FRACTAL_Z_3D || op->GetOpcode() == Opcode::OP_NDC1HWC02NCDHW ||
3697- op->GetOpcode() == Opcode::OP_FractalZ2NCHW || op->GetOpcode() == Opcode::OP_FractalZ3D2NCDHW) {
3698 if (op->HasAttr(OpAttributeKey::transDataOffset)) {3645 if (op->HasAttr(OpAttributeKey::transDataOffset)) {
3699 std::vector<SymbolicScalar> offsets;3646 std::vector<SymbolicScalar> offsets;
3700 op->GetAttr(OpAttributeKey::transDataOffset, offsets);3647 op->GetAttr(OpAttributeKey::transDataOffset, offsets);
@@ -3706,15 +3653,23 @@ void Function::NormalizeCoaForSpecialInfo(std::vector<std::vector<SymbolicScalar
3706 }3653 }
3707 op->SetAttribute(OpAttributeKey::transDataOffset, offsets);3654 op->SetAttribute(OpAttributeKey::transDataOffset, offsets);
3708 }3655 }
3709- } else if (op->GetOpcode() == Opcode::OP_L1_TO_L0A || op->GetOpcode() == Opcode::OP_L1_TO_L0B ||3656+ } else if (OpcodeManager::Inst().IsReshapeCopyIn(op->GetOpcode())) {
3710- op->GetOpcode() == Opcode::OP_L1_TO_L0_AT || op->GetOpcode() == Opcode::OP_L1_TO_L0_BT ||3657+ // reshape copy need both to and from validshape normalized, from is done during NormalizeCopyIn
3711- op->GetOpcode() == Opcode::OP_L1_TO_FIX_QUANT_PRE || op->GetOpcode() == Opcode::OP_L1_TO_L0A_SCALE ||3658+ auto copyAttr = std::static_pointer_cast<CopyOpAttribute>(op->GetOpAttribute());
3712- op->GetOpcode() == Opcode::OP_L1_TO_L0B_SCALE || op->GetOpcode() == Opcode::OP_L1_TO_BT) {3659+ auto validshape = copyAttr->GetFromDynValidShape();
3713- auto operandCoaList = NormalizeCopyIn(op.get(), coaIndex, valueToIndex);3660+ auto coaList = MaybeNormalizeValue(validshape, coaIndex, valueToIndex);
3714- coaIndex += operandCoaList.size();3661+ copyAttr->SetFromDynValidShape(OpImmediate::Specified(coaList));
3715- coaLists.emplace_back(std::move(operandCoaList));3662+ coaIndex += coaList.size();
3663+ coaLists.emplace_back(std::move(coaList));
3664+ } else if (OpcodeManager::Inst().IsReshapeCopyOut(op->GetOpcode())) {
3665+ // reshape copy need both to and from validshape normalized, from is done during NormalizeCopyIn
3666+ auto copyAttr = std::static_pointer_cast<CopyOpAttribute>(op->GetOpAttribute());
3667+ auto validshape = copyAttr->GetToDynValidShape();
3668+ auto coaList = MaybeNormalizeValue(validshape, coaIndex, valueToIndex);
3669+ copyAttr->SetToDynValidShape(OpImmediate::Specified(coaList));
3670+ coaIndex += coaList.size();
3671+ coaLists.emplace_back(std::move(coaList));
3716 }3672 }
3717- NormalizeReshapeCopyDynValidShape(op.get(), coaLists, coaIndex, valueToIndex);
3718 }3673 }
3719}3674}
3720 3675 
@@ -682,17 +682,14 @@ public:
682 std::vector<OperandAttribute>& oOpAttr);682 std::vector<OperandAttribute>& oOpAttr);
683 void NormalizeCoaForInCasts(std::vector<OperandAttribute>& iOpAttr,683 void NormalizeCoaForInCasts(std::vector<OperandAttribute>& iOpAttr,
684 std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,684 std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,
685- std::unordered_map<LogicalTensorPtr, int>& processedOperands,685+ std::unordered_map<LogicalTensorPtr, int>& normTensors,
686- std::unordered_set<Operation*>& normalizedOps,
687 const std::unordered_map<int, Operation*>& opmagicToOp);686 const std::unordered_map<int, Operation*>& opmagicToOp);
688 void NormalizeCoaForOutCasts(std::vector<OperandAttribute>& oOpAttr,687 void NormalizeCoaForOutCasts(std::vector<OperandAttribute>& oOpAttr,
689 std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,688 std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,
690- std::unordered_map<LogicalTensorPtr, int>& processedOperands,689+ std::unordered_map<LogicalTensorPtr, int>& normTensors,
691- std::unordered_set<Operation*>& normalizedOps,
692 const std::unordered_map<int, Operation*>& opmagicToOp);690 const std::unordered_map<int, Operation*>& opmagicToOp);
693 void NormalizeCoaForNormalOperands(std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,691 void NormalizeCoaForNormalOperands(std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex,
694- std::unordered_map<LogicalTensorPtr, int>& processedOperands,692+ std::unordered_map<LogicalTensorPtr, int>& normTensors);
695- const std::unordered_set<Operation*>& normalizedOps);
696 void NormalizeCoaForSpecialInfo(std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex);693 void NormalizeCoaForSpecialInfo(std::vector<std::vector<SymbolicScalar>>& coaLists, int& coaIndex);
697 void GetOutcastSymbolicExpr(std::map<int, SymbolicScalar>& tabel);694 void GetOutcastSymbolicExpr(std::map<int, SymbolicScalar>& tabel);
698 695 
@@ -575,6 +575,24 @@ public:
575 opcode == Opcode::OP_L1_TO_BT;575 opcode == Opcode::OP_L1_TO_BT;
576 }576 }
577 577 
578+ inline bool IsTransData(Opcode opCode) const
579+ {
580+ return opCode == Opcode::OP_NCHW2NC1HWC0 || opCode == Opcode::OP_NCHW2Fractal_Z ||
581+ opCode == Opcode::OP_NC1HWC02NCHW || opCode == Opcode::OP_NCDHW2NDC1HWC0 ||
582+ opCode == Opcode::OP_NCDHW2FRACTAL_Z_3D || opCode == Opcode::OP_NDC1HWC02NCDHW ||
583+ opCode == Opcode::OP_FractalZ2NCHW || opCode == Opcode::OP_FractalZ3D2NCDHW;
584+ }
585+ 
586+ inline bool IsReshapeCopyIn(Opcode opCode) const
587+ {
588+ return opCode == Opcode::OP_RESHAPE_COPY_IN || opCode == Opcode::OP_L1_RESHAPE_COPY_IN;
589+ }
590+ 
591+ inline bool IsReshapeCopyOut(Opcode opCode) const
592+ {
593+ return opCode == Opcode::OP_RESHAPE_COPY_OUT || opCode == Opcode::OP_L0C_RESHAPE_COPY_OUT;
594+ }
595+ 
578 inline bool IsCopyInOrOut(Opcode opCode) const { return IsCopyIn(opCode) || IsCopyOut(opCode); }596 inline bool IsCopyInOrOut(Opcode opCode) const { return IsCopyIn(opCode) || IsCopyOut(opCode); }
579 597 
580 inline bool IsSync(Opcode opcode) const { return opcode == Opcode::OP_SYNC_SRC || opcode == Opcode::OP_SYNC_DST; }598 inline bool IsSync(Opcode opcode) const { return opcode == Opcode::OP_SYNC_SRC || opcode == Opcode::OP_SYNC_DST; }