From b56c1758dfc233452ff73149fabe30e1c460e9d3 Mon Sep 17 00:00:00 2001 From: Manuel Bottini Date: Wed, 18 Nov 2020 17:56:30 +0000 Subject: Generalization of CLTuner Rename lws to tuning parameters in functions used externally Add new generalized objects for the OpenCL Tuner to accommodate further possible tuning parameters Resolves: COMPMID-3935 Change-Id: I0f2a0f89bca5dae4a4e4adce2f7c7cae32ecb84a Signed-off-by: Manuel Bottini Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/4584 Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins Reviewed-by: Georgios Pinitas --- Android.bp | 2 +- arm_compute/runtime/CL/CLTuner.h | 85 +++++-- arm_compute/runtime/CL/CLTunerTypes.h | 8 +- arm_compute/runtime/CL/CLTuningParams.h | 59 +++++ arm_compute/runtime/CL/tuners/CLLWSList.h | 214 ------------------ .../runtime/CL/tuners/CLTuningParametersList.h | 86 +++++++ src/core/NEON/kernels/NESelectKernel.cpp | 3 +- src/graph/backends/CL/CLDeviceBackend.cpp | 9 +- src/runtime/CL/CLTuner.cpp | 80 +++++-- src/runtime/CL/tuners/CLLWSList.cpp | 114 ---------- src/runtime/CL/tuners/CLTuningParametersList.cpp | 249 +++++++++++++++++++++ 11 files changed, 527 insertions(+), 382 deletions(-) create mode 100644 arm_compute/runtime/CL/CLTuningParams.h delete mode 100644 arm_compute/runtime/CL/tuners/CLLWSList.h create mode 100644 arm_compute/runtime/CL/tuners/CLTuningParametersList.h delete mode 100644 src/runtime/CL/tuners/CLLWSList.cpp create mode 100644 src/runtime/CL/tuners/CLTuningParametersList.cpp diff --git a/Android.bp b/Android.bp index 6e9756ec96..040ff446a1 100644 --- a/Android.bp +++ b/Android.bp @@ -593,7 +593,7 @@ cc_library_static { "src/runtime/CL/gemm/CLGEMMDefaultTypeMidgard.cpp", "src/runtime/CL/gemm/CLGEMMDefaultTypeValhall.cpp", "src/runtime/CL/tuners/BifrostTuner.cpp", - "src/runtime/CL/tuners/CLLWSList.cpp", + "src/runtime/CL/tuners/CLTuningParametersList.cpp", "src/runtime/CL/tuners/MidgardTuner.cpp", "src/runtime/CPP/CPPScheduler.cpp", "src/runtime/CPP/ICPPSimpleFunction.cpp", diff --git a/arm_compute/runtime/CL/CLTuner.h b/arm_compute/runtime/CL/CLTuner.h index 3b45a2177e..9814867142 100644 --- a/arm_compute/runtime/CL/CLTuner.h +++ b/arm_compute/runtime/CL/CLTuner.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020 Arm Limited. + * Copyright (c) 2017-2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -25,7 +25,9 @@ #define ARM_COMPUTE_CLTUNER_H #include "arm_compute/core/CL/OpenCL.h" +#include "arm_compute/core/utils/misc/Macros.h" #include "arm_compute/runtime/CL/CLTunerTypes.h" +#include "arm_compute/runtime/CL/CLTuningParams.h" #include "arm_compute/runtime/CL/ICLTuner.h" #include @@ -41,9 +43,10 @@ public: /** Constructor * * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ? + * @param[in] tuning_info (Optional) opencl parameters to tune * */ - CLTuner(bool tune_new_kernels = true); + CLTuner(bool tune_new_kernels = true, CLTuningInfo tuning_info = CLTuningInfo()); /** Destructor */ ~CLTuner() = default; @@ -53,21 +56,30 @@ public: * @param[in] tune_new_kernels Find the optimal local workgroup size for kernels which are not present in the table ? */ void set_tune_new_kernels(bool tune_new_kernels); - /** Tune kernels that are not in the LWS table + + /** Tune kernels that are not in the tuning parameters table * * @return True if tuning of new kernels is enabled. */ bool tune_new_kernels() const; + /** Setter for tune parameters option + * + * @param[in] tuning_info opencl parameters to tune + */ + void set_tuning_parameters(CLTuningInfo tuning_info); + /** Set OpenCL tuner mode * - * @param[in] mode Indicates how exhaustive the search for the optimal LWS should be while tuning. Default is Exhaustive mode + * @param[in] mode Indicates how exhaustive the search for the optimal tuning parameters should be while tuning. Default is Exhaustive mode */ void set_tuner_mode(CLTunerMode mode); /** Get the current OpenCL tuner mode * - * @return tuner_mode Indicates how exhaustive the search for the optimal LWS should be while tuning + * @return tuner_mode Indicates how exhaustive the search for the optimal tuning parameters should be while tuning + * + * @deprecated This function is deprecated and is intended to be removed in 21.05 release */ CLTunerMode get_tuner_mode() const; @@ -75,20 +87,48 @@ public: * * @param[in] kernel_id Unique identifiant of the kernel * @param[in] optimal_lws Optimal local workgroup size to use for the given kernel + * + * @deprecated This function is deprecated and is intended to be removed in 21.05 release */ + ARM_COMPUTE_DEPRECATED_REL_REPLACE(21.02, add_tuning_params) void add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws); + /** Manually add tuning parameters for a kernel + * + * @param[in] kernel_id Unique identifiant of the kernel + * @param[in] optimal_tuning_params Optimal tuning parameters to use for the given kernel + */ + void add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params); + /** Import LWS table * * @param[in] lws_table The unordered_map container to import + * + * @deprecated This function is deprecated and is intended to be removed in 21.05 release */ + ARM_COMPUTE_DEPRECATED_REL_REPLACE(21.02, import_tuning_params) void import_lws_table(const std::unordered_map &lws_table); + /** Import tuning parameters table + * + * @param[in] tuning_params_table The unordered_map container to import + */ + void import_tuning_params(const std::unordered_map &tuning_params_table); + /** Give read access to the LWS table * * @return The lws table as unordered_map container + * + * @deprecated This function is deprecated and is intended to be removed in 21.05 release + */ + ARM_COMPUTE_DEPRECATED_REL_REPLACE(21.02, tuning_params_table) + const std::unordered_map &lws_table(); + + /** Give read access to the tuning params table + * + * @return The tuning params table as unordered_map container */ - const std::unordered_map &lws_table() const; + const std::unordered_map &tuning_params_table() const; /** Set the OpenCL kernel event * @@ -101,17 +141,20 @@ public: /** clEnqueueNDRangeKernel symbol */ std::function real_clEnqueueNDRangeKernel; - /** Load the LWS table from file + /** Load the tuning parameters table from file. It also sets up the tuning read from the file + * + * @param[in] filename Load the tuning parameters table from this file.(Must exist) * - * @param[in] filename Load the LWS table from this file.(Must exist) */ void load_from_file(const std::string &filename); - /** Save the content of the LWS table to file + /** Save the content of the tuning parameters table to file * - * @param[in] filename Save the LWS table to this file. (Content will be overwritten) + * @param[in] filename Save the tuning parameters table to this file. (Content will be overwritten) + * + * @return true if the file was created */ - void save_to_file(const std::string &filename) const; + bool save_to_file(const std::string &filename) const; // Inherited methods overridden: void tune_kernel_static(ICLKernel &kernel) override; @@ -125,19 +168,21 @@ public: bool kernel_event_is_set() const; private: - /** Find optimal LWS using brute-force approach + /** Find optimal tuning parameters using brute-force approach * - * @param[in] kernel OpenCL kernel to be tuned with LWS + * @param[in] kernel OpenCL kernel to be tuned with tuning parameters * @param[in,out] tensors Tensors for the kernel to operate on * - * @return The optimal LWS to use + * @return The optimal tuning parameters to use */ - cl::NDRange find_optimal_lws(ICLKernel &kernel, ITensorPack &tensors); - - std::unordered_map _lws_table; - cl::Event _kernel_event; - bool _tune_new_kernels; - CLTunerMode _tuner_mode; + CLTuningParams find_optimal_tuning_params(ICLKernel &kernel, ITensorPack &tensors); + + std::unordered_map _tuning_params_table; + std::unordered_map _lws_table; + cl::Event _kernel_event; + bool _tune_new_kernels; + CLTuningInfo _tuning_info; + CLTunerMode _tuner_mode; }; } // namespace arm_compute #endif /*ARM_COMPUTE_CLTUNER_H */ diff --git a/arm_compute/runtime/CL/CLTunerTypes.h b/arm_compute/runtime/CL/CLTunerTypes.h index e3180f2165..49e2d615ea 100644 --- a/arm_compute/runtime/CL/CLTunerTypes.h +++ b/arm_compute/runtime/CL/CLTunerTypes.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Arm Limited. + * Copyright (c) 2019-2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -39,6 +39,12 @@ enum class CLTunerMode RAPID /**< Searches a minimal subset of LWS configurations while tuning */ }; +/**< OpenCL tuner tuning information */ +struct CLTuningInfo +{ + bool tune_lws = true; +}; + /** Converts a string to a strong types enumeration @ref CLTunerMode * * @param[in] name String to convert diff --git a/arm_compute/runtime/CL/CLTuningParams.h b/arm_compute/runtime/CL/CLTuningParams.h new file mode 100644 index 0000000000..99a386638d --- /dev/null +++ b/arm_compute/runtime/CL/CLTuningParams.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2020-2021 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 ARM_COMPUTE_CLTUNING_PARAMS_H +#define ARM_COMPUTE_CLTUNING_PARAMS_H + +#include "arm_compute/core/CL/OpenCL.h" + +namespace arm_compute +{ +/**< OpenCL tuner parameters */ +class CLTuningParams +{ +public: + CLTuningParams(const CLTuningParams &) = default; + + CLTuningParams(unsigned int lws_x = 0, unsigned int lws_y = 0, unsigned int lws_z = 0) + : _lws(lws_x, lws_y, lws_z) + { + } + CLTuningParams(cl::NDRange lws) + : _lws(lws) + { + } + void set_lws(cl::NDRange &lws) + { + _lws = lws; + } + + cl::NDRange get_lws() + { + return _lws; + } + +private: + cl::NDRange _lws; +}; +} // namespace arm_compute +#endif /*ARM_COMPUTE_CLTUNING_PARAMS_H */ diff --git a/arm_compute/runtime/CL/tuners/CLLWSList.h b/arm_compute/runtime/CL/tuners/CLLWSList.h deleted file mode 100644 index fe63754dd0..0000000000 --- a/arm_compute/runtime/CL/tuners/CLLWSList.h +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright (c) 2019-2020 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 ARM_COMPUTE_CL_LWS_LIST_H -#define ARM_COMPUTE_CL_LWS_LIST_H - -#include "arm_compute/core/CL/OpenCL.h" -#include "arm_compute/core/Error.h" -#include "arm_compute/core/Helpers.h" -#include "arm_compute/runtime/CL/CLTunerTypes.h" -#include "support/ToolchainSupport.h" - -#include - -namespace arm_compute -{ -namespace cl_tuner -{ -constexpr unsigned int max_lws_supported_x{ 64u }; -constexpr unsigned int max_lws_supported_y{ 32u }; -constexpr unsigned int max_lws_supported_z{ 32u }; - -/** Interface for LWS lists */ -class ICLLWSList -{ -public: - /** Constructor */ - ICLLWSList() = default; - /** Copy Constructor */ - ICLLWSList(const ICLLWSList &) = default; - /** Move Constructor */ - ICLLWSList(ICLLWSList &&) noexcept(true) = default; - /** Assignment */ - ICLLWSList &operator=(const ICLLWSList &) = default; - /** Move Assignment */ - ICLLWSList &operator=(ICLLWSList &&) noexcept(true) = default; - /** Destructor */ - virtual ~ICLLWSList() = default; - - /** Return the LWS value at the given index. - * - * @return LWS value at the given index - */ - virtual cl::NDRange operator[](size_t) = 0; - - /** LWS list size. - * - * @return LWS list size - */ - virtual size_t size() = 0; -}; - -/** Non instantiable base class for LWS combinations that use Index2Cooard mapping */ -class CLLWSList : public ICLLWSList -{ -protected: - /* Shape of 3-D search space */ - TensorShape search_space_shape{ 0, 0, 0 }; - - /** Constructor */ - CLLWSList() = default; - /** Copy Constructor */ - CLLWSList(const CLLWSList &) = default; - /** Move Constructor */ - CLLWSList(CLLWSList &&) noexcept(true) = default; - /** Assignment */ - CLLWSList &operator=(const CLLWSList &) = default; - /** Move Assignment */ - CLLWSList &operator=(CLLWSList &&) noexcept(true) = default; - /** Destructor */ - virtual ~CLLWSList() = default; - - // Inherited methods overridden: - virtual size_t size() override; -}; - -/** Exhaustive list of all possible LWS values */ -class CLLWSListExhaustive : public CLLWSList -{ -public: - /** Prevent default constructor calls */ - CLLWSListExhaustive() = delete; - /** Constructor */ - CLLWSListExhaustive(const cl::NDRange &gws); - /** Copy Constructor */ - CLLWSListExhaustive(const CLLWSListExhaustive &) = default; - /** Move Constructor */ - CLLWSListExhaustive(CLLWSListExhaustive &&) noexcept(true) = default; - /** Assignment */ - CLLWSListExhaustive &operator=(const CLLWSListExhaustive &) = default; - /** Move Assignment */ - CLLWSListExhaustive &operator=(CLLWSListExhaustive &&) noexcept(true) = default; - /** Destructor */ - ~CLLWSListExhaustive() = default; - - // Inherited methods overridden: - cl::NDRange operator[](size_t) override; -}; - -/** A subset of LWS values that are either factors of gws when gws[2] < 16 or power of 2 */ -class CLLWSListNormal : public CLLWSList -{ -public: - /** Constructor */ - CLLWSListNormal(const cl::NDRange &gws); - /** Copy Constructor */ - CLLWSListNormal(const CLLWSListNormal &) = default; - /** Move Constructor */ - CLLWSListNormal(CLLWSListNormal &&) noexcept(true) = default; - /** Assignment */ - CLLWSListNormal &operator=(const CLLWSListNormal &) = default; - /** Move Assignment */ - CLLWSListNormal &operator=(CLLWSListNormal &&) noexcept(true) = default; - /** Destructor */ - ~CLLWSListNormal() = default; - - // Inherited methods overridden: - cl::NDRange operator[](size_t) override; - -protected: - std::vector _lws_x{}; - std::vector _lws_y{}; - std::vector _lws_z{}; - - /** Prevent default constructor calls */ - CLLWSListNormal() = default; - -private: - /** Utility function used to initialize the LWS values to test. - * Only the LWS values which are power of 2 or satisfy the modulo conditions with GWS are taken into account by the CLTuner - * - * @param[in, out] lws Vector of LWS to test - * @param[in] gws Size of the specific GWS - * @param[in] lws_max Max LWS value allowed to be tested - * @param[in] mod_let_one True if the results of the modulo operation between gws and the lws can be less than one. - */ - void initialize_lws_values(std::vector &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one); -}; - -/** A minimal subset of LWS values that only have 1,2 and 4/8 */ -class CLLWSListRapid : public CLLWSListNormal -{ -public: - /** Prevent default constructor calls */ - CLLWSListRapid() = delete; - /** Constructor */ - CLLWSListRapid(const cl::NDRange &gws); - /** Copy Constructor */ - CLLWSListRapid(const CLLWSListRapid &) = default; - /** Move Constructor */ - CLLWSListRapid(CLLWSListRapid &&) noexcept(true) = default; - /** Assignment */ - CLLWSListRapid &operator=(const CLLWSListRapid &) = default; - /** Move Assignment */ - CLLWSListRapid &operator=(CLLWSListRapid &&) noexcept(true) = default; - /** Destructor */ - virtual ~CLLWSListRapid() = default; - -private: - /** Utility function used to initialize the LWS values to test. - * Only the LWS values that have 1,2 and 4/8 for each dimension are taken into account by the CLTuner - * - * @param[in, out] lws Vector of LWS to test - * @param[in] lws_max Max LWS value allowed to be tested - */ - void initialize_lws_values(std::vector &lws, unsigned int lws_max); -}; - -/** Factory to construct an ICLLWSList object based on the CL tuner mode */ -class CLLWSListFactory final -{ -public: - /** Construct an ICLLWSList object for the given tuner mode and gws configuration. - * - * @return unique_ptr to the requested ICLLWSList implementation. - */ - static std::unique_ptr get_lws_list(CLTunerMode mode, const cl::NDRange &gws) - { - switch(mode) - { - case CLTunerMode::EXHAUSTIVE: - return std::make_unique(gws); - case CLTunerMode::NORMAL: - return std::make_unique(gws); - case CLTunerMode::RAPID: - return std::make_unique(gws); - default: - return nullptr; - } - } -}; -} // namespace cl_tuner -} // namespace arm_compute -#endif /*ARM_COMPUTE_CL_LWS_LIST_H */ diff --git a/arm_compute/runtime/CL/tuners/CLTuningParametersList.h b/arm_compute/runtime/CL/tuners/CLTuningParametersList.h new file mode 100644 index 0000000000..c51b9901ef --- /dev/null +++ b/arm_compute/runtime/CL/tuners/CLTuningParametersList.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2020-2021 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 ARM_COMPUTE_CL_TUNINGPARAMETERS_LIST_H +#define ARM_COMPUTE_CL_TUNINGPARAMETERS_LIST_H + +#include "arm_compute/core/CL/OpenCL.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/Helpers.h" +#include "arm_compute/runtime/CL/CLTunerTypes.h" +#include "arm_compute/runtime/CL/CLTuningParams.h" +#include "support/ToolchainSupport.h" + +#include + +namespace arm_compute +{ +namespace cl_tuner +{ +/** Interface for Tuning Parameters lists + * + * The tuning parameter lists contain a set of tuning parameters to estimate. + * There are 3 tuner modes, each using its specific list: + * - Exhaustive tuner mode is the slowest during the tuning but will find faster tuning parameters + * - Normal tuner mode is the average modality in terms of tuning time and tuning parameters found + * - Rapid tuner mode is the fastest but the tuning parameters might not be the fastest + * + */ +class ICLTuningParametersList +{ +public: + /** Constructor */ + ICLTuningParametersList() = default; + /** Copy Constructor */ + ICLTuningParametersList(const ICLTuningParametersList &) = default; + /** Move Constructor */ + ICLTuningParametersList(ICLTuningParametersList &&) noexcept(true) = default; + /** Assignment */ + ICLTuningParametersList &operator=(const ICLTuningParametersList &) = default; + /** Move Assignment */ + ICLTuningParametersList &operator=(ICLTuningParametersList &&) noexcept(true) = default; + /** Destructor */ + virtual ~ICLTuningParametersList() = default; + + /** Return the tuning parameter values at the given index. + * + * @return tuning parameter values at the given index + */ + virtual CLTuningParams operator[](size_t) = 0; + + /** Tuning parameters list size. + * + * @return Tuning parameters list size + */ + virtual size_t size() = 0; +}; + +/** Construct an ICLTuningParametersList object for the given tuner mode and gws configuration. + * + * @return unique_ptr to the requested ICLTuningParametersList implementation. + */ +std::unique_ptr get_tuning_parameters_list(CLTunerMode mode, const cl::NDRange &gws); + +} // namespace cl_tuner +} // namespace arm_compute +#endif /*ARM_COMPUTE_CL_TUNINGPARAMETERS_LIST_H */ diff --git a/src/core/NEON/kernels/NESelectKernel.cpp b/src/core/NEON/kernels/NESelectKernel.cpp index 9cf9b98a0c..1d5f2b61a1 100644 --- a/src/core/NEON/kernels/NESelectKernel.cpp +++ b/src/core/NEON/kernels/NESelectKernel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 Arm Limited. + * Copyright (c) 2018-2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -34,7 +34,6 @@ #include "src/core/NEON/wrapper/wrapper.h" #include "src/core/helpers/AutoConfiguration.h" #include "src/core/helpers/WindowHelpers.h" -#include "utils/TypePrinter.h" #include #include diff --git a/src/graph/backends/CL/CLDeviceBackend.cpp b/src/graph/backends/CL/CLDeviceBackend.cpp index bc7bbddbd8..50dd799ee1 100644 --- a/src/graph/backends/CL/CLDeviceBackend.cpp +++ b/src/graph/backends/CL/CLDeviceBackend.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 Arm Limited. + * Copyright (c) 2018-2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -71,11 +71,7 @@ CLDeviceBackend::CLDeviceBackend() CLDeviceBackend::~CLDeviceBackend() { - // TODO (geopin01) : Shouldn't call non exception safe stuff here - if(_tuner.tune_new_kernels() && !_tuner.lws_table().empty() && !_tuner_file.empty()) - { - _tuner.save_to_file(_tuner_file); - } + _tuner.save_to_file(_tuner_file); } void CLDeviceBackend::set_kernel_tuning(bool enable_tuning) @@ -117,6 +113,7 @@ void CLDeviceBackend::setup_backend_context(GraphContext &ctx) // Setup tuner _tuner_file = ctx.config().tuner_file; + // Load tuner data if available if(file_exists(_tuner_file)) { diff --git a/src/runtime/CL/CLTuner.cpp b/src/runtime/CL/CLTuner.cpp index ed85e606cf..bcc50f6c28 100644 --- a/src/runtime/CL/CLTuner.cpp +++ b/src/runtime/CL/CLTuner.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020 Arm Limited. + * Copyright (c) 2017-2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -22,7 +22,7 @@ * SOFTWARE. */ #include "arm_compute/runtime/CL/CLTuner.h" -#include "arm_compute/runtime/CL/tuners/CLLWSList.h" +#include "arm_compute/runtime/CL/tuners/CLTuningParametersList.h" #include "arm_compute/core/Error.h" #include "arm_compute/runtime/CL/CLScheduler.h" @@ -38,8 +38,8 @@ namespace arm_compute { -CLTuner::CLTuner(bool tune_new_kernels) - : real_clEnqueueNDRangeKernel(nullptr), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels), _tuner_mode(CLTunerMode::NORMAL) +CLTuner::CLTuner(bool tune_new_kernels, CLTuningInfo tuning_info) + : real_clEnqueueNDRangeKernel(nullptr), _tuning_params_table(), _lws_table(), _kernel_event(), _tune_new_kernels(tune_new_kernels), _tuning_info(tuning_info), _tuner_mode(CLTunerMode::NORMAL) { } @@ -65,6 +65,7 @@ void CLTuner::set_tuner_mode(CLTunerMode mode) { _tuner_mode = mode; } + CLTunerMode CLTuner::get_tuner_mode() const { return _tuner_mode; @@ -89,36 +90,41 @@ void CLTuner::tune_kernel_dynamic(ICLKernel &kernel, ITensorPack &tensors) // Check if we need to find the Optimal LWS. If the kernel's config_id is equal to default_config_id, the kernel does not require to be tuned if(kernel.config_id() != arm_compute::default_config_id) { - auto p = _lws_table.find(config_id); + auto p = _tuning_params_table.find(config_id); - if(p == _lws_table.end()) + if(p == _tuning_params_table.end()) { if(_tune_new_kernels) { // Find the optimal LWS for the kernel - cl::NDRange opt_lws = find_optimal_lws(kernel, tensors); + CLTuningParams opt_tuning_params = find_optimal_tuning_params(kernel, tensors); // Insert the optimal LWS in the table - add_lws_to_table(config_id, opt_lws); + add_tuning_params(config_id, opt_tuning_params); // Set Local-Workgroup-Size - kernel.set_lws_hint(opt_lws); + kernel.set_lws_hint(opt_tuning_params.get_lws()); } } else { // Set Local-Workgroup-Size - kernel.set_lws_hint(p->second); + kernel.set_lws_hint(p->second.get_lws()); } } } void CLTuner::add_lws_to_table(const std::string &kernel_id, cl::NDRange optimal_lws) { - _lws_table.emplace(kernel_id, optimal_lws); + add_tuning_params(kernel_id, CLTuningParams(optimal_lws)); } -cl::NDRange CLTuner::find_optimal_lws(ICLKernel &kernel, ITensorPack &tensors) +void CLTuner::add_tuning_params(const std::string &kernel_id, CLTuningParams optimal_tuning_params) +{ + _tuning_params_table.emplace(kernel_id, optimal_tuning_params); +} + +CLTuningParams CLTuner::find_optimal_tuning_params(ICLKernel &kernel, ITensorPack &tensors) { // Profiling queue cl::CommandQueue queue_profiler; @@ -185,11 +191,11 @@ cl::NDRange CLTuner::find_optimal_lws(ICLKernel &kernel, ITensorPack &tensors) cl::NDRange opt_lws = cl::NullRange; - // Construct the list of LWS values to be tested based on the tuner mode. - auto lws_list = cl_tuner::CLLWSListFactory::get_lws_list(_tuner_mode, gws); + // Construct the list of tuning parameters values to be tested based on the tuner mode. + auto lws_list = cl_tuner::get_tuning_parameters_list(_tuner_mode, gws); for(size_t i = 0; i < lws_list->size(); ++i) { - cl::NDRange lws_test = (*lws_list)[i]; + cl::NDRange lws_test = (*lws_list)[i].get_lws(); auto x = lws_test[0]; auto y = lws_test[1]; auto z = lws_test[2]; @@ -223,21 +229,39 @@ cl::NDRange CLTuner::find_optimal_lws(ICLKernel &kernel, ITensorPack &tensors) // Restore real function CLSymbols::get().clEnqueueNDRangeKernel_ptr = real_clEnqueueNDRangeKernel; - - return opt_lws; + return CLTuningParams(opt_lws); } void CLTuner::import_lws_table(const std::unordered_map &lws_table) { - _lws_table.clear(); - _lws_table = lws_table; + _tuning_params_table.clear(); + for(auto && params : lws_table) + { + add_tuning_params(params.first, CLTuningParams(params.second)); + } } -const std::unordered_map &CLTuner::lws_table() const +const std::unordered_map &CLTuner::lws_table() { + _lws_table.clear(); + for(auto && params : _tuning_params_table) + { + _lws_table.emplace(params.first, params.second.get_lws()); + } return _lws_table; } +const std::unordered_map &CLTuner::tuning_params_table() const +{ + return _tuning_params_table; +} + +void CLTuner::import_tuning_params(const std::unordered_map &tuning_params_table) +{ + _tuning_params_table.clear(); + _tuning_params_table = tuning_params_table; +} + void CLTuner::load_from_file(const std::string &filename) { std::ifstream fs; @@ -272,20 +296,28 @@ void CLTuner::load_from_file(const std::string &filename) { lws = cl::NullRange; } - add_lws_to_table(kernel_id, lws); + add_tuning_params(kernel_id, lws); } fs.close(); + _tuning_info.tune_lws = true; } -void CLTuner::save_to_file(const std::string &filename) const +bool CLTuner::save_to_file(const std::string &filename) const { + if(!_tune_new_kernels || _tuning_params_table.empty() || filename.empty()) + { + return false; + } + std::ofstream fs; fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); fs.open(filename, std::ios::out); - for(auto const &kernel_data : _lws_table) + for(auto const &kernel_data : _tuning_params_table) { - fs << kernel_data.first << ";" << kernel_data.second[0] << ";" << kernel_data.second[1] << ";" << kernel_data.second[2] << std::endl; + const cl::NDRange lws = CLTuningParams(kernel_data.second).get_lws(); + fs << kernel_data.first << ";" << lws[0] << ";" << lws[1] << ";" << lws[2] << std::endl; } fs.close(); + return true; } } // namespace arm_compute diff --git a/src/runtime/CL/tuners/CLLWSList.cpp b/src/runtime/CL/tuners/CLLWSList.cpp deleted file mode 100644 index c537f15bf0..0000000000 --- a/src/runtime/CL/tuners/CLLWSList.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2019 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/runtime/CL/tuners/CLLWSList.h" - -namespace arm_compute -{ -namespace cl_tuner -{ -size_t CLLWSList::size() -{ - return search_space_shape.total_size(); -} - -cl::NDRange CLLWSListExhaustive::operator[](size_t index) -{ - ARM_COMPUTE_ERROR_ON(index >= size()); - auto coords = index2coords(search_space_shape, index); - return cl::NDRange{ coords[0] + 1U, coords[1] + 1U, coords[2] + 1U }; -} - -CLLWSListExhaustive::CLLWSListExhaustive(const cl::NDRange &gws) -{ - ARM_COMPUTE_UNUSED(gws); - search_space_shape = TensorShape(max_lws_supported_x, - max_lws_supported_y, - max_lws_supported_z); -} - -cl::NDRange CLLWSListNormal::operator[](size_t index) -{ - ARM_COMPUTE_ERROR_ON(index >= size()); - auto coords = index2coords(search_space_shape, index); - return cl::NDRange{ _lws_x[coords[0]], _lws_y[coords[1]], _lws_z[coords[2]] }; -} - -CLLWSListNormal::CLLWSListNormal(const cl::NDRange &gws) -{ - auto lws_x_max = std::min(static_cast(gws[0]), max_lws_supported_x); - auto lws_y_max = std::min(static_cast(gws[1]), max_lws_supported_y); - auto lws_z_max = std::min(static_cast(gws[2]), max_lws_supported_z); - - // Initialize the LWS values to test - initialize_lws_values(_lws_x, gws[0], lws_x_max, gws[2] > 16); // Explore lws that are not factors of gws only when gws[2] > 16 - initialize_lws_values(_lws_y, gws[1], lws_y_max, gws[2] > 16); // Explore lws that are not factors of gws only when gws[2] > 16 - initialize_lws_values(_lws_z, gws[2], lws_z_max, false); - - search_space_shape = TensorShape(_lws_x.size(), _lws_y.size(), _lws_z.size()); -} - -void CLLWSListNormal::initialize_lws_values(std::vector &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one) -{ - lws.push_back(1); - - for(unsigned int i = 2; i <= lws_max; ++i) - { - // Power of two condition - const bool is_power_of_two = (i & (i - 1)) == 0; - - // Condition for the module accordingly with the mod_let_one flag - const bool mod_cond = mod_let_one ? (gws % i) <= 1 : (gws % i) == 0; - - if(mod_cond || is_power_of_two) - { - lws.push_back(i); - } - } -} - -CLLWSListRapid::CLLWSListRapid(const cl::NDRange &gws) -{ - auto lws_x_max = std::min(static_cast(gws[0]), 8u); // Limit exploration to 1 - 8 - auto lws_y_max = std::min(static_cast(gws[1]), 4u); // Limit exploration to 1 - 4 - auto lws_z_max = std::min(static_cast(gws[2]), 4u); // Limit exploration to 1 - 4 - - // Initialize the LWS values to test - initialize_lws_values(_lws_x, lws_x_max); - initialize_lws_values(_lws_y, lws_y_max); - initialize_lws_values(_lws_z, lws_z_max); - - search_space_shape = TensorShape(_lws_x.size(), _lws_y.size(), _lws_z.size()); -} - -void CLLWSListRapid::initialize_lws_values(std::vector &lws, unsigned int lws_max) -{ - lws.push_back(1); - - for(unsigned int i = 2; i <= lws_max; i *= 4) - { - lws.push_back(i); - } -} -} // namespace cl_tuner -} // namespace arm_compute diff --git a/src/runtime/CL/tuners/CLTuningParametersList.cpp b/src/runtime/CL/tuners/CLTuningParametersList.cpp new file mode 100644 index 0000000000..7f63078192 --- /dev/null +++ b/src/runtime/CL/tuners/CLTuningParametersList.cpp @@ -0,0 +1,249 @@ +/* + * Copyright (c) 2019-2021 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/runtime/CL/tuners/CLTuningParametersList.h" + +namespace arm_compute +{ +namespace cl_tuner +{ +constexpr unsigned int max_lws_supported_x{ 64u }; +constexpr unsigned int max_lws_supported_y{ 32u }; +constexpr unsigned int max_lws_supported_z{ 32u }; + +/** Non instantiable base class for Tuning parameters combinations that use Index2Cooard mapping */ +class CLTuningParametersList : public ICLTuningParametersList +{ +protected: + /* Shape of 3-D search space */ + TensorShape search_space_shape{ 0, 0, 0 }; + + /** Constructor */ + CLTuningParametersList() = default; + /** Copy Constructor */ + CLTuningParametersList(const CLTuningParametersList &) = default; + /** Move Constructor */ + CLTuningParametersList(CLTuningParametersList &&) noexcept(true) = default; + /** Assignment */ + CLTuningParametersList &operator=(const CLTuningParametersList &) = default; + /** Move Assignment */ + CLTuningParametersList &operator=(CLTuningParametersList &&) noexcept(true) = default; + /** Destructor */ + virtual ~CLTuningParametersList() = default; + + // Inherited methods overridden: + virtual size_t size() override; +}; + +/** Exhaustive list of all possible Tuning parameters (lws) values */ +class CLTuningParametersListExhaustive : public CLTuningParametersList +{ +public: + /** Prevent default constructor calls */ + CLTuningParametersListExhaustive() = delete; + /** Constructor */ + CLTuningParametersListExhaustive(const cl::NDRange &gws); + /** Copy Constructor */ + CLTuningParametersListExhaustive(const CLTuningParametersListExhaustive &) = default; + /** Move Constructor */ + CLTuningParametersListExhaustive(CLTuningParametersListExhaustive &&) noexcept(true) = default; + /** Assignment */ + CLTuningParametersListExhaustive &operator=(const CLTuningParametersListExhaustive &) = default; + /** Move Assignment */ + CLTuningParametersListExhaustive &operator=(CLTuningParametersListExhaustive &&) noexcept(true) = default; + /** Destructor */ + ~CLTuningParametersListExhaustive() = default; + + // Inherited methods overridden: + CLTuningParams operator[](size_t) override; +}; + +/** A subset of LWS values that are either factors of gws when gws[2] < 16 or power of 2 */ +class CLTuningParametersListNormal : public CLTuningParametersList +{ +public: + /** Constructor */ + CLTuningParametersListNormal(const cl::NDRange &gws); + /** Copy Constructor */ + CLTuningParametersListNormal(const CLTuningParametersListNormal &) = default; + /** Move Constructor */ + CLTuningParametersListNormal(CLTuningParametersListNormal &&) noexcept(true) = default; + /** Assignment */ + CLTuningParametersListNormal &operator=(const CLTuningParametersListNormal &) = default; + /** Move Assignment */ + CLTuningParametersListNormal &operator=(CLTuningParametersListNormal &&) noexcept(true) = default; + /** Destructor */ + ~CLTuningParametersListNormal() = default; + + // Inherited methods overridden: + CLTuningParams operator[](size_t) override; + +protected: + std::vector _lws_x{}; + std::vector _lws_y{}; + std::vector _lws_z{}; + + /** Prevent default constructor calls */ + CLTuningParametersListNormal() = default; + +private: + /** Utility function used to initialize the LWS values to test. + * Only the LWS values which are power of 2 or satisfy the modulo conditions with GWS are taken into account by the CLTuner + * + * @param[in, out] lws Vector of LWS to test + * @param[in] gws Size of the specific GWS + * @param[in] lws_max Max LWS value allowed to be tested + * @param[in] mod_let_one True if the results of the modulo operation between gws and the lws can be less than one. + */ + void initialize_lws_values(std::vector &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one); +}; + +/** A minimal subset of LWS values that only have 1,2 and 4/8 */ +class CLTuningParametersListRapid : public CLTuningParametersListNormal +{ +public: + /** Prevent default constructor calls */ + CLTuningParametersListRapid() = delete; + /** Constructor */ + CLTuningParametersListRapid(const cl::NDRange &gws); + /** Copy Constructor */ + CLTuningParametersListRapid(const CLTuningParametersListRapid &) = default; + /** Move Constructor */ + CLTuningParametersListRapid(CLTuningParametersListRapid &&) noexcept(true) = default; + /** Assignment */ + CLTuningParametersListRapid &operator=(const CLTuningParametersListRapid &) = default; + /** Move Assignment */ + CLTuningParametersListRapid &operator=(CLTuningParametersListRapid &&) noexcept(true) = default; + /** Destructor */ + virtual ~CLTuningParametersListRapid() = default; + +private: + /** Utility function used to initialize the LWS values to test. + * Only the LWS values that have 1,2 and 4/8 for each dimension are taken into account by the CLTuner + * + * @param[in, out] lws Vector of LWS to test + * @param[in] lws_max Max LWS value allowed to be tested + */ + void initialize_lws_values(std::vector &lws, unsigned int lws_max); +}; + +size_t CLTuningParametersList::size() +{ + return search_space_shape.total_size(); +} + +CLTuningParams CLTuningParametersListExhaustive::operator[](size_t index) +{ + ARM_COMPUTE_ERROR_ON(index >= size()); + auto coords = index2coords(search_space_shape, index); + return CLTuningParams(coords[0] + 1U, coords[1] + 1U, coords[2] + 1U); +} + +CLTuningParametersListExhaustive::CLTuningParametersListExhaustive(const cl::NDRange &gws) +{ + ARM_COMPUTE_UNUSED(gws); + search_space_shape = TensorShape(max_lws_supported_x, + max_lws_supported_y, + max_lws_supported_z); +} + +CLTuningParams CLTuningParametersListNormal::operator[](size_t index) +{ + ARM_COMPUTE_ERROR_ON(index >= size()); + auto coords = index2coords(search_space_shape, index); + return CLTuningParams(_lws_x[coords[0]], _lws_y[coords[1]], _lws_z[coords[2]]); +} + +CLTuningParametersListNormal::CLTuningParametersListNormal(const cl::NDRange &gws) +{ + auto lws_x_max = std::min(static_cast(gws[0]), max_lws_supported_x); + auto lws_y_max = std::min(static_cast(gws[1]), max_lws_supported_y); + auto lws_z_max = std::min(static_cast(gws[2]), max_lws_supported_z); + + // Initialize the LWS values to test + initialize_lws_values(_lws_x, gws[0], lws_x_max, gws[2] > 16); // Explore lws that are not factors of gws only when gws[2] > 16 + initialize_lws_values(_lws_y, gws[1], lws_y_max, gws[2] > 16); // Explore lws that are not factors of gws only when gws[2] > 16 + initialize_lws_values(_lws_z, gws[2], lws_z_max, false); + + search_space_shape = TensorShape(_lws_x.size(), _lws_y.size(), _lws_z.size()); +} + +void CLTuningParametersListNormal::initialize_lws_values(std::vector &lws, unsigned int gws, unsigned int lws_max, bool mod_let_one) +{ + lws.push_back(1); + + for(unsigned int i = 2; i <= lws_max; ++i) + { + // Power of two condition + const bool is_power_of_two = (i & (i - 1)) == 0; + + // Condition for the module accordingly with the mod_let_one flag + const bool mod_cond = mod_let_one ? (gws % i) <= 1 : (gws % i) == 0; + + if(mod_cond || is_power_of_two) + { + lws.push_back(i); + } + } +} + +CLTuningParametersListRapid::CLTuningParametersListRapid(const cl::NDRange &gws) +{ + auto lws_x_max = std::min(static_cast(gws[0]), 8u); // Limit exploration to 1 - 8 + auto lws_y_max = std::min(static_cast(gws[1]), 4u); // Limit exploration to 1 - 4 + auto lws_z_max = std::min(static_cast(gws[2]), 4u); // Limit exploration to 1 - 4 + + // Initialize the LWS values to test + initialize_lws_values(_lws_x, lws_x_max); + initialize_lws_values(_lws_y, lws_y_max); + initialize_lws_values(_lws_z, lws_z_max); + + search_space_shape = TensorShape(_lws_x.size(), _lws_y.size(), _lws_z.size()); +} + +void CLTuningParametersListRapid::initialize_lws_values(std::vector &lws, unsigned int lws_max) +{ + lws.push_back(1); + + for(unsigned int i = 2; i <= lws_max; i *= 4) + { + lws.push_back(i); + } +} + +std::unique_ptr get_tuning_parameters_list(CLTunerMode mode, const cl::NDRange &gws) +{ + switch(mode) + { + case CLTunerMode::EXHAUSTIVE: + return std::make_unique(gws); + case CLTunerMode::NORMAL: + return std::make_unique(gws); + case CLTunerMode::RAPID: + return std::make_unique(gws); + default: + return nullptr; + } +} +} // namespace cl_tuner +} // namespace arm_compute -- cgit v1.2.1