From e357a254a830bf4d66f235813fac021d61cf225d Mon Sep 17 00:00:00 2001 From: SiCong Li Date: Sun, 9 Aug 2020 20:05:52 +0100 Subject: COMPMID-3454 Patch1: Relocate data_type_from_name to core/Utils Change-Id: I33436dab77a47868fbd9872e0b4cf54b3a173e65 Signed-off-by: SiCong Li Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/3694 Tested-by: Arm Jenkins Reviewed-by: Manuel Bottini Comments-Addressed: Arm Jenkins --- arm_compute/core/Utils.h | 21 ++++++ arm_compute/graph/TypeLoader.h | 25 +------ examples/gemm_tuner/CommonGemmExampleOptions.h | 2 +- src/core/Utils.cpp | 92 +++++++++++++++++--------- src/graph/TypeLoader.cpp | 26 +------- tests/validate_examples/cl_gemm.cpp | 37 +---------- utils/CommonGraphOptions.cpp | 1 + 7 files changed, 86 insertions(+), 118 deletions(-) diff --git a/arm_compute/core/Utils.h b/arm_compute/core/Utils.h index 7a1cc99127..13c2971930 100644 --- a/arm_compute/core/Utils.h +++ b/arm_compute/core/Utils.h @@ -1084,6 +1084,27 @@ const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType out * @return String representation of the PixelValue through the given data type. */ std::string string_from_pixel_value(const PixelValue &value, const DataType data_type); +/** Convert a string to DataType + * + * @param[in] name The name of the data type + * + * @return DataType + */ +DataType data_type_from_name(const std::string &name); +/** Input Stream operator for @ref DataType + * + * @param[in] stream Stream to parse + * @param[out] data_type Output data type + * + * @return Updated stream + */ +inline ::std::istream &operator>>(::std::istream &stream, DataType &data_type) +{ + std::string value; + stream >> value; + data_type = data_type_from_name(value); + return stream; +} /** Lower a given string. * * @param[in] val Given string to lower. diff --git a/arm_compute/graph/TypeLoader.h b/arm_compute/graph/TypeLoader.h index a53af40f77..286bfebeb5 100644 --- a/arm_compute/graph/TypeLoader.h +++ b/arm_compute/graph/TypeLoader.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2019 Arm Limited. + * Copyright (c) 2018-2020 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -30,29 +30,6 @@ namespace arm_compute { -/** Converts a string to a strong types enumeration @ref DataType - * - * @param[in] name String to convert - * - * @return Converted DataType enumeration - */ -arm_compute::DataType data_type_from_name(const std::string &name); - -/** Input Stream operator for @ref DataType - * - * @param[in] stream Stream to parse - * @param[out] data_type Output data type - * - * @return Updated stream - */ -inline ::std::istream &operator>>(::std::istream &stream, arm_compute::DataType &data_type) -{ - std::string value; - stream >> value; - data_type = data_type_from_name(value); - return stream; -} - /** Converts a string to a strong types enumeration @ref DataLayout * * @param[in] name String to convert diff --git a/examples/gemm_tuner/CommonGemmExampleOptions.h b/examples/gemm_tuner/CommonGemmExampleOptions.h index 04a8f22be6..5c4be286d6 100644 --- a/examples/gemm_tuner/CommonGemmExampleOptions.h +++ b/examples/gemm_tuner/CommonGemmExampleOptions.h @@ -25,7 +25,7 @@ #define ARM_COMPUTE_EXAMPLES_GEMM_TUNER_COMMON_GEMM_EXAMPLE_OPTIONS #include "arm_compute/core/Types.h" -#include "arm_compute/graph/TypeLoader.h" +#include "arm_compute/core/Utils.h" #include "utils/TypePrinter.h" #include "utils/command_line/CommandLineOptions.h" #include "utils/command_line/CommandLineParser.h" diff --git a/src/core/Utils.cpp b/src/core/Utils.cpp index cec7a1b4ba..3c8e735c40 100644 --- a/src/core/Utils.cpp +++ b/src/core/Utils.cpp @@ -33,9 +33,9 @@ #include #include -using namespace arm_compute; - -std::string arm_compute::read_file(const std::string &filename, bool binary) +namespace arm_compute +{ +std::string read_file(const std::string &filename, bool binary) { std::string out; std::ifstream fs; @@ -73,7 +73,7 @@ std::string arm_compute::read_file(const std::string &filename, bool binary) return out; } -const std::string &arm_compute::string_from_format(Format format) +const std::string &string_from_format(Format format) { static std::map formats_map = { @@ -99,7 +99,7 @@ const std::string &arm_compute::string_from_format(Format format) return formats_map[format]; } -const std::string &arm_compute::string_from_channel(Channel channel) +const std::string &string_from_channel(Channel channel) { static std::map channels_map = { @@ -120,7 +120,7 @@ const std::string &arm_compute::string_from_channel(Channel channel) return channels_map[channel]; } -const std::string &arm_compute::string_from_data_layout(DataLayout dl) +const std::string &string_from_data_layout(DataLayout dl) { static std::map dl_map = { @@ -132,7 +132,7 @@ const std::string &arm_compute::string_from_data_layout(DataLayout dl) return dl_map[dl]; } -const std::string &arm_compute::string_from_data_type(DataType dt) +const std::string &string_from_data_type(DataType dt) { static std::map dt_map = { @@ -160,7 +160,7 @@ const std::string &arm_compute::string_from_data_type(DataType dt) return dt_map[dt]; } -const std::string &arm_compute::string_from_activation_func(ActivationLayerInfo::ActivationFunction act) +const std::string &string_from_activation_func(ActivationLayerInfo::ActivationFunction act) { static std::map act_map = { @@ -184,7 +184,7 @@ const std::string &arm_compute::string_from_activation_func(ActivationLayerInfo: return act_map[act]; } -const std::string &arm_compute::string_from_matrix_pattern(MatrixPattern pattern) +const std::string &string_from_matrix_pattern(MatrixPattern pattern) { static std::map pattern_map = { @@ -197,7 +197,7 @@ const std::string &arm_compute::string_from_matrix_pattern(MatrixPattern pattern return pattern_map[pattern]; } -const std::string &arm_compute::string_from_non_linear_filter_function(NonLinearFilterFunction function) +const std::string &string_from_non_linear_filter_function(NonLinearFilterFunction function) { static std::map func_map = { @@ -209,7 +209,7 @@ const std::string &arm_compute::string_from_non_linear_filter_function(NonLinear return func_map[function]; } -const std::string &arm_compute::string_from_interpolation_policy(InterpolationPolicy policy) +const std::string &string_from_interpolation_policy(InterpolationPolicy policy) { static std::map interpolation_policy_map = { @@ -221,7 +221,7 @@ const std::string &arm_compute::string_from_interpolation_policy(InterpolationPo return interpolation_policy_map[policy]; } -const std::string &arm_compute::string_from_border_mode(BorderMode border_mode) +const std::string &string_from_border_mode(BorderMode border_mode) { static std::map border_mode_map = { @@ -233,7 +233,7 @@ const std::string &arm_compute::string_from_border_mode(BorderMode border_mode) return border_mode_map[border_mode]; } -const std::string &arm_compute::string_from_norm_type(NormType type) +const std::string &string_from_norm_type(NormType type) { static std::map norm_type_map = { @@ -245,7 +245,7 @@ const std::string &arm_compute::string_from_norm_type(NormType type) return norm_type_map[type]; } -const std::string &arm_compute::string_from_pooling_type(PoolingType type) +const std::string &string_from_pooling_type(PoolingType type) { static std::map pool_type_map = { @@ -257,7 +257,7 @@ const std::string &arm_compute::string_from_pooling_type(PoolingType type) return pool_type_map[type]; } -const std::string &arm_compute::string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage) +const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage) { static std::map output_stage_map = { @@ -270,7 +270,7 @@ const std::string &arm_compute::string_from_gemmlowp_output_stage(GEMMLowpOutput return output_stage_map[output_stage]; } -std::string arm_compute::string_from_pixel_value(const PixelValue &value, const DataType data_type) +std::string string_from_pixel_value(const PixelValue &value, const DataType data_type) { std::stringstream ss; std::string converted_string; @@ -323,21 +323,45 @@ std::string arm_compute::string_from_pixel_value(const PixelValue &value, const return converted_string; } -std::string arm_compute::lower_string(const std::string &val) +DataType data_type_from_name(const std::string &name) +{ + static const std::map data_types = + { + { "f16", DataType::F16 }, + { "f32", DataType::F32 }, + { "qasymm8", DataType::QASYMM8 }, + }; + +#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED + try + { +#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ + return data_types.at(utility::tolower(name)); + +#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED + } + catch(const std::out_of_range &) + { + ARM_COMPUTE_ERROR_VAR("Invalid data type name: %s", name.c_str()); + } +#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ +} + +std::string lower_string(const std::string &val) { std::string res = val; std::transform(res.begin(), res.end(), res.begin(), ::tolower); return res; } -PadStrideInfo arm_compute::calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout, const Size2D &dilation, - const DimensionRoundingType &rounding_type) +PadStrideInfo calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout, const Size2D &dilation, + const DimensionRoundingType &rounding_type) { const auto &strides = conv_info.stride(); ARM_COMPUTE_ERROR_ON_MSG((strides.first < 1 || strides.second < 1), "Stride values should be greater than or equal to 1."); - const unsigned int width_idx = arm_compute::get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); - const unsigned int height_idx = arm_compute::get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); + const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); + const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); const unsigned int in_width = input_shape[width_idx]; const unsigned int in_height = input_shape[height_idx]; const unsigned int kernel_width = weights_shape[width_idx]; @@ -372,9 +396,9 @@ PadStrideInfo arm_compute::calculate_same_pad(TensorShape input_shape, TensorSha return same_info; } -std::pair arm_compute::deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height, - unsigned int kernel_width, unsigned int kernel_height, - const PadStrideInfo &pad_stride_info) +std::pair deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height, + unsigned int kernel_width, unsigned int kernel_height, + const PadStrideInfo &pad_stride_info) { const unsigned int pad_left = pad_stride_info.pad_left(); const unsigned int pad_top = pad_stride_info.pad_top(); @@ -392,10 +416,10 @@ std::pair arm_compute::deconvolution_output_dimensio return std::make_pair(w, h); } -std::pair arm_compute::scaled_dimensions(int width, int height, - int kernel_width, int kernel_height, - const PadStrideInfo &pad_stride_info, - const Size2D &dilation) +std::pair scaled_dimensions(int width, int height, + int kernel_width, int kernel_height, + const PadStrideInfo &pad_stride_info, + const Size2D &dilation) { const int dilation_x = dilation.x(); const int dilation_y = dilation.y(); @@ -426,7 +450,7 @@ std::pair arm_compute::scaled_dimensions(int width, return std::make_pair(static_cast(w), static_cast(h)); } -bool arm_compute::needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis) +bool needs_serialized_reduction(ReductionOperation op, DataType dt, unsigned int axis) { const bool is_min_max = (op == ReductionOperation::MAX || op == ReductionOperation::MIN); const bool is_quantized_type = is_data_type_quantized(dt); @@ -435,7 +459,7 @@ bool arm_compute::needs_serialized_reduction(ReductionOperation op, DataType dt, return !is_first_dim || is_min_max || is_quantized_type; } -QuantizationInfo arm_compute::get_softmax_output_quantization_info(DataType input_type, bool is_log) +QuantizationInfo get_softmax_output_quantization_info(DataType input_type, bool is_log) { // Note: Output quantization info for softmax should always have // * Softmax with QASYMM8: scale = 1/256, offset = 0 @@ -456,7 +480,7 @@ QuantizationInfo arm_compute::get_softmax_output_quantization_info(DataType inpu return QuantizationInfo(1.f / 256, 0); } -std::pair arm_compute::get_quantized_activation_min_max(ActivationLayerInfo act_info, DataType data_type, UniformQuantizationInfo oq_info) +std::pair get_quantized_activation_min_max(ActivationLayerInfo act_info, DataType data_type, UniformQuantizationInfo oq_info) { const bool is_qasymm8_signed = is_data_type_quantized_asymmetric_signed(data_type); const auto a = act_info.a(); @@ -472,7 +496,7 @@ std::pair arm_compute::get_quantized_activation_min_max(Activa } #ifdef ARM_COMPUTE_ASSERTS_ENABLED -void arm_compute::print_consecutive_elements(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n, int stream_width, const std::string &element_delim) +void print_consecutive_elements(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n, int stream_width, const std::string &element_delim) { switch(dt) { @@ -514,7 +538,7 @@ void arm_compute::print_consecutive_elements(std::ostream &s, DataType dt, const } } -int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n) +int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n) { switch(dt) { @@ -548,3 +572,5 @@ int arm_compute::max_consecutive_elements_display_width(std::ostream &s, DataTyp return 0; } #endif /* ARM_COMPUTE_ASSERTS_ENABLED */ + +} // namespace arm_compute \ No newline at end of file diff --git a/src/graph/TypeLoader.cpp b/src/graph/TypeLoader.cpp index a1b3fd899c..7082d6b99e 100644 --- a/src/graph/TypeLoader.cpp +++ b/src/graph/TypeLoader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2019 Arm Limited. + * Copyright (c) 2018-2020 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -29,30 +29,6 @@ namespace arm_compute { -arm_compute::DataType data_type_from_name(const std::string &name) -{ - static const std::map data_types = - { - { "f16", DataType::F16 }, - { "f32", DataType::F32 }, - { "qasymm8", DataType::QASYMM8 }, - }; - -#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED - try - { -#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ - return data_types.at(arm_compute::utility::tolower(name)); - -#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED - } - catch(const std::out_of_range &) - { - throw std::invalid_argument(name); - } -#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ -} - arm_compute::DataLayout data_layout_from_name(const std::string &name) { static const std::map data_layouts = diff --git a/tests/validate_examples/cl_gemm.cpp b/tests/validate_examples/cl_gemm.cpp index 34895840e1..0e71f9d5bf 100644 --- a/tests/validate_examples/cl_gemm.cpp +++ b/tests/validate_examples/cl_gemm.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Arm Limited. + * Copyright (c) 2017-2020 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -26,6 +26,7 @@ #endif /* ARM_COMPUTE_CL */ #include "arm_compute/core/Types.h" +#include "arm_compute/core/Utils.h" #include "arm_compute/core/utils/quantization/AsymmHelpers.h" #include "arm_compute/runtime/CL/CLFunctions.h" #include "arm_compute/runtime/CL/CLScheduler.h" @@ -59,40 +60,6 @@ RelativeTolerance tolerance_f32(0.001f); /**< F32 Toleran RelativeTolerance tolerance_f16(half(0.2)); /**< F16 Tolerance value for comparing reference's output against implementation's output for floating point data types */ constexpr float tolerance_num_f16 = 0.02f; /**< F16 Tolerance number */ -namespace arm_compute -{ -DataType data_type_from_name(const std::string &name) -{ - static const std::map data_types = - { - { "f16", DataType::F16 }, - { "f32", DataType::F32 }, - { "qasymm8", DataType::QASYMM8 }, - }; - -#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED - try - { -#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ - return data_types.at(utility::tolower(name)); - -#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED - } - catch(const std::out_of_range &) - { - throw std::invalid_argument(name); - } -#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */ -} - -inline ::std::istream &operator>>(::std::istream &stream, DataType &data_type) -{ - std::string value; - stream >> value; - data_type = data_type_from_name(value); - return stream; -} -} // namespace arm_compute namespace { class GEMMCommandLineOptions final diff --git a/utils/CommonGraphOptions.cpp b/utils/CommonGraphOptions.cpp index bcfb865753..cae98726bb 100644 --- a/utils/CommonGraphOptions.cpp +++ b/utils/CommonGraphOptions.cpp @@ -23,6 +23,7 @@ */ #include "CommonGraphOptions.h" +#include "arm_compute/core/Utils.h" #include "arm_compute/graph/TypeLoader.h" #include "arm_compute/graph/TypePrinter.h" -- cgit v1.2.1