SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 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 | #include "GpuCkwElementwiseBinary.h" |
| 25 | |
| 26 | #include "acl/AclKernelWriter.h" |
| 27 | #include "acl/AclScopedKernelWriter.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "ckw/TensorTileSampler.h" |
| 31 | #include "ckw/Types.h" |
| 32 | #include "src/core/helpers/WindowHelpers.h" |
| 33 | #include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h" |
| 34 | #include "src/dynamic_fusion/sketch/gpu/ckw_driver/GpuCkwVariableTable.h" |
| 35 | #include "src/dynamic_fusion/sketch/gpu/ckw_driver/components/utils/WriterHelper.h" |
| 36 | #include <string> |
| 37 | |
| 38 | using namespace ckw; |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace experimental |
| 42 | { |
| 43 | namespace |
| 44 | { |
| 45 | /** Create a simple sampler from tile of dimension [m0, n0] |
| 46 | */ |
| 47 | inline TensorTileSampler create_simple_sampler(AclScopedKernelWriter &writer, int32_t m0, int32_t n0) |
| 48 | { |
| 49 | TensorTileSampler sampler; |
| 50 | |
| 51 | auto &gid_0 = writer->declare_tile("gid_0", ckw::DataType::Int32); |
| 52 | auto &gid_1 = writer->declare_tile("gid_1", ckw::DataType::Int32); |
| 53 | auto &gid_2 = writer->declare_tile("gid_2", ckw::DataType::Int32); |
| 54 | |
| 55 | auto &const_0 = writer->declare_tile("0", 0); |
| 56 | |
| 57 | writer->op_get_global_id(gid_0, 0); |
| 58 | writer->op_get_global_id(gid_1, 1); |
| 59 | writer->op_get_global_id(gid_2, 2); |
| 60 | |
| 61 | sampler.x(gid_0); |
| 62 | sampler.y(gid_1); |
| 63 | sampler.z(const_0); // 3rd dimension collapsed with 2nd dimension |
| 64 | sampler.b(gid_2); |
| 65 | |
| 66 | sampler.width(n0); |
| 67 | sampler.height(m0); |
| 68 | |
| 69 | sampler.format(TensorSamplerFormat::C_WH_1); // 3rd dimension collapsed with 2nd dimension |
| 70 | sampler.address_mode_x(TensorSamplerAddressModeX::None); |
| 71 | sampler.address_mode_y(TensorSamplerAddressModeY::ClampToBorder); |
| 72 | sampler.address_mode_z(TensorSamplerAddressModeZ::Skip); // Dimensions higher than 3 not supported yet |
| 73 | |
| 74 | return sampler; |
| 75 | } |
| 76 | } // namespace |
| 77 | |
| 78 | namespace dynamic_fusion |
| 79 | { |
| 80 | GpuCkwElementwiseBinary::GpuCkwElementwiseBinary(ComponentId id, |
| 81 | const ArgumentPack<ITensorInfo> &tensors, |
| 82 | const Attributes &attributes) |
| 83 | : IGpuCkwComponentDriver{ id, tensors }, |
| 84 | _lhs{}, |
| 85 | _rhs{}, |
| 86 | _dst{}, |
| 87 | _attributes{ attributes } |
| 88 | { |
| 89 | _lhs = this->tensors().get_const_tensor(TensorType::ACL_SRC_0); |
| 90 | _rhs = this->tensors().get_const_tensor(TensorType::ACL_SRC_1); |
| 91 | _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0); |
| 92 | ARM_COMPUTE_ERROR_ON_NULLPTR(_lhs, _rhs, _dst); |
| 93 | } |
| 94 | |
| 95 | void GpuCkwElementwiseBinary::write_component_code(const ComponentGroup &comp_group, GpuCkwVariableTable &vtable, AclScopedKernelWriter writer) const |
| 96 | { |
| 97 | const auto root_window = comp_group.get_root_component()->ckw_component_driver()->get_window(); |
| 98 | const unsigned int n0 = root_window.x().step(); |
| 99 | const unsigned int m0 = root_window.y().step(); |
| 100 | |
| 101 | AclComponentArgument *lhs = vtable.declare_variable(comp_group, writer, _lhs, "lhs"); |
| 102 | AclComponentArgument *rhs = vtable.declare_variable(comp_group, writer, _rhs, "rhs"); |
| 103 | AclComponentArgument *dst = vtable.declare_variable(comp_group, writer, _dst, "dst"); |
| 104 | |
| 105 | // Load the LHS and RHS tiles and prepare the tensor sampler. |
| 106 | load_lhs_rhs_tiles_and_prepare_sampler(writer, lhs, rhs, m0, n0, create_simple_sampler); |
| 107 | |
| 108 | auto &lhs_tile = lhs->tile(); |
| 109 | auto &rhs_tile = rhs->tile(); |
| 110 | const auto &sampler = lhs->tile_sampler(); |
| 111 | |
| 112 | // Prepare the output tile. |
| 113 | if(!dst->has_tile()) |
| 114 | { |
| 115 | auto &tile = writer->declare_tile("dst_tile", lhs_tile.tile_info()); |
| 116 | dst->init_virtual_tensor(tile, sampler); |
| 117 | } |
| 118 | |
| 119 | auto &dst_tile = dst->tile(); |
| 120 | |
| 121 | // Perform the operation. |
| 122 | writer->op_binary_expression(dst_tile, lhs_tile, rhs_tile, BinaryOp::Add); |
| 123 | } |
| 124 | |
| 125 | Window GpuCkwElementwiseBinary::get_window() const |
| 126 | { |
| 127 | ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized"); |
| 128 | |
| 129 | TensorShape output_shape = _dst->tensor_shape(); |
| 130 | // Collapse Dim 1 (W) and Dim 2 (H) together, leave Dim 0 (C) unchanged |
| 131 | // This is in line with the collapsing convention used by operators like Conv2d |
| 132 | output_shape.collapse(2U, 1U); |
| 133 | // constexpr unsigned int vector_size_byte_opencl = 16; |
| 134 | // const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / _dst->element_size(), _dst->dimension(0)); |
| 135 | const unsigned int num_elems_processed_per_iteration = 1U; // Hard-coded for now |
| 136 | Window win = calculate_max_window(output_shape, Steps(num_elems_processed_per_iteration)); |
| 137 | |
| 138 | return win; |
| 139 | } |
| 140 | |
| 141 | } // namespace dynamic_fusion |
| 142 | } // namespace experimental |
| 143 | } // namespace arm_compute |