Merged
feat(operation): Add FZ Reverse Conversion #5464
yangfulaiAdd on Aug 15
feat(operation): Add FZ Reverse Conversion #5464
Merged
Merged fromyangfulai/pypto-openfzintocann/pyptomaster
Total: 13 files changed+621-52
| @@ -85,6 +85,8 @@ CodeGenOpNPU::CodeGenOpNPU(const CodeGenOpNPUCtx& ctx) | |||
| 85 | {Opcode::OP_NCDHW2NDC1HWC0, [this]() { return GenTransData(); }}, | 85 | {Opcode::OP_NCDHW2NDC1HWC0, [this]() { return GenTransData(); }}, |
| 86 | {Opcode::OP_NCDHW2FRACTAL_Z_3D, [this]() { return GenTransData(); }}, | 86 | {Opcode::OP_NCDHW2FRACTAL_Z_3D, [this]() { return GenTransData(); }}, |
| 87 | {Opcode::OP_NDC1HWC02NCDHW, [this]() { return GenTransData(); }}, | 87 | {Opcode::OP_NDC1HWC02NCDHW, [this]() { return GenTransData(); }}, |
| 88 | + {Opcode::OP_FractalZ2NCHW, [this]() { return GenTransData(); }}, | ||
| 89 | + {Opcode::OP_FractalZ3D2NCDHW, [this]() { return GenTransData(); }}, | ||
| 88 | 90 | ||
| 89 | // index outcast | 91 | // index outcast |
| 90 | {Opcode::OP_INDEX_OUTCAST, [this]() { return GenIndexOutCastOp(); }}, | 92 | {Opcode::OP_INDEX_OUTCAST, [this]() { return GenIndexOutCastOp(); }}, |
| @@ -223,7 +223,8 @@ std::string CodeGenOpNPU::GenTransData() const | |||
| 223 | 223 | ||
| 224 | if (opCode == Opcode::OP_NCHW2NC1HWC0 || opCode == Opcode::OP_NC1HWC02NCHW || opCode == Opcode::OP_NCHW2Fractal_Z || | 224 | if (opCode == Opcode::OP_NCHW2NC1HWC0 || opCode == Opcode::OP_NC1HWC02NCHW || opCode == Opcode::OP_NCHW2Fractal_Z || |
| 225 | opCode == Opcode::OP_NCDHW2NDC1HWC0 || opCode == Opcode::OP_NCDHW2FRACTAL_Z_3D || | 225 | opCode == Opcode::OP_NCDHW2NDC1HWC0 || opCode == Opcode::OP_NCDHW2FRACTAL_Z_3D || |
| 226 | - opCode == Opcode::OP_NDC1HWC02NCDHW) { | 226 | + opCode == Opcode::OP_NDC1HWC02NCDHW || opCode == Opcode::OP_FractalZ2NCHW || |
| 227 | + opCode == Opcode::OP_FractalZ3D2NCDHW) { | ||
| 227 | return PrintTransDataLayoutTmp(); | 228 | return PrintTransDataLayoutTmp(); |
| 228 | } | 229 | } |
| 229 | 230 | ||
| @@ -3270,7 +3270,8 @@ void Function::NormalizeCoaForSpecialInfo(std::vector<std::vector<SymbolicScalar | |||
| 3270 | } | 3270 | } |
| 3271 | } else if (op->GetOpcode() == Opcode::OP_NCHW2NC1HWC0 || op->GetOpcode() == Opcode::OP_NCHW2Fractal_Z || | 3271 | } else if (op->GetOpcode() == Opcode::OP_NCHW2NC1HWC0 || op->GetOpcode() == Opcode::OP_NCHW2Fractal_Z || |
| 3272 | op->GetOpcode() == Opcode::OP_NC1HWC02NCHW || op->GetOpcode() == Opcode::OP_NCDHW2NDC1HWC0 || | 3272 | op->GetOpcode() == Opcode::OP_NC1HWC02NCHW || op->GetOpcode() == Opcode::OP_NCDHW2NDC1HWC0 || |
| 3273 | - op->GetOpcode() == Opcode::OP_NCDHW2FRACTAL_Z_3D || op->GetOpcode() == Opcode::OP_NDC1HWC02NCDHW) { | 3273 | + op->GetOpcode() == Opcode::OP_NCDHW2FRACTAL_Z_3D || op->GetOpcode() == Opcode::OP_NDC1HWC02NCDHW || |
| 3274 | + op->GetOpcode() == Opcode::OP_FractalZ2NCHW || op->GetOpcode() == Opcode::OP_FractalZ3D2NCDHW) { | ||
| 3274 | if (op->HasAttr(OpAttributeKey::transDataOffset)) { | 3275 | if (op->HasAttr(OpAttributeKey::transDataOffset)) { |
| 3275 | std::vector<SymbolicScalar> offsets; | 3276 | std::vector<SymbolicScalar> offsets; |
| 3276 | op->GetAttr(OpAttributeKey::transDataOffset, offsets); | 3277 | op->GetAttr(OpAttributeKey::transDataOffset, offsets); |
| @@ -1188,41 +1188,19 @@ REGISTER_INFER_SHAPE_FUNC(OP_LOAD2D_CONV, Opcode::OP_LOAD2D_CONV, L1ToL0ConvInfe | |||
| 1188 | REGISTER_INFER_SHAPE_FUNC(OP_LOAD2DDX_CONV, Opcode::OP_LOAD2DDX_CONV, L1ToL0ConvInferFunc); | 1188 | REGISTER_INFER_SHAPE_FUNC(OP_LOAD2DDX_CONV, Opcode::OP_LOAD2DDX_CONV, L1ToL0ConvInferFunc); |
| 1189 | REGISTER_INFER_SHAPE_FUNC(OP_L0C_COPY_OUT_CONV, Opcode::OP_L0C_COPY_OUT_CONV, L0CCopyOutConvInferFunc); | 1189 | REGISTER_INFER_SHAPE_FUNC(OP_L0C_COPY_OUT_CONV, Opcode::OP_L0C_COPY_OUT_CONV, L0CCopyOutConvInferFunc); |
| 1190 | 1190 | ||
| 1191 | -void TransDataDefaultInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | ||
| 1192 | -{ | ||
| 1193 | - outValidShapes.push_back(op->GetOOperands()[0]->GetDynValidShape()); | ||
| 1194 | - std::vector<int64_t> tmpValidShape = op->GetOOperands()[1]->GetShape(); | ||
| 1195 | - outValidShapes.push_back(SymbolicScalar::FromConcrete(tmpValidShape)); | ||
| 1196 | -} | ||
| 1197 | - | ||
| 1198 | -void TransDataNC1HWC02NCHWInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | ||
| 1199 | -{ | ||
| 1200 | - std::vector<SymbolicScalar> validShape = op->GetOOperands()[0]->GetDynValidShape(); | ||
| 1201 | - outValidShapes.push_back(validShape); | ||
| 1202 | - std::vector<int64_t> tmpValidShape = op->GetOOperands()[1]->GetShape(); | ||
| 1203 | - outValidShapes.push_back(SymbolicScalar::FromConcrete(tmpValidShape)); | ||
| 1204 | -} | ||
| 1205 | - | ||
| 1206 | void TransDataInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 1191 | void TransDataInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 1207 | { | 1192 | { |
| 1208 | switch (op->GetOpcode()) { | 1193 | switch (op->GetOpcode()) { |
| 1209 | case Opcode::OP_NCHW2NC1HWC0: | 1194 | case Opcode::OP_NCHW2NC1HWC0: |
| 1210 | - TransDataDefaultInferFunc(op, outValidShapes); | ||
| 1211 | - return; | ||
| 1212 | case Opcode::OP_NCHW2Fractal_Z: | 1195 | case Opcode::OP_NCHW2Fractal_Z: |
| 1213 | - TransDataDefaultInferFunc(op, outValidShapes); | ||
| 1214 | - return; | ||
| 1215 | case Opcode::OP_NC1HWC02NCHW: | 1196 | case Opcode::OP_NC1HWC02NCHW: |
| 1216 | - TransDataNC1HWC02NCHWInferFunc(op, outValidShapes); | ||
| 1217 | - return; | ||
| 1218 | case Opcode::OP_NCDHW2FRACTAL_Z_3D: | 1197 | case Opcode::OP_NCDHW2FRACTAL_Z_3D: |
| 1219 | - TransDataDefaultInferFunc(op, outValidShapes); | ||
| 1220 | - return; | ||
| 1221 | case Opcode::OP_NCDHW2NDC1HWC0: | 1198 | case Opcode::OP_NCDHW2NDC1HWC0: |
| 1222 | - TransDataDefaultInferFunc(op, outValidShapes); | ||
| 1223 | - return; | ||
| 1224 | case Opcode::OP_NDC1HWC02NCDHW: | 1199 | case Opcode::OP_NDC1HWC02NCDHW: |
| 1225 | - TransDataDefaultInferFunc(op, outValidShapes); | 1200 | + case Opcode::OP_FractalZ2NCHW: |
| 1201 | + case Opcode::OP_FractalZ3D2NCDHW: | ||
| 1202 | + outValidShapes.push_back(op->GetOOperands()[0]->GetDynValidShape()); | ||
| 1203 | + outValidShapes.push_back(SymbolicScalar::FromConcrete(op->GetOOperands()[1]->GetShape())); | ||
| 1226 | return; | 1204 | return; |
| 1227 | default: | 1205 | default: |
| 1228 | ASSERT(VectorErrorCode::ERR_PARAM_INVALID, false) << "The transDataType is not supported"; | 1206 | ASSERT(VectorErrorCode::ERR_PARAM_INVALID, false) << "The transDataType is not supported"; |
| @@ -1235,6 +1213,8 @@ REGISTER_INFER_SHAPE_FUNC(OP_NC1HWC02NCHW, Opcode::OP_NC1HWC02NCHW, TransDataInf | |||
| 1235 | REGISTER_INFER_SHAPE_FUNC(OP_NCDHW2NDC1HWC0, Opcode::OP_NCDHW2NDC1HWC0, TransDataInferFunc); | 1213 | REGISTER_INFER_SHAPE_FUNC(OP_NCDHW2NDC1HWC0, Opcode::OP_NCDHW2NDC1HWC0, TransDataInferFunc); |
| 1236 | REGISTER_INFER_SHAPE_FUNC(OP_NCDHW2FRACTAL_Z_3D, Opcode::OP_NCDHW2FRACTAL_Z_3D, TransDataInferFunc); | 1214 | REGISTER_INFER_SHAPE_FUNC(OP_NCDHW2FRACTAL_Z_3D, Opcode::OP_NCDHW2FRACTAL_Z_3D, TransDataInferFunc); |
| 1237 | REGISTER_INFER_SHAPE_FUNC(OP_NDC1HWC02NCDHW, Opcode::OP_NDC1HWC02NCDHW, TransDataInferFunc); | 1215 | REGISTER_INFER_SHAPE_FUNC(OP_NDC1HWC02NCDHW, Opcode::OP_NDC1HWC02NCDHW, TransDataInferFunc); |
| 1216 | +REGISTER_INFER_SHAPE_FUNC(OP_FractalZ2NCHW, Opcode::OP_FractalZ2NCHW, TransDataInferFunc); | ||
| 1217 | +REGISTER_INFER_SHAPE_FUNC(OP_FractalZ3D2NCDHW, Opcode::OP_FractalZ3D2NCDHW, TransDataInferFunc); | ||
| 1238 | 1218 | ||
| 1239 | void CopyInInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 1219 | void CopyInInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 1240 | { | 1220 | { |
| @@ -697,6 +697,14 @@ void OpcodeManager::RegisterVector() | |||
| 697 | {MemoryType::MEM_UB, MemoryType::MEM_UB}, | 697 | {MemoryType::MEM_UB, MemoryType::MEM_UB}, |
| 698 | {"TileOp::TtransData_ndc1hwc02ncdhw", PIPE_V, PIPE_V, CoreType::AIV}, OpCalcType::OTHER, | 698 | {"TileOp::TtransData_ndc1hwc02ncdhw", PIPE_V, PIPE_V, CoreType::AIV}, OpCalcType::OTHER, |
| 699 | {OpAttributeKey::transDataOffset, OP_ATTR_PREFIX + "group", OpAttributeKey::excludeBufferReuse}); | 699 | {OpAttributeKey::transDataOffset, OP_ATTR_PREFIX + "group", OpAttributeKey::excludeBufferReuse}); |
| 700 | + RegisterInfo(Opcode::OP_FractalZ2NCHW, OpCoreType::AIV, "FractalZ2NCHW", {MemoryType::MEM_UB}, | ||
| 701 | + {MemoryType::MEM_UB, MemoryType::MEM_UB}, | ||
| 702 | + {"TileOp::TtransData_fractalz2nchw", PIPE_S, PIPE_S, CoreType::AIV}, OpCalcType::OTHER, | ||
| 703 | + {OpAttributeKey::transDataOffset, OP_ATTR_PREFIX + "group", OpAttributeKey::excludeBufferReuse}); | ||
| 704 | + RegisterInfo(Opcode::OP_FractalZ3D2NCDHW, OpCoreType::AIV, "FractalZ3D2NCDHW", {MemoryType::MEM_UB}, | ||
| 705 | + {MemoryType::MEM_UB, MemoryType::MEM_UB}, | ||
| 706 | + {"TileOp::TtransData_fractalz3d2ncdhw", PIPE_S, PIPE_S, CoreType::AIV}, OpCalcType::OTHER, | ||
| 707 | + {OpAttributeKey::transDataOffset, OP_ATTR_PREFIX + "group", OpAttributeKey::excludeBufferReuse}); | ||
| 700 | RegisterInfo(Opcode::OP_PERMUTE, OpCoreType::AIV, "PERMUTE", {MemoryType::MEM_DEVICE_DDR}, {MemoryType::MEM_UB}, | 708 | RegisterInfo(Opcode::OP_PERMUTE, OpCoreType::AIV, "PERMUTE", {MemoryType::MEM_DEVICE_DDR}, {MemoryType::MEM_UB}, |
| 701 | {"TileOp::TPermute", PIPE_S, PIPE_MTE2, CoreType::AIV}, OpCalcType::OTHER, {OpAttributeKey::perm}, | 709 | {"TileOp::TPermute", PIPE_S, PIPE_MTE2, CoreType::AIV}, OpCalcType::OTHER, {OpAttributeKey::perm}, |
| 702 | TileShapeVerifier::Verify); | 710 | TileShapeVerifier::Verify); |
| @@ -1224,6 +1232,8 @@ std::unordered_map<Opcode, std::string> SUPPORT_TILETENSOR_OPS{ | |||
| 1224 | {Opcode::OP_NCDHW2NDC1HWC0, "TTransDataNCDHW2NDC1HWC0"}, | 1232 | {Opcode::OP_NCDHW2NDC1HWC0, "TTransDataNCDHW2NDC1HWC0"}, |
| 1225 | {Opcode::OP_NCDHW2FRACTAL_Z_3D, "TTransDataNCDHW2FRACTAL_Z_3D"}, | 1233 | {Opcode::OP_NCDHW2FRACTAL_Z_3D, "TTransDataNCDHW2FRACTAL_Z_3D"}, |
| 1226 | {Opcode::OP_NDC1HWC02NCDHW, "TTransDataNDC1HWC02NCDHW"}, | 1234 | {Opcode::OP_NDC1HWC02NCDHW, "TTransDataNDC1HWC02NCDHW"}, |
| 1235 | + {Opcode::OP_FractalZ2NCHW, "TTransDataFractalZ2NCHW"}, | ||
| 1236 | + {Opcode::OP_FractalZ3D2NCDHW, "TTransDataFractalZ3D2NCDHW"}, | ||
| 1227 | {Opcode::OP_INDEX_PUT, "TIndexPut"}, | 1237 | {Opcode::OP_INDEX_PUT, "TIndexPut"}, |
| 1228 | {Opcode::OP_GCD, "TGcd"}, | 1238 | {Opcode::OP_GCD, "TGcd"}, |
| 1229 | {Opcode::OP_ERF, "TErf"}, | 1239 | {Opcode::OP_ERF, "TErf"}, |
| @@ -88,6 +88,8 @@ enum class Opcode { | |||
| 88 | OP_NCDHW2NDC1HWC0, | 88 | OP_NCDHW2NDC1HWC0, |
| 89 | OP_NCDHW2FRACTAL_Z_3D, | 89 | OP_NCDHW2FRACTAL_Z_3D, |
| 90 | OP_NDC1HWC02NCDHW, | 90 | OP_NDC1HWC02NCDHW, |
| 91 | + OP_FractalZ2NCHW, | ||
| 92 | + OP_FractalZ3D2NCDHW, | ||
| 91 | OP_ABS, | 93 | OP_ABS, |
| 92 | OP_PERMUTE, | 94 | OP_PERMUTE, |
| 93 | OP_PERMUTE_ELEMENT, | 95 | OP_PERMUTE_ELEMENT, |
| @@ -560,9 +562,6 @@ public: | |||
| 560 | return opCode == Opcode::OP_COPY_OUT || opCode == Opcode::OP_UB_COPY_OUT || opCode == Opcode::OP_L0C_COPY_OUT || | 562 | return opCode == Opcode::OP_COPY_OUT || opCode == Opcode::OP_UB_COPY_OUT || opCode == Opcode::OP_L0C_COPY_OUT || |
| 561 | opCode == Opcode::OP_L1_COPY_OUT || opCode == Opcode::OP_TRANSPOSE_MOVEOUT || | 563 | opCode == Opcode::OP_L1_COPY_OUT || opCode == Opcode::OP_TRANSPOSE_MOVEOUT || |
| 562 | opCode == Opcode::OP_INDEX_OUTCAST || opCode == Opcode::OP_INDEX_PUT || opCode == Opcode::OP_INDEX_ADD || | 564 | opCode == Opcode::OP_INDEX_OUTCAST || opCode == Opcode::OP_INDEX_PUT || opCode == Opcode::OP_INDEX_ADD || |
| 563 | - opCode == Opcode::OP_NCHW2NC1HWC0 || opCode == Opcode::OP_NCHW2Fractal_Z || | ||
| 564 | - opCode == Opcode::OP_NC1HWC02NCHW || opCode == Opcode::OP_NCDHW2NDC1HWC0 || | ||
| 565 | - opCode == Opcode::OP_NCDHW2FRACTAL_Z_3D || opCode == Opcode::OP_NDC1HWC02NCDHW || | ||
| 566 | opCode == Opcode::OP_SHMEM_PUT || opCode == Opcode::OP_SHMEM_SIGNAL || opCode == Opcode::OP_SHMEM_GET || | 565 | opCode == Opcode::OP_SHMEM_PUT || opCode == Opcode::OP_SHMEM_SIGNAL || opCode == Opcode::OP_SHMEM_GET || |
| 567 | opCode == Opcode::OP_SHMEM_STORE || opCode == Opcode::OP_RESHAPE_COPY_OUT || | 566 | opCode == Opcode::OP_SHMEM_STORE || opCode == Opcode::OP_RESHAPE_COPY_OUT || |
| 568 | opCode == Opcode::OP_L0C_COPY_OUT_CONV || opCode == Opcode::OP_L0C_RESHAPE_COPY_OUT; | 567 | opCode == Opcode::OP_L0C_COPY_OUT_CONV || opCode == Opcode::OP_L0C_RESHAPE_COPY_OUT; |
| @@ -755,6 +754,8 @@ const std::unordered_set<Opcode> SUPPORT_DYNAMIC_UNALIGNED_OPS{Opcode::OP_RANGE, | |||
| 755 | Opcode::OP_NCDHW2NDC1HWC0, | 754 | Opcode::OP_NCDHW2NDC1HWC0, |
| 756 | Opcode::OP_NCDHW2FRACTAL_Z_3D, | 755 | Opcode::OP_NCDHW2FRACTAL_Z_3D, |
| 757 | Opcode::OP_NDC1HWC02NCDHW, | 756 | Opcode::OP_NDC1HWC02NCDHW, |
| 757 | + Opcode::OP_FractalZ2NCHW, | ||
| 758 | + Opcode::OP_FractalZ3D2NCDHW, | ||
| 758 | Opcode::OP_INDEX_OUTCAST, | 759 | Opcode::OP_INDEX_OUTCAST, |
| 759 | Opcode::OP_ADD, | 760 | Opcode::OP_ADD, |
| 760 | Opcode::OP_SUB, | 761 | Opcode::OP_SUB, |
| @@ -1091,9 +1092,7 @@ inline bool IsCopyOut(const Opcode& op) | |||
| 1091 | return (op == Opcode::OP_COPY_OUT || op == Opcode::OP_L0C_COPY_OUT || op == Opcode::OP_TRANSPOSE_MOVEOUT || | 1092 | return (op == Opcode::OP_COPY_OUT || op == Opcode::OP_L0C_COPY_OUT || op == Opcode::OP_TRANSPOSE_MOVEOUT || |
| 1092 | op == Opcode::OP_INDEX_OUTCAST || op == Opcode::OP_INDEX_PUT || op == Opcode::OP_SHMEM_PUT || | 1093 | op == Opcode::OP_INDEX_OUTCAST || op == Opcode::OP_INDEX_PUT || op == Opcode::OP_SHMEM_PUT || |
| 1093 | op == Opcode::OP_SHMEM_SIGNAL || op == Opcode::OP_SHMEM_GET || op == Opcode::OP_SHMEM_SET || | 1094 | op == Opcode::OP_SHMEM_SIGNAL || op == Opcode::OP_SHMEM_GET || op == Opcode::OP_SHMEM_SET || |
| 1094 | - op == Opcode::OP_RESHAPE_COPY_OUT || op == Opcode::OP_SHMEM_STORE || op == Opcode::OP_NCHW2NC1HWC0 || | 1095 | + op == Opcode::OP_RESHAPE_COPY_OUT || op == Opcode::OP_SHMEM_STORE || op == Opcode::OP_INDEX_ADD || |
| 1095 | - op == Opcode::OP_NCHW2Fractal_Z || op == Opcode::OP_NC1HWC02NCHW || op == Opcode::OP_NDC1HWC02NCDHW || | ||
| 1096 | - op == Opcode::OP_NCDHW2NDC1HWC0 || op == Opcode::OP_NCDHW2FRACTAL_Z_3D || op == Opcode::OP_INDEX_ADD || | ||
| 1097 | op == Opcode::OP_L0C_COPY_OUT_CONV || op == Opcode::OP_L0C_RESHAPE_COPY_OUT); | 1096 | op == Opcode::OP_L0C_COPY_OUT_CONV || op == Opcode::OP_L0C_RESHAPE_COPY_OUT); |
| 1098 | } | 1097 | } |
| 1099 | 1098 | ||
| @@ -548,6 +548,8 @@ public: | |||
| 548 | Opcode::OP_NCDHW2NDC1HWC0, | 548 | Opcode::OP_NCDHW2NDC1HWC0, |
| 549 | Opcode::OP_NCDHW2FRACTAL_Z_3D, | 549 | Opcode::OP_NCDHW2FRACTAL_Z_3D, |
| 550 | Opcode::OP_NDC1HWC02NCDHW, | 550 | Opcode::OP_NDC1HWC02NCDHW, |
| 551 | + Opcode::OP_FractalZ2NCHW, | ||
| 552 | + Opcode::OP_FractalZ3D2NCDHW, | ||
| 551 | Opcode::OP_SHMEM_PUT, | 553 | Opcode::OP_SHMEM_PUT, |
| 552 | Opcode::OP_SHMEM_STORE, | 554 | Opcode::OP_SHMEM_STORE, |
| 553 | Opcode::OP_SHMEM_SIGNAL, | 555 | Opcode::OP_SHMEM_SIGNAL, |
| @@ -539,6 +539,10 @@ struct TransDataPara { | |||
| 539 | const std::vector<SymbolicScalar> tileParams; | 539 | const std::vector<SymbolicScalar> tileParams; |
| 540 | const int group; | 540 | const int group; |
| 541 | int groupIdx; | 541 | int groupIdx; |
| 542 | + int DIdx; | ||
| 543 | + int C1Idx; | ||
| 544 | + int HIdx; | ||
| 545 | + int WIdx; | ||
| 542 | }; | 546 | }; |
| 543 | 547 | ||
| 544 | std::shared_ptr<LogicalTensor> transDataPadNC1HWC0(Function& function, const std::shared_ptr<LogicalTensor>& inputTile, | 548 | std::shared_ptr<LogicalTensor> transDataPadNC1HWC0(Function& function, const std::shared_ptr<LogicalTensor>& inputTile, |
| @@ -1199,10 +1203,6 @@ template <TileOpFormat T> | |||
| 1199 | void InnerTransData(size_t cur, Function& function, const TileShape& tileShape, const TransDataPara& transDataPara, | 1203 | void InnerTransData(size_t cur, Function& function, const TileShape& tileShape, const TransDataPara& transDataPara, |
| 1200 | TransDataTileInfoPara& transDataTileInfoPara) | 1204 | TransDataTileInfoPara& transDataTileInfoPara) |
| 1201 | { | 1205 | { |
| 1202 | - if (T == TileOpFormat::TILEOP_ND) { | ||
| 1203 | - return InnerTransDataND<T>(cur, function, tileShape, transDataPara, transDataTileInfoPara); | ||
| 1204 | - } | ||
| 1205 | - | ||
| 1206 | const LogicalTensorPtr& input = transDataPara.input; | 1206 | const LogicalTensorPtr& input = transDataPara.input; |
| 1207 | const LogicalTensorPtr& dstTensor = transDataPara.dstTensor; | 1207 | const LogicalTensorPtr& dstTensor = transDataPara.dstTensor; |
| 1208 | std::vector<SymbolicScalar> tileParams = transDataPara.tileParams; | 1208 | std::vector<SymbolicScalar> tileParams = transDataPara.tileParams; |
| @@ -1265,16 +1265,264 @@ void InnerTransData(size_t cur, Function& function, const TileShape& tileShape, | |||
| 1265 | } | 1265 | } |
| 1266 | } | 1266 | } |
| 1267 | 1267 | ||
| 1268 | +std::shared_ptr<LogicalTensor> GetFzNCHWDstTile(Function& function, const LogicalTensorPtr& dstTensor, | ||
| 1269 | + const std::shared_ptr<LogicalTensor>& inputTile, | ||
| 1270 | + const TransDataTileInfoPara& transDataTileInfoPara, | ||
| 1271 | + const TransDataPara& transDataPara, int64_t C0) | ||
| 1272 | +{ | ||
| 1273 | + auto& inputShape = inputTile->GetShape(); | ||
| 1274 | + int N0 = 16; | ||
| 1275 | + int DSTN = dstTensor->GetShape()[0]; | ||
| 1276 | + int DSTC = dstTensor->GetShape()[1]; | ||
| 1277 | + int DSTW = dstTensor->GetShape()[3]; | ||
| 1278 | + int DSTPerGroupN = DSTN / transDataPara.group; | ||
| 1279 | + | ||
| 1280 | + int N1Offset = transDataTileInfoPara.inputTileInfo.offset[1]; | ||
| 1281 | + | ||
| 1282 | + int64_t dstTileN = std::min(inputShape[1] * N0, static_cast<int64_t>(DSTPerGroupN - N1Offset * N0)); | ||
| 1283 | + int64_t dstTileC = C0; | ||
| 1284 | + int64_t dstTileH = 1; | ||
| 1285 | + int64_t dstTileW = inputShape[0]; | ||
| 1286 | + | ||
| 1287 | + int totalC1HWOffset = transDataTileInfoPara.inputTileInfo.offset[0]; // 1 * 1 * w | ||
| 1288 | + | ||
| 1289 | + int suffixOffset = totalC1HWOffset % DSTW; | ||
| 1290 | + int C1Offset = transDataPara.C1Idx; | ||
| 1291 | + int dstCOffset = C1Offset * C0; | ||
| 1292 | + int dstHOffset = transDataPara.HIdx; | ||
| 1293 | + int dstWOffset = suffixOffset; | ||
| 1294 | + | ||
| 1295 | + int dstNOffset = transDataPara.groupIdx * DSTPerGroupN + N1Offset * N0; | ||
| 1296 | + | ||
| 1297 | + if ((dstCOffset + dstTileC) > DSTC) { | ||
| 1298 | + dstTileC = DSTC - dstCOffset; | ||
| 1299 | + } | ||
| 1300 | + | ||
| 1301 | + Shape dstShape = {dstTileN, dstTileC, dstTileH, dstTileW}; | ||
| 1302 | + Offset dstOffset = {dstNOffset, dstCOffset, dstHOffset, dstWOffset}; | ||
| 1303 | + | ||
| 1304 | + return dstTensor->View(function, dstShape, dstOffset); | ||
| 1305 | +} | ||
| 1306 | + | ||
| 1307 | +std::shared_ptr<LogicalTensor> GetFz3DNCDHWDstTile(Function& function, const LogicalTensorPtr& dstTensor, | ||
| 1308 | + const std::shared_ptr<LogicalTensor>& inputTile, | ||
| 1309 | + const TransDataTileInfoPara& transDataTileInfoPara, | ||
| 1310 | + const TransDataPara& transDataPara, int64_t C0) | ||
| 1311 | +{ | ||
| 1312 | + auto& inputShape = inputTile->GetShape(); | ||
| 1313 | + int N0 = 16; | ||
| 1314 | + int DSTN = dstTensor->GetShape()[0]; | ||
| 1315 | + int DSTC = dstTensor->GetShape()[1]; | ||
| 1316 | + int DSTW = dstTensor->GetShape()[4]; | ||
| 1317 | + int DSTPerGroupN = DSTN / transDataPara.group; | ||
| 1318 | + | ||
| 1319 | + int N1Offset = transDataTileInfoPara.inputTileInfo.offset[1]; | ||
| 1320 | + | ||
| 1321 | + int64_t dstTileN = std::min(inputShape[1] * N0, static_cast<int64_t>(DSTPerGroupN - N1Offset * N0)); | ||
| 1322 | + int64_t dstTileC = C0; | ||
| 1323 | + int64_t dstTileD = 1; | ||
| 1324 | + int64_t dstTileH = 1; | ||
| 1325 | + int64_t dstTileW = inputShape[0]; | ||
| 1326 | + | ||
| 1327 | + int totalDC1HWOffset = transDataTileInfoPara.inputTileInfo.offset[0]; | ||
| 1328 | + | ||
| 1329 | + int suffixOffset = totalDC1HWOffset % DSTW; | ||
| 1330 | + int dstDOffset = transDataPara.DIdx; | ||
| 1331 | + int dstCOffset = transDataPara.C1Idx * C0; | ||
| 1332 | + int dstHOffset = transDataPara.HIdx; | ||
| 1333 | + int dstWOffset = suffixOffset; | ||
| 1334 | + int dstNOffset = transDataPara.groupIdx * DSTPerGroupN + N1Offset * N0; | ||
| 1335 | + | ||
| 1336 | + if ((dstCOffset + dstTileC) > DSTC) { | ||
| 1337 | + dstTileC = DSTC - dstCOffset; | ||
| 1338 | + } | ||
| 1339 | + | ||
| 1340 | + Shape dstShape = {dstTileN, dstTileC, dstTileD, dstTileH, dstTileW}; | ||
| 1341 | + Offset dstOffset = {dstNOffset, dstCOffset, dstDOffset, dstHOffset, dstWOffset}; | ||
| 1342 | + | ||
| 1343 | + return dstTensor->View(function, dstShape, dstOffset); | ||
| 1344 | +} | ||
| 1345 | + | ||
| 1346 | +void HandleFractalZ2NCHW(Function& function, const LogicalTensorPtr& dstTensor, const LogicalTensorPtr& inputTile, | ||
| 1347 | + std::vector<SymbolicScalar>& tileParams, const TransDataTileInfoPara& transDataTileInfoPara, | ||
| 1348 | + [[maybe_unused]] int64_t dstW, const TransDataPara& transDataPara) | ||
| 1349 | +{ | ||
| 1350 | + int64_t C0 = BLOCK_SIZE / BytesOf(inputTile->Datatype()); | ||
| 1351 | + int shape2 = 1; | ||
| 1352 | + std::vector<int64_t> tmpShape = {shape2}; | ||
| 1353 | + auto tmpTile = std::make_shared<LogicalTensor>(function, inputTile->Datatype(), tmpShape); | ||
| 1354 | + auto dstTensorTile = GetFzNCHWDstTile(function, dstTensor, inputTile, transDataTileInfoPara, transDataPara, C0); | ||
| 1355 | + | ||
| 1356 | + for (int i = 0; i < SHAPE_DIM4; i++) { | ||
| 1357 | + tileParams[i] = SymbolicScalar(transDataTileInfoPara.inputTileInfo.offset[i]); | ||
| 1358 | + } | ||
| 1359 | + tileParams[4] = transDataPara.groupIdx; | ||
| 1360 | + tileParams[5] = transDataPara.group; | ||
| 1361 | + | ||
| 1362 | + auto& op = function.AddOperation(Opcode::OP_FractalZ2NCHW, {inputTile}, {dstTensorTile, tmpTile}); | ||
| 1363 | + op.SetAttribute(OpAttributeKey::transDataOffset, tileParams); | ||
| 1364 | +} | ||
| 1365 | + | ||
| 1366 | +void HandleFractalZ3D2NCDHW(Function& function, const LogicalTensorPtr& dstTensor, const LogicalTensorPtr& inputTile, | ||
| 1367 | + std::vector<SymbolicScalar>& tileParams, const TransDataTileInfoPara& transDataTileInfoPara, | ||
| 1368 | + [[maybe_unused]] int64_t dstW, const TransDataPara& transDataPara) | ||
| 1369 | +{ | ||
| 1370 | + int64_t C0 = BLOCK_SIZE / BytesOf(inputTile->Datatype()); | ||
| 1371 | + int shape2 = 1; | ||
| 1372 | + std::vector<int64_t> tmpShape = {shape2}; | ||
| 1373 | + auto tmpTile = std::make_shared<LogicalTensor>(function, inputTile->Datatype(), tmpShape); | ||
| 1374 | + auto dstTensorTile = GetFz3DNCDHWDstTile(function, dstTensor, inputTile, transDataTileInfoPara, transDataPara, C0); | ||
| 1375 | + | ||
| 1376 | + for (int i = 0; i < SHAPE_DIM4; i++) { | ||
| 1377 | + tileParams[i] = SymbolicScalar(transDataTileInfoPara.inputTileInfo.offset[i]); | ||
| 1378 | + } | ||
| 1379 | + tileParams[4] = transDataPara.groupIdx; | ||
| 1380 | + tileParams[5] = transDataPara.group; | ||
| 1381 | + | ||
| 1382 | + auto& op = function.AddOperation(Opcode::OP_FractalZ3D2NCDHW, {inputTile}, {dstTensorTile, tmpTile}); | ||
| 1383 | + op.SetAttribute(OpAttributeKey::transDataOffset, tileParams); | ||
| 1384 | +} | ||
| 1385 | + | ||
| 1386 | +template <TileOpFormat T> | ||
| 1387 | +void InnerTransDataFz2ND(size_t cur, Function& function, const TileShape& tileShape, const TransDataPara& transDataPara, | ||
| 1388 | + TransDataTileInfoPara& transDataTileInfoPara) | ||
| 1389 | +{ | ||
| 1390 | + const LogicalTensorPtr& input = transDataPara.input; | ||
| 1391 | + const LogicalTensorPtr& dstTensor = transDataPara.dstTensor; | ||
| 1392 | + std::vector<SymbolicScalar> tileParams = transDataPara.tileParams; | ||
| 1393 | + int64_t C0 = BLOCK_SIZE / BytesOf(input->Datatype()); | ||
| 1394 | + const int dstC1 = (dstTensor->GetShape()[1] + C0 - 1) / C0; | ||
| 1395 | + const int dstH = dstTensor->GetShape()[2]; | ||
| 1396 | + const int dstW = dstTensor->GetShape()[3]; | ||
| 1397 | + const int groupIdx = transDataPara.groupIdx; | ||
| 1398 | + const int C1Idx = transDataPara.C1Idx; | ||
| 1399 | + const int HIdx = transDataPara.HIdx; | ||
| 1400 | + auto vecTile = tileShape.GetVecTile(); | ||
| 1401 | + int outputSize = dstTensor->GetShape().size(); | ||
| 1402 | + | ||
| 1403 | + std::unordered_map<int64_t, int64_t> format2DstAxis = {{4, 0}, {5, 0}}; | ||
| 1404 | + int64_t outputAxis = format2DstAxis[outputSize]; | ||
| 1405 | + int64_t tmpOffset = groupIdx * dstC1 * dstH * dstW + C1Idx * dstH * dstW + HIdx * dstW; | ||
| 1406 | + | ||
| 1407 | + if (cur == input->GetShape().size()) { | ||
| 1408 | + int64_t offsetSuffix = transDataTileInfoPara.inputTileInfo.offset[0] % dstW; | ||
| 1409 | + transDataTileInfoPara.inputTileInfo.offset[0] = tmpOffset + offsetSuffix; | ||
| 1410 | + std::shared_ptr<LogicalTensor> inputTile = input->View(function, transDataTileInfoPara.inputTileInfo.shape, | ||
| 1411 | + transDataTileInfoPara.inputTileInfo.offset); | ||
| 1412 | + | ||
| 1413 | + switch (outputSize) { | ||
| 1414 | + case 4: | ||
| 1415 | + HandleFractalZ2NCHW(function, dstTensor, inputTile, tileParams, transDataTileInfoPara, dstW, | ||
| 1416 | + transDataPara); | ||
| 1417 | + return; | ||
| 1418 | + default: | ||
| 1419 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "The transDataType is not supported"; | ||
| 1420 | + } | ||
| 1421 | + } | ||
| 1422 | + | ||
| 1423 | + int64_t tmpTile = vecTile[cur]; | ||
| 1424 | + int64_t curShapeLen = cur == static_cast<size_t>(outputAxis) ? dstW : input->GetShape()[cur]; | ||
| 1425 | + | ||
| 1426 | + for (int i = 0; i < curShapeLen; i += tmpTile) { | ||
| 1427 | + transDataTileInfoPara.inputTileInfo.offset[cur] = i; | ||
| 1428 | + transDataTileInfoPara.inputTileInfo.shape[cur] = std::min(curShapeLen - i, tmpTile); | ||
| 1429 | + InnerTransDataFz2ND<T>(cur + 1, function, tileShape, transDataPara, transDataTileInfoPara); | ||
| 1430 | + } | ||
| 1431 | +} | ||
| 1432 | + | ||
| 1433 | +template <TileOpFormat T> | ||
| 1434 | +void InnerTransDataFz3D2ND(size_t cur, Function& function, const TileShape& tileShape, | ||
| 1435 | + const TransDataPara& transDataPara, TransDataTileInfoPara& transDataTileInfoPara) | ||
| 1436 | +{ | ||
| 1437 | + const LogicalTensorPtr& input = transDataPara.input; | ||
| 1438 | + const LogicalTensorPtr& dstTensor = transDataPara.dstTensor; | ||
| 1439 | + std::vector<SymbolicScalar> tileParams = transDataPara.tileParams; | ||
| 1440 | + int64_t C0 = BLOCK_SIZE / BytesOf(input->Datatype()); | ||
| 1441 | + const int dstC1 = (dstTensor->GetShape()[1] + C0 - 1) / C0; | ||
| 1442 | + const int dstD = dstTensor->GetShape()[2]; | ||
| 1443 | + const int dstH = dstTensor->GetShape()[3]; | ||
| 1444 | + const int dstW = dstTensor->GetShape()[4]; | ||
| 1445 | + const int groupIdx = transDataPara.groupIdx; | ||
| 1446 | + const int DIdx = transDataPara.DIdx; | ||
| 1447 | + const int C1Idx = transDataPara.C1Idx; | ||
| 1448 | + const int HIdx = transDataPara.HIdx; | ||
| 1449 | + auto vecTile = tileShape.GetVecTile(); | ||
| 1450 | + | ||
| 1451 | + int64_t outputAxis = 0; | ||
| 1452 | + int64_t tmpOffset = groupIdx * dstD * dstC1 * dstH * dstW + DIdx * dstC1 * dstH * dstW + C1Idx * dstH * dstW + | ||
| 1453 | + HIdx * dstW; | ||
| 1454 | + | ||
| 1455 | + if (cur == input->GetShape().size()) { | ||
| 1456 | + int64_t offsetSuffix = transDataTileInfoPara.inputTileInfo.offset[0] % dstW; | ||
| 1457 | + transDataTileInfoPara.inputTileInfo.offset[0] = tmpOffset + offsetSuffix; | ||
| 1458 | + std::shared_ptr<LogicalTensor> inputTile = input->View(function, transDataTileInfoPara.inputTileInfo.shape, | ||
| 1459 | + transDataTileInfoPara.inputTileInfo.offset); | ||
| 1460 | + HandleFractalZ3D2NCDHW(function, dstTensor, inputTile, tileParams, transDataTileInfoPara, dstW, transDataPara); | ||
| 1461 | + return; | ||
| 1462 | + } | ||
| 1463 | + | ||
| 1464 | + int64_t tmpTile = vecTile[cur]; | ||
| 1465 | + int64_t curShapeLen = cur == static_cast<size_t>(outputAxis) ? dstW : input->GetShape()[cur]; | ||
| 1466 | + | ||
| 1467 | + for (int i = 0; i < curShapeLen; i += tmpTile) { | ||
| 1468 | + transDataTileInfoPara.inputTileInfo.offset[cur] = i; | ||
| 1469 | + transDataTileInfoPara.inputTileInfo.shape[cur] = std::min(curShapeLen - i, tmpTile); | ||
| 1470 | + InnerTransDataFz3D2ND<T>(cur + 1, function, tileShape, transDataPara, transDataTileInfoPara); | ||
| 1471 | + } | ||
| 1472 | +} | ||
| 1473 | + | ||
| 1268 | template <TileOpFormat T> | 1474 | template <TileOpFormat T> |
| 1269 | void TiledTransData(Function& function, const TileShape& tileShape, TransDataPara& transDataPara) | 1475 | void TiledTransData(Function& function, const TileShape& tileShape, TransDataPara& transDataPara) |
| 1270 | { | 1476 | { |
| 1477 | + int64_t C0 = BLOCK_SIZE / BytesOf(transDataPara.input->Datatype()); | ||
| 1271 | TransDataTileInfoPara transDataTileInfoPara{ | 1478 | TransDataTileInfoPara transDataTileInfoPara{ |
| 1272 | TileInfo(transDataPara.input->GetShape().size(), transDataPara.input->GetOffset().size()), | 1479 | TileInfo(transDataPara.input->GetShape().size(), transDataPara.input->GetOffset().size()), |
| 1273 | TileInfo(transDataPara.dstTensor->GetShape().size(), transDataPara.dstTensor->GetOffset().size())}; | 1480 | TileInfo(transDataPara.dstTensor->GetShape().size(), transDataPara.dstTensor->GetOffset().size())}; |
| 1274 | int group = transDataPara.group; | 1481 | int group = transDataPara.group; |
| 1275 | - for (int i = 0; i < group; i++) { | 1482 | + int inputShapeSize = transDataPara.input->GetShape().size(); |
| 1276 | - transDataPara.groupIdx = i; | 1483 | + int outputShapeSize = transDataPara.dstTensor->GetShape().size(); |
| 1277 | - InnerTransData<T>(0, function, tileShape, transDataPara, transDataTileInfoPara); | 1484 | + if (T == TileOpFormat::TILEOP_ND) { |
| 1485 | + if ((inputShapeSize == 5 && outputShapeSize == 4) || (inputShapeSize == 6 && outputShapeSize == 5)) { | ||
| 1486 | + for (int i = 0; i < group; i++) { | ||
| 1487 | + transDataPara.groupIdx = i; | ||
| 1488 | + InnerTransDataND<T>(0, function, tileShape, transDataPara, transDataTileInfoPara); | ||
| 1489 | + } | ||
| 1490 | + } else if (inputShapeSize == 4 && outputShapeSize == 5) { | ||
| 1491 | + int dstC1 = (transDataPara.dstTensor->GetShape()[1] + C0 - 1) / C0; | ||
| 1492 | + int dstD = transDataPara.dstTensor->GetShape()[2]; | ||
| 1493 | + int dstH = transDataPara.dstTensor->GetShape()[3]; | ||
| 1494 | + for (int i = 0; i < group; i++) { | ||
| 1495 | + transDataPara.groupIdx = i; | ||
| 1496 | + for (int j = 0; j < dstD; j++) { | ||
| 1497 | + transDataPara.DIdx = j; | ||
| 1498 | + for (int k = 0; k < dstC1; k++) { | ||
| 1499 | + transDataPara.C1Idx = k; | ||
| 1500 | + for (int l = 0; l < dstH; l++) { | ||
| 1501 | + transDataPara.HIdx = l; | ||
| 1502 | + InnerTransDataFz3D2ND<T>(0, function, tileShape, transDataPara, transDataTileInfoPara); | ||
| 1503 | + } | ||
| 1504 | + } | ||
| 1505 | + } | ||
| 1506 | + } | ||
| 1507 | + } else { | ||
| 1508 | + int dstC1 = (transDataPara.dstTensor->GetShape()[1] + C0 - 1) / C0; | ||
| 1509 | + int dstH = transDataPara.dstTensor->GetShape()[2]; | ||
| 1510 | + for (int i = 0; i < group; i++) { | ||
| 1511 | + transDataPara.groupIdx = i; | ||
| 1512 | + for (int j = 0; j < dstC1; j++) { | ||
| 1513 | + transDataPara.C1Idx = j; | ||
| 1514 | + for (int k = 0; k < dstH; k++) { | ||
| 1515 | + transDataPara.HIdx = k; | ||
| 1516 | + InnerTransDataFz2ND<T>(0, function, tileShape, transDataPara, transDataTileInfoPara); | ||
| 1517 | + } | ||
| 1518 | + } | ||
| 1519 | + } | ||
| 1520 | + } | ||
| 1521 | + } else { | ||
| 1522 | + for (int i = 0; i < group; i++) { | ||
| 1523 | + transDataPara.groupIdx = i; | ||
| 1524 | + InnerTransData<T>(0, function, tileShape, transDataPara, transDataTileInfoPara); | ||
| 1525 | + } | ||
| 1278 | } | 1526 | } |
| 1279 | } | 1527 | } |
| 1280 | 1528 | ||
| @@ -1530,9 +1778,6 @@ LogicalTensorPtr TransDataNDC1HWC02NCDHW(Function& function, const LogicalTensor | |||
| 1530 | LogicalTensorPtr TransDataNC1HWC02NCHW(Function& function, const LogicalTensorPtr& self, const LogicalTensorPtr& output, | 1778 | LogicalTensorPtr TransDataNC1HWC02NCHW(Function& function, const LogicalTensorPtr& self, const LogicalTensorPtr& output, |
| 1531 | int group) | 1779 | int group) |
| 1532 | { | 1780 | { |
| 1533 | - if (self->GetShape().size() != 5) { | ||
| 1534 | - return TransDataNDC1HWC02NCDHW(function, self, output, group); | ||
| 1535 | - } | ||
| 1536 | SymbolicScalar dstValidC = output->GetDynValidShape()[1]; | 1781 | SymbolicScalar dstValidC = output->GetDynValidShape()[1]; |
| 1537 | SymbolicScalar inputValidC1 = self->GetDynValidShape()[1]; | 1782 | SymbolicScalar inputValidC1 = self->GetDynValidShape()[1]; |
| 1538 | int64_t C0 = BLOCK_SIZE / BytesOf(self->Datatype()); | 1783 | int64_t C0 = BLOCK_SIZE / BytesOf(self->Datatype()); |
| @@ -1565,6 +1810,102 @@ LogicalTensorPtr TransDataNC1HWC02NCHW(Function& function, const LogicalTensorPt | |||
| 1565 | return result; | 1810 | return result; |
| 1566 | } | 1811 | } |
| 1567 | 1812 | ||
| 1813 | +LogicalTensorPtr TransDataFractalZ2NCHW(Function& function, const LogicalTensorPtr& self, | ||
| 1814 | + const LogicalTensorPtr& output, int group) | ||
| 1815 | +{ | ||
| 1816 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, group > 0) << "The group is not valid !"; | ||
| 1817 | + SymbolicScalar dstValidPerN = output->GetDynValidShape()[0] / group; | ||
| 1818 | + SymbolicScalar dstValidC = output->GetDynValidShape()[1]; | ||
| 1819 | + int64_t N0 = 16; | ||
| 1820 | + int64_t C0 = BLOCK_SIZE / BytesOf(self->Datatype()); | ||
| 1821 | + SymbolicScalar padNSize = (dstValidPerN + N0 - 1) / N0 * N0 - dstValidPerN; | ||
| 1822 | + SymbolicScalar padCSize = (dstValidC + C0 - 1) / C0 * C0 - dstValidC; | ||
| 1823 | + | ||
| 1824 | + VecTile oriVectile = TileShape::Current().GetVecTile(); | ||
| 1825 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, C0 > 0) << "The C0 is not valid !"; | ||
| 1826 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, oriVectile.tile[2] == N0) << "The tileShape n0 should be equal to N0!"; | ||
| 1827 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, oriVectile.tile[3] == C0) << "The tileShape c0 should be equal to C0!"; | ||
| 1828 | + | ||
| 1829 | + auto result = std::make_shared<LogicalTensor>(function, self->Datatype(), output->GetShape(), | ||
| 1830 | + output->dynValidShape_, TileOpFormat::TILEOP_ND); | ||
| 1831 | + | ||
| 1832 | + auto& op = function.AddOperation(Opcode::OP_FractalZ2NCHW, {self}, {result}); | ||
| 1833 | + std::vector<SymbolicScalar> tileParams = {}; | ||
| 1834 | + // 0-3 input offset; | ||
| 1835 | + // c1hw n1 n0 c0 idx group padNSize padCSize N C H W | ||
| 1836 | + for (auto i : self->GetShape()) { | ||
| 1837 | + (void)i; | ||
| 1838 | + tileParams.push_back(SymbolicScalar(0)); | ||
| 1839 | + } | ||
| 1840 | + tileParams.push_back(0); | ||
| 1841 | + tileParams.push_back(0); | ||
| 1842 | + tileParams.push_back(padNSize); | ||
| 1843 | + tileParams.push_back(padCSize); | ||
| 1844 | + for (auto i : output->GetShape()) { | ||
| 1845 | + tileParams.push_back(SymbolicScalar(i)); | ||
| 1846 | + } | ||
| 1847 | + op.SetAttribute(OpAttributeKey::transDataOffset, tileParams); | ||
| 1848 | + op.SetAttribute(OP_ATTR_PREFIX + "group", group); | ||
| 1849 | + return result; | ||
| 1850 | +} | ||
| 1851 | + | ||
| 1852 | +LogicalTensorPtr TransDataFractalZ3D2NCDHW(Function& function, const LogicalTensorPtr& self, | ||
| 1853 | + const LogicalTensorPtr& output, int group) | ||
| 1854 | +{ | ||
| 1855 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, group > 0) << "The group is not valid !"; | ||
| 1856 | + SymbolicScalar dstValidPerN = output->GetDynValidShape()[0] / group; | ||
| 1857 | + SymbolicScalar dstValidC = output->GetDynValidShape()[1]; | ||
| 1858 | + int64_t N0 = 16; | ||
| 1859 | + int64_t C0 = BLOCK_SIZE / BytesOf(self->Datatype()); | ||
| 1860 | + SymbolicScalar padNSize = (dstValidPerN + N0 - 1) / N0 * N0 - dstValidPerN; | ||
| 1861 | + SymbolicScalar padCSize = (dstValidC + C0 - 1) / C0 * C0 - dstValidC; | ||
| 1862 | + | ||
| 1863 | + VecTile oriVectile = TileShape::Current().GetVecTile(); | ||
| 1864 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, C0 > 0) << "The C0 is not valid !"; | ||
| 1865 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, oriVectile.tile[2] == N0) << "The tileShape n0 should be equal to N0!"; | ||
| 1866 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, oriVectile.tile[3] == C0) << "The tileShape c0 should be equal to C0!"; | ||
| 1867 | + | ||
| 1868 | + auto result = std::make_shared<LogicalTensor>(function, self->Datatype(), output->GetShape(), | ||
| 1869 | + output->dynValidShape_, TileOpFormat::TILEOP_ND); | ||
| 1870 | + | ||
| 1871 | + auto& op = function.AddOperation(Opcode::OP_FractalZ3D2NCDHW, {self}, {result}); | ||
| 1872 | + std::vector<SymbolicScalar> tileParams = {}; | ||
| 1873 | + // 0-3 input offset (DC1HW, N1, N0, C0) | ||
| 1874 | + // 4: groupIdx, 5: group, 6: padNSize, 7: padCSize, 8-12: N C D H W | ||
| 1875 | + for (auto i : self->GetShape()) { | ||
| 1876 | + (void)i; | ||
| 1877 | + tileParams.push_back(SymbolicScalar(0)); | ||
| 1878 | + } | ||
| 1879 | + tileParams.push_back(0); | ||
| 1880 | + tileParams.push_back(0); | ||
| 1881 | + tileParams.push_back(padNSize); | ||
| 1882 | + tileParams.push_back(padCSize); | ||
| 1883 | + for (auto i : output->GetShape()) { | ||
| 1884 | + tileParams.push_back(SymbolicScalar(i)); | ||
| 1885 | + } | ||
| 1886 | + op.SetAttribute(OpAttributeKey::transDataOffset, tileParams); | ||
| 1887 | + op.SetAttribute(OP_ATTR_PREFIX + "group", group); | ||
| 1888 | + return result; | ||
| 1889 | +} | ||
| 1890 | + | ||
| 1891 | +LogicalTensorPtr TransDataReverse(Function& function, const LogicalTensorPtr& self, const LogicalTensorPtr& output, | ||
| 1892 | + int group) | ||
| 1893 | +{ | ||
| 1894 | + int inputShapeSize = self->GetShape().size(); | ||
| 1895 | + int outputShapeSize = output->GetShape().size(); | ||
| 1896 | + if (inputShapeSize == 5 && outputShapeSize == 4) { | ||
| 1897 | + return TransDataNC1HWC02NCHW(function, self, output, group); | ||
| 1898 | + } else if (inputShapeSize == 6 && outputShapeSize == 5) { | ||
| 1899 | + return TransDataNDC1HWC02NCDHW(function, self, output, group); | ||
| 1900 | + } else if (inputShapeSize == 4 && outputShapeSize == 4) { | ||
| 1901 | + return TransDataFractalZ2NCHW(function, self, output, group); | ||
| 1902 | + } else if (inputShapeSize == 4 && outputShapeSize == 5) { | ||
| 1903 | + return TransDataFractalZ3D2NCDHW(function, self, output, group); | ||
| 1904 | + } else { | ||
| 1905 | + return TransDataFractalZ2NCHW(function, self, output, group); // TODO | ||
| 1906 | + } | ||
| 1907 | +} | ||
| 1908 | + | ||
| 1568 | LogicalTensorPtr TensorTransData(Function& function, const LogicalTensorPtr& self, const LogicalTensorPtr& output, | 1909 | LogicalTensorPtr TensorTransData(Function& function, const LogicalTensorPtr& self, const LogicalTensorPtr& output, |
| 1569 | TileOpFormat transDataType, int group) | 1910 | TileOpFormat transDataType, int group) |
| 1570 | { | 1911 | { |
| @@ -1578,7 +1919,7 @@ LogicalTensorPtr TensorTransData(Function& function, const LogicalTensorPtr& sel | |||
| 1578 | case TileOpFormat::TILEOP_FRACTAL_Z_3D: | 1919 | case TileOpFormat::TILEOP_FRACTAL_Z_3D: |
| 1579 | return TransDataFRACTAL_Z_3D(function, self, group); | 1920 | return TransDataFRACTAL_Z_3D(function, self, group); |
| 1580 | case TileOpFormat::TILEOP_ND: | 1921 | case TileOpFormat::TILEOP_ND: |
| 1581 | - return TransDataNC1HWC02NCHW(function, self, output, group); // 两种情况,NC1HWC0和NDC1HWC0 | 1922 | + return TransDataReverse(function, self, output, group); // 反向 |
| 1582 | default: | 1923 | default: |
| 1583 | CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "The transDataType is not supported"; | 1924 | CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "The transDataType is not supported"; |
| 1584 | } | 1925 | } |
| @@ -1621,7 +1962,7 @@ LogicalTensorPtr TransData(Function& function, const LogicalTensorPtr& self, con | |||
| 1621 | CheckTensorDimRange(self, SHAPE_DIM5, SHAPE_DIM5, "TRANSDATA FRACTAL_Z_3D"); | 1962 | CheckTensorDimRange(self, SHAPE_DIM5, SHAPE_DIM5, "TRANSDATA FRACTAL_Z_3D"); |
| 1622 | break; | 1963 | break; |
| 1623 | case TileOpFormat::TILEOP_ND: | 1964 | case TileOpFormat::TILEOP_ND: |
| 1624 | - CheckTensorDimRange(self, SHAPE_DIM5, SHAPE_DIM6, "TRANSDATA ND"); | 1965 | + CheckTensorDimRange(self, SHAPE_DIM4, SHAPE_DIM6, "TRANSDATA ND"); |
| 1625 | break; | 1966 | break; |
| 1626 | default: | 1967 | default: |
| 1627 | CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "The transDataType is not supported"; | 1968 | CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "The transDataType is not supported"; |
| @@ -1989,7 +2330,7 @@ void TransDataTileFunc(Function& function, const TileShape& tileShape, const std | |||
| 1989 | { | 2330 | { |
| 1990 | std::vector<SymbolicScalar> tileParams = op.GetVectorSymbolicScalarAttribute(OpAttributeKey::transDataOffset); | 2331 | std::vector<SymbolicScalar> tileParams = op.GetVectorSymbolicScalarAttribute(OpAttributeKey::transDataOffset); |
| 1991 | int group = op.GetIntAttribute(OP_ATTR_PREFIX + "group"); | 2332 | int group = op.GetIntAttribute(OP_ATTR_PREFIX + "group"); |
| 1992 | - TransDataPara transDataPara = TransDataPara{iOperand[0], oOperand[0], tileParams, group, 0}; | 2333 | + TransDataPara transDataPara = TransDataPara{iOperand[0], oOperand[0], tileParams, group, 0, 0, 0, 0, 0}; |
| 1993 | switch (op.GetOpcode()) { | 2334 | switch (op.GetOpcode()) { |
| 1994 | case Opcode::OP_NCHW2NC1HWC0: | 2335 | case Opcode::OP_NCHW2NC1HWC0: |
| 1995 | TiledTransData<TileOpFormat::TILEOP_NC1HWC0>(function, tileShape, transDataPara); | 2336 | TiledTransData<TileOpFormat::TILEOP_NC1HWC0>(function, tileShape, transDataPara); |
| @@ -2009,6 +2350,12 @@ void TransDataTileFunc(Function& function, const TileShape& tileShape, const std | |||
| 2009 | case Opcode::OP_NDC1HWC02NCDHW: | 2350 | case Opcode::OP_NDC1HWC02NCDHW: |
| 2010 | TiledTransData<TileOpFormat::TILEOP_ND>(function, tileShape, transDataPara); | 2351 | TiledTransData<TileOpFormat::TILEOP_ND>(function, tileShape, transDataPara); |
| 2011 | break; | 2352 | break; |
| 2353 | + case Opcode::OP_FractalZ2NCHW: | ||
| 2354 | + TiledTransData<TileOpFormat::TILEOP_ND>(function, tileShape, transDataPara); | ||
| 2355 | + break; | ||
| 2356 | + case Opcode::OP_FractalZ3D2NCDHW: | ||
| 2357 | + TiledTransData<TileOpFormat::TILEOP_ND>(function, tileShape, transDataPara); | ||
| 2358 | + break; | ||
| 2012 | default: | 2359 | default: |
| 2013 | CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "The transDataType is not supported"; | 2360 | CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "The transDataType is not supported"; |
| 2014 | } | 2361 | } |
| @@ -2026,5 +2373,7 @@ REGISTER_OPERATION_TILED_FUNC(OP_NC1HWC02NCHW, Opcode::OP_NC1HWC02NCHW, TransDat | |||
| 2026 | REGISTER_OPERATION_TILED_FUNC(OP_NCDHW2NDC1HWC0, Opcode::OP_NCDHW2NDC1HWC0, TransDataTileFunc); | 2373 | REGISTER_OPERATION_TILED_FUNC(OP_NCDHW2NDC1HWC0, Opcode::OP_NCDHW2NDC1HWC0, TransDataTileFunc); |
| 2027 | REGISTER_OPERATION_TILED_FUNC(OP_NCDHW2FRACTAL_Z_3D, Opcode::OP_NCDHW2FRACTAL_Z_3D, TransDataTileFunc); | 2374 | REGISTER_OPERATION_TILED_FUNC(OP_NCDHW2FRACTAL_Z_3D, Opcode::OP_NCDHW2FRACTAL_Z_3D, TransDataTileFunc); |
| 2028 | REGISTER_OPERATION_TILED_FUNC(OP_NDC1HWC02NCDHW, Opcode::OP_NDC1HWC02NCDHW, TransDataTileFunc); | 2375 | REGISTER_OPERATION_TILED_FUNC(OP_NDC1HWC02NCDHW, Opcode::OP_NDC1HWC02NCDHW, TransDataTileFunc); |
| 2376 | +REGISTER_OPERATION_TILED_FUNC(OP_FractalZ2NCHW, Opcode::OP_FractalZ2NCHW, TransDataTileFunc); | ||
| 2377 | +REGISTER_OPERATION_TILED_FUNC(OP_FractalZ3D2NCDHW, Opcode::OP_FractalZ3D2NCDHW, TransDataTileFunc); | ||
| 2029 | 2378 | ||
| 2030 | } // namespace npu::tile_fwk | 2379 | } // namespace npu::tile_fwk |
| @@ -402,4 +402,120 @@ __aicore__ inline void TTransDataNCDHW2FRACTAL_Z_3D(DST dst, TMP tmpTensor, INPU | |||
| 402 | 402 | ||
| 403 | pto::TTRANS(ConvDst, convInput, tmpTile); | 403 | pto::TTRANS(ConvDst, convInput, tmpTile); |
| 404 | } | 404 | } |
| 405 | + | ||
| 406 | +#define OP_TILE_OP_TRANSDATA_FractalZ2NCHW TTransDataFractalZ2NCHW | ||
| 407 | +template <typename DST, typename TMP, typename INPUT> | ||
| 408 | +__aicore__ inline void TTransDataFractalZ2NCHW(DST dst, TMP tmpTensor, INPUT input) | ||
| 409 | +{ | ||
| 410 | + constexpr auto inputTypeSize = sizeof(typename INPUT::Type); | ||
| 411 | + constexpr auto C0 = 32 / inputTypeSize; | ||
| 412 | + constexpr auto N0 = 16; | ||
| 413 | + constexpr auto dstTileH = Std::tuple_element<DIM_3RD, typename DST::TileShape>::type::value; | ||
| 414 | + constexpr auto dstTileW = Std::tuple_element<DIM_4TH, typename DST::TileShape>::type::value; | ||
| 415 | + | ||
| 416 | + const auto inputLayout = input.GetLayout(); | ||
| 417 | + auto inputC1HW = inputLayout.template GetShapeDim<DIM_2ND, MAX_DIMS>(); | ||
| 418 | + auto inputN1 = inputLayout.template GetShapeDim<DIM_3RD, MAX_DIMS>(); | ||
| 419 | + auto inputN0 = inputLayout.template GetShapeDim<DIM_4TH, MAX_DIMS>(); | ||
| 420 | + auto inputC0 = inputLayout.template GetShapeDim<DIM_5TH, MAX_DIMS>(); | ||
| 421 | + auto inputStride0 = inputLayout.template GetStrideDim<DIM_1ST, MAX_DIMS>(); // 注意验证 | ||
| 422 | + auto inputStride1 = inputLayout.template GetStrideDim<DIM_2ND, MAX_DIMS>(); | ||
| 423 | + auto inputStride2 = inputLayout.template GetStrideDim<DIM_3RD, MAX_DIMS>(); | ||
| 424 | + auto inputStride3 = inputLayout.template GetStrideDim<DIM_4TH, MAX_DIMS>(); | ||
| 425 | + | ||
| 426 | + const auto dstLayout = dst.GetLayout(); | ||
| 427 | + auto dstStride0 = dstLayout.template GetStrideDim<DIM_1ST>(); | ||
| 428 | + auto dstStride1 = dstLayout.template GetStrideDim<DIM_2ND>(); | ||
| 429 | + auto dstStride2 = dstLayout.template GetStrideDim<DIM_3RD>(); | ||
| 430 | + auto dstStride3 = dstLayout.template GetStrideDim<DIM_4TH>(); | ||
| 431 | + auto dstN = dstLayout.template GetShapeDim<DIM_1ST>(); | ||
| 432 | + auto dstC = dstLayout.template GetShapeDim<DIM_2ND>(); | ||
| 433 | + | ||
| 434 | + auto inputAddr = (__ubuf__ typename INPUT::Type*)((uint64_t)(input.GetAddr())); | ||
| 435 | + auto dstAddr = (__ubuf__ typename INPUT::Type*)((uint64_t)(dst.GetAddr())); | ||
| 436 | + | ||
| 437 | + if (inputC1HW == 0 || inputN1 == 0 || inputN0 == 0 || inputC0 == 0) { | ||
| 438 | + return; | ||
| 439 | + } | ||
| 440 | + | ||
| 441 | + for (LoopVar i = 0; i < inputC1HW; i++) { | ||
| 442 | + for (LoopVar j = 0; j < inputN1; j++) { | ||
| 443 | + for (LoopVar k = 0; k < inputN0; k++) { | ||
| 444 | + for (LoopVar m = 0; m < inputC0; m++) { | ||
| 445 | + int inputOffset = i * inputStride1 + j * inputStride2 + k * inputStride3 + m; | ||
| 446 | + int n = j * N0 + k; | ||
| 447 | + int c1 = i / (dstTileH * dstTileW); | ||
| 448 | + int hw = i % (dstTileH * dstTileW); | ||
| 449 | + int c = c1 * C0 + m; | ||
| 450 | + int h = hw / dstTileW; | ||
| 451 | + int w = hw % dstTileW; | ||
🟠 High Priority TTransDataFractalZ2NCHW(trans_data.h 第 443-458 行)的循环变量语义是: 但输出坐标计算写反了: 证据链:FractalZ 布局为 [C1HW, N1, N0, C0],反变换到 NCHW 时 N 由 N1×N0 构成、C1/H/W 由 C1HW 分解构成 → 上述三处把 建议:按 FractalZ→NCHW 的正确映射修正:N 由 N1(j)/N0(k) 计算,C1/H/W 由 C1HW(i) 分解,并使用输出张量真实的 H、W(而非 tile 尺寸)。 ![]() ![]() Incorrect? | |||
| 452 | + if (n < dstN && c < dstC) { | ||
| 453 | + int dstOffset = n * dstStride0 + c * dstStride1 + h * dstStride2 + w * dstStride3; | ||
| 454 | + dstAddr[dstOffset] = inputAddr[inputOffset]; | ||
| 455 | + } | ||
| 456 | + } | ||
| 457 | + } | ||
| 458 | + } | ||
| 459 | + } | ||
| 460 | +} | ||
| 461 | + | ||
| 462 | +#define OP_TILE_OP_TRANSDATA_FractalZ3D2NCDHW TTransDataFractalZ3D2NCDHW | ||
| 463 | +template <typename DST, typename TMP, typename INPUT> | ||
| 464 | +__aicore__ inline void TTransDataFractalZ3D2NCDHW(DST dst, TMP tmpTensor, INPUT input) | ||
| 465 | +{ | ||
| 466 | + constexpr auto inputTypeSize = sizeof(typename INPUT::Type); | ||
| 467 | + constexpr auto C0 = 32 / inputTypeSize; | ||
| 468 | + constexpr auto N0 = 16; | ||
| 469 | + constexpr auto dstTileC = Std::tuple_element<DIM_2ND, typename DST::TileShape>::type::value; | ||
| 470 | + constexpr auto dstTileD = Std::tuple_element<DIM_3RD, typename DST::TileShape>::type::value; | ||
| 471 | + constexpr auto dstTileH = Std::tuple_element<DIM_4TH, typename DST::TileShape>::type::value; | ||
| 472 | + constexpr auto dstTileW = Std::tuple_element<DIM_5TH, typename DST::TileShape>::type::value; | ||
| 473 | + constexpr auto tileC1 = (dstTileC + C0 - 1) / C0; | ||
| 474 | + | ||
| 475 | + const auto inputLayout = input.GetLayout(); | ||
| 476 | + auto inputDC1HW = inputLayout.template GetShapeDim<DIM_2ND, MAX_DIMS>(); | ||
| 477 | + auto inputN1 = inputLayout.template GetShapeDim<DIM_3RD, MAX_DIMS>(); | ||
| 478 | + auto inputN0 = inputLayout.template GetShapeDim<DIM_4TH, MAX_DIMS>(); | ||
| 479 | + auto inputC0 = inputLayout.template GetShapeDim<DIM_5TH, MAX_DIMS>(); | ||
| 480 | + auto inputStride1 = inputLayout.template GetStrideDim<DIM_2ND, MAX_DIMS>(); | ||
| 481 | + auto inputStride2 = inputLayout.template GetStrideDim<DIM_3RD, MAX_DIMS>(); | ||
| 482 | + auto inputStride3 = inputLayout.template GetStrideDim<DIM_4TH, MAX_DIMS>(); | ||
| 483 | + | ||
| 484 | + const auto dstLayout = dst.GetLayout(); | ||
| 485 | + auto dstStride0 = dstLayout.template GetStrideDim<DIM_1ST, MAX_DIMS>(); | ||
| 486 | + auto dstStride1 = dstLayout.template GetStrideDim<DIM_2ND, MAX_DIMS>(); | ||
| 487 | + auto dstStride2 = dstLayout.template GetStrideDim<DIM_3RD, MAX_DIMS>(); | ||
| 488 | + auto dstStride3 = dstLayout.template GetStrideDim<DIM_4TH, MAX_DIMS>(); | ||
| 489 | + auto dstN = dstLayout.template GetShapeDim<DIM_1ST, MAX_DIMS>(); | ||
| 490 | + auto dstC = dstLayout.template GetShapeDim<DIM_2ND, MAX_DIMS>(); | ||
| 491 | + | ||
| 492 | + auto inputAddr = (__ubuf__ typename INPUT::Type*)((uint64_t)(input.GetAddr())); | ||
| 493 | + auto dstAddr = (__ubuf__ typename INPUT::Type*)((uint64_t)(dst.GetAddr())); | ||
| 494 | + | ||
| 495 | + if (inputDC1HW == 0 || inputN1 == 0 || inputN0 == 0 || inputC0 == 0) { | ||
| 496 | + return; | ||
| 497 | + } | ||
| 498 | + | ||
| 499 | + for (LoopVar i = 0; i < inputDC1HW; i++) { | ||
| 500 | + for (LoopVar j = 0; j < inputN1; j++) { | ||
| 501 | + for (LoopVar k = 0; k < inputN0; k++) { | ||
| 502 | + for (LoopVar m = 0; m < inputC0; m++) { | ||
| 503 | + int inputOffset = i * inputStride1 + j * inputStride2 + k * inputStride3 + m; | ||
| 504 | + int n = j * N0 + k; | ||
| 505 | + int d = i / (tileC1 * dstTileH * dstTileW); | ||
| 506 | + int rem = i % (tileC1 * dstTileH * dstTileW); | ||
| 507 | + int c1 = rem / (dstTileH * dstTileW); | ||
| 508 | + int hw = rem % (dstTileH * dstTileW); | ||
| 509 | + int c = c1 * C0 + m; | ||
| 510 | + int h = hw / dstTileW; | ||
| 511 | + int w = hw % dstTileW; | ||
| 512 | + if (n < dstN && c < dstC) { | ||
| 513 | + int dstOffset = n * dstStride0 + c * dstStride1 + d * dstStride2 + h * dstStride3 + w; | ||
| 514 | + dstAddr[dstOffset] = inputAddr[inputOffset]; | ||
| 515 | + } | ||
| 516 | + } | ||
| 517 | + } | ||
| 518 | + } | ||
| 519 | + } | ||
| 520 | +} | ||
| 405 | #endif | 521 | #endif |
| @@ -240,9 +240,9 @@ std::vector<std::reference_wrapper<SymbolicScalar>> DynAttrToStatic::GetOpDynami | |||
| 240 | } | 240 | } |
| 241 | 241 | ||
| 242 | const std::set<Opcode> specifiedOps = { | 242 | const std::set<Opcode> specifiedOps = { |
| 243 | - Opcode::OP_VEC_DUP, Opcode::OP_EXPAND, Opcode::OP_RESHAPE, Opcode::OP_NCHW2NC1HWC0, | 243 | + Opcode::OP_VEC_DUP, Opcode::OP_EXPAND, Opcode::OP_RESHAPE, Opcode::OP_NCHW2NC1HWC0, |
| 244 | - Opcode::OP_NCHW2Fractal_Z, Opcode::OP_NC1HWC02NCHW, Opcode::OP_NCDHW2NDC1HWC0, Opcode::OP_NCDHW2FRACTAL_Z_3D, | 244 | + Opcode::OP_NCHW2Fractal_Z, Opcode::OP_NC1HWC02NCHW, Opcode::OP_NCDHW2NDC1HWC0, Opcode::OP_NCDHW2FRACTAL_Z_3D, |
| 245 | - Opcode::OP_NDC1HWC02NCDHW}; | 245 | + Opcode::OP_NDC1HWC02NCDHW, Opcode::OP_FractalZ2NCHW, Opcode::OP_FractalZ3D2NCDHW}; |
| 246 | if (specifiedOps.count(opcode)) { | 246 | if (specifiedOps.count(opcode)) { |
| 247 | auto& attrDict = op.GetAllAttr(); | 247 | auto& attrDict = op.GetAllAttr(); |
| 248 | auto it = attrDict.find(OpAttributeKey::dynScalar); | 248 | auto it = attrDict.find(OpAttributeKey::dynScalar); |
| @@ -1979,7 +1979,7 @@ def gen_transpose_op_golden(case_name: str, output: Path, case_index: int = None | |||
| 1979 | 1979 | ||
| 1980 | return [res.numpy()] | 1980 | return [res.numpy()] |
| 1981 | elif transdata_type == 0: | 1981 | elif transdata_type == 0: |
| 1982 | - if len(input_tensor.shape) == 5: | 1982 | + if len(input_tensor.shape) == 5 and len(config["output_tensors"][0]["shape"]) == 4: |
| 1983 | input_n, input_c1, input_h, input_w, c0 = input_tensor.shape | 1983 | input_n, input_c1, input_h, input_w, c0 = input_tensor.shape |
| 1984 | total_c = c0 * input_c1 | 1984 | total_c = c0 * input_c1 |
| 1985 | output_c = config["output_tensors"][0]["shape"][1] | 1985 | output_c = config["output_tensors"][0]["shape"][1] |
| @@ -2020,6 +2020,75 @@ def gen_transpose_op_golden(case_name: str, output: Path, case_index: int = None | |||
| 2020 | res = res.to(torch.float32).numpy().astype(bfloat16) | 2020 | res = res.to(torch.float32).numpy().astype(bfloat16) |
| 2021 | return [res] | 2021 | return [res] |
| 2022 | 2022 | ||
| 2023 | + return [res.numpy()] | ||
| 2024 | + elif len(input_tensor.shape) == 4 and len(config["output_tensors"][0]["shape"]) == 4: | ||
| 2025 | + input_c1hw, input_n1, input_n0, input_c0 = input_tensor.shape | ||
| 2026 | + c0 = int(32 / get_c_sizeof_type(str(inputs[0].dtype))) | ||
| 2027 | + n0 = 16 | ||
| 2028 | + | ||
| 2029 | + output_n = config["output_tensors"][0]["shape"][0] | ||
| 2030 | + output_per_n = output_n // group | ||
| 2031 | + output_per_n1 = (output_per_n + n0 - 1) // n0 | ||
| 2032 | + output_c = config["output_tensors"][0]["shape"][1] | ||
| 2033 | + output_c1 = (output_c + c0 - 1) // c0 | ||
| 2034 | + output_h = config["output_tensors"][0]["shape"][2] | ||
| 2035 | + output_w = config["output_tensors"][0]["shape"][3] | ||
| 2036 | + res = torch.zeros(output_n, output_c, output_h, output_w) | ||
| 2037 | + | ||
| 2038 | + output_pad_per_n = output_per_n1 * n0 | ||
| 2039 | + output_pad_per_c = output_c1 * c0 | ||
| 2040 | + intput_per_c1hw = input_c1hw // group | ||
| 2041 | + | ||
| 2042 | + for i in range(group): | ||
| 2043 | + tmp_input_tensor = input_tensor[i * intput_per_c1hw:(i + 1) * intput_per_c1hw, :, :, :] | ||
| 2044 | + tmp_res = tmp_input_tensor.reshape( | ||
| 2045 | + [output_c1, output_h, output_w, output_per_n1, n0, c0]).permute( | ||
| 2046 | + 3, 4, 0, 5, 1, 2).reshape( | ||
| 2047 | + [output_pad_per_n, output_pad_per_c, output_h, output_w]) | ||
| 2048 | + | ||
| 2049 | + res[i * output_per_n:(i +1) * output_per_n:, :, :, :] = tmp_res[ | ||
| 2050 | + 0:output_per_n, 0:output_c, :, : | ||
| 2051 | + ] | ||
| 2052 | + | ||
| 2053 | + if inputs[0].dtype == bfloat16: | ||
| 2054 | + res = res.to(torch.float32).numpy().astype(bfloat16) | ||
| 2055 | + return [res] | ||
| 2056 | + | ||
| 2057 | + return [res.numpy()] | ||
| 2058 | + elif len(input_tensor.shape) == 4 and len(config["output_tensors"][0]["shape"]) == 5: | ||
| 2059 | + input_dc1hw, input_n1, input_n0, input_c0 = input_tensor.shape | ||
| 2060 | + c0 = int(32 / get_c_sizeof_type(str(inputs[0].dtype))) | ||
| 2061 | + n0 = 16 | ||
| 2062 | + | ||
| 2063 | + output_n = config["output_tensors"][0]["shape"][0] | ||
| 2064 | + output_per_n = output_n // group | ||
| 2065 | + output_per_n1 = (output_per_n + n0 - 1) // n0 | ||
| 2066 | + output_c = config["output_tensors"][0]["shape"][1] | ||
| 2067 | + output_c1 = (output_c + c0 - 1) // c0 | ||
| 2068 | + output_d = config["output_tensors"][0]["shape"][2] | ||
| 2069 | + output_h = config["output_tensors"][0]["shape"][3] | ||
| 2070 | + output_w = config["output_tensors"][0]["shape"][4] | ||
| 2071 | + res = torch.zeros(output_n, output_c, output_d, output_h, output_w) | ||
| 2072 | + | ||
| 2073 | + output_pad_per_n = output_per_n1 * n0 | ||
| 2074 | + output_pad_per_c = output_c1 * c0 | ||
| 2075 | + input_per_dc1hw = input_dc1hw // group | ||
| 2076 | + | ||
| 2077 | + for i in range(group): | ||
| 2078 | + tmp_input_tensor = input_tensor[i * input_per_dc1hw:(i + 1) * input_per_dc1hw, :, :, :] | ||
| 2079 | + tmp_res = tmp_input_tensor.reshape( | ||
| 2080 | + [output_d, output_c1, output_h, output_w, output_per_n1, n0, c0]).permute( | ||
| 2081 | + 4, 5, 1, 6, 0, 2, 3).reshape( | ||
| 2082 | + [output_pad_per_n, output_pad_per_c, output_d, output_h, output_w]) | ||
| 2083 | + | ||
| 2084 | + res[i * output_per_n:(i + 1) * output_per_n, :, :, :, :] = tmp_res[ | ||
| 2085 | + 0:output_per_n, 0:output_c, :, :, : | ||
| 2086 | + ] | ||
| 2087 | + | ||
| 2088 | + if inputs[0].dtype == bfloat16: | ||
| 2089 | + res = res.to(torch.float32).numpy().astype(bfloat16) | ||
| 2090 | + return [res] | ||
| 2091 | + | ||
| 2023 | return [res.numpy()] | 2092 | return [res.numpy()] |
| 2024 | elif transdata_type == 3: | 2093 | elif transdata_type == 3: |
| 2025 | input_n, input_c, input_d, input_h, input_w = input_tensor.shape | 2094 | input_n, input_c, input_d, input_h, input_w = input_tensor.shape |
| @@ -67,6 +67,21 @@ static void TransDataOperationExeFunc4Dims(const std::vector<Tensor>& inputs, st | |||
| 67 | auto res = TransData(tileTensor0, static_cast<TileOpFormat>(transDataInfo->type_), outputs[0].GetShape(), | 67 | auto res = TransData(tileTensor0, static_cast<TileOpFormat>(transDataInfo->type_), outputs[0].GetShape(), |
| 68 | SymbolicScalar::FromConcrete(transDataInfo->validShape_), transDataInfo->group_); | 68 | SymbolicScalar::FromConcrete(transDataInfo->validShape_), transDataInfo->group_); |
| 69 | Assemble(res, {0, 0, 0, 0}, outputs[0]); | 69 | Assemble(res, {0, 0, 0, 0}, outputs[0]); |
| 70 | + } else if (transDataInfo->type_ == 0) { | ||
| 71 | + Tensor tileTensor0 = View( | ||
| 72 | + inputs[0], | ||
| 73 | + {inputs[0].GetShape()[0], inputs[0].GetShape()[1], inputs[0].GetShape()[2], inputs[0].GetShape()[3]}, | ||
| 74 | + {SymbolicScalar(inputs[0].GetShape()[0]), SymbolicScalar(inputs[0].GetShape()[1]), | ||
| 75 | + SymbolicScalar(inputs[0].GetShape()[2]), SymbolicScalar(inputs[0].GetShape()[3])}, | ||
| 76 | + {0, 0, 0, 0}); | ||
| 77 | + TileShape::Current().SetVecTile(transDataInfo->tileShape_); | ||
| 78 | + auto res = TransData(tileTensor0, static_cast<TileOpFormat>(transDataInfo->type_), outputs[0].GetShape(), | ||
| 79 | + SymbolicScalar::FromConcrete(transDataInfo->validShape_), transDataInfo->group_); | ||
| 80 | + if (outputs[0].GetShape().size() == 5) { | ||
| 81 | + Assemble(res, {0, 0, 0, 0, 0}, outputs[0]); | ||
| 82 | + } else { | ||
| 83 | + Assemble(res, {0, 0, 0, 0}, outputs[0]); | ||
| 84 | + } | ||
| 70 | } | 85 | } |
| 71 | } | 86 | } |
| 72 | } | 87 | } |
| @@ -79,7 +94,7 @@ static void TransDataOperationExeFunc5Dims(const std::vector<Tensor>& inputs, st | |||
| 79 | FUNCTION("main", {inputs[0]}, {outputs[0]}) | 94 | FUNCTION("main", {inputs[0]}, {outputs[0]}) |
| 80 | { | 95 | { |
| 81 | if (transDataInfo->type_ == 0) { | 96 | if (transDataInfo->type_ == 0) { |
| 82 | - if (inputs[0].GetShape().size() == 5) { | 97 | + if (inputs[0].GetShape().size() == 5 && outputs[0].GetShape().size() == 4) { |
| 83 | Tensor tileTensor0 = View( | 98 | Tensor tileTensor0 = View( |
| 84 | inputs[0], | 99 | inputs[0], |
| 85 | {inputs[0].GetShape()[0], inputs[0].GetShape()[1], inputs[0].GetShape()[2], inputs[0].GetShape()[3], | 100 | {inputs[0].GetShape()[0], inputs[0].GetShape()[1], inputs[0].GetShape()[2], inputs[0].GetShape()[3], |
| @@ -171,6 +186,7 @@ INSTANTIATE_TEST_SUITE_P(TestTransData, TransDataOperationTest, | |||
| 171 | 186 | ||
| 172 | TEST_P(TransDataOperationTest, TestTransData) | 187 | TEST_P(TransDataOperationTest, TestTransData) |
| 173 | { | 188 | { |
| 189 | + config::SetPassOption(ENABLE_SLICE, false); | ||
| 174 | auto test_data = GetParam().test_data_; | 190 | auto test_data = GetParam().test_data_; |
| 175 | int type = GetValueByName<int>(test_data, "type"); | 191 | int type = GetValueByName<int>(test_data, "type"); |
| 176 | int group = GetValueByName<int>(test_data, "group"); | 192 | int group = GetValueByName<int>(test_data, "group"); |
Mframework/tests/ut/codegen/src/test_dynamic/test_codegen_dyn_vector/test_codegen_dyn_transdata.cpp+24-0
| @@ -161,4 +161,28 @@ TEST_F(TestCodegenDynTransData, TestTransData0_6) | |||
| 161 | .validShape = {1, 7, 1, 1, 8}, | 161 | .validShape = {1, 7, 1, 1, 8}, |
| 162 | .expect = R"!!!(TTransDataNDC1HWC02NCDHW(ubTensor_)!!!"}); | 162 | .expect = R"!!!(TTransDataNDC1HWC02NCDHW(ubTensor_)!!!"}); |
| 163 | } | 163 | } |
| 164 | + | ||
| 165 | +TEST_F(TestCodegenDynTransData, TestTransData0_4) | ||
| 166 | +{ | ||
| 167 | + RunTransDataTest({.caseName = "TestTransData0_4", | ||
| 168 | + .inputShape = {8, 1, 16, 8}, | ||
| 169 | + .vecTile = {8, 1, 16, 8}, | ||
| 170 | + .dtype = DT_FP32, | ||
| 171 | + .outputShape = {16, 8, 1, 8}, | ||
| 172 | + .tileOpFormat = 0, | ||
| 173 | + .validShape = {16, 8, 1, 8}, | ||
| 174 | + .expect = R"!!!(TTransDataFractalZ2NCHW(ubTensor_)!!!"}); | ||
| 175 | +} | ||
| 176 | + | ||
| 177 | +TEST_F(TestCodegenDynTransData, TestTransData0_5) | ||
| 178 | +{ | ||
| 179 | + RunTransDataTest({.caseName = "TestTransData0_5", | ||
| 180 | + .inputShape = {8, 1, 16, 8}, | ||
| 181 | + .vecTile = {8, 1, 16, 8}, | ||
| 182 | + .dtype = DT_FP32, | ||
| 183 | + .outputShape = {16, 8, 1, 1, 8}, | ||
| 184 | + .tileOpFormat = 0, | ||
| 185 | + .validShape = {16, 8, 1, 1, 8}, | ||
| 186 | + .expect = R"!!!(TTransDataFractalZ3D2NCDHW(ubTensor_)!!!"}); | ||
| 187 | +} | ||
| 164 | } // namespace npu::tile_fwk | 188 | } // namespace npu::tile_fwk |


🔴 Critical
新增的 TTransDataFractalZ2NCHW 内核(trans_data.h 第 408-460 行)在第 439 行使用了
inputN、inputC、inputD、inputH、inputW五个变量,但本函数体内只声明了inputC1HW、inputN1、inputN0、inputC0(第 424-427 行),这五个变量从未声明。证据链:第 439 行
if (inputN == 0 || inputC == 0 || inputD == 0 || inputH == 0 || inputW == 0)→ 非依赖名字在模板定义/实例化时查找失败 → 当 FZ 反向转换(OP_FractalZ2NCHW)被 codegen 实例化时产生 "not declared in this scope" 编译错误。这是从其他内核(如 TTransDataNCDHW2FRACTAL_Z_3D 第 350-354 行声明了这些名字)复制粘贴时漏改导致的。建议:将判零条件改为使用本函数声明的维度变量:inputC1HW、inputN1、inputN0、inputC0。