From 002e6530f6218b00a28aef9be8b21efb08cf3602 Mon Sep 17 00:00:00 2001 From: Ramy Elgammal Date: Wed, 11 Jan 2023 18:48:04 +0000 Subject: Implement dynamic fusion softmax operator - Return aux tensorInfo by get_aux_tensors() at runtime to init the aux tensor with the right size. - Keep softmax unfusable for this commit - Hence, added Tensor3D to template writer arguments declaration, for sake of keeping dynamic fusion softmax componenets' kernels matching their cl counterparts. Resolves: COMPMID-5523 Change-Id: I667f39545db925f667036ef448302c79a0330373 Signed-off-by: Ramy Elgammal Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/c/VisualCompute/ComputeLibrary/+/483924 Tested-by: bsgcomp Reviewed-by: Gunes Bayir Comments-Addressed: bsgcomp Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/8986 Comments-Addressed: Arm Jenkins Reviewed-by: Jakub Sujak Tested-by: Arm Jenkins Benchmark: Arm Jenkins --- .../cl/ClTemplateLogits1DMaxShiftExpSum.cpp | 67 ++++---- .../template_writer/cl/ClTemplateLogits1DNorm.cpp | 185 +++++++++++++++++++++ .../template_writer/cl/ClTemplateLogits1DNorm.h | 106 ++++++++++++ .../gpu/template_writer/cl/ClTemplateWriter.cpp | 16 +- 4 files changed, 336 insertions(+), 38 deletions(-) create mode 100644 src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DNorm.cpp create mode 100644 src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DNorm.h (limited to 'src/dynamic_fusion/sketch/gpu/template_writer') diff --git a/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.cpp b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.cpp index 8f1ed95351..6de0ba7617 100644 --- a/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.cpp +++ b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Arm Limited. + * Copyright (c) 2022-2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -34,6 +34,10 @@ namespace experimental { namespace dynamic_fusion { +namespace +{ + constexpr unsigned int serial_vector_size = 8; +} // namespace ClTemplateLogits1DMaxShiftExpSum::ClTemplateLogits1DMaxShiftExpSum(ComponentId id, const ArgumentPack &tensors, const Attributes &attributes) @@ -46,7 +50,9 @@ ClTemplateLogits1DMaxShiftExpSum::ClTemplateLogits1DMaxShiftExpSum(ComponentId _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0); _sum = this->tensors().get_const_tensor(TensorType::ACL_DST_0); _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_1); - ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _sum, _dst); + ARM_COMPUTE_ERROR_ON_NULLPTR(_src); + ARM_COMPUTE_ERROR_ON_NULLPTR(_sum); + ARM_COMPUTE_ERROR_ON_NULLPTR(_dst); } std::string ClTemplateLogits1DMaxShiftExpSum::get_name() const @@ -65,7 +71,6 @@ std::string ClTemplateLogits1DMaxShiftExpSum::get_component_code(const Component { __global uchar *src_addr = {{src}}_ptr + {{src}}_offset_first_element_in_bytes + g_ind_1 * {{src}}_stride_y + g_ind_2 * {{src}}_stride_z; __global uchar *dst_addr = {{dst}}_ptr + {{dst}}_offset_first_element_in_bytes + g_ind_1 * {{dst}}_stride_y + g_ind_2 * {{dst}}_stride_z; - Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT({{sum}}); VEC_TYPE max_val_vec = (VEC_TYPE)({{MINVAL}}); )_"; @@ -139,44 +144,41 @@ std::string ClTemplateLogits1DMaxShiftExpSum::get_component_code(const Component sum1D += data; )_"; } - else - { - code += R"_( + code += R"_( for(uint i = PARTIAL_N0; i < {{SRC_WIDTH}}; i += N0) { VEC_TYPE data = VLOAD(N0)(0, (__global {{DATA_TYPE}} *)(src_addr + i * sizeof({{DATA_TYPE}}))); data -= max_val; )_"; - if(beta_defined) - { - code += R"_( - data *= beta; + if(beta_defined) + { + code += R"_( + data *= beta; )_"; - } + } - if(_attributes.is_log_softmax()) - { - code += R"_( - VSTORE(N0) - (data, 0, (__global {{DATA_TYPE}} *)(dst_addr + i * sizeof({{DATA_TYPE}}))); - data = exp(data); + if(_attributes.is_log_softmax()) + { + code += R"_( + VSTORE(N0) + (data, 0, (__global {{DATA_TYPE}} *)(dst_addr + i * sizeof({{DATA_TYPE}}))); + data = exp(data); )_"; - } - else - { - code += R"_( - data = exp(data); - VSTORE(N0) - (data, 0, (__global {{DATA_TYPE}} *)(dst_addr + i * sizeof({{DATA_TYPE}}))); + } + else + { + code += R"_( + data = exp(data); + VSTORE(N0) + (data, 0, (__global {{DATA_TYPE}} *)(dst_addr + i * sizeof({{DATA_TYPE}}))); )_"; - } + } - code += R"_( - sum1D += data; + code += R"_( + sum1D += data; } )_"; - } code += R"_( *((__global {{DATA_TYPE}} *)sum.ptr) = SUM_REDUCE(sum1D, N0); @@ -192,19 +194,19 @@ void ClTemplateLogits1DMaxShiftExpSum::declare_variables(GpuKernelVariableTable vtable.declare_variable( comp_group, _src, - GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer), + GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D), "src"); vtable.declare_variable( comp_group, _sum, - GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer), + GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D), "sum"); vtable.declare_variable( comp_group, _dst, - GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer), + GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D), "dst"); } @@ -237,7 +239,6 @@ CLBuildOptions ClTemplateLogits1DMaxShiftExpSum::get_build_options(const Compone ARM_COMPUTE_UNUSED(comp_group); CLBuildOptions build_opts{}; - constexpr unsigned int serial_vector_size = 8; const unsigned int reduction_dim_size = _src->dimension(0); const unsigned int vector_size = adjust_vec_size(serial_vector_size, reduction_dim_size); @@ -261,7 +262,7 @@ std::string ClTemplateLogits1DMaxShiftExpSum::get_config_id() const std::set ClTemplateLogits1DMaxShiftExpSum::get_headers_list() const { - return std::set{ "helpers.h" }; + return std::set{ "helpers.h", "tile_helpers.h" }; } Window ClTemplateLogits1DMaxShiftExpSum::get_window() const diff --git a/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DNorm.cpp b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DNorm.cpp new file mode 100644 index 0000000000..0e1c9ef28f --- /dev/null +++ b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DNorm.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DNorm.h" + +#include "src/core/helpers/WindowHelpers.h" +#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h" +#include "support/StringSupport.h" + +namespace arm_compute +{ +namespace experimental +{ +namespace dynamic_fusion +{ +ClTemplateLogits1DNorm::ClTemplateLogits1DNorm(ComponentId id, + const ArgumentPack &tensors, + const Attributes &attributes) + : IGpuTemplateComponentWriter{ id, tensors }, + _src{}, + _sum{}, + _dst{}, + _attributes{ attributes } +{ + _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0); + _sum = this->tensors().get_const_tensor(TensorType::ACL_SRC_1); + _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0); + ARM_COMPUTE_ERROR_ON_NULLPTR(_src); + ARM_COMPUTE_ERROR_ON_NULLPTR(_sum); + ARM_COMPUTE_ERROR_ON_NULLPTR(_dst); +} + +std::string ClTemplateLogits1DNorm::get_name() const +{ + return "logits_1d_norm"; +} + +std::string ClTemplateLogits1DNorm::get_component_code(const ComponentGroup &comp_group) const +{ + ARM_COMPUTE_UNUSED(comp_group); + + std::string code = R"_( +//------------------ START KERNEL {{meta_kernel_id}} --------------------- +{ + const int x_offs = g_ind_0 * sizeof({{DATA_TYPE}}); + __global uchar *src_addr = {{src}}_ptr + {{src}}_offset_first_element_in_bytes + x_offs + g_ind_1 * {{src}}_stride_y + g_ind_2 * {{src}}_stride_z; + __global uchar *dst_addr = {{dst}}_ptr + {{dst}}_offset_first_element_in_bytes + x_offs + g_ind_1 * {{dst}}_stride_y + g_ind_2 * {{dst}}_stride_z; + Image sum = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP({{sum}}); +)_"; + // Load max value of 1D logits vector (row) + code += R"_( + {{DATA_TYPE}} sum_val = *((__global {{DATA_TYPE}} *)offset(&sum, 0, g_ind_1)); + VEC_DATA_TYPE({{DATA_TYPE}}, N0) + data0 = VLOAD(N0)(0, (__global {{DATA_TYPE}} *)src_addr); +)_"; + + if(_attributes.is_log_softmax()) + { + code += R"_( + sum_val = log(sum_val); + data0 -= sum_val; +)_"; + } + else + { + code += R"_( + data0 /= sum_val; +)_"; + } + + code += R"_( + STORE_VECTOR_SELECT(data, {{DATA_TYPE}}, dst_addr, N0, PARTIAL_N0, PARTIAL_N0 != 0 && g_ind_0 == 0); +} +//------------------ END KERNEL {{meta_kernel_id}} --------------------- +)_"; + + return code; +} + +void ClTemplateLogits1DNorm::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const +{ + vtable.declare_variable( + comp_group, + _src, + GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D), + "src"); + + vtable.declare_variable( + comp_group, + _sum, + GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D), + "sum"); + + vtable.declare_variable( + comp_group, + _dst, + GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_3D), + "dst"); +} + +TagLUT ClTemplateLogits1DNorm::get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const +{ + ARM_COMPUTE_UNUSED(comp_group); + + TagLUT lut{}; + + // Arguments and global shared variables + lut["src"] = vtable.get_variable(_src); + lut["sum"] = vtable.get_variable(_sum); + lut["dst"] = vtable.get_variable(_dst); + + // Local build options + lut["meta_kernel_id"] = id(); + + const DataType data_type = _src->data_type(); + + lut["DATA_TYPE"] = get_cl_type_from_data_type(data_type); + + return lut; +} + +CLBuildOptions ClTemplateLogits1DNorm::get_build_options(const ComponentGroup &comp_group) const +{ + ARM_COMPUTE_UNUSED(comp_group); + CLBuildOptions build_opts{}; + + const auto root_window = comp_group.get_root_component()->template_writer()->get_window(); + const unsigned int n0 = root_window.x().step(); + build_opts.add_option("-DN0=" + support::cpp11::to_string(n0)); + build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string((_src->dimension(0) % n0))); + + return build_opts; +} + +std::string ClTemplateLogits1DNorm::get_config_id() const +{ + std::string config_id = get_name(); + + config_id += "_"; + config_id += support::cpp11::to_string(_src->dimension(0)); + config_id += "_"; + config_id += string_from_data_type(_src->data_type()); + + return config_id; +} + +std::set ClTemplateLogits1DNorm::get_headers_list() const +{ + return std::set{ "helpers.h", "tile_helpers.h" }; +} + +Window ClTemplateLogits1DNorm::get_window() const +{ + ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized"); + constexpr unsigned int serial_vector_size = 16; + const unsigned int vector_size = adjust_vec_size(serial_vector_size, _src->dimension(0)); + + Window win = calculate_max_window(*_src, Steps(vector_size)); + return win.collapse(win, Window::DimZ); +} + +} // namespace dynamic_fusion +} // namespace experimental +} // namespace arm_compute diff --git a/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DNorm.h b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DNorm.h new file mode 100644 index 0000000000..5a74be5842 --- /dev/null +++ b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DNorm.h @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_CL_CLTEMPLATELOGITS1DNORM +#define SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_CL_CLTEMPLATELOGITS1DNORM + +#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DNorm.h" +#include "src/dynamic_fusion/sketch/gpu/template_writer/GpuKernelVariableTable.h" +#include "src/dynamic_fusion/sketch/gpu/template_writer/IGpuTemplateComponentWriter.h" + +namespace arm_compute +{ +namespace experimental +{ +namespace dynamic_fusion +{ +class ClTemplateLogits1DNorm final : public IGpuTemplateComponentWriter +{ +public: + using Attributes = ClComponentLogits1DNorm::Attributes; + + /** Constructor + * + * @param[in] id Component id + * @param[in] tensors Tensor arguments to the components + * @param[in] attributes Component attributes + */ + ClTemplateLogits1DNorm(ComponentId id, const ArgumentPack &tensors, const Attributes &attributes); + /** Prevent instances of this class from being copy constructed */ + ClTemplateLogits1DNorm(const ClTemplateLogits1DNorm &) = delete; + /** Prevent instances of this class from being copied */ + ClTemplateLogits1DNorm &operator=(const ClTemplateLogits1DNorm &) = delete; + /** Allow instances of this class to be move constructed */ + ClTemplateLogits1DNorm(ClTemplateLogits1DNorm &&) = default; + /** Allow instances of this class to be moved */ + ClTemplateLogits1DNorm &operator=(ClTemplateLogits1DNorm &&) = default; + /** Generate kernel component name */ + std::string get_name() const override; + /** Generate kernel component code template + * + * @param[in] comp_group Component group of which the component is a part of + * + * @return std::string Component code + */ + std::string get_component_code(const ComponentGroup &comp_group) const override; + /** Declare all variables used by the component in the @p vtable + * + * @param[out] vtable Variable table + * @param[in] comp_group Component group of which the component is a part of + */ + void declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const override; + /** Generate the tag look-up table used to instantiate the component code. + * + * @param[in] vtable Variable table + * @param[in] comp_group Component group of which the component is a part of + * + * @return TagLUT Tag lookup table + */ + TagLUT get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const override; + /** Generate the build options used in the component + * + * @param[in] comp_group Component group of which the component is a part of + * + * @return CLBuildOptions Build options + */ + CLBuildOptions get_build_options(const ComponentGroup &comp_group) const override; + /** Generate the component config id string used for tuning */ + std::string get_config_id() const override; + /** Generate the header list used in the component */ + std::set get_headers_list() const override; + /** Generate the execution window for the component */ + Window get_window() const override; + +private: + const ITensorInfo *_src; // exponentiated input + const ITensorInfo *_sum; // exponentiated and summed input + const ITensorInfo *_dst; // normalization of input with _sum + + Attributes _attributes; +}; +} // namespace dynamic_fusion +} // namespace experimental +} // namespace arm_compute + +#endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_CL_CLTEMPLATELOGITS1DNORM */ diff --git a/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateWriter.cpp b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateWriter.cpp index 2ab6316947..eda15f1d95 100644 --- a/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateWriter.cpp +++ b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateWriter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Arm Limited. + * Copyright (c) 2022-2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -180,7 +180,8 @@ std::string ClTemplateWriter::write_code() } auto arguments = _components.get_argument_tensors(); - std::sort(arguments.begin(), arguments.end(), [](const ITensorInfo *l, const ITensorInfo *r) { + std::sort(arguments.begin(), arguments.end(), [](const ITensorInfo * l, const ITensorInfo * r) + { return l->id() < r->id(); }); code += write_kernel_signature(_vtable.get_variable_list(arguments)); @@ -192,16 +193,16 @@ std::string ClTemplateWriter::write_code() code += " //------------------ END KERNEL_BUILDER_COORDINATE ---------------------\n"; { - const auto tiles = _components.get_tiles(); + const auto tiles = _components.get_tiles(); std::stringstream tiles_ss; tiles_ss << " //------------------ START TILE DECLARATION ---------------------\n"; for(auto tile : tiles) { - const auto var = _vtable.get_variable(tile); + const auto var = _vtable.get_variable(tile); const auto data_type = get_cl_type_from_data_type(tile->data_type()); - const auto var_name = var.uniq_name; + const auto var_name = var.uniq_name; tiles_ss << " TILE(" << data_type << ", M0, N0, " << var_name << ");\n"; } @@ -276,6 +277,11 @@ std::string ClTemplateWriter::write_argument_declaration(const GpuKernelVariable code += "\n TENSOR4D_T(" + var.uniq_name + ", IMAGE)"; break; } + case GpuKernelArgumentInfo::Type::Tensor_3D: + { + code += "\n TENSOR3D_DECLARATION(" + var.uniq_name + ")"; + break; + } default: { ARM_COMPUTE_ERROR("Unsupported declaration generation for GpuKernelArgumentInfo::Type"); -- cgit v1.2.1