Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022 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 | #if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) |
| 25 | |
| 26 | #include "src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseAddKernelComponent.h" |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame^] | 27 | #include "arm_compute/core/Validate.h" |
| 28 | #include "src/core/helpers/AutoConfiguration.h" |
| 29 | #include "src/core/helpers/WindowHelpers.h" |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace experimental |
| 34 | { |
| 35 | namespace dynamic_fusion |
| 36 | { |
| 37 | ComponentType ClElementwiseAddKernelComponent::get_component_type() const |
| 38 | { |
| 39 | return ComponentType::Simple; |
| 40 | } |
| 41 | |
| 42 | std::set<std::string> ClElementwiseAddKernelComponent::get_headers_list() const |
| 43 | { |
| 44 | return std::set<std::string> { "gemm_helpers.h", "repeat.h" }; |
| 45 | } |
| 46 | |
Gunes Bayir | 8a87983 | 2022-03-10 21:21:01 +0000 | [diff] [blame^] | 47 | Window ClElementwiseAddKernelComponent::get_window() const |
| 48 | { |
| 49 | const ITensorInfo *lhs_info = _blueprint->impl().get_kernel_argument_info(_lhs.arg_id); |
| 50 | const ITensorInfo *rhs_info = _blueprint->impl().get_kernel_argument_info(_rhs.arg_id); |
| 51 | ITensorInfo *dst_info = _blueprint->impl().get_kernel_argument_info(_blueprint->impl().get_dst_id()); |
| 52 | |
| 53 | ARM_COMPUTE_ERROR_ON_NULLPTR(lhs_info, rhs_info, dst_info); |
| 54 | |
| 55 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*lhs_info, *rhs_info); |
| 56 | const TensorShape &out_shape = broadcast_pair.first; |
| 57 | |
| 58 | auto_init_if_empty(*dst_info, out_shape, 1, lhs_info->data_type()); |
| 59 | |
| 60 | const unsigned int vector_size_byte_opencl = 16; |
| 61 | const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst_info->element_size(), dst_info->dimension(0)); |
| 62 | Window win = calculate_max_window(*dst_info, Steps(num_elems_processed_per_iteration)); |
| 63 | |
| 64 | return win; |
| 65 | } |
| 66 | |
Giorgio Arena | 232c452 | 2022-03-03 10:09:01 +0000 | [diff] [blame] | 67 | std::string ClElementwiseAddKernelComponent::get_component_code() const |
| 68 | { |
| 69 | std::string code; |
| 70 | return R"_( |
| 71 | //------------------ START KERNEL {{meta_kernel_id}} ELTWISE_ADD --------------------- |
| 72 | // IN_0(Accumulator) {{acc}} |
| 73 | // IN_1(Addend) {{addend}} |
| 74 | |
| 75 | // c = addend + c (mix-precision, broadcast, boundary aware) |
| 76 | { |
| 77 | __global uchar *addend_addr = {{addend}}_ptr + {{addend}}_offset_first_element_in_bytes + (get_global_id(0) * (uint)N0 * sizeof(DATA_TYPE)) + (COMPUTE_M0_START_ROW(g_y, M0, PARTIAL_STORE_M0) * {{addend}}_stride_y) + get_global_id(2) * {{addend}}_stride_z; \ |
| 78 | LOAD_BLOCK_BOUNDARY_AWARE(M0, N0, DATA_TYPE, addend, addend_addr, 0, {{addend}}_stride_y, g_zero, PARTIAL_LOAD_M0, PARTIAL_LOAD_N0, PARTIAL_COND_Y, PARTIAL_COND_X); \ |
| 79 | MIXED_PRECISION_ELTWISE_OP_BLOCK(ADD_X_POS_0, M0, N0, {{acc}}, addend, DATA_TYPE_ACCUMULATOR, addend_hp); |
| 80 | } |
| 81 | //------------------ END KERNEL {{meta_kernel_id}} ELTWISE_ADD --------------------- |
| 82 | |
| 83 | )_"; |
| 84 | } |
| 85 | ClElementwiseAddKernelComponent::TagLUT ClElementwiseAddKernelComponent::allocate_vars(SharedVarTable &vtable) const |
| 86 | { |
| 87 | // Determine which argument is the accumulator |
| 88 | Link accumulator; |
| 89 | Link addend; |
| 90 | if(_lhs.group == SharedVarGroup::Automatic) |
| 91 | { |
| 92 | accumulator = _lhs; |
| 93 | addend = _rhs; |
| 94 | } |
| 95 | else if(_rhs.group == SharedVarGroup::Automatic) |
| 96 | { |
| 97 | accumulator = _rhs; |
| 98 | addend = _lhs; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | ARM_COMPUTE_ERROR("Invalid elementwise component linking"); |
| 103 | } |
| 104 | return { |
| 105 | { "meta_kernel_id", id() }, |
| 106 | { "acc", vtable.add(accumulator, ClKernelArgRuntimeDescriptor(accumulator.arg_id, TensorArgType::Image_3D), "add_acc") }, |
| 107 | { "addend", vtable.add(addend, ClKernelArgRuntimeDescriptor(addend.arg_id, TensorArgType::Image_3D), "add_addend") }, |
| 108 | // {"dst", vtable.add(_dst, ClKernelArgRuntimeDescriptor(_dst.arg_id, TensorArgType::Image_3D), "dst")}, // dst is needed for the root version and/or non-inplace version should we need one |
| 109 | }; |
| 110 | } |
| 111 | } // namespace dynamic_fusion |
| 112 | } // namespace experimental |
| 113 | } // namespace arm_compute |
| 114 | |
| 115 | #endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) |