From aecb5d93225deace061cb7038d5844eec32c4e52 Mon Sep 17 00:00:00 2001 From: Gunes Bayir Date: Sun, 18 Dec 2022 21:31:29 +0000 Subject: Implement Logits1DMaxShiftExpSum kernel component in dynamic fusion Resolves: COMPMID-5719 Change-Id: I2f0911ffccce2b42a9a63fe6826eaa5d2cad06ba Signed-off-by: Gunes Bayir Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/8831 Comments-Addressed: Arm Jenkins Reviewed-by: Gian Marco Iodice Reviewed-by: Viet-Hoa Do Benchmark: Arm Jenkins Tested-by: Arm Jenkins --- .../sketch/attributes/SoftmaxAttributes.cpp | 68 +++++ .../cl/ClComponentLogits1DMaxShiftExpSum.cpp | 97 ++++++++ .../cl/ClComponentLogits1DMaxShiftExpSum.h | 131 ++++++++++ .../cl/ClTemplateLogits1DMaxShiftExpSum.cpp | 277 +++++++++++++++++++++ .../cl/ClTemplateLogits1DMaxShiftExpSum.h | 105 ++++++++ 5 files changed, 678 insertions(+) create mode 100644 src/dynamic_fusion/sketch/attributes/SoftmaxAttributes.cpp create mode 100644 src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DMaxShiftExpSum.cpp create mode 100644 src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DMaxShiftExpSum.h create mode 100644 src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.cpp create mode 100644 src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.h (limited to 'src/dynamic_fusion') diff --git a/src/dynamic_fusion/sketch/attributes/SoftmaxAttributes.cpp b/src/dynamic_fusion/sketch/attributes/SoftmaxAttributes.cpp new file mode 100644 index 0000000000..5d4d666263 --- /dev/null +++ b/src/dynamic_fusion/sketch/attributes/SoftmaxAttributes.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2022 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 "arm_compute/dynamic_fusion/sketch/attributes/SoftmaxAttributes.h" + +namespace arm_compute +{ +namespace experimental +{ +namespace dynamic_fusion +{ +SoftmaxAttributes &SoftmaxAttributes::beta(float beta) +{ + _beta = beta; + return *this; +} + +float SoftmaxAttributes::beta() const +{ + return _beta; +} + +SoftmaxAttributes &SoftmaxAttributes::is_log_softmax(bool is_log_softmax) +{ + _is_log_softmax = is_log_softmax; + return *this; +} + +bool SoftmaxAttributes::is_log_softmax() const +{ + return _is_log_softmax; +} + +SoftmaxAttributes &SoftmaxAttributes::axis(int axis) +{ + _axis = axis; + return *this; +} + +int SoftmaxAttributes::axis() const +{ + return _axis; +} + +} // namespace dynamic_fusion +} // namespace experimental +} // namespace arm_compute diff --git a/src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DMaxShiftExpSum.cpp b/src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DMaxShiftExpSum.cpp new file mode 100644 index 0000000000..8ab1853e84 --- /dev/null +++ b/src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DMaxShiftExpSum.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2022 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/components/cl/ClComponentLogits1DMaxShiftExpSum.h" + +#include "arm_compute/core/CL/CLHelpers.h" +#include "arm_compute/core/Validate.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "arm_compute/dynamic_fusion/sketch/attributes/DepthwiseConv2dAttributes.h" +#include "src/core/CL/CLValidate.h" +#include "src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateDepthwiseConv2d.h" +#include "src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.h" + +namespace arm_compute +{ +namespace experimental +{ +namespace dynamic_fusion +{ +Status ClComponentLogits1DMaxShiftExpSum::validate( + const Properties &properties, + const ArgumentPack &tensors, + const Attributes &attributes) +{ + ARM_COMPUTE_UNUSED(properties, attributes); + + const ITensorInfo *src = tensors.get_const_tensor(TensorType::ACL_SRC_0); + const ITensorInfo *sum = tensors.get_const_tensor(TensorType::ACL_DST_0); + const ITensorInfo *dst = tensors.get_const_tensor(TensorType::ACL_DST_1); + + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, sum, dst); + + // 1. Check validity + // All tensor infos are initialized + ARM_COMPUTE_RETURN_ERROR_ON(src->tensor_shape().total_size() == 0); + ARM_COMPUTE_RETURN_ERROR_ON(sum->tensor_shape().total_size() == 0); + ARM_COMPUTE_RETURN_ERROR_ON(dst->tensor_shape().total_size() == 0); + + // Check for mismatches in shapes and data types + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst, sum); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst); + + const uint32_t src_dimensions = src->tensor_shape().num_dimensions(); + const uint32_t sum_dimensions = sum->tensor_shape().num_dimensions(); + + ARM_COMPUTE_RETURN_ERROR_ON(src_dimensions != sum_dimensions + 1); + + // Device requirements are met + ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src); + + // 2. Check support level + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32); + + return Status{}; +} + +ClComponentLogits1DMaxShiftExpSum::ClComponentLogits1DMaxShiftExpSum(ComponentId id, + const Properties &properties, + const ArgumentPack &tensors, + const Attributes &attributes) + : IGpuKernelComponent{ id, properties, tensors }, + _component_writer{ std::make_unique(id, tensors, attributes) } +{ +} + +ClComponentLogits1DMaxShiftExpSum::~ClComponentLogits1DMaxShiftExpSum() +{ +} + +const IGpuTemplateComponentWriter *ClComponentLogits1DMaxShiftExpSum::template_writer() const +{ + return _component_writer.get(); +} +} // namespace dynamic_fusion +} // namespace experimental +} // namespace arm_compute \ No newline at end of file diff --git a/src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DMaxShiftExpSum.h b/src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DMaxShiftExpSum.h new file mode 100644 index 0000000000..af47609a2c --- /dev/null +++ b/src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DMaxShiftExpSum.h @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2022 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_COMPONENTS_CL_CLCOMPONENTLOGITS1DMAXSHIFTEXPSUM +#define SRC_DYNAMIC_FUSION_SKETCH_GPU_COMPONENTS_CL_CLCOMPONENTLOGITS1DMAXSHIFTEXPSUM + +#include "arm_compute/dynamic_fusion/sketch/attributes/SoftmaxAttributes.h" +#include "src/dynamic_fusion/sketch/gpu/components/IGpuKernelComponent.h" + +namespace arm_compute +{ +/** Forward declaration */ +class ITensorInfo; +namespace experimental +{ +namespace dynamic_fusion +{ +/** Forward declaration */ +template +class ArgumentPack; + +/** Forward declaration */ +class ClTemplateLogits1DMaxShiftExpSum; + +/** Component to calculate max-shifted exponentials and their sum + * + * 1D example: + * input: [x1, x2, ... , xn], shape: (1 x d) + * + * Let max(x1...xn) = m + * + * (output) sum: [exp(x1-m) + ... + exp(xn-m)], shape: (1 x 1) + * (output) dst: [exp(x1-m) ... exp(xn-m)], shape: (1 x d) + * + * This component is used by the softmax operator. The subsequent + * operation normalizes dst with sum, therefore the max-shifting + * since exp(m) will be cancelled in numerator and denominator. +*/ +class ClComponentLogits1DMaxShiftExpSum final : public IGpuKernelComponent +{ +public: + /** Attributes are a set of backend-agnostic parameters that define what a component does */ + using Attributes = SoftmaxAttributes; + + /** Validate the component + * + * @param[in] properties Component properties @ref Properties + * @param[in,out] tensors Tensor arguments to the component + * @param[in] attributes Component attributes @ref Attributes + * + * @return Status Validation results + * + * Tensor argument names: + * - ACL_SRC_0: Input + * - ACL_DST_0: Output + * - ACL_DST_1: Output + * + * Tensor argument constness: + * - ACL_SRC_0: Const + * - ACL_DST_0: Const + * - ACL_DST_1: Const + * + * Valid data layouts: + * - All + * + ** Valid data type configurations: + * |ACL_SRC_0 |ACL_DST_0 |ACL_DST_1 | + * |:----------|:----------|:----------| + * |F16 | F16 | F16 | + * |F32 | F32 | F32 | + */ + static Status validate( + const Properties &properties, + const ArgumentPack &tensors, + const Attributes &attributes); + + /** Constructor + * + * Similar to @ref ClComponentLogits1DMaxShiftExpSum::validate() + */ + ClComponentLogits1DMaxShiftExpSum(ComponentId id, + const Properties &properties, + const ArgumentPack &tensors, + const Attributes &attributes); + + /** Destructor */ + ~ClComponentLogits1DMaxShiftExpSum() override; + /** Prevent instances of this class from being copy constructed */ + ClComponentLogits1DMaxShiftExpSum(const ClComponentLogits1DMaxShiftExpSum &component) = delete; + /** Prevent instances of this class from being copied */ + ClComponentLogits1DMaxShiftExpSum &operator=(const ClComponentLogits1DMaxShiftExpSum &component) = delete; + /** Allow instances of this class to be move constructed */ + ClComponentLogits1DMaxShiftExpSum(ClComponentLogits1DMaxShiftExpSum &&component) = default; + /** Allow instances of this class to be moved */ + ClComponentLogits1DMaxShiftExpSum &operator=(ClComponentLogits1DMaxShiftExpSum &&component) = default; + /** Get template writer for the component */ + const IGpuTemplateComponentWriter *template_writer() const override; + /** Get component type */ + GpuComponentType type() const override + { + return GpuComponentType::Unfusable; + } + +private: + std::unique_ptr _component_writer; +}; +} // namespace dynamic_fusion +} // namespace experimental +} // namespace arm_compute + +#endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_COMPONENTS_CL_CLCOMPONENTLOGITS1DMAXSHIFTEXPSUM */ diff --git a/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.cpp b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.cpp new file mode 100644 index 0000000000..05bdd27f11 --- /dev/null +++ b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.cpp @@ -0,0 +1,277 @@ +/* + * Copyright (c) 2022 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/ClTemplateLogits1DMaxShiftExpSum.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 +{ +ClTemplateLogits1DMaxShiftExpSum::ClTemplateLogits1DMaxShiftExpSum(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_DST_0); + _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_1); + ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _sum, _dst); +} + +std::string ClTemplateLogits1DMaxShiftExpSum::get_name() const +{ + return "logits_1d_max_shift_exp_sum"; +} + +std::string ClTemplateLogits1DMaxShiftExpSum::get_component_code(const ComponentGroup &comp_group) const +{ + ARM_COMPUTE_UNUSED(comp_group); + + std::string code = R"_( +//------------------ START KERNEL {{meta_kernel_id}} --------------------- +#define VEC_TYPE VEC_DATA_TYPE({{DATA_TYPE}}, N0) +#define SELECT_TYPE SELECT_VEC_DATA_TYPE({{DATA_TYPE}}, N0) +{ + __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}}); +)_"; + + const bool beta_defined = (_attributes.beta() != 1.f); + + if(beta_defined) + { + code += R"_( + VEC_TYPE beta = (VEC_TYPE){{BETA}}; +)_"; + } + + 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); + const bool non_multiple_of_n0 = ((reduction_dim_size % vector_size) != 0); + + if(non_multiple_of_n0) + { + code += R"_( + VEC_TYPE data = VLOAD(N0)(0, (__global {{DATA_TYPE}} *)src_addr); + SELECT_TYPE widx = (SELECT_TYPE)PARTIAL_N0 > VEC_OFFS(SELECT_DATA_TYPE({{DATA_TYPE}}), N0); + max_val_vec = max(max_val_vec, select((VEC_TYPE)({{MINVAL}}), data, widx)); +)_"; + } + + 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}}))); + max_val_vec = max(data, max_val_vec); + } + + {{DATA_TYPE}} max_val = MAX_REDUCE(max_val_vec, N0); + VEC_TYPE sum1D = 0; +)_"; + + if(non_multiple_of_n0) + { + code += R"_( + data -= max_val; +)_"; + if(beta_defined) + { + code += R"_( + data *= beta; +)_"; + } + + if(_attributes.is_log_softmax()) + { + code += R"_( + VSTORE_PARTIAL(N0, PARTIAL_N0) + (data, 0, (__global {{DATA_TYPE}} *)dst_addr); + data = exp(data); + data = select(0, data, widx); +)_"; + } + else + { + code += R"_( + data = exp(data); + data = select(0, data, widx); + VSTORE_PARTIAL(N0, PARTIAL_N0) + (data, 0, (__global {{DATA_TYPE}} *)dst_addr); +)_"; + } + + code += R"_( + sum1D += data; +)_"; + } + else + { + 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(_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}}))); +)_"; + } + + code += R"_( + sum1D += data; + } +)_"; + } + + code += R"_( + *((__global {{DATA_TYPE}} *)sum.ptr) = SUM_REDUCE(sum1D, N0); +} +//------------------ END KERNEL {{meta_kernel_id}} --------------------- +)_"; + + return code; +} + +void ClTemplateLogits1DMaxShiftExpSum::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const +{ + vtable.declare_variable( + _src, + GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer), + comp_group.is_intermediate_tensor(_src), + "src"); + + vtable.declare_variable( + _sum, + GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer), + comp_group.is_intermediate_tensor(_sum), + "sum"); + + vtable.declare_variable( + _dst, + GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer), + comp_group.is_intermediate_tensor(_dst), + "dst"); +} + +TagLUT ClTemplateLogits1DMaxShiftExpSum::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); + lut["BETA"] = float_to_string_with_full_precision(_attributes.beta()); + lut["MINVAL"] = (data_type == DataType::F16) ? std::string("-HALF_MAX") : std::string("-FLT_MAX"); + lut["SRC_WIDTH"] = support::cpp11::to_string(_src->dimension(0)); + + return lut; +} + +CLBuildOptions ClTemplateLogits1DMaxShiftExpSum::get_build_options(const ComponentGroup &comp_group) const +{ + 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); + + build_opts.add_option("-DN0=" + support::cpp11::to_string(vector_size)); + build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string((reduction_dim_size % vector_size))); + + return build_opts; +} + +std::string ClTemplateLogits1DMaxShiftExpSum::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 ClTemplateLogits1DMaxShiftExpSum::get_headers_list() const +{ + return std::set{ "helpers.h" }; +} + +Window ClTemplateLogits1DMaxShiftExpSum::get_window() const +{ + ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized"); + + Window win = calculate_max_window(*_dst, Steps(_src->dimension(0))); + return win.collapse(win, Window::DimZ); +} + +} // namespace dynamic_fusion +} // namespace experimental +} // namespace arm_compute \ No newline at end of file diff --git a/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.h b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.h new file mode 100644 index 0000000000..5d232c0cf2 --- /dev/null +++ b/src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateLogits1DMaxShiftExpSum.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2022 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_CLTEMPLATELOGITS1DMAXSHIFTEXPSUM +#define SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_CL_CLTEMPLATELOGITS1DMAXSHIFTEXPSUM + +#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DMaxShiftExpSum.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 ClTemplateLogits1DMaxShiftExpSum final : public IGpuTemplateComponentWriter +{ +public: + using Attributes = ClComponentLogits1DMaxShiftExpSum::Attributes; + + /** Constructor + * + * @param[in] id Component id + * @param[in] tensors Tensor arguments to the components + * @param[in] attributes Component attributes + */ + ClTemplateLogits1DMaxShiftExpSum(ComponentId id, const ArgumentPack &tensors, const Attributes &attributes); + /** Prevent instances of this class from being copy constructed */ + ClTemplateLogits1DMaxShiftExpSum(const ClTemplateLogits1DMaxShiftExpSum &) = delete; + /** Prevent instances of this class from being copied */ + ClTemplateLogits1DMaxShiftExpSum &operator=(const ClTemplateLogits1DMaxShiftExpSum &) = delete; + /** Allow instances of this class to be move constructed */ + ClTemplateLogits1DMaxShiftExpSum(ClTemplateLogits1DMaxShiftExpSum &&) = default; + /** Allow instances of this class to be moved */ + ClTemplateLogits1DMaxShiftExpSum &operator=(ClTemplateLogits1DMaxShiftExpSum &&) = 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; // input + const ITensorInfo *_sum; // exponentiated and summed input + const ITensorInfo *_dst; // exponentiated input + Attributes _attributes; +}; +} // namespace dynamic_fusion +} // namespace experimental +} // namespace arm_compute + +#endif /* SRC_DYNAMIC_FUSION_SKETCH_GPU_TEMPLATE_WRITER_CL_CLTEMPLATELOGITS1DMAXSHIFTEXPSUM */ -- cgit v1.2.1