From f67903b8ab8205b47f0ee2c27aeca8bed405c58e Mon Sep 17 00:00:00 2001 From: Mohammed Suhail Munshi Date: Mon, 4 Jul 2022 13:36:14 +0100 Subject: Add Dynamic Fusion Tests with BugFixes - Allow fusing arbitrary number of existing elementwise operators - Fix issues with 3D and 4D tensors in Elementwise Addition and Floor components - Collapse the 3D/4D window in the same way as that used by Conv2d, i.e. collapse dim 1 and dim 2 together - Fix Floor component issues when used after other components - Add Dynamic Fusion Tests (Floor + Div, Conv2d + Add + Div) - Add Addition ElementWise Broadcasting Test Resolves: [COMPMID-5356] Change-Id: I58b93a90175bb0440d43531d18cac94b5f5c2689 Signed-off-by: Mohammed Suhail Munshi Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/c/VisualCompute/ComputeLibrary/+/433956 Tested-by: bsgcomp Reviewed-by: Pablo Tello Comments-Addressed: bsgcomp Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/7957 Reviewed-by: SiCong Li Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins Benchmark: Arm Jenkins --- .../dynamic_fusion/ClKernelBuildingImpl/Common.h | 8 +-- .../components/ClElementwiseKernelComponent.cpp | 61 +++++++++++++++++++--- .../components/ClElementwiseKernelComponent.h | 14 +++++ .../components/ClFloorKernelComponent.cpp | 61 +++++++++++++++------- .../components/ClFloorKernelComponent.h | 13 ++++- .../components/ClStoreKernelComponents.cpp | 4 ++ 6 files changed, 130 insertions(+), 31 deletions(-) (limited to 'src/core/experimental') diff --git a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/Common.h b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/Common.h index 57ac70aa22..04919acb83 100644 --- a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/Common.h +++ b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/Common.h @@ -371,6 +371,7 @@ public: { return Window{}; } + /** Get the tag look-up table used to instantiate the component code. * * @param vtable @@ -557,7 +558,7 @@ public: std::string build_code() { - ARM_COMPUTE_ERROR_ON_MSG(_graph_root < 0, "No root found in the component graph"); + ARM_COMPUTE_ERROR_ON_MSG(_graph_root == -1, "No root found in the component graph"); // These data structures will hold the data from all the components in the blueprint std::set headers_list{}; @@ -666,9 +667,10 @@ public: return _tile_info; } + // Get the global execution window, i.e. that of the root component Window get_execution_window() const { - ARM_COMPUTE_ERROR_ON_MSG(_graph_root < 0, "No root found in the component graph"); + ARM_COMPUTE_ERROR_ON_MSG(_graph_root == -1, "No root found in the component graph"); ARM_COMPUTE_ERROR_ON_MSG(_dst_id == -1, "Destination Tensor Id should be ready before calling get_execution_window()"); return _components.find(_graph_root)->second->get_window(); @@ -925,4 +927,4 @@ private: } // namespace experimental } // namespace arm_compute #endif //ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_IMPL_COMMON_H -#endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */ \ No newline at end of file +#endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */ diff --git a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseKernelComponent.cpp b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseKernelComponent.cpp index 24a9eee9a3..7515aec27a 100644 --- a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseKernelComponent.cpp +++ b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseKernelComponent.cpp @@ -24,6 +24,7 @@ #ifdef ENABLE_EXPERIMENTAL_DYNAMIC_FUSION #include "src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseKernelComponent.h" +#include "arm_compute/core/Error.h" #include "arm_compute/core/Validate.h" #include "src/core/helpers/AutoConfiguration.h" #include "src/core/helpers/WindowHelpers.h" @@ -57,9 +58,13 @@ Window ClElementwiseKernelComponent::get_window() const auto_init_if_empty(*dst_info, out_shape, 1, lhs_info->data_type()); + TensorShape output_shape = dst_info->tensor_shape(); + // Collapse Dim 1 (W) and Dim 2 (H) together, leave Dim 0 (C) and upper dimensions unchanged + // This is in line with the collapsing convention used by Conv2d + output_shape.collapse(2U, 1U); const unsigned int vector_size_byte_opencl = 16; const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst_info->element_size(), dst_info->dimension(0)); - Window win = calculate_max_window(*dst_info, Steps(num_elems_processed_per_iteration)); + Window win = calculate_max_window(output_shape, Steps(num_elems_processed_per_iteration)); return win; } @@ -83,8 +88,12 @@ std::string ClElementwiseKernelComponent::get_component_code() const TILE({{DATA_TYPE}}, M0, N0, lhs_tile); TILE({{DATA_TYPE}}, M0, N0, rhs_tile); + // Since mout maps to dimensions 1 (y) and dimension 2 (z) of the input tensor because of the collapsed window, bout maps to dimension 3 (w) + {{lhs}}_offset_first_element_in_bytes += bout * {{lhs}}_stride_w; + {{rhs}}_offset_first_element_in_bytes += bout * {{rhs}}_stride_w; + T_LOAD({{DATA_TYPE}}, M0, N0, BUFFER, {{lhs}}, cout, mout, 1, {{lhs}}_stride_y, lhs_tile); - T_LOAD({{DATA_TYPE}}, M0, N0, BUFFER, {{rhs}}, cout, mout, 1, {{rhs}}_stride_y, rhs_tile); + T_LOAD({{DATA_TYPE}}, {{rhs_m0}}, {{rhs_n0}}, BUFFER, {{rhs}}, {{rhs_start_x}}, {{rhs_start_y}}, 1, {{rhs}}_stride_y, rhs_tile); #if defined(IS_BROADCAST) T_ELTWISE_BROADCAST_{{ELTWISE_OP}}_X({{DATA_TYPE}}, M0, N0, lhs_tile, rhs_tile, {{dst}}); @@ -107,7 +116,7 @@ std::string ClElementwiseKernelComponent::get_component_code() const { TILE({{DATA_TYPE}}, M0, N0, addend_tile); - T_LOAD({{DATA_TYPE}}, M0, N0, BUFFER, {{addend}}, cout, mout, 1, {{addend}}_stride_y, addend_tile); + T_LOAD({{DATA_TYPE}}, {{rhs_m0}}, {{rhs_n0}}, BUFFER, {{addend}}, {{rhs_start_x}}, {{rhs_start_y}}, 1, {{addend}}_stride_y, addend_tile); #if defined(IS_BROADCAST) T_ELTWISE_BROADCAST_{{ELTWISE_OP}}_X({{DATA_TYPE}}, M0, N0, {{acc}}, addend_tile, {{acc}}); @@ -122,16 +131,18 @@ std::string ClElementwiseKernelComponent::get_component_code() const CLBuildOptions ClElementwiseKernelComponent::generate_build_options() const { - const auto t_src_info = _blueprint->impl().get_kernel_argument_info(_rhs.arg_id); + const auto t_rhs_info = _blueprint->impl().get_kernel_argument_info(_rhs.arg_id); const auto t_dst_info = _blueprint->impl().get_kernel_argument_info(_blueprint->impl().get_dst_id()); - CLBuildOptions build_opts{}; - const auto n0 = _blueprint->impl().get_execution_window().x().step(); - const auto m0 = _blueprint->impl().get_execution_window().y().step(); - const bool is_broadcast = t_src_info->tensor_shape() != t_dst_info->tensor_shape(); + CLBuildOptions build_opts{}; + const auto n0 = _blueprint->impl().get_execution_window().x().step(); + const auto m0 = _blueprint->impl().get_execution_window().y().step(); + const unsigned int partial_store_n0 = t_dst_info->dimension(0) % n0; + const bool is_broadcast = t_rhs_info->tensor_shape() != t_dst_info->tensor_shape(); build_opts.add_option("-DM0=" + support::cpp11::to_string(m0)); build_opts.add_option("-DN0=" + support::cpp11::to_string(n0)); + build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0)); build_opts.add_option_if(is_broadcast, "-DIS_BROADCAST"); return build_opts; @@ -166,6 +177,7 @@ ClElementwiseKernelComponent::TagLUT ClElementwiseKernelComponent::get_tag_lut(c { TagLUT lut{}; const auto t_dst_info = _blueprint->impl().get_kernel_argument_info(_blueprint->impl().get_dst_id()); + const auto t_rhs_info = _blueprint->impl().get_kernel_argument_info(_rhs.arg_id); // Arguments and global shared variables const bool is_root = _blueprint->impl().group(_lhs.arg_id) == SharedVarGroup::Argument && _blueprint->impl().group(_rhs.arg_id) == SharedVarGroup::Argument; if(is_root) @@ -211,6 +223,39 @@ ClElementwiseKernelComponent::TagLUT ClElementwiseKernelComponent::get_tag_lut(c default: ARM_COMPUTE_ERROR("Arithmetic Operation not supported"); } + + // Set broadcast parameters + // PRE: All tensors are broadcast-compatible + const bool is_broadcast = t_rhs_info->tensor_shape() != t_dst_info->tensor_shape(); + if(is_broadcast) + { + // Note that n0 maps to input tensor dimension 0, m0 maps to input dimensions 1 and 2 because of our collapse strategy + if(t_rhs_info->dimension(0) == 1U && t_rhs_info->dimension(1) == 1U && t_rhs_info->dimension(2) == 1U) // Broadcast in X, Y, Z: collapsed rhs win [M0xN0] = [1x1] + { + lut["rhs_m0"] = "1"; + lut["rhs_n0"] = "1"; + lut["rhs_start_y"] = "0"; + lut["rhs_start_x"] = "0"; + } + else if(t_rhs_info->dimension(1) == 1U && t_rhs_info->dimension(2) == 1U) // Broadcast in Y and Z: collapsed rhs win [M0xN0] = [1xN] + { + lut["rhs_m0"] = "1"; + lut["rhs_n0"] = "N0"; + lut["rhs_start_y"] = "0"; + lut["rhs_start_x"] = "cout"; + } + else + { + ARM_COMPUTE_ERROR("Only support rhs broadcasting in all X, Y, Z dimensions, or just in Y and Z dimensions"); + } + } + else + { + lut["rhs_m0"] = "M0"; + lut["rhs_n0"] = "N0"; + lut["rhs_start_y"] = "mout"; + lut["rhs_start_x"] = "cout"; + } return lut; } } // namespace dynamic_fusion diff --git a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseKernelComponent.h b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseKernelComponent.h index 91b14ffafa..f8377457d3 100644 --- a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseKernelComponent.h +++ b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClElementwiseKernelComponent.h @@ -37,6 +37,20 @@ namespace dynamic_fusion class ClElementwiseKernelComponent : public IClKernelComponent { public: + /** Construct a new Cl Elementwise Kernel Component object + * + * @param[in] blueprint Blueprint to which this component is added + * @param[in] desc Component descriptor + * @param[in] lhs Link to LHS tensor + * @param[in] rhs Link to RHS tensor + * @param[out] dst Link to DST tensor + * + * Support Level + * Data Type: F16, F32 + * Tensor Shape: Any shape of arbitrary dimension >= 1 and <= 4 + * Value Range: All + * Broadcasting: Only RHS tensor can be broadcasted into LHS. Only support broadcasting in dimension 1 and dimension 2 or all dimension 0, 1 and 2 + */ ClElementwiseKernelComponent(ClKernelBlueprint *blueprint, const ClElementwiseKernelDescriptor &desc, const Link &lhs, const Link &rhs, const Link &dst) : IClKernelComponent(blueprint), _desc{ desc }, _lhs{ lhs }, _rhs{ rhs }, _dst{ dst } { diff --git a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClFloorKernelComponent.cpp b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClFloorKernelComponent.cpp index 87cc110561..0a20a8f600 100644 --- a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClFloorKernelComponent.cpp +++ b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClFloorKernelComponent.cpp @@ -21,9 +21,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifdef ENABLE_EXPERIMENTAL_DYNAMIC_FUSION +#ifdef ENABLE_EXPERIMENTAL_DYNAMIC_FUSION #include "src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClFloorKernelComponent.h" +#include "arm_compute/core/Error.h" #include "arm_compute/core/Validate.h" #include "src/core/helpers/AutoConfiguration.h" #include "src/core/helpers/WindowHelpers.h" @@ -38,12 +39,10 @@ ComponentType ClFloorKernelComponent::get_component_type() const { return ComponentType::Simple; } - std::set ClFloorKernelComponent::get_headers_list() const { return std::set { "common/experimental/gemm_fused_post_ops/fp_mixed_precision_helpers.h", "tile_helpers.h" }; } - Window ClFloorKernelComponent::get_window() const { const ITensorInfo *src_info = _blueprint->impl().get_kernel_argument_info(_src.arg_id); @@ -52,16 +51,22 @@ Window ClFloorKernelComponent::get_window() const ARM_COMPUTE_ERROR_ON_NULLPTR(src_info, dst_info); auto_init_if_empty(*dst_info, src_info->tensor_shape(), 1, src_info->data_type()); + TensorShape output_shape = dst_info->tensor_shape(); + // Collapse Dim 1 (W) and Dim 2 (H) together, leave Dim 0 (C) and upper dimensions unchanged + // This is in line with the collapsing convention used by Conv2d + output_shape.collapse(2U, 1U); const unsigned int vector_size_byte_opencl = 16; const unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / dst_info->element_size(), dst_info->dimension(0)); - Window win = calculate_max_window(*dst_info, Steps(num_elems_processed_per_iteration)); + Window win = calculate_max_window(output_shape, Steps(num_elems_processed_per_iteration)); return win; } - std::string ClFloorKernelComponent::get_component_code() const { - return R"_( + bool is_root = _blueprint->impl().group(_src.arg_id) == SharedVarGroup::Argument; + if(is_root) + { + return R"_( //------------------ START KERNEL {{meta_kernel_id}} FLOOR --------------------- // IN_0(src) {{src}} // OUT(dst, accum) {{dst}} @@ -69,30 +74,40 @@ std::string ClFloorKernelComponent::get_component_code() const { TILE({{DATA_TYPE}}, M0, N0, src_tile); + // Since mout maps to dimensions 1 (y) and dimension 2 (z) of the input tensor because of the collapsed window, bout maps to dimension 3 (w) + {{src}}_offset_first_element_in_bytes += bout * {{src}}_stride_w; T_LOAD({{DATA_TYPE}}, M0, N0, BUFFER, {{src}}, cout, mout, 1, {{src}}_stride_y, src_tile); + T_FLOOR({{DATA_TYPE}}, M0, N0, src_tile, {{dst}}); } - //------------------ END KERNEL {{meta_kernel_id}} FLOOR --------------------- )_"; + } + else + { + return R"_( + //------------------ START KERNEL {{meta_kernel_id}} FLOOR --------------------- + // IN_0/Out(Accumulator) {{acc}} + // output = floor(input) + { + T_FLOOR({{DATA_TYPE}}, M0, N0, {{acc}}, {{acc}}); + } + //------------------ END KERNEL {{meta_kernel_id}} FLOOR --------------------- +)_"; + } } - CLBuildOptions ClFloorKernelComponent::generate_build_options() const { - CLBuildOptions build_opts{}; - - const auto n0 = _blueprint->impl().get_execution_window().x().step(); - const auto m0 = _blueprint->impl().get_execution_window().y().step(); - + CLBuildOptions build_opts{}; + const auto n0 = _blueprint->impl().get_execution_window().x().step(); + const auto m0 = _blueprint->impl().get_execution_window().y().step(); const auto dst_info = _blueprint->impl().get_kernel_argument_info(_blueprint->impl().get_dst_id()); const unsigned int partial_store_n0 = dst_info->dimension(0) % n0; build_opts.add_option("-DM0=" + support::cpp11::to_string(m0)); build_opts.add_option("-DN0=" + support::cpp11::to_string(n0)); build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0)); - return build_opts; } - std::string ClFloorKernelComponent::generate_config_id() const { auto t_dst_info = _blueprint->impl().get_kernel_argument_info(_blueprint->impl().get_dst_id()); @@ -106,20 +121,28 @@ std::string ClFloorKernelComponent::generate_config_id() const config_id += lower_string(string_from_data_layout(t_dst_info->data_layout())); return config_id; } - void ClFloorKernelComponent::allocate_shared_vars(SharedVarTable &vtable) const { vtable.add(_src, _blueprint->impl().group(_src.arg_id), ClKernelArgDescriptor(_src.arg_id, ClKernelTensorArgType::Tensor_4D_t_Buffer), "src"); vtable.add(_dst, _blueprint->impl().group(_dst.arg_id), ClKernelArgDescriptor(_dst.arg_id, ClKernelTensorArgType::Tensor_4D_t_Buffer), "dst"); } - ClFloorKernelComponent::TagLUT ClFloorKernelComponent::get_tag_lut(const SharedVarTable &vtable) const { TagLUT lut{}; const auto t_dst_info = _blueprint->impl().get_kernel_argument_info(_blueprint->impl().get_dst_id()); // Arguments and global shared variables - lut["src"] = vtable.get(_src); - lut["dst"] = vtable.get(_dst); + const bool is_root = _blueprint->impl().group(_src.arg_id) == SharedVarGroup::Argument; + + if(is_root) + { + lut["src"] = vtable.get(_src); + lut["dst"] = vtable.get(_dst); + } + else + { + lut["acc"] = vtable.get(_src); + } + lut["meta_kernel_id"] = id(); lut["DATA_TYPE"] = get_cl_type_from_data_type(t_dst_info->data_type()); return lut; diff --git a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClFloorKernelComponent.h b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClFloorKernelComponent.h index 5463e233d4..e791b36382 100644 --- a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClFloorKernelComponent.h +++ b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClFloorKernelComponent.h @@ -37,6 +37,17 @@ namespace dynamic_fusion class ClFloorKernelComponent : public IClKernelComponent { public: + /** Construct a new Cl Floor Kernel Component object + * + * @param blueprint Blueprint to which this component is added + * @param src Link to SRC tensor + * @param dst Link to DST tensor + * + * Support Level + * Data Type: F16, F32 + * Tensor Shape: Any shape of arbitrary dimension >= 1 and <= 4 + * Value Range: All + */ ClFloorKernelComponent(ClKernelBlueprint *blueprint, const Link &src, const Link &dst) : IClKernelComponent(blueprint), _src{ src }, _dst{ dst } { @@ -71,4 +82,4 @@ private: } // namespace experimental } // namespace arm_compute #endif // ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_IMPL_COMPONENTS_CLFLOORKERNELCOMPONENT_H -#endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */ \ No newline at end of file +#endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */ diff --git a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClStoreKernelComponents.cpp b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClStoreKernelComponents.cpp index 4ac27e007f..7c805d5368 100644 --- a/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClStoreKernelComponents.cpp +++ b/src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClStoreKernelComponents.cpp @@ -108,6 +108,10 @@ std::string ClStoreIndirectWidthSelectKernelComponent::get_component_code() cons return R"_( //------------------ START KERNEL {{meta_kernel_id}} STORE --------------------- { + // This also follows NHWC layout + // cout maps to global_id(0) maps to Channel + // mout maps to global_id(1) maps to Height and Weight (Collapsed Window) + // bout maps to global_id(3) maps to N / Batch #define _IDST_WIDTH {{dst}}_w #define _IDST_HEIGHT {{dst}}_h TILE(uint, M0, 1, dst_indirect_y); -- cgit v1.2.1