1 /* 2 * Copyright (c) 2022-2023 Arm Limited. 3 * 4 * SPDX-License-Identifier: MIT 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to 8 * deal in the Software without restriction, including without limitation the 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 #ifndef SRC_DYNAMIC_FUSION_SKETCH_GPU_OPERATORS_INTERNAL_GPUELEMENTWISEBINARYCOMMON 25 #define SRC_DYNAMIC_FUSION_SKETCH_GPU_OPERATORS_INTERNAL_GPUELEMENTWISEBINARYCOMMON 26 27 #include "arm_compute/core/Error.h" 28 29 namespace arm_compute 30 { 31 /** Forward declaration */ 32 class ITensorInfo; 33 34 namespace experimental 35 { 36 namespace dynamic_fusion 37 { 38 class ElementwiseBinaryCommonAttributes 39 { 40 public: 41 enum class ElementwiseOp 42 { 43 Add, /**< (x + y) */ 44 Sub, /**< (x - y) */ 45 Div, /**< (x / y) */ 46 Mul, /**< (x * y) */ 47 Min, /**< Min(x, y) */ 48 Max, /**< Max(x, y) */ 49 SquaredDiff, /**< (x - y)^2 */ 50 Power, /**< x ^ y */ 51 Prelu, /**< y*x if x < 0, x otherwise */ 52 }; 53 /** Set operation*/ 54 ElementwiseBinaryCommonAttributes &operation(const ElementwiseBinaryCommonAttributes::ElementwiseOp &operation); 55 /** Get operation*/ 56 ElementwiseOp operation() const; 57 58 private: 59 ElementwiseOp _operation; /**< Elementwise operation */ 60 }; 61 62 /** Forward declaration */ 63 class GpuWorkloadContext; 64 class GpuWorkloadSketch; 65 66 /** Operator interface. */ 67 class GpuElementwiseBinaryCommon final 68 { 69 public: 70 /** Create an operator and fuse it into the workload sketch. 71 * @note If @ref validate_op() fails, the creation also fails and may throw an error. 72 * @note If @ref validate_op() fails, @p sketch remains unchanged and valid. 73 * 74 * Valid data type configurations are checked at the operator level i.e. GpuAdd::validate_op(), GpuSub::validate_op(), ... etc. 75 * 76 * Valid data layouts: 77 * - Any 78 * 79 * @param[in,out] sketch Workload sketch into which the operator will be fused 80 * @param[in] lhs Left hand side tensor info. Data types supported: U8/S16/S32/F16/F32. 81 * @param[in] rhs Right hand side tensor info. Data types supported: U8/S16/S32/F16/F32. 82 * @param[in] attributes ElementwiseBinaryCommonAttributes containing the operator type: ADD, SUB, DIV, ... etc. 83 * 84 * @return Pointer for the destination tensor info 85 */ 86 static ITensorInfo *create_op(GpuWorkloadSketch &sketch, 87 ITensorInfo *lhs, 88 ITensorInfo *rhs, 89 const ElementwiseBinaryCommonAttributes &attributes); 90 /** Check if the operator configuration is supported, irrespective of fusion 91 * 92 * @param[in] context Workload context within which the operator is running 93 * @param[in] lhs Left hand side tensor info. Data types supported: U8/S16/S32/F16/F32. 94 * @param[in] rhs Right hand side tensor info. Data types supported: U8/S16/S32/F16/F32. 95 * @param[in] attributes ElementwiseBinaryCommonAttributes containing the operator type: ADD, SUB, DIV, ... etc. 96 * 97 * @return Status 98 */ 99 static Status is_supported_op(const GpuWorkloadContext &context, 100 const ITensorInfo *lhs, 101 const ITensorInfo *rhs, 102 const ElementwiseBinaryCommonAttributes &attributes); 103 /** Validate the operator and check if it can be fused into the workload sketch. 104 * 105 * Parameters are similar to @ref GpuElementwiseBinaryCommon::create_op() 106 * 107 * @return Status 108 */ 109 static Status validate_op(const GpuWorkloadSketch &sketch, 110 const ITensorInfo *rhs, 111 const ITensorInfo *lhs, 112 const ElementwiseBinaryCommonAttributes &attributes); 113 }; 114 } // namespace dynamic_fusion 115 } // namespace experimental 116 } // namespace arm_compute 117 #endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_OPERATORS_INTERNAL_GPUELEMENTWISEBINARYCOMMON */ 118