Merged
fix(Operation): modify cleancode #5748
chenblAdd on Aug 28
fix(Operation): modify cleancode #5748
Merged
Merged fromchenbl/pypto_newfix/docsintocann/pyptomaster
Total: 122 files changed+17920-15267
| @@ -23,6 +23,7 @@ namespace npu::tile_fwk { | |||
| 23 | constexpr const int BYTES_PER_REPEAT = 256; | 23 | constexpr const int BYTES_PER_REPEAT = 256; |
| 24 | constexpr const int DEFAULT_MAX_PARALLELISM = 128; | 24 | constexpr const int DEFAULT_MAX_PARALLELISM = 128; |
| 25 | constexpr const int DEFAULT_LATENCY = 10; | 25 | constexpr const int DEFAULT_LATENCY = 10; |
| 26 | +constexpr const size_t MIN_GATHER_SHAPE_SIZE = 2; | ||
| 26 | 27 | ||
| 27 | // get element per cycle | 28 | // get element per cycle |
| 28 | int GetParallelism(const std::string& op, DataType dtype) | 29 | int GetParallelism(const std::string& op, DataType dtype) |
| @@ -71,7 +72,7 @@ int64_t GetGatherInUBResultShapeSize(const std::vector<std::vector<int64_t>>& sh | |||
| 71 | // param: [token_size, hidden_dim], indices: [1, k], block_table: [1, ...] | 72 | // param: [token_size, hidden_dim], indices: [1, k], block_table: [1, ...] |
| 72 | // result: [k, hidden_dim] | 73 | // result: [k, hidden_dim] |
| 73 | // Use result tile size for sparse gather estimation. | 74 | // Use result tile size for sparse gather estimation. |
| 74 | - if (shape.size() < 2 || shape[0].empty() || shape[1].empty()) { | 75 | + if (shape.size() < MIN_GATHER_SHAPE_SIZE || shape[0].empty() || shape[1].empty()) { |
| 75 | return GetMaxShapeSize(shape); | 76 | return GetMaxShapeSize(shape); |
| 76 | } | 77 | } |
| 77 | 78 | ||
| @@ -16,6 +16,7 @@ | |||
| 16 | #include "op_infer_shape_impl.h" | 16 | #include "op_infer_shape_impl.h" |
| 17 | #include "interface/operation/attr_holder.h" | 17 | #include "interface/operation/attr_holder.h" |
| 18 | #include "interface/operation/operation.h" | 18 | #include "interface/operation/operation.h" |
| 19 | +#include "interface/operation/vector/gather_mask_common.h" | ||
| 19 | #include "interface/tensor/symbolic_scalar.h" | 20 | #include "interface/tensor/symbolic_scalar.h" |
| 20 | #include "interface/utils/common.h" | 21 | #include "interface/utils/common.h" |
| 21 | #include "tilefwk/error_code.h" | 22 | #include "tilefwk/error_code.h" |
| @@ -116,12 +117,12 @@ void ElewiseInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& o | |||
| 116 | int64_t mode = 0; | 117 | int64_t mode = 0; |
| 117 | if (op->GetAttr(OP_ATTR_PREFIX + "cmp_mode", mode) && mode == 1) { | 118 | if (op->GetAttr(OP_ATTR_PREFIX + "cmp_mode", mode) && mode == 1) { |
| 118 | inputValidShape[inputValidShape.size() - 1] = inputValidShape[inputValidShape.size() - 1] / | 119 | inputValidShape[inputValidShape.size() - 1] = inputValidShape[inputValidShape.size() - 1] / |
| 119 | - 8; // 8 bit to 1 byte | 120 | + NUM_VALUE_8; // 8 bit to 1 byte |
| 120 | } | 121 | } |
| 121 | 122 | ||
| 122 | int64_t whereBitMode = 0; | 123 | int64_t whereBitMode = 0; |
| 123 | if (op->GetAttr(OP_ATTR_PREFIX + "whereBitMode", whereBitMode) && whereBitMode == 1 && inputIdx.back() == 0) { | 124 | if (op->GetAttr(OP_ATTR_PREFIX + "whereBitMode", whereBitMode) && whereBitMode == 1 && inputIdx.back() == 0) { |
| 124 | - inputValidShape[inputValidShape.size() - 1] = inputValidShape[inputValidShape.size() - 1] * 8; | 125 | + inputValidShape[inputValidShape.size() - 1] = inputValidShape[inputValidShape.size() - 1] * NUM_VALUE_8; |
| 125 | } | 126 | } |
| 126 | 127 | ||
| 127 | for (auto output : op->GetOOperands()) { | 128 | for (auto output : op->GetOOperands()) { |
| @@ -255,13 +256,13 @@ void InferShapeWithTailScaleFunc(Operation* op, std::vector<std::vector<Symbolic | |||
| 255 | outValidShapes.emplace_back(inputValidShape); | 256 | outValidShapes.emplace_back(inputValidShape); |
| 256 | outValidShapes.emplace_back(std::vector<SymbolicScalar>{inputValidShape[ndim - 1] * TailScale}); | 257 | outValidShapes.emplace_back(std::vector<SymbolicScalar>{inputValidShape[ndim - 1] * TailScale}); |
| 257 | } | 258 | } |
| 258 | -REGISTER_INFER_SHAPE_FUNC(OP_FLOORDIV, Opcode::OP_FLOORDIV, InferShapeWithTailScaleFunc<4>); | 259 | +REGISTER_INFER_SHAPE_FUNC(OP_FLOORDIV, Opcode::OP_FLOORDIV, InferShapeWithTailScaleFunc<NUM_VALUE_4>); |
| 259 | -REGISTER_INFER_SHAPE_FUNC(OP_FLOORDIVS, Opcode::OP_FLOORDIVS, InferShapeWithTailScaleFunc<3>); | 260 | +REGISTER_INFER_SHAPE_FUNC(OP_FLOORDIVS, Opcode::OP_FLOORDIVS, InferShapeWithTailScaleFunc<NUM_VALUE_3>); |
| 260 | -REGISTER_INFER_SHAPE_FUNC(OP_SINH, Opcode::OP_SINH, InferShapeWithTailScaleFunc<4>); | 261 | +REGISTER_INFER_SHAPE_FUNC(OP_SINH, Opcode::OP_SINH, InferShapeWithTailScaleFunc<NUM_VALUE_4>); |
| 261 | REGISTER_INFER_SHAPE_FUNC(OP_COSH, Opcode::OP_COSH, InferShapeWithTailScaleFunc<1>); | 262 | REGISTER_INFER_SHAPE_FUNC(OP_COSH, Opcode::OP_COSH, InferShapeWithTailScaleFunc<1>); |
| 262 | -REGISTER_INFER_SHAPE_FUNC(OP_TAN, Opcode::OP_TAN, InferShapeWithTailScaleFunc<7>); | 263 | +REGISTER_INFER_SHAPE_FUNC(OP_TAN, Opcode::OP_TAN, InferShapeWithTailScaleFunc<NUM_VALUE_7>); |
| 263 | -REGISTER_INFER_SHAPE_FUNC(OP_ASINH, Opcode::OP_ASINH, InferShapeWithTailScaleFunc<4>); | 264 | +REGISTER_INFER_SHAPE_FUNC(OP_ASINH, Opcode::OP_ASINH, InferShapeWithTailScaleFunc<NUM_VALUE_4>); |
| 264 | -REGISTER_INFER_SHAPE_FUNC(OP_ACOSH, Opcode::OP_ACOSH, InferShapeWithTailScaleFunc<3>); | 265 | +REGISTER_INFER_SHAPE_FUNC(OP_ACOSH, Opcode::OP_ACOSH, InferShapeWithTailScaleFunc<NUM_VALUE_3>); |
| 265 | 266 | ||
| 266 | void AsinAcosInferShapeFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 267 | void AsinAcosInferShapeFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 267 | { | 268 | { |
| @@ -272,16 +273,16 @@ void AsinAcosInferShapeFunc(Operation* op, std::vector<std::vector<SymbolicScala | |||
| 272 | size_t ndim = inputValidShape.size(); | 273 | size_t ndim = inputValidShape.size(); |
| 273 | outValidShapes.emplace_back(inputValidShape); | 274 | outValidShapes.emplace_back(inputValidShape); |
| 274 | // tmp: 5 个 H x W 中间块,1D 输入退化为 H=1 | 275 | // tmp: 5 个 H x W 中间块,1D 输入退化为 H=1 |
| 275 | - if (ndim >= 2) { | 276 | + if (ndim >= NUM_VALUE_2) { |
| 276 | outValidShapes.emplace_back( | 277 | outValidShapes.emplace_back( |
| 277 | - std::vector<SymbolicScalar>{inputValidShape[ndim - 2] * inputValidShape[ndim - 1] * 5}); | 278 | + std::vector<SymbolicScalar>{inputValidShape[ndim - NUM_VALUE_2] * inputValidShape[ndim - 1] * NUM_VALUE_5}); |
| 278 | } else { | 279 | } else { |
| 279 | - outValidShapes.emplace_back(std::vector<SymbolicScalar>{inputValidShape[ndim - 1] * 5}); | 280 | + outValidShapes.emplace_back(std::vector<SymbolicScalar>{inputValidShape[ndim - 1] * NUM_VALUE_5}); |
| 280 | } | 281 | } |
| 281 | } | 282 | } |
| 282 | REGISTER_INFER_SHAPE_FUNC(OP_ASIN, Opcode::OP_ASIN, AsinAcosInferShapeFunc); | 283 | REGISTER_INFER_SHAPE_FUNC(OP_ASIN, Opcode::OP_ASIN, AsinAcosInferShapeFunc); |
| 283 | REGISTER_INFER_SHAPE_FUNC(OP_ACOS, Opcode::OP_ACOS, AsinAcosInferShapeFunc); | 284 | REGISTER_INFER_SHAPE_FUNC(OP_ACOS, Opcode::OP_ACOS, AsinAcosInferShapeFunc); |
| 284 | -REGISTER_INFER_SHAPE_FUNC(OP_ATANH, Opcode::OP_ATANH, InferShapeWithTailScaleFunc<5>); | 285 | +REGISTER_INFER_SHAPE_FUNC(OP_ATANH, Opcode::OP_ATANH, InferShapeWithTailScaleFunc<NUM_VALUE_5>); |
| 285 | 286 | ||
| 286 | void PadInferShapeFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 287 | void PadInferShapeFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 287 | { | 288 | { |
| @@ -299,8 +300,8 @@ void PadInferShapeFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& | |||
| 299 | if (ndim >= 1 && padRight > 0) { | 300 | if (ndim >= 1 && padRight > 0) { |
| 300 | outValidShape[ndim - 1] = outValidShape[ndim - 1] + padRight; | 301 | outValidShape[ndim - 1] = outValidShape[ndim - 1] + padRight; |
| 301 | } | 302 | } |
| 302 | - if (ndim >= 2 && padBottom > 0) { | 303 | + if (ndim >= NUM_VALUE_2 && padBottom > 0) { |
| 303 | - outValidShape[ndim - 2] = outValidShape[ndim - 2] + padBottom; | 304 | + outValidShape[ndim - NUM_VALUE_2] = outValidShape[ndim - NUM_VALUE_2] + padBottom; |
| 304 | } | 305 | } |
| 305 | for (auto output : op->GetOOperands()) { | 306 | for (auto output : op->GetOOperands()) { |
| 306 | outValidShapes.push_back(outValidShape); | 307 | outValidShapes.push_back(outValidShape); |
| @@ -330,7 +331,7 @@ void DeInterleaveSingleInferShapeFunc(Operation* op, std::vector<std::vector<Sym | |||
| 330 | { | 331 | { |
| 331 | std::vector<SymbolicScalar> outValidShape = op->GetIOperands()[0]->GetDynValidShape(); | 332 | std::vector<SymbolicScalar> outValidShape = op->GetIOperands()[0]->GetDynValidShape(); |
| 332 | if (!outValidShape.empty()) { | 333 | if (!outValidShape.empty()) { |
| 333 | - outValidShape[outValidShape.size() - 1] = outValidShape[outValidShape.size() - 1] / 2; | 334 | + outValidShape[outValidShape.size() - 1] = outValidShape[outValidShape.size() - 1] / NUM_VALUE_2; |
| 334 | } | 335 | } |
| 335 | for (auto output : op->GetOOperands()) { | 336 | for (auto output : op->GetOOperands()) { |
| 336 | outValidShapes.push_back(outValidShape); | 337 | outValidShapes.push_back(outValidShape); |
| @@ -354,7 +355,7 @@ void IndexOutCastInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar | |||
| 354 | { | 355 | { |
| 355 | std::vector<SymbolicScalar> outValidShape; | 356 | std::vector<SymbolicScalar> outValidShape; |
| 356 | /* 这里取IOperands索引是依据AddOperation中ioprand中的输入的顺序,这里使用的是dst参数的ioprand,即第2个索引 */ | 357 | /* 这里取IOperands索引是依据AddOperation中ioprand中的输入的顺序,这里使用的是dst参数的ioprand,即第2个索引 */ |
| 357 | - size_t input_dim = 2; | 358 | + size_t input_dim = NUM_VALUE_2; |
| 358 | auto inValidShape = op->GetIOperands()[input_dim]->GetDynValidShape(); | 359 | auto inValidShape = op->GetIOperands()[input_dim]->GetDynValidShape(); |
| 359 | 360 | ||
| 360 | for (size_t i = 0; i < inValidShape.size(); ++i) { | 361 | for (size_t i = 0; i < inValidShape.size(); ++i) { |
| @@ -416,10 +417,11 @@ void GatherMaskFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& out | |||
| 416 | } | 417 | } |
| 417 | std::vector<SymbolicScalar> res(inputValidShapes[0]); | 418 | std::vector<SymbolicScalar> res(inputValidShapes[0]); |
| 418 | uint8_t patternMode = op->GetIntAttribute(OP_ATTR_PREFIX + "patternMode"); | 419 | uint8_t patternMode = op->GetIntAttribute(OP_ATTR_PREFIX + "patternMode"); |
| 419 | - if (patternMode == 1 || patternMode == 2) { | 420 | + const auto pattern = static_cast<GatherMaskPattern>(patternMode); |
| 420 | - res.back() = res.back() / 2; | 421 | + if (IsHalfGatherMaskPattern(pattern)) { |
| 421 | - } else if (patternMode == 3 || patternMode == 4 || patternMode == 5 || patternMode == 6) { | 422 | + res.back() = res.back() / NUM_VALUE_2; |
| 422 | - res.back() = res.back() / 4; | 423 | + } else if (IsQuarterGatherMaskPattern(pattern)) { |
| 424 | + res.back() = res.back() / NUM_VALUE_4; | ||
| 423 | } | 425 | } |
| 424 | outValidShapes.push_back(res); | 426 | outValidShapes.push_back(res); |
| 425 | } | 427 | } |
| @@ -478,14 +480,15 @@ void LogicalNotInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>> | |||
| 478 | select_dtype = DT_FP16; | 480 | select_dtype = DT_FP16; |
| 479 | } | 481 | } |
| 480 | constexpr int64_t COUNT_SIZE = 2048; | 482 | constexpr int64_t COUNT_SIZE = 2048; |
| 481 | - constexpr int64_t vcmp_bit_size = COUNT_SIZE / 8; | 483 | + constexpr int64_t vcmp_bit_size = COUNT_SIZE / NUM_VALUE_8; |
| 482 | - constexpr size_t ALIGN_SIZE = 32; | 484 | + constexpr size_t ALIGN_SIZE = NUM_VALUE_32; |
| 483 | 485 | ||
| 484 | int64_t total_size; | 486 | int64_t total_size; |
| 485 | if (data_type == DT_INT16 || data_type == DT_UINT16 || data_type == DT_INT32 || data_type == DT_UINT32) { | 487 | if (data_type == DT_INT16 || data_type == DT_UINT16 || data_type == DT_INT32 || data_type == DT_UINT32) { |
| 486 | total_size = COUNT_SIZE * BytesOf(select_dtype); | 488 | total_size = COUNT_SIZE * BytesOf(select_dtype); |
| 487 | } else { | 489 | } else { |
| 488 | - total_size = COUNT_SIZE * 2 + COUNT_SIZE * BytesOf(select_dtype) * 2 + vcmp_bit_size + 8; | 490 | + total_size = COUNT_SIZE * NUM_VALUE_2 + COUNT_SIZE * BytesOf(select_dtype) * NUM_VALUE_2 + vcmp_bit_size + |
| 491 | + NUM_VALUE_8; | ||
| 489 | } | 492 | } |
| 490 | total_size = (total_size + ALIGN_SIZE - 1) / ALIGN_SIZE * ALIGN_SIZE; | 493 | total_size = (total_size + ALIGN_SIZE - 1) / ALIGN_SIZE * ALIGN_SIZE; |
| 491 | int64_t shape = total_size / BytesOf(select_dtype); | 494 | int64_t shape = total_size / BytesOf(select_dtype); |
| @@ -497,8 +500,8 @@ void LogicalAndInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>> | |||
| 497 | { | 500 | { |
| 498 | ElewiseInferFunc(op, outValidShapes); | 501 | ElewiseInferFunc(op, outValidShapes); |
| 499 | outValidShapes.erase(outValidShapes.begin() + 1, outValidShapes.end()); | 502 | outValidShapes.erase(outValidShapes.begin() + 1, outValidShapes.end()); |
| 500 | - const int64_t COUNT_SIZE = 64; | 503 | + const int64_t COUNT_SIZE = NUM_VALUE_64; |
| 501 | - outValidShapes.push_back({COUNT_SIZE * 5 + COUNT_SIZE / 8 + 1}); | 504 | + outValidShapes.push_back({COUNT_SIZE * NUM_VALUE_5 + COUNT_SIZE / NUM_VALUE_8 + 1}); |
| 502 | } | 505 | } |
| 503 | REGISTER_INFER_SHAPE_FUNC(OP_LOGICALAND, Opcode::OP_LOGICALAND, LogicalAndInferFunc); | 506 | REGISTER_INFER_SHAPE_FUNC(OP_LOGICALAND, Opcode::OP_LOGICALAND, LogicalAndInferFunc); |
| 504 | 507 | ||
| @@ -615,9 +618,9 @@ void QuantizeInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& | |||
| 615 | 618 | ||
| 616 | // For axis=-2, align the second-to-last dimension to 32 bytes (8 int32 elements) | 619 | // For axis=-2, align the second-to-last dimension to 32 bytes (8 int32 elements) |
| 617 | int64_t axis = 0; | 620 | int64_t axis = 0; |
| 618 | - if (op->GetAttr(OP_ATTR_PREFIX + "axis", axis) && axis == -2 && tmpValidShape.size() >= 2) { | 621 | + if (op->GetAttr(OP_ATTR_PREFIX + "axis", axis) && axis == -NUM_VALUE_2 && tmpValidShape.size() >= NUM_VALUE_2) { |
| 619 | - constexpr int64_t alignElements = 8; // 8 * 4 = 32 bytes | 622 | + constexpr int64_t alignElements = NUM_VALUE_8; // 8 * 4 = 32 bytes |
| 620 | - size_t lastDimIndex = tmpValidShape.size() - 2; | 623 | + size_t lastDimIndex = tmpValidShape.size() - NUM_VALUE_2; |
| 621 | tmpValidShape[lastDimIndex] = (tmpValidShape[lastDimIndex] + alignElements - 1) / alignElements * | 624 | tmpValidShape[lastDimIndex] = (tmpValidShape[lastDimIndex] + alignElements - 1) / alignElements * |
| 622 | alignElements; | 625 | alignElements; |
| 623 | } | 626 | } |
| @@ -633,9 +636,9 @@ void QuantizeInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& | |||
| 633 | 636 | ||
| 634 | // For axis=-2, align the second-to-last dimension to 32 bytes (8 int32 elements) | 637 | // For axis=-2, align the second-to-last dimension to 32 bytes (8 int32 elements) |
| 635 | int64_t axis = 0; | 638 | int64_t axis = 0; |
| 636 | - if (op->GetAttr(OP_ATTR_PREFIX + "axis", axis) && axis == -2 && tmpValidShape.size() >= 2) { | 639 | + if (op->GetAttr(OP_ATTR_PREFIX + "axis", axis) && axis == -NUM_VALUE_2 && tmpValidShape.size() >= NUM_VALUE_2) { |
| 637 | - constexpr int64_t alignElements = 8; // 8 * 4 = 32 bytes | 640 | + constexpr int64_t alignElements = NUM_VALUE_8; // 8 * 4 = 32 bytes |
| 638 | - size_t lastDimIndex = tmpValidShape.size() - 2; | 641 | + size_t lastDimIndex = tmpValidShape.size() - NUM_VALUE_2; |
| 639 | tmpValidShape[lastDimIndex] = (tmpValidShape[lastDimIndex] + alignElements - 1) / alignElements * | 642 | tmpValidShape[lastDimIndex] = (tmpValidShape[lastDimIndex] + alignElements - 1) / alignElements * |
| 640 | alignElements; | 643 | alignElements; |
| 641 | } | 644 | } |
| @@ -719,9 +722,9 @@ void OnlineSoftmaxInferFunc(Operation* op, std::vector<std::vector<SymbolicScala | |||
| 719 | outValidShapes.push_back(columnStatisticValidShape); | 722 | outValidShapes.push_back(columnStatisticValidShape); |
| 720 | outValidShapes.push_back(columnStatisticValidShape); | 723 | outValidShapes.push_back(columnStatisticValidShape); |
| 721 | outValidShapes.push_back(scoresValidShape); | 724 | outValidShapes.push_back(scoresValidShape); |
| 722 | - auto reduceWorkspaceValidShape = op->GetOOperands()[4]->GetDynValidShape(); | 725 | + auto reduceWorkspaceValidShape = op->GetOOperands()[NUM_VALUE_4]->GetDynValidShape(); |
| 723 | if (reduceWorkspaceValidShape.empty()) { | 726 | if (reduceWorkspaceValidShape.empty()) { |
| 724 | - reduceWorkspaceValidShape = SymbolicScalar::FromConcrete(op->GetOOperands()[4]->GetShape()); | 727 | + reduceWorkspaceValidShape = SymbolicScalar::FromConcrete(op->GetOOperands()[NUM_VALUE_4]->GetShape()); |
| 725 | } | 728 | } |
| 726 | outValidShapes.push_back(reduceWorkspaceValidShape); | 729 | outValidShapes.push_back(reduceWorkspaceValidShape); |
| 727 | } | 730 | } |
| @@ -729,13 +732,13 @@ void OnlineSoftmaxInferFunc(Operation* op, std::vector<std::vector<SymbolicScala | |||
| 729 | void OnlineSoftmaxUpdateInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 732 | void OnlineSoftmaxUpdateInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 730 | { | 733 | { |
| 731 | auto statisticValidShape = op->GetIOperands()[0]->GetDynValidShape(); | 734 | auto statisticValidShape = op->GetIOperands()[0]->GetDynValidShape(); |
| 732 | - auto outputValidShape = op->GetIOperands()[2]->GetDynValidShape(); | 735 | + auto outputValidShape = op->GetIOperands()[NUM_VALUE_2]->GetDynValidShape(); |
| 733 | outValidShapes.push_back(statisticValidShape); | 736 | outValidShapes.push_back(statisticValidShape); |
| 734 | outValidShapes.push_back(statisticValidShape); | 737 | outValidShapes.push_back(statisticValidShape); |
| 735 | outValidShapes.push_back(outputValidShape); | 738 | outValidShapes.push_back(outputValidShape); |
| 736 | - auto updateWorkspaceValidShape = op->GetOOperands()[3]->GetDynValidShape(); | 739 | + auto updateWorkspaceValidShape = op->GetOOperands()[NUM_VALUE_3]->GetDynValidShape(); |
| 737 | if (updateWorkspaceValidShape.empty()) { | 740 | if (updateWorkspaceValidShape.empty()) { |
| 738 | - updateWorkspaceValidShape = SymbolicScalar::FromConcrete(op->GetOOperands()[3]->GetShape()); | 741 | + updateWorkspaceValidShape = SymbolicScalar::FromConcrete(op->GetOOperands()[NUM_VALUE_3]->GetShape()); |
| 739 | } | 742 | } |
| 740 | outValidShapes.push_back(updateWorkspaceValidShape); | 743 | outValidShapes.push_back(updateWorkspaceValidShape); |
| 741 | } | 744 | } |
| @@ -783,7 +786,7 @@ REGISTER_INFER_SHAPE_FUNC(OP_GATHER, Opcode::OP_GATHER, InferFunc4Gather); | |||
| 783 | void InferFuncGatherInL1(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 786 | void InferFuncGatherInL1(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 784 | { | 787 | { |
| 785 | auto iOperands = op->GetIOperands(); | 788 | auto iOperands = op->GetIOperands(); |
| 786 | - ASSERT(MatmulErrorCode::ERR_PARAM_MISMATCH, iOperands.size() == 3) << "iOperands.size() should be 3"; | 789 | + ASSERT(MatmulErrorCode::ERR_PARAM_MISMATCH, iOperands.size() == NUM_VALUE_3) << "iOperands.size() should be 3"; |
| 787 | auto srcValidShape = iOperands[0]->GetDynValidShape(); | 790 | auto srcValidShape = iOperands[0]->GetDynValidShape(); |
| 788 | auto offsetValidShape = iOperands[1]->GetDynValidShape(); | 791 | auto offsetValidShape = iOperands[1]->GetDynValidShape(); |
| 789 | auto srcStartColumnOffset = op->GetIntAttribute(OpAttributeKey::startOffset); | 792 | auto srcStartColumnOffset = op->GetIntAttribute(OpAttributeKey::startOffset); |
| @@ -805,7 +808,7 @@ REGISTER_INFER_SHAPE_FUNC(OP_GATHER_IN_L1, Opcode::OP_GATHER_IN_L1, InferFuncGat | |||
| 805 | void InferFuncGatherInUB(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 808 | void InferFuncGatherInUB(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 806 | { | 809 | { |
| 807 | auto iOperands = op->GetIOperands(); | 810 | auto iOperands = op->GetIOperands(); |
| 808 | - ASSERT(VectorErrorCode::ERR_PARAM_COUNT_INVALID, iOperands.size() == 3) << "iOperands.size() should be 3"; | 811 | + ASSERT(VectorErrorCode::ERR_PARAM_COUNT_INVALID, iOperands.size() == NUM_VALUE_3) << "iOperands.size() should be 3"; |
| 809 | auto srcValidShape = iOperands[0]->GetDynValidShape(); | 812 | auto srcValidShape = iOperands[0]->GetDynValidShape(); |
| 810 | auto indicesValidShape = iOperands[1]->GetDynValidShape(); | 813 | auto indicesValidShape = iOperands[1]->GetDynValidShape(); |
| 811 | ASSERT(VectorErrorCode::ERR_PARAM_COUNT_INVALID, op->GetOOperands().size() == 1) | 814 | ASSERT(VectorErrorCode::ERR_PARAM_COUNT_INVALID, op->GetOOperands().size() == 1) |
| @@ -1605,9 +1608,9 @@ const std::string TOPK_ALGO = OP_ATTR_PREFIX + "algo"; | |||
| 1605 | const std::string TOPK_KVALUE = OP_ATTR_PREFIX + "kvalue"; | 1608 | const std::string TOPK_KVALUE = OP_ATTR_PREFIX + "kvalue"; |
| 1606 | const std::string EXTRACT_MASKMODE = OP_ATTR_PREFIX + "makeMode"; | 1609 | const std::string EXTRACT_MASKMODE = OP_ATTR_PREFIX + "makeMode"; |
| 1607 | const std::string SORT_AXIS = OP_ATTR_PREFIX + "axis"; | 1610 | const std::string SORT_AXIS = OP_ATTR_PREFIX + "axis"; |
| 1608 | -constexpr int32_t blockSize = 32; | 1611 | +constexpr int32_t blockSize = NUM_VALUE_32; |
| 1609 | -constexpr int32_t kFactorSize = 4; | 1612 | +constexpr int32_t kFactorSize = NUM_VALUE_4; |
| 1610 | -constexpr int32_t kBlockFpNum = 8; | 1613 | +constexpr int32_t kBlockFpNum = NUM_VALUE_8; |
| 1611 | 1614 | ||
| 1612 | // m,n -> m,4*n align32 | 1615 | // m,n -> m,4*n align32 |
| 1613 | void BitSortFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 1616 | void BitSortFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| @@ -1692,10 +1695,10 @@ REGISTER_INFER_SHAPE_FUNC(OP_EXTRACT, Opcode::OP_EXTRACT, ExtractFunc); | |||
| 1692 | 1695 | ||
| 1693 | void QuantMXInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 1696 | void QuantMXInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 1694 | { | 1697 | { |
| 1695 | - constexpr int64_t kQuantMXGroupSize = 32; | 1698 | + constexpr int64_t kQuantMXGroupSize = NUM_VALUE_32; |
| 1696 | - constexpr int64_t kQuantMXScaleGroupSize = 64; | 1699 | + constexpr int64_t kQuantMXScaleGroupSize = NUM_VALUE_64; |
| 1697 | - constexpr int64_t kQuantMXScalePairSize = 2; | 1700 | + constexpr int64_t kQuantMXScalePairSize = NUM_VALUE_2; |
| 1698 | - constexpr int64_t kQuantMXFp32ScalingFactor = 2; | 1701 | + constexpr int64_t kQuantMXFp32ScalingFactor = NUM_VALUE_2; |
| 1699 | std::vector<std::vector<SymbolicScalar>> inputValidShapes; | 1702 | std::vector<std::vector<SymbolicScalar>> inputValidShapes; |
| 1700 | for (auto inputTensor : op->GetIOperands()) { | 1703 | for (auto inputTensor : op->GetIOperands()) { |
| 1701 | inputValidShapes.push_back(inputTensor->GetDynValidShape()); | 1704 | inputValidShapes.push_back(inputTensor->GetDynValidShape()); |
| @@ -1708,13 +1711,15 @@ void QuantMXInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& o | |||
| 1708 | if (axis < 0) { | 1711 | if (axis < 0) { |
| 1709 | axis += static_cast<int64_t>(inputValidShape.size()); | 1712 | axis += static_cast<int64_t>(inputValidShape.size()); |
| 1710 | } | 1713 | } |
| 1711 | - const bool isDnAxis = inputValidShape.size() >= 2 && axis == static_cast<int64_t>(inputValidShape.size()) - 2; | 1714 | + const bool isDnAxis = inputValidShape.size() >= NUM_VALUE_2 && |
| 1715 | + axis == static_cast<int64_t>(inputValidShape.size()) - NUM_VALUE_2; | ||
| 1712 | if (isDnAxis) { | 1716 | if (isDnAxis) { |
| 1713 | auto groupedValidShape = inputValidShape; | 1717 | auto groupedValidShape = inputValidShape; |
| 1714 | - groupedValidShape[groupedValidShape.size() - 2] = groupedValidShape[groupedValidShape.size() - 2] / | 1718 | + groupedValidShape[groupedValidShape.size() - |
| 1715 | - kQuantMXGroupSize; | 1719 | + NUM_VALUE_2] = groupedValidShape[groupedValidShape.size() - NUM_VALUE_2] / kQuantMXGroupSize; |
| 1716 | auto expValidShape = inputValidShape; | 1720 | auto expValidShape = inputValidShape; |
| 1717 | - expValidShape[expValidShape.size() - 2] = expValidShape[expValidShape.size() - 2] / kQuantMXScaleGroupSize; | 1721 | + expValidShape[expValidShape.size() - NUM_VALUE_2] = expValidShape[expValidShape.size() - NUM_VALUE_2] / |
| 1722 | + kQuantMXScaleGroupSize; | ||
| 1718 | expValidShape.back() = expValidShape.back() * kQuantMXScalePairSize; | 1723 | expValidShape.back() = expValidShape.back() * kQuantMXScalePairSize; |
| 1719 | outValidShapes.push_back(inputValidShape); | 1724 | outValidShapes.push_back(inputValidShape); |
| 1720 | outValidShapes.push_back(expValidShape); | 1725 | outValidShapes.push_back(expValidShape); |
| @@ -1727,10 +1732,10 @@ void QuantMXInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& o | |||
| 1727 | groupedValidShape = {(inputValidShape[0] + kQuantMXGroupSize - 1) / kQuantMXGroupSize}; | 1732 | groupedValidShape = {(inputValidShape[0] + kQuantMXGroupSize - 1) / kQuantMXGroupSize}; |
| 1728 | } else { | 1733 | } else { |
| 1729 | groupedValidShape.reserve(inputValidShape.size() - 1); | 1734 | groupedValidShape.reserve(inputValidShape.size() - 1); |
| 1730 | - for (size_t i = 0; i + 2 < inputValidShape.size(); ++i) { | 1735 | + for (size_t i = 0; i + NUM_VALUE_2 < inputValidShape.size(); ++i) { |
| 1731 | groupedValidShape.push_back(inputValidShape[i]); | 1736 | groupedValidShape.push_back(inputValidShape[i]); |
| 1732 | } | 1737 | } |
| 1733 | - groupedValidShape.push_back(inputValidShape[inputValidShape.size() - 2] * | 1738 | + groupedValidShape.push_back(inputValidShape[inputValidShape.size() - NUM_VALUE_2] * |
| 1734 | ((inputValidShape.back() + kQuantMXGroupSize - 1) / kQuantMXGroupSize)); | 1739 | ((inputValidShape.back() + kQuantMXGroupSize - 1) / kQuantMXGroupSize)); |
| 1735 | } | 1740 | } |
| 1736 | std::vector<SymbolicScalar> expValidShape = groupedValidShape; | 1741 | std::vector<SymbolicScalar> expValidShape = groupedValidShape; |
| @@ -1757,7 +1762,7 @@ void RadixSelectFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& ou | |||
| 1757 | res.back() = op->GetIntAttribute(TOPK_KVALUE); | 1762 | res.back() = op->GetIntAttribute(TOPK_KVALUE); |
| 1758 | outValidShapes.push_back(res); | 1763 | outValidShapes.push_back(res); |
| 1759 | outValidShapes.push_back(res); | 1764 | outValidShapes.push_back(res); |
| 1760 | - auto output = op->GetOOperands()[2]; | 1765 | + auto output = op->GetOOperands()[NUM_VALUE_2]; |
| 1761 | if (output->GetDynValidShape().empty()) { | 1766 | if (output->GetDynValidShape().empty()) { |
| 1762 | auto immShape = OpImmediate::Specified(output->GetShape()); | 1767 | auto immShape = OpImmediate::Specified(output->GetShape()); |
| 1763 | std::vector<SymbolicScalar> validShape; | 1768 | std::vector<SymbolicScalar> validShape; |
| @@ -1836,16 +1841,16 @@ void ExtractSingleFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& | |||
| 1836 | return; | 1841 | return; |
| 1837 | } | 1842 | } |
| 1838 | std::vector<SymbolicScalar> res(inputValidShapes[0]); | 1843 | std::vector<SymbolicScalar> res(inputValidShapes[0]); |
| 1839 | - res.back() = res.back() / 2; | 1844 | + res.back() = res.back() / NUM_VALUE_2; |
| 1840 | outValidShapes.push_back(res); | 1845 | outValidShapes.push_back(res); |
| 1841 | } | 1846 | } |
| 1842 | REGISTER_INFER_SHAPE_FUNC(OP_EXTRACT_SINGLE, Opcode::OP_EXTRACT_SINGLE, ExtractSingleFunc); | 1847 | REGISTER_INFER_SHAPE_FUNC(OP_EXTRACT_SINGLE, Opcode::OP_EXTRACT_SINGLE, ExtractSingleFunc); |
| 1843 | 1848 | ||
| 1844 | void PReLUInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 1849 | void PReLUInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 1845 | { | 1850 | { |
| 1846 | - ASSERT(VectorErrorCode::ERR_PARAM_COUNT_INVALID, op->GetIOperands().size() == 2) | 1851 | + ASSERT(VectorErrorCode::ERR_PARAM_COUNT_INVALID, op->GetIOperands().size() == NUM_VALUE_2) |
| 1847 | << "PReLU input operand size should be 2"; | 1852 | << "PReLU input operand size should be 2"; |
| 1848 | - ASSERT(VectorErrorCode::ERR_PARAM_COUNT_INVALID, op->GetOOperands().size() == 2) | 1853 | + ASSERT(VectorErrorCode::ERR_PARAM_COUNT_INVALID, op->GetOOperands().size() == NUM_VALUE_2) |
| 1849 | << "PReLU output operand size should be 2"; | 1854 | << "PReLU output operand size should be 2"; |
| 1850 | 1855 | ||
| 1851 | auto input0 = op->GetIOperands()[0]; | 1856 | auto input0 = op->GetIOperands()[0]; |
| @@ -1855,10 +1860,10 @@ void PReLUInferFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& out | |||
| 1855 | std::vector<SymbolicScalar> output1ValidShape; | 1860 | std::vector<SymbolicScalar> output1ValidShape; |
| 1856 | auto input0ShapeDim = input0->GetDynValidShape().size(); | 1861 | auto input0ShapeDim = input0->GetDynValidShape().size(); |
| 1857 | 1862 | ||
| 1858 | - if (input0ShapeDim == 2) { | 1863 | + if (input0ShapeDim == NUM_VALUE_2) { |
| 1859 | output1ValidShape.emplace_back(input0->GetDynValidShape().back()); | 1864 | output1ValidShape.emplace_back(input0->GetDynValidShape().back()); |
| 1860 | } else { | 1865 | } else { |
| 1861 | - constexpr int64_t ALIGN_SIZE = 32; | 1866 | + constexpr int64_t ALIGN_SIZE = NUM_VALUE_32; |
| 1862 | int64_t elementCount = ALIGN_SIZE / BytesOf(input0->Datatype()); | 1867 | int64_t elementCount = ALIGN_SIZE / BytesOf(input0->Datatype()); |
| 1863 | output1ValidShape.emplace_back(elementCount); | 1868 | output1ValidShape.emplace_back(elementCount); |
| 1864 | } | 1869 | } |
| @@ -419,7 +419,7 @@ void DebugJson(const Json& j) | |||
| 419 | { | 419 | { |
| 420 | constexpr int32_t dumpLen = 2; | 420 | constexpr int32_t dumpLen = 2; |
| 421 | std::string s = j.dump(dumpLen); | 421 | std::string s = j.dump(dumpLen); |
| 422 | - printf("%s\n", s.c_str()); | 422 | + PYPTO_LOGD("%s", s.c_str()); |
| 423 | } | 423 | } |
| 424 | 424 | ||
| 425 | void Operation::DumpOperandsJson(Json& opDump, bool dumpTensor) const | 425 | void Operation::DumpOperandsJson(Json& opDump, bool dumpTensor) const |
| @@ -44,7 +44,9 @@ constexpr int32_t NUM_VALUE_3 = 3; | |||
| 44 | constexpr int32_t NUM_VALUE_4 = 4; | 44 | constexpr int32_t NUM_VALUE_4 = 4; |
| 45 | constexpr int32_t NUM_VALUE_5 = 5; | 45 | constexpr int32_t NUM_VALUE_5 = 5; |
| 46 | constexpr int32_t NUM_VALUE_6 = 6; | 46 | constexpr int32_t NUM_VALUE_6 = 6; |
| 47 | +constexpr int32_t NUM_VALUE_7 = 7; | ||
| 47 | constexpr int32_t NUM_VALUE_8 = 8; | 48 | constexpr int32_t NUM_VALUE_8 = 8; |
| 49 | +constexpr int32_t NUM_VALUE_9 = 9; | ||
| 48 | constexpr int32_t NUM_VALUE_10 = 10; | 50 | constexpr int32_t NUM_VALUE_10 = 10; |
| 49 | constexpr int32_t NUM_VALUE_16 = 16; | 51 | constexpr int32_t NUM_VALUE_16 = 16; |
| 50 | constexpr int32_t NUM_VALUE_26 = 26; | 52 | constexpr int32_t NUM_VALUE_26 = 26; |
| @@ -19,7 +19,9 @@ | |||
| 19 | 19 | ||
| 20 | #include "interface/utils/error.h" | 20 | #include "interface/utils/error.h" |
| 21 | #include "interface/operation/opcode.h" | 21 | #include "interface/operation/opcode.h" |
| 22 | +#include "interface/operation/operation_common.h" | ||
| 22 | #include "interface/operation/operation_impl.h" | 23 | #include "interface/operation/operation_impl.h" |
| 24 | +#include "interface/operation/vector/gather_mask_common.h" | ||
| 23 | #include "interface/tensor/logical_tensor.h" | 25 | #include "interface/tensor/logical_tensor.h" |
| 24 | 26 | ||
| 25 | namespace npu::tile_fwk { | 27 | namespace npu::tile_fwk { |
| @@ -613,11 +615,12 @@ TileShape TileShapeResolver::GetOutputTileShape(const Operation& op, int index) | |||
| 613 | // Last axis tile shrinks by patternMode: /2 for mode 1/2, /4 for mode 3-6, else full min; | 615 | // Last axis tile shrinks by patternMode: /2 for mode 1/2, /4 for mode 3-6, else full min; |
| 614 | // other axes min(outShape, vecTile). (TiledGatherMaskBuildIn.) | 616 | // other axes min(outShape, vecTile). (TiledGatherMaskBuildIn.) |
| 615 | uint8_t patternMode = static_cast<uint8_t>(op.GetIntAttribute(OP_ATTR_PREFIX + "patternMode")); | 617 | uint8_t patternMode = static_cast<uint8_t>(op.GetIntAttribute(OP_ATTR_PREFIX + "patternMode")); |
| 616 | - int64_t divisor = 1; | 618 | + const auto pattern = static_cast<GatherMaskPattern>(patternMode); |
| 617 | - if (patternMode == 1 || patternMode == 2) { | 619 | + int64_t divisor = NUM_VALUE_1; |
| 618 | - divisor = 2; | 620 | + if (IsHalfGatherMaskPattern(pattern)) { |
| 619 | - } else if (patternMode >= 3 && patternMode <= 6) { | 621 | + divisor = NUM_VALUE_2; |
| 620 | - divisor = 4; | 622 | + } else if (IsQuarterGatherMaskPattern(pattern)) { |
| 623 | + divisor = NUM_VALUE_4; | ||
| 621 | } | 624 | } |
| 622 | std::vector<int64_t> tile(outShape.size()); | 625 | std::vector<int64_t> tile(outShape.size()); |
| 623 | for (size_t a = 0; a < outShape.size(); ++a) { | 626 | for (size_t a = 0; a < outShape.size(); ++a) { |
| @@ -0,0 +1,164 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file axpy.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | +#include "tilefwk/data_type.h" | ||
| 16 | +#include "unary.h" | ||
| 17 | +#include "binary.h" | ||
| 18 | +#include "tensor_transformation.h" | ||
| 19 | +#include "interface/utils/operator_tracer.h" | ||
| 20 | +#include "interface/configs/config_manager.h" | ||
| 21 | +#include "tilefwk/error_code.h" | ||
| 22 | +#include "passes/tile_graph_pass/graph_constraint/axis_combine.h" | ||
| 23 | + | ||
| 24 | +namespace npu::tile_fwk { | ||
| 25 | + | ||
| 26 | +void TiledAxpyOperation(Function& function, const TileShape& tileShape, size_t cur, LogicalInput& inputSelf, | ||
| 27 | + LogicalInput& inputOther, const Element& alpha, const LogicalTensorPtr& result, | ||
| 28 | + TileInfo& resultTileInfo) | ||
| 29 | +{ | ||
| 30 | + size_t shapeSize = inputSelf.tensor->GetShape().size(); | ||
| 31 | + if (cur == shapeSize) { | ||
| 32 | + auto selfTile = inputSelf.tensor->View(function, inputSelf.tileInfo.shape, inputSelf.tileInfo.offset); | ||
| 33 | + auto otherTile = inputOther.tensor->View(function, inputOther.tileInfo.shape, inputOther.tileInfo.offset); | ||
| 34 | + auto resultTile = result->View(function, resultTileInfo.shape, resultTileInfo.offset); | ||
| 35 | + | ||
| 36 | + auto& op = function.AddOperation(Opcode::OP_AXPY, {selfTile, otherTile}, {resultTile}); | ||
| 37 | + op.SetAttribute(OpAttributeKey::scalar, alpha); | ||
| 38 | + std::vector<int64_t> brcOperand(shapeSize, 0); | ||
| 39 | + size_t brcAxesCount = 0; | ||
| 40 | + for (size_t i = 0; i < shapeSize; i++) { | ||
| 41 | + int brcResult = BrcAxisBinaryOp(inputSelf.tensor, inputOther.tensor, i); | ||
| 42 | + brcOperand[i] = (brcResult == 1) ? 0 : brcResult; | ||
| 43 | + if (brcOperand[i] != 0) { | ||
| 44 | + brcAxesCount++; | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + if (brcOperand[shapeSize - 1] != 0) { | ||
| 48 | + op.SetAttribute(OpAttributeKey::excludeBufferReuse, true); | ||
| 49 | + op.SetAttribute(OpAttributeKey::brcbIdx, brcOperand[shapeSize - 1]); | ||
| 50 | + } | ||
| 51 | + if (brcAxesCount > 0) { | ||
| 52 | + op.SetAttribute(OpAttributeKey::brcOperand, brcOperand); | ||
| 53 | + } | ||
| 54 | + return; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 58 | + for (int i = 0; i < result->shape[cur]; i += vecTile[cur]) { | ||
| 59 | + resultTileInfo.offset[cur] = i; | ||
| 60 | + resultTileInfo.shape[cur] = std::min(result->shape[cur] - resultTileInfo.offset[cur], vecTile[cur]); | ||
| 61 | + inputSelf.tileInfo.offset[cur] = i % inputSelf.tensor->GetShape()[cur]; | ||
| 62 | + inputSelf.tileInfo.shape[cur] = std::min(inputSelf.tensor->GetShape()[cur] - inputSelf.tileInfo.offset[cur], | ||
| 63 | + vecTile[cur]); | ||
| 64 | + inputOther.tileInfo.offset[cur] = i % inputOther.tensor->GetShape()[cur]; | ||
| 65 | + inputOther.tileInfo.shape[cur] = std::min(inputOther.tensor->GetShape()[cur] - inputOther.tileInfo.offset[cur], | ||
| 66 | + vecTile[cur]); | ||
| 67 | + TiledAxpyOperation(function, tileShape, cur + 1, inputSelf, inputOther, alpha, result, resultTileInfo); | ||
| 68 | + } | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +void TiledAxpyOperation(Function& function, const TileShape& tileShape, LogicalTensorPtr self, LogicalTensorPtr other, | ||
| 72 | + const Element& alpha, const LogicalTensorPtr& result) | ||
| 73 | +{ | ||
| 74 | + CheckBinOpOperandsValid(self, other); | ||
| 75 | + BroadcastOperandTensor(other, self, result, function, tileShape); | ||
| 76 | + | ||
| 77 | + TileInfo selfTileInfo(self->shape.size(), self->offset.size()); | ||
| 78 | + TileInfo otherTileInfo(other->shape.size(), other->offset.size()); | ||
| 79 | + TileInfo resultTileInfo(result->shape.size(), result->offset.size()); | ||
| 80 | + auto inputSelf = LogicalInput{self, selfTileInfo}; | ||
| 81 | + auto inputOther = LogicalInput{other, otherTileInfo}; | ||
| 82 | + | ||
| 83 | + TiledAxpyOperation(function, tileShape, 0, inputSelf, inputOther, alpha, result, resultTileInfo); | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +void AxpyOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 87 | + const std::vector<LogicalTensorPtr>& iOperand, const std::vector<LogicalTensorPtr>& oOperand, | ||
| 88 | + const Operation& op) | ||
| 89 | +{ | ||
| 90 | + auto alpha = op.GetElementAttribute(OpAttributeKey::scalar); | ||
| 91 | + TiledAxpyOperation(function, tileShape, iOperand[0], iOperand[1], alpha, oOperand[0]); | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +LogicalTensorPtr TensorAxpyOperation(Function& function, const Tensor& self, const Tensor& other, float alpha) | ||
| 95 | +{ | ||
| 96 | + auto selfTensor = self.GetStorage(); | ||
| 97 | + auto otherTensor = other.GetStorage(); | ||
| 98 | + | ||
| 99 | + if (selfTensor->shape.size() != otherTensor->shape.size()) { | ||
| 100 | + std::vector<int> broadCastShape = GetBroadCastShape(selfTensor, otherTensor); | ||
| 101 | + selfTensor = BinaryOperationBroadCast(selfTensor, broadCastShape); | ||
| 102 | + otherTensor = BinaryOperationBroadCast(otherTensor, broadCastShape); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + CheckTensorShapeSize(selfTensor, "AXPY"); | ||
| 106 | + CheckTensorShapeSize(otherTensor, "AXPY"); | ||
| 107 | + CheckBinOpOperandsValid(selfTensor, otherTensor); | ||
| 108 | + CheckTensorsFormatConsistency(selfTensor, otherTensor, "AXPY"); | ||
| 109 | + | ||
| 110 | + std::vector<SymbolicScalar> resultValidShape; | ||
| 111 | + std::vector<int64_t> resultShape = BinaryOperationResultShape(selfTensor, otherTensor); | ||
| 112 | + size_t shapeSize = resultShape.size(); | ||
| 113 | + if ((!selfTensor->GetDynValidShape().empty()) && (!otherTensor->GetDynValidShape().empty())) { | ||
| 114 | + for (size_t i = 0; i < shapeSize; ++i) { | ||
| 115 | + if (resultShape[i] == selfTensor->shape[i]) { | ||
| 116 | + resultValidShape.push_back(selfTensor->GetDynValidShape()[i]); | ||
| 117 | + } else { | ||
| 118 | + resultValidShape.push_back(otherTensor->GetDynValidShape()[i]); | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | + // AXPY: y = alpha * x + y, y is in-place updated, cannot broadcast | ||
| 123 | + // Validate: if any dimension of y is 1 but x is not 1, it's invalid | ||
| 124 | + for (size_t i = 0; i < shapeSize; i++) { | ||
| 125 | + if ((selfTensor->shape[i] == 1) && (otherTensor->shape[i] != 1)) { | ||
| 126 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) | ||
| 127 | + << "AXPY: self tensor cannot broadcast, self.shape[" << i << "]=" << selfTensor->shape[i] | ||
| 128 | + << " but other.shape[" << i << "]=" << otherTensor->shape[i]; | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + auto result = std::make_shared<LogicalTensor>(function, selfTensor->Datatype(), resultShape, resultValidShape, | ||
| 133 | + selfTensor->Format()); | ||
| 134 | + auto& op = function.AddOperation(Opcode::OP_AXPY, {selfTensor, otherTensor}, {result}); | ||
| 135 | + op.SetAttribute(OpAttributeKey::scalar, Element(selfTensor->Datatype(), alpha)); | ||
| 136 | + std::map<int, int> inplaceInfo = {{0, 0}}; | ||
| 137 | + op.SetAttr(OpAttributeKey::inplaceInfo, inplaceInfo); | ||
| 138 | + | ||
| 139 | + return result; | ||
| 140 | +} | ||
| 141 | + | ||
| 142 | +Tensor Axpy(const Tensor& self, const Tensor& other, float alpha) | ||
| 143 | +{ | ||
| 144 | + DECLARE_TRACER(); | ||
| 145 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Axpy"); | ||
| 146 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Axpy"); | ||
| 147 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "AXPY"); | ||
| 148 | + CheckTensorDimRange(other.GetStorage(), 1, NUM_VALUE_4, "AXPY"); | ||
| 149 | + | ||
| 150 | + auto selfDtype = self.GetDataType(); | ||
| 151 | + auto otherDtype = other.GetDataType(); | ||
| 152 | + if (selfDtype == otherDtype) { | ||
| 153 | + std::unordered_set<DataType> supportedTypes = {DT_FP32, DT_FP16}; | ||
| 154 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "AXPY"); | ||
| 155 | + } else { | ||
| 156 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, selfDtype == DT_FP32 && otherDtype == DT_FP16) | ||
| 157 | + << "AXPY: when dtype mismatch, only dst(y)=fp32 with src(x)=fp16 is supported."; | ||
| 158 | + } | ||
| 159 | + RETURN_CALL(AxpyOperation, *Program::GetInstance().GetCurrentFunction(), self, other, alpha); | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +REGISTER_OPERATION_TILED_FUNC(OP_AXPY, Opcode::OP_AXPY, AxpyOperationTileFunc); | ||
| 163 | + | ||
| 164 | +} // namespace npu::tile_fwk | ||
| @@ -228,6 +228,7 @@ int BrcAxisBinaryOp(LogicalTensorPtr operand1, LogicalTensorPtr operand2, int64_ | |||
| 228 | void CheckBinOpOperandsValid(const LogicalTensorPtr& operand1, const LogicalTensorPtr& operand2); | 228 | void CheckBinOpOperandsValid(const LogicalTensorPtr& operand1, const LogicalTensorPtr& operand2); |
| 229 | void BinaryOperationOperandCheck(const std::vector<LogicalTensorPtr>& iOperand, | 229 | void BinaryOperationOperandCheck(const std::vector<LogicalTensorPtr>& iOperand, |
| 230 | const std::vector<LogicalTensorPtr>& oOperand); | 230 | const std::vector<LogicalTensorPtr>& oOperand); |
| 231 | +int64_t GetOperationPrecisionType(const Operation& op); | ||
| 231 | void BroadcastOperandTensor(LogicalTensorPtr& operand, LogicalTensorPtr& other, LogicalTensorPtr result, | 232 | void BroadcastOperandTensor(LogicalTensorPtr& operand, LogicalTensorPtr& other, LogicalTensorPtr result, |
| 232 | Function& function, const TileShape& tileShape, std::vector<int64_t> dstShape = {}); | 233 | Function& function, const TileShape& tileShape, std::vector<int64_t> dstShape = {}); |
| 233 | 234 | ||
| @@ -0,0 +1,159 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file binary_math.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | +#include "tilefwk/data_type.h" | ||
| 16 | +#include "unary.h" | ||
| 17 | +#include "binary.h" | ||
| 18 | +#include "binary_tiled.h" | ||
| 19 | +#include "tensor_transformation.h" | ||
| 20 | +#include "interface/utils/operator_tracer.h" | ||
| 21 | +#include "interface/configs/config_manager.h" | ||
| 22 | +#include "tilefwk/error_code.h" | ||
| 23 | +#include "passes/tile_graph_pass/graph_constraint/axis_combine.h" | ||
| 24 | + | ||
| 25 | +namespace npu::tile_fwk { | ||
| 26 | + | ||
| 27 | +DataType GetPowRealResultDataType(DataType selfType, DataType otherType) | ||
| 28 | +{ | ||
| 29 | + if (selfType == DT_INT32) { | ||
| 30 | + return otherType; | ||
| 31 | + } | ||
| 32 | + if (otherType == DT_INT32) { | ||
| 33 | + return selfType; | ||
| 34 | + } | ||
| 35 | + if (selfType == DT_BF16) { | ||
| 36 | + return otherType == DT_FP16 ? DT_FP32 : otherType; | ||
| 37 | + } | ||
| 38 | + if (otherType == DT_BF16) { | ||
| 39 | + return selfType == DT_FP16 ? DT_FP32 : selfType; | ||
| 40 | + } | ||
| 41 | + return selfType == DT_FP16 && otherType == DT_FP16 ? DT_FP16 : DT_FP32; | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +DataType GetPowCalcResultDataType(DataType selfType, DataType otherType) | ||
| 45 | +{ | ||
| 46 | + if (selfType == DT_INT32 && otherType == DT_INT32) { | ||
| 47 | + return DT_INT32; | ||
| 48 | + } | ||
| 49 | + return DT_FP32; | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +LogicalTensorPtr CastToResultType(const LogicalTensorPtr& tensor, DataType originType, DataType resultType) | ||
| 53 | +{ | ||
| 54 | + if (originType == resultType) { | ||
| 55 | + return tensor; | ||
| 56 | + } | ||
| 57 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), tensor, resultType, | ||
| 58 | + CastMode::CAST_NONE); | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +void PowCheck(const Tensor& self, const Tensor& other) | ||
| 62 | +{ | ||
| 63 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_INT32, DT_FP32, DT_INT8, DT_UINT8, DT_INT16}; | ||
| 64 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "POW"); | ||
| 65 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "POW"); | ||
| 66 | + CheckTensorShapeSize(self.GetStorage(), "POW"); | ||
| 67 | + CheckTensorShapeSize(other.GetStorage(), "POW"); | ||
| 68 | + CheckTensorsDimConsistency({self.GetStorage(), other.GetStorage()}, "POW"); | ||
| 69 | + CheckTensorsShapeConsistencyOrBroadcast({self.GetStorage(), other.GetStorage()}, "POW"); | ||
| 70 | + CheckTensorsFormatConsistency(self.GetStorage(), other.GetStorage(), "POW"); | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +Tensor Pow(const Tensor& self, const Tensor& other, PrecisionType precisionType) | ||
| 74 | +{ | ||
| 75 | + DECLARE_TRACER(); | ||
| 76 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Pow"); | ||
| 77 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Pow"); | ||
| 78 | + | ||
| 79 | + PowCheck(self, other); | ||
| 80 | + DataType selfType = self.GetDataType(); | ||
| 81 | + DataType otherType = other.GetDataType(); | ||
| 82 | + if (IsInteger(selfType) && IsInteger(otherType)) { | ||
| 83 | + CheckTensorsDataTypeConsistency(self.GetStorage(), other.GetStorage(), "Pow"); | ||
| 84 | + auto [result, op] = TensorBinaryOperationWithOp<BinaryOpType::POW>(*Program::GetInstance().GetCurrentFunction(), | ||
| 85 | + self.GetStorage(), other.GetStorage()); | ||
| 86 | + op->SetAttribute(OpAttributeKey::precisionType, static_cast<int64_t>(PrecisionType::INTRINSIC)); | ||
| 87 | + return result; | ||
| 88 | + } | ||
| 89 | + DataType realResultType = GetPowRealResultDataType(selfType, otherType); | ||
| 90 | + DataType calcResultType = GetPowCalcResultDataType(selfType, otherType); | ||
| 91 | + auto selfSt = CastToResultType(self.GetStorage(), selfType, calcResultType); | ||
| 92 | + auto otherSt = CastToResultType(other.GetStorage(), otherType, calcResultType); | ||
| 93 | + auto [result, op] = TensorBinaryOperationWithOp<BinaryOpType::POW>(*Program::GetInstance().GetCurrentFunction(), | ||
| 94 | + selfSt, otherSt); | ||
| 95 | + op->SetAttribute(OpAttributeKey::precisionType, static_cast<int64_t>(precisionType)); | ||
| 96 | + if (realResultType != calcResultType) { | ||
| 97 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), result, | ||
| 98 | + realResultType, CastMode::CAST_NONE); | ||
| 99 | + } | ||
| 100 | + return result; | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +Tensor Atan2(const Tensor& y, const Tensor& x) | ||
| 104 | +{ | ||
| 105 | + DECLARE_TRACER(); | ||
| 106 | + CheckTensorFormat(y.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Atan2"); | ||
| 107 | + CheckTensorFormat(x.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Atan2"); | ||
| 108 | + | ||
| 109 | + CheckTensorsDataTypeConsistency(y.GetStorage(), x.GetStorage(), "ATAN2"); | ||
| 110 | + std::unordered_set<DataType> supportedTypes = {DT_FP32, DT_FP16, DT_BF16}; | ||
| 111 | + CheckTensorDataType(y.GetStorage(), supportedTypes, "ATAN2"); | ||
| 112 | + auto castY = y.GetStorage(); | ||
| 113 | + auto castX = x.GetStorage(); | ||
| 114 | + DataType dataType = y.GetDataType(); | ||
| 115 | + if (dataType != DataType::DT_FP32) { | ||
| 116 | + castY = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), y.GetStorage(), | ||
| 117 | + DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 118 | + castX = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), x.GetStorage(), | ||
| 119 | + DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 120 | + } | ||
| 121 | + auto res = CALL(BinaryOperation<BinaryOpType::ATAN2>, *Program::GetInstance().GetCurrentFunction(), castY, castX); | ||
| 122 | + if (dataType != DataType::DT_FP32) { | ||
| 123 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), res, dataType, | ||
| 124 | + CastMode::CAST_NONE); | ||
| 125 | + } | ||
| 126 | + return res; | ||
| 127 | +} | ||
| 128 | + | ||
| 129 | +Tensor CopySign(const Tensor& self, const Tensor& other) | ||
| 130 | +{ | ||
| 131 | + DECLARE_TRACER(); | ||
| 132 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "CopySign"); | ||
| 133 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "CopySign"); | ||
| 134 | + | ||
| 135 | + CheckTensorsDataTypeConsistency(self.GetStorage(), other.GetStorage(), "COPYSIGN"); | ||
| 136 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_FP32}; | ||
| 137 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "COPYSIGN"); | ||
| 138 | + | ||
| 139 | + DataType selfDType = self.GetDataType(); | ||
| 140 | + DataType otherDType = other.GetDataType(); | ||
| 141 | + Tensor castSelf = self; | ||
| 142 | + Tensor castOther = other; | ||
| 143 | + if (selfDType == DT_INT16 || selfDType == DT_INT32) { | ||
| 144 | + castSelf = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), | ||
| 145 | + self.GetStorage(), DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 146 | + } | ||
| 147 | + if (otherDType == DT_INT16 || otherDType == DT_INT32) { | ||
| 148 | + castOther = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), | ||
| 149 | + other.GetStorage(), DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 150 | + } | ||
| 151 | + RETURN_CALL(BinaryOperation<BinaryOpType::COPYSIGN>, *Program::GetInstance().GetCurrentFunction(), castSelf, | ||
| 152 | + castOther); | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +REGISTER_OPERATION_TILED_FUNC(OP_POW, Opcode::OP_POW, BinaryOperationTileFunc<BinaryOpType::POW>); | ||
| 156 | +REGISTER_OPERATION_TILED_FUNC(OP_COPYSIGN, Opcode::OP_COPYSIGN, BinaryOperationTileFunc<BinaryOpType::COPYSIGN>); | ||
| 157 | +REGISTER_OPERATION_TILED_FUNC(OP_ATAN2, Opcode::OP_ATAN2, BinaryOperationTileFunc<BinaryOpType::ATAN2>); | ||
| 158 | + | ||
| 159 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,668 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file binary_scalar.cpp | ||
| 13 | + * \brief Binary scalar operation implementations. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include <limits> | ||
| 17 | +#include "tilefwk/data_type.h" | ||
| 18 | +#include "unary.h" | ||
| 19 | +#include "binary.h" | ||
| 20 | +#include "tensor_transformation.h" | ||
| 21 | +#include "interface/utils/operator_tracer.h" | ||
| 22 | +#include "tilefwk/error_code.h" | ||
| 23 | + | ||
| 24 | +namespace npu::tile_fwk { | ||
| 25 | + | ||
| 26 | +static Element CastScalarToDtype(const Element& scalar, DataType dtype) | ||
| 27 | +{ | ||
| 28 | + if (dtype == DT_INT64) { | ||
| 29 | + return Element(dtype, scalar.Cast<int64_t>()); | ||
| 30 | + } | ||
| 31 | + return Element(dtype, scalar.Cast<float>()); | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +static std::pair<int64_t, int64_t> GetIntegerScalarRange(DataType dtype) | ||
| 35 | +{ | ||
| 36 | + switch (dtype) { | ||
| 37 | + case DT_INT8: | ||
| 38 | + return {std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()}; | ||
| 39 | + case DT_INT16: | ||
| 40 | + return {std::numeric_limits<int16_t>::min(), std::numeric_limits<int16_t>::max()}; | ||
| 41 | + case DT_INT32: | ||
| 42 | + return {std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max()}; | ||
| 43 | + case DT_UINT8: | ||
| 44 | + return {0LL, static_cast<int64_t>(std::numeric_limits<uint8_t>::max())}; | ||
| 45 | + default: | ||
| 46 | + return {std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max()}; | ||
| 47 | + } | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +Tensor Gcd(const Tensor& self, const Element& other) | ||
| 51 | +{ | ||
| 52 | + DECLARE_TRACER(); | ||
| 53 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Gcd"); | ||
| 54 | + | ||
| 55 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "GCD"); | ||
| 56 | + std::unordered_set<DataType> supportedTypes = {DT_INT8, DT_INT16, DT_INT32, DT_UINT8}; | ||
| 57 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "GCD"); | ||
| 58 | + auto [elemMin, elemMax] = GetIntegerScalarRange(self.GetDataType()); | ||
| 59 | + if (other.GetSignedData() < elemMin || other.GetSignedData() > elemMax) { | ||
| 60 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) | ||
| 61 | + << "The value range for the element is incorrect! expected [" << elemMin << ", " << elemMax << "], got " | ||
| 62 | + << other.GetSignedData(); | ||
| 63 | + } | ||
| 64 | + return TensorBinaryOperationScalar<BinaryOpType::GCD>(*Program::GetInstance().GetCurrentFunction(), | ||
| 65 | + self.GetStorage(), other); | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +LogicalTensorPtr GenAllOneTensor(const Shape& shape, std::vector<SymbolicScalar> validShape, const DataType& dataType) | ||
| 69 | +{ | ||
| 70 | + auto result = CALL(FullOperation, *Program::GetInstance().GetCurrentFunction(), Element(DataType::DT_FP32, 1.0), | ||
| 71 | + SymbolicScalar(), DataType::DT_FP32, shape, validShape); | ||
| 72 | + if (dataType != DataType::DT_FP32) { | ||
| 73 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), result.GetStorage(), | ||
| 74 | + dataType, CastMode::CAST_NONE); | ||
| 75 | + } | ||
| 76 | + return result.GetStorage(); | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +LogicalTensorPtr PowSCalc(const LogicalTensorPtr& self, const Element& other, PrecisionType precisionType) | ||
| 80 | +{ | ||
| 81 | + double exponent = other.Cast<double>(); | ||
| 82 | + if (std::abs(exponent - NUM_VALUE_0_5) < NUM_VALUE_EPS) { // sqrt(x) | ||
| 83 | + RETURN_CALL(UnaryOperation<UnaryOpType::SQRT>, *Program::GetInstance().GetCurrentFunction(), self); | ||
| 84 | + } else if (std::abs(exponent + NUM_VALUE_0_5) < NUM_VALUE_EPS) { // 1 / sqrt(x) | ||
| 85 | + auto sqrt = CALL(UnaryOperation<UnaryOpType::SQRT>, *Program::GetInstance().GetCurrentFunction(), self); | ||
| 86 | + auto ones = GenAllOneTensor(self->shape, self->GetDynValidShape(), DT_FP32); | ||
| 87 | + auto [result, op] = TensorBinaryOperationWithOp<BinaryOpType::DIV>(*Program::GetInstance().GetCurrentFunction(), | ||
| 88 | + ones, sqrt); | ||
| 89 | + op->SetAttribute(OpAttributeKey::precisionType, static_cast<int64_t>(PrecisionType::HIGH_PRECISION)); | ||
| 90 | + return result; | ||
| 91 | + } else if (std::abs(exponent - NUM_VALUE_3) < NUM_VALUE_EPS) { // x * x * x | ||
| 92 | + auto mul = CALL(BinaryOperation<BinaryOpType::MUL>, *Program::GetInstance().GetCurrentFunction(), self, self); | ||
| 93 | + RETURN_CALL(BinaryOperation<BinaryOpType::MUL>, *Program::GetInstance().GetCurrentFunction(), mul, self); | ||
| 94 | + } else if (std::abs(exponent - NUM_VALUE_2) < NUM_VALUE_EPS) { // x * x | ||
| 95 | + RETURN_CALL(BinaryOperation<BinaryOpType::MUL>, *Program::GetInstance().GetCurrentFunction(), self, self); | ||
| 96 | + } else if (std::abs(exponent + NUM_VALUE_2) < NUM_VALUE_EPS) { // 1 / (x * x) | ||
| 97 | + auto mul = CALL(BinaryOperation<BinaryOpType::MUL>, *Program::GetInstance().GetCurrentFunction(), self, self); | ||
| 98 | + auto ones = GenAllOneTensor(self->shape, self->GetDynValidShape(), DT_FP32); | ||
| 99 | + auto [result, op] = TensorBinaryOperationWithOp<BinaryOpType::DIV>(*Program::GetInstance().GetCurrentFunction(), | ||
| 100 | + ones, mul); | ||
| @@ -0,0 +1,210 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file binary_tiled.h | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | +#pragma once | ||
| 16 | + | ||
| 17 | +#include "tilefwk/data_type.h" | ||
| 18 | +#include "binary.h" | ||
| 19 | +#include "tensor_transformation.h" | ||
| 20 | +#include "interface/configs/config_manager.h" | ||
| 21 | +#include "tilefwk/error_code.h" | ||
| 22 | +#include "passes/tile_graph_pass/graph_constraint/axis_combine.h" | ||
| 23 | + | ||
| 24 | +namespace npu::tile_fwk { | ||
| 25 | + | ||
| 26 | +template <BinaryOpType T> | ||
| 27 | +void AddTiledBinaryOperation(Function& function, const TileShape& tileShape, LogicalInput& input1, LogicalInput& input2, | ||
| 28 | + const LogicalTensorPtr& result, TileInfo& resultTileInfo, size_t shapeSize, | ||
| 29 | + [[maybe_unused]] int64_t precisionType) | ||
| 30 | +{ | ||
| 31 | + auto inputTile1 = input1.tensor->View(function, input1.tileInfo.shape, input1.tileInfo.offset); | ||
| 32 | + auto inputTile2 = input2.tensor->View(function, input2.tileInfo.shape, input2.tileInfo.offset); | ||
| 33 | + auto resultTile = result->View(function, resultTileInfo.shape, resultTileInfo.offset); | ||
| 34 | + auto opName = GetBinaryOpName<T>(); | ||
| 35 | + Operation* op = nullptr; | ||
| 36 | + if (opName == "BITWISEXOR" || opName == "COPYSIGN" || opName == "POW") { | ||
| 37 | + std::vector<int64_t> tmpShape(resultTileInfo.shape); | ||
| 38 | + auto alignSize = BLOCK_SIZE / BytesOf(result->Datatype()); | ||
| 39 | + auto alignedLast = AlignUp(tmpShape[resultTileInfo.shape.size() - 1], alignSize); | ||
| 40 | + if (opName == "POW" && result->Datatype() == DT_FP32 && | ||
| 41 | + Platform::Instance().GetSoc().GetNPUArch() == NPUArch::DAV_2201) { | ||
| 42 | + auto maskColsFloat = AlignUp((alignedLast + NUM_VALUE_8 - 1) / NUM_VALUE_8, BLOCK_SIZE) / | ||
| 43 | + BytesOf(result->Datatype()); | ||
| 44 | + alignedLast = NUM_VALUE_2 * alignedLast + NUM_VALUE_3 * maskColsFloat; | ||
| 45 | + } | ||
| 46 | + tmpShape[resultTileInfo.shape.size() - 1] = alignedLast; | ||
| 47 | + auto tempTensor = std::make_shared<LogicalTensor>(function, result->Datatype(), tmpShape); | ||
| 48 | + op = &function.AddOperation(GetBinaryOpNameCode<T, false, false>(), {inputTile1, inputTile2}, | ||
| 49 | + {resultTile, tempTensor}); | ||
| 50 | + } else if (opName == "REM") { | ||
| 51 | + int64_t alignSize = static_cast<int64_t>(BLOCK_SIZE / BytesOf(result->Datatype())); | ||
| 52 | + int64_t alignedLast = AlignUp(resultTileInfo.shape[shapeSize - 1], alignSize); | ||
| 53 | + int64_t maskBytes = (alignedLast + NUM_VALUE_8 - 1) / NUM_VALUE_8; | ||
| 54 | + int64_t maskFloats = AlignUp(maskBytes, BLOCK_SIZE) / static_cast<int64_t>(BytesOf(result->Datatype())); | ||
| 55 | + int64_t tmpCols = alignedLast + maskFloats + alignSize; | ||
| 56 | + std::vector<int64_t> tmpShape{1, tmpCols}; | ||
| 57 | + auto tmpTensor = std::make_shared<LogicalTensor>(function, result->Datatype(), tmpShape); | ||
| 58 | + op = &function.AddOperation(GetBinaryOpNameCode<T, false, false>(), {inputTile1, inputTile2}, | ||
| 59 | + {resultTile, tmpTensor}); | ||
| 60 | + } else if (opName == "ATAN2") { | ||
| 61 | + std::vector<int64_t> tmpShape(resultTileInfo.shape); | ||
| 62 | + auto lastDim = tmpShape[resultTileInfo.shape.size() - 1]; | ||
| 63 | + auto alignSize = BLOCK_SIZE / BytesOf(result->Datatype()); | ||
| 64 | + tmpShape[resultTileInfo.shape.size() - 1] = AlignUp(lastDim, alignSize) * NUM_VALUE_4 * BytesOf(DT_FP32); | ||
| 65 | + std::vector<SymbolicScalar> tmpValidShape; | ||
| 66 | + for (auto s : tmpShape) { | ||
| 67 | + tmpValidShape.emplace_back(s); | ||
| 68 | + } | ||
| 69 | + LogicalTensorPtr workspace = std::make_shared<LogicalTensor>(function, DT_UINT8, tmpShape, tmpValidShape); | ||
| 70 | + op = &function.AddOperation(GetBinaryOpNameCode<T, false, false>(), {inputTile1, inputTile2}, | ||
| 71 | + {resultTile, workspace}); | ||
| 72 | + } else if (opName == "FLOORDIV") { | ||
| 73 | + auto tmpShape = resultTileInfo.shape; | ||
| 74 | + auto alignSize = BLOCK_SIZE / BytesOf(result->Datatype()); | ||
| 75 | + tmpShape[resultTileInfo.shape.size() - 1] = AlignUp(tmpShape.back(), alignSize) * NUM_VALUE_6; | ||
| 76 | + auto tmpElemBytes = result->Datatype() == DT_INT64 ? BytesOf(DT_INT64) : BytesOf(DT_FP32); | ||
| 77 | + int64_t intermediateBytes = std::accumulate(tmpShape.begin(), tmpShape.end(), 1LL, std::multiplies<int64_t>()) * | ||
| 78 | + tmpElemBytes; | ||
| 79 | + auto tempTensor = std::make_shared<LogicalTensor>(function, DT_UINT8, std::vector<int64_t>{intermediateBytes}); | ||
| 80 | + op = &function.AddOperation(GetBinaryOpNameCode<T, false, false>(), {inputTile1, inputTile2}, | ||
| 81 | + {resultTile, tempTensor}); | ||
| 82 | + } else if (opName == "GCD") { | ||
| 83 | + auto vecTile = tileShape.GetVecTile().tile; | ||
| 84 | + int64_t tileW = vecTile.empty() ? 1 : vecTile.back(); | ||
| 85 | + int64_t tileH = vecTile.size() > 1 ? vecTile[vecTile.size() - NUM_VALUE_2] : 1; | ||
| 86 | + int64_t tileFootprint = tileH * tileW; | ||
| 87 | + int64_t maskCols = ((tileW + NUM_VALUE_7) / NUM_VALUE_8 + NUM_VALUE_31) / BLOCK_SIZE * BLOCK_SIZE; | ||
| 88 | + int64_t intermediateBytes = (NUM_VALUE_6 * tileFootprint * BytesOf(DT_INT32)) + | ||
| 89 | + (NUM_VALUE_2 * tileFootprint * BytesOf(DT_FP32)) + NUM_VALUE_4 * tileH * maskCols + | ||
| 90 | + 320; | ||
| 91 | + auto tempTensor = std::make_shared<LogicalTensor>(function, DT_UINT8, std::vector<int64_t>{intermediateBytes}); | ||
| 92 | + op = &function.AddOperation(GetBinaryOpNameCode<T, false, false>(), {inputTile1, inputTile2}, | ||
| 93 | + {resultTile, tempTensor}); | ||
| 94 | + } else { | ||
| 95 | + op = &function.AddOperation(GetBinaryOpNameCode<T, false, false>(), {inputTile1, inputTile2}, {resultTile}); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + if (op != nullptr) { | ||
| 99 | + std::vector<int64_t> brcOperand(shapeSize, 0); | ||
| 100 | + size_t brcAxesCount = 0; | ||
| 101 | + for (size_t i = 0; i < shapeSize; i++) { | ||
| 102 | + brcOperand[i] = BrcAxisBinaryOp(input1.tensor, input2.tensor, i); | ||
| 103 | + if (brcOperand[i] != 0) { | ||
| 104 | + brcAxesCount++; | ||
| 105 | + } | ||
| 106 | + } | ||
| 107 | + if (brcAxesCount > 0) { | ||
| 108 | + if (brcOperand[shapeSize - 1] != 0 && brcAxesCount >= NUM_VALUE_2) { | ||
| 109 | + op->SetAttribute(OpAttributeKey::excludeBufferReuse, true); | ||
| 110 | + } | ||
| 111 | + op->SetAttribute(OpAttributeKey::brcOperand, brcOperand); | ||
| 112 | + } | ||
| 113 | + } | ||
| 114 | + if constexpr (T == BinaryOpType::DIV || T == BinaryOpType::MOD || T == BinaryOpType::POW || | ||
| 115 | + T == BinaryOpType::REM) { | ||
| 116 | + op->SetAttribute(OpAttributeKey::precisionType, precisionType); | ||
| 117 | + } | ||
| 118 | +} | ||
| 119 | + | ||
| 120 | +template <BinaryOpType T> | ||
| 121 | +void TiledBinaryOperation(Function& function, const TileShape& tileShape, size_t cur, LogicalInput& input1, | ||
| 122 | + LogicalInput& input2, const LogicalTensorPtr& result, TileInfo& resultTileInfo, | ||
| 123 | + int64_t precisionType) | ||
| 124 | +{ | ||
| 125 | + size_t shapeSize = input1.tensor->GetShape().size(); | ||
| 126 | + if (cur == shapeSize) { | ||
| 127 | + AddTiledBinaryOperation<T>(function, tileShape, input1, input2, result, resultTileInfo, shapeSize, | ||
| 128 | + precisionType); | ||
| 129 | + return; | ||
| 130 | + } | ||
| 131 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 132 | + for (int i = 0; i < result->shape[cur]; i += vecTile[cur]) { | ||
| 133 | + resultTileInfo.offset[cur] = i; | ||
| 134 | + resultTileInfo.shape[cur] = std::min(result->shape[cur] - resultTileInfo.offset[cur], vecTile[cur]); | ||
| 135 | + input1.tileInfo.offset[cur] = i % input1.tensor->GetShape()[cur]; | ||
| 136 | + input1.tileInfo.shape[cur] = std::min(input1.tensor->GetShape()[cur] - input1.tileInfo.offset[cur], | ||
| 137 | + vecTile[cur]); | ||
| 138 | + input2.tileInfo.offset[cur] = i % input2.tensor->GetShape()[cur]; | ||
| 139 | + input2.tileInfo.shape[cur] = std::min(input2.tensor->GetShape()[cur] - input2.tileInfo.offset[cur], | ||
| 140 | + vecTile[cur]); | ||
| 141 | + TiledBinaryOperation<T>(function, tileShape, cur + 1, input1, input2, result, resultTileInfo, precisionType); | ||
| 142 | + } | ||
| 143 | +} | ||
| 144 | + | ||
| 145 | +// Determine the target shape for expand before tileop | ||
| 146 | +template <BinaryOpType T> | ||
| 147 | +std::pair<std::vector<int64_t>, std::vector<int64_t>> GetBrcExpandShape(Function& function, LogicalTensorPtr operand1, | ||
| 148 | + LogicalTensorPtr operand2, | ||
| 149 | + LogicalTensorPtr result) | ||
| 150 | +{ | ||
| 151 | + auto operand1Shape = result->shape; | ||
| 152 | + auto operand2Shape = result->shape; | ||
| 153 | + size_t shapeSize = result->shape.size(); | ||
| 154 | + | ||
| 155 | + bool isInWhiteList = SUPPORT_BRC_INLINE.count(GetBinaryOpNameCode<T>()); | ||
| 156 | + bool isCombineAxisEnabled = function.paramConfigs_.combineAxis && isInWhiteList; | ||
| 157 | + if (isInWhiteList) { | ||
| 158 | + // Outer axis: handled by tileop loop with stride control, keep operand shape. | ||
| 159 | + if (shapeSize > NUM_VALUE_2) { | ||
| 160 | + for (size_t i = 0; i < shapeSize - NUM_VALUE_2; i++) { | ||
| 161 | + operand1Shape[i] = operand1->shape[i]; | ||
| 162 | + operand2Shape[i] = operand2->shape[i]; | ||
| 163 | + } | ||
| 164 | + } | ||
| 165 | + // The 2nd last axis: skip expand, brcinline | ||
| 166 | + if (shapeSize > 1) { | ||
| 167 | + operand1Shape[shapeSize - NUM_VALUE_2] = operand1->shape[shapeSize - NUM_VALUE_2]; | ||
| 168 | + operand2Shape[shapeSize - NUM_VALUE_2] = operand2->shape[shapeSize - NUM_VALUE_2]; | ||
| 169 | + } | ||
| 170 | + // The last axis: brcinline when combineAxis is enabled | ||
| 171 | + if (shapeSize > 0 && isCombineAxisEnabled) { | ||
| 172 | + operand1Shape[shapeSize - 1] = operand1->shape[shapeSize - 1]; | ||
| 173 | + operand2Shape[shapeSize - 1] = operand2->shape[shapeSize - 1]; | ||
| 174 | + } | ||
| 175 | + } | ||
| 176 | + return {operand1Shape, operand2Shape}; | ||
| 177 | +} | ||
| 178 | + | ||
| 179 | +template <BinaryOpType T> | ||
| 180 | +void TiledBinaryOperation(Function& function, const TileShape& tileShape, LogicalTensorPtr operand1, | ||
| 181 | + LogicalTensorPtr operand2, const LogicalTensorPtr& result, int64_t precisionType) | ||
| 182 | +{ | ||
| 183 | + CheckBinOpOperandsValid(operand1, operand2); | ||
| 184 | + auto [dstShape1, dstShape2] = GetBrcExpandShape<T>(function, operand1, operand2, result); | ||
| 185 | + BroadcastOperandTensor(operand1, operand2, result, function, tileShape, dstShape1); | ||
| 186 | + BroadcastOperandTensor(operand2, operand1, result, function, tileShape, dstShape2); | ||
| 187 | + | ||
| 188 | + TileInfo tileInfo1(operand1->shape.size(), operand1->offset.size()); | ||
| 189 | + TileInfo tileInfo2(operand2->shape.size(), operand2->offset.size()); | ||
| 190 | + TileInfo resultTileInfo(result->shape.size(), result->offset.size()); | ||
| 191 | + auto input1 = LogicalInput{operand1, tileInfo1}; | ||
| 192 | + auto input2 = LogicalInput{operand2, tileInfo2}; | ||
| 193 | + TiledBinaryOperation<T>(function, tileShape, 0, input1, input2, result, resultTileInfo, precisionType); | ||
| 194 | +} | ||
| 195 | + | ||
| 196 | +template <BinaryOpType T> | ||
| 197 | +void BinaryOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 198 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 199 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 200 | +{ | ||
| 201 | + BinaryOperationOperandCheck(iOperand, oOperand); | ||
| 202 | + int64_t precisionType = static_cast<int64_t>(PrecisionType::INTRINSIC); | ||
| 203 | + if constexpr (T == BinaryOpType::DIV || T == BinaryOpType::MOD || T == BinaryOpType::POW || | ||
| 204 | + T == BinaryOpType::REM) { | ||
| 205 | + precisionType = GetOperationPrecisionType(op); | ||
| 206 | + } | ||
| 207 | + TiledBinaryOperation<T>(function, tileShape, iOperand[0], iOperand[1], oOperand[0], precisionType); | ||
| 208 | +} | ||
| 209 | + | ||
| 210 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,79 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file bitwise.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | +#include "tilefwk/data_type.h" | ||
| 16 | +#include "unary.h" | ||
| 17 | +#include "binary.h" | ||
| 18 | +#include "binary_tiled.h" | ||
| 19 | +#include "tensor_transformation.h" | ||
| 20 | +#include "interface/utils/operator_tracer.h" | ||
| 21 | +#include "interface/configs/config_manager.h" | ||
| 22 | +#include "tilefwk/error_code.h" | ||
| 23 | +#include "passes/tile_graph_pass/graph_constraint/axis_combine.h" | ||
| 24 | + | ||
| 25 | +namespace npu::tile_fwk { | ||
| 26 | + | ||
| 27 | +Tensor BitwiseAnd(const Tensor& self, const Tensor& other) | ||
| 28 | +{ | ||
| 29 | + DECLARE_TRACER(); | ||
| 30 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseAnd"); | ||
| 31 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseAnd"); | ||
| 32 | + | ||
| 33 | + CheckTensorsDataTypeConsistency(self.GetStorage(), other.GetStorage(), "BITWISEAND"); | ||
| 34 | + static const std::unordered_set<DataType> BITWISE_A2A3_TYPES = {DT_INT16, DT_UINT16, DT_INT8, | ||
| 35 | + DT_UINT8, DT_INT32, DT_UINT32}; | ||
| 36 | + static const std::unordered_set<DataType> BITWISE_A5_TYPES = {DT_INT16, DT_UINT16, DT_INT8, DT_UINT8, | ||
| 37 | + DT_INT32, DT_UINT32, DT_INT64, DT_UINT64}; | ||
| 38 | + const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISE_A2A3_TYPES, BITWISE_A5_TYPES); | ||
| 39 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "BITWISEAND"); | ||
| 40 | + RETURN_CALL(BinaryOperation<BinaryOpType::BITWISEAND>, *Program::GetInstance().GetCurrentFunction(), self, other); | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +Tensor BitwiseOr(const Tensor& self, const Tensor& other) | ||
| 44 | +{ | ||
| 45 | + DECLARE_TRACER(); | ||
| 46 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseOr"); | ||
| 47 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseOr"); | ||
| 48 | + | ||
| 49 | + CheckTensorsDataTypeConsistency(self.GetStorage(), other.GetStorage(), "BITWISEOR"); | ||
| 50 | + static const std::unordered_set<DataType> BITWISE_A2A3_TYPES = {DT_INT16, DT_UINT16, DT_INT8, | ||
| 51 | + DT_UINT8, DT_INT32, DT_UINT32}; | ||
| 52 | + static const std::unordered_set<DataType> BITWISE_A5_TYPES = {DT_INT16, DT_UINT16, DT_INT8, DT_UINT8, | ||
| 53 | + DT_INT32, DT_UINT32, DT_INT64, DT_UINT64}; | ||
| 54 | + const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISE_A2A3_TYPES, BITWISE_A5_TYPES); | ||
| 55 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "BITWISEOR"); | ||
| 56 | + RETURN_CALL(BinaryOperation<BinaryOpType::BITWISEOR>, *Program::GetInstance().GetCurrentFunction(), self, other); | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +Tensor BitwiseXor(const Tensor& self, const Tensor& other) | ||
| 60 | +{ | ||
| 61 | + DECLARE_TRACER(); | ||
| 62 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseXor"); | ||
| 63 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseXor"); | ||
| 64 | + | ||
| 65 | + CheckTensorsDataTypeConsistency(self.GetStorage(), other.GetStorage(), "BITWISEXOR"); | ||
| 66 | + static const std::unordered_set<DataType> BITWISE_A2A3_TYPES = {DT_INT16, DT_UINT16, DT_INT8, | ||
| 67 | + DT_UINT8, DT_INT32, DT_UINT32}; | ||
| 68 | + static const std::unordered_set<DataType> BITWISE_A5_TYPES = {DT_INT16, DT_UINT16, DT_INT8, DT_UINT8, | ||
| 69 | + DT_INT32, DT_UINT32, DT_INT64, DT_UINT64}; | ||
| 70 | + const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISE_A2A3_TYPES, BITWISE_A5_TYPES); | ||
| 71 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "BITWISEXOR"); | ||
| 72 | + RETURN_CALL(BinaryOperation<BinaryOpType::BITWISEXOR>, *Program::GetInstance().GetCurrentFunction(), self, other); | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +REGISTER_OPERATION_TILED_FUNC(OP_BITWISEAND, Opcode::OP_BITWISEAND, BinaryOperationTileFunc<BinaryOpType::BITWISEAND>); | ||
| 76 | +REGISTER_OPERATION_TILED_FUNC(OP_BITWISEOR, Opcode::OP_BITWISEOR, BinaryOperationTileFunc<BinaryOpType::BITWISEOR>); | ||
| 77 | +REGISTER_OPERATION_TILED_FUNC(OP_BITWISEXOR, Opcode::OP_BITWISEXOR, BinaryOperationTileFunc<BinaryOpType::BITWISEXOR>); | ||
| 78 | + | ||
| 79 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,174 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file cast.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "unary.h" | ||
| 17 | +#include <sstream> | ||
| 18 | +#include <string> | ||
| 19 | +#include "tensor_transformation.h" | ||
| 20 | +#include "interface/utils/operator_tracer.h" | ||
| 21 | +#include "tilefwk/error_code.h" | ||
| 22 | +#include "tilefwk/platform.h" | ||
| 23 | + | ||
| 24 | +namespace npu::tile_fwk { | ||
| 25 | + | ||
| 26 | +template <CastOpType T> | ||
| 27 | +void TiledCastOperation(Function& function, const TileShape& tileShape, const int cur, Input& input, | ||
| 28 | + const LogicalTensorPtr& result, const CastMode& mode, const SaturationMode& satmode) | ||
| 29 | +{ | ||
| 30 | + if (cur == static_cast<int>(input.tensor.GetShape().size())) { | ||
| 31 | + auto tile = input.tensor.GetStorage()->View(function, input.tileInfo.shape, input.tileInfo.offset); | ||
| 32 | + auto resultTile = result->View(function, input.tileInfo.shape, input.tileInfo.offset); | ||
| 33 | + | ||
| 34 | + DataType srcDtype = tile->Datatype(); | ||
| 35 | + DataType dstDtype = resultTile->Datatype(); | ||
| 36 | + | ||
| 37 | + bool needTmpBuffer = false; | ||
| 38 | + if (((srcDtype == DT_FP32 && dstDtype == DT_INT16) || (srcDtype == DT_FP16 && dstDtype == DT_INT16) || | ||
| 39 | + (srcDtype == DT_FP16 && dstDtype == DT_INT8)) && | ||
| 40 | + satmode == SaturationMode::OFF) { | ||
| 41 | + needTmpBuffer = true; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + Operation* op = nullptr; | ||
| 45 | + if (needTmpBuffer) { | ||
| 46 | + size_t shapeSize = input.tileInfo.shape.size(); | ||
| 47 | + int64_t shapeW = (shapeSize >= 1) ? input.tileInfo.shape[shapeSize - 1] : 1; | ||
| 48 | + shapeW = AlignUp(shapeW + ALIGN_SIZE_64, static_cast<int64_t>(BLOCK_SIZE / BytesOf(DT_INT32))); | ||
| 49 | + int64_t shapeH = (shapeSize >= NUM_VALUE_2) ? input.tileInfo.shape[shapeSize - NUM_VALUE_2] : 1; | ||
| 50 | + shapeH = std::min(shapeH, static_cast<int64_t>(MAX_REPEAT)); | ||
| 51 | + std::vector<int64_t> tmpShape = {shapeH, shapeW}; | ||
| 52 | + auto tmpTensor = std::make_shared<LogicalTensor>(function, DT_INT32, tmpShape); | ||
| 53 | + op = &function.AddOperation(GetCastOpName<T>(), {tile}, {resultTile, tmpTensor}); | ||
| 54 | + } else { | ||
| 55 | + op = &function.AddOperation(GetCastOpName<T>(), {tile}, {resultTile}); | ||
| 56 | + } | ||
| 57 | + op->SetAttribute(OP_ATTR_PREFIX + "mode", mode); | ||
| 58 | + op->SetAttribute(OP_ATTR_PREFIX + "satmode", static_cast<int64_t>(satmode)); | ||
| 59 | + return; | ||
| 60 | + } | ||
| 61 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 62 | + for (int i = 0; i < input.tensor.GetShape()[cur]; i += vecTile[cur]) { | ||
| 63 | + input.tileInfo.shape[cur] = std::min(input.tensor.GetShape()[cur] - i, vecTile[cur]); | ||
| 64 | + input.tileInfo.offset[cur] = i; | ||
| 65 | + TiledCastOperation<T>(function, tileShape, cur + 1, input, result, mode, satmode); | ||
| 66 | + } | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +template <CastOpType T> | ||
| 70 | +void TiledCastOperation(Function& function, const TileShape& tileShape, const LogicalTensorPtr& operand, | ||
| 71 | + const LogicalTensorPtr& result, const CastMode& mode, const SaturationMode& satmode) | ||
| 72 | +{ | ||
| 73 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, operand->shape.size() == operand->offset.size()) | ||
| 74 | + << "The shape size of operand and offset should be equal"; | ||
| 75 | + | ||
| 76 | + TileInfo tileInfo(result->shape.size(), result->offset.size()); | ||
| 77 | + auto input = Input{operand, tileInfo}; | ||
| 78 | + TiledCastOperation<T>(function, tileShape, 0, input, result, mode, satmode); | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +void CheckCastTypeSupport(DataType srcType, DataType dstType, const std::string& opName) | ||
| 82 | +{ | ||
| 83 | + // 同类型转换始终支持 | ||
| 84 | + if (srcType == dstType) { | ||
| 85 | + return; | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + auto arch = Platform::Instance().GetSoc().GetNPUArch(); | ||
| 89 | + bool isA5Architecture = (arch == NPUArch::DAV_3510); | ||
| 90 | + | ||
| 91 | + if (isA5Architecture) { | ||
| 92 | + // A5 架构支持的转换 | ||
| 93 | + std::unordered_map<DataType, std::unordered_set<DataType>> a5SupportedConversions = { | ||
| 94 | + {DT_FP32, {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_INT64, DT_FP8E4M3, DT_FP8E5M2, DT_HF8}}, | ||
| 95 | + {DT_FP16, {DT_FP32, DT_INT32, DT_INT16, DT_INT8, DT_UINT8, DT_HF8}}, | ||
| 96 | + {DT_BF16, {DT_FP32, DT_INT32, DT_FP16, DT_FP4_E1M2X2, DT_FP4_E2M1X2}}, | ||
| 97 | + {DT_UINT8, {DT_FP16, DT_UINT16}}, | ||
| 98 | + {DT_INT8, {DT_FP16, DT_INT16, DT_INT32}}, | ||
| 99 | + {DT_INT16, {DT_UINT8, DT_FP16, DT_FP32, DT_UINT32, DT_INT32}}, | ||
| 100 | + {DT_INT32, {DT_FP32, DT_INT16, DT_UINT16, DT_INT64, DT_UINT8, DT_FP16}}, | ||
| 101 | + {DT_UINT32, {DT_UINT8, DT_UINT16, DT_INT16}}, | ||
| 102 | + {DT_INT64, {DT_FP32, DT_INT32}}, | ||
| 103 | + {DT_FP8E4M3, {DT_FP32}}, | ||
| 104 | + {DT_FP8E5M2, {DT_FP32}}, | ||
| 105 | + {DT_HF8, {DT_FP32}}, | ||
| 106 | + {DT_FP4_E1M2X2, {DT_BF16}}, | ||
| 107 | + {DT_FP4_E2M1X2, {DT_BF16}}}; | ||
| 108 | + | ||
| 109 | + if (a5SupportedConversions.count(srcType) == 0 || a5SupportedConversions[srcType].count(dstType) == 0) { | ||
| 110 | + CHECK(VectorErrorCode::ERR_PARAM_DTYPE_UNSUPPORTED, false) | ||
| 111 | + << "A5 architecture does not support cast from " << npu::tile_fwk::DataType2String(srcType) << " to " | ||
| 112 | + << npu::tile_fwk::DataType2String(dstType) << " in " << opName; | ||
| 113 | + } | ||
| 114 | + } else { | ||
| 115 | + // A2A3 架构支持的转换(其他架构也按A2A3处理) | ||
| 116 | + std::unordered_map<DataType, std::unordered_set<DataType>> a2a3SupportedConversions = { | ||
| 117 | + {DT_FP16, {DT_FP32, DT_INT32, DT_INT16, DT_INT8, DT_UINT8, DT_INT4}}, | ||
| 118 | + {DT_BF16, {DT_FP32, DT_INT32}}, | ||
| 119 | + {DT_INT32, {DT_FP32, DT_INT16, DT_INT64, DT_FP16}}, | ||
| 120 | + {DT_FP32, {DT_BF16, DT_FP16, DT_INT16, DT_INT32, DT_INT64}}, | ||
| 121 | + {DT_UINT8, {DT_FP16}}, | ||
| 122 | + {DT_INT8, {DT_FP16}}, | ||
| 123 | + {DT_INT16, {DT_FP32, DT_FP16}}, | ||
| 124 | + {DT_INT64, {DT_FP32, DT_INT32}}, | ||
| 125 | + {DT_INT4, {DT_FP16}}}; | ||
| 126 | + | ||
| 127 | + if (a2a3SupportedConversions.count(srcType) == 0 || a2a3SupportedConversions[srcType].count(dstType) == 0) { | ||
| 128 | + CHECK(VectorErrorCode::ERR_PARAM_DTYPE_UNSUPPORTED, false) | ||
| 129 | + << "A2A3 architecture does not support cast from " << npu::tile_fwk::DataType2String(srcType) << " to " | ||
| 130 | + << npu::tile_fwk::DataType2String(dstType) << " in " << opName; | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +Tensor Cast(const Tensor& self, DataType dstDataType, CastMode mode, SaturationMode satmode) | ||
| 136 | +{ | ||
| 137 | + DECLARE_TRACER(); | ||
| 138 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Cast"); | ||
| 139 | + | ||
| 140 | + CheckCastTypeSupport(self.GetDataType(), dstDataType, "CAST"); | ||
| 141 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "CAST"); | ||
| 142 | + CheckTensorShapeSize(self.GetStorage(), "CAST"); | ||
| 143 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, self.GetShape().size() == self.GetStorage()->offset.size()) | ||
| 144 | + << "The shape size of self and offset should be equal"; | ||
| 145 | + // Cast to same dType with no mode will do nothing | ||
| 146 | + if (self.GetStorage()->tensor->datatype == dstDataType && (mode == CAST_NONE || mode == CAST_RINT)) { | ||
| 147 | + return self; | ||
| 148 | + } | ||
| 149 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), | ||
| 150 | + dstDataType, mode, satmode); | ||
| 151 | +} | ||
| 152 | + | ||
| 153 | +inline void CastOperationOperandCheck(const std::vector<LogicalTensorPtr>& iOperand, | ||
| 154 | + const std::vector<LogicalTensorPtr>& oOperand) | ||
| 155 | +{ | ||
| 156 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, iOperand.size() == 1) << "The input operand size should be 1"; | ||
| 157 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, oOperand.size() == 1) << "The output operand size should be 1"; | ||
| 158 | +} | ||
| 159 | + | ||
| 160 | +void CastOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 161 | + const std::vector<LogicalTensorPtr>& iOperand, const std::vector<LogicalTensorPtr>& oOperand, | ||
| 162 | + const Operation& op) | ||
| 163 | +{ | ||
| 164 | + CastOperationOperandCheck(iOperand, oOperand); | ||
| 165 | + int64_t satmodeValue = 1; | ||
| 166 | + op.GetAttr(OP_ATTR_PREFIX + "satmode", satmodeValue); | ||
| 167 | + SaturationMode satmode = static_cast<SaturationMode>(satmodeValue); | ||
| 168 | + auto mode = op.GetCastModeAttribute(OP_ATTR_PREFIX + "mode"); | ||
| 169 | + TiledCastOperation<CastOpType::CAST>(function, tileShape, iOperand[0], oOperand[0], mode, satmode); | ||
| 170 | +} | ||
| 171 | + | ||
| 172 | +REGISTER_OPERATION_TILED_FUNC(OP_CAST, Opcode::OP_CAST, CastOperationTileFunc); | ||
| 173 | + | ||
| 174 | +} // namespace npu::tile_fwk | ||
| @@ -33,15 +33,15 @@ void TiledCompareOperationImpl(Function& function, const TileShape& tileShape, s | |||
| 33 | size_t element_size = BytesOf(input1.tensor.GetDataType()); | 33 | size_t element_size = BytesOf(input1.tensor.GetDataType()); |
| 34 | CHECK(VectorErrorCode::ERR_RUNTIME_LOGIC, element_size != 0) << "Element size cannot be zero."; | 34 | CHECK(VectorErrorCode::ERR_RUNTIME_LOGIC, element_size != 0) << "Element size cannot be zero."; |
| 35 | int64_t elements_per_chunk = COUNT_MODE_SIZE / element_size; | 35 | int64_t elements_per_chunk = COUNT_MODE_SIZE / element_size; |
| 36 | - int64_t vcmp_bits_size = (elements_per_chunk + 7) / 8; | 36 | + int64_t vcmp_bits_size = (elements_per_chunk + NUM_VALUE_7) / NUM_VALUE_8; |
| 37 | 37 | ||
| 38 | - const size_t ALIGN_SIZE = 32; | 38 | + const size_t ALIGN_SIZE = NUM_VALUE_32; |
| 39 | 39 | ||
| 40 | size_t vcmpBitResult_size = ((vcmp_bits_size + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE; | 40 | size_t vcmpBitResult_size = ((vcmp_bits_size + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE; |
| 41 | size_t array_size = elements_per_chunk * element_size; | 41 | size_t array_size = elements_per_chunk * element_size; |
| 42 | size_t aligned_array_size = ((array_size + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE; | 42 | size_t aligned_array_size = ((array_size + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE; |
| 43 | 43 | ||
| 44 | - size_t total_bytes = vcmpBitResult_size + 3 * aligned_array_size + ALIGN_SIZE * 2; | 44 | + size_t total_bytes = vcmpBitResult_size + NUM_VALUE_3 * aligned_array_size + ALIGN_SIZE * NUM_VALUE_2; |
| 45 | std::vector<int64_t> tmp_shape({static_cast<int64_t>(total_bytes)}); | 45 | std::vector<int64_t> tmp_shape({static_cast<int64_t>(total_bytes)}); |
| 46 | auto tmp_tensor = std::make_shared<LogicalTensor>(function, DT_UINT8, tmp_shape); | 46 | auto tmp_tensor = std::make_shared<LogicalTensor>(function, DT_UINT8, tmp_shape); |
| 47 | 47 | ||
| @@ -232,15 +232,15 @@ void TiledCmpsOperationImpl(Function& function, const TileShape& tileShape, size | |||
| 232 | size_t element_size = BytesOf(input.tensor.GetDataType()); | 232 | size_t element_size = BytesOf(input.tensor.GetDataType()); |
| 233 | CHECK(VectorErrorCode::ERR_RUNTIME_LOGIC, element_size != 0) << "Element size cannot be zero."; | 233 | CHECK(VectorErrorCode::ERR_RUNTIME_LOGIC, element_size != 0) << "Element size cannot be zero."; |
| 234 | int64_t elements_per_chunk = COUNT_MODE_SIZE / element_size; | 234 | int64_t elements_per_chunk = COUNT_MODE_SIZE / element_size; |
| 235 | - int64_t vcmp_bits_size = (elements_per_chunk + 8 - 1) / 8; | 235 | + int64_t vcmp_bits_size = (elements_per_chunk + NUM_VALUE_8 - 1) / NUM_VALUE_8; |
| 236 | 236 | ||
| 237 | - const size_t ALIGN_SIZE = 32; | 237 | + const size_t ALIGN_SIZE = NUM_VALUE_32; |
| 238 | 238 | ||
| 239 | size_t vcmpBitResult_size = ((vcmp_bits_size + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE; | 239 | size_t vcmpBitResult_size = ((vcmp_bits_size + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE; |
| 240 | size_t array_size = elements_per_chunk * element_size; | 240 | size_t array_size = elements_per_chunk * element_size; |
| 241 | size_t aligned_array_size = ((array_size + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE; | 241 | size_t aligned_array_size = ((array_size + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE; |
| 242 | 242 | ||
| 243 | - size_t total_bytes = vcmpBitResult_size + 3 * aligned_array_size + ALIGN_SIZE; | 243 | + size_t total_bytes = vcmpBitResult_size + NUM_VALUE_3 * aligned_array_size + ALIGN_SIZE; |
| 244 | std::vector<int64_t> tmp_shape({static_cast<int64_t>(total_bytes)}); | 244 | std::vector<int64_t> tmp_shape({static_cast<int64_t>(total_bytes)}); |
| 245 | auto tmp_tensor = std::make_shared<LogicalTensor>(function, DT_UINT8, tmp_shape); | 245 | auto tmp_tensor = std::make_shared<LogicalTensor>(function, DT_UINT8, tmp_shape); |
| 246 | 246 | ||
| @@ -255,37 +255,15 @@ void TiledCmpsOperationImpl(Function& function, const TileShape& tileShape, size | |||
| 255 | } | 255 | } |
| 256 | 256 | ||
| 257 | auto& vecTile = tileShape.GetVecTile(); | 257 | auto& vecTile = tileShape.GetVecTile(); |
| 258 | - int64_t step = vecTile[cur]; | 258 | + int64_t inputScale = mode == OutType::BIT && cur == result->shape.size() - 1 ? NUM_VALUE_8 : 1; |
| 259 | - | 259 | + int64_t step = inputScale == NUM_VALUE_8 ? std::max<int64_t>(vecTile[cur] / NUM_VALUE_8, 1) : vecTile[cur]; |
| 260 | - if (mode == OutType::BIT && cur == result->shape.size() - 1) { | 260 | + int64_t inputStep = step * inputScale; |
| 261 | - step = vecTile[cur] / NUM_VALUE_8; | 261 | + for (int i = 0; i < result->shape[cur]; i += step) { |
| 262 | - if (step < 1) | 262 | + resultTileInfo.offset[cur] = i; |
| 263 | - step = 1; | 263 | + resultTileInfo.shape[cur] = std::min(result->shape[cur] - i, step); |
| 264 | - | 264 | + input.tileInfo.offset[cur] = (i * inputScale) % input.tensor.GetShape()[cur]; |
| 265 | - int64_t actualInputStep = step * NUM_VALUE_8; | 265 | + input.tileInfo.shape[cur] = std::min(input.tensor.GetShape()[cur] - input.tileInfo.offset[cur], inputStep); |
| 266 | - | 266 | + TiledCmpsOperationImpl(function, tileShape, cur + 1, input, scalar, result, resultTileInfo, operation, mode); |
| 267 | - for (int i = 0; i < result->shape[cur]; i += step) { | ||
| 268 | - resultTileInfo.offset[cur] = i; | ||
| 269 | - resultTileInfo.shape[cur] = std::min(result->shape[cur] - i, step); | ||
| 270 | - | ||
| 271 | - input.tileInfo.offset[cur] = (i * NUM_VALUE_8) % input.tensor.GetShape()[cur]; | ||
| 272 | - input.tileInfo.shape[cur] = std::min(input.tensor.GetShape()[cur] - input.tileInfo.offset[cur], | ||
| 273 | - actualInputStep); | ||
| 274 | - | ||
| 275 | - TiledCmpsOperationImpl(function, tileShape, cur + 1, input, scalar, result, resultTileInfo, operation, | ||
| 276 | - mode); | ||
| 277 | - } | ||
| 278 | - } else { | ||
| 279 | - for (int i = 0; i < result->shape[cur]; i += step) { | ||
| 280 | - resultTileInfo.offset[cur] = i; | ||
| 281 | - resultTileInfo.shape[cur] = std::min(result->shape[cur] - i, step); | ||
| 282 | - | ||
| 283 | - input.tileInfo.offset[cur] = i % input.tensor.GetShape()[cur]; | ||
| 284 | - input.tileInfo.shape[cur] = std::min(input.tensor.GetShape()[cur] - input.tileInfo.offset[cur], step); | ||
| 285 | - | ||
| 286 | - TiledCmpsOperationImpl(function, tileShape, cur + 1, input, scalar, result, resultTileInfo, operation, | ||
| 287 | - mode); | ||
| 288 | - } | ||
| 289 | } | 267 | } |
| 290 | } | 268 | } |
| 291 | 269 | ||
| @@ -324,7 +302,7 @@ Tensor Compare(const Tensor& self, const Element& other, OpType op, OutType mode | |||
| 324 | static const std::unordered_set<DataType> CMP_A5_TYPES = {DT_FP16, DT_FP32, DT_INT16, DT_INT64, DT_UINT64}; | 302 | static const std::unordered_set<DataType> CMP_A5_TYPES = {DT_FP16, DT_FP32, DT_INT16, DT_INT64, DT_UINT64}; |
| 325 | const auto& supportedTypes = GetSupportedDataTypesByArch(CMP_A2A3_TYPES, CMP_A5_TYPES); | 303 | const auto& supportedTypes = GetSupportedDataTypesByArch(CMP_A2A3_TYPES, CMP_A5_TYPES); |
| 326 | CheckTensorDataType(self.GetStorage(), supportedTypes, "COMPARE"); | 304 | CheckTensorDataType(self.GetStorage(), supportedTypes, "COMPARE"); |
| 327 | - CheckTensorDimRange(self.GetStorage(), 1, 4, "COMPARE"); | 305 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "COMPARE"); |
| 328 | CheckTensorShapeSize(self.GetStorage(), "COMPARE"); | 306 | CheckTensorShapeSize(self.GetStorage(), "COMPARE"); |
| 329 | CheckTensorNonEmpty(self.GetStorage(), "COMPARE"); | 307 | CheckTensorNonEmpty(self.GetStorage(), "COMPARE"); |
| 330 | RETURN_CALL(CompareOperationScalar, *Program::GetInstance().GetCurrentFunction(), self, other, op, mode); | 308 | RETURN_CALL(CompareOperationScalar, *Program::GetInstance().GetCurrentFunction(), self, other, op, mode); |
| @@ -339,7 +317,7 @@ Tensor Compare(const Element& self, const Tensor& other, OpType op, OutType mode | |||
| 339 | static const std::unordered_set<DataType> CMP_A5_TYPES = {DT_FP16, DT_FP32, DT_INT16, DT_INT64, DT_UINT64}; | 317 | static const std::unordered_set<DataType> CMP_A5_TYPES = {DT_FP16, DT_FP32, DT_INT16, DT_INT64, DT_UINT64}; |
| 340 | const auto& supportedTypes = GetSupportedDataTypesByArch(CMP_A2A3_TYPES, CMP_A5_TYPES); | 318 | const auto& supportedTypes = GetSupportedDataTypesByArch(CMP_A2A3_TYPES, CMP_A5_TYPES); |
| 341 | CheckTensorDataType(other.GetStorage(), supportedTypes, "COMPARE"); | 319 | CheckTensorDataType(other.GetStorage(), supportedTypes, "COMPARE"); |
| 342 | - CheckTensorDimRange(other.GetStorage(), 1, 4, "COMPARE"); | 320 | + CheckTensorDimRange(other.GetStorage(), 1, NUM_VALUE_4, "COMPARE"); |
| 343 | CheckTensorShapeSize(other.GetStorage(), "COMPARE"); | 321 | CheckTensorShapeSize(other.GetStorage(), "COMPARE"); |
| 344 | CheckTensorNonEmpty(other.GetStorage(), "COMPARE"); | 322 | CheckTensorNonEmpty(other.GetStorage(), "COMPARE"); |
| 345 | RETURN_CALL(CompareOperationScalar, *Program::GetInstance().GetCurrentFunction(), self, other, op, mode); | 323 | RETURN_CALL(CompareOperationScalar, *Program::GetInstance().GetCurrentFunction(), self, other, op, mode); |
| @@ -0,0 +1,93 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file concat.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "unary.h" | ||
| 17 | +#include <sstream> | ||
| 18 | +#include <string> | ||
| 19 | +#include "tensor_transformation.h" | ||
| 20 | +#include "interface/utils/operator_tracer.h" | ||
| 21 | +#include "tilefwk/error_code.h" | ||
| 22 | +#include "tilefwk/platform.h" | ||
| 23 | + | ||
| 24 | +namespace npu::tile_fwk { | ||
| 25 | + | ||
| 26 | +void TensorInnerConcatNew(Function& function, const LogicalTensorPtr& operand, const LogicalTensorPtr& result) | ||
| 27 | +{ | ||
| 28 | + result->UpdateDynValidShape(operand->GetDynValidShape()); | ||
| 29 | + function.AddOperation(Opcode::OP_REGISTER_COPY, {operand}, {result}); | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +void InnerConcatNew(Function& function, const LogicalTensorPtr& operand, const LogicalTensorPtr& result) | ||
| 33 | +{ | ||
| 34 | + CALL(InnerConcatNew, function, operand, result); | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +void CheckCat(const std::vector<Tensor>& tensors, int axis) | ||
| 38 | +{ | ||
| 39 | + std::unordered_set<DataType> supportedTypes = {DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_INT32, DT_UINT32, | ||
| 40 | + DT_FP16, DT_FP32, DT_BF16, DT_INT64, DT_UINT64}; | ||
| 41 | + CheckTensorDataType(tensors[0].GetStorage(), supportedTypes, "CAT"); | ||
| 42 | + CheckAxisRange(tensors[0], axis); | ||
| 43 | + std::vector<LogicalTensorPtr> tensorPtrs; | ||
| 44 | + for (auto tensor : tensors) { | ||
| 45 | + CheckTensorShapeSize(tensor.GetStorage(), "CAT"); | ||
| 46 | + tensorPtrs.push_back(tensor.GetStorage()); | ||
| 47 | + } | ||
| 48 | + CheckTensorsDimConsistency(tensorPtrs, "CAT"); | ||
| 49 | + CheckTensorsDataTypeConsistency(tensorPtrs, "CAT"); | ||
| 50 | + CheckTensorsFormatConsistency(tensorPtrs, "CAT"); | ||
| 51 | + auto shape = tensors[0].GetShape(); | ||
| 52 | + for (auto tensor : tensors) { | ||
| 53 | + for (int i = 0; static_cast<size_t>(i) < tensors[0].GetShape().size(); ++i) { | ||
| 54 | + if (i == axis) { | ||
| 55 | + continue; | ||
| 56 | + } | ||
| 57 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, shape[i] == tensor.GetShape()[i]) | ||
| 58 | + << "The shape of all tensors should be equal except at axis"; | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +Tensor Cat(const std::vector<Tensor>& tensors, int axis) | ||
| 64 | +{ | ||
| 65 | + DECLARE_TRACER(); | ||
| 66 | + for (const auto& tensor : tensors) { | ||
| 67 | + CheckTensorFormat(tensor.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Cat"); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + CheckCat(tensors, axis); | ||
| 71 | + | ||
| 72 | + auto resultShape = tensors[0].GetShape(); | ||
| 73 | + auto shapeSize = resultShape.size(); | ||
| 74 | + CheckAxisRange(tensors[0], axis); | ||
| 75 | + int axisSize = 0; | ||
| 76 | + for (auto tensor : tensors) { | ||
| 77 | + axisSize += tensor.GetShape()[axis]; | ||
| 78 | + } | ||
| 79 | + resultShape[axis] = axisSize; | ||
| 80 | + | ||
| 81 | + auto format = tensors[0].Format(); | ||
| 82 | + Tensor result(tensors[0].GetDataType(), resultShape, "", format); | ||
| 83 | + std::vector<SymbolicScalar> offset(shapeSize, 0); | ||
| 84 | + for (auto tensor : tensors) { | ||
| 85 | + auto materialized = Assign(tensor); | ||
| 86 | + Assemble(materialized, offset, result); | ||
| 87 | + offset[axis] = offset[axis] + tensor.GetShape()[axis]; | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + return result; | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,338 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file cumulative.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "unary.h" | ||
| 17 | +#include "binary.h" | ||
| 18 | +#include "tensor_transformation.h" | ||
| 19 | +#include "interface/utils/operator_tracer.h" | ||
| 20 | +#include "passes/pass_utils/graph_utils.h" | ||
| 21 | +#include "tilefwk/error_code.h" | ||
| 22 | + | ||
| 23 | +namespace npu::tile_fwk { | ||
| 24 | + | ||
| 25 | +struct CumOperationTileInfoPara { | ||
| 26 | + TileInfo inputTileInfo; | ||
| 27 | + TileInfo dstTileInfo; | ||
| 28 | +}; | ||
| 29 | + | ||
| 30 | +struct CumOperationPara { | ||
| 31 | + const LogicalTensorPtr& input; | ||
| 32 | + const LogicalTensorPtr& dstTensor; | ||
| 33 | + const int axis; | ||
| 34 | + const bool is_sum; | ||
| 35 | +}; | ||
| 36 | + | ||
| 37 | +// CumSum/CumProd tiling (rank 1-4): | ||
| 38 | +// Phase 1 — loop and tile every non-cum dimension (0..rank-1 except axis). | ||
| 39 | +// Phase 2 — within each fixed non-cum fiber, tile only along axis and propagate | ||
| 40 | +// carry (last cum-axis element) between consecutive tiles on that axis. | ||
| 41 | +// Carry is scoped per non-cum fiber so higher-rank slices do not clobber each other. | ||
| 42 | + | ||
| 43 | +namespace { | ||
| 44 | + | ||
| 45 | +std::vector<int> BuildNonCumDimIndices(int rank, int axis) | ||
| 46 | +{ | ||
| 47 | + std::vector<int> dims; | ||
| 48 | + dims.reserve(rank > 0 ? static_cast<size_t>(rank - 1) : 0); | ||
| 49 | + for (int d = 0; d < rank; ++d) { | ||
| 50 | + if (d != axis) { | ||
| 51 | + dims.push_back(d); | ||
| 52 | + } | ||
| 53 | + } | ||
| 54 | + return dims; | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +void EmitCumAxisTile(Function& function, const CumOperationPara& cumOperationPara, CumOperationTileInfoPara& tileInfo, | ||
| 58 | + LogicalTensorPtr& lastCarry) | ||
| 59 | +{ | ||
| 60 | + const LogicalTensorPtr& input = cumOperationPara.input; | ||
| 61 | + const LogicalTensorPtr& dstTensor = cumOperationPara.dstTensor; | ||
| 62 | + const int axis = cumOperationPara.axis; | ||
| 63 | + const bool is_sum = cumOperationPara.is_sum; | ||
| 64 | + | ||
| 65 | + auto dstTile = dstTensor->View(function, tileInfo.dstTileInfo.shape, tileInfo.dstTileInfo.offset); | ||
| 66 | + auto inputTile = input->View(function, tileInfo.inputTileInfo.shape, tileInfo.inputTileInfo.offset); | ||
| 67 | + | ||
| 68 | + LogicalTensorPtr srcTile = std::make_shared<LogicalTensor>(function, dstTile->Datatype(), dstTile->GetShape(), | ||
| 69 | + inputTile->GetDynValidShape()); | ||
| 70 | + if (is_sum) { | ||
| 71 | + auto& op = function.AddOperation(Opcode::OP_CUM_SUM, {inputTile}, {srcTile}); | ||
| 72 | + op.SetAttribute(OP_ATTR_PREFIX + "axis", axis); | ||
| 73 | + op.SetAttribute(OP_ATTR_PREFIX + "flag", is_sum); | ||
| 74 | + } else { | ||
| 75 | + auto& op = function.AddOperation(Opcode::OP_CUM_PROD, {inputTile}, {srcTile}); | ||
| 76 | + op.SetAttribute(OP_ATTR_PREFIX + "axis", axis); | ||
| 77 | + op.SetAttribute(OP_ATTR_PREFIX + "flag", is_sum); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + if (tileInfo.dstTileInfo.offset[axis] > 0) { | ||
| 81 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, lastCarry != nullptr) | ||
| 82 | + << "carry must be set when cum axis tile offset > 0"; | ||
| 83 | + LogicalTensorPtr lastTile = std::make_shared<LogicalTensor>(function, srcTile->Datatype(), srcTile->GetShape(), | ||
| 84 | + srcTile->GetDynValidShape()); | ||
| 85 | + auto& eop = function.AddOperation("TILE_EXPAND", {lastCarry}, {lastTile}); | ||
| 86 | + eop.SetAttribute(OpAttributeKey::expandDims, std::vector<int>{axis}); | ||
| 87 | + if (is_sum) { | ||
| 88 | + function.AddOperation(Opcode::OP_ADD, {srcTile, lastTile}, {dstTile}); | ||
| 89 | + } else { | ||
| 90 | + function.AddOperation(Opcode::OP_MUL, {srcTile, lastTile}, {dstTile}); | ||
| 91 | + } | ||
| 92 | + } else { | ||
| 93 | + function.AddOperation(Opcode::OP_REGISTER_COPY, {srcTile}, {dstTile}); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + std::vector<int64_t> lastShape = tileInfo.dstTileInfo.shape; | ||
| 97 | + lastShape[axis] = 1; | ||
| 98 | + std::vector<int64_t> lastViewOffset(lastShape.size(), 0); | ||
| 99 | + lastViewOffset[axis] = tileInfo.dstTileInfo.shape[axis] - 1; | ||
| 100 | + lastCarry = dstTile->View(function, lastShape, lastViewOffset); | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +void TiledCumAlongAxis(Function& function, const TileShape& tileShape, const CumOperationPara& cumOperationPara, | ||
| 104 | + CumOperationTileInfoPara& tileInfo) | ||
| 105 | +{ | ||
| 106 | + const int axis = cumOperationPara.axis; | ||
| 107 | + const auto& input = cumOperationPara.input; | ||
| 108 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 109 | + const int64_t cumTileSize = vecTile[axis]; | ||
| 110 | + const int64_t cumDim = input->GetShape()[axis]; | ||
| 111 | + | ||
| 112 | + LogicalTensorPtr lastCarry = nullptr; | ||
| 113 | + for (int64_t i = 0; i < cumDim; i += cumTileSize) { | ||
| 114 | + tileInfo.dstTileInfo.offset[axis] = i; | ||
| 115 | + tileInfo.dstTileInfo.shape[axis] = std::min(cumDim - i, cumTileSize); | ||
| 116 | + tileInfo.inputTileInfo.offset[axis] = i; | ||
| 117 | + tileInfo.inputTileInfo.shape[axis] = std::min(cumDim - i, cumTileSize); | ||
| 118 | + EmitCumAxisTile(function, cumOperationPara, tileInfo, lastCarry); | ||
| 119 | + } | ||
| 120 | +} | ||
| 121 | + | ||
| 122 | +void InnerTiledCumNonAxisDims(size_t idx, const std::vector<int>& nonAxisDims, Function& function, | ||
| 123 | + const TileShape& tileShape, const CumOperationPara& cumOperationPara, | ||
| 124 | + CumOperationTileInfoPara& tileInfo) | ||
| 125 | +{ | ||
| 126 | + if (idx == nonAxisDims.size()) { | ||
| 127 | + TiledCumAlongAxis(function, tileShape, cumOperationPara, tileInfo); | ||
| 128 | + return; | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + const int dim = nonAxisDims[idx]; | ||
| 132 | + const auto& input = cumOperationPara.input; | ||
| 133 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 134 | + const int64_t dimSize = input->GetShape()[dim]; | ||
| 135 | + const int64_t tileSize = vecTile[dim]; | ||
| 136 | + | ||
| 137 | + for (int64_t i = 0; i < dimSize; i += tileSize) { | ||
| 138 | + tileInfo.dstTileInfo.offset[dim] = i; | ||
| 139 | + tileInfo.dstTileInfo.shape[dim] = std::min(dimSize - i, tileSize); | ||
| 140 | + tileInfo.inputTileInfo.offset[dim] = i; | ||
| 141 | + tileInfo.inputTileInfo.shape[dim] = std::min(dimSize - i, tileSize); | ||
| 142 | + InnerTiledCumNonAxisDims(idx + 1, nonAxisDims, function, tileShape, cumOperationPara, tileInfo); | ||
| 143 | + } | ||
| 144 | +} | ||
| 145 | + | ||
| 146 | +} // namespace | ||
| 147 | + | ||
| 148 | +void TiledCumOperation(Function& function, const TileShape& tileShape, const CumOperationPara& cumOperationPara) | ||
| 149 | +{ | ||
| 150 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, | ||
| 151 | + cumOperationPara.input->GetShape().size() == cumOperationPara.input->GetOffset().size()) | ||
| 152 | + << "Shape size and offset size should be equal"; | ||
| 153 | + | ||
| 154 | + const int rank = static_cast<int>(cumOperationPara.input->GetShape().size()); | ||
| 155 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, | ||
| 156 | + rank >= static_cast<int>(MIN_TENSOR_DIM) && rank <= static_cast<int>(MAX_TENSOR_DIM)) | ||
| 157 | + << "CumSum/CumProd tiling supports rank 1-4"; | ||
| 158 | + | ||
| 159 | + CumOperationTileInfoPara tileInfo{ | ||
| 160 | + TileInfo(cumOperationPara.input->GetShape().size(), cumOperationPara.input->GetOffset().size()), | ||
| 161 | + TileInfo(cumOperationPara.dstTensor->GetShape().size(), cumOperationPara.dstTensor->GetOffset().size())}; | ||
| 162 | + | ||
| 163 | + const std::vector<int> nonAxisDims = BuildNonCumDimIndices(rank, cumOperationPara.axis); | ||
| 164 | + InnerTiledCumNonAxisDims(0, nonAxisDims, function, tileShape, cumOperationPara, tileInfo); | ||
| 165 | +} | ||
| 166 | + | ||
| 167 | +LogicalTensorPtr AddCumCast(Function& function, const LogicalTensorPtr& input, DataType dataType, CastMode mode) | ||
| 168 | +{ | ||
| 169 | + auto result = std::make_shared<LogicalTensor>(function, dataType, input->GetShape(), input->GetDynValidShape()); | ||
| 170 | + auto& op = function.AddOperation(Opcode::OP_CAST, {input}, {result}); | ||
| 171 | + op.SetAttribute(OP_ATTR_PREFIX + "mode", mode); | ||
| 172 | + return result; | ||
| 173 | +} | ||
| 174 | + | ||
| 175 | +void AddCumCompute(Function& function, Opcode opcode, const LogicalTensorPtr& input, const LogicalTensorPtr& output, | ||
| 176 | + const CumOperationPara& cumOperationPara) | ||
| 177 | +{ | ||
| 178 | + auto& op = function.AddOperation(opcode, {input}, {output}); | ||
| 179 | + op.SetAttribute(OP_ATTR_PREFIX + "axis", cumOperationPara.axis); | ||
| 180 | + op.SetAttribute(OP_ATTR_PREFIX + "flag", cumOperationPara.is_sum); | ||
| 181 | +} | ||
| 182 | + | ||
| 183 | +void AddCumOutputCast(Function& function, const LogicalTensorPtr& input, const CumOperationPara& cumOperationPara) | ||
| 184 | +{ | ||
| 185 | + cumOperationPara.dstTensor->UpdateDynValidShape(input->GetDynValidShape()); | ||
| 186 | + auto& op = function.AddOperation(Opcode::OP_CAST, {input}, {cumOperationPara.dstTensor}); | ||
| 187 | + op.SetAttribute(OP_ATTR_PREFIX + "mode", CastMode::CAST_NONE); | ||
| 188 | +} | ||
| 189 | + | ||
| 190 | +void TensorCumInt16Operation(Function& function, const CumOperationPara& cumOperationPara) | ||
| 191 | +{ | ||
| 192 | + auto inputFp32 = AddCumCast(function, cumOperationPara.input, DT_FP32, CastMode::CAST_NONE); | ||
| 193 | + auto inputInt32 = AddCumCast(function, inputFp32, DT_INT32, CastMode::CAST_TRUNC); | ||
| 194 | + auto outputInt32 = std::make_shared<LogicalTensor>(function, DT_INT32, cumOperationPara.dstTensor->GetShape(), | ||
| 195 | + inputInt32->GetDynValidShape()); | ||
| 196 | + AddCumCompute(function, Opcode::OP_CUM_SUM, inputInt32, outputInt32, cumOperationPara); | ||
| 197 | + AddCumOutputCast(function, outputInt32, cumOperationPara); | ||
| 198 | +} | ||
| 199 | + | ||
| 200 | +void TensorCumFp16Operation(Function& function, const CumOperationPara& cumOperationPara) | ||
| 201 | +{ | ||
| 202 | + auto inputFp32 = AddCumCast(function, cumOperationPara.input, DT_FP32, CastMode::CAST_NONE); | ||
| 203 | + auto outputFp32 = std::make_shared<LogicalTensor>(function, DT_FP32, cumOperationPara.dstTensor->GetShape(), | ||
| 204 | + inputFp32->GetDynValidShape()); | ||
| 205 | + auto opcode = cumOperationPara.is_sum ? Opcode::OP_CUM_SUM : Opcode::OP_CUM_PROD; | ||
| 206 | + AddCumCompute(function, opcode, inputFp32, outputFp32, cumOperationPara); | ||
| 207 | + AddCumOutputCast(function, outputFp32, cumOperationPara); | ||
| 208 | +} | ||
| 209 | + | ||
| 210 | +void TensorCumInt32Operation(Function& function, const CumOperationPara& cumOperationPara) | ||
| 211 | +{ | ||
| 212 | + auto outputInt32 = std::make_shared<LogicalTensor>(function, DT_INT32, cumOperationPara.dstTensor->GetShape(), | ||
| 213 | + cumOperationPara.input->GetDynValidShape()); | ||
| 214 | + AddCumCompute(function, Opcode::OP_CUM_SUM, cumOperationPara.input, outputInt32, cumOperationPara); | ||
| 215 | + AddCumOutputCast(function, outputInt32, cumOperationPara); | ||
| 216 | +} | ||
| 217 | + | ||
| 218 | +void TensorCumOperation(Function& function, const CumOperationPara& cumOperationPara) | ||
| 219 | +{ | ||
| 220 | + auto dataType = cumOperationPara.input->Datatype(); | ||
| 221 | + if (dataType == DT_INT16) { | ||
| 222 | + TensorCumInt16Operation(function, cumOperationPara); | ||
| 223 | + return; | ||
| 224 | + } | ||
| 225 | + if (dataType == DT_BF16 || dataType == DT_FP16) { | ||
| 226 | + TensorCumFp16Operation(function, cumOperationPara); | ||
| 227 | + return; | ||
| 228 | + } | ||
| 229 | + if (dataType == DT_INT32) { | ||
| 230 | + TensorCumInt32Operation(function, cumOperationPara); | ||
| 231 | + return; | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + cumOperationPara.dstTensor->UpdateDynValidShape(cumOperationPara.input->GetDynValidShape()); | ||
| 235 | + auto opcode = cumOperationPara.is_sum ? Opcode::OP_CUM_SUM : Opcode::OP_CUM_PROD; | ||
| 236 | + AddCumCompute(function, opcode, cumOperationPara.input, cumOperationPara.dstTensor, cumOperationPara); | ||
| 237 | +} | ||
| 238 | + | ||
| 239 | +void CheckCumOperation(const Tensor& input, const int& axis, const bool& is_sum) | ||
| 240 | +{ | ||
| 241 | + if (is_sum) { | ||
| 242 | + std::unordered_set<DataType> supportedTypes = {DT_FP32, DT_FP16, DT_INT32, DT_INT16, DT_BF16}; | ||
| 243 | + CheckTensorDataType(input.GetStorage(), supportedTypes, "CUMSUM"); | ||
| 244 | + CheckTensorDimRange(input.GetStorage(), 1, NUM_VALUE_4, "CUMSUM"); | ||
| 245 | + CheckTensorShapeSize(input.GetStorage(), "CUMSUM"); | ||
| 246 | + } else { | ||
| 247 | + std::unordered_set<DataType> supportedTypes = {DT_FP32, DT_FP16, DT_BF16}; | ||
| 248 | + CheckTensorDataType(input.GetStorage(), supportedTypes, "CUMPROD"); | ||
| 249 | + CheckTensorDimRange(input.GetStorage(), 1, NUM_VALUE_4, "CUMPROD"); | ||
| 250 | + CheckTensorShapeSize(input.GetStorage(), "CUMPROD"); | ||
| 251 | + } | ||
| 252 | + int tmpAxis0 = axis; | ||
| 253 | + CheckAxisRange(input, tmpAxis0); | ||
| 254 | + if (input.GetShape().size() == 1) { | ||
| 255 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, tmpAxis0 == 0) << "when input.GetShape().size() is 1, axis must be 0"; | ||
| 256 | + } | ||
| 257 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, tmpAxis0 == 0 || static_cast<size_t>(tmpAxis0) < input.GetShape().size()) | ||
| 258 | + << "The tmpAxis0 should be 0 and less than shape size"; | ||
| 259 | +} | ||
| 260 | + | ||
| 261 | +Tensor CumOperation(const Tensor& input, const int& axis, const bool& is_sum) | ||
| 262 | +{ | ||
| 263 | + DECLARE_TRACER(); | ||
| 264 | + CheckCumOperation(input, axis, is_sum); | ||
| 265 | + | ||
| 266 | + auto resultDType = input.GetDataType(); | ||
| 267 | + int shapeSize = input.GetShape().size(); | ||
| 268 | + int tmpAxis0 = axis < 0 ? shapeSize + axis : axis; | ||
| 269 | + | ||
| 270 | + if (resultDType == DataType::DT_INT16 || resultDType == DataType::DT_INT32) { | ||
| 271 | + resultDType = DataType::DT_INT64; | ||
| 272 | + } | ||
| 273 | + | ||
| 274 | + const int n_1 = shapeSize - 1; | ||
| 275 | + const int n_2 = shapeSize - NUM_VALUE_2; | ||
| 276 | + if ((resultDType != DataType::DT_INT64) && tmpAxis0 > 0 && tmpAxis0 == n_1) { | ||
| 277 | + Tensor tmpInput = Transpose(input, {n_2, n_1}); | ||
| 278 | + const int transposedAxis = n_2; | ||
| 279 | + | ||
| 280 | + VecTile oriVectile = TileShape::Current().GetVecTile(); | ||
| 281 | + VecTile tmpVectile = TileShape::Current().GetVecTile(); | ||
| 282 | + int64_t tmp = tmpVectile.tile[n_2]; | ||
| 283 | + tmpVectile.tile[n_2] = tmpVectile.tile[n_1]; | ||
| 284 | + tmpVectile.tile[n_1] = tmp; | ||
| 285 | + TileShape::Current().SetVecTile(tmpVectile); | ||
| 286 | + | ||
| 287 | + auto tmpValidShape = input.GetStorage()->dynValidShape_; | ||
| 288 | + SymbolicScalar tmpValid = tmpValidShape[n_2]; | ||
| 289 | + tmpValidShape[n_2] = tmpValidShape[n_1]; | ||
| 290 | + tmpValidShape[n_1] = tmpValid; | ||
| 291 | + | ||
| 292 | + Tensor result(tmpInput.GetDataType(), tmpInput.GetShape()); | ||
| 293 | + CALL(CumOperation, *Program::GetInstance().GetCurrentFunction(), | ||
| 294 | + {tmpInput.GetStorage(), result.GetStorage(), transposedAxis, is_sum}); | ||
| 295 | + Tensor tmpresult = Transpose(result, {n_2, n_1}); | ||
| 296 | + TileShape::Current().SetVecTile(oriVectile); | ||
| 297 | + return tmpresult; | ||
| 298 | + } else { | ||
| 299 | + Tensor result(resultDType, input.GetShape()); | ||
| 300 | + CALL(CumOperation, *Program::GetInstance().GetCurrentFunction(), | ||
| 301 | + {input.GetStorage(), result.GetStorage(), tmpAxis0, is_sum}); | ||
| 302 | + return result; | ||
| 303 | + } | ||
| 304 | +} | ||
| 305 | + | ||
| 306 | +Tensor CumSum(const Tensor& input, const int& axis) | ||
| 307 | +{ | ||
| 308 | + DECLARE_TRACER(); | ||
| 309 | + CheckTensorFormat(input.GetStorage(), {TileOpFormat::TILEOP_NZ}, "CumSum"); | ||
| 310 | + | ||
| 311 | + bool is_sum = true; | ||
| 312 | + Tensor result = CumOperation(input, axis, is_sum); | ||
| 313 | + return result; | ||
| 314 | +} | ||
| 315 | + | ||
| 316 | +Tensor CumProd(const Tensor& input, const int& axis) | ||
| 317 | +{ | ||
| 318 | + DECLARE_TRACER(); | ||
| 319 | + CheckTensorFormat(input.GetStorage(), {TileOpFormat::TILEOP_NZ}, "CumProd"); | ||
| 320 | + | ||
| 321 | + bool is_sum = false; | ||
| 322 | + Tensor result = CumOperation(input, axis, is_sum); | ||
| 323 | + return result; | ||
| 324 | +} | ||
| 325 | + | ||
| 326 | +void CumSumOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 327 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 328 | + const std::vector<LogicalTensorPtr>& oOperand, const Operation& op) | ||
| 329 | +{ | ||
| 330 | + int axis = op.GetIntAttribute(OP_ATTR_PREFIX + "axis"); | ||
| 331 | + bool is_sum = op.GetBoolAttribute(OP_ATTR_PREFIX + "flag"); | ||
| 332 | + TiledCumOperation(function, tileShape, {iOperand[0], oOperand[0], axis, is_sum}); | ||
| 333 | +} | ||
| 334 | + | ||
| 335 | +REGISTER_OPERATION_TILED_FUNC(OP_CUM_SUM, Opcode::OP_CUM_SUM, CumSumOperationTileFunc); | ||
| 336 | +REGISTER_OPERATION_TILED_FUNC(OP_CUM_PROD, Opcode::OP_CUM_PROD, CumSumOperationTileFunc); | ||
| 337 | + | ||
| 338 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,246 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file dequantize.cpp | ||
| 13 | + * \brief Quantization operation implementation for INT8 symmetric and asymmetric quantization | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "interface/utils/operator_tracer.h" | ||
| 17 | +#include "interface/operation/opcode.h" | ||
| 18 | +#include "interface/operation/operation_common.h" | ||
| 19 | +#include "interface/function/function.h" | ||
| 20 | +#include "interface/program/program.h" | ||
| 21 | +#include "interface/operation/vector/tensor_transformation.h" | ||
| 22 | + | ||
| 23 | +#include <unordered_set> | ||
| 24 | + | ||
| 25 | +namespace npu::tile_fwk { | ||
| 26 | + | ||
| 27 | +namespace { | ||
| 28 | +constexpr size_t QUANT_MX_MIN_RANK = 1; | ||
| 29 | + | ||
| 30 | +// ============================================================================= | ||
| 31 | +// Dequantization Operations (INT8/INT16 -> FP32) | ||
| 32 | +// TDequant always requires 4 params: dst, src, scale, offset (symmetric: offset=0) | ||
| 33 | +// ============================================================================= | ||
| 34 | + | ||
| 35 | +void TiledDequantize(Function& function, const TileShape& tileShape, size_t cur, Input& srcInput, Input& scaleInput, | ||
| 36 | + Input& offsetInput, Input& dstInput, int64_t axis) | ||
| 37 | +{ | ||
| 38 | + if (cur == dstInput.tensor.GetShape().size()) { | ||
| 39 | + auto srcTile = srcInput.tensor.GetStorage()->View(function, srcInput.tileInfo.shape, srcInput.tileInfo.offset); | ||
| 40 | + auto scaleTile = scaleInput.tensor.GetStorage()->View(function, scaleInput.tileInfo.shape, | ||
| 41 | + scaleInput.tileInfo.offset); | ||
| 42 | + auto offsetTile = offsetInput.tensor.GetStorage()->View(function, offsetInput.tileInfo.shape, | ||
| 43 | + offsetInput.tileInfo.offset); | ||
| 44 | + auto dstTile = dstInput.tensor.GetStorage()->View(function, dstInput.tileInfo.shape, dstInput.tileInfo.offset); | ||
| 45 | + | ||
| 46 | + auto& op = function.AddOperation(Opcode::OP_DEQUANTIZE, {srcTile, scaleTile, offsetTile}, {dstTile}); | ||
| 47 | + op.SetAttribute(OP_ATTR_PREFIX + "axis", axis); | ||
| 48 | + return; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 52 | + for (int64_t i = 0; i < dstInput.tensor.GetShape()[cur]; i += vecTile[cur]) { | ||
| 53 | + dstInput.tileInfo.shape[cur] = std::min(dstInput.tensor.GetShape()[cur] - i, vecTile[cur]); | ||
| 54 | + dstInput.tileInfo.offset[cur] = i; | ||
| 55 | + | ||
| 56 | + if (cur < srcInput.tensor.GetShape().size()) { | ||
| 57 | + srcInput.tileInfo.shape[cur] = std::min(srcInput.tensor.GetShape()[cur] - i, vecTile[cur]); | ||
| 58 | + srcInput.tileInfo.offset[cur] = i; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + if (cur < scaleInput.tensor.GetShape().size()) { | ||
| 62 | + int64_t scaleIdx = i % scaleInput.tensor.GetShape()[cur]; | ||
| 63 | + scaleInput.tileInfo.shape[cur] = std::min(scaleInput.tensor.GetShape()[cur] - scaleIdx, vecTile[cur]); | ||
| 64 | + scaleInput.tileInfo.offset[cur] = scaleIdx; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + if (cur < offsetInput.tensor.GetShape().size()) { | ||
| 68 | + int64_t offsetIdx = i % offsetInput.tensor.GetShape()[cur]; | ||
| 69 | + offsetInput.tileInfo.shape[cur] = std::min(offsetInput.tensor.GetShape()[cur] - offsetIdx, vecTile[cur]); | ||
| 70 | + offsetInput.tileInfo.offset[cur] = offsetIdx; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + TiledDequantize(function, tileShape, cur + 1, srcInput, scaleInput, offsetInput, dstInput, axis); | ||
| 74 | + } | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | +void TiledDequantize(Function& function, const TileShape& tileShape, const LogicalTensorPtr& src, | ||
| 78 | + const LogicalTensorPtr& scale, const LogicalTensorPtr& offset, const LogicalTensorPtr& dst, | ||
| 79 | + int64_t axis) | ||
| 80 | +{ | ||
| 81 | + TileInfo srcTileInfo(src->shape.size(), src->offset.size()); | ||
| 82 | + TileInfo scaleTileInfo(scale->shape.size(), scale->offset.size()); | ||
| 83 | + TileInfo offsetTileInfo(offset->shape.size(), offset->offset.size()); | ||
| 84 | + TileInfo dstTileInfo(dst->shape.size(), dst->offset.size()); | ||
| 85 | + | ||
| 86 | + auto srcInput = Input{Tensor(src), srcTileInfo}; | ||
| 87 | + auto scaleInput = Input{Tensor(scale), scaleTileInfo}; | ||
| 88 | + auto offsetInput = Input{Tensor(offset), offsetTileInfo}; | ||
| 89 | + auto dstInput = Input{Tensor(dst), dstTileInfo}; | ||
| 90 | + | ||
| 91 | + TiledDequantize(function, tileShape, 0, srcInput, scaleInput, offsetInput, dstInput, axis); | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +LogicalTensorPtr TensorDequantizeOperation(Function& function, const LogicalTensorPtr& src, | ||
| 95 | + const LogicalTensorPtr& scale, const LogicalTensorPtr& offset, int64_t axis) | ||
| 96 | +{ | ||
| 97 | + auto result = std::make_shared<LogicalTensor>(function, DataType::DT_FP32, src->shape, src->GetDynValidShape()); | ||
| 98 | + auto& op = function.AddOperation(Opcode::OP_DEQUANTIZE, {src, scale, offset}, {result}); | ||
| 99 | + op.SetAttribute(OP_ATTR_PREFIX + "axis", axis); | ||
| 100 | + return result; | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +// Helper: create zero tensor for symmetric dequantization | ||
| 104 | +static LogicalTensorPtr CreateZeroOffsetTensor(Function& function, const LogicalTensorPtr& scale) | ||
| 105 | +{ | ||
| 106 | + Element zeroVal(DataType::DT_FP32, (int64_t)0); | ||
| 107 | + | ||
| 108 | + Tensor zeroTensor = TensorFullOperation(function, zeroVal, SymbolicScalar(), DataType::DT_FP32, scale->shape, | ||
| 109 | + scale->GetDynValidShape()); | ||
| 110 | + | ||
| 111 | + return zeroTensor.GetStorage(); | ||
| 112 | +} | ||
| 113 | + | ||
| 114 | +Tensor PrepareDequantize1DInput(const Tensor& input, VecTile& originalVecTile) | ||
| 115 | +{ | ||
| 116 | + originalVecTile = TileShape::Current().GetVecTile(); | ||
| 117 | + if (!originalVecTile.tile.empty()) { | ||
| 118 | + VecTile extendedVecTile = originalVecTile; | ||
| 119 | + extendedVecTile.tile.insert(extendedVecTile.tile.begin(), 1); | ||
| 120 | + TileShape::Current().SetVecTile(extendedVecTile); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + std::vector<int64_t> newShape = {1, input.GetShape()[0]}; | ||
| 124 | + auto originalValidShape = input.GetStorage()->GetDynValidShape(); | ||
| 125 | + std::vector<SymbolicScalar> extendedValidShape; | ||
| 126 | + if (!originalValidShape.empty()) { | ||
| 127 | + extendedValidShape.push_back(SymbolicScalar(1)); | ||
| 128 | + extendedValidShape.push_back(originalValidShape[0]); | ||
| 129 | + } | ||
| 130 | + return Reshape(input, newShape, extendedValidShape); | ||
| 131 | +} | ||
| 132 | + | ||
| 133 | +Tensor RestoreDequantize1DResult(Tensor result, const Tensor& input, const VecTile& originalVecTile) | ||
| 134 | +{ | ||
| 135 | + std::vector<int64_t> originalShape = {input.GetShape()[0]}; | ||
| 136 | + auto originalValidShape = input.GetStorage()->GetDynValidShape(); | ||
| 137 | + result = Reshape(result, originalShape, originalValidShape); | ||
| 138 | + if (!originalVecTile.tile.empty()) { | ||
| 139 | + TileShape::Current().SetVecTile(originalVecTile); | ||
| 140 | + } | ||
| 141 | + return result; | ||
| 142 | +} | ||
| 143 | + | ||
| 144 | +Tensor DequantizeAlongLastAxis(const Tensor& input, const Tensor& scale, int axis, const Tensor& zeroPoints) | ||
| 145 | +{ | ||
| 146 | + if (zeroPoints.GetStorage() != nullptr) { | ||
| 147 | + return CALL(DequantizeOperation, *Program::GetInstance().GetCurrentFunction(), input.GetStorage(), | ||
| 148 | + scale.GetStorage(), zeroPoints.GetStorage(), axis); | ||
| 149 | + } | ||
| 150 | + auto zeroOffset = CreateZeroOffsetTensor(*Program::GetInstance().GetCurrentFunction(), scale.GetStorage()); | ||
| 151 | + return CALL(DequantizeOperation, *Program::GetInstance().GetCurrentFunction(), input.GetStorage(), | ||
| 152 | + scale.GetStorage(), zeroOffset, axis); | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +Tensor DequantizeAlongSecondLastAxis(const Tensor& input, const Tensor& scale, const Tensor& zeroPoints) | ||
| 156 | +{ | ||
| 157 | + int ndim = static_cast<int>(input.GetShape().size()); | ||
| 158 | + int lastDim = ndim - 1; | ||
| 159 | + int secondLastDim = ndim - NUM_VALUE_2; | ||
| 160 | + Tensor transposedInput = Transpose(input, {secondLastDim, lastDim}); | ||
| 161 | + VecTile oriVectile = TileShape::Current().GetVecTile(); | ||
| 162 | + VecTile tmpVectile = TileShape::Current().GetVecTile(); | ||
| 163 | + std::swap(tmpVectile[secondLastDim], tmpVectile[lastDim]); | ||
| 164 | + TileShape::Current().SetVecTile(tmpVectile); | ||
| 165 | + auto tmpValidShape = input.GetStorage()->dynValidShape_; | ||
| 166 | + std::swap(tmpValidShape[secondLastDim], tmpValidShape[lastDim]); | ||
| 167 | + transposedInput.GetStorage()->UpdateDynValidShape(tmpValidShape); | ||
| 168 | + | ||
| 169 | + Tensor dequantizedResult = DequantizeAlongLastAxis(transposedInput, scale, -1, zeroPoints); | ||
| 170 | + dequantizedResult.GetStorage()->UpdateDynValidShape(tmpValidShape); | ||
| 171 | + TileShape::Current().SetVecTile(tmpVectile); | ||
| 172 | + Tensor result = Transpose(dequantizedResult, {secondLastDim, lastDim}); | ||
| 173 | + result.GetStorage()->UpdateDynValidShape(input.GetStorage()->dynValidShape_); | ||
| 174 | + TileShape::Current().SetVecTile(oriVectile); | ||
| 175 | + return result; | ||
| 176 | +} | ||
| 177 | + | ||
| 178 | +} // namespace | ||
| 179 | + | ||
| 180 | +// Public Dequantize API | ||
| 181 | +Tensor Dequantize(const Tensor& input, const Tensor& scale, DataType otype, int axis, const Tensor& zeroPoints) | ||
| 182 | +{ | ||
| 183 | + DECLARE_TRACER(); | ||
| 184 | + CheckTensorFormat(input.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Dequantize"); | ||
| 185 | + CheckTensorFormat(scale.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Dequantize"); | ||
| 186 | + if (zeroPoints.GetStorage() != nullptr) { | ||
| 187 | + CheckTensorFormat(zeroPoints.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Dequantize"); | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + CHECK(VectorErrorCode::ERR_PARAM_SHAPE_DIM_UNSUPPORTED, | ||
| 191 | + input.GetShape().size() >= SHAPE_DIM1 && input.GetShape().size() <= SHAPE_DIM5) | ||
| 192 | + << "The shape.size() only supports 1~5"; | ||
| 193 | + bool is1DInput = (input.GetShape().size() == 1); | ||
| 194 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, !(is1DInput && axis == -NUM_VALUE_2)) | ||
| 195 | + << "1D input only supports axis=-1, axis=-2 is not supported"; | ||
| 196 | + | ||
| 197 | + VecTile originalVecTile; | ||
| 198 | + Tensor processedInput = is1DInput ? PrepareDequantize1DInput(input, originalVecTile) : input; | ||
| 199 | + | ||
| 200 | + CHECK(VectorErrorCode::ERR_PARAM_DTYPE_UNSUPPORTED, | ||
| 201 | + input.GetDataType() == DataType::DT_INT8 || input.GetDataType() == DataType::DT_INT16) | ||
| 202 | + << "Dequantize input dtype must be INT8 or INT16, but got dtype=" << static_cast<int>(input.GetDataType()); | ||
| 203 | + CHECK(VectorErrorCode::ERR_PARAM_DTYPE_UNSUPPORTED, otype == DataType::DT_FP32) | ||
| 204 | + << "Dequantize output type must be FP32, but got dtype=" << static_cast<int>(otype); | ||
| 205 | + | ||
| 206 | + int ndim = static_cast<int>(processedInput.GetShape().size()); | ||
| 207 | + int normalizedAxis = axis >= 0 ? axis - ndim : axis; | ||
| 208 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, normalizedAxis == -NUM_VALUE_1 || normalizedAxis == -NUM_VALUE_2) | ||
| 209 | + << "Dequantize axis must be -1 (per-row) or -2 (per-column), but got axis=" << axis | ||
| 210 | + << " (normalized=" << normalizedAxis << ")"; | ||
| 211 | + | ||
| 212 | + Tensor result = normalizedAxis == -NUM_VALUE_2 ? | ||
| 213 | + DequantizeAlongSecondLastAxis(processedInput, scale, zeroPoints) : | ||
| 214 | + DequantizeAlongLastAxis(processedInput, scale, normalizedAxis, zeroPoints); | ||
| 215 | + return is1DInput ? RestoreDequantize1DResult(result, input, originalVecTile) : result; | ||
| 216 | +} | ||
| 217 | + | ||
| 218 | +Tensor DequantizeSymmetric(const Tensor& src, const Tensor& scale, int64_t axis) | ||
| 219 | +{ | ||
| 220 | + CheckTensorFormat(src.GetStorage(), {TileOpFormat::TILEOP_NZ}, "DequantizeSymmetric"); | ||
| 221 | + CheckTensorFormat(scale.GetStorage(), {TileOpFormat::TILEOP_NZ}, "DequantizeSymmetric"); | ||
| 222 | + | ||
| 223 | + return Dequantize(src, scale, DataType::DT_FP32, axis, Tensor()); | ||
| 224 | +} | ||
| 225 | + | ||
| 226 | +Tensor DequantizeAsymmetric(const Tensor& src, const Tensor& scale, const Tensor& zeroPoints, int64_t axis) | ||
| 227 | +{ | ||
| 228 | + CheckTensorFormat(src.GetStorage(), {TileOpFormat::TILEOP_NZ}, "DequantizeAsymmetric"); | ||
| 229 | + CheckTensorFormat(scale.GetStorage(), {TileOpFormat::TILEOP_NZ}, "DequantizeAsymmetric"); | ||
| 230 | + CheckTensorFormat(zeroPoints.GetStorage(), {TileOpFormat::TILEOP_NZ}, "DequantizeAsymmetric"); | ||
| 231 | + | ||
| 232 | + return Dequantize(src, scale, DataType::DT_FP32, axis, zeroPoints); | ||
| 233 | +} | ||
| 234 | + | ||
| 235 | +// Tile Function Registration | ||
| 236 | +void DequantizeOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 237 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 238 | + const std::vector<LogicalTensorPtr>& oOperand, const Operation& op) | ||
| 239 | +{ | ||
| 240 | + int64_t axis = op.GetIntAttribute(OP_ATTR_PREFIX + "axis"); | ||
| 241 | + TiledDequantize(function, tileShape, iOperand[0], iOperand[1], iOperand[NUM_VALUE_2], oOperand[0], axis); | ||
| 242 | +} | ||
| 243 | + | ||
| 244 | +REGISTER_OPERATION_TILED_FUNC(OP_DEQUANTIZE, Opcode::OP_DEQUANTIZE, DequantizeOperationTileFunc); | ||
| 245 | + | ||
| 246 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,97 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file error_function.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "unary_tiled.h" | ||
| 17 | +#include "binary.h" | ||
| 18 | +#include "tensor_transformation.h" | ||
| 19 | +#include "interface/utils/operator_tracer.h" | ||
| 20 | +#include "passes/pass_utils/graph_utils.h" | ||
| 21 | +#include "tilefwk/error_code.h" | ||
| 22 | + | ||
| 23 | +namespace npu::tile_fwk { | ||
| 24 | + | ||
| 25 | +Tensor Erf(const Tensor& self) | ||
| 26 | +{ | ||
| 27 | + DECLARE_TRACER(); | ||
| 28 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Erf"); | ||
| 29 | + | ||
| 30 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_FP32}; | ||
| 31 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "Erf"); | ||
| 32 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "Erf"); | ||
| 33 | + CheckTensorShapeSize(self.GetStorage(), "Erf"); | ||
| 34 | + | ||
| 35 | + auto castSelf = Cast(self, DataType::DT_FP32); | ||
| 36 | + auto result = CALL(UnaryOperation<UnaryOpType::ERF>, *Program::GetInstance().GetCurrentFunction(), | ||
| 37 | + castSelf.GetStorage()); | ||
| 38 | + auto castResult = Cast(result, self.GetDataType()); | ||
| 39 | + return castResult; | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +Tensor Erfc(const Tensor& self) | ||
| 43 | +{ | ||
| 44 | + DECLARE_TRACER(); | ||
| 45 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Erfc"); | ||
| 46 | + | ||
| 47 | + std::unordered_set<DataType> supportedTypes = {DT_BF16, DT_FP16, DT_FP32}; | ||
| 48 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "Erfc"); | ||
| 49 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "Erfc"); | ||
| 50 | + CheckTensorShapeSize(self.GetStorage(), "Erfc"); | ||
| 51 | + if (self.GetDataType() != DataType::DT_FP32) { | ||
| 52 | + auto castSelf = Cast(self, DataType::DT_FP32); | ||
| 53 | + auto result = CALL(UnaryOperation<UnaryOpType::ERFC>, *Program::GetInstance().GetCurrentFunction(), | ||
| 54 | + castSelf.GetStorage()); | ||
| 55 | + auto castResult = Cast(result, self.GetDataType()); | ||
| 56 | + return castResult; | ||
| 57 | + } | ||
| 58 | + auto result = CALL(UnaryOperation<UnaryOpType::ERFC>, *Program::GetInstance().GetCurrentFunction(), | ||
| 59 | + self.GetStorage()); | ||
| 60 | + return result; | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +void ErfOperationTileFunc(Function& function, const TileShape& tileShape, const std::vector<LogicalTensorPtr>& iOperand, | ||
| 64 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 65 | +{ | ||
| 66 | + UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 67 | + auto shape = tileShape.GetVecTile().tile; | ||
| 68 | + std::vector<int64_t> tmpShape; | ||
| 69 | + tmpShape.assign(shape.begin(), shape.end()); | ||
| 70 | + auto alignSize = BLOCK_SIZE / BytesOf(DT_FP32); | ||
| 71 | + tmpShape[tmpShape.size() - 1] = (tmpShape[tmpShape.size() - 1] + alignSize - 1) / alignSize * alignSize; | ||
| 72 | + // 3个中间变量 | ||
| 73 | + uint64_t intermediateBytes = static_cast<int64_t>(BytesOf(DT_FP32)) * NUM_VALUE_3 * | ||
| 74 | + std::accumulate(tmpShape.begin(), tmpShape.end(), 1LL, std::multiplies<int64_t>()); | ||
| 75 | + | ||
| 76 | + return TiledUnaryOperation<UnaryOpType::ERF>(function, tileShape, iOperand[0], oOperand[0], intermediateBytes); | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +void ErfcOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 80 | + const std::vector<LogicalTensorPtr>& iOperand, const std::vector<LogicalTensorPtr>& oOperand, | ||
| 81 | + [[maybe_unused]] const Operation& op) | ||
| 82 | +{ | ||
| 83 | + UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 84 | + auto shape = tileShape.GetVecTile().tile; | ||
| 85 | + std::vector<int64_t> tmpShape; | ||
| 86 | + tmpShape.assign(shape.begin(), shape.end()); | ||
| 87 | + auto alignSize = BLOCK_SIZE / BytesOf(DT_FP32); | ||
| 88 | + tmpShape[tmpShape.size() - 1] = (tmpShape[tmpShape.size() - 1] + alignSize - 1) / alignSize * alignSize; | ||
| 89 | + uint64_t intermediateBytes = static_cast<int64_t>(BytesOf(DT_FP32)) * NUM_VALUE_4 * | ||
| 90 | + std::accumulate(tmpShape.begin(), tmpShape.end(), 1LL, std::multiplies<int64_t>()); | ||
| 91 | + return TiledUnaryOperation<UnaryOpType::ERFC>(function, tileShape, iOperand[0], oOperand[0], intermediateBytes); | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +REGISTER_OPERATION_TILED_FUNC(OP_ERF, Opcode::OP_ERF, ErfOperationTileFunc); | ||
| 95 | +REGISTER_OPERATION_TILED_FUNC(OP_ERFC, Opcode::OP_ERFC, ErfcOperationTileFunc); | ||
| 96 | + | ||
| 97 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,247 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file expand.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "unary.h" | ||
| 17 | +#include <sstream> | ||
| 18 | +#include <string> | ||
| 19 | +#include "tensor_transformation.h" | ||
| 20 | +#include "interface/utils/operator_tracer.h" | ||
| 21 | +#include "tilefwk/error_code.h" | ||
| 22 | +#include "tilefwk/platform.h" | ||
| 23 | + | ||
| 24 | +namespace npu::tile_fwk { | ||
| 25 | + | ||
| 26 | +struct ExpandInfo { | ||
| 27 | + const std::shared_ptr<LogicalTensor>& srcTensor; | ||
| 28 | + const std::shared_ptr<LogicalTensor>& result; | ||
| 29 | + std::vector<int64_t>& viewShape; | ||
| 30 | + std::vector<int64_t>& offset; | ||
| 31 | + const std::vector<int> expandDims; | ||
| 32 | + ExpandInfo(const std::shared_ptr<LogicalTensor>& srcTensor0, const std::shared_ptr<LogicalTensor>& result0, | ||
| 33 | + std::vector<int64_t>& viewShape0, std::vector<int64_t>& offset0, const std::vector<int> expandDims0) | ||
| 34 | + : srcTensor(srcTensor0), result(result0), viewShape(viewShape0), offset(offset0), expandDims(expandDims0) | ||
| 35 | + {} | ||
| 36 | +}; | ||
| 37 | + | ||
| 38 | +void CheckExpandTensorValid(const LogicalTensorPtr& operand, const LogicalTensorPtr& result) | ||
| 39 | +{ | ||
| 40 | + const auto& operand_shape = operand->shape; | ||
| 41 | + const auto& result_shape = result->shape; | ||
| 42 | + | ||
| 43 | + if (operand_shape.size() != result_shape.size()) { | ||
| 44 | + std::ostringstream oss; | ||
| 45 | + oss << "The number of dimensions must match! " | ||
| 46 | + << "Operand shape: " << operand_shape.size() << "D (" << operand_shape << ") " | ||
| 47 | + << "Result shape: " << result_shape.size() << "D (" << result_shape << ")"; | ||
| 48 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << oss.str(); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + for (size_t i = 0; i < result_shape.size(); ++i) { | ||
| 52 | + if (operand_shape[i] != result_shape[i] && operand_shape[i] != 1) { | ||
| 53 | + std::ostringstream oss; | ||
| 54 | + oss << "The size of tensor a (" << operand_shape[i] << ") must match the size of tensor b (" | ||
| 55 | + << result_shape[i] << ") at non-singleton dimension " << i << ". " | ||
| 56 | + << "Operand shape: (" << operand_shape << ") " | ||
| 57 | + << "Result shape: (" << result_shape << ")"; | ||
| 58 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << oss.str(); | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +void ExpandTile(Function& function, const struct ExpandInfo& expandInfo) | ||
| 64 | +{ | ||
| 65 | + auto resultTile = expandInfo.result->View(function, expandInfo.viewShape, expandInfo.offset); | ||
| 66 | + | ||
| 67 | + std::vector<int64_t> srcShape(expandInfo.srcTensor->shape.size(), 1); | ||
| 68 | + for (size_t i = 0; i < expandInfo.result->shape.size(); i++) { | ||
| 69 | + srcShape[i] = std::min(expandInfo.viewShape[i], expandInfo.srcTensor->shape[i]); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + std::vector<int64_t> srcOffset = expandInfo.offset; | ||
| 73 | + for (size_t j = 0; j < srcOffset.size(); j++) { | ||
| 74 | + if (expandInfo.srcTensor->shape[j] < expandInfo.result->shape[j]) { | ||
| 75 | + srcOffset[j] = expandInfo.offset[j] % expandInfo.srcTensor->shape[j]; | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + auto srcTile = expandInfo.srcTensor->View(function, srcShape, srcOffset); | ||
| 79 | + auto& newOp = function.AddOperation("TILE_EXPAND", {srcTile}, {resultTile}); | ||
| 80 | + newOp.SetAttribute(OpAttributeKey::expandDims, expandInfo.expandDims); | ||
| 81 | + newOp.SetAttribute(OP_ATTR_PREFIX + "validShape", resultTile->GetDynValidShape()); | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +void ExpandTile(Function& function, const TileShape& tileShape, int dimIdx, const struct ExpandInfo& expandInfo, | ||
| 85 | + std::vector<SymbolicScalar> validShape) | ||
| 86 | +{ | ||
| 87 | + if (static_cast<size_t>(dimIdx) == expandInfo.result->shape.size()) { | ||
| 88 | + ExpandTile(function, expandInfo); | ||
| 89 | + return; | ||
| 90 | + } | ||
| 91 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 92 | + for (int i = 0; i < expandInfo.result->shape[dimIdx]; i += vecTile[dimIdx]) { | ||
| 93 | + expandInfo.offset[dimIdx] = i; | ||
| 94 | + expandInfo.viewShape[dimIdx] = std::min(expandInfo.result->shape[dimIdx] - i, | ||
| 95 | + static_cast<int64_t>(vecTile[dimIdx])); | ||
| 96 | + ExpandTile(function, tileShape, dimIdx + 1, expandInfo, validShape); | ||
| 97 | + } | ||
| 98 | +} | ||
| 99 | + | ||
| 100 | +void Expand(Function& function, const TileShape& tileShape, const LogicalTensorPtr& operand, | ||
| 101 | + const std::vector<LogicalTensorPtr>& other, const LogicalTensorPtr& result) | ||
| 102 | +{ | ||
| 103 | + CheckExpandTensorValid(operand, result); | ||
| 104 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, function.GetGraphType() == GraphType::TILE_GRAPH) | ||
| 105 | + << "The GetGraphType of function is incorrect"; | ||
| 106 | + std::vector<int64_t> offset(result->shape.size(), 0); | ||
| 107 | + std::vector<int64_t> viewShape(result->shape.size(), 1); | ||
| 108 | + std::vector<SymbolicScalar> outValidShape; | ||
| 109 | + std::vector<int> expandDims; | ||
| 110 | + for (size_t i = 0; i < result->shape.size(); ++i) { | ||
| 111 | + if (operand->shape[i] != result->shape[i]) { | ||
| 112 | + expandDims.push_back(i); | ||
| 113 | + for (auto it : other) { | ||
| 114 | + if (it != nullptr && it->shape[i] == result->shape[i]) { | ||
| 115 | + if (it->GetDynValidShape().empty()) { | ||
| 116 | + outValidShape.push_back(it->shape[i]); | ||
| 117 | + } else { | ||
| 118 | + outValidShape.push_back(it->GetDynValidShape()[i]); | ||
| 119 | + } | ||
| 120 | + break; | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + } else { | ||
| 124 | + if (operand->GetDynValidShape().empty()) { | ||
| 125 | + outValidShape.push_back(operand->shape[i]); | ||
| 126 | + } else { | ||
| 127 | + outValidShape.push_back(operand->GetDynValidShape()[i]); | ||
| 128 | + } | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + result->UpdateDynValidShape(outValidShape); | ||
| 133 | + struct ExpandInfo expandInfo(operand, result, viewShape, offset, expandDims); | ||
| 134 | + ExpandTile(function, tileShape, 0, expandInfo, outValidShape); | ||
| 135 | +} | ||
| 136 | + | ||
| 137 | +void ExpandWithResultValidShape(Function& function, const TileShape& tileShape, const LogicalTensorPtr& operand, | ||
| 138 | + const LogicalTensorPtr& result, const std::vector<SymbolicScalar> resultValidShape) | ||
| 139 | +{ | ||
| 140 | + CheckExpandTensorValid(operand, result); | ||
| 141 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, function.GetGraphType() == GraphType::TILE_GRAPH) | ||
| 142 | + << "The GetGraphType of function is incorrect"; | ||
| 143 | + std::vector<int64_t> offset(result->shape.size(), 0); | ||
| 144 | + std::vector<int64_t> viewShape(result->shape.size(), 1); | ||
| 145 | + std::vector<int> expandDims; | ||
| 146 | + for (size_t i = 0; i < result->shape.size(); ++i) { | ||
| 147 | + if (operand->shape[i] != result->shape[i]) { | ||
| 148 | + expandDims.push_back(i); | ||
| 149 | + } | ||
| 150 | + } | ||
| 151 | + result->UpdateDynValidShape(resultValidShape); | ||
| 152 | + struct ExpandInfo expandInfo(operand, result, viewShape, offset, expandDims); | ||
| 153 | + ExpandTile(function, tileShape, 0, expandInfo, resultValidShape); | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +void TiledExpand(Function& function, const TileShape& tileShape, const LogicalTensorPtr& operand, | ||
| 157 | + const LogicalTensorPtr& result, const std::vector<SymbolicScalar>& validShape) | ||
| 158 | +{ | ||
| 159 | + CheckExpandTensorValid(operand, result); | ||
| 160 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, function.GetGraphType() == GraphType::TILE_GRAPH) | ||
| 161 | + << "The GetGraphType of function is incorrect"; | ||
| 162 | + | ||
| 163 | + std::vector<int64_t> offset(result->shape.size(), 0); | ||
| 164 | + std::vector<int64_t> viewShape(result->shape.size(), 1); | ||
| 165 | + std::vector<int> expandDims; | ||
| 166 | + for (size_t i = 0; i < result->shape.size(); ++i) { | ||
| 167 | + if (operand->shape[i] != result->shape[i]) { | ||
| 168 | + expandDims.push_back(i); | ||
| 169 | + } | ||
| 170 | + } | ||
| 171 | + result->UpdateDynValidShape(validShape); | ||
| 172 | + struct ExpandInfo expandInfo(operand, result, viewShape, offset, expandDims); | ||
| 173 | + ExpandTile(function, tileShape, 0, expandInfo, validShape); | ||
| 174 | +} | ||
| 175 | + | ||
| 176 | +Tensor TensorExpandOperation(Function& function, const LogicalTensorPtr& operand, const std::vector<int64_t>& dstShape, | ||
| 177 | + const std::vector<SymbolicScalar>& validShape) | ||
| 178 | +{ | ||
| 179 | + auto result = std::make_shared<LogicalTensor>(function, operand->Datatype(), dstShape, validShape); | ||
| 180 | + auto& op = function.AddOperation(Opcode::OP_EXPAND, {operand}, {result}); | ||
| 181 | + | ||
| 182 | + op.SetAttribute(OP_ATTR_PREFIX + "shape", dstShape); | ||
| 183 | + op.SetAttribute(OP_ATTR_PREFIX + "validShape", validShape); | ||
| 184 | + return result; | ||
| 185 | +} | ||
| 186 | + | ||
| 187 | +Tensor TensorJustNeedCopyOperation(Function& function, const LogicalTensorPtr& operand, | ||
| 188 | + const std::vector<int64_t>& dstShape, const std::vector<SymbolicScalar>& validShape) | ||
| 189 | +{ | ||
| 190 | + auto result = std::make_shared<LogicalTensor>(function, operand->Datatype(), dstShape, validShape); | ||
| 191 | + function.AddOperation(Opcode::OP_REGISTER_COPY, {operand}, {result}); | ||
| 192 | + return result; | ||
| 193 | +} | ||
| 194 | + | ||
| 195 | +Tensor Expand(const Tensor& self, const std::vector<int64_t>& dstShape, std::vector<SymbolicScalar> validShape) | ||
| 196 | +{ | ||
| 197 | + DECLARE_TRACER(); | ||
| 198 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Expand"); | ||
| 199 | + | ||
| 200 | + static const std::unordered_set<DataType> EXPAND_A2A3_TYPES = {DT_BF16, DT_FP32, DT_FP16, DT_INT8, DT_INT16, | ||
| 201 | + DT_INT32, DT_UINT8, DT_UINT16, DT_UINT32, DT_BOOL}; | ||
| 202 | + static const std::unordered_set<DataType> EXPAND_A5_TYPES = {DT_BF16, DT_FP32, DT_FP16, DT_INT8, DT_INT16, | ||
| 203 | + DT_INT32, DT_UINT8, DT_UINT16, DT_UINT32, DT_BOOL}; | ||
| 204 | + const auto& supportedTypes = GetSupportedDataTypesByArch(EXPAND_A2A3_TYPES, EXPAND_A5_TYPES); | ||
| 205 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "EXPAND"); | ||
| 206 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "EXPAND"); | ||
| 207 | + CheckTensorShapeSize(self.GetStorage(), "EXPAND"); | ||
| 208 | + CheckDstShapeSize(dstShape, "EXPAND"); | ||
| 209 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, self.GetShape().size() == dstShape.size()) | ||
| 210 | + << "The shape size of self and dst should be equal"; | ||
| 211 | + if (validShape.empty()) { | ||
| 212 | + for (size_t i = 0; i < dstShape.size(); ++i) { | ||
| 213 | + if (self.GetShape()[i] != dstShape[i]) { | ||
| 214 | + validShape.emplace_back(dstShape[i]); | ||
| 215 | + } else { | ||
| 216 | + validShape.emplace_back(self.GetShape()[i]); | ||
| 217 | + } | ||
| 218 | + } | ||
| 219 | + } | ||
| 220 | + bool needExpand = false; | ||
| 221 | + for (size_t i = 0; i < dstShape.size(); ++i) { | ||
| 222 | + if (self.GetShape()[i] != dstShape[i]) { | ||
| 223 | + needExpand = true; | ||
| 224 | + } | ||
| 225 | + } | ||
| 226 | + if (needExpand) { | ||
| 227 | + RETURN_CALL(ExpandOperation, *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), dstShape, | ||
| 228 | + validShape); | ||
| 229 | + } else { | ||
| 230 | + RETURN_CALL(JustNeedCopyOperation, *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), dstShape, | ||
| 231 | + validShape); | ||
| 232 | + } | ||
| 233 | +} | ||
| 234 | + | ||
| 235 | +void ExpandOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 236 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 237 | + const std::vector<LogicalTensorPtr>& oOperand, const Operation& op) | ||
| 238 | +{ | ||
| 239 | + UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 240 | + std::vector<SymbolicScalar> validShape; | ||
| 241 | + op.GetAttr(OP_ATTR_PREFIX + "validShape", validShape); | ||
| 242 | + TiledExpand(function, tileShape, iOperand[0], oOperand[0], validShape); | ||
| 243 | +} | ||
| 244 | + | ||
| 245 | +REGISTER_OPERATION_TILED_FUNC(OP_EXPAND, Opcode::OP_EXPAND, ExpandOperationTileFunc); | ||
| 246 | + | ||
| 247 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,305 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file exponential.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "unary_tiled.h" | ||
| 17 | +#include "binary.h" | ||
| 18 | +#include "tensor_transformation.h" | ||
| 19 | +#include "interface/utils/operator_tracer.h" | ||
| 20 | +#include "passes/pass_utils/graph_utils.h" | ||
| 21 | +#include "tilefwk/error_code.h" | ||
| 22 | + | ||
| 23 | +namespace npu::tile_fwk { | ||
| 24 | + | ||
| 25 | +Tensor Exp(const Tensor& self, PrecisionType precisionType) | ||
| 26 | +{ | ||
| 27 | + DECLARE_TRACER(); | ||
| 28 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Exp"); | ||
| 29 | + | ||
| 30 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_FP32}; | ||
| 31 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "Exp"); | ||
| 32 | + | ||
| 33 | + auto [result, op] = TensorUnaryOperationWithOp<UnaryOpType::EXP>(*Program::GetInstance().GetCurrentFunction(), | ||
| 34 | + self.GetStorage()); | ||
| 35 | + op->SetAttribute(OpAttributeKey::precisionType, static_cast<int64_t>(precisionType)); | ||
| 36 | + return Tensor(result); | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +void ExpOperationTileFunc(Function& function, const TileShape& tileShape, const std::vector<LogicalTensorPtr>& iOperand, | ||
| 40 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 41 | +{ | ||
| 42 | + UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 43 | + int64_t precisionType = static_cast<int64_t>(PrecisionType::INTRINSIC); | ||
| 44 | + if (op.HasAttr(OpAttributeKey::precisionType)) { | ||
| 45 | + precisionType = op.GetIntAttribute(OpAttributeKey::precisionType); | ||
| 46 | + } | ||
| 47 | + return TiledUnaryOperation<UnaryOpType::EXP>(function, tileShape, iOperand[0], oOperand[0], 0, precisionType); | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +Tensor TensorExp2(Function& function, const LogicalTensorPtr& self) | ||
| 51 | +{ | ||
| 52 | + auto result = std::make_shared<LogicalTensor>(function, self->Datatype(), self->GetShape(), | ||
| 53 | + self->GetDynValidShape()); | ||
| 54 | + if (self->Datatype() == DataType::DT_INT32 || self->Datatype() == DataType::DT_INT16) { | ||
| 55 | + result = std::make_shared<LogicalTensor>(function, DT_FP32, self->GetShape(), self->GetDynValidShape()); | ||
| 56 | + } | ||
| 57 | + function.AddOperation(Opcode::OP_EXP2, {self}, {result}); | ||
| 58 | + return result; | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +Tensor Exp2(const Tensor& self) | ||
| 62 | +{ | ||
| 63 | + DECLARE_TRACER(); | ||
| 64 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Exp2"); | ||
| 65 | + | ||
| 66 | + std::unordered_set<DataType> supportedTypes = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16}; | ||
| 67 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "EXP2"); | ||
| 68 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "EXP2"); | ||
| 69 | + CheckTensorShapeSize(self.GetStorage(), "EXP2"); | ||
| 70 | + | ||
| 71 | + RETURN_CALL(Exp2, *Program::GetInstance().GetCurrentFunction(), self.GetStorage()); | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +void TiledExp2(Function& function, const TileShape& tileShape, size_t cur, Input& input, const LogicalTensorPtr& result) | ||
| 75 | +{ | ||
| 76 | + if (cur == input.tensor.GetShape().size()) { | ||
| 77 | + auto tile = input.tensor.GetStorage()->View(function, input.tileInfo.shape, input.tileInfo.offset); | ||
| 78 | + auto resultTile = result->View(function, input.tileInfo.shape, input.tileInfo.offset); | ||
| 79 | + std::vector<int64_t> srcTileShape(input.tileInfo.shape); | ||
| 80 | + auto tileShapeLen = srcTileShape.size(); | ||
| 81 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, SHAPE_DIM1 <= tileShapeLen && tileShapeLen <= SHAPE_DIM4) | ||
| 82 | + << "Length of tile shape only supports 1~4"; | ||
| 83 | + std::vector<int64_t> tmpShape; | ||
| 84 | + std::vector<int64_t> tmpShape2; | ||
| 85 | + if (srcTileShape.size() == 1) { | ||
| 86 | + tmpShape2.assign(srcTileShape.end() - SHAPE_DIM1, srcTileShape.end()); | ||
| 87 | + } else { | ||
| 88 | + tmpShape2.assign(srcTileShape.end() - SHAPE_DIM2, srcTileShape.end()); | ||
| 89 | + } | ||
| 90 | + auto alignSize2 = BLOCK_SIZE / BytesOf(DT_FP32); | ||
| 91 | + tmpShape2[tmpShape2.size() - 1] = (tmpShape2[tmpShape2.size() - 1] + alignSize2 - 1) / alignSize2 * alignSize2; | ||
| 92 | + if (input.tensor.GetDataType() == DT_FP32) { | ||
| 93 | + tmpShape = {BLOCK_SIZE / sizeof(float)}; | ||
| 94 | + } else { | ||
| 95 | + if (srcTileShape.size() == 1) { | ||
| 96 | + tmpShape.assign(srcTileShape.end() - SHAPE_DIM1, srcTileShape.end()); | ||
| 97 | + } else { | ||
| 98 | + tmpShape.assign(srcTileShape.end() - SHAPE_DIM2, srcTileShape.end()); | ||
| 99 | + } | ||
| 100 | + auto alignSize = BLOCK_SIZE / BytesOf(DT_FP32); | ||
| 101 | + tmpShape[tmpShape.size() - 1] = (tmpShape[tmpShape.size() - 1] + alignSize - 1) / alignSize * alignSize; | ||
| 102 | + } | ||
| 103 | + auto tmpTensor = std::make_shared<LogicalTensor>(function, DT_FP32, tmpShape); | ||
| 104 | + auto tmpTensorNext = std::make_shared<LogicalTensor>(function, DT_FP32, tmpShape2); | ||
| 105 | + | ||
| 106 | + function.AddOperation(Opcode::OP_EXP2, {tile}, {resultTile, tmpTensor, tmpTensorNext}); | ||
| 107 | + return; | ||
| 108 | + } | ||
| 109 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 110 | + for (int i = 0; i < input.tensor.GetShape()[cur]; i += vecTile[cur]) { | ||
| 111 | + input.tileInfo.shape[cur] = std::min(input.tensor.GetShape()[cur] - i, vecTile[cur]); | ||
| 112 | + input.tileInfo.offset[cur] = i; | ||
| 113 | + TiledExp2(function, tileShape, cur + 1, input, result); | ||
| 114 | + } | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +void TiledExp2(Function& function, const TileShape& tileShape, const LogicalTensorPtr& operand, | ||
| 118 | + const LogicalTensorPtr& result) | ||
| 119 | +{ | ||
| 120 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, operand->shape.size() == operand->offset.size()) | ||
| 121 | + << "The shape size of operand and offset must be equal"; | ||
| 122 | + | ||
| 123 | + TileInfo tileInfo(result->shape.size(), result->offset.size()); | ||
| 124 | + auto input = Input{operand, tileInfo}; | ||
| 125 | + | ||
| 126 | + TiledExp2(function, tileShape, 0, input, result); | ||
| 127 | +} | ||
| 128 | + | ||
| 129 | +void Exp2OperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 130 | + const std::vector<LogicalTensorPtr>& iOperand, const std::vector<LogicalTensorPtr>& oOperand, | ||
| 131 | + [[maybe_unused]] const Operation& op) | ||
| 132 | +{ | ||
| 133 | + TiledExp2(function, tileShape, iOperand[0], oOperand[0]); | ||
| 134 | +} | ||
| 135 | + | ||
| 136 | +Tensor TensorExpm1(Function& function, const LogicalTensorPtr& self) | ||
| 137 | +{ | ||
| 138 | + auto result = std::make_shared<LogicalTensor>(function, self->Datatype(), self->GetShape(), | ||
| 139 | + self->GetDynValidShape()); | ||
| 140 | + if (self->Datatype() == DataType::DT_INT32 || self->Datatype() == DataType::DT_INT16) { | ||
| 141 | + result = std::make_shared<LogicalTensor>(function, DT_FP32, self->GetShape(), self->GetDynValidShape()); | ||
| 142 | + } | ||
| 143 | + function.AddOperation(Opcode::OP_EXPM1, {self}, {result}); | ||
| 144 | + return result; | ||
| 145 | +} | ||
| 146 | + | ||
| 147 | +Tensor Expm1(const Tensor& self) | ||
| 148 | +{ | ||
| 149 | + DECLARE_TRACER(); | ||
| 150 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Expm1"); | ||
| 151 | + | ||
| 152 | + std::unordered_set<DataType> supportedTypes = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16}; | ||
| 153 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "EXPM1"); | ||
| 154 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "EXPM1"); | ||
| 155 | + CheckTensorShapeSize(self.GetStorage(), "EXPM1"); | ||
| 156 | + | ||
| 157 | + RETURN_CALL(Expm1, *Program::GetInstance().GetCurrentFunction(), self.GetStorage()); | ||
| 158 | +} | ||
| 159 | + | ||
| 160 | +void TiledExpm1(Function& function, const TileShape& tileShape, size_t cur, Input& input, | ||
| 161 | + const LogicalTensorPtr& result) | ||
| 162 | +{ | ||
| 163 | + if (cur == input.tensor.GetShape().size()) { | ||
| 164 | + auto tile = input.tensor.GetStorage()->View(function, input.tileInfo.shape, input.tileInfo.offset); | ||
| 165 | + auto resultTile = result->View(function, input.tileInfo.shape, input.tileInfo.offset); | ||
| 166 | + std::vector<int64_t> srcTileShape(input.tileInfo.shape); | ||
| 167 | + auto tileShapeLen = srcTileShape.size(); | ||
| 168 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, SHAPE_DIM1 <= tileShapeLen && tileShapeLen <= SHAPE_DIM4) | ||
| 169 | + << "Length of tile shape only supports 1~4"; | ||
| 170 | + std::vector<int64_t> tmpShape; | ||
| 171 | + if (input.tensor.GetDataType() == DT_FP32) { | ||
| 172 | + tmpShape = {BLOCK_SIZE / sizeof(float)}; | ||
| 173 | + } else { | ||
| 174 | + if (srcTileShape.size() == 1) { | ||
| 175 | + tmpShape.assign(srcTileShape.end() - SHAPE_DIM1, srcTileShape.end()); | ||
| 176 | + } else { | ||
| 177 | + tmpShape.assign(srcTileShape.end() - SHAPE_DIM2, srcTileShape.end()); | ||
| 178 | + } | ||
| 179 | + auto alignSize = BLOCK_SIZE / BytesOf(DT_FP32); | ||
| 180 | + tmpShape[tmpShape.size() - 1] = (tmpShape[tmpShape.size() - 1] + alignSize - 1) / alignSize * alignSize; | ||
| 181 | + } | ||
| 182 | + auto tmpTensor = std::make_shared<LogicalTensor>(function, DT_FP32, tmpShape); | ||
| 183 | + function.AddOperation(Opcode::OP_EXPM1, {tile}, {resultTile, tmpTensor}); | ||
| 184 | + return; | ||
| 185 | + } | ||
| 186 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 187 | + for (int i = 0; i < input.tensor.GetShape()[cur]; i += vecTile[cur]) { | ||
| 188 | + input.tileInfo.shape[cur] = std::min(input.tensor.GetShape()[cur] - i, vecTile[cur]); | ||
| 189 | + input.tileInfo.offset[cur] = i; | ||
| 190 | + TiledExpm1(function, tileShape, cur + 1, input, result); | ||
| 191 | + } | ||
| 192 | +} | ||
| 193 | + | ||
| 194 | +void TiledExpm1(Function& function, const TileShape& tileShape, const LogicalTensorPtr& operand, | ||
| 195 | + const LogicalTensorPtr& result) | ||
| 196 | +{ | ||
| 197 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, operand->shape.size() == operand->offset.size()) | ||
| 198 | + << "The shape size of operand and offset must be equal"; | ||
| 199 | + | ||
| 200 | + TileInfo tileInfo(result->shape.size(), result->offset.size()); | ||
| 201 | + auto input = Input{operand, tileInfo}; | ||
| 202 | + | ||
| 203 | + TiledExpm1(function, tileShape, 0, input, result); | ||
| 204 | +} | ||
| 205 | + | ||
| 206 | +void Expm1OperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 207 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 208 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 209 | +{ | ||
| 210 | + TiledExpm1(function, tileShape, iOperand[0], oOperand[0]); | ||
| 211 | +} | ||
| 212 | + | ||
| 213 | +LogicalTensorPtr IntegerPow(const Tensor& self, int32_t intExponent) | ||
| 214 | +{ | ||
| 215 | + // 快速幂 | ||
| 216 | + auto result = GenAllOneTensor(self.GetShape(), self.GetStorage()->GetDynValidShape(), self.GetDataType()); | ||
| 217 | + auto current = CALL(BinaryOperation<BinaryOpType::MUL>, *Program::GetInstance().GetCurrentFunction(), self, result); | ||
| 218 | + | ||
| 219 | + while (intExponent != NUM_VALUE_0) { | ||
| 220 | + if (intExponent % NUM_VALUE_2 != NUM_VALUE_0) { | ||
| 221 | + result = CALL(BinaryOperation<BinaryOpType::MUL>, *Program::GetInstance().GetCurrentFunction(), result, | ||
| 222 | + current); | ||
| 223 | + } | ||
| 224 | + current = CALL(BinaryOperation<BinaryOpType::MUL>, *Program::GetInstance().GetCurrentFunction(), current, | ||
| 225 | + current); | ||
| 226 | + intExponent /= NUM_VALUE_2; | ||
| 227 | + } | ||
| 228 | + return result; | ||
| 229 | +} | ||
| 230 | + | ||
| 231 | +LogicalTensorPtr GeneralPow(const Tensor& self, double exponent) | ||
| 232 | +{ | ||
| 233 | + // 如果指数小于0,先计算a^(-b),最后再取倒数 | ||
| 234 | + bool expLessThanZero = exponent < NUM_VALUE_0; | ||
| 235 | + exponent = std::abs(exponent); | ||
| 236 | + | ||
| 237 | + LogicalTensorPtr result; | ||
| 238 | + int32_t intExponent = static_cast<int32_t>(std::floor(exponent)); | ||
| 239 | + if (exponent - intExponent < NUM_VALUE_EPS) { | ||
| 240 | + result = IntegerPow(self, intExponent); | ||
| 241 | + } else { | ||
| 242 | + auto lnSelf = CALL(UnaryOperation<UnaryOpType::LN>, *Program::GetInstance().GetCurrentFunction(), | ||
| 243 | + self.GetStorage()); | ||
| 244 | + auto exponentLnSelf = CALL(BinaryOperationScalar<BinaryOpType::MUL>, | ||
| 245 | + *Program::GetInstance().GetCurrentFunction(), lnSelf, | ||
| 246 | + Element(DataType::DT_FP32, exponent)); | ||
| 247 | + result = CALL(UnaryOperation<UnaryOpType::EXP>, *Program::GetInstance().GetCurrentFunction(), exponentLnSelf); | ||
| 248 | + } | ||
| 249 | + | ||
| 250 | + // 指数小于零,结果取倒数 | ||
| 251 | + if (expLessThanZero) { | ||
| 252 | + auto oneTensor = GenAllOneTensor(self.GetShape(), self.GetStorage()->GetDynValidShape(), self.GetDataType()); | ||
| 253 | + // 求倒数 | ||
| 254 | + RETURN_CALL(BinaryOperation<BinaryOpType::DIV>, *Program::GetInstance().GetCurrentFunction(), oneTensor, | ||
| 255 | + result); | ||
| 256 | + } | ||
| 257 | + return result; | ||
| 258 | +} | ||
| 259 | + | ||
| 260 | +Tensor Pow(const Tensor& self, const Element& other) | ||
| 261 | +{ | ||
| 262 | + DECLARE_TRACER(); | ||
| 263 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Pow"); | ||
| 264 | + | ||
| 265 | + LogicalTensorPtr castSelf = self.GetStorage(); | ||
| 266 | + if ((self.GetDataType() == DT_INT32 || self.GetDataType() == DT_INT16) && other.GetDataType() != DT_INT32) { | ||
| 267 | + castSelf = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), castSelf, | ||
| 268 | + DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 269 | + } | ||
| 270 | + double exponent = other.Cast<double>(); | ||
| 271 | + // 指数为0,输出全1 | ||
| 272 | + if (std::abs(exponent) < NUM_VALUE_EPS) { | ||
| 273 | + return GenAllOneTensor(self.GetShape(), self.GetStorage()->GetDynValidShape(), self.GetDataType()); | ||
| 274 | + } | ||
| 275 | + DataType dataType = castSelf->Datatype(); | ||
| 276 | + bool shouldUpToFp32 = dataType == DT_FP16 || dataType == DT_BF16; | ||
| 277 | + if (shouldUpToFp32) { | ||
| 278 | + castSelf = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), castSelf, | ||
| 279 | + DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 280 | + } | ||
| 281 | + auto result = castSelf; | ||
| 282 | + if (std::abs(exponent - NUM_VALUE_0_5) < NUM_VALUE_EPS) { | ||
| 283 | + result = CALL(UnaryOperation<UnaryOpType::SQRT>, *Program::GetInstance().GetCurrentFunction(), result); | ||
| 284 | + } else if (std::abs(exponent - NUM_VALUE_2) < NUM_VALUE_EPS) { | ||
| 285 | + result = CALL(BinaryOperation<BinaryOpType::MUL>, *Program::GetInstance().GetCurrentFunction(), result, result); | ||
| 286 | + } else if (std::abs(exponent - NUM_VALUE_3) < NUM_VALUE_EPS) { | ||
| 287 | + auto doubleSelf = CALL(BinaryOperation<BinaryOpType::MUL>, *Program::GetInstance().GetCurrentFunction(), result, | ||
| 288 | + result); | ||
| 289 | + result = CALL(BinaryOperation<BinaryOpType::MUL>, *Program::GetInstance().GetCurrentFunction(), doubleSelf, | ||
| 290 | + result); | ||
| 291 | + } else { | ||
| 292 | + result = GeneralPow(result, exponent); | ||
| 293 | + } | ||
| 294 | + if (shouldUpToFp32) { | ||
| 295 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), result, dataType, | ||
| 296 | + CastMode::CAST_NONE); | ||
| 297 | + } | ||
| 298 | + return result; | ||
| 299 | +} | ||
| 300 | + | ||
| 301 | +REGISTER_OPERATION_TILED_FUNC(OP_EXP, Opcode::OP_EXP, ExpOperationTileFunc); | ||
| 302 | +REGISTER_OPERATION_TILED_FUNC(OP_EXP2, Opcode::OP_EXP2, Exp2OperationTileFunc); | ||
| 303 | +REGISTER_OPERATION_TILED_FUNC(OP_EXPM1, Opcode::OP_EXPM1, Expm1OperationTileFunc); | ||
| 304 | + | ||
| 305 | +} // namespace npu::tile_fwk | ||
Rframework/src/interface/operation/vector/quantization.cpp→framework/src/interface/operation/vector/quant_mx.cpp+37-702
Rframework/src/interface/operation/vector/tensor_transformation.cpp→framework/src/interface/operation/vector/trans_data.cpp+14-872