diff options
Diffstat (limited to 'src/common/utils')
-rw-r--r-- | src/common/utils/LegacySupport.cpp | 81 | ||||
-rw-r--r-- | src/common/utils/LegacySupport.h | 11 | ||||
-rw-r--r-- | src/common/utils/Log.h | 176 | ||||
-rw-r--r-- | src/common/utils/Macros.h | 2 | ||||
-rw-r--r-- | src/common/utils/Object.h | 8 | ||||
-rw-r--r-- | src/common/utils/Utils.h | 9 | ||||
-rw-r--r-- | src/common/utils/Validate.h | 2 |
7 files changed, 244 insertions, 45 deletions
diff --git a/src/common/utils/LegacySupport.cpp b/src/common/utils/LegacySupport.cpp index 569b2abd89..102644227e 100644 --- a/src/common/utils/LegacySupport.cpp +++ b/src/common/utils/LegacySupport.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. + * Copyright (c) 2021, 2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -23,6 +23,8 @@ */ #include "src/common/utils/LegacySupport.h" +#include "arm_compute/function_info/ActivationLayerInfo.h" + namespace arm_compute { namespace detail @@ -31,7 +33,7 @@ namespace { DataType convert_to_legacy_data_type(AclDataType data_type) { - switch(data_type) + switch (data_type) { case AclDataType::AclFloat32: return DataType::F32; @@ -46,7 +48,7 @@ DataType convert_to_legacy_data_type(AclDataType data_type) AclDataType convert_to_c_data_type(DataType data_type) { - switch(data_type) + switch (data_type) { case DataType::F32: return AclDataType::AclFloat32; @@ -62,7 +64,7 @@ AclDataType convert_to_c_data_type(DataType data_type) TensorShape create_legacy_tensor_shape(int32_t ndims, int32_t *shape) { TensorShape legacy_shape{}; - for(int32_t d = 0; d < ndims; ++d) + for (int32_t d = 0; d < ndims; ++d) { legacy_shape.set(d, shape[d], false); } @@ -71,14 +73,14 @@ TensorShape create_legacy_tensor_shape(int32_t ndims, int32_t *shape) int32_t *create_tensor_shape_array(const TensorInfo &info) { const auto num_dims = info.num_dimensions(); - if(num_dims <= 0) + if (num_dims <= 0) { return nullptr; } int32_t *shape_array = new int32_t[num_dims]; - for(size_t d = 0; d < num_dims; ++d) + for (size_t d = 0; d < num_dims; ++d) { shape_array[d] = info.tensor_shape()[d]; } @@ -90,22 +92,71 @@ int32_t *create_tensor_shape_array(const TensorInfo &info) TensorInfo convert_to_legacy_tensor_info(const AclTensorDescriptor &desc) { TensorInfo legacy_desc; - legacy_desc.init(create_legacy_tensor_shape(desc.ndims, desc.shape), 1, convert_to_legacy_data_type(desc.data_type)); + legacy_desc.init(create_legacy_tensor_shape(desc.ndims, desc.shape), 1, + convert_to_legacy_data_type(desc.data_type)); return legacy_desc; } AclTensorDescriptor convert_to_descriptor(const TensorInfo &info) { const auto num_dims = info.num_dimensions(); - AclTensorDescriptor desc - { - static_cast<int32_t>(num_dims), - create_tensor_shape_array(info), - convert_to_c_data_type(info.data_type()), - nullptr, - 0 - }; + AclTensorDescriptor desc{static_cast<int32_t>(num_dims), create_tensor_shape_array(info), + convert_to_c_data_type(info.data_type()), nullptr, 0}; return desc; } + +ActivationLayerInfo convert_to_activation_info(const AclActivationDescriptor &desc) +{ + ActivationLayerInfo::ActivationFunction act; + switch (desc.type) + { + case AclActivationType::AclIdentity: + act = ActivationLayerInfo::ActivationFunction::IDENTITY; + break; + case AclActivationType::AclLogistic: + act = ActivationLayerInfo::ActivationFunction::LOGISTIC; + break; + case AclActivationType::AclTanh: + act = ActivationLayerInfo::ActivationFunction::TANH; + break; + case AclActivationType::AclRelu: + act = ActivationLayerInfo::ActivationFunction::RELU; + break; + case AclActivationType::AclBoundedRelu: + act = ActivationLayerInfo::ActivationFunction::BOUNDED_RELU; + break; + case AclActivationType::AclLuBoundedRelu: + act = ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU; + break; + case AclActivationType::AclLeakyRelu: + act = ActivationLayerInfo::ActivationFunction::LEAKY_RELU; + break; + case AclActivationType::AclSoftRelu: + act = ActivationLayerInfo::ActivationFunction::SOFT_RELU; + break; + case AclActivationType::AclElu: + act = ActivationLayerInfo::ActivationFunction::ELU; + break; + case AclActivationType::AclAbs: + act = ActivationLayerInfo::ActivationFunction::ABS; + break; + case AclActivationType::AclSquare: + act = ActivationLayerInfo::ActivationFunction::SQUARE; + break; + case AclActivationType::AclSqrt: + act = ActivationLayerInfo::ActivationFunction::SQRT; + break; + case AclActivationType::AclLinear: + act = ActivationLayerInfo::ActivationFunction::LINEAR; + break; + case AclActivationType::AclHardSwish: + act = ActivationLayerInfo::ActivationFunction::HARD_SWISH; + break; + default: + return ActivationLayerInfo(); + } + + return ActivationLayerInfo(act, desc.a, desc.b); +} } // namespace detail } // namespace arm_compute diff --git a/src/common/utils/LegacySupport.h b/src/common/utils/LegacySupport.h index c2cc1bc182..05a70fc2c6 100644 --- a/src/common/utils/LegacySupport.h +++ b/src/common/utils/LegacySupport.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. + * Copyright (c) 2021, 2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -26,6 +26,8 @@ #include "arm_compute/Acl.h" #include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/function_info/ActivationLayerInfo.h" namespace arm_compute { @@ -45,6 +47,13 @@ TensorInfo convert_to_legacy_tensor_info(const AclTensorDescriptor &desc); * @return A converted descriptor */ AclTensorDescriptor convert_to_descriptor(const TensorInfo &info); +/** Convert an AclActivation descriptor to an internal one + * + * @param[in] desc Descriptor to convert + * + * @return Legacy tensor meta-data + */ +ActivationLayerInfo convert_to_activation_info(const AclActivationDescriptor &desc); } // namespace detail } // namespace arm_compute diff --git a/src/common/utils/Log.h b/src/common/utils/Log.h index 496ee74a16..6ebfed366e 100644 --- a/src/common/utils/Log.h +++ b/src/common/utils/Log.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. + * Copyright (c) 2021,2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -24,25 +24,37 @@ #ifndef SRC_COMMON_LOG_H #define SRC_COMMON_LOG_H +#ifndef ARM_COMPUTE_LOGGING_ENABLED + +#define ARM_COMPUTE_CREATE_ACL_LOGGER() +#define ARM_COMPUTE_LOG_MSG_ACL(log_level, msg) +#define ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(log_level, fmt, ...) +#define ARM_COMPUTE_LOG_ERROR_ACL(msg) +#define ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL(msg) +#define ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL(msg) +#define ARM_COMPUTE_LOG_PARAMS(...) + +#else /* ARM_COMPUTE_LOGGING_ENABLED */ + #include "arm_compute/core/Error.h" #include "arm_compute/core/utils/logging/Macros.h" -#ifdef ARM_COMPUTE_LOGGING_ENABLED +#include "utils/TypePrinter.h" + /** Create a logger * * @note It will eventually create all default loggers in don't exist */ -#define ARM_COMPUTE_CREATE_ACL_LOGGER() \ - do \ - { \ - if(arm_compute::logging::LoggerRegistry::get().logger("ComputeLibrary") == nullptr) \ - { \ - arm_compute::logging::LoggerRegistry::get().create_logger("ComputeLibrary", arm_compute::logging::LogLevel::INFO); \ - } \ - } while(false) -#else /* ARM_COMPUTE_LOGGING_ENABLED */ -#define ARM_COMPUTE_CREATE_ACL_LOGGER() -#endif /* ARM_COMPUTE_LOGGING_ENABLED */ +#define ARM_COMPUTE_CREATE_ACL_LOGGER() \ + do \ + { \ + if (arm_compute::logging::LoggerRegistry::get().logger("ComputeLibrary") == nullptr) \ + { \ + arm_compute::logging::LoggerRegistry::get().create_logger("ComputeLibrary", \ + arm_compute::logging::LogLevel::INFO); \ + } \ + } while (false) + /** Log a message to the logger * * @param[in] log_level Logging level @@ -53,7 +65,8 @@ { \ ARM_COMPUTE_CREATE_ACL_LOGGER(); \ ARM_COMPUTE_LOG_MSG("ComputeLibrary", log_level, msg); \ - } while(false) + } while (false) + /** Log a message with format to the logger * * @param[in] log_level Logging level @@ -65,7 +78,8 @@ { \ ARM_COMPUTE_CREATE_ACL_LOGGER(); \ ARM_COMPUTE_LOG_MSG_WITH_FORMAT("ComputeLibrary", log_level, fmt, __VA_ARGS__); \ - } while(false) + } while (false) + /** Log an error message to the logger * * @param[in] msg Message to log @@ -75,7 +89,7 @@ { \ ARM_COMPUTE_CREATE_ACL_LOGGER(); \ ARM_COMPUTE_LOG_MSG("ComputeLibrary", arm_compute::logging::LogLevel::ERROR, msg); \ - } while(false) + } while (false) /** Log an error message to the logger with function name before the message * @@ -86,6 +100,134 @@ { \ ARM_COMPUTE_CREATE_ACL_LOGGER(); \ ARM_COMPUTE_LOG_MSG_WITH_FUNCNAME("ComputeLibrary", arm_compute::logging::LogLevel::ERROR, msg); \ - } while(false) + } while (false) + +/** Log an information message to the logger with function name before the message + * + * @param[in] msg Message to log + */ +#define ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL(msg) \ + do \ + { \ + ARM_COMPUTE_CREATE_ACL_LOGGER(); \ + ARM_COMPUTE_LOG_MSG_WITH_FUNCNAME("ComputeLibrary", arm_compute::logging::LogLevel::INFO, msg); \ + } while (false) +/** Function template specialization for the out of bound element at index = tuple_size + * + * @param[in,out] data_registry Reference to the input parameters data in a string format + * @param[in] in_params_tuple Tuple of different input data types + */ +template <std::size_t Index, typename... Tp> +inline typename std::enable_if<Index == sizeof...(Tp), void>::type +logParamsImpl(std::vector<std::string> &data_registry, const std::tuple<Tp...> &in_params_tuple) +{ + // Because it is out of bound index so do nothing + ARM_COMPUTE_UNUSED(data_registry); + ARM_COMPUTE_UNUSED(in_params_tuple); +} + +/** Function template to iterate over all input parameters tuple at compile time: + * + * @param[in,out] data_registry Reference to a vector of input parameters data in a string format + * @param[in] in_params_tuple Constant reference to a tuple of different input data types + */ +template <std::size_t Index, typename... Tp> + inline typename std::enable_if < + Index<sizeof...(Tp), void>::type logParamsImpl(std::vector<std::string> &data_registry, + const std::tuple<Tp...> &in_params_tuple) +{ + data_registry.push_back(arm_compute::to_string(std::get<Index>(in_params_tuple))); + // Unfold the next tuple element + logParamsImpl<Index + 1, Tp...>(data_registry, in_params_tuple); +} + +/** Function Template with variable number of inputs to collect all the passed parameters from + * the logging macro ARM_COMPUTE_LOG_PARAMS(...) + * + * @param[in] ...ins The input parameters in the variadic template, taken by universal references Ts.. &&, (not by value) + * to avoid detecting T as an abstract data type when passing any of these parameters as an L-value + * reference to an abstract type. + * + * @return Vector of the parameters' data in a string format + */ +template <typename... Ts> +const std::vector<std::string> logParams(Ts &&...ins) +{ + std::vector<std::string> data_registry{}; + std::tuple<Ts...> in_params_tuple{ins...}; + + // Start logging the tuple elements, starting from 0 to tuple_size-1 + logParamsImpl<0>(data_registry, in_params_tuple); + return data_registry; +} + +/** Inline function to parse the input parameters string passed from strignizing of the variadic macro input + * #__VA_ARGS__. + * It is Inline to avoid the redefinition of this function each time this header is included + * + * @param[in] in_params_str Constant reference to a string consists of the names of the input parameters provided + * as:ARM_COMPUTE_LOG_PARAMS(src0, src1) the params_names = "src0, src1" + * + * @return Vector of strings containing all the names of the input parameters + */ +inline const std::vector<std::string> getParamsNames(const std::string &in_params_str) +{ + std::stringstream ss(in_params_str); + + // Vector containing all the names of the input parameters + std::vector<std::string> names; + std::string temp; + + // Usually the input parameters string would be name of parameters separated + // by ',' e.g. "src0, src1, policy" + while (std::getline(ss, temp, ',')) + { + names.push_back(temp); + } + for (auto &name : names) + { + // Totally get rid of white space characters + name.erase(std::remove(name.begin(), name.end(), ' '), name.end()); + } + return names; +} + +/** It constructs the log message to be displayed by the logger by writing each parameter name and its + * corresponding data info string. + * + * @param[in] params_names Constant reference to a string consists of the the input parameters' names + * provided e.g.: ARM_COMPUTE_LOG_PARAMS(src0, src1) then params_names = "src0, src1" + * @param[in] data_registry Constant reference to a registry of all parameters' data in string format, + * stringnized by arm_compute::to_string() + * + * @return Log message string to be displayed + */ +inline const std::string constructDataLog(const std::vector<std::string> ¶ms_names, + const std::vector<std::string> &data_registry) +{ + std::string dataLog = "\n "; + ARM_COMPUTE_ERROR_ON(params_names.size() != data_registry.size()); + for (uint8_t i = 0; i < params_names.size(); ++i) + { + dataLog += params_names[i] + ": " + data_registry.at(i) + "\n "; + } + + return dataLog; +} + +/** Macro for logging input Parameters from any function. + * It detects the input parameters names, and their corresponding values before stringizing them using + * the overloaded arm_compute::to_string() type printer. Finally, displayed using the printer configured + * in the logger. + * + * @param[in] ... Input parameters + */ +#define ARM_COMPUTE_LOG_PARAMS(...) \ + do \ + { \ + ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL( \ + constructDataLog(getParamsNames(#__VA_ARGS__), logParams(__VA_ARGS__))); \ + } while (false) +#endif /* ARM_COMPUTE_LOGGING_ENABLED */ #endif /* SRC_COMMON_LOG_H */ diff --git a/src/common/utils/Macros.h b/src/common/utils/Macros.h index 2e44ea599e..35f7e759d3 100644 --- a/src/common/utils/Macros.h +++ b/src/common/utils/Macros.h @@ -28,7 +28,7 @@ #define ARM_COMPUTE_RETURN_CENUM_ON_FAILURE(status) \ { \ - if(status != arm_compute::StatusCode::Success) \ + if (status != arm_compute::StatusCode::Success) \ { \ return arm_compute::utils::as_cenum<AclStatus>(status); \ } \ diff --git a/src/common/utils/Object.h b/src/common/utils/Object.h index 1f194737d4..b73de8e430 100644 --- a/src/common/utils/Object.h +++ b/src/common/utils/Object.h @@ -52,14 +52,12 @@ struct Header * @param[in] type_ Object identification type * @param[in] ctx_ Context to reference */ - Header(ObjectType type_, IContext *ctx_) noexcept - : type(type_), - ctx(ctx_) + Header(ObjectType type_, IContext *ctx_) noexcept : type(type_), ctx(ctx_) { } - ObjectType type{ ObjectType::Invalid }; - IContext *ctx{ nullptr }; + ObjectType type{ObjectType::Invalid}; + IContext *ctx{nullptr}; }; } // namespace detail } // namespace arm_compute diff --git a/src/common/utils/Utils.h b/src/common/utils/Utils.h index 79f4f39c47..33fe6c0e81 100644 --- a/src/common/utils/Utils.h +++ b/src/common/utils/Utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. + * Copyright (c) 2021,2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -24,6 +24,8 @@ #ifndef SRC_COMMON_UTILS_H #define SRC_COMMON_UTILS_H +#include <algorithm> +#include <initializer_list> #include <type_traits> namespace arm_compute @@ -72,10 +74,7 @@ constexpr SE as_enum(const E val) noexcept template <typename E> bool is_in(E check, std::initializer_list<E> list) { - return std::any_of(std::cbegin(list), std::cend(list), [&check](E e) - { - return check == e; - }); + return std::any_of(list.begin(), list.end(), [&check](E e) { return check == e; }); } } // namespace utils } // namespace arm_compute diff --git a/src/common/utils/Validate.h b/src/common/utils/Validate.h index 4e8807273a..97819c619f 100644 --- a/src/common/utils/Validate.h +++ b/src/common/utils/Validate.h @@ -29,7 +29,7 @@ #include <cassert> -#define ARM_COMPUTE_ASSERT(cond) assert(cond) +#define ARM_COMPUTE_ASSERT(cond) assert(cond) #define ARM_COMPUTE_ASSERT_NOT_NULLPTR(ptr) assert((ptr) != nullptr) #else /* defined(ARM_COMPUTE_ASSERTS_ENABLED) */ |