Merged
fix(Operation): clean up implementation and magic numbers #5852
chenblAdd on Sep 1
fix(Operation): clean up implementation and magic numbers #5852
Merged
Merged fromchenbl/pypto_newfix/docsintocann/pyptomaster
Total: 101 files changed+3322-3292
| @@ -245,6 +245,19 @@ void UnPackInferShapeFunc(Operation* op, std::vector<std::vector<SymbolicScalar> | |||
| 245 | } | 245 | } |
| 246 | REGISTER_INFER_SHAPE_FUNC(OP_UNPACK, Opcode::OP_UNPACK, UnPackInferShapeFunc); | 246 | REGISTER_INFER_SHAPE_FUNC(OP_UNPACK, Opcode::OP_UNPACK, UnPackInferShapeFunc); |
| 247 | 247 | ||
| 248 | +void FloorDivInferShapeFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | ||
| 249 | +{ | ||
| 250 | + auto inputValidShape = op->GetIOperands()[0]->GetDynValidShape(); | ||
| 251 | + if (inputValidShape.empty()) { | ||
| 252 | + return; | ||
| 253 | + } | ||
| 254 | + outValidShapes.emplace_back(inputValidShape); | ||
| 255 | + inputValidShape.front() = inputValidShape.front() * NUM_VALUE_6; | ||
| 256 | + outValidShapes.emplace_back(inputValidShape); | ||
| 257 | +} | ||
| 258 | +REGISTER_INFER_SHAPE_FUNC(OP_FLOORDIV, Opcode::OP_FLOORDIV, FloorDivInferShapeFunc); | ||
| 259 | +REGISTER_INFER_SHAPE_FUNC(OP_FLOORDIVS, Opcode::OP_FLOORDIVS, FloorDivInferShapeFunc); | ||
| 260 | + | ||
| 248 | template <int64_t TailScale> | 261 | template <int64_t TailScale> |
| 249 | void InferShapeWithTailScaleFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) | 262 | void InferShapeWithTailScaleFunc(Operation* op, std::vector<std::vector<SymbolicScalar>>& outValidShapes) |
| 250 | { | 263 | { |
| @@ -256,8 +269,6 @@ void InferShapeWithTailScaleFunc(Operation* op, std::vector<std::vector<Symbolic | |||
| 256 | outValidShapes.emplace_back(inputValidShape); | 269 | outValidShapes.emplace_back(inputValidShape); |
| 257 | outValidShapes.emplace_back(std::vector<SymbolicScalar>{inputValidShape[ndim - 1] * TailScale}); | 270 | outValidShapes.emplace_back(std::vector<SymbolicScalar>{inputValidShape[ndim - 1] * TailScale}); |
| 258 | } | 271 | } |
| 259 | -REGISTER_INFER_SHAPE_FUNC(OP_FLOORDIV, Opcode::OP_FLOORDIV, InferShapeWithTailScaleFunc<NUM_VALUE_4>); | ||
| 260 | -REGISTER_INFER_SHAPE_FUNC(OP_FLOORDIVS, Opcode::OP_FLOORDIVS, InferShapeWithTailScaleFunc<NUM_VALUE_3>); | ||
| 261 | REGISTER_INFER_SHAPE_FUNC(OP_SINH, Opcode::OP_SINH, InferShapeWithTailScaleFunc<NUM_VALUE_4>); | 272 | REGISTER_INFER_SHAPE_FUNC(OP_SINH, Opcode::OP_SINH, InferShapeWithTailScaleFunc<NUM_VALUE_4>); |
| 262 | REGISTER_INFER_SHAPE_FUNC(OP_COSH, Opcode::OP_COSH, InferShapeWithTailScaleFunc<1>); | 273 | REGISTER_INFER_SHAPE_FUNC(OP_COSH, Opcode::OP_COSH, InferShapeWithTailScaleFunc<1>); |
| 263 | REGISTER_INFER_SHAPE_FUNC(OP_TAN, Opcode::OP_TAN, InferShapeWithTailScaleFunc<NUM_VALUE_7>); | 274 | REGISTER_INFER_SHAPE_FUNC(OP_TAN, Opcode::OP_TAN, InferShapeWithTailScaleFunc<NUM_VALUE_7>); |
| @@ -95,7 +95,6 @@ LogicalTensorPtr TensorAxpyOperation(Function& function, const Tensor& self, con | |||
| 95 | { | 95 | { |
| 96 | auto selfTensor = self.GetStorage(); | 96 | auto selfTensor = self.GetStorage(); |
| 97 | auto otherTensor = other.GetStorage(); | 97 | auto otherTensor = other.GetStorage(); |
| 98 | - | ||
| 99 | if (selfTensor->shape.size() != otherTensor->shape.size()) { | 98 | if (selfTensor->shape.size() != otherTensor->shape.size()) { |
| 100 | std::vector<int> broadCastShape = GetBroadCastShape(selfTensor, otherTensor); | 99 | std::vector<int> broadCastShape = GetBroadCastShape(selfTensor, otherTensor); |
| 101 | selfTensor = BinaryOperationBroadCast(selfTensor, broadCastShape); | 100 | selfTensor = BinaryOperationBroadCast(selfTensor, broadCastShape); |
| @@ -273,6 +273,70 @@ Tensor Minimum(const Tensor& operand1, const Tensor& operand2) | |||
| 273 | operand2); | 273 | operand2); |
| 274 | } | 274 | } |
| 275 | 275 | ||
| 276 | +Tensor Clip(const Tensor& self, const Element& min, const Element& max) | ||
| 277 | +{ | ||
| 278 | + DECLARE_TRACER(); | ||
| 279 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Clip"); | ||
| 280 | + | ||
| 281 | + static const std::unordered_set<DataType> CLIP_A2A3_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16}; | ||
| 282 | + static const std::unordered_set<DataType> CLIP_A5_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16, DT_INT64}; | ||
| 283 | + const auto& supportedTypes = GetSupportedDataTypesByArch(CLIP_A2A3_TYPES, CLIP_A5_TYPES); | ||
| 284 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "CLIP"); | ||
| 285 | + CheckTensorShapeSize(self.GetStorage(), "CLIP"); | ||
| 286 | + | ||
| 287 | + Element min_ = min, max_ = max; | ||
| 288 | + | ||
| 289 | + Tensor result = self; | ||
| 290 | + if (min_.GetDataType() != DT_BOTTOM) { | ||
| 291 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, min_.GetDataType() == self.GetDataType()) | ||
| 292 | + << "The datatype of inputs should be same"; | ||
| 293 | + result = Maximum(result, min_); | ||
| 294 | + } | ||
| 295 | + if (max_.GetDataType() != DT_BOTTOM) { | ||
| 296 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, max_.GetDataType() == self.GetDataType()) | ||
| 297 | + << "The datatype of inputs should be same"; | ||
| 298 | + result = Minimum(result, max_); | ||
| 299 | + } | ||
| 300 | + result.GetStorage()->UpdateDynValidShape(self.GetStorage()->GetDynValidShape()); | ||
| 301 | + return result; | ||
| 302 | +} | ||
| 303 | + | ||
| 304 | +Tensor Clip(const Tensor& self, const Tensor& min, const Tensor& max) | ||
| 305 | +{ | ||
| 306 | + DECLARE_TRACER(); | ||
| 307 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Clip"); | ||
| 308 | + | ||
| 309 | + static const std::unordered_set<DataType> CLIP_A2A3_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16}; | ||
| 310 | + static const std::unordered_set<DataType> CLIP_A5_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16, DT_INT64}; | ||
| 311 | + const auto& supportedTypes = GetSupportedDataTypesByArch(CLIP_A2A3_TYPES, CLIP_A5_TYPES); | ||
| 312 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "CLIP"); | ||
| 313 | + CheckTensorShapeSize(self.GetStorage(), "CLIP"); | ||
| 314 | + | ||
| 315 | + Tensor result = self; | ||
| 316 | + if (min.GetStorage() != nullptr) { | ||
| 317 | + CheckTensorFormat(min.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Clip"); | ||
| 318 | + CheckTensorShapeSize(min.GetStorage(), "CLIP"); | ||
| 319 | + CheckTensorsFormatConsistency(self.GetStorage(), min.GetStorage(), "CLIP"); | ||
| 320 | + std::vector minBroadcastAxes = GetBroadcastAxes(min.GetShape(), self.GetShape()); | ||
| 321 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, minBroadcastAxes.size() <= 1) | ||
| 322 | + << "minBroadcastAxes size should be <= 1"; | ||
| 323 | + CheckInt64Broadcast(self.GetStorage(), min.GetStorage(), "CLIP"); | ||
| 324 | + result = Maximum(result, min); | ||
| 325 | + } | ||
| 326 | + if (max.GetStorage() != nullptr) { | ||
| 327 | + CheckTensorFormat(max.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Clip"); | ||
| 328 | + CheckTensorShapeSize(max.GetStorage(), "CLIP"); | ||
| 329 | + CheckTensorsFormatConsistency(self.GetStorage(), max.GetStorage(), "CLIP"); | ||
| 330 | + std::vector maxBroadcastAxes = GetBroadcastAxes(max.GetShape(), self.GetShape()); | ||
| 331 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, maxBroadcastAxes.size() <= 1) | ||
| 332 | + << "maxBroadcastAxes size should be <= 1"; | ||
| 333 | + CheckInt64Broadcast(self.GetStorage(), max.GetStorage(), "CLIP"); | ||
| 334 | + result = Minimum(result, max); | ||
| 335 | + } | ||
| 336 | + result.GetStorage()->UpdateDynValidShape(self.GetStorage()->GetDynValidShape()); | ||
| 337 | + return result; | ||
| 338 | +} | ||
| 339 | + | ||
| 276 | Tensor Gcd(const Tensor& self, const Tensor& other) | 340 | Tensor Gcd(const Tensor& self, const Tensor& other) |
| 277 | { | 341 | { |
| 278 | DECLARE_TRACER(); | 342 | DECLARE_TRACER(); |
| @@ -24,6 +24,8 @@ | |||
| 24 | 24 | ||
| 25 | namespace npu::tile_fwk { | 25 | namespace npu::tile_fwk { |
| 26 | 26 | ||
| 27 | +constexpr int64_t GCD_TSEL_TMP_BYTES = NUM_VALUE_8 * BLOCK_SIZE + NUM_VALUE_64; | ||
| 28 | + | ||
| 27 | enum class BinaryOpType { | 29 | enum class BinaryOpType { |
| 28 | ADD, | 30 | ADD, |
| 29 | SUB, | 31 | SUB, |
| @@ -1,159 +0,0 @@ | |||
| 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 | ||
| @@ -189,13 +189,10 @@ void TiledBinaryOperationScalar(Function& function, const TileShape& tileShape, | |||
| 189 | } else if (opNameCode == Opcode::OP_FLOORDIVS) { | 189 | } else if (opNameCode == Opcode::OP_FLOORDIVS) { |
| 190 | auto alignSize = BLOCK_SIZE / BytesOf(input1.tensor->Datatype()); | 190 | auto alignSize = BLOCK_SIZE / BytesOf(input1.tensor->Datatype()); |
| 191 | auto tmpShape = resultTileInfo.shape; | 191 | auto tmpShape = resultTileInfo.shape; |
| 192 | - tmpShape[resultTileInfo.shape.size() - 1] = AlignUp(tmpShape.back(), alignSize) * NUM_VALUE_6; | 192 | + tmpShape.back() = AlignUp(tmpShape.back(), alignSize); |
| 193 | - auto tmpElemBytes = input1.tensor->Datatype() == DT_INT64 ? BytesOf(DT_INT64) : BytesOf(DT_FP32); | 193 | + tmpShape.front() *= NUM_VALUE_6; |
| 194 | - int64_t intermediateBytes = std::accumulate(tmpShape.begin(), tmpShape.end(), 1LL, | 194 | + auto tmpType = input1.tensor->Datatype() == DT_INT64 ? DT_INT64 : DT_FP32; |
| 195 | - std::multiplies<int64_t>()) * | 195 | + auto tempTensor = std::make_shared<LogicalTensor>(function, tmpType, tmpShape); |
| 196 | - tmpElemBytes; | ||
| 197 | - auto tempTensor = std::make_shared<LogicalTensor>(function, DT_UINT8, | ||
| 198 | - std::vector<int64_t>{intermediateBytes}); | ||
| 199 | auto& tmpOp = function.AddOperation(opNameCode, {inputTile1}, {resultTile, tempTensor}); | 196 | auto& tmpOp = function.AddOperation(opNameCode, {inputTile1}, {resultTile, tempTensor}); |
| 200 | tmpOp.SetAttribute(OpAttributeKey::scalar, value); | 197 | tmpOp.SetAttribute(OpAttributeKey::scalar, value); |
| 201 | tmpOp.SetAttribute(OP_ATTR_PREFIX + "reverseOperand", reverseOperand); | 198 | tmpOp.SetAttribute(OP_ATTR_PREFIX + "reverseOperand", reverseOperand); |
| @@ -208,7 +205,7 @@ void TiledBinaryOperationScalar(Function& function, const TileShape& tileShape, | |||
| 208 | int64_t maskCols = ((tileW + NUM_VALUE_7) / NUM_VALUE_8 + NUM_VALUE_31) / BLOCK_SIZE * BLOCK_SIZE; | 205 | int64_t maskCols = ((tileW + NUM_VALUE_7) / NUM_VALUE_8 + NUM_VALUE_31) / BLOCK_SIZE * BLOCK_SIZE; |
| 209 | int64_t intermediateBytes = (NUM_VALUE_6 * tileFootprint * BytesOf(DT_INT32)) + | 206 | int64_t intermediateBytes = (NUM_VALUE_6 * tileFootprint * BytesOf(DT_INT32)) + |
| 210 | (NUM_VALUE_2 * tileFootprint * BytesOf(DT_FP32)) + | 207 | (NUM_VALUE_2 * tileFootprint * BytesOf(DT_FP32)) + |
| 211 | - NUM_VALUE_4 * tileH * maskCols + 320; | 208 | + NUM_VALUE_4 * tileH * maskCols + GCD_TSEL_TMP_BYTES; |
| 212 | auto tempTensor = std::make_shared<LogicalTensor>(function, DT_UINT8, | 209 | auto tempTensor = std::make_shared<LogicalTensor>(function, DT_UINT8, |
| 213 | std::vector<int64_t>{intermediateBytes}); | 210 | std::vector<int64_t>{intermediateBytes}); |
| 214 | auto& tmpOp = function.AddOperation(opNameCode, {inputTile1}, {resultTile, tempTensor}); | 211 | auto& tmpOp = function.AddOperation(opNameCode, {inputTile1}, {resultTile, tempTensor}); |
| @@ -72,11 +72,10 @@ void AddTiledBinaryOperation(Function& function, const TileShape& tileShape, Log | |||
| 72 | } else if (opName == "FLOORDIV") { | 72 | } else if (opName == "FLOORDIV") { |
| 73 | auto tmpShape = resultTileInfo.shape; | 73 | auto tmpShape = resultTileInfo.shape; |
| 74 | auto alignSize = BLOCK_SIZE / BytesOf(result->Datatype()); | 74 | auto alignSize = BLOCK_SIZE / BytesOf(result->Datatype()); |
| 75 | - tmpShape[resultTileInfo.shape.size() - 1] = AlignUp(tmpShape.back(), alignSize) * NUM_VALUE_6; | 75 | + tmpShape.back() = AlignUp(tmpShape.back(), alignSize); |
| 76 | - auto tmpElemBytes = result->Datatype() == DT_INT64 ? BytesOf(DT_INT64) : BytesOf(DT_FP32); | 76 | + tmpShape.front() *= NUM_VALUE_6; |
| 77 | - int64_t intermediateBytes = std::accumulate(tmpShape.begin(), tmpShape.end(), 1LL, std::multiplies<int64_t>()) * | 77 | + auto tmpType = result->Datatype() == DT_INT64 ? DT_INT64 : DT_FP32; |
| 78 | - tmpElemBytes; | 78 | + auto tempTensor = std::make_shared<LogicalTensor>(function, tmpType, tmpShape); |
| 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}, | 79 | op = &function.AddOperation(GetBinaryOpNameCode<T, false, false>(), {inputTile1, inputTile2}, |
| 81 | {resultTile, tempTensor}); | 80 | {resultTile, tempTensor}); |
| 82 | } else if (opName == "GCD") { | 81 | } else if (opName == "GCD") { |
| @@ -87,7 +86,7 @@ void AddTiledBinaryOperation(Function& function, const TileShape& tileShape, Log | |||
| 87 | int64_t maskCols = ((tileW + NUM_VALUE_7) / NUM_VALUE_8 + NUM_VALUE_31) / BLOCK_SIZE * BLOCK_SIZE; | 86 | 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)) + | 87 | int64_t intermediateBytes = (NUM_VALUE_6 * tileFootprint * BytesOf(DT_INT32)) + |
| 89 | (NUM_VALUE_2 * tileFootprint * BytesOf(DT_FP32)) + NUM_VALUE_4 * tileH * maskCols + | 88 | (NUM_VALUE_2 * tileFootprint * BytesOf(DT_FP32)) + NUM_VALUE_4 * tileH * maskCols + |
| 90 | - 320; | 89 | + GCD_TSEL_TMP_BYTES; |
| 91 | auto tempTensor = std::make_shared<LogicalTensor>(function, DT_UINT8, std::vector<int64_t>{intermediateBytes}); | 90 | auto tempTensor = std::make_shared<LogicalTensor>(function, DT_UINT8, std::vector<int64_t>{intermediateBytes}); |
| 92 | op = &function.AddOperation(GetBinaryOpNameCode<T, false, false>(), {inputTile1, inputTile2}, | 91 | op = &function.AddOperation(GetBinaryOpNameCode<T, false, false>(), {inputTile1, inputTile2}, |
| 93 | {resultTile, tempTensor}); | 92 | {resultTile, tempTensor}); |
| @@ -12,15 +12,16 @@ | |||
| 12 | * \file bitwise.cpp | 12 | * \file bitwise.cpp |
| 13 | * \brief | 13 | * \brief |
| 14 | */ | 14 | */ |
| 15 | + | ||
| 15 | #include "tilefwk/data_type.h" | 16 | #include "tilefwk/data_type.h" |
| 16 | -#include "unary.h" | 17 | +#include "unary_tiled.h" |
| 17 | #include "binary.h" | 18 | #include "binary.h" |
| 18 | #include "binary_tiled.h" | 19 | #include "binary_tiled.h" |
| 19 | #include "tensor_transformation.h" | 20 | #include "tensor_transformation.h" |
| 20 | #include "interface/utils/operator_tracer.h" | 21 | #include "interface/utils/operator_tracer.h" |
| 22 | +#include "interface/utils/common.h" | ||
| 21 | #include "interface/configs/config_manager.h" | 23 | #include "interface/configs/config_manager.h" |
| 22 | #include "tilefwk/error_code.h" | 24 | #include "tilefwk/error_code.h" |
| 23 | -#include "passes/tile_graph_pass/graph_constraint/axis_combine.h" | ||
| 24 | 25 | ||
| 25 | namespace npu::tile_fwk { | 26 | namespace npu::tile_fwk { |
| 26 | 27 | ||
| @@ -72,8 +73,411 @@ Tensor BitwiseXor(const Tensor& self, const Tensor& other) | |||
| 72 | RETURN_CALL(BinaryOperation<BinaryOpType::BITWISEXOR>, *Program::GetInstance().GetCurrentFunction(), self, other); | 73 | RETURN_CALL(BinaryOperation<BinaryOpType::BITWISEXOR>, *Program::GetInstance().GetCurrentFunction(), self, other); |
| 73 | } | 74 | } |
| 74 | 75 | ||
| 76 | +Tensor BitwiseNot(const Tensor& self) | ||
| 77 | +{ | ||
| 78 | + DECLARE_TRACER(); | ||
| 79 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseNot"); | ||
| 80 | + | ||
| 81 | + if (self.GetDataType() == DT_BOOL) { | ||
| 82 | + return LogicalNot(self); | ||
| 83 | + } | ||
| 84 | + static const std::unordered_set<DataType> BITWISE_A2A3_TYPES = {DT_INT16, DT_UINT16, DT_INT8, | ||
| 85 | + DT_UINT8, DT_INT32, DT_UINT32}; | ||
| 86 | + static const std::unordered_set<DataType> BITWISE_A5_TYPES = {DT_INT16, DT_UINT16, DT_INT8, DT_UINT8, | ||
| 87 | + DT_INT32, DT_UINT32, DT_INT64, DT_UINT64}; | ||
| 88 | + const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISE_A2A3_TYPES, BITWISE_A5_TYPES); | ||
| 89 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "BitwiseNot"); | ||
| 90 | + RETURN_CALL(UnaryOperation<UnaryOpType::BITWISENOT>, *Program::GetInstance().GetCurrentFunction(), | ||
| 91 | + self.GetStorage()); | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +enum class BitwiseShiftOpType { | ||
| 95 | + BITWISERIGHTSHIFT, | ||
| 96 | + BITWISELEFTSHIFT, | ||
| 97 | + SBITWISERIGHTSHIFT, | ||
| 98 | + SBITWISELEFTSHIFT, | ||
| 99 | +}; | ||
| 100 | + | ||
| 101 | +template <BitwiseShiftOpType T> | ||
| 102 | +std::string GetBitwiseShiftOpName() | ||
| 103 | +{ | ||
| 104 | + switch (T) { | ||
| 105 | + case BitwiseShiftOpType::BITWISERIGHTSHIFT: | ||
| 106 | + return "BITWISERIGHTSHIFT"; | ||
| 107 | + case BitwiseShiftOpType::BITWISELEFTSHIFT: | ||
| 108 | + return "BITWISELEFTSHIFT"; | ||
| 109 | + case BitwiseShiftOpType::SBITWISERIGHTSHIFT: | ||
| 110 | + return "SBITWISERIGHTSHIFT"; | ||
| 111 | + case BitwiseShiftOpType::SBITWISELEFTSHIFT: | ||
| 112 | + return "SBITWISELEFTSHIFT"; | ||
| 113 | + default: | ||
| 114 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "unknown binary op type"; | ||
| 115 | + return ""; | ||
| 116 | + } | ||
| 117 | +} | ||
| 118 | + | ||
| 119 | +template <BitwiseShiftOpType T, bool WithElement = false> | ||
| 120 | +Opcode GetBitwiseShiftOpNameCode() | ||
| 121 | +{ | ||
| 122 | + if constexpr (WithElement) { | ||
| 123 | +#define CASE(X) \ | ||
| 124 | + case BitwiseShiftOpType::X: \ | ||
| 125 | + return Opcode::OP_##X##S | ||
| 126 | + switch (T) { | ||
| 127 | + CASE(BITWISERIGHTSHIFT); | ||
| 128 | + CASE(BITWISELEFTSHIFT); | ||
| 129 | + default: | ||
| 130 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "unknown binary op type"; | ||
| 131 | + } | ||
| 132 | +#undef CASE | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | +#define CASE(X) \ | ||
| 136 | + case BitwiseShiftOpType::X: \ | ||
| 137 | + return Opcode::OP_##X | ||
| 138 | + switch (T) { | ||
| 139 | + CASE(BITWISERIGHTSHIFT); | ||
| 140 | + CASE(BITWISELEFTSHIFT); | ||
| 141 | + CASE(SBITWISERIGHTSHIFT); | ||
| 142 | + CASE(SBITWISELEFTSHIFT); | ||
| 143 | + default: | ||
| 144 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "unknown binary op type"; | ||
| 145 | + } | ||
| 146 | +#undef CASE | ||
| 147 | +} | ||
| 148 | + | ||
| 149 | +template <BitwiseShiftOpType T> | ||
| 150 | +void TiledBitwiseShiftOperation(Function& function, const TileShape& tileShape, size_t cur, LogicalInput& input1, | ||
| 151 | + LogicalInput& input2, const LogicalTensorPtr& result, TileInfo& resultTileInfo) | ||
| 152 | +{ | ||
| 153 | + if (cur == input1.tensor->GetShape().size()) { | ||
| 154 | + auto inputTile1 = input1.tensor->View(function, input1.tileInfo.shape, input1.tileInfo.offset); | ||
| 155 | + auto inputTile2 = input2.tensor->View(function, input2.tileInfo.shape, input2.tileInfo.offset); | ||
| 156 | + auto resultTile = result->View(function, resultTileInfo.shape, resultTileInfo.offset); | ||
| 157 | + | ||
| 158 | + Shape tmpShape = resultTileInfo.shape; | ||
| 159 | + auto alignSize = BLOCK_SIZE / static_cast<int64_t>(BytesOf(result->Datatype())); | ||
| 160 | + tmpShape[tmpShape.size() - 1] = AlignUp(tmpShape[tmpShape.size() - 1], alignSize); | ||
| 161 | + auto tmpTile = std::make_shared<LogicalTensor>(function, result->Datatype(), tmpShape); | ||
| 162 | + function.AddOperation(GetBitwiseShiftOpNameCode<T, false>(), {inputTile1, inputTile2}, {resultTile, tmpTile}); | ||
| 163 | + return; | ||
| 164 | + } | ||
| 165 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 166 | + for (int i = 0; i < result->shape[cur]; i += vecTile[cur]) { | ||
| 167 | + resultTileInfo.offset[cur] = i; | ||
| 168 | + input1.tileInfo.offset[cur] = i % input1.tensor->GetShape()[cur]; | ||
| 169 | + input2.tileInfo.offset[cur] = i % input2.tensor->GetShape()[cur]; | ||
| 170 | + resultTileInfo.shape[cur] = std::min(result->shape[cur] - resultTileInfo.offset[cur], vecTile[cur]); | ||
| 171 | + input1.tileInfo.shape[cur] = std::min(input1.tensor->GetShape()[cur] - input1.tileInfo.offset[cur], | ||
| 172 | + vecTile[cur]); | ||
| 173 | + input2.tileInfo.shape[cur] = std::min(input2.tensor->GetShape()[cur] - input2.tileInfo.offset[cur], | ||
| 174 | + vecTile[cur]); | ||
| 175 | + TiledBitwiseShiftOperation<T>(function, tileShape, cur + 1, input1, input2, result, resultTileInfo); | ||
| 176 | + } | ||
| 177 | +} | ||
| 178 | + | ||
| 179 | +template <BitwiseShiftOpType T> | ||
| 180 | +void TiledBitwiseShiftOperation(Function& function, const TileShape& tileShape, LogicalTensorPtr operand1, | ||
| 181 | + LogicalTensorPtr operand2, const LogicalTensorPtr& result) | ||
| 182 | +{ | ||
| 183 | + CheckBinOpOperandsValid(operand1, operand2); | ||
| 184 | + BroadcastOperandTensor(operand1, operand2, result, function, tileShape); | ||
| 185 | + BroadcastOperandTensor(operand2, operand1, result, function, tileShape); | ||
| 186 | + | ||
| 187 | + TileInfo tileInfo1(result->shape.size(), result->offset.size()); | ||
| 188 | + TileInfo tileInfo2(result->shape.size(), result->offset.size()); | ||
| 189 | + TileInfo resultTileInfo(result->shape.size(), result->offset.size()); | ||
| 190 | + auto input1 = LogicalInput{operand1, tileInfo1}; | ||
| 191 | + auto input2 = LogicalInput{operand2, tileInfo2}; | ||
| 192 | + // 如果使能了Combine Axis逻辑,需要将withbrc置为false,避免后续走OP_XX_BRC逻辑 | ||
| 193 | + TiledBitwiseShiftOperation<T>(function, tileShape, 0, input1, input2, result, resultTileInfo); | ||
| 194 | +} | ||
| 195 | + | ||
| 196 | +template <BitwiseShiftOpType T> | ||
| 197 | +LogicalTensorPtr TensorBitwiseShiftOperation(Function& function, const Tensor& self, const Tensor& other) | ||
| 198 | +{ | ||
| 199 | + auto operand1 = self.GetStorage(); | ||
| 200 | + auto operand2 = other.GetStorage(); | ||
| 201 | + if (operand1->shape.size() != operand2->shape.size()) { | ||
| 202 | + std::vector<int> broadCastShape = GetBroadCastShape(operand1, operand2); | ||
| 203 | + operand1 = BinaryOperationBroadCast(operand1, broadCastShape); | ||
| 204 | + operand2 = BinaryOperationBroadCast(operand2, broadCastShape); | ||
| 205 | + } | ||
| 206 | + auto opName = GetBitwiseShiftOpName<T>(); | ||
| 207 | + CheckBinaryInputTensors(operand1, operand2, opName); | ||
| 208 | + CheckTensorsDataTypeConsistency(operand1, operand2, opName); | ||
| 209 | + | ||
| 210 | + std::vector<SymbolicScalar> resultValidShape; | ||
| 211 | + std::vector<int64_t> resultShape = BinaryOperationResultShape(operand1, operand2); | ||
| 212 | + if ((!operand1->GetDynValidShape().empty()) && (!operand2->GetDynValidShape().empty())) { | ||
| 213 | + for (size_t i = 0; i < resultShape.size(); ++i) { | ||
| 214 | + if (resultShape[i] == operand1->shape[i]) { | ||
| 215 | + resultValidShape.push_back(self.GetStorage()->GetDynValidShape()[i]); | ||
| 216 | + } else { | ||
| 217 | + resultValidShape.push_back(other.GetStorage()->GetDynValidShape()[i]); | ||
| 218 | + } | ||
| 219 | + } | ||
| 220 | + } | ||
| 221 | + auto result = std::make_shared<LogicalTensor>(function, operand1->Datatype(), resultShape, resultValidShape, | ||
| 222 | + operand1->Format()); | ||
| 223 | + function.AddOperation(GetBitwiseShiftOpNameCode<T>(), {operand1, operand2}, {result}); | ||
| 224 | + return result; | ||
| 225 | +} | ||
| 226 | + | ||
| 227 | +template <BitwiseShiftOpType T> | ||
| 228 | +void TiledBitwiseShiftOperationScalar(Function& function, const TileShape& tileShape, size_t cur, LogicalInput& self, | ||
| 229 | + Element& value, const LogicalTensorPtr& result, TileInfo& resultTileInfo, | ||
| 230 | + bool reverseOperand) | ||
| 231 | +{ | ||
| 232 | + if (cur == self.tensor->GetShape().size()) { | ||
| 233 | + auto inputTile = self.tensor->View(function, self.tileInfo.shape, self.tileInfo.offset); | ||
| 234 | + auto resultTile = result->View(function, resultTileInfo.shape, resultTileInfo.offset); | ||
| 235 | + auto& op = function.AddOperation(GetBitwiseShiftOpNameCode<T, true>(), {inputTile}, {resultTile}); | ||
| 236 | + op.SetAttribute(OpAttributeKey::scalar, value); | ||
| 237 | + op.SetAttribute(OP_ATTR_PREFIX + "reverseOperand", reverseOperand); | ||
| 238 | + return; | ||
| 239 | + } | ||
| 240 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 241 | + for (int i = 0; i < result->shape[cur]; i += vecTile[cur]) { | ||
| 242 | + resultTileInfo.offset[cur] = i; | ||
| 243 | + resultTileInfo.shape[cur] = std::min(result->shape[cur] - resultTileInfo.offset[cur], vecTile[cur]); | ||
| 244 | + self.tileInfo.offset[cur] = i % self.tensor->GetShape()[cur]; | ||
| 245 | + self.tileInfo.shape[cur] = std::min(self.tensor->GetShape()[cur] - self.tileInfo.offset[cur], vecTile[cur]); | ||
| 246 | + TiledBitwiseShiftOperationScalar<T>(function, tileShape, cur + 1, self, value, result, resultTileInfo, | ||
| 247 | + reverseOperand); | ||
| 248 | + } | ||
| 249 | +} | ||
| 250 | + | ||
| 251 | +template <BitwiseShiftOpType T> | ||
| 252 | +void TiledBitwiseShiftOperationScalar(Function& function, const TileShape& tileShape, LogicalTensorPtr operand, | ||
| 253 | + Element value, const LogicalTensorPtr& result, bool reverseOperand = false) | ||
| 254 | +{ | ||
| 255 | + TileInfo tileInfo(result->shape.size(), result->offset.size()); | ||
| 256 | + TileInfo resultTileInfo(result->shape.size(), result->offset.size()); | ||
| 257 | + auto self = LogicalInput{operand, tileInfo}; | ||
| 258 | + TiledBitwiseShiftOperationScalar<T>(function, tileShape, 0, self, value, result, resultTileInfo, reverseOperand); | ||
| 259 | +} | ||
| 260 | + | ||
| 261 | +template <BitwiseShiftOpType T> | ||
| 262 | +LogicalTensorPtr TensorBitwiseShiftOperationScalar(Function& function, const LogicalTensorPtr& self, | ||
| 263 | + const Element& other) | ||
| 264 | +{ | ||
| 265 | + auto opName = GetBitwiseShiftOpName<T>(); | ||
| 266 | + CheckTensorDimRange(self, MIN_TENSOR_DIM, MAX_TENSOR_DIM, opName); | ||
| 267 | + CheckTensorShapeSize(self, opName); | ||
| 268 | + auto result = std::make_shared<LogicalTensor>(function, self->Datatype(), self->shape, self->GetDynValidShape()); | ||
| 269 | + auto& op = function.AddOperation(GetBitwiseShiftOpNameCode<T, true>(), {self}, {result}); | ||
| 270 | + op.SetAttribute(OpAttributeKey::scalar, other); | ||
| 271 | + return result; | ||
| 272 | +} | ||
| 273 | + | ||
| 274 | +template <BitwiseShiftOpType T> | ||
| 275 | +void TiledBitwiseShiftOperationSelfScalar(Function& function, const TileShape& tileShape, size_t cur, Element& value, | ||
| 276 | + LogicalInput& other, const LogicalTensorPtr& result, TileInfo& resultTileInfo, | ||
| 277 | + bool reverseOperand) | ||
| 278 | +{ | ||
| 279 | + if (cur == other.tensor->GetShape().size()) { | ||
| 280 | + auto inputTile = other.tensor->View(function, other.tileInfo.shape, other.tileInfo.offset); | ||
| 281 | + auto resultTile = result->View(function, resultTileInfo.shape, resultTileInfo.offset); | ||
| 282 | + | ||
| 283 | + Shape tmpShape = resultTileInfo.shape; | ||
| 284 | + auto alignSize = BLOCK_SIZE / static_cast<int64_t>(BytesOf(result->Datatype())); | ||
| 285 | + tmpShape[tmpShape.size() - 1] = AlignUp(tmpShape[tmpShape.size() - 1], alignSize); | ||
| 286 | + auto tmpTile = std::make_shared<LogicalTensor>(function, result->Datatype(), tmpShape); | ||
| 287 | + auto& op = function.AddOperation(GetBitwiseShiftOpNameCode<T>(), {inputTile}, {resultTile, tmpTile}); | ||
| 288 | + op.SetAttribute(OpAttributeKey::scalar, value); | ||
| 289 | + op.SetAttribute(OP_ATTR_PREFIX + "reverseOperand", reverseOperand); | ||
| 290 | + return; | ||
| 291 | + } | ||
| 292 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 293 | + for (int i = 0; i < result->shape[cur]; i += vecTile[cur]) { | ||
| 294 | + resultTileInfo.offset[cur] = i; | ||
| 295 | + resultTileInfo.shape[cur] = std::min(result->shape[cur] - resultTileInfo.offset[cur], vecTile[cur]); | ||
| 296 | + other.tileInfo.offset[cur] = i % other.tensor->GetShape()[cur]; | ||
| 297 | + other.tileInfo.shape[cur] = std::min(other.tensor->GetShape()[cur] - other.tileInfo.offset[cur], vecTile[cur]); | ||
| 298 | + TiledBitwiseShiftOperationSelfScalar<T>(function, tileShape, cur + 1, value, other, result, resultTileInfo, | ||
| 299 | + reverseOperand); | ||
| 300 | + } | ||
| 301 | +} | ||
| 302 | + | ||
| 303 | +template <BitwiseShiftOpType T> | ||
| 304 | +void TiledBitwiseShiftOperationSelfScalar(Function& function, const TileShape& tileShape, Element value, | ||
| 305 | + LogicalTensorPtr operand, const LogicalTensorPtr& result, | ||
| 306 | + bool reverseOperand = false) | ||
| 307 | +{ | ||
| 308 | + TileInfo tileInfo(result->shape.size(), result->offset.size()); | ||
| 309 | + TileInfo resultTileInfo(result->shape.size(), result->offset.size()); | ||
| 310 | + auto other = LogicalInput{operand, tileInfo}; | ||
| 311 | + TiledBitwiseShiftOperationSelfScalar<T>(function, tileShape, 0, value, other, result, resultTileInfo, | ||
| 312 | + reverseOperand); | ||
| 313 | +} | ||
| 314 | + | ||
| 315 | +template <BitwiseShiftOpType T> | ||
| 316 | +LogicalTensorPtr TensorBitwiseShiftOperationSelfScalar(Function& function, const Element& self, | ||
| 317 | + const LogicalTensorPtr& other) | ||
| 318 | +{ | ||
| 319 | + auto opName = GetBitwiseShiftOpName<T>(); | ||
| 320 | + CheckTensorDimRange(other, MIN_TENSOR_DIM, MAX_TENSOR_DIM, opName); | ||
| 321 | + CheckTensorShapeSize(other, opName); | ||
| 322 | + auto result = std::make_shared<LogicalTensor>(function, other->Datatype(), other->shape, other->GetDynValidShape()); | ||
| 323 | + auto& op = function.AddOperation(GetBitwiseShiftOpNameCode<T>(), {other}, {result}); | ||
| 324 | + op.SetAttribute(OpAttributeKey::scalar, self); | ||
| 325 | + return result; | ||
| 326 | +} | ||
| 327 | + | ||
| 328 | +namespace { | ||
| 329 | +const std::unordered_set<DataType> BITWISESHIFT_A2A3_TYPES = {DT_INT16, DT_UINT16}; | ||
| 330 | +const std::unordered_set<DataType> BITWISESHIFT_A5_TYPES = {DT_INT16, DT_UINT16, DT_INT32, DT_UINT32, | ||
| 331 | + DT_INT8, DT_UINT8, DT_INT64, DT_UINT64}; | ||
| 332 | + | ||
| 333 | +// 按目标 dtype 构造位移标量 Element:uint64 不经 int64 中转,避免大值符号翻转。 | ||
| 334 | +Element CastShiftScalarToDtype(const Element& scalar, DataType dtype) | ||
| 335 | +{ | ||
| 336 | + if (dtype == DT_UINT64) { | ||
| 337 | + return Element(dtype, scalar.Cast<uint64_t>()); | ||
| 338 | + } | ||
| 339 | + return Element(dtype, scalar.Cast<int64_t>()); | ||
| 340 | +} | ||
| 341 | +} // namespace | ||
| 342 | + | ||
| 343 | +Tensor BitwiseRightShift(const Tensor& self, const Tensor& other) | ||
| 344 | +{ | ||
| 345 | + DECLARE_TRACER(); | ||
| 346 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseRightShift"); | ||
| 347 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseRightShift"); | ||
| 348 | + | ||
| 349 | + const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 350 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "BitwiseRightShift"); | ||
| 351 | + RETURN_CALL(BitwiseShiftOperation<BitwiseShiftOpType::BITWISERIGHTSHIFT>, | ||
| 352 | + *Program::GetInstance().GetCurrentFunction(), self, other); | ||
| 353 | +} | ||
| 354 | + | ||
| 355 | +Tensor BitwiseRightShift(const Tensor& self, const Element& other) | ||
| 356 | +{ | ||
| 357 | + DECLARE_TRACER(); | ||
| 358 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseRightShift"); | ||
| 359 | + | ||
| 360 | + const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 361 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "BitwiseRightShift"); | ||
| 362 | + Element newOther = other; | ||
| 363 | + if (self.GetDataType() != other.GetDataType()) { | ||
| 364 | + newOther = CastShiftScalarToDtype(other, self.GetDataType()); | ||
| 365 | + } | ||
| 366 | + RETURN_CALL(BitwiseShiftOperationScalar<BitwiseShiftOpType::BITWISERIGHTSHIFT>, | ||
| 367 | + *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), newOther); | ||
| 368 | +} | ||
| 369 | + | ||
| 370 | +Tensor BitwiseRightShift(const Element& self, const Tensor& other) | ||
| 371 | +{ | ||
| 372 | + DECLARE_TRACER(); | ||
| 373 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseRightShift"); | ||
| 374 | + | ||
| 375 | + const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 376 | + CheckTensorDataType(other.GetStorage(), supportedTypes, "BitwiseRightShift"); | ||
| 377 | + Element newSelf = self; | ||
| 378 | + if (self.GetDataType() != other.GetDataType()) { | ||
| 379 | + newSelf = CastShiftScalarToDtype(self, other.GetDataType()); | ||
| 380 | + } | ||
| 381 | + RETURN_CALL(BitwiseShiftOperationSelfScalar<BitwiseShiftOpType::SBITWISERIGHTSHIFT>, | ||
| 382 | + *Program::GetInstance().GetCurrentFunction(), newSelf, other.GetStorage()); | ||
| 383 | +} | ||
| 384 | + | ||
| 385 | +Tensor BitwiseLeftShift(const Tensor& self, const Tensor& other) | ||
| 386 | +{ | ||
| 387 | + DECLARE_TRACER(); | ||
| 388 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseLeftShift"); | ||
| 389 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseLeftShift"); | ||
| 390 | + | ||
| 391 | + const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 392 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "BitwiseLeftShift"); | ||
| 393 | + RETURN_CALL(BitwiseShiftOperation<BitwiseShiftOpType::BITWISELEFTSHIFT>, | ||
| 394 | + *Program::GetInstance().GetCurrentFunction(), self, other); | ||
| 395 | +} | ||
| 396 | + | ||
| 397 | +Tensor BitwiseLeftShift(const Tensor& self, const Element& other) | ||
| 398 | +{ | ||
| 399 | + DECLARE_TRACER(); | ||
| 400 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseLeftShift"); | ||
| 401 | + | ||
| 402 | + const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 403 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "BitwiseLeftShift"); | ||
| 404 | + Element newOther = other; | ||
| 405 | + if (self.GetDataType() != other.GetDataType()) { | ||
| 406 | + newOther = CastShiftScalarToDtype(other, self.GetDataType()); | ||
| 407 | + } | ||
| 408 | + RETURN_CALL(BitwiseShiftOperationScalar<BitwiseShiftOpType::BITWISELEFTSHIFT>, | ||
| 409 | + *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), newOther); | ||
| 410 | +} | ||
| 411 | + | ||
| 412 | +Tensor BitwiseLeftShift(const Element& self, const Tensor& other) | ||
| 413 | +{ | ||
| 414 | + DECLARE_TRACER(); | ||
| 415 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseLeftShift"); | ||
| 416 | + | ||
| 417 | + const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 418 | + CheckTensorDataType(other.GetStorage(), supportedTypes, "BitwiseLeftShift"); | ||
| 419 | + Element newSelf = self; | ||
| 420 | + if (self.GetDataType() != other.GetDataType()) { | ||
| 421 | + newSelf = CastShiftScalarToDtype(self, other.GetDataType()); | ||
| 422 | + } | ||
| 423 | + RETURN_CALL(BitwiseShiftOperationSelfScalar<BitwiseShiftOpType::SBITWISELEFTSHIFT>, | ||
| 424 | + *Program::GetInstance().GetCurrentFunction(), newSelf, other.GetStorage()); | ||
| 425 | +} | ||
| 426 | + | ||
| 427 | +template <BitwiseShiftOpType T> | ||
| 428 | +void BitwiseShiftOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 429 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 430 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 431 | +{ | ||
| 432 | + BinaryOperationOperandCheck(iOperand, oOperand); | ||
| 433 | + TiledBitwiseShiftOperation<T>(function, tileShape, iOperand[0], iOperand[1], oOperand[0]); | ||
| 434 | +} | ||
| 435 | + | ||
| 436 | +template <BitwiseShiftOpType T> | ||
| 437 | +void BitwiseShiftOperationScalarTileFunc(Function& function, const TileShape& tileShape, | ||
| 438 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 439 | + const std::vector<LogicalTensorPtr>& oOperand, | ||
| 440 | + [[maybe_unused]] const Operation& op) | ||
| 441 | +{ | ||
| 442 | + TiledBitwiseShiftOperationScalar<T>(function, tileShape, iOperand[0], | ||
| 443 | + op.GetElementAttribute(OpAttributeKey::scalar), oOperand[0]); | ||
| 444 | +} | ||
| 445 | + | ||
| 446 | +template <BitwiseShiftOpType T> | ||
| 447 | +void BitwiseShiftOperationSelfScalarTileFunc(Function& function, const TileShape& tileShape, | ||
| 448 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 449 | + const std::vector<LogicalTensorPtr>& oOperand, | ||
| 450 | + [[maybe_unused]] const Operation& op) | ||
| 451 | +{ | ||
| 452 | + TiledBitwiseShiftOperationSelfScalar<T>(function, tileShape, op.GetElementAttribute(OpAttributeKey::scalar), | ||
| 453 | + iOperand[0], oOperand[0]); | ||
| 454 | +} | ||
| 455 | + | ||
| 456 | +void BitwiseNotOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 457 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 458 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 459 | +{ | ||
| 460 | + UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 461 | + return TiledUnaryOperation<UnaryOpType::BITWISENOT>(function, tileShape, iOperand[0], oOperand[0]); | ||
| 462 | +} | ||
| 463 | + | ||
| 75 | REGISTER_OPERATION_TILED_FUNC(OP_BITWISEAND, Opcode::OP_BITWISEAND, BinaryOperationTileFunc<BinaryOpType::BITWISEAND>); | 464 | 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>); | 465 | 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>); | 466 | REGISTER_OPERATION_TILED_FUNC(OP_BITWISEXOR, Opcode::OP_BITWISEXOR, BinaryOperationTileFunc<BinaryOpType::BITWISEXOR>); |
| 467 | +REGISTER_OPERATION_TILED_FUNC(OP_BITWISENOT, Opcode::OP_BITWISENOT, BitwiseNotOperationTileFunc); | ||
| 78 | 468 | ||
| 469 | +REGISTER_OPERATION_TILED_FUNC(OP_BITWISERIGHTSHIFT, Opcode::OP_BITWISERIGHTSHIFT, | ||
| 470 | + BitwiseShiftOperationTileFunc<BitwiseShiftOpType::BITWISERIGHTSHIFT>); | ||
| 471 | +REGISTER_OPERATION_TILED_FUNC(OP_BITWISELEFTSHIFT, Opcode::OP_BITWISELEFTSHIFT, | ||
| 472 | + BitwiseShiftOperationTileFunc<BitwiseShiftOpType::BITWISELEFTSHIFT>); | ||
| 473 | + | ||
| 474 | +REGISTER_OPERATION_TILED_FUNC(OP_BITWISERIGHTSHIFTS, Opcode::OP_BITWISERIGHTSHIFTS, | ||
| 475 | + BitwiseShiftOperationScalarTileFunc<BitwiseShiftOpType::BITWISERIGHTSHIFT>); | ||
| 476 | +REGISTER_OPERATION_TILED_FUNC(OP_BITWISELEFTSHIFTS, Opcode::OP_BITWISELEFTSHIFTS, | ||
| 477 | + BitwiseShiftOperationScalarTileFunc<BitwiseShiftOpType::BITWISELEFTSHIFT>); | ||
| 478 | + | ||
| 479 | +REGISTER_OPERATION_TILED_FUNC(OP_SBITWISERIGHTSHIFT, Opcode::OP_SBITWISERIGHTSHIFT, | ||
| 480 | + BitwiseShiftOperationSelfScalarTileFunc<BitwiseShiftOpType::SBITWISERIGHTSHIFT>); | ||
| 481 | +REGISTER_OPERATION_TILED_FUNC(OP_SBITWISELEFTSHIFT, Opcode::OP_SBITWISELEFTSHIFT, | ||
| 482 | + BitwiseShiftOperationSelfScalarTileFunc<BitwiseShiftOpType::SBITWISELEFTSHIFT>); | ||
| 79 | } // namespace npu::tile_fwk | 483 | } // namespace npu::tile_fwk |
| @@ -1,404 +0,0 @@ | |||
| 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_shift.cpp | ||
| 13 | - * \brief | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#include "binary.h" | ||
| 17 | -#include "tensor_transformation.h" | ||
| 18 | -#include "interface/utils/operator_tracer.h" | ||
| 19 | -#include "interface/utils/common.h" | ||
| 20 | -#include "interface/configs/config_manager.h" | ||
| 21 | -#include "tilefwk/error_code.h" | ||
| 22 | - | ||
| 23 | -namespace npu::tile_fwk { | ||
| 24 | - | ||
| 25 | -enum class BitwiseShiftOpType { | ||
| 26 | - BITWISERIGHTSHIFT, | ||
| 27 | - BITWISELEFTSHIFT, | ||
| 28 | - SBITWISERIGHTSHIFT, | ||
| 29 | - SBITWISELEFTSHIFT, | ||
| 30 | -}; | ||
| 31 | - | ||
| 32 | -template <BitwiseShiftOpType T> | ||
| 33 | -std::string GetBitwiseShiftOpName() | ||
| 34 | -{ | ||
| 35 | - switch (T) { | ||
| 36 | - case BitwiseShiftOpType::BITWISERIGHTSHIFT: | ||
| 37 | - return "BITWISERIGHTSHIFT"; | ||
| 38 | - case BitwiseShiftOpType::BITWISELEFTSHIFT: | ||
| 39 | - return "BITWISELEFTSHIFT"; | ||
| 40 | - case BitwiseShiftOpType::SBITWISERIGHTSHIFT: | ||
| 41 | - return "SBITWISERIGHTSHIFT"; | ||
| 42 | - case BitwiseShiftOpType::SBITWISELEFTSHIFT: | ||
| 43 | - return "SBITWISELEFTSHIFT"; | ||
| 44 | - default: | ||
| 45 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "unknown binary op type"; | ||
| 46 | - return ""; | ||
| 47 | - } | ||
| 48 | -} | ||
| 49 | - | ||
| 50 | -template <BitwiseShiftOpType T, bool WithElement = false> | ||
| 51 | -Opcode GetBitwiseShiftOpNameCode() | ||
| 52 | -{ | ||
| 53 | - if constexpr (WithElement) { | ||
| 54 | -#define CASE(X) \ | ||
| 55 | - case BitwiseShiftOpType::X: \ | ||
| 56 | - return Opcode::OP_##X##S | ||
| 57 | - switch (T) { | ||
| 58 | - CASE(BITWISERIGHTSHIFT); | ||
| 59 | - CASE(BITWISELEFTSHIFT); | ||
| 60 | - default: | ||
| 61 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "unknown binary op type"; | ||
| 62 | - } | ||
| 63 | -#undef CASE | ||
| 64 | - } | ||
| 65 | - | ||
| 66 | -#define CASE(X) \ | ||
| 67 | - case BitwiseShiftOpType::X: \ | ||
| 68 | - return Opcode::OP_##X | ||
| 69 | - switch (T) { | ||
| 70 | - CASE(BITWISERIGHTSHIFT); | ||
| 71 | - CASE(BITWISELEFTSHIFT); | ||
| 72 | - CASE(SBITWISERIGHTSHIFT); | ||
| 73 | - CASE(SBITWISELEFTSHIFT); | ||
| 74 | - default: | ||
| 75 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, false) << "unknown binary op type"; | ||
| 76 | - } | ||
| 77 | -#undef CASE | ||
| 78 | -} | ||
| 79 | - | ||
| 80 | -template <BitwiseShiftOpType T> | ||
| 81 | -void TiledBitwiseShiftOperation(Function& function, const TileShape& tileShape, size_t cur, LogicalInput& input1, | ||
| 82 | - LogicalInput& input2, const LogicalTensorPtr& result, TileInfo& resultTileInfo) | ||
| 83 | -{ | ||
| 84 | - if (cur == input1.tensor->GetShape().size()) { | ||
| 85 | - auto inputTile1 = input1.tensor->View(function, input1.tileInfo.shape, input1.tileInfo.offset); | ||
| 86 | - auto inputTile2 = input2.tensor->View(function, input2.tileInfo.shape, input2.tileInfo.offset); | ||
| 87 | - auto resultTile = result->View(function, resultTileInfo.shape, resultTileInfo.offset); | ||
| 88 | - | ||
| 89 | - Shape tmpShape = resultTileInfo.shape; | ||
| 90 | - auto alignSize = BLOCK_SIZE / static_cast<int64_t>(BytesOf(result->Datatype())); | ||
| 91 | - tmpShape[tmpShape.size() - 1] = AlignUp(tmpShape[tmpShape.size() - 1], alignSize); | ||
| 92 | - auto tmpTile = std::make_shared<LogicalTensor>(function, result->Datatype(), tmpShape); | ||
| 93 | - function.AddOperation(GetBitwiseShiftOpNameCode<T, false>(), {inputTile1, inputTile2}, {resultTile, tmpTile}); | ||
| 94 | - return; | ||
| 95 | - } | ||
| 96 | - auto& vecTile = tileShape.GetVecTile(); | ||
| 97 | - for (int i = 0; i < result->shape[cur]; i += vecTile[cur]) { | ||
| 98 | - resultTileInfo.offset[cur] = i; | ||
| 99 | - input1.tileInfo.offset[cur] = i % input1.tensor->GetShape()[cur]; | ||
| 100 | - input2.tileInfo.offset[cur] = i % input2.tensor->GetShape()[cur]; | ||
| 101 | - resultTileInfo.shape[cur] = std::min(result->shape[cur] - resultTileInfo.offset[cur], vecTile[cur]); | ||
| 102 | - input1.tileInfo.shape[cur] = std::min(input1.tensor->GetShape()[cur] - input1.tileInfo.offset[cur], | ||
| 103 | - vecTile[cur]); | ||
| 104 | - input2.tileInfo.shape[cur] = std::min(input2.tensor->GetShape()[cur] - input2.tileInfo.offset[cur], | ||
| 105 | - vecTile[cur]); | ||
| 106 | - TiledBitwiseShiftOperation<T>(function, tileShape, cur + 1, input1, input2, result, resultTileInfo); | ||
| 107 | - } | ||
| 108 | -} | ||
| 109 | - | ||
| 110 | -template <BitwiseShiftOpType T> | ||
| 111 | -void TiledBitwiseShiftOperation(Function& function, const TileShape& tileShape, LogicalTensorPtr operand1, | ||
| 112 | - LogicalTensorPtr operand2, const LogicalTensorPtr& result) | ||
| 113 | -{ | ||
| 114 | - CheckBinOpOperandsValid(operand1, operand2); | ||
| 115 | - BroadcastOperandTensor(operand1, operand2, result, function, tileShape); | ||
| 116 | - BroadcastOperandTensor(operand2, operand1, result, function, tileShape); | ||
| 117 | - | ||
| 118 | - TileInfo tileInfo1(result->shape.size(), result->offset.size()); | ||
| 119 | - TileInfo tileInfo2(result->shape.size(), result->offset.size()); | ||
| 120 | - TileInfo resultTileInfo(result->shape.size(), result->offset.size()); | ||
| 121 | - auto input1 = LogicalInput{operand1, tileInfo1}; | ||
| 122 | - auto input2 = LogicalInput{operand2, tileInfo2}; | ||
| 123 | - // 如果使能了Combine Axis逻辑,需要将withbrc置为false,避免后续走OP_XX_BRC逻辑 | ||
| 124 | - TiledBitwiseShiftOperation<T>(function, tileShape, 0, input1, input2, result, resultTileInfo); | ||
| 125 | -} | ||
| 126 | - | ||
| 127 | -template <BitwiseShiftOpType T> | ||
| 128 | -LogicalTensorPtr TensorBitwiseShiftOperation(Function& function, const Tensor& self, const Tensor& other) | ||
| 129 | -{ | ||
| 130 | - auto operand1 = self.GetStorage(); | ||
| 131 | - auto operand2 = other.GetStorage(); | ||
| 132 | - if (operand1->shape.size() != operand2->shape.size()) { | ||
| 133 | - std::vector<int> broadCastShape = GetBroadCastShape(operand1, operand2); | ||
| 134 | - operand1 = BinaryOperationBroadCast(operand1, broadCastShape); | ||
| 135 | - operand2 = BinaryOperationBroadCast(operand2, broadCastShape); | ||
| 136 | - } | ||
| 137 | - auto opName = GetBitwiseShiftOpName<T>(); | ||
| 138 | - CheckBinaryInputTensors(operand1, operand2, opName); | ||
| 139 | - CheckTensorsDataTypeConsistency(operand1, operand2, opName); | ||
| 140 | - | ||
| 141 | - std::vector<SymbolicScalar> resultValidShape; | ||
| 142 | - std::vector<int64_t> resultShape = BinaryOperationResultShape(operand1, operand2); | ||
| 143 | - if ((!operand1->GetDynValidShape().empty()) && (!operand2->GetDynValidShape().empty())) { | ||
| 144 | - for (size_t i = 0; i < resultShape.size(); ++i) { | ||
| 145 | - if (resultShape[i] == operand1->shape[i]) { | ||
| 146 | - resultValidShape.push_back(self.GetStorage()->GetDynValidShape()[i]); | ||
| 147 | - } else { | ||
| 148 | - resultValidShape.push_back(other.GetStorage()->GetDynValidShape()[i]); | ||
| 149 | - } | ||
| 150 | - } | ||
| 151 | - } | ||
| 152 | - auto result = std::make_shared<LogicalTensor>(function, operand1->Datatype(), resultShape, resultValidShape, | ||
| 153 | - operand1->Format()); | ||
| 154 | - function.AddOperation(GetBitwiseShiftOpNameCode<T>(), {operand1, operand2}, {result}); | ||
| 155 | - return result; | ||
| 156 | -} | ||
| 157 | - | ||
| 158 | -template <BitwiseShiftOpType T> | ||
| 159 | -void TiledBitwiseShiftOperationScalar(Function& function, const TileShape& tileShape, size_t cur, LogicalInput& self, | ||
| 160 | - Element& value, const LogicalTensorPtr& result, TileInfo& resultTileInfo, | ||
| 161 | - bool reverseOperand) | ||
| 162 | -{ | ||
| 163 | - if (cur == self.tensor->GetShape().size()) { | ||
| 164 | - auto inputTile = self.tensor->View(function, self.tileInfo.shape, self.tileInfo.offset); | ||
| 165 | - auto resultTile = result->View(function, resultTileInfo.shape, resultTileInfo.offset); | ||
| 166 | - auto& op = function.AddOperation(GetBitwiseShiftOpNameCode<T, true>(), {inputTile}, {resultTile}); | ||
| 167 | - op.SetAttribute(OpAttributeKey::scalar, value); | ||
| 168 | - op.SetAttribute(OP_ATTR_PREFIX + "reverseOperand", reverseOperand); | ||
| 169 | - return; | ||
| 170 | - } | ||
| 171 | - auto& vecTile = tileShape.GetVecTile(); | ||
| 172 | - for (int i = 0; i < result->shape[cur]; i += vecTile[cur]) { | ||
| 173 | - resultTileInfo.offset[cur] = i; | ||
| 174 | - resultTileInfo.shape[cur] = std::min(result->shape[cur] - resultTileInfo.offset[cur], vecTile[cur]); | ||
| 175 | - self.tileInfo.offset[cur] = i % self.tensor->GetShape()[cur]; | ||
| 176 | - self.tileInfo.shape[cur] = std::min(self.tensor->GetShape()[cur] - self.tileInfo.offset[cur], vecTile[cur]); | ||
| 177 | - | ||
| 178 | - TiledBitwiseShiftOperationScalar<T>(function, tileShape, cur + 1, self, value, result, resultTileInfo, | ||
| 179 | - reverseOperand); | ||
| 180 | - } | ||
| 181 | -} | ||
| 182 | - | ||
| 183 | -template <BitwiseShiftOpType T> | ||
| 184 | -void TiledBitwiseShiftOperationScalar(Function& function, const TileShape& tileShape, LogicalTensorPtr operand, | ||
| 185 | - Element value, const LogicalTensorPtr& result, bool reverseOperand = false) | ||
| 186 | -{ | ||
| 187 | - TileInfo tileInfo(result->shape.size(), result->offset.size()); | ||
| 188 | - TileInfo resultTileInfo(result->shape.size(), result->offset.size()); | ||
| 189 | - auto self = LogicalInput{operand, tileInfo}; | ||
| 190 | - TiledBitwiseShiftOperationScalar<T>(function, tileShape, 0, self, value, result, resultTileInfo, reverseOperand); | ||
| 191 | -} | ||
| 192 | - | ||
| 193 | -template <BitwiseShiftOpType T> | ||
| 194 | -LogicalTensorPtr TensorBitwiseShiftOperationScalar(Function& function, const LogicalTensorPtr& self, | ||
| 195 | - const Element& other) | ||
| 196 | -{ | ||
| 197 | - auto opName = GetBitwiseShiftOpName<T>(); | ||
| 198 | - CheckTensorDimRange(self, MIN_TENSOR_DIM, MAX_TENSOR_DIM, opName); | ||
| 199 | - CheckTensorShapeSize(self, opName); | ||
| 200 | - auto result = std::make_shared<LogicalTensor>(function, self->Datatype(), self->shape, self->GetDynValidShape()); | ||
| 201 | - auto& op = function.AddOperation(GetBitwiseShiftOpNameCode<T, true>(), {self}, {result}); | ||
| 202 | - op.SetAttribute(OpAttributeKey::scalar, other); | ||
| 203 | - return result; | ||
| 204 | -} | ||
| 205 | - | ||
| 206 | -template <BitwiseShiftOpType T> | ||
| 207 | -void TiledBitwiseShiftOperationSelfScalar(Function& function, const TileShape& tileShape, size_t cur, Element& value, | ||
| 208 | - LogicalInput& other, const LogicalTensorPtr& result, TileInfo& resultTileInfo, | ||
| 209 | - bool reverseOperand) | ||
| 210 | -{ | ||
| 211 | - if (cur == other.tensor->GetShape().size()) { | ||
| 212 | - auto inputTile = other.tensor->View(function, other.tileInfo.shape, other.tileInfo.offset); | ||
| 213 | - auto resultTile = result->View(function, resultTileInfo.shape, resultTileInfo.offset); | ||
| 214 | - | ||
| 215 | - Shape tmpShape = resultTileInfo.shape; | ||
| 216 | - auto alignSize = BLOCK_SIZE / static_cast<int64_t>(BytesOf(result->Datatype())); | ||
| 217 | - tmpShape[tmpShape.size() - 1] = AlignUp(tmpShape[tmpShape.size() - 1], alignSize); | ||
| 218 | - auto tmpTile = std::make_shared<LogicalTensor>(function, result->Datatype(), tmpShape); | ||
| 219 | - | ||
| 220 | - auto& op = function.AddOperation(GetBitwiseShiftOpNameCode<T>(), {inputTile}, {resultTile, tmpTile}); | ||
| 221 | - op.SetAttribute(OpAttributeKey::scalar, value); | ||
| 222 | - op.SetAttribute(OP_ATTR_PREFIX + "reverseOperand", reverseOperand); | ||
| 223 | - return; | ||
| 224 | - } | ||
| 225 | - auto& vecTile = tileShape.GetVecTile(); | ||
| 226 | - for (int i = 0; i < result->shape[cur]; i += vecTile[cur]) { | ||
| 227 | - resultTileInfo.offset[cur] = i; | ||
| 228 | - resultTileInfo.shape[cur] = std::min(result->shape[cur] - resultTileInfo.offset[cur], vecTile[cur]); | ||
| 229 | - other.tileInfo.offset[cur] = i % other.tensor->GetShape()[cur]; | ||
| 230 | - other.tileInfo.shape[cur] = std::min(other.tensor->GetShape()[cur] - other.tileInfo.offset[cur], vecTile[cur]); | ||
| 231 | - | ||
| 232 | - TiledBitwiseShiftOperationSelfScalar<T>(function, tileShape, cur + 1, value, other, result, resultTileInfo, | ||
| 233 | - reverseOperand); | ||
| 234 | - } | ||
| 235 | -} | ||
| 236 | - | ||
| 237 | -template <BitwiseShiftOpType T> | ||
| 238 | -void TiledBitwiseShiftOperationSelfScalar(Function& function, const TileShape& tileShape, Element value, | ||
| 239 | - LogicalTensorPtr operand, const LogicalTensorPtr& result, | ||
| 240 | - bool reverseOperand = false) | ||
| 241 | -{ | ||
| 242 | - TileInfo tileInfo(result->shape.size(), result->offset.size()); | ||
| 243 | - TileInfo resultTileInfo(result->shape.size(), result->offset.size()); | ||
| 244 | - auto other = LogicalInput{operand, tileInfo}; | ||
| 245 | - TiledBitwiseShiftOperationSelfScalar<T>(function, tileShape, 0, value, other, result, resultTileInfo, | ||
| 246 | - reverseOperand); | ||
| 247 | -} | ||
| 248 | - | ||
| 249 | -template <BitwiseShiftOpType T> | ||
| 250 | -LogicalTensorPtr TensorBitwiseShiftOperationSelfScalar(Function& function, const Element& self, | ||
| 251 | - const LogicalTensorPtr& other) | ||
| 252 | -{ | ||
| 253 | - auto opName = GetBitwiseShiftOpName<T>(); | ||
| 254 | - CheckTensorDimRange(other, MIN_TENSOR_DIM, MAX_TENSOR_DIM, opName); | ||
| 255 | - CheckTensorShapeSize(other, opName); | ||
| 256 | - auto result = std::make_shared<LogicalTensor>(function, other->Datatype(), other->shape, other->GetDynValidShape()); | ||
| 257 | - auto& op = function.AddOperation(GetBitwiseShiftOpNameCode<T>(), {other}, {result}); | ||
| 258 | - op.SetAttribute(OpAttributeKey::scalar, self); | ||
| 259 | - return result; | ||
| 260 | -} | ||
| 261 | - | ||
| 262 | -namespace { | ||
| 263 | -// TODO i8/u8/i32/u32: pending pto-isa additions on a2a3. | ||
| 264 | -const std::unordered_set<DataType> BITWISESHIFT_A2A3_TYPES = {DT_INT16, DT_UINT16}; | ||
| 265 | -const std::unordered_set<DataType> BITWISESHIFT_A5_TYPES = {DT_INT16, DT_UINT16, DT_INT32, DT_UINT32, | ||
| 266 | - DT_INT8, DT_UINT8, DT_INT64, DT_UINT64}; | ||
| 267 | - | ||
| 268 | -// 按目标 dtype 构造位移标量 Element:uint64 不经 int64 中转,避免大值符号翻转。 | ||
| 269 | -Element CastShiftScalarToDtype(const Element& scalar, DataType dtype) | ||
| 270 | -{ | ||
| 271 | - if (dtype == DT_UINT64) { | ||
| 272 | - return Element(dtype, scalar.Cast<uint64_t>()); | ||
| 273 | - } | ||
| 274 | - return Element(dtype, scalar.Cast<int64_t>()); | ||
| 275 | -} | ||
| 276 | -} // namespace | ||
| 277 | - | ||
| 278 | -Tensor BitwiseRightShift(const Tensor& self, const Tensor& other) | ||
| 279 | -{ | ||
| 280 | - DECLARE_TRACER(); | ||
| 281 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseRightShift"); | ||
| 282 | - CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseRightShift"); | ||
| 283 | - | ||
| 284 | - const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 285 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "BitwiseRightShift"); | ||
| 286 | - RETURN_CALL(BitwiseShiftOperation<BitwiseShiftOpType::BITWISERIGHTSHIFT>, | ||
| 287 | - *Program::GetInstance().GetCurrentFunction(), self, other); | ||
| 288 | -} | ||
| 289 | - | ||
| 290 | -Tensor BitwiseRightShift(const Tensor& self, const Element& other) | ||
| 291 | -{ | ||
| 292 | - DECLARE_TRACER(); | ||
| 293 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseRightShift"); | ||
| 294 | - | ||
| 295 | - const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 296 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "BitwiseRightShift"); | ||
| 297 | - Element newOther = other; | ||
| 298 | - if (self.GetDataType() != other.GetDataType()) { | ||
| 299 | - newOther = CastShiftScalarToDtype(other, self.GetDataType()); | ||
| 300 | - } | ||
| 301 | - RETURN_CALL(BitwiseShiftOperationScalar<BitwiseShiftOpType::BITWISERIGHTSHIFT>, | ||
| 302 | - *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), newOther); | ||
| 303 | -} | ||
| 304 | - | ||
| 305 | -Tensor BitwiseRightShift(const Element& self, const Tensor& other) | ||
| 306 | -{ | ||
| 307 | - DECLARE_TRACER(); | ||
| 308 | - CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseRightShift"); | ||
| 309 | - | ||
| 310 | - const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 311 | - CheckTensorDataType(other.GetStorage(), supportedTypes, "BitwiseRightShift"); | ||
| 312 | - Element newSelf = self; | ||
| 313 | - if (self.GetDataType() != other.GetDataType()) { | ||
| 314 | - newSelf = CastShiftScalarToDtype(self, other.GetDataType()); | ||
| 315 | - } | ||
| 316 | - RETURN_CALL(BitwiseShiftOperationSelfScalar<BitwiseShiftOpType::SBITWISERIGHTSHIFT>, | ||
| 317 | - *Program::GetInstance().GetCurrentFunction(), newSelf, other.GetStorage()); | ||
| 318 | -} | ||
| 319 | -Tensor BitwiseLeftShift(const Tensor& self, const Tensor& other) | ||
| 320 | -{ | ||
| 321 | - DECLARE_TRACER(); | ||
| 322 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseLeftShift"); | ||
| 323 | - CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseLeftShift"); | ||
| 324 | - | ||
| 325 | - const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 326 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "BitwiseLeftShift"); | ||
| 327 | - RETURN_CALL(BitwiseShiftOperation<BitwiseShiftOpType::BITWISELEFTSHIFT>, | ||
| 328 | - *Program::GetInstance().GetCurrentFunction(), self, other); | ||
| 329 | -} | ||
| 330 | - | ||
| 331 | -Tensor BitwiseLeftShift(const Tensor& self, const Element& other) | ||
| 332 | -{ | ||
| 333 | - DECLARE_TRACER(); | ||
| 334 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseLeftShift"); | ||
| 335 | - | ||
| 336 | - const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 337 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "BitwiseLeftShift"); | ||
| 338 | - Element newOther = other; | ||
| 339 | - if (self.GetDataType() != other.GetDataType()) { | ||
| 340 | - newOther = CastShiftScalarToDtype(other, self.GetDataType()); | ||
| 341 | - } | ||
| 342 | - RETURN_CALL(BitwiseShiftOperationScalar<BitwiseShiftOpType::BITWISELEFTSHIFT>, | ||
| 343 | - *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), newOther); | ||
| 344 | -} | ||
| 345 | - | ||
| 346 | -Tensor BitwiseLeftShift(const Element& self, const Tensor& other) | ||
| 347 | -{ | ||
| 348 | - DECLARE_TRACER(); | ||
| 349 | - CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseLeftShift"); | ||
| 350 | - | ||
| 351 | - const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISESHIFT_A2A3_TYPES, BITWISESHIFT_A5_TYPES); | ||
| 352 | - CheckTensorDataType(other.GetStorage(), supportedTypes, "BitwiseLeftShift"); | ||
| 353 | - Element newSelf = self; | ||
| 354 | - if (self.GetDataType() != other.GetDataType()) { | ||
| 355 | - newSelf = CastShiftScalarToDtype(self, other.GetDataType()); | ||
| 356 | - } | ||
| 357 | - RETURN_CALL(BitwiseShiftOperationSelfScalar<BitwiseShiftOpType::SBITWISELEFTSHIFT>, | ||
| 358 | - *Program::GetInstance().GetCurrentFunction(), newSelf, other.GetStorage()); | ||
| 359 | -} | ||
| 360 | - | ||
| 361 | -template <BitwiseShiftOpType T> | ||
| 362 | -void BitwiseShiftOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 363 | - const std::vector<LogicalTensorPtr>& iOperand, | ||
| 364 | - const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 365 | -{ | ||
| 366 | - BinaryOperationOperandCheck(iOperand, oOperand); | ||
| 367 | - TiledBitwiseShiftOperation<T>(function, tileShape, iOperand[0], iOperand[1], oOperand[0]); | ||
| 368 | -} | ||
| 369 | - | ||
| 370 | -template <BitwiseShiftOpType T> | ||
| 371 | -void BitwiseShiftOperationScalarTileFunc(Function& function, const TileShape& tileShape, | ||
| 372 | - const std::vector<LogicalTensorPtr>& iOperand, | ||
| 373 | - const std::vector<LogicalTensorPtr>& oOperand, | ||
| 374 | - [[maybe_unused]] const Operation& op) | ||
| 375 | -{ | ||
| 376 | - TiledBitwiseShiftOperationScalar<T>(function, tileShape, iOperand[0], | ||
| 377 | - op.GetElementAttribute(OpAttributeKey::scalar), oOperand[0]); | ||
| 378 | -} | ||
| 379 | - | ||
| 380 | -template <BitwiseShiftOpType T> | ||
| 381 | -void BitwiseShiftOperationSelfScalarTileFunc(Function& function, const TileShape& tileShape, | ||
| 382 | - const std::vector<LogicalTensorPtr>& iOperand, | ||
| 383 | - const std::vector<LogicalTensorPtr>& oOperand, | ||
| 384 | - [[maybe_unused]] const Operation& op) | ||
| 385 | -{ | ||
| 386 | - TiledBitwiseShiftOperationSelfScalar<T>(function, tileShape, op.GetElementAttribute(OpAttributeKey::scalar), | ||
| 387 | - iOperand[0], oOperand[0]); | ||
| 388 | -} | ||
| 389 | - | ||
| 390 | -REGISTER_OPERATION_TILED_FUNC(OP_BITWISERIGHTSHIFT, Opcode::OP_BITWISERIGHTSHIFT, | ||
| 391 | - BitwiseShiftOperationTileFunc<BitwiseShiftOpType::BITWISERIGHTSHIFT>); | ||
| 392 | -REGISTER_OPERATION_TILED_FUNC(OP_BITWISELEFTSHIFT, Opcode::OP_BITWISELEFTSHIFT, | ||
| 393 | - BitwiseShiftOperationTileFunc<BitwiseShiftOpType::BITWISELEFTSHIFT>); | ||
| 394 | - | ||
| 395 | -REGISTER_OPERATION_TILED_FUNC(OP_BITWISERIGHTSHIFTS, Opcode::OP_BITWISERIGHTSHIFTS, | ||
| 396 | - BitwiseShiftOperationScalarTileFunc<BitwiseShiftOpType::BITWISERIGHTSHIFT>); | ||
| 397 | -REGISTER_OPERATION_TILED_FUNC(OP_BITWISELEFTSHIFTS, Opcode::OP_BITWISELEFTSHIFTS, | ||
| 398 | - BitwiseShiftOperationScalarTileFunc<BitwiseShiftOpType::BITWISELEFTSHIFT>); | ||
| 399 | - | ||
| 400 | -REGISTER_OPERATION_TILED_FUNC(OP_SBITWISERIGHTSHIFT, Opcode::OP_SBITWISERIGHTSHIFT, | ||
| 401 | - BitwiseShiftOperationSelfScalarTileFunc<BitwiseShiftOpType::SBITWISERIGHTSHIFT>); | ||
| 402 | -REGISTER_OPERATION_TILED_FUNC(OP_SBITWISELEFTSHIFT, Opcode::OP_SBITWISELEFTSHIFT, | ||
| 403 | - BitwiseShiftOperationSelfScalarTileFunc<BitwiseShiftOpType::SBITWISELEFTSHIFT>); | ||
| 404 | -} // namespace npu::tile_fwk | ||
| @@ -14,7 +14,6 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "unary.h" | 16 | #include "unary.h" |
| 17 | -#include <sstream> | ||
| 18 | #include <string> | 17 | #include <string> |
| 19 | #include "tensor_transformation.h" | 18 | #include "tensor_transformation.h" |
| 20 | #include "interface/utils/operator_tracer.h" | 19 | #include "interface/utils/operator_tracer.h" |
| @@ -14,8 +14,6 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "unary.h" | 16 | #include "unary.h" |
| 17 | -#include <sstream> | ||
| 18 | -#include <string> | ||
| 19 | #include "tensor_transformation.h" | 17 | #include "tensor_transformation.h" |
| 20 | #include "interface/utils/operator_tracer.h" | 18 | #include "interface/utils/operator_tracer.h" |
| 21 | #include "tilefwk/error_code.h" | 19 | #include "tilefwk/error_code.h" |
| @@ -1,98 +0,0 @@ | |||
| 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 expand_exp_dif.cpp | ||
| 13 | - * \brief | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#include "binary.h" | ||
| 17 | -#include "interface/utils/operator_tracer.h" | ||
| 18 | -#include "passes/pass_utils/graph_utils.h" | ||
| 19 | -#include "tensor_transformation.h" | ||
| 20 | - | ||
| 21 | -namespace npu::tile_fwk { | ||
| 22 | - | ||
| 23 | -void TiledExpandExpDifOperation(Function& function, const TileShape& tileShape, size_t cur, LogicalInput& input1, | ||
| 24 | - LogicalInput& input2, const LogicalTensorPtr& result, TileInfo& resultTileInfo) | ||
| 25 | -{ | ||
| 26 | - if (cur == input1.tensor->GetShape().size()) { | ||
| 27 | - auto inputTile1 = input1.tensor->View(function, input1.tileInfo.shape, input1.tileInfo.offset); | ||
| 28 | - auto inputTile2 = input2.tensor->View(function, input2.tileInfo.shape, input2.tileInfo.offset); | ||
| 29 | - auto resultTile = result->View(function, resultTileInfo.shape, resultTileInfo.offset); | ||
| 30 | - auto opName = GetBinaryOpNameCode<BinaryOpType::EXPANDEXPDIF>(); | ||
| 31 | - auto& op = function.AddOperation(opName, {inputTile1, inputTile2}, {resultTile}); | ||
| 32 | - size_t shapeSize = input1.tensor->GetShape().size(); | ||
| 33 | - std::vector<int64_t> brcOperand(shapeSize, 0); | ||
| 34 | - size_t brcAxesCount = 0; | ||
| 35 | - for (size_t i = 0; i < shapeSize; i++) { | ||
| 36 | - brcOperand[i] = BrcAxisBinaryOp(input1.tensor, input2.tensor, i); | ||
| 37 | - if (brcOperand[i] != 0) { | ||
| 38 | - brcAxesCount++; | ||
| 39 | - } | ||
| 40 | - } | ||
| 41 | - if (brcAxesCount > 0) { | ||
| 42 | - op.SetAttribute(OpAttributeKey::brcOperand, brcOperand); | ||
| 43 | - } | ||
| 44 | - } else { | ||
| 45 | - auto& vecTile = tileShape.GetVecTile(); | ||
| 46 | - for (int i = 0; i < result->shape[cur]; i += vecTile[cur]) { | ||
| 47 | - resultTileInfo.offset[cur] = i; | ||
| 48 | - resultTileInfo.shape[cur] = std::min(result->shape[cur] - resultTileInfo.offset[cur], vecTile[cur]); | ||
| 49 | - input1.tileInfo.offset[cur] = i % input1.tensor->GetShape()[cur]; | ||
| 50 | - input1.tileInfo.shape[cur] = std::min(input1.tensor->GetShape()[cur] - input1.tileInfo.offset[cur], | ||
| 51 | - vecTile[cur]); | ||
| 52 | - input2.tileInfo.offset[cur] = i % input2.tensor->GetShape()[cur]; | ||
| 53 | - input2.tileInfo.shape[cur] = std::min(input2.tensor->GetShape()[cur] - input2.tileInfo.offset[cur], | ||
| 54 | - vecTile[cur]); | ||
| 55 | - TiledExpandExpDifOperation(function, tileShape, cur + 1, input1, input2, result, resultTileInfo); | ||
| 56 | - } | ||
| 57 | - } | ||
| 58 | -} | ||
| 59 | - | ||
| 60 | -void TiledExpandExpDifOperation(Function& function, const TileShape& tileShape, LogicalTensorPtr operand1, | ||
| 61 | - LogicalTensorPtr operand2, const LogicalTensorPtr& result) | ||
| 62 | -{ | ||
| 63 | - CheckBinOpOperandsValid(operand1, operand2); | ||
| 64 | - | ||
| 65 | - TileInfo tileInfo1(result->shape.size(), result->offset.size()); | ||
| 66 | - TileInfo tileInfo2(result->shape.size(), result->offset.size()); | ||
| 67 | - TileInfo resultTileInfo(result->shape.size(), result->offset.size()); | ||
| 68 | - auto input1 = LogicalInput{operand1, tileInfo1}; | ||
| 69 | - auto input2 = LogicalInput{operand2, tileInfo2}; | ||
| 70 | - TiledExpandExpDifOperation(function, tileShape, 0, input1, input2, result, resultTileInfo); | ||
| 71 | -} | ||
| 72 | - | ||
| 73 | -Tensor ExpandExpDif(const Tensor& input, const Tensor& other) | ||
| 74 | -{ | ||
| 75 | - DECLARE_TRACER(); | ||
| 76 | - CheckTensorFormat(input.GetStorage(), {TileOpFormat::TILEOP_NZ}, "ExpandExpDif"); | ||
| 77 | - CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "ExpandExpDif"); | ||
| 78 | - | ||
| 79 | - CheckTensorsDataTypeConsistency(input.GetStorage(), other.GetStorage(), "EXPANDEXPDIF"); | ||
| 80 | - std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_FP32}; | ||
| 81 | - CheckTensorDataType(input.GetStorage(), supportedTypes, "EXPANDEXPDIF"); | ||
| 82 | - CheckBinaryInputTensors(input.GetStorage(), other.GetStorage(), "EXPANDEXPDIF"); | ||
| 83 | - config::SetOperationOption(KEY_COMBINE_AXIS, true); | ||
| 84 | - RETURN_CALL(BinaryOperation<BinaryOpType::EXPANDEXPDIF>, *Program::GetInstance().GetCurrentFunction(), input, | ||
| 85 | - other); | ||
| 86 | -} | ||
| 87 | - | ||
| 88 | -void ExpandExpDifOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 89 | - const std::vector<LogicalTensorPtr>& iOperand, | ||
| 90 | - const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 91 | -{ | ||
| 92 | - BinaryOperationOperandCheck(iOperand, oOperand); | ||
| 93 | - TiledExpandExpDifOperation(function, tileShape, iOperand[0], iOperand[1], oOperand[0]); | ||
| 94 | -} | ||
| 95 | - | ||
| 96 | -REGISTER_OPERATION_TILED_FUNC(OP_EXPANDEXPDIF, Opcode::OP_EXPANDEXPDIF, ExpandExpDifOperationTileFunc); | ||
| 97 | - | ||
| 98 | -} // namespace npu::tile_fwk | ||
| @@ -15,6 +15,7 @@ | |||
| 15 | 15 | ||
| 16 | #include "unary_tiled.h" | 16 | #include "unary_tiled.h" |
| 17 | #include "binary.h" | 17 | #include "binary.h" |
| 18 | +#include "binary_tiled.h" | ||
| 18 | #include "tensor_transformation.h" | 19 | #include "tensor_transformation.h" |
| 19 | #include "interface/utils/operator_tracer.h" | 20 | #include "interface/utils/operator_tracer.h" |
| 20 | #include "passes/pass_utils/graph_utils.h" | 21 | #include "passes/pass_utils/graph_utils.h" |
| @@ -210,6 +211,155 @@ void Expm1OperationTileFunc(Function& function, const TileShape& tileShape, | |||
| 210 | TiledExpm1(function, tileShape, iOperand[0], oOperand[0]); | 211 | TiledExpm1(function, tileShape, iOperand[0], oOperand[0]); |
| 211 | } | 212 | } |
| 212 | 213 | ||
| 214 | +void TiledExpandExpDifOperation(Function& function, const TileShape& tileShape, size_t cur, LogicalInput& input1, | ||
| 215 | + LogicalInput& input2, const LogicalTensorPtr& result, TileInfo& resultTileInfo) | ||
| 216 | +{ | ||
| 217 | + if (cur == input1.tensor->GetShape().size()) { | ||
| 218 | + auto inputTile1 = input1.tensor->View(function, input1.tileInfo.shape, input1.tileInfo.offset); | ||
| 219 | + auto inputTile2 = input2.tensor->View(function, input2.tileInfo.shape, input2.tileInfo.offset); | ||
| 220 | + auto resultTile = result->View(function, resultTileInfo.shape, resultTileInfo.offset); | ||
| 221 | + auto opName = GetBinaryOpNameCode<BinaryOpType::EXPANDEXPDIF>(); | ||
| 222 | + auto& op = function.AddOperation(opName, {inputTile1, inputTile2}, {resultTile}); | ||
| 223 | + size_t shapeSize = input1.tensor->GetShape().size(); | ||
| 224 | + std::vector<int64_t> brcOperand(shapeSize, 0); | ||
| 225 | + size_t brcAxesCount = 0; | ||
| 226 | + for (size_t i = 0; i < shapeSize; i++) { | ||
| 227 | + brcOperand[i] = BrcAxisBinaryOp(input1.tensor, input2.tensor, i); | ||
| 228 | + if (brcOperand[i] != 0) { | ||
| 229 | + brcAxesCount++; | ||
| 230 | + } | ||
| 231 | + } | ||
| 232 | + if (brcAxesCount > 0) { | ||
| 233 | + op.SetAttribute(OpAttributeKey::brcOperand, brcOperand); | ||
| 234 | + } | ||
| 235 | + } else { | ||
| 236 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 237 | + for (int i = 0; i < result->shape[cur]; i += vecTile[cur]) { | ||
| 238 | + resultTileInfo.offset[cur] = i; | ||
| 239 | + resultTileInfo.shape[cur] = std::min(result->shape[cur] - resultTileInfo.offset[cur], vecTile[cur]); | ||
| 240 | + input1.tileInfo.offset[cur] = i % input1.tensor->GetShape()[cur]; | ||
| 241 | + input1.tileInfo.shape[cur] = std::min(input1.tensor->GetShape()[cur] - input1.tileInfo.offset[cur], | ||
| 242 | + vecTile[cur]); | ||
| 243 | + input2.tileInfo.offset[cur] = i % input2.tensor->GetShape()[cur]; | ||
| 244 | + input2.tileInfo.shape[cur] = std::min(input2.tensor->GetShape()[cur] - input2.tileInfo.offset[cur], | ||
| 245 | + vecTile[cur]); | ||
| 246 | + TiledExpandExpDifOperation(function, tileShape, cur + 1, input1, input2, result, resultTileInfo); | ||
| 247 | + } | ||
| 248 | + } | ||
| 249 | +} | ||
| 250 | + | ||
| 251 | +void TiledExpandExpDifOperation(Function& function, const TileShape& tileShape, LogicalTensorPtr operand1, | ||
| 252 | + LogicalTensorPtr operand2, const LogicalTensorPtr& result) | ||
| 253 | +{ | ||
| 254 | + CheckBinOpOperandsValid(operand1, operand2); | ||
| 255 | + | ||
| 256 | + TileInfo tileInfo1(result->shape.size(), result->offset.size()); | ||
| 257 | + TileInfo tileInfo2(result->shape.size(), result->offset.size()); | ||
| 258 | + TileInfo resultTileInfo(result->shape.size(), result->offset.size()); | ||
| 259 | + auto input1 = LogicalInput{operand1, tileInfo1}; | ||
| 260 | + auto input2 = LogicalInput{operand2, tileInfo2}; | ||
| 261 | + TiledExpandExpDifOperation(function, tileShape, 0, input1, input2, result, resultTileInfo); | ||
| 262 | +} | ||
| 263 | + | ||
| 264 | +Tensor ExpandExpDif(const Tensor& input, const Tensor& other) | ||
| 265 | +{ | ||
| 266 | + DECLARE_TRACER(); | ||
| 267 | + CheckTensorFormat(input.GetStorage(), {TileOpFormat::TILEOP_NZ}, "ExpandExpDif"); | ||
| 268 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "ExpandExpDif"); | ||
| 269 | + | ||
| 270 | + CheckTensorsDataTypeConsistency(input.GetStorage(), other.GetStorage(), "EXPANDEXPDIF"); | ||
| 271 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_FP32}; | ||
| 272 | + CheckTensorDataType(input.GetStorage(), supportedTypes, "EXPANDEXPDIF"); | ||
| 273 | + CheckBinaryInputTensors(input.GetStorage(), other.GetStorage(), "EXPANDEXPDIF"); | ||
| 274 | + config::SetOperationOption(KEY_COMBINE_AXIS, true); | ||
| 275 | + RETURN_CALL(BinaryOperation<BinaryOpType::EXPANDEXPDIF>, *Program::GetInstance().GetCurrentFunction(), input, | ||
| 276 | + other); | ||
| 277 | +} | ||
| 278 | + | ||
| 279 | +void ExpandExpDifOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 280 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 281 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 282 | +{ | ||
| 283 | + BinaryOperationOperandCheck(iOperand, oOperand); | ||
| 284 | + TiledExpandExpDifOperation(function, tileShape, iOperand[0], iOperand[1], oOperand[0]); | ||
| 285 | +} | ||
| 286 | + | ||
| 287 | +DataType GetPowRealResultDataType(DataType selfType, DataType otherType) | ||
| 288 | +{ | ||
| 289 | + if (selfType == DT_INT32) { | ||
| 290 | + return otherType; | ||
| 291 | + } | ||
| 292 | + if (otherType == DT_INT32) { | ||
| 293 | + return selfType; | ||
| 294 | + } | ||
| 295 | + if (selfType == DT_BF16) { | ||
| 296 | + return otherType == DT_FP16 ? DT_FP32 : otherType; | ||
| 297 | + } | ||
| 298 | + if (otherType == DT_BF16) { | ||
| 299 | + return selfType == DT_FP16 ? DT_FP32 : selfType; | ||
| 300 | + } | ||
| 301 | + return selfType == DT_FP16 && otherType == DT_FP16 ? DT_FP16 : DT_FP32; | ||
| 302 | +} | ||
| 303 | + | ||
| 304 | +DataType GetPowCalcResultDataType(DataType selfType, DataType otherType) | ||
| 305 | +{ | ||
| 306 | + if (selfType == DT_INT32 && otherType == DT_INT32) { | ||
| 307 | + return DT_INT32; | ||
| 308 | + } | ||
| 309 | + return DT_FP32; | ||
| 310 | +} | ||
| 311 | + | ||
| 312 | +LogicalTensorPtr CastToResultType(const LogicalTensorPtr& tensor, DataType originType, DataType resultType) | ||
| 313 | +{ | ||
| 314 | + if (originType == resultType) { | ||
| 315 | + return tensor; | ||
| 316 | + } | ||
| 317 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), tensor, resultType, | ||
| 318 | + CastMode::CAST_NONE); | ||
| 319 | +} | ||
| 320 | + | ||
| 321 | +void PowCheck(const Tensor& self, const Tensor& other) | ||
| 322 | +{ | ||
| 323 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_INT32, DT_FP32, DT_INT8, DT_UINT8, DT_INT16}; | ||
| 324 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "POW"); | ||
| 325 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "POW"); | ||
| 326 | + CheckTensorShapeSize(self.GetStorage(), "POW"); | ||
| 327 | + CheckTensorShapeSize(other.GetStorage(), "POW"); | ||
| 328 | + CheckTensorsDimConsistency({self.GetStorage(), other.GetStorage()}, "POW"); | ||
| 329 | + CheckTensorsShapeConsistencyOrBroadcast({self.GetStorage(), other.GetStorage()}, "POW"); | ||
| 330 | + CheckTensorsFormatConsistency(self.GetStorage(), other.GetStorage(), "POW"); | ||
| 331 | +} | ||
| 332 | + | ||
| 333 | +Tensor Pow(const Tensor& self, const Tensor& other, PrecisionType precisionType) | ||
| 334 | +{ | ||
| 335 | + DECLARE_TRACER(); | ||
| 336 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Pow"); | ||
| 337 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Pow"); | ||
| 338 | + | ||
| 339 | + PowCheck(self, other); | ||
| 340 | + DataType selfType = self.GetDataType(); | ||
| 341 | + DataType otherType = other.GetDataType(); | ||
| 342 | + if (IsInteger(selfType) && IsInteger(otherType)) { | ||
| 343 | + CheckTensorsDataTypeConsistency(self.GetStorage(), other.GetStorage(), "Pow"); | ||
| 344 | + auto [result, op] = TensorBinaryOperationWithOp<BinaryOpType::POW>(*Program::GetInstance().GetCurrentFunction(), | ||
| 345 | + self.GetStorage(), other.GetStorage()); | ||
| 346 | + op->SetAttribute(OpAttributeKey::precisionType, static_cast<int64_t>(PrecisionType::INTRINSIC)); | ||
| 347 | + return result; | ||
| 348 | + } | ||
| 349 | + DataType realResultType = GetPowRealResultDataType(selfType, otherType); | ||
| 350 | + DataType calcResultType = GetPowCalcResultDataType(selfType, otherType); | ||
| 351 | + auto selfSt = CastToResultType(self.GetStorage(), selfType, calcResultType); | ||
| 352 | + auto otherSt = CastToResultType(other.GetStorage(), otherType, calcResultType); | ||
| 353 | + auto [result, op] = TensorBinaryOperationWithOp<BinaryOpType::POW>(*Program::GetInstance().GetCurrentFunction(), | ||
| 354 | + selfSt, otherSt); | ||
| 355 | + op->SetAttribute(OpAttributeKey::precisionType, static_cast<int64_t>(precisionType)); | ||
| 356 | + if (realResultType != calcResultType) { | ||
| 357 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), result, | ||
| 358 | + realResultType, CastMode::CAST_NONE); | ||
| 359 | + } | ||
| 360 | + return result; | ||
| 361 | +} | ||
| 362 | + | ||
| 213 | LogicalTensorPtr IntegerPow(const Tensor& self, int32_t intExponent) | 363 | LogicalTensorPtr IntegerPow(const Tensor& self, int32_t intExponent) |
| 214 | { | 364 | { |
| 215 | // 快速幂 | 365 | // 快速幂 |
| @@ -301,5 +451,7 @@ Tensor Pow(const Tensor& self, const Element& other) | |||
| 301 | REGISTER_OPERATION_TILED_FUNC(OP_EXP, Opcode::OP_EXP, ExpOperationTileFunc); | 451 | REGISTER_OPERATION_TILED_FUNC(OP_EXP, Opcode::OP_EXP, ExpOperationTileFunc); |
| 302 | REGISTER_OPERATION_TILED_FUNC(OP_EXP2, Opcode::OP_EXP2, Exp2OperationTileFunc); | 452 | REGISTER_OPERATION_TILED_FUNC(OP_EXP2, Opcode::OP_EXP2, Exp2OperationTileFunc); |
| 303 | REGISTER_OPERATION_TILED_FUNC(OP_EXPM1, Opcode::OP_EXPM1, Expm1OperationTileFunc); | 453 | REGISTER_OPERATION_TILED_FUNC(OP_EXPM1, Opcode::OP_EXPM1, Expm1OperationTileFunc); |
| 454 | +REGISTER_OPERATION_TILED_FUNC(OP_EXPANDEXPDIF, Opcode::OP_EXPANDEXPDIF, ExpandExpDifOperationTileFunc); | ||
| 455 | +REGISTER_OPERATION_TILED_FUNC(OP_POW, Opcode::OP_POW, BinaryOperationTileFunc<BinaryOpType::POW>); | ||
| 304 | 456 | ||
| 305 | } // namespace npu::tile_fwk | 457 | } // namespace npu::tile_fwk |
| @@ -14,8 +14,6 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "unary.h" | 16 | #include "unary.h" |
| 17 | -#include <sstream> | ||
| 18 | -#include <string> | ||
| 19 | #include "tensor_transformation.h" | 17 | #include "tensor_transformation.h" |
| 20 | #include "interface/utils/operator_tracer.h" | 18 | #include "interface/utils/operator_tracer.h" |
| 21 | #include "tilefwk/error_code.h" | 19 | #include "tilefwk/error_code.h" |
| @@ -13,9 +13,6 @@ | |||
| 13 | * \\brief | 13 | * \\brief |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | -#include <climits> | ||
| 17 | -#include <limits> | ||
| 18 | -#include <cmath> | ||
| 19 | #include "interface/utils/operator_tracer.h" | 16 | #include "interface/utils/operator_tracer.h" |
| 20 | #include "passes/pass_utils/graph_utils.h" | 17 | #include "passes/pass_utils/graph_utils.h" |
| 21 | #include "interface/function/function.h" | 18 | #include "interface/function/function.h" |
| @@ -13,9 +13,6 @@ | |||
| 13 | * \\brief | 13 | * \\brief |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | -#include <climits> | ||
| 17 | -#include <limits> | ||
| 18 | -#include <cmath> | ||
| 19 | #include "interface/utils/operator_tracer.h" | 16 | #include "interface/utils/operator_tracer.h" |
| 20 | #include "passes/pass_utils/graph_utils.h" | 17 | #include "passes/pass_utils/graph_utils.h" |
| 21 | #include "interface/function/function.h" | 18 | #include "interface/function/function.h" |
| @@ -14,7 +14,6 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "unary_tiled.h" | 16 | #include "unary_tiled.h" |
| 17 | -#include "unary_utils.h" | ||
| 18 | #include "binary.h" | 17 | #include "binary.h" |
| 19 | #include "tensor_transformation.h" | 18 | #include "tensor_transformation.h" |
| 20 | #include "interface/utils/operator_tracer.h" | 19 | #include "interface/utils/operator_tracer.h" |
| @@ -22,6 +21,18 @@ | |||
| 22 | #include "tilefwk/error_code.h" | 21 | #include "tilefwk/error_code.h" |
| 23 | 22 | ||
| 24 | namespace npu::tile_fwk { | 23 | namespace npu::tile_fwk { |
| 24 | +namespace { | ||
| 25 | + | ||
| 26 | +int64_t CmpResAlign(const std::vector<int64_t>& vec) | ||
| 27 | +{ | ||
| 28 | + constexpr size_t ALIGN_SIZE = NUM_VALUE_32; | ||
| 29 | + constexpr size_t ALIGN_BIT = NUM_VALUE_8; | ||
| 30 | + int64_t axis2 = (vec[vec.size() - 1] + ALIGN_BIT - 1) / ALIGN_BIT * ALIGN_BIT; | ||
| 31 | + axis2 = (axis2 + ALIGN_SIZE - 1) / ALIGN_SIZE * ALIGN_SIZE; | ||
| 32 | + return axis2 * vec[vec.size() - NUM_VALUE_2]; | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +} // namespace | ||
| 25 | 36 | ||
| 26 | Tensor Sinh(const Tensor& self) | 37 | Tensor Sinh(const Tensor& self) |
| 27 | { | 38 | { |
| @@ -20,9 +20,6 @@ | |||
| 20 | 20 | ||
| 21 | namespace npu::tile_fwk { | 21 | namespace npu::tile_fwk { |
| 22 | 22 | ||
| 23 | -/** | ||
| 24 | - * @brief Hypot | ||
| 25 | - */ | ||
| 26 | void TiledHypotOperationImpl(Function& function, const TileShape& tileShape, size_t cur, Input& input1, Input& input2, | 23 | void TiledHypotOperationImpl(Function& function, const TileShape& tileShape, size_t cur, Input& input1, Input& input2, |
| 27 | const LogicalTensorPtr& result, TileInfo& resultTileInfo) | 24 | const LogicalTensorPtr& result, TileInfo& resultTileInfo) |
| 28 | { | 25 | { |
| @@ -13,7 +13,6 @@ | |||
| 13 | * \\brief | 13 | * \\brief |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | -#include <climits> | ||
| 17 | #include <limits> | 16 | #include <limits> |
| 18 | #include <cmath> | 17 | #include <cmath> |
| 19 | #include "interface/utils/operator_tracer.h" | 18 | #include "interface/utils/operator_tracer.h" |
| @@ -13,9 +13,6 @@ | |||
| 13 | * \\brief | 13 | * \\brief |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | -#include <climits> | ||
| 17 | -#include <limits> | ||
| 18 | -#include <cmath> | ||
| 19 | #include "interface/utils/operator_tracer.h" | 16 | #include "interface/utils/operator_tracer.h" |
| 20 | #include "passes/pass_utils/graph_utils.h" | 17 | #include "passes/pass_utils/graph_utils.h" |
| 21 | #include "interface/function/function.h" | 18 | #include "interface/function/function.h" |
| @@ -19,15 +19,6 @@ | |||
| 19 | 19 | ||
| 20 | namespace npu::tile_fwk { | 20 | namespace npu::tile_fwk { |
| 21 | 21 | ||
| 22 | -Tensor Duplicate(const Tensor& operand) | ||
| 23 | -{ | ||
| 24 | - DECLARE_TRACER(); | ||
| 25 | - CheckTensorFormat(operand.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Duplicate"); | ||
| 26 | - | ||
| 27 | - RETURN_CALL(UnaryOperation<UnaryOpType::DUPLICATE>, *Program::GetInstance().GetCurrentFunction(), | ||
| 28 | - operand.GetStorage()); | ||
| 29 | -} | ||
| 30 | - | ||
| 31 | Tensor Pack(const Tensor& self) | 22 | Tensor Pack(const Tensor& self) |
| 32 | { | 23 | { |
| 33 | DECLARE_TRACER(); | 24 | DECLARE_TRACER(); |
| @@ -13,7 +13,6 @@ | |||
| 13 | * \brief | 13 | * \brief |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | -#include <cmath> | ||
| 17 | #include "tensor_transformation.h" | 16 | #include "tensor_transformation.h" |
| 18 | #include "interface/operation/operation_common.h" | 17 | #include "interface/operation/operation_common.h" |
| 19 | #include "interface/utils/operator_tracer.h" | 18 | #include "interface/utils/operator_tracer.h" |
| @@ -19,9 +19,9 @@ | |||
| 19 | #include "interface/program/program.h" | 19 | #include "interface/program/program.h" |
| 20 | #include "interface/configs/config_manager.h" | 20 | #include "interface/configs/config_manager.h" |
| 21 | #include "tensor_transformation.h" | 21 | #include "tensor_transformation.h" |
| 22 | -#include "permute.h" | ||
| 23 | #include <algorithm> | 22 | #include <algorithm> |
| 24 | -#include <sstream> | 23 | +#include <unordered_set> |
| 24 | +#include <vector> | ||
| 25 | 25 | ||
| 26 | namespace npu::tile_fwk { | 26 | namespace npu::tile_fwk { |
| 27 | 27 | ||
| @@ -1,36 +0,0 @@ | |||
| 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 permute.h | ||
| 13 | - * \brief Permute operation header file | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#pragma once | ||
| 17 | -#include <string> | ||
| 18 | -#include <vector> | ||
| 19 | -#include "interface/utils/common.h" | ||
| 20 | -#include "interface/operation/opcode.h" | ||
| 21 | -#include "interface/operation/operation_common.h" | ||
| 22 | -#include "interface/function/function.h" | ||
| 23 | -#include "interface/program/program.h" | ||
| 24 | - | ||
| 25 | -namespace npu::tile_fwk { | ||
| 26 | - | ||
| 27 | -void PermuteOperationOperandCheck(const std::vector<LogicalTensorPtr>& iOperand, | ||
| 28 | - const std::vector<LogicalTensorPtr>& oOperand); | ||
| 29 | -std::vector<int64_t> PermuteResultShape(const std::vector<int64_t>& inputShape, const std::vector<int>& perm); | ||
| 30 | -bool IsIdentityPermutation(const std::vector<int>& perm); | ||
| 31 | -void NormalizePermutation(std::vector<int>& perm, int shapeSize); | ||
| 32 | -void ValidatePermutation(const std::vector<int>& perm, int shapeSize); | ||
| 33 | -Tensor TensorPermuteOperation(Function& function, LogicalTensorPtr self, const std::vector<int>& perm); | ||
| 34 | -Tensor TensorElementPermuteOperation(Function& function, LogicalTensorPtr self, const std::vector<int>& perm); | ||
| 35 | - | ||
| 36 | -} // namespace npu::tile_fwk | ||
| @@ -1,82 +0,0 @@ | |||
| 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 predicate.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 IsFinite(const Tensor& self) | ||
| 26 | -{ | ||
| 27 | - DECLARE_TRACER(); | ||
| 28 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "IsFinite"); | ||
| 29 | - | ||
| 30 | - std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_FP32, DT_BF16, DT_INT16, DT_INT4, DT_INT8, | ||
| 31 | - DT_INT32, DT_UINT16, DT_UINT32, DT_UINT8, DT_UINT64, DT_INT64}; | ||
| 32 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "IsFinite"); | ||
| 33 | - RETURN_CALL(UnaryOperation<UnaryOpType::ISFINITE>, *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), | ||
| 34 | - DT_BOOL); | ||
| 35 | -} | ||
| 36 | - | ||
| 37 | -Tensor IsNan(const Tensor& self) | ||
| 38 | -{ | ||
| 39 | - DECLARE_TRACER(); | ||
| 40 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "IsNan"); | ||
| 41 | - | ||
| 42 | - std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_FP32, DT_BF16}; | ||
| 43 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "ISNAN"); | ||
| 44 | - | ||
| 45 | - RETURN_CALL(UnaryOperation<UnaryOpType::ISNAN>, *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), | ||
| 46 | - DT_BOOL); | ||
| 47 | -} | ||
| 48 | - | ||
| 49 | -void IsFiniteOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 50 | - const std::vector<LogicalTensorPtr>& iOperand, | ||
| 51 | - const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 52 | -{ | ||
| 53 | - UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 54 | - auto shape = tileShape.GetVecTile().tile; | ||
| 55 | - // tileShape 对应的中间变量结果,类型为 FP16 | ||
| 56 | - uint32_t intermediateBytes = static_cast<int64_t>(BytesOf(DT_FP16)) * | ||
| 57 | - std::accumulate(shape.begin(), shape.end(), 1LL, std::multiplies<int64_t>()); | ||
| 58 | - uint32_t workspaceSize = intermediateBytes; | ||
| 59 | - return TiledUnaryOperation<UnaryOpType::ISFINITE>(function, tileShape, iOperand[0], oOperand[0], workspaceSize); | ||
| 60 | -} | ||
| 61 | - | ||
| 62 | -void IsNanOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 63 | - const std::vector<LogicalTensorPtr>& iOperand, | ||
| 64 | - const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 65 | -{ | ||
| 66 | - UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 67 | - auto tmpShape = tileShape.GetVecTile().tile; | ||
| 68 | - int dim = static_cast<int>(tmpShape.size()); | ||
| 69 | - auto alignSize = BLOCK_SIZE / BytesOf(DT_FP16); | ||
| 70 | - int64_t tmpW = AlignUp(tmpShape[dim - 1], alignSize); | ||
| 71 | - int64_t tmpH = (dim >= NUM_VALUE_2) ? tmpShape[dim - NUM_VALUE_2] : 1; | ||
| 72 | - | ||
| 73 | - constexpr int64_t kNumBlocks = NUM_VALUE_3; | ||
| 74 | - int64_t blockBytes = tmpH * tmpW * BytesOf(DT_FP32); | ||
| 75 | - uint32_t workspaceSize = kNumBlocks * blockBytes + BLOCK_SIZE; | ||
| 76 | - return TiledUnaryOperation<UnaryOpType::ISNAN>(function, tileShape, iOperand[0], oOperand[0], workspaceSize); | ||
| 77 | -} | ||
| 78 | - | ||
| 79 | -REGISTER_OPERATION_TILED_FUNC(OP_ISFINITE, Opcode::OP_ISFINITE, IsFiniteOperationTileFunc); | ||
| 80 | -REGISTER_OPERATION_TILED_FUNC(OP_ISNAN, Opcode::OP_ISNAN, IsNanOperationTileFunc); | ||
| 81 | - | ||
| 82 | -} // namespace npu::tile_fwk | ||
| @@ -14,7 +14,6 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include <climits> | 16 | #include <climits> |
| 17 | -#include <limits> | ||
| 18 | #include <cmath> | 17 | #include <cmath> |
| 19 | #include "interface/utils/operator_tracer.h" | 18 | #include "interface/utils/operator_tracer.h" |
| 20 | #include "passes/pass_utils/graph_utils.h" | 19 | #include "passes/pass_utils/graph_utils.h" |
| @@ -13,6 +13,8 @@ | |||
| 13 | * \brief | 13 | * \brief |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | +#include <algorithm> | ||
| 17 | +#include <set> | ||
| 16 | #include "unary.h" | 18 | #include "unary.h" |
| 17 | #include "interface/configs/config_manager.h" | 19 | #include "interface/configs/config_manager.h" |
| 18 | #include "interface/utils/operator_tracer.h" | 20 | #include "interface/utils/operator_tracer.h" |
| @@ -570,6 +572,113 @@ Tensor Prod(const Tensor& self, int axis, bool keepDim) | |||
| 570 | return ProcessResultShape(result, self, axis, keepDim); | 572 | return ProcessResultShape(result, self, axis, keepDim); |
| 571 | } | 573 | } |
| 572 | 574 | ||
| 575 | +static void VarParamVaildCheck(const Tensor& input, std::vector<int>& dim) | ||
| 576 | +{ | ||
| 577 | + std::unordered_set<DataType> supportedTypes = {DT_FP32, DT_FP16, DT_BF16}; | ||
| 578 | + CheckTensorDataType(input.GetStorage(), supportedTypes, "VAR"); | ||
| 579 | + CheckTensorDimRange(input.GetStorage(), 1, NUM_VALUE_4, "VAR"); | ||
| 580 | + CheckTensorShapeSize(input.GetStorage(), "VAR"); | ||
| 581 | + | ||
| 582 | + Shape shape = input.GetShape(); | ||
| 583 | + uint64_t shapeSize = shape.size(); | ||
| 584 | + | ||
| 585 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, dim.size() <= shapeSize) << "The dim.size() should <= input.size()"; | ||
| 586 | + for (uint64_t i = 0; i < shapeSize; i++) { | ||
| 587 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, shape[i] > 0) << "The input shape should > 0"; | ||
| 588 | + } | ||
| 589 | + | ||
| 590 | + if (dim.empty()) { | ||
| 591 | + for (uint64_t i = 0; i < shapeSize; i++) { | ||
| 592 | + dim.push_back(static_cast<int>(i)); | ||
| 593 | + } | ||
| 594 | + } | ||
| 595 | + std::set<int> dupDimSet(dim.begin(), dim.end()); | ||
| 596 | + | ||
| 597 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, dupDimSet.size() == dim.size()) << "There are duplicate elements in dim"; | ||
| 598 | + for (size_t i = 0; i < dim.size(); i++) { | ||
| 599 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, | ||
| 600 | + dim[i] < static_cast<int>(shapeSize) && dim[i] >= -(static_cast<int>(shapeSize))) | ||
| 601 | + << "The value in dim is out of range"; | ||
| 602 | + if (dim[i] < 0) { | ||
| 603 | + dim[i] = dim[i] + static_cast<int>(shapeSize); | ||
| 604 | + } | ||
| 605 | + } | ||
| 606 | + std::sort(dim.begin(), dim.end()); | ||
| 607 | +} | ||
| 608 | + | ||
| 609 | +static Tensor VarResSqueeze(const Tensor& res, const std::vector<int>& dim, const std::vector<int64_t>& oriVecTile, | ||
| 610 | + DataType dtype) | ||
| 611 | +{ | ||
| 612 | + std::vector<int64_t> vecTile(oriVecTile.begin(), oriVecTile.end()); | ||
| 613 | + for (auto it = dim.rbegin(); it != dim.rend(); ++it) { | ||
| 614 | + vecTile.erase(vecTile.begin() + *it); | ||
| 615 | + } | ||
| 616 | + int64_t algnedSize = BLOCK_SIZE / BytesOf(dtype); | ||
| 617 | + if (vecTile.empty()) { | ||
| 618 | + vecTile.push_back(algnedSize); | ||
| 619 | + } | ||
| 620 | + int64_t lastDimSize = vecTile.back(); | ||
| 621 | + if (lastDimSize % algnedSize != 0) { | ||
| 622 | + vecTile.back() = CeilDiv(lastDimSize, algnedSize) * algnedSize; | ||
| 623 | + } | ||
| 624 | + TileShape::Current().SetVecTile(vecTile); | ||
| 625 | + return Squeeze(res, dim); | ||
| 626 | +} | ||
| 627 | + | ||
| 628 | +Tensor Var(const Tensor& input, const std::vector<int>& dim, float correction, bool keepDim) | ||
| 629 | +{ | ||
| 630 | + CheckTensorFormat(input.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Var"); | ||
| 631 | + | ||
| 632 | + std::vector<int> innerDim(dim.begin(), dim.end()); | ||
| 633 | + VarParamVaildCheck(input, innerDim); | ||
| 634 | + | ||
| 635 | + DataType dtype = input.GetDataType(); | ||
| 636 | + Shape shape = input.GetShape(); | ||
| 637 | + auto castInput = Tensor(DT_FP32, shape); | ||
| 638 | + if (dtype == DT_FP16 || dtype == DT_BF16) { | ||
| 639 | + castInput = Cast(input, DT_FP32, CAST_NONE); | ||
| 640 | + } else { | ||
| 641 | + castInput = input; | ||
| 642 | + } | ||
| 643 | + | ||
| 644 | + int calcN = 1; | ||
| 645 | + auto res = castInput; | ||
| 646 | + for (size_t i = 0; i < innerDim.size(); i++) { | ||
| 647 | + calcN *= static_cast<int>(shape[innerDim[i]]); | ||
| 648 | + } | ||
| 649 | + res = Div(res, Element(DT_FP32, static_cast<float>(calcN))); | ||
| 650 | + for (size_t i = 0; i < innerDim.size(); i++) { | ||
| 651 | + res = Sum(res, innerDim[i], true); | ||
| 652 | + } | ||
| 653 | + | ||
| 654 | + Shape dstShape = res.GetShape(); | ||
| 655 | + for (size_t i = 0; i < innerDim.size(); i++) { | ||
| 656 | + dstShape[innerDim[i]] = shape[innerDim[i]]; | ||
| 657 | + res = Expand(res, dstShape); | ||
| 658 | + } | ||
| 659 | + | ||
| 660 | + res = Sub(castInput, res); | ||
| 661 | + res = Mul(res, res); | ||
| 662 | + float count = std::max(0.0f, static_cast<float>(calcN) - correction); | ||
| 663 | + res = Div(res, Element(DT_FP32, count)); | ||
| 664 | + for (size_t i = 0; i < innerDim.size(); i++) { | ||
| 665 | + res = Sum(res, innerDim[i], true); | ||
| 666 | + } | ||
| 667 | + auto oriVecTile = TileShape::Current().GetVecTile(); | ||
| 668 | + if (!keepDim) { | ||
| 669 | + res = VarResSqueeze(res, innerDim, oriVecTile.tile, dtype); | ||
| 670 | + } | ||
| 671 | + | ||
| 672 | + if (dtype == DT_FP16 || dtype == DT_BF16) { | ||
| 673 | + res = Cast(res, dtype, CAST_NONE); | ||
| 674 | + } | ||
| 675 | + if (!keepDim) { | ||
| 676 | + TileShape::Current().SetVecTile(oriVecTile.tile); | ||
| 677 | + } | ||
| 678 | + | ||
| 679 | + return res; | ||
| 680 | +} | ||
| 681 | + | ||
| 573 | void TiledReduceExpand(Function& function, const TileShape& tileShape, const std::string& op, | 682 | void TiledReduceExpand(Function& function, const TileShape& tileShape, const std::string& op, |
| 574 | const LogicalTensorPtr& operand, const LogicalTensorPtr& result) | 683 | const LogicalTensorPtr& operand, const LogicalTensorPtr& result) |
| 575 | { | 684 | { |
| @@ -1,220 +0,0 @@ | |||
| 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 rounding.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 Ceil(const Tensor& self) | ||
| 26 | -{ | ||
| 27 | - DECLARE_TRACER(); | ||
| 28 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Ceil"); | ||
| 29 | - | ||
| 30 | - std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_FP32}; | ||
| 31 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "Ceil"); | ||
| 32 | - | ||
| 33 | - if (self.GetDataType() == DataType::DT_INT16 || self.GetDataType() == DataType::DT_INT32) { | ||
| 34 | - RETURN_CALL(UnaryOperation<UnaryOpType::CEIL>, *Program::GetInstance().GetCurrentFunction(), self.GetStorage()); | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - auto castSelf = self.GetStorage(); | ||
| 38 | - if (self.GetDataType() != DataType::DT_FP32) { | ||
| 39 | - castSelf = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), | ||
| 40 | - self.GetStorage(), DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - auto ceilResult = CALL(UnaryOperation<UnaryOpType::CEIL>, *Program::GetInstance().GetCurrentFunction(), castSelf); | ||
| 44 | - if (self.GetDataType() != DataType::DT_FP32) { | ||
| 45 | - RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), ceilResult, | ||
| 46 | - self.GetDataType(), CastMode::CAST_NONE); | ||
| 47 | - } | ||
| 48 | - return ceilResult; | ||
| 49 | -} | ||
| 50 | - | ||
| 51 | -Tensor Floor(const Tensor& self) | ||
| 52 | -{ | ||
| 53 | - DECLARE_TRACER(); | ||
| 54 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Floor"); | ||
| 55 | - | ||
| 56 | - std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_FP32}; | ||
| 57 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "Floor"); | ||
| 58 | - | ||
| 59 | - if (self.GetDataType() == DataType::DT_INT16 || self.GetDataType() == DataType::DT_INT32) { | ||
| 60 | - RETURN_CALL(UnaryOperation<UnaryOpType::FLOOR>, *Program::GetInstance().GetCurrentFunction(), | ||
| 61 | - self.GetStorage()); | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - auto castSelf = self.GetStorage(); | ||
| 65 | - if (self.GetDataType() != DataType::DT_FP32) { | ||
| 66 | - castSelf = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), | ||
| 67 | - self.GetStorage(), DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 68 | - } | ||
| 69 | - | ||
| 70 | - auto floorResult = CALL(UnaryOperation<UnaryOpType::FLOOR>, *Program::GetInstance().GetCurrentFunction(), castSelf); | ||
| 71 | - if (self.GetDataType() != DataType::DT_FP32) { | ||
| 72 | - RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), floorResult, | ||
| 73 | - self.GetDataType(), CastMode::CAST_NONE); | ||
| 74 | - } | ||
| 75 | - return floorResult; | ||
| 76 | -} | ||
| 77 | - | ||
| 78 | -Tensor Trunc(const Tensor& self) | ||
| 79 | -{ | ||
| 80 | - DECLARE_TRACER(); | ||
| 81 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Trunc"); | ||
| 82 | - | ||
| 83 | - std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_FP32}; | ||
| 84 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "Trunc"); | ||
| 85 | - | ||
| 86 | - if (self.GetDataType() == DataType::DT_INT16 || self.GetDataType() == DataType::DT_INT32) { | ||
| 87 | - RETURN_CALL(UnaryOperation<UnaryOpType::TRUNC>, *Program::GetInstance().GetCurrentFunction(), | ||
| 88 | - self.GetStorage()); | ||
| 89 | - } | ||
| 90 | - | ||
| 91 | - auto castSelf = self.GetStorage(); | ||
| 92 | - if (self.GetDataType() != DataType::DT_FP32) { | ||
| 93 | - castSelf = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), | ||
| 94 | - self.GetStorage(), DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 95 | - } | ||
| 96 | - | ||
| 97 | - auto truncResult = CALL(UnaryOperation<UnaryOpType::TRUNC>, *Program::GetInstance().GetCurrentFunction(), castSelf); | ||
| 98 | - if (self.GetDataType() != DataType::DT_FP32) { | ||
| 99 | - RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), truncResult, | ||
| 100 | - self.GetDataType(), CastMode::CAST_NONE); | ||
| 101 | - } | ||
| 102 | - return truncResult; | ||
| 103 | -} | ||
| 104 | - | ||
| 105 | -void CeilOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 106 | - const std::vector<LogicalTensorPtr>& iOperand, const std::vector<LogicalTensorPtr>& oOperand, | ||
| 107 | - [[maybe_unused]] const Operation& op) | ||
| 108 | -{ | ||
| 109 | - UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 110 | - return TiledUnaryOperation<UnaryOpType::CEIL>(function, tileShape, iOperand[0], oOperand[0]); | ||
| 111 | -} | ||
| 112 | - | ||
| 113 | -void FloorOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 114 | - const std::vector<LogicalTensorPtr>& iOperand, | ||
| 115 | - const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 116 | -{ | ||
| 117 | - UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 118 | - return TiledUnaryOperation<UnaryOpType::FLOOR>(function, tileShape, iOperand[0], oOperand[0]); | ||
| 119 | -} | ||
| 120 | - | ||
| 121 | -void TruncOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 122 | - const std::vector<LogicalTensorPtr>& iOperand, | ||
| 123 | - const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 124 | -{ | ||
| 125 | - UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 126 | - return TiledUnaryOperation<UnaryOpType::TRUNC>(function, tileShape, iOperand[0], oOperand[0]); | ||
| 127 | -} | ||
| 128 | - | ||
| 129 | -Tensor TensorRound(Function& function, const LogicalTensorPtr& self, const int& decimals = 0) | ||
| 130 | -{ | ||
| 131 | - auto result = std::make_shared<LogicalTensor>(function, self->Datatype(), self->GetShape(), | ||
| 132 | - self->GetDynValidShape()); | ||
| 133 | - auto& op = function.AddOperation(Opcode::OP_ROUND, {self}, {result}); | ||
| 134 | - op.SetAttribute(OP_ATTR_PREFIX + "decimals", decimals); | ||
| 135 | - return result; | ||
| 136 | -} | ||
| 137 | - | ||
| 138 | -Tensor Round(const Tensor& self, const int& decimals) | ||
| 139 | -{ | ||
| 140 | - DECLARE_TRACER(); | ||
| 141 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Round"); | ||
| 142 | - | ||
| 143 | - static const std::unordered_set<DataType> ROUND_A2A3_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16}; | ||
| 144 | - static const std::unordered_set<DataType> ROUND_A5_TYPES = {DT_FP32, DT_FP16, DT_BF16, | ||
| 145 | - DT_INT32, DT_INT16, DT_INT64}; | ||
| 146 | - const auto& supportedTypes = GetSupportedDataTypesByArch(ROUND_A2A3_TYPES, ROUND_A5_TYPES); | ||
| 147 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "ROUND"); | ||
| 148 | - CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "ROUND"); | ||
| 149 | - CheckTensorShapeSize(self.GetStorage(), "ROUND"); | ||
| 150 | - | ||
| 151 | - RETURN_CALL(Round, *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), decimals); | ||
| 152 | -} | ||
| 153 | - | ||
| 154 | -void TiledRound(Function& function, const TileShape& tileShape, size_t cur, Input& input, | ||
| 155 | - const LogicalTensorPtr& result, const int& decimals = 0) | ||
| 156 | -{ | ||
| 157 | - if (cur == input.tensor.GetShape().size()) { | ||
| 158 | - auto tile = input.tensor.GetStorage()->View(function, input.tileInfo.shape, input.tileInfo.offset); | ||
| 159 | - auto resultTile = result->View(function, input.tileInfo.shape, input.tileInfo.offset); | ||
| 160 | - std::vector<int64_t> srcTileShape(input.tileInfo.shape); | ||
| 161 | - auto tileShapeLen = srcTileShape.size(); | ||
| 162 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, SHAPE_DIM1 <= tileShapeLen && tileShapeLen <= SHAPE_DIM4) | ||
| 163 | - << "Length of tile shape only supports 1~4"; | ||
| 164 | - std::vector<int64_t> tmpShape; | ||
| 165 | - if (result->Datatype() == DT_FP32) { | ||
| 166 | - tmpShape = {BLOCK_SIZE / sizeof(float)}; | ||
| 167 | - } else { | ||
| 168 | - if (srcTileShape.size() == 1) { | ||
| 169 | - tmpShape.assign(srcTileShape.end() - SHAPE_DIM1, srcTileShape.end()); | ||
| 170 | - } else { | ||
| 171 | - tmpShape.assign(srcTileShape.end() - SHAPE_DIM2, srcTileShape.end()); | ||
| 172 | - } | ||
| 173 | - auto alignSize = BLOCK_SIZE / BytesOf(DT_FP32); | ||
| 174 | - tmpShape[tmpShape.size() - 1] = (tmpShape[tmpShape.size() - 1] + alignSize - 1) / alignSize * alignSize; | ||
| 175 | - } | ||
| 176 | - auto tmpTensor = std::make_shared<LogicalTensor>(function, DT_FP32, tmpShape); | ||
| 177 | - auto& newOp = function.AddOperation(Opcode::OP_ROUND, {tile}, {resultTile, tmpTensor}); | ||
| 178 | - float powDecimals = std::pow(static_cast<float>(NUM_VALUE_10), static_cast<float>(decimals)); | ||
| 179 | - const int32_t maxFp32Len = 38; | ||
| 180 | - if (decimals > maxFp32Len) { | ||
| 181 | - powDecimals = INFINITY; | ||
| 182 | - } | ||
| 183 | - newOp.SetAttribute(OP_ATTR_PREFIX + "decimals", decimals); | ||
| 184 | - newOp.SetAttribute(OpAttributeKey::scalar, Element(DataType::DT_FP32, powDecimals)); | ||
| 185 | - return; | ||
| 186 | - } | ||
| 187 | - auto& vecTile = tileShape.GetVecTile(); | ||
| 188 | - for (int i = 0; i < input.tensor.GetShape()[cur]; i += vecTile[cur]) { | ||
| 189 | - input.tileInfo.shape[cur] = std::min(input.tensor.GetShape()[cur] - i, vecTile[cur]); | ||
| 190 | - input.tileInfo.offset[cur] = i; | ||
| 191 | - TiledRound(function, tileShape, cur + 1, input, result, decimals); | ||
| 192 | - } | ||
| 193 | -} | ||
| 194 | - | ||
| 195 | -void TiledRound(Function& function, const TileShape& tileShape, const LogicalTensorPtr& operand, | ||
| 196 | - const LogicalTensorPtr& result, const int& decimals = 0) | ||
| 197 | -{ | ||
| 198 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, operand->shape.size() == operand->offset.size()) | ||
| 199 | - << "The shape size of operand and offset must be equal"; | ||
| 200 | - | ||
| 201 | - TileInfo tileInfo(result->shape.size(), result->offset.size()); | ||
| 202 | - auto input = Input{operand, tileInfo}; | ||
| 203 | - | ||
| 204 | - TiledRound(function, tileShape, 0, input, result, decimals); | ||
| 205 | -} | ||
| 206 | - | ||
| 207 | -void RoundOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 208 | - const std::vector<LogicalTensorPtr>& iOperand, | ||
| 209 | - const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 210 | -{ | ||
| 211 | - int decimals = op.GetIntAttribute(OP_ATTR_PREFIX + "decimals"); | ||
| 212 | - TiledRound(function, tileShape, iOperand[0], oOperand[0], decimals); | ||
| 213 | -} | ||
| 214 | - | ||
| 215 | -REGISTER_OPERATION_TILED_FUNC(OP_CEIL, Opcode::OP_CEIL, CeilOperationTileFunc); | ||
| 216 | -REGISTER_OPERATION_TILED_FUNC(OP_FLOOR, Opcode::OP_FLOOR, FloorOperationTileFunc); | ||
| 217 | -REGISTER_OPERATION_TILED_FUNC(OP_TRUNC, Opcode::OP_TRUNC, TruncOperationTileFunc); | ||
| 218 | -REGISTER_OPERATION_TILED_FUNC(OP_ROUND, Opcode::OP_ROUND, RoundOperationTileFunc); | ||
| 219 | - | ||
| 220 | -} // namespace npu::tile_fwk | ||
| @@ -13,7 +13,6 @@ | |||
| 13 | * \brief Scalar operation implementations. | 13 | * \brief Scalar operation implementations. |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | -#include <limits> | ||
| 17 | #include "tilefwk/data_type.h" | 16 | #include "tilefwk/data_type.h" |
| 18 | #include "unary.h" | 17 | #include "unary.h" |
| 19 | #include "binary.h" | 18 | #include "binary.h" |
| @@ -13,9 +13,6 @@ | |||
| 13 | * \\brief | 13 | * \\brief |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | -#include <climits> | ||
| 17 | -#include <limits> | ||
| 18 | -#include <cmath> | ||
| 19 | #include "interface/utils/operator_tracer.h" | 16 | #include "interface/utils/operator_tracer.h" |
| 20 | #include "passes/pass_utils/graph_utils.h" | 17 | #include "passes/pass_utils/graph_utils.h" |
| 21 | #include "interface/function/function.h" | 18 | #include "interface/function/function.h" |
| @@ -13,9 +13,6 @@ | |||
| 13 | * \\brief | 13 | * \\brief |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | -#include <climits> | ||
| 17 | -#include <limits> | ||
| 18 | -#include <cmath> | ||
| 19 | #include "interface/utils/operator_tracer.h" | 16 | #include "interface/utils/operator_tracer.h" |
| 20 | #include "passes/pass_utils/graph_utils.h" | 17 | #include "passes/pass_utils/graph_utils.h" |
| 21 | #include "interface/function/function.h" | 18 | #include "interface/function/function.h" |
| @@ -14,8 +14,8 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "unary.h" | 16 | #include "unary.h" |
| 17 | -#include "unary_utils.h" | ||
| 18 | #include "binary.h" | 17 | #include "binary.h" |
| 18 | +#include "binary_tiled.h" | ||
| 19 | #include "tensor_transformation.h" | 19 | #include "tensor_transformation.h" |
| 20 | #include "interface/utils/operator_tracer.h" | 20 | #include "interface/utils/operator_tracer.h" |
| 21 | #include "passes/pass_utils/graph_utils.h" | 21 | #include "passes/pass_utils/graph_utils.h" |
| @@ -146,13 +146,30 @@ Tensor Signbit(const Tensor& self) | |||
| 146 | RETURN_CALL(SignbitOperation, *Program::GetInstance().GetCurrentFunction(), self.GetStorage()); | 146 | RETURN_CALL(SignbitOperation, *Program::GetInstance().GetCurrentFunction(), self.GetStorage()); |
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | -int64_t CmpResAlign(const std::vector<int64_t>& vec) | 149 | +Tensor CopySign(const Tensor& self, const Tensor& other) |
| 150 | { | 150 | { |
| 151 | - constexpr size_t ALIGN_SIZE = NUM_VALUE_32; | 151 | + DECLARE_TRACER(); |
| 152 | - constexpr size_t ALIGN_BIT = NUM_VALUE_8; | 152 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "CopySign"); |
| 153 | - int64_t axis2 = (vec[vec.size() - 1] + ALIGN_BIT - 1) / ALIGN_BIT * ALIGN_BIT; | 153 | + CheckTensorFormat(other.GetStorage(), {TileOpFormat::TILEOP_NZ}, "CopySign"); |
| 154 | - axis2 = (axis2 + ALIGN_SIZE - 1) / ALIGN_SIZE * ALIGN_SIZE; | 154 | + |
| 155 | - return axis2 * vec[vec.size() - NUM_VALUE_2]; | 155 | + CheckTensorsDataTypeConsistency(self.GetStorage(), other.GetStorage(), "COPYSIGN"); |
| 156 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_FP32}; | ||
| 157 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "COPYSIGN"); | ||
| 158 | + | ||
| 159 | + DataType selfDType = self.GetDataType(); | ||
| 160 | + DataType otherDType = other.GetDataType(); | ||
| 161 | + Tensor castSelf = self; | ||
| 162 | + Tensor castOther = other; | ||
| 163 | + if (selfDType == DT_INT16 || selfDType == DT_INT32) { | ||
| 164 | + castSelf = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), | ||
| 165 | + self.GetStorage(), DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 166 | + } | ||
| 167 | + if (otherDType == DT_INT16 || otherDType == DT_INT32) { | ||
| 168 | + castOther = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), | ||
| 169 | + other.GetStorage(), DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 170 | + } | ||
| 171 | + RETURN_CALL(BinaryOperation<BinaryOpType::COPYSIGN>, *Program::GetInstance().GetCurrentFunction(), castSelf, | ||
| 172 | + castOther); | ||
| 156 | } | 173 | } |
| 157 | 174 | ||
| 158 | void SignOperationTileFunc(Function& function, const TileShape& tileShape, | 175 | void SignOperationTileFunc(Function& function, const TileShape& tileShape, |
| @@ -171,5 +188,6 @@ void SignbitOperationTileFunc(Function& function, const TileShape& tileShape, | |||
| 171 | 188 | ||
| 172 | REGISTER_OPERATION_TILED_FUNC(OP_SIGN, Opcode::OP_SIGN, SignOperationTileFunc); | 189 | REGISTER_OPERATION_TILED_FUNC(OP_SIGN, Opcode::OP_SIGN, SignOperationTileFunc); |
| 173 | REGISTER_OPERATION_TILED_FUNC(OP_SIGNBIT, Opcode::OP_SIGNBIT, SignbitOperationTileFunc); | 190 | REGISTER_OPERATION_TILED_FUNC(OP_SIGNBIT, Opcode::OP_SIGNBIT, SignbitOperationTileFunc); |
| 191 | +REGISTER_OPERATION_TILED_FUNC(OP_COPYSIGN, Opcode::OP_COPYSIGN, BinaryOperationTileFunc<BinaryOpType::COPYSIGN>); | ||
| 174 | 192 | ||
| 175 | } // namespace npu::tile_fwk | 193 | } // namespace npu::tile_fwk |
| @@ -1,197 +0,0 @@ | |||
| 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 statistics.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 | -Tensor Clip(const Tensor& self, const Element& min, const Element& max) | ||
| 26 | -{ | ||
| 27 | - DECLARE_TRACER(); | ||
| 28 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Clip"); | ||
| 29 | - | ||
| 30 | - static const std::unordered_set<DataType> CLIP_A2A3_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16}; | ||
| 31 | - static const std::unordered_set<DataType> CLIP_A5_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16, DT_INT64}; | ||
| 32 | - const auto& supportedTypes = GetSupportedDataTypesByArch(CLIP_A2A3_TYPES, CLIP_A5_TYPES); | ||
| 33 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "CLIP"); | ||
| 34 | - CheckTensorShapeSize(self.GetStorage(), "CLIP"); | ||
| 35 | - | ||
| 36 | - Element min_ = min, max_ = max; | ||
| 37 | - | ||
| 38 | - Tensor result = self; | ||
| 39 | - if (min_.GetDataType() != DT_BOTTOM) { | ||
| 40 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, min_.GetDataType() == self.GetDataType()) | ||
| 41 | - << "The datatype of inputs should be same"; | ||
| 42 | - result = Maximum(result, min_); | ||
| 43 | - } | ||
| 44 | - if (max_.GetDataType() != DT_BOTTOM) { | ||
| 45 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, max_.GetDataType() == self.GetDataType()) | ||
| 46 | - << "The datatype of inputs should be same"; | ||
| 47 | - result = Minimum(result, max_); | ||
| 48 | - } | ||
| 49 | - result.GetStorage()->UpdateDynValidShape(self.GetStorage()->GetDynValidShape()); | ||
| 50 | - return result; | ||
| 51 | -} | ||
| 52 | - | ||
| 53 | -Tensor Clip(const Tensor& self, const Tensor& min, const Tensor& max) | ||
| 54 | -{ | ||
| 55 | - DECLARE_TRACER(); | ||
| 56 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Clip"); | ||
| 57 | - | ||
| 58 | - static const std::unordered_set<DataType> CLIP_A2A3_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16}; | ||
| 59 | - static const std::unordered_set<DataType> CLIP_A5_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16, DT_INT64}; | ||
| 60 | - const auto& supportedTypes = GetSupportedDataTypesByArch(CLIP_A2A3_TYPES, CLIP_A5_TYPES); | ||
| 61 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "CLIP"); | ||
| 62 | - CheckTensorShapeSize(self.GetStorage(), "CLIP"); | ||
| 63 | - | ||
| 64 | - Tensor result = self; | ||
| 65 | - if (min.GetStorage() != nullptr) { | ||
| 66 | - CheckTensorFormat(min.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Clip"); | ||
| 67 | - CheckTensorShapeSize(min.GetStorage(), "CLIP"); | ||
| 68 | - CheckTensorsFormatConsistency(self.GetStorage(), min.GetStorage(), "CLIP"); | ||
| 69 | - std::vector minBroadcastAxes = GetBroadcastAxes(min.GetShape(), self.GetShape()); | ||
| 70 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, minBroadcastAxes.size() <= 1) | ||
| 71 | - << "minBroadcastAxes size should be <= 1"; | ||
| 72 | - CheckInt64Broadcast(self.GetStorage(), min.GetStorage(), "CLIP"); | ||
| 73 | - result = Maximum(result, min); | ||
| 74 | - } | ||
| 75 | - if (max.GetStorage() != nullptr) { | ||
| 76 | - CheckTensorFormat(max.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Clip"); | ||
| 77 | - CheckTensorShapeSize(max.GetStorage(), "CLIP"); | ||
| 78 | - CheckTensorsFormatConsistency(self.GetStorage(), max.GetStorage(), "CLIP"); | ||
| 79 | - std::vector maxBroadcastAxes = GetBroadcastAxes(max.GetShape(), self.GetShape()); | ||
| 80 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, maxBroadcastAxes.size() <= 1) | ||
| 81 | - << "maxBroadcastAxes size should be <= 1"; | ||
| 82 | - CheckInt64Broadcast(self.GetStorage(), max.GetStorage(), "CLIP"); | ||
| 83 | - result = Minimum(result, max); | ||
| 84 | - } | ||
| 85 | - result.GetStorage()->UpdateDynValidShape(self.GetStorage()->GetDynValidShape()); | ||
| 86 | - return result; | ||
| 87 | -} | ||
| 88 | -// endregion: Clip | ||
| 89 | - | ||
| 90 | -static void VarParamVaildCheck(const Tensor& input, std::vector<int>& dim) | ||
| 91 | -{ | ||
| 92 | - std::unordered_set<DataType> supportedTypes = {DT_FP32, DT_FP16, DT_BF16}; | ||
| 93 | - CheckTensorDataType(input.GetStorage(), supportedTypes, "VAR"); | ||
| 94 | - CheckTensorDimRange(input.GetStorage(), 1, NUM_VALUE_4, "VAR"); | ||
| 95 | - CheckTensorShapeSize(input.GetStorage(), "VAR"); | ||
| 96 | - | ||
| 97 | - Shape shape = input.GetShape(); | ||
| 98 | - uint64_t shapeSize = shape.size(); | ||
| 99 | - | ||
| 100 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, dim.size() <= shapeSize) << "The dim.size() should <= input.size()"; | ||
| 101 | - for (uint64_t i = 0; i < shapeSize; i++) { | ||
| 102 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, shape[i] > 0) << "The input shape should > 0"; | ||
| 103 | - } | ||
| 104 | - | ||
| 105 | - if (dim.empty()) { | ||
| 106 | - for (uint64_t i = 0; i < shapeSize; i++) { | ||
| 107 | - dim.push_back(static_cast<int>(i)); | ||
| 108 | - } | ||
| 109 | - } | ||
| 110 | - std::set<int> dupDimSet(dim.begin(), dim.end()); | ||
| 111 | - | ||
| 112 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, dupDimSet.size() == dim.size()) << "There are duplicate elements in dim"; | ||
| 113 | - for (size_t i = 0; i < dim.size(); i++) { | ||
| 114 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, | ||
| 115 | - dim[i] < static_cast<int>(shapeSize) && dim[i] >= -(static_cast<int>(shapeSize))) | ||
| 116 | - << "The value in dim is out of range"; | ||
| 117 | - if (dim[i] < 0) { | ||
| 118 | - dim[i] = dim[i] + static_cast<int>(shapeSize); | ||
| 119 | - } | ||
| 120 | - } | ||
| 121 | - std::sort(dim.begin(), dim.end()); | ||
| 122 | -} | ||
| 123 | - | ||
| 124 | -static Tensor VarResSqueeze(const Tensor& res, const std::vector<int>& dim, const std::vector<int64_t>& oriVecTile, | ||
| 125 | - DataType dtype) | ||
| 126 | -{ | ||
| 127 | - std::vector<int64_t> vecTile(oriVecTile.begin(), oriVecTile.end()); | ||
| 128 | - for (auto it = dim.rbegin(); it != dim.rend(); ++it) { | ||
| 129 | - vecTile.erase(vecTile.begin() + *it); | ||
| 130 | - } | ||
| 131 | - int64_t algnedSize = BLOCK_SIZE / BytesOf(dtype); | ||
| 132 | - if (vecTile.empty()) { | ||
| 133 | - vecTile.push_back(algnedSize); | ||
| 134 | - } | ||
| 135 | - int64_t lastDimSize = vecTile.back(); | ||
| 136 | - if (lastDimSize % algnedSize != 0) { | ||
| 137 | - vecTile.back() = CeilDiv(lastDimSize, algnedSize) * algnedSize; | ||
| 138 | - } | ||
| 139 | - TileShape::Current().SetVecTile(vecTile); | ||
| 140 | - return Squeeze(res, dim); | ||
| 141 | -} | ||
| 142 | - | ||
| 143 | -Tensor Var(const Tensor& input, const std::vector<int>& dim, float correction, bool keepDim) | ||
| 144 | -{ | ||
| 145 | - CheckTensorFormat(input.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Var"); | ||
| 146 | - | ||
| 147 | - std::vector<int> innerDim(dim.begin(), dim.end()); | ||
| 148 | - VarParamVaildCheck(input, innerDim); | ||
| 149 | - | ||
| 150 | - DataType dtype = input.GetDataType(); | ||
| 151 | - Shape shape = input.GetShape(); | ||
| 152 | - auto castInput = Tensor(DT_FP32, shape); | ||
| 153 | - if (dtype == DT_FP16 || dtype == DT_BF16) { | ||
| 154 | - castInput = Cast(input, DT_FP32, CAST_NONE); | ||
| 155 | - } else { | ||
| 156 | - castInput = input; | ||
| 157 | - } | ||
| 158 | - | ||
| 159 | - int calcN = 1; | ||
| 160 | - auto res = castInput; | ||
| 161 | - for (size_t i = 0; i < innerDim.size(); i++) { | ||
| 162 | - calcN *= static_cast<int>(shape[innerDim[i]]); | ||
| 163 | - } | ||
| 164 | - res = Div(res, Element(DT_FP32, static_cast<float>(calcN))); | ||
| 165 | - for (size_t i = 0; i < innerDim.size(); i++) { | ||
| 166 | - res = Sum(res, innerDim[i], true); | ||
| 167 | - } | ||
| 168 | - | ||
| 169 | - Shape dstShape = res.GetShape(); | ||
| 170 | - for (size_t i = 0; i < innerDim.size(); i++) { | ||
| 171 | - dstShape[innerDim[i]] = shape[innerDim[i]]; | ||
| 172 | - res = Expand(res, dstShape); | ||
| 173 | - } | ||
| 174 | - | ||
| 175 | - res = Sub(castInput, res); | ||
| 176 | - res = Mul(res, res); | ||
| 177 | - float count = std::max(0.0f, static_cast<float>(calcN) - correction); | ||
| 178 | - res = Div(res, Element(DT_FP32, count)); | ||
| 179 | - for (size_t i = 0; i < innerDim.size(); i++) { | ||
| 180 | - res = Sum(res, innerDim[i], true); | ||
| 181 | - } | ||
| 182 | - auto oriVecTile = TileShape::Current().GetVecTile(); | ||
| 183 | - if (!keepDim) { | ||
| 184 | - res = VarResSqueeze(res, innerDim, oriVecTile.tile, dtype); | ||
| 185 | - } | ||
| 186 | - | ||
| 187 | - if (dtype == DT_FP16 || dtype == DT_BF16) { | ||
| 188 | - res = Cast(res, dtype, CAST_NONE); | ||
| 189 | - } | ||
| 190 | - if (!keepDim) { | ||
| 191 | - TileShape::Current().SetVecTile(oriVecTile.tile); | ||
| 192 | - } | ||
| 193 | - | ||
| 194 | - return res; | ||
| 195 | -} | ||
| 196 | - | ||
| 197 | -} // namespace npu::tile_fwk | ||
| @@ -14,8 +14,6 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "unary.h" | 16 | #include "unary.h" |
| 17 | -#include <sstream> | ||
| 18 | -#include <string> | ||
| 19 | #include "tensor_transformation.h" | 17 | #include "tensor_transformation.h" |
| 20 | #include "interface/utils/operator_tracer.h" | 18 | #include "interface/utils/operator_tracer.h" |
| 21 | #include "tilefwk/error_code.h" | 19 | #include "tilefwk/error_code.h" |
| @@ -570,7 +568,7 @@ void InnerTransDataND(size_t cur, Function& function, const TileShape& tileShape | |||
| 570 | auto vecTile = tileShape.GetVecTile(); | 568 | auto vecTile = tileShape.GetVecTile(); |
| 571 | int inputSize = input->GetShape().size(); | 569 | int inputSize = input->GetShape().size(); |
| 572 | 570 | ||
| 573 | - std::unordered_map<int64_t, int64_t> format2InputAxis = {{5, 1}, {6, 2}}; | 571 | + std::unordered_map<int64_t, int64_t> format2InputAxis = {{SHAPE_DIM5, 1}, {SHAPE_DIM6, 2}}; |
| 574 | int64_t inputGroupAxis = format2InputAxis[inputSize]; | 572 | int64_t inputGroupAxis = format2InputAxis[inputSize]; |
| 575 | CHECK(VectorErrorCode::ERR_PARAM_INVALID, group > 0) << "The group is not valid !"; | 573 | CHECK(VectorErrorCode::ERR_PARAM_INVALID, group > 0) << "The group is not valid !"; |
| 576 | int64_t inputPerGroup = input->GetShape()[inputGroupAxis] / group; | 574 | int64_t inputPerGroup = input->GetShape()[inputGroupAxis] / group; |
| @@ -582,11 +580,11 @@ void InnerTransDataND(size_t cur, Function& function, const TileShape& tileShape | |||
| 582 | transDataTileInfoPara.inputTileInfo.offset); | 580 | transDataTileInfoPara.inputTileInfo.offset); |
| 583 | 581 | ||
| 584 | switch (inputSize) { | 582 | switch (inputSize) { |
| 585 | - case 5: | 583 | + case SHAPE_DIM5: |
| 586 | HandleNCHW5DimFormat(function, dstTensor, inputTile, tileParams, transDataTileInfoPara, inputPerGroup, | 584 | HandleNCHW5DimFormat(function, dstTensor, inputTile, tileParams, transDataTileInfoPara, inputPerGroup, |
| 587 | transDataPara); | 585 | transDataPara); |
| 588 | return; | 586 | return; |
| 589 | - case 6: | 587 | + case SHAPE_DIM6: |
| 590 | HandleNCDHW6DimFormat(function, dstTensor, inputTile, tileParams, transDataTileInfoPara, inputPerGroup, | 588 | HandleNCDHW6DimFormat(function, dstTensor, inputTile, tileParams, transDataTileInfoPara, inputPerGroup, |
| 591 | transDataPara); | 589 | transDataPara); |
| 592 | return; | 590 | return; |
| @@ -895,7 +893,7 @@ void InnerTransDataFz2ND(size_t cur, Function& function, const TileShape& tileSh | |||
| 895 | auto vecTile = tileShape.GetVecTile(); | 893 | auto vecTile = tileShape.GetVecTile(); |
| 896 | int outputSize = dstTensor->GetShape().size(); | 894 | int outputSize = dstTensor->GetShape().size(); |
| 897 | 895 | ||
| 898 | - std::unordered_map<int64_t, int64_t> format2DstAxis = {{4, 0}, {5, 0}}; | 896 | + std::unordered_map<int64_t, int64_t> format2DstAxis = {{SHAPE_DIM4, 0}, {SHAPE_DIM5, 0}}; |
| 899 | int64_t outputAxis = format2DstAxis[outputSize]; | 897 | int64_t outputAxis = format2DstAxis[outputSize]; |
| 900 | int64_t tmpOffset = groupIdx * dstC1 * dstH * dstW + C1Idx * dstH * dstW + HIdx * dstW; | 898 | int64_t tmpOffset = groupIdx * dstC1 * dstH * dstW + C1Idx * dstH * dstW + HIdx * dstW; |
| 901 | 899 | ||
| @@ -906,7 +904,7 @@ void InnerTransDataFz2ND(size_t cur, Function& function, const TileShape& tileSh | |||
| 906 | transDataTileInfoPara.inputTileInfo.offset); | 904 | transDataTileInfoPara.inputTileInfo.offset); |
| 907 | 905 | ||
| 908 | switch (outputSize) { | 906 | switch (outputSize) { |
| 909 | - case 4: | 907 | + case SHAPE_DIM4: |
| 910 | HandleFractalZ2NCHW(function, dstTensor, inputTile, tileParams, transDataTileInfoPara, dstW, | 908 | HandleFractalZ2NCHW(function, dstTensor, inputTile, tileParams, transDataTileInfoPara, dstW, |
| 911 | transDataPara); | 909 | transDataPara); |
| 912 | return; | 910 | return; |
| @@ -977,12 +975,13 @@ void TiledTransData(Function& function, const TileShape& tileShape, TransDataPar | |||
| 977 | int inputShapeSize = transDataPara.input->GetShape().size(); | 975 | int inputShapeSize = transDataPara.input->GetShape().size(); |
| 978 | int outputShapeSize = transDataPara.dstTensor->GetShape().size(); | 976 | int outputShapeSize = transDataPara.dstTensor->GetShape().size(); |
| 979 | if (T == TileOpFormat::TILEOP_ND) { | 977 | if (T == TileOpFormat::TILEOP_ND) { |
| 980 | - if ((inputShapeSize == 5 && outputShapeSize == 4) || (inputShapeSize == 6 && outputShapeSize == 5)) { | 978 | + if ((inputShapeSize == SHAPE_DIM5 && outputShapeSize == SHAPE_DIM4) || |
| 979 | + (inputShapeSize == SHAPE_DIM6 && outputShapeSize == SHAPE_DIM5)) { | ||
| 981 | for (int i = 0; i < group; i++) { | 980 | for (int i = 0; i < group; i++) { |
| 982 | transDataPara.groupIdx = i; | 981 | transDataPara.groupIdx = i; |
| 983 | InnerTransDataND<T>(0, function, tileShape, transDataPara, transDataTileInfoPara); | 982 | InnerTransDataND<T>(0, function, tileShape, transDataPara, transDataTileInfoPara); |
| 984 | } | 983 | } |
| 985 | - } else if (inputShapeSize == 4 && outputShapeSize == 5) { | 984 | + } else if (inputShapeSize == SHAPE_DIM4 && outputShapeSize == SHAPE_DIM5) { |
| 986 | int dstC1 = (transDataPara.dstTensor->GetShape()[1] + C0 - 1) / C0; | 985 | int dstC1 = (transDataPara.dstTensor->GetShape()[1] + C0 - 1) / C0; |
| 987 | int dstD = transDataPara.dstTensor->GetShape()[2]; | 986 | int dstD = transDataPara.dstTensor->GetShape()[2]; |
| 988 | int dstH = transDataPara.dstTensor->GetShape()[3]; | 987 | int dstH = transDataPara.dstTensor->GetShape()[3]; |
| @@ -1394,16 +1393,16 @@ LogicalTensorPtr TransDataReverse(Function& function, const LogicalTensorPtr& se | |||
| 1394 | { | 1393 | { |
| 1395 | int inputShapeSize = self->GetShape().size(); | 1394 | int inputShapeSize = self->GetShape().size(); |
| 1396 | int outputShapeSize = output->GetShape().size(); | 1395 | int outputShapeSize = output->GetShape().size(); |
| 1397 | - if (inputShapeSize == 5 && outputShapeSize == 4) { | 1396 | + if (inputShapeSize == SHAPE_DIM5 && outputShapeSize == SHAPE_DIM4) { |
| 1398 | return TransDataNC1HWC02NCHW(function, self, output, group); | 1397 | return TransDataNC1HWC02NCHW(function, self, output, group); |
| 1399 | - } else if (inputShapeSize == 6 && outputShapeSize == 5) { | 1398 | + } else if (inputShapeSize == SHAPE_DIM6 && outputShapeSize == SHAPE_DIM5) { |
| 1400 | return TransDataNDC1HWC02NCDHW(function, self, output, group); | 1399 | return TransDataNDC1HWC02NCDHW(function, self, output, group); |
| 1401 | - } else if (inputShapeSize == 4 && outputShapeSize == 4) { | 1400 | + } else if (inputShapeSize == SHAPE_DIM4 && outputShapeSize == SHAPE_DIM4) { |
| 1402 | return TransDataFractalZ2NCHW(function, self, output, group); | 1401 | return TransDataFractalZ2NCHW(function, self, output, group); |
| 1403 | - } else if (inputShapeSize == 4 && outputShapeSize == 5) { | 1402 | + } else if (inputShapeSize == SHAPE_DIM4 && outputShapeSize == SHAPE_DIM5) { |
| 1404 | return TransDataFractalZ3D2NCDHW(function, self, output, group); | 1403 | return TransDataFractalZ3D2NCDHW(function, self, output, group); |
| 1405 | } else { | 1404 | } else { |
| 1406 | - return TransDataFractalZ2NCHW(function, self, output, group); // TODO | 1405 | + return TransDataFractalZ2NCHW(function, self, output, group); |
| 1407 | } | 1406 | } |
| 1408 | } | 1407 | } |
| 1409 | 1408 | ||
| @@ -14,8 +14,6 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "unary.h" | 16 | #include "unary.h" |
| 17 | -#include <sstream> | ||
| 18 | -#include <string> | ||
| 19 | #include "tensor_transformation.h" | 17 | #include "tensor_transformation.h" |
| 20 | #include "interface/utils/operator_tracer.h" | 18 | #include "interface/utils/operator_tracer.h" |
| 21 | #include "tilefwk/error_code.h" | 19 | #include "tilefwk/error_code.h" |
| @@ -141,8 +141,6 @@ void TriULOperationTileFunc(Function& function, const TileShape& tileShape, | |||
| 141 | TiledTriUL(function, tileShape, {iOperand[0], oOperand[0], diagonal, isUpper}); | 141 | TiledTriUL(function, tileShape, {iOperand[0], oOperand[0], diagonal, isUpper}); |
| 142 | } | 142 | } |
| 143 | 143 | ||
| 144 | -// beginregin: Clip | ||
| 145 | - | ||
| 146 | REGISTER_OPERATION_TILED_FUNC(OP_TRIUL, Opcode::OP_TRIUL, TriULOperationTileFunc); | 144 | REGISTER_OPERATION_TILED_FUNC(OP_TRIUL, Opcode::OP_TRIUL, TriULOperationTileFunc); |
| 147 | 145 | ||
| 148 | } // namespace npu::tile_fwk | 146 | } // namespace npu::tile_fwk |
| @@ -14,8 +14,8 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "unary_tiled.h" | 16 | #include "unary_tiled.h" |
| 17 | -#include "unary_utils.h" | ||
| 18 | #include "binary.h" | 17 | #include "binary.h" |
| 18 | +#include "binary_tiled.h" | ||
| 19 | #include "tensor_transformation.h" | 19 | #include "tensor_transformation.h" |
| 20 | #include "interface/utils/operator_tracer.h" | 20 | #include "interface/utils/operator_tracer.h" |
| 21 | #include "passes/pass_utils/graph_utils.h" | 21 | #include "passes/pass_utils/graph_utils.h" |
| @@ -43,6 +43,32 @@ Tensor Atan(const Tensor& self) | |||
| 43 | return res; | 43 | return res; |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | +Tensor Atan2(const Tensor& y, const Tensor& x) | ||
| 47 | +{ | ||
| 48 | + DECLARE_TRACER(); | ||
| 49 | + CheckTensorFormat(y.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Atan2"); | ||
| 50 | + CheckTensorFormat(x.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Atan2"); | ||
| 51 | + | ||
| 52 | + CheckTensorsDataTypeConsistency(y.GetStorage(), x.GetStorage(), "ATAN2"); | ||
| 53 | + std::unordered_set<DataType> supportedTypes = {DT_FP32, DT_FP16, DT_BF16}; | ||
| 54 | + CheckTensorDataType(y.GetStorage(), supportedTypes, "ATAN2"); | ||
| 55 | + auto castY = y.GetStorage(); | ||
| 56 | + auto castX = x.GetStorage(); | ||
| 57 | + DataType dataType = y.GetDataType(); | ||
| 58 | + if (dataType != DataType::DT_FP32) { | ||
| 59 | + castY = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), y.GetStorage(), | ||
| 60 | + DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 61 | + castX = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), x.GetStorage(), | ||
| 62 | + DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 63 | + } | ||
| 64 | + auto res = CALL(BinaryOperation<BinaryOpType::ATAN2>, *Program::GetInstance().GetCurrentFunction(), castY, castX); | ||
| 65 | + if (dataType != DataType::DT_FP32) { | ||
| 66 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), res, dataType, | ||
| 67 | + CastMode::CAST_NONE); | ||
| 68 | + } | ||
| 69 | + return res; | ||
| 70 | +} | ||
| 71 | + | ||
| 46 | Tensor Sin(const Tensor& self) | 72 | Tensor Sin(const Tensor& self) |
| 47 | { | 73 | { |
| 48 | DECLARE_TRACER(); | 74 | DECLARE_TRACER(); |
| @@ -241,6 +267,7 @@ void TanOperationTileFunc(Function& function, const TileShape& tileShape, const | |||
| 241 | } | 267 | } |
| 242 | 268 | ||
| 243 | REGISTER_OPERATION_TILED_FUNC(OP_ATAN, Opcode::OP_ATAN, AtanOperationTileFunc); | 269 | REGISTER_OPERATION_TILED_FUNC(OP_ATAN, Opcode::OP_ATAN, AtanOperationTileFunc); |
| 270 | +REGISTER_OPERATION_TILED_FUNC(OP_ATAN2, Opcode::OP_ATAN2, BinaryOperationTileFunc<BinaryOpType::ATAN2>); | ||
| 244 | REGISTER_OPERATION_TILED_FUNC(OP_SIN, Opcode::OP_SIN, SinOperationTileFunc); | 271 | REGISTER_OPERATION_TILED_FUNC(OP_SIN, Opcode::OP_SIN, SinOperationTileFunc); |
| 245 | REGISTER_OPERATION_TILED_FUNC(OP_COS, Opcode::OP_COS, CosOperationTileFunc); | 272 | REGISTER_OPERATION_TILED_FUNC(OP_COS, Opcode::OP_COS, CosOperationTileFunc); |
| 246 | REGISTER_OPERATION_TILED_FUNC(OP_ASIN, Opcode::OP_ASIN, AsinAcosOperationTileFunc<UnaryOpType::ASIN>); | 273 | REGISTER_OPERATION_TILED_FUNC(OP_ASIN, Opcode::OP_ASIN, AsinAcosOperationTileFunc<UnaryOpType::ASIN>); |
| @@ -1,31 +0,0 @@ | |||
| 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 unary.cpp | ||
| 13 | - * \brief | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#include "binary.h" | ||
| 17 | -#include "unary.h" | ||
| 18 | -#include "tensor_transformation.h" | ||
| 19 | -#include "interface/utils/operator_tracer.h" | ||
| 20 | -#include "tilefwk/error_code.h" | ||
| 21 | - | ||
| 22 | -namespace npu::tile_fwk { | ||
| 23 | - | ||
| 24 | -void UnaryOperationOperandCheck(const std::vector<LogicalTensorPtr>& iOperand, | ||
| 25 | - const std::vector<LogicalTensorPtr>& oOperand) | ||
| 26 | -{ | ||
| 27 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, iOperand.size() == 1) << "The input operand size should be 1"; | ||
| 28 | - CHECK(VectorErrorCode::ERR_PARAM_INVALID, oOperand.size() == 1) << "The output operand size should be 1"; | ||
| 29 | -} | ||
| 30 | - | ||
| 31 | -} // namespace npu::tile_fwk | ||
| @@ -177,8 +177,22 @@ Opcode GetUnaryOpNameCode() | |||
| 177 | #undef CASE | 177 | #undef CASE |
| 178 | } | 178 | } |
| 179 | 179 | ||
| 180 | -void UnaryOperationOperandCheck(const std::vector<LogicalTensorPtr>& iOperand, | 180 | +inline void UnaryOperationOperandCheck(const std::vector<LogicalTensorPtr>& iOperand, |
| 181 | - const std::vector<LogicalTensorPtr>& oOperand); | 181 | + const std::vector<LogicalTensorPtr>& oOperand) |
| 182 | +{ | ||
| 183 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, iOperand.size() == 1) << "The input operand size should be 1"; | ||
| 184 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, oOperand.size() == 1) << "The output operand size should be 1"; | ||
| 185 | +} | ||
| 186 | + | ||
| 187 | +template <typename T> | ||
| 188 | +int64_t MultiplyLastTwoDims(const std::vector<int64_t>& vec) | ||
| 189 | +{ | ||
| 190 | + constexpr size_t ALIGN_SIZE = NUM_VALUE_32; | ||
| 191 | + constexpr size_t ELEMENT_SIZE = sizeof(T); | ||
| 192 | + constexpr size_t ALIGN_ELEMENTS = ALIGN_SIZE / ELEMENT_SIZE; | ||
| 193 | + int64_t axis2 = (vec[vec.size() - 1] + ALIGN_ELEMENTS - 1) / ALIGN_ELEMENTS * ALIGN_ELEMENTS; | ||
| 194 | + return axis2 * vec[vec.size() - NUM_VALUE_2]; | ||
| 195 | +} | ||
| 182 | 196 | ||
| 183 | template <UnaryOpType T> | 197 | template <UnaryOpType T> |
| 184 | std::pair<LogicalTensorPtr, Operation*> TensorUnaryOperationWithOp(Function& function, LogicalTensorPtr operand, | 198 | std::pair<LogicalTensorPtr, Operation*> TensorUnaryOperationWithOp(Function& function, LogicalTensorPtr operand, |
| @@ -13,6 +13,7 @@ | |||
| 13 | * \brief | 13 | * \brief |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | +#include <cmath> | ||
| 16 | #include "unary_tiled.h" | 17 | #include "unary_tiled.h" |
| 17 | #include "binary.h" | 18 | #include "binary.h" |
| 18 | #include "tensor_transformation.h" | 19 | #include "tensor_transformation.h" |
| @@ -76,24 +77,6 @@ Tensor Relu(const Tensor& self) | |||
| 76 | RETURN_CALL(UnaryOperation<UnaryOpType::RELU>, *Program::GetInstance().GetCurrentFunction(), self.GetStorage()); | 77 | RETURN_CALL(UnaryOperation<UnaryOpType::RELU>, *Program::GetInstance().GetCurrentFunction(), self.GetStorage()); |
| 77 | } | 78 | } |
| 78 | 79 | ||
| 79 | -Tensor BitwiseNot(const Tensor& self) | ||
| 80 | -{ | ||
| 81 | - DECLARE_TRACER(); | ||
| 82 | - CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "BitwiseNot"); | ||
| 83 | - | ||
| 84 | - if (self.GetDataType() == DT_BOOL) { | ||
| 85 | - return LogicalNot(self); | ||
| 86 | - } | ||
| 87 | - static const std::unordered_set<DataType> BITWISE_A2A3_TYPES = {DT_INT16, DT_UINT16, DT_INT8, | ||
| 88 | - DT_UINT8, DT_INT32, DT_UINT32}; | ||
| 89 | - static const std::unordered_set<DataType> BITWISE_A5_TYPES = {DT_INT16, DT_UINT16, DT_INT8, DT_UINT8, | ||
| 90 | - DT_INT32, DT_UINT32, DT_INT64, DT_UINT64}; | ||
| 91 | - const auto& supportedTypes = GetSupportedDataTypesByArch(BITWISE_A2A3_TYPES, BITWISE_A5_TYPES); | ||
| 92 | - CheckTensorDataType(self.GetStorage(), supportedTypes, "BitwiseNot"); | ||
| 93 | - RETURN_CALL(UnaryOperation<UnaryOpType::BITWISENOT>, *Program::GetInstance().GetCurrentFunction(), | ||
| 94 | - self.GetStorage()); | ||
| 95 | -} | ||
| 96 | - | ||
| 97 | Tensor Reciprocal(const Tensor& operand, PrecisionType precisionType) | 80 | Tensor Reciprocal(const Tensor& operand, PrecisionType precisionType) |
| 98 | { | 81 | { |
| 99 | DECLARE_TRACER(); | 82 | DECLARE_TRACER(); |
| @@ -150,6 +133,197 @@ Tensor Neg(const Tensor& self) | |||
| 150 | } | 133 | } |
| 151 | } | 134 | } |
| 152 | 135 | ||
| 136 | +Tensor Duplicate(const Tensor& operand) | ||
| 137 | +{ | ||
| 138 | + DECLARE_TRACER(); | ||
| 139 | + CheckTensorFormat(operand.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Duplicate"); | ||
| 140 | + | ||
| 141 | + RETURN_CALL(UnaryOperation<UnaryOpType::DUPLICATE>, *Program::GetInstance().GetCurrentFunction(), | ||
| 142 | + operand.GetStorage()); | ||
| 143 | +} | ||
| 144 | + | ||
| 145 | +Tensor IsFinite(const Tensor& self) | ||
| 146 | +{ | ||
| 147 | + DECLARE_TRACER(); | ||
| 148 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "IsFinite"); | ||
| 149 | + | ||
| 150 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_FP32, DT_BF16, DT_INT16, DT_INT4, DT_INT8, | ||
| 151 | + DT_INT32, DT_UINT16, DT_UINT32, DT_UINT8, DT_UINT64, DT_INT64}; | ||
| 152 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "IsFinite"); | ||
| 153 | + RETURN_CALL(UnaryOperation<UnaryOpType::ISFINITE>, *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), | ||
| 154 | + DT_BOOL); | ||
| 155 | +} | ||
| 156 | + | ||
| 157 | +Tensor IsNan(const Tensor& self) | ||
| 158 | +{ | ||
| 159 | + DECLARE_TRACER(); | ||
| 160 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "IsNan"); | ||
| 161 | + | ||
| 162 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_FP32, DT_BF16}; | ||
| 163 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "ISNAN"); | ||
| 164 | + | ||
| 165 | + RETURN_CALL(UnaryOperation<UnaryOpType::ISNAN>, *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), | ||
| 166 | + DT_BOOL); | ||
| 167 | +} | ||
| 168 | + | ||
| 169 | +Tensor Ceil(const Tensor& self) | ||
| 170 | +{ | ||
| 171 | + DECLARE_TRACER(); | ||
| 172 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Ceil"); | ||
| 173 | + | ||
| 174 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_FP32}; | ||
| 175 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "Ceil"); | ||
| 176 | + | ||
| 177 | + if (self.GetDataType() == DataType::DT_INT16 || self.GetDataType() == DataType::DT_INT32) { | ||
| 178 | + RETURN_CALL(UnaryOperation<UnaryOpType::CEIL>, *Program::GetInstance().GetCurrentFunction(), self.GetStorage()); | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + auto castSelf = self.GetStorage(); | ||
| 182 | + if (self.GetDataType() != DataType::DT_FP32) { | ||
| 183 | + castSelf = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), | ||
| 184 | + self.GetStorage(), DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + auto ceilResult = CALL(UnaryOperation<UnaryOpType::CEIL>, *Program::GetInstance().GetCurrentFunction(), castSelf); | ||
| 188 | + if (self.GetDataType() != DataType::DT_FP32) { | ||
| 189 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), ceilResult, | ||
| 190 | + self.GetDataType(), CastMode::CAST_NONE); | ||
| 191 | + } | ||
| 192 | + return ceilResult; | ||
| 193 | +} | ||
| 194 | + | ||
| 195 | +Tensor Floor(const Tensor& self) | ||
| 196 | +{ | ||
| 197 | + DECLARE_TRACER(); | ||
| 198 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Floor"); | ||
| 199 | + | ||
| 200 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_FP32}; | ||
| 201 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "Floor"); | ||
| 202 | + | ||
| 203 | + if (self.GetDataType() == DataType::DT_INT16 || self.GetDataType() == DataType::DT_INT32) { | ||
| 204 | + RETURN_CALL(UnaryOperation<UnaryOpType::FLOOR>, *Program::GetInstance().GetCurrentFunction(), | ||
| 205 | + self.GetStorage()); | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + auto castSelf = self.GetStorage(); | ||
| 209 | + if (self.GetDataType() != DataType::DT_FP32) { | ||
| 210 | + castSelf = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), | ||
| 211 | + self.GetStorage(), DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + auto floorResult = CALL(UnaryOperation<UnaryOpType::FLOOR>, *Program::GetInstance().GetCurrentFunction(), castSelf); | ||
| 215 | + if (self.GetDataType() != DataType::DT_FP32) { | ||
| 216 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), floorResult, | ||
| 217 | + self.GetDataType(), CastMode::CAST_NONE); | ||
| 218 | + } | ||
| 219 | + return floorResult; | ||
| 220 | +} | ||
| 221 | + | ||
| 222 | +Tensor Trunc(const Tensor& self) | ||
| 223 | +{ | ||
| 224 | + DECLARE_TRACER(); | ||
| 225 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Trunc"); | ||
| 226 | + | ||
| 227 | + std::unordered_set<DataType> supportedTypes = {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_FP32}; | ||
| 228 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "Trunc"); | ||
| 229 | + | ||
| 230 | + if (self.GetDataType() == DataType::DT_INT16 || self.GetDataType() == DataType::DT_INT32) { | ||
| 231 | + RETURN_CALL(UnaryOperation<UnaryOpType::TRUNC>, *Program::GetInstance().GetCurrentFunction(), | ||
| 232 | + self.GetStorage()); | ||
| 233 | + } | ||
| 234 | + | ||
| 235 | + auto castSelf = self.GetStorage(); | ||
| 236 | + if (self.GetDataType() != DataType::DT_FP32) { | ||
| 237 | + castSelf = CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), | ||
| 238 | + self.GetStorage(), DataType::DT_FP32, CastMode::CAST_NONE); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + auto truncResult = CALL(UnaryOperation<UnaryOpType::TRUNC>, *Program::GetInstance().GetCurrentFunction(), castSelf); | ||
| 242 | + if (self.GetDataType() != DataType::DT_FP32) { | ||
| 243 | + RETURN_CALL(CastOperation<CastOpType::CAST>, *Program::GetInstance().GetCurrentFunction(), truncResult, | ||
| 244 | + self.GetDataType(), CastMode::CAST_NONE); | ||
| 245 | + } | ||
| 246 | + return truncResult; | ||
| 247 | +} | ||
| 248 | + | ||
| 249 | +Tensor TensorRound(Function& function, const LogicalTensorPtr& self, const int& decimals = 0) | ||
| 250 | +{ | ||
| 251 | + auto result = std::make_shared<LogicalTensor>(function, self->Datatype(), self->GetShape(), | ||
| 252 | + self->GetDynValidShape()); | ||
| 253 | + auto& op = function.AddOperation(Opcode::OP_ROUND, {self}, {result}); | ||
| 254 | + op.SetAttribute(OP_ATTR_PREFIX + "decimals", decimals); | ||
| 255 | + return result; | ||
| 256 | +} | ||
| 257 | + | ||
| 258 | +Tensor Round(const Tensor& self, const int& decimals) | ||
| 259 | +{ | ||
| 260 | + DECLARE_TRACER(); | ||
| 261 | + CheckTensorFormat(self.GetStorage(), {TileOpFormat::TILEOP_NZ}, "Round"); | ||
| 262 | + | ||
| 263 | + static const std::unordered_set<DataType> ROUND_A2A3_TYPES = {DT_FP32, DT_FP16, DT_BF16, DT_INT32, DT_INT16}; | ||
| 264 | + static const std::unordered_set<DataType> ROUND_A5_TYPES = {DT_FP32, DT_FP16, DT_BF16, | ||
| 265 | + DT_INT32, DT_INT16, DT_INT64}; | ||
| 266 | + const auto& supportedTypes = GetSupportedDataTypesByArch(ROUND_A2A3_TYPES, ROUND_A5_TYPES); | ||
| 267 | + CheckTensorDataType(self.GetStorage(), supportedTypes, "ROUND"); | ||
| 268 | + CheckTensorDimRange(self.GetStorage(), 1, NUM_VALUE_4, "ROUND"); | ||
| 269 | + CheckTensorShapeSize(self.GetStorage(), "ROUND"); | ||
| 270 | + | ||
| 271 | + RETURN_CALL(Round, *Program::GetInstance().GetCurrentFunction(), self.GetStorage(), decimals); | ||
| 272 | +} | ||
| 273 | + | ||
| 274 | +void TiledRound(Function& function, const TileShape& tileShape, size_t cur, Input& input, | ||
| 275 | + const LogicalTensorPtr& result, const int& decimals = 0) | ||
| 276 | +{ | ||
| 277 | + if (cur == input.tensor.GetShape().size()) { | ||
| 278 | + auto tile = input.tensor.GetStorage()->View(function, input.tileInfo.shape, input.tileInfo.offset); | ||
| 279 | + auto resultTile = result->View(function, input.tileInfo.shape, input.tileInfo.offset); | ||
| 280 | + std::vector<int64_t> srcTileShape(input.tileInfo.shape); | ||
| 281 | + auto tileShapeLen = srcTileShape.size(); | ||
| 282 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, SHAPE_DIM1 <= tileShapeLen && tileShapeLen <= SHAPE_DIM4) | ||
| 283 | + << "Length of tile shape only supports 1~4"; | ||
| 284 | + std::vector<int64_t> tmpShape; | ||
| 285 | + if (result->Datatype() == DT_FP32) { | ||
| 286 | + tmpShape = {BLOCK_SIZE / sizeof(float)}; | ||
| 287 | + } else { | ||
| 288 | + if (srcTileShape.size() == 1) { | ||
| 289 | + tmpShape.assign(srcTileShape.end() - SHAPE_DIM1, srcTileShape.end()); | ||
| 290 | + } else { | ||
| 291 | + tmpShape.assign(srcTileShape.end() - SHAPE_DIM2, srcTileShape.end()); | ||
| 292 | + } | ||
| 293 | + auto alignSize = BLOCK_SIZE / BytesOf(DT_FP32); | ||
| 294 | + tmpShape[tmpShape.size() - 1] = (tmpShape[tmpShape.size() - 1] + alignSize - 1) / alignSize * alignSize; | ||
| 295 | + } | ||
| 296 | + auto tmpTensor = std::make_shared<LogicalTensor>(function, DT_FP32, tmpShape); | ||
| 297 | + auto& newOp = function.AddOperation(Opcode::OP_ROUND, {tile}, {resultTile, tmpTensor}); | ||
| 298 | + float powDecimals = std::pow(static_cast<float>(NUM_VALUE_10), static_cast<float>(decimals)); | ||
| 299 | + const int32_t maxFp32Len = 38; | ||
| 300 | + if (decimals > maxFp32Len) { | ||
| 301 | + powDecimals = INFINITY; | ||
| 302 | + } | ||
| 303 | + newOp.SetAttribute(OP_ATTR_PREFIX + "decimals", decimals); | ||
| 304 | + newOp.SetAttribute(OpAttributeKey::scalar, Element(DataType::DT_FP32, powDecimals)); | ||
| 305 | + return; | ||
| 306 | + } | ||
| 307 | + auto& vecTile = tileShape.GetVecTile(); | ||
| 308 | + for (int i = 0; i < input.tensor.GetShape()[cur]; i += vecTile[cur]) { | ||
| 309 | + input.tileInfo.shape[cur] = std::min(input.tensor.GetShape()[cur] - i, vecTile[cur]); | ||
| 310 | + input.tileInfo.offset[cur] = i; | ||
| 311 | + TiledRound(function, tileShape, cur + 1, input, result, decimals); | ||
| 312 | + } | ||
| 313 | +} | ||
| 314 | + | ||
| 315 | +void TiledRound(Function& function, const TileShape& tileShape, const LogicalTensorPtr& operand, | ||
| 316 | + const LogicalTensorPtr& result, const int& decimals = 0) | ||
| 317 | +{ | ||
| 318 | + CHECK(VectorErrorCode::ERR_PARAM_INVALID, operand->shape.size() == operand->offset.size()) | ||
| 319 | + << "The shape size of operand and offset must be equal"; | ||
| 320 | + | ||
| 321 | + TileInfo tileInfo(result->shape.size(), result->offset.size()); | ||
| 322 | + auto input = Input{operand, tileInfo}; | ||
| 323 | + | ||
| 324 | + TiledRound(function, tileShape, 0, input, result, decimals); | ||
| 325 | +} | ||
| 326 | + | ||
| 153 | void RsqrtOperationTileFunc(Function& function, const TileShape& tileShape, | 327 | void RsqrtOperationTileFunc(Function& function, const TileShape& tileShape, |
| 154 | const std::vector<LogicalTensorPtr>& iOperand, | 328 | const std::vector<LogicalTensorPtr>& iOperand, |
| 155 | const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | 329 | const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) |
| @@ -178,14 +352,6 @@ void SqrtOperationTileFunc(Function& function, const TileShape& tileShape, | |||
| 178 | return TiledUnaryOperation<UnaryOpType::SQRT>(function, tileShape, iOperand[0], oOperand[0], 0, precisionType); | 352 | return TiledUnaryOperation<UnaryOpType::SQRT>(function, tileShape, iOperand[0], oOperand[0], 0, precisionType); |
| 179 | } | 353 | } |
| 180 | 354 | ||
| 181 | -void BitwiseNotOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 182 | - const std::vector<LogicalTensorPtr>& iOperand, | ||
| 183 | - const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 184 | -{ | ||
| 185 | - UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 186 | - return TiledUnaryOperation<UnaryOpType::BITWISENOT>(function, tileShape, iOperand[0], oOperand[0]); | ||
| 187 | -} | ||
| 188 | - | ||
| 189 | void ReciprocalOperationTileFunc(Function& function, const TileShape& tileShape, | 355 | void ReciprocalOperationTileFunc(Function& function, const TileShape& tileShape, |
| 190 | const std::vector<LogicalTensorPtr>& iOperand, | 356 | const std::vector<LogicalTensorPtr>& iOperand, |
| 191 | const std::vector<LogicalTensorPtr>& oOperand, const Operation& op) | 357 | const std::vector<LogicalTensorPtr>& oOperand, const Operation& op) |
| @@ -213,12 +379,79 @@ void HubOperationTileFunc(Function& function, const TileShape& tileShape, const | |||
| 213 | return TiledUnaryOperation<UnaryOpType::HUB>(function, tileShape, iOperand[0], oOperand[0]); | 379 | return TiledUnaryOperation<UnaryOpType::HUB>(function, tileShape, iOperand[0], oOperand[0]); |
| 214 | } | 380 | } |
| 215 | 381 | ||
| 382 | +void IsFiniteOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 383 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 384 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 385 | +{ | ||
| 386 | + UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 387 | + auto shape = tileShape.GetVecTile().tile; | ||
| 388 | + // tileShape 对应的中间变量结果,类型为 FP16 | ||
| 389 | + uint32_t intermediateBytes = static_cast<int64_t>(BytesOf(DT_FP16)) * | ||
| 390 | + std::accumulate(shape.begin(), shape.end(), 1LL, std::multiplies<int64_t>()); | ||
| 391 | + uint32_t workspaceSize = intermediateBytes; | ||
| 392 | + return TiledUnaryOperation<UnaryOpType::ISFINITE>(function, tileShape, iOperand[0], oOperand[0], workspaceSize); | ||
| 393 | +} | ||
| 394 | + | ||
| 395 | +void IsNanOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 396 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 397 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 398 | +{ | ||
| 399 | + UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 400 | + auto tmpShape = tileShape.GetVecTile().tile; | ||
| 401 | + int dim = static_cast<int>(tmpShape.size()); | ||
| 402 | + auto alignSize = BLOCK_SIZE / BytesOf(DT_FP16); | ||
| 403 | + int64_t tmpW = AlignUp(tmpShape[dim - 1], alignSize); | ||
| 404 | + int64_t tmpH = (dim >= NUM_VALUE_2) ? tmpShape[dim - NUM_VALUE_2] : 1; | ||
| 405 | + | ||
| 406 | + constexpr int64_t kNumBlocks = NUM_VALUE_3; | ||
| 407 | + int64_t blockBytes = tmpH * tmpW * BytesOf(DT_FP32); | ||
| 408 | + uint32_t workspaceSize = kNumBlocks * blockBytes + BLOCK_SIZE; | ||
| 409 | + return TiledUnaryOperation<UnaryOpType::ISNAN>(function, tileShape, iOperand[0], oOperand[0], workspaceSize); | ||
| 410 | +} | ||
| 411 | + | ||
| 412 | +void CeilOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 413 | + const std::vector<LogicalTensorPtr>& iOperand, const std::vector<LogicalTensorPtr>& oOperand, | ||
| 414 | + [[maybe_unused]] const Operation& op) | ||
| 415 | +{ | ||
| 416 | + UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 417 | + return TiledUnaryOperation<UnaryOpType::CEIL>(function, tileShape, iOperand[0], oOperand[0]); | ||
| 418 | +} | ||
| 419 | + | ||
| 420 | +void FloorOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 421 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 422 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 423 | +{ | ||
| 424 | + UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 425 | + return TiledUnaryOperation<UnaryOpType::FLOOR>(function, tileShape, iOperand[0], oOperand[0]); | ||
| 426 | +} | ||
| 427 | + | ||
| 428 | +void TruncOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 429 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 430 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 431 | +{ | ||
| 432 | + UnaryOperationOperandCheck(iOperand, oOperand); | ||
| 433 | + return TiledUnaryOperation<UnaryOpType::TRUNC>(function, tileShape, iOperand[0], oOperand[0]); | ||
| 434 | +} | ||
| 435 | + | ||
| 436 | +void RoundOperationTileFunc(Function& function, const TileShape& tileShape, | ||
| 437 | + const std::vector<LogicalTensorPtr>& iOperand, | ||
| 438 | + const std::vector<LogicalTensorPtr>& oOperand, [[maybe_unused]] const Operation& op) | ||
| 439 | +{ | ||
| 440 | + int decimals = op.GetIntAttribute(OP_ATTR_PREFIX + "decimals"); | ||
| 441 | + TiledRound(function, tileShape, iOperand[0], oOperand[0], decimals); | ||
| 442 | +} | ||
| 443 | + | ||
| 216 | REGISTER_OPERATION_TILED_FUNC(OP_RSQRT, Opcode::OP_RSQRT, RsqrtOperationTileFunc); | 444 | REGISTER_OPERATION_TILED_FUNC(OP_RSQRT, Opcode::OP_RSQRT, RsqrtOperationTileFunc); |
| 217 | REGISTER_OPERATION_TILED_FUNC(OP_RELU, Opcode::OP_RELU, ReluOperationTileFunc); | 445 | REGISTER_OPERATION_TILED_FUNC(OP_RELU, Opcode::OP_RELU, ReluOperationTileFunc); |
| 218 | REGISTER_OPERATION_TILED_FUNC(OP_SQRT, Opcode::OP_SQRT, SqrtOperationTileFunc); | 446 | REGISTER_OPERATION_TILED_FUNC(OP_SQRT, Opcode::OP_SQRT, SqrtOperationTileFunc); |
| 219 | -REGISTER_OPERATION_TILED_FUNC(OP_BITWISENOT, Opcode::OP_BITWISENOT, BitwiseNotOperationTileFunc); | ||
| 220 | REGISTER_OPERATION_TILED_FUNC(OP_RECIPROCAL, Opcode::OP_RECIPROCAL, ReciprocalOperationTileFunc); | 447 | REGISTER_OPERATION_TILED_FUNC(OP_RECIPROCAL, Opcode::OP_RECIPROCAL, ReciprocalOperationTileFunc); |
| 221 | REGISTER_OPERATION_TILED_FUNC(OP_ABS, Opcode::OP_ABS, AbsOperationTileFunc); | 448 | REGISTER_OPERATION_TILED_FUNC(OP_ABS, Opcode::OP_ABS, AbsOperationTileFunc); |
| 222 | REGISTER_OPERATION_TILED_FUNC(OP_HUB, Opcode::OP_HUB, HubOperationTileFunc); | 449 | REGISTER_OPERATION_TILED_FUNC(OP_HUB, Opcode::OP_HUB, HubOperationTileFunc); |
| 450 | +REGISTER_OPERATION_TILED_FUNC(OP_ISFINITE, Opcode::OP_ISFINITE, IsFiniteOperationTileFunc); | ||
| 451 | +REGISTER_OPERATION_TILED_FUNC(OP_ISNAN, Opcode::OP_ISNAN, IsNanOperationTileFunc); | ||
| 452 | +REGISTER_OPERATION_TILED_FUNC(OP_CEIL, Opcode::OP_CEIL, CeilOperationTileFunc); | ||
| 453 | +REGISTER_OPERATION_TILED_FUNC(OP_FLOOR, Opcode::OP_FLOOR, FloorOperationTileFunc); | ||
| 454 | +REGISTER_OPERATION_TILED_FUNC(OP_TRUNC, Opcode::OP_TRUNC, TruncOperationTileFunc); | ||
| 455 | +REGISTER_OPERATION_TILED_FUNC(OP_ROUND, Opcode::OP_ROUND, RoundOperationTileFunc); | ||
| 223 | 456 | ||
| 224 | } // namespace npu::tile_fwk | 457 | } // namespace npu::tile_fwk |
| @@ -1,34 +0,0 @@ | |||
| 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 unary_utils.h | ||
| 13 | - * \brief | ||
| 14 | - */ | ||
| 15 | - | ||
| 16 | -#pragma once | ||
| 17 | - | ||
| 18 | -#include "unary.h" | ||
| 19 | - | ||
| 20 | -namespace npu::tile_fwk { | ||
| 21 | - | ||
| 22 | -template <typename T> | ||
| 23 | -int64_t MultiplyLastTwoDims(const std::vector<int64_t>& vec) | ||
| 24 | -{ | ||
| 25 | - constexpr size_t ALIGN_SIZE = NUM_VALUE_32; | ||
| 26 | - constexpr size_t ELEMENT_SIZE = sizeof(T); | ||
| 27 | - constexpr size_t ALIGN_ELEMENTS = ALIGN_SIZE / ELEMENT_SIZE; | ||
| 28 | - int64_t axis2 = (vec[vec.size() - 1] + ALIGN_ELEMENTS - 1) / ALIGN_ELEMENTS * ALIGN_ELEMENTS; | ||
| 29 | - return axis2 * vec[vec.size() - NUM_VALUE_2]; | ||
| 30 | -} | ||
| 31 | - | ||
| 32 | -int64_t CmpResAlign(const std::vector<int64_t>& vec); | ||
| 33 | - | ||
| 34 | -} // namespace npu::tile_fwk | ||
| @@ -95,6 +95,7 @@ enum class BroadcastOperand : int64_t { | |||
| 95 | }; | 95 | }; |
| 96 | 96 | ||
| 97 | constexpr uint64_t MASK_LEN = 64; | 97 | constexpr uint64_t MASK_LEN = 64; |
| 98 | +constexpr uint64_t BITS_PER_BYTE = 8; | ||
| 98 | constexpr uint64_t BLOCK_NELEM_B16 = 16; | 99 | constexpr uint64_t BLOCK_NELEM_B16 = 16; |
| 99 | constexpr uint64_t BLOCK_NELEM_B32 = 8; | 100 | constexpr uint64_t BLOCK_NELEM_B32 = 8; |
| 100 | constexpr uint64_t NBLOCK_PER_MASK_B16 = 4; | 101 | constexpr uint64_t NBLOCK_PER_MASK_B16 = 4; |
| @@ -35,6 +35,9 @@ TILEOP void AtanCalc(DST dst, SRC src, TMP1 tmp1, TMP2 tmp2, CMP cmp) | |||
| 35 | { | 35 | { |
| 36 | constexpr float a[] = {-0.333329409, 0.199887753, -0.141718030, 0.105184801, | 36 | constexpr float a[] = {-0.333329409, 0.199887753, -0.141718030, 0.105184801, |
| 37 | -0.0725297481, 0.0398497507, -0.0143969795, 0.00245002890}; | 37 | -0.0725297481, 0.0398497507, -0.0143969795, 0.00245002890}; |
| 38 | + constexpr int POLY_LAST_INDEX = 7; | ||
| 39 | + constexpr int POLY_SECOND_LAST_INDEX = POLY_LAST_INDEX - 1; | ||
| 40 | + constexpr int HORNER_START_INDEX = POLY_SECOND_LAST_INDEX - 1; | ||
| 38 | constexpr float pi2 = 1.570796326794896619; | 41 | constexpr float pi2 = 1.570796326794896619; |
| 39 | pto::TABS(tmp1, src); | 42 | pto::TABS(tmp1, src); |
| 40 | pto::TEXPANDS(dst, 1.0); | 43 | pto::TEXPANDS(dst, 1.0); |
| @@ -46,11 +49,11 @@ TILEOP void AtanCalc(DST dst, SRC src, TMP1 tmp1, TMP2 tmp2, CMP cmp) | |||
| 46 | SyncV(); | 49 | SyncV(); |
| 47 | pto::TMUL(tmp1, tmp2, tmp2); | 50 | pto::TMUL(tmp1, tmp2, tmp2); |
| 48 | SyncV(); | 51 | SyncV(); |
| 49 | - pto::TMULS(dst, tmp1, a[7]); | 52 | + pto::TMULS(dst, tmp1, a[POLY_LAST_INDEX]); |
| 50 | SyncV(); | 53 | SyncV(); |
| 51 | - pto::TADDS(dst, dst, a[6]); | 54 | + pto::TADDS(dst, dst, a[POLY_SECOND_LAST_INDEX]); |
| 52 | SyncV(); | 55 | SyncV(); |
| 53 | - for (int i = 5; i >= 0; --i) { | 56 | + for (int i = HORNER_START_INDEX; i >= 0; --i) { |
| 54 | pto::TMUL(dst, dst, tmp1); | 57 | pto::TMUL(dst, dst, tmp1); |
| 55 | SyncV(); | 58 | SyncV(); |
| 56 | pto::TADDS(dst, dst, a[i]); | 59 | pto::TADDS(dst, dst, a[i]); |
| @@ -50,6 +50,9 @@ TILEOP void TAtanh(T0 dst, T1 src, T2 tmp) | |||
| 50 | DataTileDefine tmp2Tile(dstShape3, dstShape4); | 50 | DataTileDefine tmp2Tile(dstShape3, dstShape4); |
| 51 | DataTileDefine tmp3Tile(dstShape3, dstShape4); | 51 | DataTileDefine tmp3Tile(dstShape3, dstShape4); |
| 52 | MaskTileDefine tmpMaskTile(dstShape3, dstShape4); | 52 | MaskTileDefine tmpMaskTile(dstShape3, dstShape4); |
| 53 | + constexpr size_t TMP2_SLOT = 2; | ||
| 54 | + constexpr size_t TMP3_SLOT = 3; | ||
| 55 | + constexpr size_t MASK_SLOT = 4; | ||
| 53 | 56 | ||
| 54 | for (LoopVar n0Index = 0; n0Index < dstShape0; n0Index++) { | 57 | for (LoopVar n0Index = 0; n0Index < dstShape0; n0Index++) { |
| 55 | for (LoopVar n1Index = 0; n1Index < dstShape1; n1Index++) { | 58 | for (LoopVar n1Index = 0; n1Index < dstShape1; n1Index++) { |
| @@ -62,9 +65,12 @@ TILEOP void TAtanh(T0 dst, T1 src, T2 tmp) | |||
| 62 | 65 | ||
| 63 | pto::TASSIGN(tmp0Tile, (uint64_t)(tmp.GetAddr() + tmpByteOffset)); | 66 | pto::TASSIGN(tmp0Tile, (uint64_t)(tmp.GetAddr() + tmpByteOffset)); |
| 64 | pto::TASSIGN(tmp1Tile, (uint64_t)(tmp.GetAddr() + tmpByteOffset + tileShapeSize * dstTypeSize)); | 67 | pto::TASSIGN(tmp1Tile, (uint64_t)(tmp.GetAddr() + tmpByteOffset + tileShapeSize * dstTypeSize)); |
| 65 | - pto::TASSIGN(tmp2Tile, (uint64_t)(tmp.GetAddr() + tmpByteOffset + 2 * tileShapeSize * dstTypeSize)); | 68 | + pto::TASSIGN(tmp2Tile, |
| 66 | - pto::TASSIGN(tmp3Tile, (uint64_t)(tmp.GetAddr() + tmpByteOffset + 3 * tileShapeSize * dstTypeSize)); | 69 | + (uint64_t)(tmp.GetAddr() + tmpByteOffset + TMP2_SLOT * tileShapeSize * dstTypeSize)); |
| 67 | - pto::TASSIGN(tmpMaskTile, (uint64_t)(tmp.GetAddr() + tmpByteOffset + 4 * tileShapeSize * dstTypeSize)); | 70 | + pto::TASSIGN(tmp3Tile, |
| 71 | + (uint64_t)(tmp.GetAddr() + tmpByteOffset + TMP3_SLOT * tileShapeSize * dstTypeSize)); | ||
| 72 | + pto::TASSIGN(tmpMaskTile, | ||
| 73 | + (uint64_t)(tmp.GetAddr() + tmpByteOffset + MASK_SLOT * tileShapeSize * dstTypeSize)); | ||
| 68 | 74 | ||
| 69 | // atanh(x) = 0.5 * ln((1 + x) / (1 - x)) | 75 | // atanh(x) = 0.5 * ln((1 + x) / (1 - x)) |
| 70 | pto::TABS(tmp3Tile, srcExecTile); | 76 | pto::TABS(tmp3Tile, srcExecTile); |
| @@ -19,5 +19,6 @@ | |||
| 19 | #include "binary/basic.h" | 19 | #include "binary/basic.h" |
| 20 | #include "binary/extended.h" | 20 | #include "binary/extended.h" |
| 21 | #include "binary/floor_div.h" | 21 | #include "binary/floor_div.h" |
| 22 | +#include "binary/gcd.h" | ||
| 22 | 23 | ||
| 23 | #endif // TILEOP_TILE_OPERATOR_BINARY_H | 24 | #endif // TILEOP_TILE_OPERATOR_BINARY_H |
| @@ -295,312 +295,6 @@ TILEOP void TBitwiseOr(T0 dst, T1 src0, T2 src1) | |||
| 295 | BinaryCompute<BinaryOp::BITWISEOR, 0, LastUse, BrcOperands...>(dst, src0, src1); | 295 | BinaryCompute<BinaryOp::BITWISEOR, 0, LastUse, BrcOperands...>(dst, src0, src1); |
| 296 | } | 296 | } |
| 297 | 297 | ||
| 298 | -#define OP_TILE_OP_GCD TGcd | ||
| 299 | -template <int... BrcOperands, typename T0, typename T1, typename T2, typename T3> | ||
| 300 | -TILEOP void TGcd(T0 dst, T1 src0, T2 src1, T3 temp) | ||
| 301 | -{ | ||
| 302 | - // 计算位宽按输入类型选择:int8/uint8 输入时中间值 <= 510(q*b <= a+b),int16 槽不溢出; | ||
| 303 | - // int16/int32 输入时 q*b 可达 a+b <= 65534/2e8,需 int32 槽。 | ||
| 304 | - using CalcType = typename std::conditional<Std::is_same_v<typename T0::Type, int8_t> || | ||
| 305 | - Std::is_same_v<typename T0::Type, uint8_t>, | ||
| 306 | - int16_t, int32_t>::type; | ||
| 307 | - constexpr auto tileH = TileOp::GetTensorTileShapeDim<T0, DIM_4TH, MAX_DIMS>(); | ||
| 308 | - constexpr auto tileW = TileOp::GetTensorTileShapeDim<T0, DIM_5TH, MAX_DIMS>(); | ||
| 309 | - using CalcTile = pto::Tile<pto::TileType::Vec, CalcType, tileH, tileW, pto::BLayout::RowMajor, -1, -1>; | ||
| 310 | - using TTile = pto::Tile<pto::TileType::Vec, typename T0::Type, tileH, tileW, pto::BLayout::RowMajor, -1, -1>; | ||
| 311 | - using FTile = pto::Tile<pto::TileType::Vec, float, tileH, tileW, pto::BLayout::RowMajor, -1, -1>; | ||
| 312 | - using F16Tile = pto::Tile<pto::TileType::Vec, half, tileH, tileW, pto::BLayout::RowMajor, -1, -1>; | ||
| 313 | - // TCMPS/TCMP/TSEL 的掩码按位打包存放:每行 ceil(tileW/32)*4 字节,按 32B 对齐 | ||
| 314 | - constexpr auto maskCols = ((tileW + 7) / 8 + 31) / 32 * 32; | ||
| 315 | - using MaskTile = pto::Tile<pto::TileType::Vec, uint8_t, tileH, maskCols, pto::BLayout::RowMajor, -1, -1>; | ||
| 316 | - | ||
| 317 | - const auto dstLayout = dst.GetLayout(); | ||
| 318 | - const auto src0Layout = src0.GetLayout(); | ||
| 319 | - const auto src1Layout = src1.GetLayout(); | ||
| 320 | - auto shape0 = dstLayout.template GetShapeDim<DIM_1ST, MAX_DIMS>(); | ||
| 321 | - auto shape1 = dstLayout.template GetShapeDim<DIM_2ND, MAX_DIMS>(); | ||
| 322 | - auto shape2 = dstLayout.template GetShapeDim<DIM_3RD, MAX_DIMS>(); | ||
| 323 | - auto shape3 = dstLayout.template GetShapeDim<DIM_4TH, MAX_DIMS>(); | ||
| 324 | - auto shape4 = dstLayout.template GetShapeDim<DIM_5TH, MAX_DIMS>(); | ||
| 325 | - if (shape0 == 0 || shape1 == 0 || shape2 == 0) { | ||
| 326 | - return; | ||
| 327 | - } | ||
| 328 | - using Src0TileInfo = TensorTileInfo<T1>; | ||
| 329 | - using Src1TileInfo = TensorTileInfo<T2>; | ||
| 330 | - constexpr BrcMode brcmode = GetBrcMode<BrcOperands...>(); | ||
| 331 | - | ||
| 332 | - // temp 布局(与 BinaryOperationTileFunc 中 GCD 的临时空间分配保持一致): | ||
| 333 | - // slot0: a = |src0| slot1: b = |src1| slot2: r = a % b slot3: q | ||
| 334 | - // slot4: 暂存,顺序复用为 q*b / q修正 / r修正(生命周期互不重叠) | ||
| 335 | - // f/fb: 两个 f32 槽(近似商与 refine 除法) | ||
| 336 | - // 之后依次为:冻结掩码、修正掩码、TSEL tmp(256B+64B 保护带) | ||
| 337 | - // 计算槽按 CalcType 位宽(int8/uint8 输入为 int16),f32 槽与掩码固定大小 | ||
| 338 | - constexpr auto calcStride = tileH * tileW * sizeof(CalcType); | ||
| 339 | - constexpr auto floatBytes = tileH * tileW * sizeof(float); | ||
| 340 | - constexpr auto maskBytes = tileH * maskCols; | ||
| 341 | - constexpr auto fBase = 6 * calcStride; | ||
| 342 | - constexpr auto fbBase = fBase + floatBytes; | ||
| 343 | - constexpr auto maskBase = fbBase + floatBytes; | ||
| 344 | - constexpr auto bNegBase = maskBase + maskBytes; | ||
| 345 | - constexpr auto oppBase = bNegBase + maskBytes; | ||
| 346 | - constexpr auto fixMaskBase = oppBase + maskBytes; | ||
| 347 | - constexpr auto selTmpBase = fixMaskBase + maskBytes; | ||
| 348 | - | ||
| 349 | - CalcTile aTile(shape3, shape4); | ||
| 350 | - CalcTile bTile(shape3, shape4); | ||
| 351 | - CalcTile rTile(shape3, shape4); | ||
| 352 | - CalcTile qTile(shape3, shape4); | ||
| 353 | - CalcTile scratchTile(shape3, shape4); | ||
| 354 | - CalcTile auxTile(shape3, shape4); | ||
| 355 | - FTile fTile(shape3, shape4); | ||
| 356 | - FTile fbTile(shape3, shape4); | ||
| 357 | - MaskTile maskTile(shape3, shape4); | ||
| 358 | - MaskTile bNegTile(shape3, shape4); | ||
| 359 | - MaskTile oppTile(shape3, shape4); | ||
| 360 | - MaskTile fixMaskTile(shape3, shape4); | ||
| 361 | - CalcTile selTmpTile(shape3, shape4); | ||
| 362 | - TTile src0Tile(shape3, shape4); | ||
| 363 | - TTile src1Tile(shape3, shape4); | ||
| 364 | - TTile dstTile(shape3, shape4); | ||
| 365 | - | ||
| 366 | - TASSIGN(aTile, temp.GetAddr()); | ||
| 367 | - TASSIGN(bTile, temp.GetAddr() + calcStride); | ||
| 368 | - TASSIGN(rTile, temp.GetAddr() + 2 * calcStride); | ||
| 369 | - TASSIGN(qTile, temp.GetAddr() + 3 * calcStride); | ||
| 370 | - TASSIGN(scratchTile, temp.GetAddr() + 4 * calcStride); | ||
| 371 | - TASSIGN(auxTile, temp.GetAddr() + 5 * calcStride); | ||
| 372 | - TASSIGN(fTile, temp.GetAddr() + fBase); | ||
| 373 | - TASSIGN(fbTile, temp.GetAddr() + fbBase); | ||
| 374 | - TASSIGN(maskTile, temp.GetAddr() + maskBase); | ||
| 375 | - TASSIGN(bNegTile, temp.GetAddr() + bNegBase); | ||
| 376 | - TASSIGN(oppTile, temp.GetAddr() + oppBase); | ||
| 377 | - TASSIGN(fixMaskTile, temp.GetAddr() + fixMaskBase); | ||
| 378 | - TASSIGN(selTmpTile, temp.GetAddr() + selTmpBase); | ||
| 379 | - | ||
| 380 | - for (LoopVar n0Index = 0; n0Index < shape0; ++n0Index) { | ||
| 381 | - for (LoopVar n1Index = 0; n1Index < shape1; ++n1Index) { | ||
| 382 | - for (LoopVar n2Index = 0; n2Index < shape2; ++n2Index) { | ||
| 383 | - auto tileOffsets = TileOffset(n0Index, n1Index, n2Index); | ||
| 384 | - auto dstOffset = GenTileOffset(dst, tileOffsets); | ||
| 385 | - auto src0tileOffsets = TileOffset( | ||
| 386 | - (Src0TileInfo::tile0 == 1 || GetBrcOperandAt<DIM_1ST, BrcOperands...>() == BRC_LEFT) ? 0 : n0Index, | ||
| 387 | - (Src0TileInfo::tile1 == 1 || GetBrcOperandAt<DIM_2ND, BrcOperands...>() == BRC_LEFT) ? 0 : n1Index, | ||
| 388 | - (Src0TileInfo::tile2 == 1 || GetBrcOperandAt<DIM_3RD, BrcOperands...>() == BRC_LEFT) ? 0 : n2Index); | ||
| 389 | - auto src1tileOffsets = TileOffset( | ||
| 390 | - (Src1TileInfo::tile0 == 1 || GetBrcOperandAt<DIM_1ST, BrcOperands...>() == BRC_RIGHT) ? 0 : n0Index, | ||
| 391 | - (Src1TileInfo::tile1 == 1 || GetBrcOperandAt<DIM_2ND, BrcOperands...>() == BRC_RIGHT) ? 0 : n1Index, | ||
| 392 | - (Src1TileInfo::tile2 == 1 || GetBrcOperandAt<DIM_3RD, BrcOperands...>() == BRC_RIGHT) ? 0 : | ||
| 393 | - n2Index); | ||
| 394 | - auto src0Offset = GenTileOffset(src0, src0tileOffsets); | ||
| 395 | - auto src1Offset = GenTileOffset(src1, src1tileOffsets); | ||
| 396 | - TASSIGN(dstTile, (uint64_t)(dst.GetAddr() + dstOffset * sizeof(typename T0::Type))); | ||
| 397 | - TASSIGN(src0Tile, (uint64_t)(src0.GetAddr() + src0Offset * sizeof(typename T1::Type))); | ||
| 398 | - TASSIGN(src1Tile, (uint64_t)(src1.GetAddr() + src1Offset * sizeof(typename T2::Type))); | ||
| 399 | - | ||
| 400 | - // 统一转换到计算位宽。a2/a3 的 TCVT 没有 int8/uint8/int16 到 int32 的 | ||
| 401 | - // 直接转换,int16 经 f32、int8/uint8 经 f16 中转(小整数转换无精度损失) | ||
| 402 | - if constexpr (Std::is_same_v<typename T0::Type, int32_t>) { | ||
| 403 | - pto::TMOV(aTile, src0Tile); | ||
| 404 | - pto::TMOV(bTile, src1Tile); | ||
| 405 | - } else if constexpr (Std::is_same_v<typename T0::Type, int16_t>) { | ||
| 406 | - pto::TCVT(fTile, src0Tile, pto::RoundMode::CAST_NONE); | ||
| 407 | - pto::TCVT(fbTile, src1Tile, pto::RoundMode::CAST_NONE); | ||
| 408 | - SyncV(); | ||
| 409 | - pto::TCVT(aTile, fTile, pto::RoundMode::CAST_RINT); | ||
| 410 | - pto::TCVT(bTile, fbTile, pto::RoundMode::CAST_RINT); | ||
| 411 | - } else { | ||
| 412 | - // int8/uint8: s8/u8 -> f16 -> s16(f16 中转视图复用 fbTile 的地址) | ||
| 413 | - F16Tile f16Tile(shape3, shape4); | ||
| 414 | - TASSIGN(f16Tile, temp.GetAddr() + fbBase); | ||
| 415 | - pto::TCVT(f16Tile, src0Tile, pto::RoundMode::CAST_NONE); | ||
| 416 | - SyncV(); | ||
| 417 | - pto::TCVT(aTile, f16Tile, pto::RoundMode::CAST_RINT); | ||
| 418 | - SyncV(); | ||
| 419 | - pto::TCVT(f16Tile, src1Tile, pto::RoundMode::CAST_NONE); | ||
| 420 | - SyncV(); | ||
| 421 | - pto::TCVT(bTile, f16Tile, pto::RoundMode::CAST_RINT); | ||
| 422 | - } | ||
| 423 | - SyncV(); | ||
| 424 | - | ||
| 425 | - // |x| = x * clamp(x, -1, 1) | ||
| 426 | - pto::TMINS(rTile, aTile, 1); | ||
| 427 | - SyncV(); | ||
| 428 | - pto::TMAXS(rTile, rTile, -1); | ||
| 429 | - SyncV(); | ||
| 430 | - pto::TMUL(aTile, aTile, rTile); | ||
| 431 | - SyncV(); | ||
| 432 | - pto::TMINS(rTile, bTile, 1); | ||
| 433 | - SyncV(); | ||
| 434 | - pto::TMAXS(rTile, rTile, -1); | ||
| 435 | - SyncV(); | ||
| 436 | - pto::TMUL(bTile, bTile, rTile); | ||
| 437 | - SyncV(); | ||
| 438 | - if constexpr (Std::is_same_v<typename T0::Type, int16_t>) { | ||
| 439 | - // int16 回绕: -32768 的绝对值 32768 回绕为 -32768(与 torch.gcd 一致) | ||
| 440 | - pto::TCVT(fTile, aTile, pto::RoundMode::CAST_NONE); | ||
| 441 | - SyncV(); | ||
| 442 | - pto::TCMPS(maskTile, fTile, 32768.0f, pto::CmpMode::GE); | ||
| 443 | - SyncV(); | ||
| 444 | - pto::TADDS(scratchTile, aTile, -65536); | ||
| 445 | - SyncV(); | ||
| 446 | - pto::TSEL(aTile, maskTile, scratchTile, aTile, selTmpTile); | ||
| 447 | - SyncV(); | ||
| 448 | - pto::TCVT(fTile, bTile, pto::RoundMode::CAST_NONE); | ||
| 449 | - SyncV(); | ||
| 450 | - pto::TCMPS(maskTile, fTile, 32768.0f, pto::CmpMode::GE); | ||
| 451 | - SyncV(); | ||
| 452 | - pto::TADDS(scratchTile, bTile, -65536); | ||
| 453 | - SyncV(); | ||
| 454 | - pto::TSEL(bTile, maskTile, scratchTile, bTile, selTmpTile); | ||
| 455 | - SyncV(); | ||
| 456 | - } else if constexpr (Std::is_same_v<typename T0::Type, int8_t>) { | ||
| 457 | - // int8 回绕: -128 的绝对值 128 回绕为 -128(与 torch.gcd 一致) | ||
| 458 | - pto::TCVT(fTile, aTile, pto::RoundMode::CAST_NONE); | ||
| 459 | - SyncV(); | ||
| 460 | - pto::TCMPS(maskTile, fTile, 128.0f, pto::CmpMode::GE); | ||
| 461 | - SyncV(); | ||
| 462 | - pto::TADDS(scratchTile, aTile, -256); | ||
| 463 | - SyncV(); | ||
| 464 | - pto::TSEL(aTile, maskTile, scratchTile, aTile, selTmpTile); | ||
| 465 | - SyncV(); | ||
| 466 | - pto::TCVT(fTile, bTile, pto::RoundMode::CAST_NONE); | ||
| 467 | - SyncV(); | ||
| 468 | - pto::TCMPS(maskTile, fTile, 128.0f, pto::CmpMode::GE); | ||
| 469 | - SyncV(); | ||
| 470 | - pto::TADDS(scratchTile, bTile, -256); | ||
| 471 | - SyncV(); | ||
| 472 | - pto::TSEL(bTile, maskTile, scratchTile, bTile, selTmpTile); | ||
| 473 | - SyncV(); | ||
| 474 | - } | ||
| 475 | - | ||
| 476 | - // Euclid 迭代:固定迭代次数,避免仿真器上 while + GetValue 的标量同步竞态(卡死)。 | ||
| 477 | - // b == 0 的 lane 用掩码冻结,收敛后保持 (gcd, 0) 不变;除零时 vdiv 结果 | ||
| 478 | - // 为 inf/0,商饱和后与 0 相乘仍得 0,余数保持 a,冻结逻辑不受影响。 | ||
| 479 | - // 最大迭代次数:int32 <= 1e8 时 Euclid 步数 <= 40(Fibonacci 界),64 封顶; | ||
| 480 | - // int16 <= 32767 时 <= 23,32 封顶;int8/uint8 <= 255 时 <= 13,16 封顶。 | ||
| 481 | - constexpr int kMaxIter = Std::is_same_v<typename T0::Type, int32_t> ? | ||
| 482 | - 64 : | ||
| 483 | - (Std::is_same_v<typename T0::Type, int16_t> ? 32 : 16); | ||
| 484 | - for (LoopVar k = 0; k < kMaxIter; ++k) { | ||
| 485 | - // 冻结 b == 0 的 lane,避免除零 | ||
| 486 | - pto::TCMPS(maskTile, bTile, 0, pto::CmpMode::EQ); | ||
| 487 | - SyncV(); | ||
| 488 | - // 符号归一化副本(a/b 原值不动,链更新用原始符号值): | ||
| 489 | - // aDiv = bNeg ? -a : a(aux);bDiv = bNeg ? -b : b(rTile,bDiv > 0) | ||
| 490 | - pto::TCVT(fbTile, bTile, pto::RoundMode::CAST_NONE); | ||
| 491 | - SyncV(); | ||
| 492 | - pto::TCMPS(bNegTile, fbTile, 0.0f, pto::CmpMode::LT); | ||
| 493 | - SyncV(); | ||
| 494 | - pto::TMULS(auxTile, aTile, -1); | ||
| 495 | - SyncV(); | ||
| 496 | - pto::TSEL(auxTile, bNegTile, auxTile, aTile, selTmpTile); | ||
| 497 | - SyncV(); | ||
| 498 | - pto::TMULS(rTile, bTile, -1); | ||
| 499 | - SyncV(); | ||
| 500 | - pto::TSEL(rTile, bNegTile, rTile, bTile, selTmpTile); | ||
| 501 | - SyncV(); | ||
| 502 | - // floored 精确余数(bDiv > 0,aDiv 任意符号,结果 r_f = aux ∈ [0, bDiv)) | ||
| 503 | - pto::TCVT(fbTile, rTile, pto::RoundMode::CAST_NONE); | ||
| 504 | - SyncV(); | ||
| 505 | - pto::TCVT(fTile, auxTile, pto::RoundMode::CAST_NONE); | ||
| 506 | - SyncV(); | ||
| 507 | - pto::TDIV<pto::DivAlgorithm::HIGH_PRECISION>(fTile, fTile, fbTile); | ||
| 508 | - SyncV(); | ||
| 509 | - pto::TCVT(qTile, fTile, pto::RoundMode::CAST_FLOOR); | ||
| 510 | - SyncV(); | ||
| 511 | - pto::TMUL(scratchTile, qTile, rTile); | ||
| 512 | - SyncV(); | ||
| 513 | - pto::TSUB(auxTile, auxTile, scratchTile); // aux = r = aDiv - q*bDiv | ||
| 514 | - SyncV(); | ||
| 515 | - pto::TCVT(fTile, auxTile, pto::RoundMode::CAST_NONE); | ||
| 516 | - SyncV(); | ||
| 517 | - pto::TDIV<pto::DivAlgorithm::HIGH_PRECISION>(fTile, fTile, fbTile); | ||
| 518 | - SyncV(); | ||
| 519 | - pto::TCVT(scratchTile, fTile, pto::RoundMode::CAST_FLOOR); // scratch = qc | ||
| 520 | - SyncV(); | ||
| 521 | - // r_new = r_old - qc * bDiv(aux 已是 r_old,不能从 aDiv 重算) | ||
| 522 | - pto::TMUL(scratchTile, scratchTile, rTile); | ||
| 523 | - SyncV(); | ||
| 524 | - pto::TSUB(auxTile, auxTile, scratchTile); | ||
| 525 | - SyncV(); | ||
| 526 | - // floored +/-1 修正(f32 符号桥接,bDiv > 0) | ||
| 527 | - pto::TSUB(scratchTile, auxTile, rTile); | ||
| 528 | - SyncV(); | ||
| 529 | - pto::TCVT(fTile, scratchTile, pto::RoundMode::CAST_NONE); | ||
| 530 | - SyncV(); | ||
| 531 | - pto::TCMPS(fixMaskTile, fTile, 0.0f, pto::CmpMode::GE); | ||
| 532 | - SyncV(); | ||
| 533 | - pto::TSEL(auxTile, fixMaskTile, scratchTile, auxTile, selTmpTile); | ||
| 534 | - SyncV(); | ||
| 535 | - pto::TADD(scratchTile, auxTile, rTile); | ||
| 536 | - SyncV(); | ||
| 537 | - pto::TCVT(fTile, auxTile, pto::RoundMode::CAST_NONE); | ||
| 538 | - SyncV(); | ||
| 539 | - pto::TCMPS(fixMaskTile, fTile, 0.0f, pto::CmpMode::LT); | ||
| 540 | - SyncV(); | ||
| 541 | - pto::TSEL(auxTile, fixMaskTile, scratchTile, auxTile, selTmpTile); | ||
| 542 | - SyncV(); | ||
| 543 | - // r_f == 0 标记(守卫用) | ||
| 544 | - pto::TCVT(fTile, auxTile, pto::RoundMode::CAST_NONE); | ||
| 545 | - SyncV(); | ||
| 546 | - pto::TCMPS(fixMaskTile, fTile, 0.0f, pto::CmpMode::EQ); | ||
| 547 | - SyncV(); | ||
| 548 | - // 转换为 C 截断余数(用原始 a/b 的符号): | ||
| 549 | - // opp = (f32(a) * f32(b) < 0)(原始异号,乘积符号精确) | ||
| 550 | - // r_trunc = opp ? (bDiv - r_f) : (bNeg ? -r_f : r_f) | ||
| 551 | - pto::TCVT(fTile, aTile, pto::RoundMode::CAST_NONE); | ||
| 552 | - SyncV(); | ||
| 553 | - pto::TCVT(fbTile, bTile, pto::RoundMode::CAST_NONE); | ||
| 554 | - SyncV(); | ||
| 555 | - pto::TMUL(fTile, fTile, fbTile); | ||
| 556 | - SyncV(); | ||
| 557 | - pto::TCMPS(oppTile, fTile, 0.0f, pto::CmpMode::LT); | ||
| 558 | - SyncV(); | ||
| 559 | - pto::TMULS(qTile, auxTile, -1); // qTile = -r_f(q 已死) | ||
| 560 | - SyncV(); | ||
| 561 | - pto::TSUB(scratchTile, rTile, auxTile); // bDiv - r_f(bNeg 分支) | ||
| 562 | - SyncV(); | ||
| 563 | - pto::TSUB(rTile, auxTile, rTile); // r_f - bDiv(非 bNeg 分支, bDiv 已死) | ||
| 564 | - SyncV(); | ||
| 565 | - pto::TSEL(scratchTile, bNegTile, scratchTile, rTile, selTmpTile); // rOpp | ||
| 566 | - SyncV(); | ||
| 567 | - pto::TSEL(qTile, bNegTile, qTile, auxTile, selTmpTile); // rBase | ||
| 568 | - SyncV(); | ||
| 569 | - pto::TSEL(auxTile, oppTile, scratchTile, qTile, selTmpTile); // r = opp ? rOpp : rBase | ||
| 570 | - SyncV(); | ||
| 571 | - // r_f == 0 时保持 0(±b 毛刺会翻转最终符号; 零 tile 用自减生成) | ||
| 572 | - pto::TSUB(scratchTile, auxTile, auxTile); // 0 | ||
| 573 | - SyncV(); | ||
| 574 | - pto::TSEL(scratchTile, fixMaskTile, scratchTile, auxTile, selTmpTile); | ||
| 575 | - SyncV(); | ||
| 576 | - pto::TMOV(auxTile, scratchTile); | ||
| 577 | - SyncV(); | ||
| 578 | - // 冻结 lane:a, b 保持不变(原始符号值),其余 a = b, b = a % b | ||
| 579 | - pto::TSEL(aTile, maskTile, aTile, bTile, selTmpTile); | ||
| 580 | - SyncV(); | ||
| 581 | - pto::TSEL(bTile, maskTile, bTile, auxTile, selTmpTile); | ||
| 582 | - SyncV(); | ||
| 583 | - } // 结果取非负 gcd(与 torch.gcd 语义一致) | ||
| 584 | - if constexpr (Std::is_same_v<typename T0::Type, int32_t>) { | ||
| 585 | - pto::TMOV(dstTile, aTile); | ||
| 586 | - } else if constexpr (Std::is_same_v<typename T0::Type, int16_t>) { | ||
| 587 | - pto::TCVT(dstTile, aTile, pto::RoundMode::CAST_RINT); | ||
| 588 | - } else { | ||
| 589 | - // int8/uint8: s16 -> f32 -> f16 -> s8/u8(a2/a3 无直接转换) | ||
| 590 | - F16Tile f16Tile(shape3, shape4); | ||
| 591 | - TASSIGN(f16Tile, temp.GetAddr() + fbBase); | ||
| 592 | - pto::TCVT(fTile, aTile, pto::RoundMode::CAST_RINT); | ||
| 593 | - SyncV(); | ||
| 594 | - pto::TCVT(f16Tile, fTile, pto::RoundMode::CAST_RINT); | ||
| 595 | - SyncV(); | ||
| 596 | - pto::TCVT(dstTile, f16Tile, pto::RoundMode::CAST_RINT); | ||
| 597 | - } | ||
| 598 | - SyncV(); | ||
| 599 | - } | ||
| 600 | - } | ||
| 601 | - } | ||
| 602 | -} | ||
| 603 | - | ||
| 604 | #define OP_TILE_OP_Mod TMod | 298 | #define OP_TILE_OP_Mod TMod |
| 605 | template <auto PrecisionType = pto::FmodAlgorithm::DEFAULT, typename LastUse = LastUse3Dim<0, 0, 0>, int... BrcOperands, | 299 | template <auto PrecisionType = pto::FmodAlgorithm::DEFAULT, typename LastUse = LastUse3Dim<0, 0, 0>, int... BrcOperands, |
| 606 | typename T0, typename T1, typename T2> | 300 | typename T0, typename T1, typename T2> |
| @@ -216,10 +216,14 @@ TILEOP void TPow(T0 dst, T1 src0, T2 src1, T3 tmp) | |||
| 216 | constexpr auto tileH = TileOp::GetTensorTileShapeDim<T0, DIM_4TH, MAX_DIMS>(); | 216 | constexpr auto tileH = TileOp::GetTensorTileShapeDim<T0, DIM_4TH, MAX_DIMS>(); |
| 217 | constexpr auto tileW = TileOp::GetTensorTileShapeDim<T0, DIM_5TH, MAX_DIMS>(); | 217 | constexpr auto tileW = TileOp::GetTensorTileShapeDim<T0, DIM_5TH, MAX_DIMS>(); |
| 218 | constexpr auto dataTypeSize = sizeof(typename T0::Type); | 218 | constexpr auto dataTypeSize = sizeof(typename T0::Type); |
| 219 | - constexpr auto floatSlot = tileH * tileW * dataTypeSize; // bytes, 32B-aligned | 219 | + constexpr auto floatSlot = tileH * tileW * dataTypeSize; // bytes, 32B-aligned |
| 220 | - constexpr auto maskCols = ((tileW + 7) / 8 + 31) / 32 * 32; // bit-packed mask bytes/row, 32B-aligned | 220 | + constexpr auto BITS_PER_BYTE = TileOp::BITS_PER_BYTE; |
| 221 | - constexpr auto maskSlot = tileH * maskCols; // bytes | 221 | + constexpr auto MASK_ALIGNMENT = TileOp::BLOCK_SIZE; |
| 222 | - constexpr auto maskBase = 2 * floatSlot; // masks laid out after 2 float tiles | 222 | + constexpr auto MASK_COUNT = 2; |
| 223 | + constexpr auto maskCols = ((tileW + BITS_PER_BYTE - 1) / BITS_PER_BYTE + MASK_ALIGNMENT - 1) / MASK_ALIGNMENT * | ||
| 224 | + MASK_ALIGNMENT; | ||
| 225 | + constexpr auto maskSlot = tileH * maskCols; // bytes | ||
| 226 | + constexpr auto maskBase = MASK_COUNT * floatSlot; | ||
| 223 | using DataTile = pto::Tile<pto::TileType::Vec, float, tileH, tileW, pto::BLayout::RowMajor, -1, -1>; | 227 | using DataTile = pto::Tile<pto::TileType::Vec, float, tileH, tileW, pto::BLayout::RowMajor, -1, -1>; |
| 224 | using MaskTile = pto::Tile<pto::TileType::Vec, uint8_t, tileH, maskCols, pto::BLayout::RowMajor, -1, -1>; | 228 | using MaskTile = pto::Tile<pto::TileType::Vec, uint8_t, tileH, maskCols, pto::BLayout::RowMajor, -1, -1>; |
| 225 | DataTile dstTile(dstShape3, dstShape4); | 229 | DataTile dstTile(dstShape3, dstShape4); |
| @@ -227,9 +231,10 @@ TILEOP void TPow(T0 dst, T1 src0, T2 src1, T3 tmp) | |||
| 227 | DataTile src1Tile(dstShape3, dstShape4); | 231 | DataTile src1Tile(dstShape3, dstShape4); |
| 228 | DataTile tmp0Tile(dstShape3, dstShape4); | 232 | DataTile tmp0Tile(dstShape3, dstShape4); |
| 229 | DataTile tmp1Tile(dstShape3, dstShape4); | 233 | DataTile tmp1Tile(dstShape3, dstShape4); |
| 230 | - MaskTile mask0Tile(dstShape3, (dstShape4 + 7) / 8); | 234 | + const auto validMaskCols = (dstShape4 + BITS_PER_BYTE - 1) / BITS_PER_BYTE; |
| 231 | - MaskTile mask1Tile(dstShape3, (dstShape4 + 7) / 8); | 235 | + MaskTile mask0Tile(dstShape3, validMaskCols); |
| 232 | - MaskTile selTmpTile(dstShape3, (dstShape4 + 7) / 8); | 236 | + MaskTile mask1Tile(dstShape3, validMaskCols); |
| 237 | + MaskTile selTmpTile(dstShape3, validMaskCols); | ||
| 233 | for (LoopVar n0Index = 0; n0Index < dstShape0; n0Index++) { | 238 | for (LoopVar n0Index = 0; n0Index < dstShape0; n0Index++) { |
| 234 | for (LoopVar n1Index = 0; n1Index < dstShape1; n1Index++) { | 239 | for (LoopVar n1Index = 0; n1Index < dstShape1; n1Index++) { |
| 235 | for (LoopVar n2Index = 0; n2Index < dstShape2; n2Index++) { | 240 | for (LoopVar n2Index = 0; n2Index < dstShape2; n2Index++) { |
| @@ -244,7 +249,7 @@ TILEOP void TPow(T0 dst, T1 src0, T2 src1, T3 tmp) | |||
| 244 | pto::TASSIGN(tmp1Tile, (uint64_t)(tmp.GetAddr() + 1 * floatSlot)); | 249 | pto::TASSIGN(tmp1Tile, (uint64_t)(tmp.GetAddr() + 1 * floatSlot)); |
| 245 | pto::TASSIGN(mask0Tile, (uint64_t)(tmp.GetAddr() + maskBase + 0 * maskSlot)); | 250 | pto::TASSIGN(mask0Tile, (uint64_t)(tmp.GetAddr() + maskBase + 0 * maskSlot)); |
| 246 | pto::TASSIGN(mask1Tile, (uint64_t)(tmp.GetAddr() + maskBase + 1 * maskSlot)); | 251 | pto::TASSIGN(mask1Tile, (uint64_t)(tmp.GetAddr() + maskBase + 1 * maskSlot)); |
| 247 | - pto::TASSIGN(selTmpTile, (uint64_t)(tmp.GetAddr() + maskBase + 2 * maskSlot)); | 252 | + pto::TASSIGN(selTmpTile, (uint64_t)(tmp.GetAddr() + maskBase + MASK_COUNT * maskSlot)); |
| 248 | TPowFloatTile(dstTile, src0Tile, src1Tile, tmp0Tile, tmp1Tile, mask0Tile, mask1Tile, selTmpTile); | 253 | TPowFloatTile(dstTile, src0Tile, src1Tile, tmp0Tile, tmp1Tile, mask0Tile, mask1Tile, selTmpTile); |
| 249 | } | 254 | } |
| 250 | } | 255 | } |
| @@ -19,6 +19,11 @@ | |||
| 19 | #include "utils/sync.h" | 19 | #include "utils/sync.h" |
| 20 | #include "basic.h" | 20 | #include "basic.h" |
| 21 | 21 | ||
| 22 | +constexpr int32_t INT32_NEGATIVE_DIVIDEND_ZERO_DIVISOR_RESULT = 0x7FFF7F7F; | ||
| 23 | +constexpr int32_t INT32_NONNEGATIVE_DIVIDEND_ZERO_DIVISOR_RESULT = static_cast<int32_t>(0x80008080U); | ||
| 24 | +constexpr int64_t INT64_NEGATIVE_DIVIDEND_ZERO_DIVISOR_RESULT = 0x7FFFFFFFFFFFFFFF; | ||
| 25 | +constexpr int64_t INT64_NONNEGATIVE_DIVIDEND_ZERO_DIVISOR_RESULT = static_cast<int64_t>(0x8000000000000000ULL); | ||
| 26 | + | ||
| 22 | template <auto pos, auto neg, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, | 27 | template <auto pos, auto neg, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, |
| 23 | typename T7> | 28 | typename T7> |
| 24 | TILEOP void IntFloorDiv(T0 dst, T1 src0, T2 src1, T3 tmp0, T4 tmp1, T5 tmp2, T6 tmp3, T7 tmp4) | 29 | TILEOP void IntFloorDiv(T0 dst, T1 src0, T2 src1, T3 tmp0, T4 tmp1, T5 tmp2, T6 tmp3, T7 tmp4) |
| @@ -63,7 +68,7 @@ template <typename TmpTensor> | |||
| 63 | TILEOP uint64_t FloorDivTmpAddr(TmpTensor tmp, size_t byteOffset, size_t tileShapeSize, size_t tileIndex, | 68 | TILEOP uint64_t FloorDivTmpAddr(TmpTensor tmp, size_t byteOffset, size_t tileShapeSize, size_t tileIndex, |
| 64 | size_t elementSize) | 69 | size_t elementSize) |
| 65 | { | 70 | { |
| 66 | - return (uint64_t)(tmp.GetAddr() + byteOffset + tileIndex * tileShapeSize * elementSize); | 71 | + return (uint64_t)((__ubuf__ uint8_t*)tmp.GetAddr() + byteOffset + tileIndex * tileShapeSize * elementSize); |
| 67 | } | 72 | } |
| 68 | 73 | ||
| 69 | template <typename T0, typename T3, auto tileH, auto tileW, typename Src0Tile, typename Src1Tile, typename DstTile> | 74 | template <typename T0, typename T3, auto tileH, auto tileW, typename Src0Tile, typename Src1Tile, typename DstTile> |
| @@ -308,8 +313,8 @@ TILEOP void FloorDivNonV220Int32Compute(DstTile dstTile, Src0Tile src0Tile, Src1 | |||
| 308 | pto::TASSIGN(tmp3MaskTile, FloorDivTmpAddr(tmp, offset, tileShapeSize, 2, sizeof(int32_t))); | 313 | pto::TASSIGN(tmp3MaskTile, FloorDivTmpAddr(tmp, offset, tileShapeSize, 2, sizeof(int32_t))); |
| 309 | pto::TASSIGN(tmp4MaskTile, FloorDivTmpAddr(tmp, offset, tileShapeSize, 3, sizeof(int32_t))); | 314 | pto::TASSIGN(tmp4MaskTile, FloorDivTmpAddr(tmp, offset, tileShapeSize, 3, sizeof(int32_t))); |
| 310 | 315 | ||
| 311 | - IntFloorDiv<(int32_t)0x7FFF7F7F, (int32_t)0x80008080>(dstTile, src0Tile, src1Tile, tmp0DataTile, tmp1DataTile, | 316 | + IntFloorDiv<INT32_NEGATIVE_DIVIDEND_ZERO_DIVISOR_RESULT, INT32_NONNEGATIVE_DIVIDEND_ZERO_DIVISOR_RESULT>( |
| 312 | - tmp2DataTile, tmp3MaskTile, tmp4MaskTile); | 317 | + dstTile, src0Tile, src1Tile, tmp0DataTile, tmp1DataTile, tmp2DataTile, tmp3MaskTile, tmp4MaskTile); |
| 313 | } | 318 | } |
| 314 | 319 | ||
| 315 | template <typename T0, typename T3, auto tileH, auto tileW, typename SrcTile, typename DstTile> | 320 | template <typename T0, typename T3, auto tileH, auto tileW, typename SrcTile, typename DstTile> |
| @@ -329,7 +334,7 @@ TILEOP void FloorDivNonV220Int64Compute(DstTile dstTile, SrcTile src0Tile, SrcTi | |||
| 329 | pto::TASSIGN(tmp3MaskTile, FloorDivTmpAddr(tmp, offset, tileShapeSize, 2, sizeof(int64_t))); | 334 | pto::TASSIGN(tmp3MaskTile, FloorDivTmpAddr(tmp, offset, tileShapeSize, 2, sizeof(int64_t))); |
| 330 | pto::TASSIGN(tmp4MaskTile, FloorDivTmpAddr(tmp, offset, tileShapeSize, 3, sizeof(int64_t))); | 335 | pto::TASSIGN(tmp4MaskTile, FloorDivTmpAddr(tmp, offset, tileShapeSize, 3, sizeof(int64_t))); |
| 331 | 336 | ||
| 332 | - IntFloorDiv<(int64_t)0x7FFFFFFFFFFFFFFF, (int64_t)0x8000000000000000>( | 337 | + IntFloorDiv<INT64_NEGATIVE_DIVIDEND_ZERO_DIVISOR_RESULT, INT64_NONNEGATIVE_DIVIDEND_ZERO_DIVISOR_RESULT>( |
| 333 | dstTile, src0Tile, src1Tile, tmp0DataTile, tmp1DataTile, tmp2DataTile, tmp3MaskTile, tmp4MaskTile); | 338 | dstTile, src0Tile, src1Tile, tmp0DataTile, tmp1DataTile, tmp2DataTile, tmp3MaskTile, tmp4MaskTile); |
| 334 | } | 339 | } |
| 335 | #endif | 340 | #endif |
| @@ -348,9 +353,11 @@ TILEOP void TFloorDiv(T0 dst, T1 src0, T2 src1, T3 tmp) | |||
| 348 | constexpr auto tileH = TileOp::GetTensorTileShapeDim<T0, DIM_4TH, MAX_DIMS>(); | 353 | constexpr auto tileH = TileOp::GetTensorTileShapeDim<T0, DIM_4TH, MAX_DIMS>(); |
| 349 | constexpr auto tileW = TileOp::GetTensorTileShapeDim<T0, DIM_5TH, MAX_DIMS>(); | 354 | constexpr auto tileW = TileOp::GetTensorTileShapeDim<T0, DIM_5TH, MAX_DIMS>(); |
| 350 | constexpr auto dstTypeSize = sizeof(typename T0::Type); | 355 | constexpr auto dstTypeSize = sizeof(typename T0::Type); |
| 356 | + constexpr size_t FLOOR_DIV_TMP_PLANE_COUNT = 6; | ||
| 351 | 357 | ||
| 352 | - constexpr auto tileShapeSize = TileOp::GetAnyAxisMergeResult< | 358 | + constexpr auto tmpPlaneSize = TileOp::GetAnyAxisMergeResult<DIM_1ST, Std::tuple_size<typename T3::TileShape>::value, |
| 353 | - DIM_1ST, Std::tuple_size<typename T0::TileShape>::value, typename T0::TileShape>(); | 359 | + typename T3::TileShape>() / |
| 360 | + FLOOR_DIV_TMP_PLANE_COUNT; | ||
| 354 | 361 | ||
| 355 | using DataTileDefine = pto::Tile<pto::TileType::Vec, typename T0::Type, tileH, tileW, pto::BLayout::RowMajor, -1, | 362 | using DataTileDefine = pto::Tile<pto::TileType::Vec, typename T0::Type, tileH, tileW, pto::BLayout::RowMajor, -1, |
| 356 | -1>; | 363 | -1>; |
| @@ -371,7 +378,7 @@ TILEOP void TFloorDiv(T0 dst, T1 src0, T2 src1, T3 tmp) | |||
| 371 | if constexpr (std::is_same_v<typename T0::Type, half> || | 378 | if constexpr (std::is_same_v<typename T0::Type, half> || |
| 372 | std::is_same_v<typename T0::Type, bfloat16_t>) { | 379 | std::is_same_v<typename T0::Type, bfloat16_t>) { |
| 373 | FloorDivFp32TmpCompute<T0, T3, tileH, tileW>(dstTile, src0ExecTile, src1ExecTile, tmp, | 380 | FloorDivFp32TmpCompute<T0, T3, tileH, tileW>(dstTile, src0ExecTile, src1ExecTile, tmp, |
| 374 | - tmpByteOffset, dstShape3, dstShape4, tileShapeSize); | 381 | + tmpByteOffset, dstShape3, dstShape4, tmpPlaneSize); |
| 375 | } else if constexpr (std::is_same_v<typename T0::Type, float>) { | 382 | } else if constexpr (std::is_same_v<typename T0::Type, float>) { |
| 376 | FloorDivFloatCompute(dstTile, src0ExecTile, src1ExecTile); | 383 | FloorDivFloatCompute(dstTile, src0ExecTile, src1ExecTile); |
| 377 | } | 384 | } |
| @@ -379,25 +386,25 @@ TILEOP void TFloorDiv(T0 dst, T1 src0, T2 src1, T3 tmp) | |||
| 379 | #ifdef __DAV_V220 | 386 | #ifdef __DAV_V220 |
| 380 | if constexpr (std::is_same_v<typename T0::Type, int32_t>) { | 387 | if constexpr (std::is_same_v<typename T0::Type, int32_t>) { |
| 381 | FloorDivV220Int32Compute<T0, T3, tileH, tileW>(dstTile, src0ExecTile, src1ExecTile, tmp, | 388 | FloorDivV220Int32Compute<T0, T3, tileH, tileW>(dstTile, src0ExecTile, src1ExecTile, tmp, |
| 382 | - tmpByteOffset, dstShape3, dstShape4, tileShapeSize); | 389 | + tmpByteOffset, dstShape3, dstShape4, tmpPlaneSize); |
| 383 | } else if constexpr (std::is_same_v<typename T0::Type, int8_t> || | 390 | } else if constexpr (std::is_same_v<typename T0::Type, int8_t> || |
| 384 | std::is_same_v<typename T0::Type, uint8_t>) { | 391 | std::is_same_v<typename T0::Type, uint8_t>) { |
| 385 | FloorDivV220Int8Compute<T0, T3, tileH, tileW>(dstTile, src0ExecTile, src1ExecTile, tmp, | 392 | FloorDivV220Int8Compute<T0, T3, tileH, tileW>(dstTile, src0ExecTile, src1ExecTile, tmp, |
| 386 | - tmpByteOffset, dstShape3, dstShape4, tileShapeSize); | 393 | + tmpByteOffset, dstShape3, dstShape4, tmpPlaneSize); |
| 387 | } | 394 | } |
| 388 | #else | 395 | #else |
| 389 | if constexpr (std::is_same_v<typename T0::Type, uint8_t>) { | 396 | if constexpr (std::is_same_v<typename T0::Type, uint8_t>) { |
| 390 | FloorDivNonV220Uint8Compute<T0, T3, tileH, tileW>( | 397 | FloorDivNonV220Uint8Compute<T0, T3, tileH, tileW>( |
| 391 | - dstTile, src0ExecTile, src1ExecTile, tmp, tmpByteOffset, dstShape3, dstShape4, tileShapeSize); | 398 | + dstTile, src0ExecTile, src1ExecTile, tmp, tmpByteOffset, dstShape3, dstShape4, tmpPlaneSize); |
| 392 | } else if constexpr (std::is_same_v<typename T0::Type, int8_t>) { | 399 | } else if constexpr (std::is_same_v<typename T0::Type, int8_t>) { |
| 393 | - FloorDivNonV220Int8Compute<T0, T3, tileH, tileW>( | 400 | + FloorDivNonV220Int8Compute<T0, T3, tileH, tileW>(dstTile, src0ExecTile, src1ExecTile, tmp, |
| 394 | - dstTile, src0ExecTile, src1ExecTile, tmp, tmpByteOffset, dstShape3, dstShape4, tileShapeSize); | 401 | + tmpByteOffset, dstShape3, dstShape4, tmpPlaneSize); |
| 395 | } else if constexpr (std::is_same_v<typename T0::Type, int32_t>) { | 402 | } else if constexpr (std::is_same_v<typename T0::Type, int32_t>) { |
| 396 | FloorDivNonV220Int32Compute<T0, T3, tileH, tileW>( | 403 | FloorDivNonV220Int32Compute<T0, T3, tileH, tileW>( |
| 397 | - dstTile, src0ExecTile, src1ExecTile, tmp, tmpByteOffset, dstShape3, dstShape4, tileShapeSize); | 404 | + dstTile, src0ExecTile, src1ExecTile, tmp, tmpByteOffset, dstShape3, dstShape4, tmpPlaneSize); |
| 398 | } else if constexpr (std::is_same_v<typename T0::Type, int64_t>) { | 405 | } else if constexpr (std::is_same_v<typename T0::Type, int64_t>) { |
| 399 | FloorDivNonV220Int64Compute<T0, T3, tileH, tileW>( | 406 | FloorDivNonV220Int64Compute<T0, T3, tileH, tileW>( |
| 400 | - dstTile, src0ExecTile, src1ExecTile, tmp, tmpByteOffset, dstShape3, dstShape4, tileShapeSize); | 407 | + dstTile, src0ExecTile, src1ExecTile, tmp, tmpByteOffset, dstShape3, dstShape4, tmpPlaneSize); |
| 401 | } | 408 | } |
| 402 | #endif | 409 | #endif |
| 403 | } | 410 | } |