Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | b3077fb | 2023-01-03 17:59:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022-2023 Arm Limited. |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 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 | #include "ClComponentElementwiseBinary.h" |
| 25 | |
| 26 | #include "arm_compute/core/Validate.h" |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 27 | #include "src/core/CL/CLValidate.h" |
| 28 | #include "src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateElementwiseBinary.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace experimental |
| 33 | { |
| 34 | namespace dynamic_fusion |
| 35 | { |
| 36 | namespace |
| 37 | { |
| 38 | std::set<ElementwiseBinaryCommonAttributes::ElementwiseOp> supported_ops |
| 39 | { |
Jakub Sujak | 7359a87 | 2023-01-05 14:24:13 +0000 | [diff] [blame] | 40 | ElementwiseBinaryCommonAttributes::ElementwiseOp::Add, |
| 41 | ElementwiseBinaryCommonAttributes::ElementwiseOp::Mul |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 42 | }; |
| 43 | } |
| 44 | |
| 45 | Status ClComponentElementwiseBinary::validate(const ArgumentPack<ITensorInfo> &tensors, const ElementwiseBinaryCommonAttributes &attributes) |
| 46 | { |
| 47 | const auto lhs = tensors.get_const_tensor(TensorType::ACL_SRC_0); |
| 48 | const auto rhs = tensors.get_const_tensor(TensorType::ACL_SRC_1); |
| 49 | const auto dst = tensors.get_const_tensor(TensorType::ACL_DST_0); |
| 50 | |
| 51 | // Check operator type |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(supported_ops.find(attributes.operation()) == supported_ops.end(), "Provided Elementwise operation not supported."); |
| 53 | |
| 54 | // Check validity |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst); |
| 56 | |
| 57 | //Check data type for different elementwise operators |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::F32, DataType::F16, DataType::S32, DataType::S16, DataType::U8); |
| 59 | |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 60 | // dst shape is correct |
| 61 | const TensorShape out_shape = TensorShape::broadcast_shape(lhs->tensor_shape(), rhs->tensor_shape()); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0), "Wrong shape for dst."); |
Viet-Hoa Do | b3077fb | 2023-01-03 17:59:14 +0000 | [diff] [blame] | 64 | |
| 65 | const auto &lhs_shape = lhs->tensor_shape(); |
| 66 | const auto &rhs_shape = rhs->tensor_shape(); |
| 67 | const auto &dst_shape = dst->tensor_shape(); |
| 68 | |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
| 70 | detail::have_different_dimensions(lhs_shape, dst_shape, 0) && detail::have_different_dimensions(rhs_shape, dst_shape, 0), |
| 71 | "Only LHS or RHS can be broadcasting, not both."); |
| 72 | |
| 73 | // Dimension Y and Z are collapsed together in the current kernel implementation, |
| 74 | // hence they cannot be independently broadcast or non-broadcast. |
| 75 | // See: ClTemplateElementwiseBinary::get_window |
| 76 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
| 77 | (lhs_shape[1] != dst_shape[1] || rhs_shape[1] != dst_shape[1]) != (lhs_shape[2] != dst_shape[2] || rhs_shape[2] != dst_shape[2]), |
| 78 | "Dimension Y and Z must both be either broadcast or non-broadcast."); |
| 79 | |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
| 81 | detail::have_different_dimensions(lhs_shape, dst_shape, 3), |
| 82 | "LHS broadcast in dimension 3 or higher is not supported."); |
| 83 | |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
| 85 | detail::have_different_dimensions(rhs_shape, dst_shape, 3), |
| 86 | "RHS broadcast in dimension 3 or higher is not supported."); |
| 87 | |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 88 | // Matching data type |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs); |
| 90 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst); |
| 91 | |
| 92 | // Matching data layout |
| 93 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(lhs, rhs); |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(lhs, dst); |
| 95 | |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 96 | // All tensor infos are initialized |
| 97 | ARM_COMPUTE_RETURN_ERROR_ON(lhs->tensor_shape().total_size() == 0); |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON(rhs->tensor_shape().total_size() == 0); |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON(dst->tensor_shape().total_size() == 0); |
| 100 | |
| 101 | // Device requirements are met |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(lhs); |
| 103 | |
| 104 | return Status{}; |
| 105 | } |
| 106 | |
| 107 | ClComponentElementwiseBinary::ClComponentElementwiseBinary( |
| 108 | ComponentId id, |
| 109 | const Properties &properties, |
| 110 | const ArgumentPack<ITensorInfo> &tensors, |
| 111 | const Attributes &attributes) |
| 112 | : IGpuKernelComponent{ id, properties, tensors }, |
| 113 | _component_writer{ std::make_unique<ClTemplateElementwiseBinary>(id, tensors, attributes) } |
| 114 | { |
| 115 | } |
| 116 | ClComponentElementwiseBinary::~ClComponentElementwiseBinary() |
| 117 | { |
| 118 | } |
| 119 | const IGpuTemplateComponentWriter *ClComponentElementwiseBinary::template_writer() const |
| 120 | { |
| 121 | return _component_writer.get(); |
| 122 | } |
| 123 | } // namespace dynamic_fusion |
| 124 | } // namespace experimental |
| 125 | } // namespace arm_compute |