From e920d6a62b8d96e08950e81bba769e8674d4921b Mon Sep 17 00:00:00 2001 From: Ramy Elgammal Date: Thu, 19 Aug 2021 22:10:30 +0100 Subject: Printing operators parameters, currently for CpuAdd operator only. Resolves: COMPMID-4718 Change-Id: Id4dd762cd1b759bb814b9d0b1ea0c9ba4dfbae6f Signed-off-by: Ramy Elgammal Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/6139 Reviewed-by: Giorgio Arena Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins --- arm_compute/core/utils/logging/Macros.h | 2 +- src/common/utils/Log.h | 112 ++++++++++++++++++++++++++++++++ src/cpu/operators/CpuAdd.cpp | 3 + utils/TypePrinter.h | 13 ++++ 4 files changed, 129 insertions(+), 1 deletion(-) diff --git a/arm_compute/core/utils/logging/Macros.h b/arm_compute/core/utils/logging/Macros.h index 4900bf9e6b..0ab17c4464 100644 --- a/arm_compute/core/utils/logging/Macros.h +++ b/arm_compute/core/utils/logging/Macros.h @@ -74,7 +74,7 @@ inline std::string signature_name(const std::string &pretty_func) { \ size_t size = ::snprintf(nullptr, 0, fmt, __VA_ARGS__) + 1; \ auto char_str = std::make_unique(size); \ - ::snprintf(char_str.get(), size, #fmt, __VA_ARGS__); \ + ::snprintf(char_str.get(), size, fmt, __VA_ARGS__); \ __logger->log(log_level, std::string(char_str.get(), char_str.get() + size - 1)); \ } \ } while(false) diff --git a/src/common/utils/Log.h b/src/common/utils/Log.h index cfbc95a627..89e86bf2fc 100644 --- a/src/common/utils/Log.h +++ b/src/common/utils/Log.h @@ -26,6 +26,7 @@ #include "arm_compute/core/Error.h" #include "arm_compute/core/utils/logging/Macros.h" +#include "utils/TypePrinter.h" #ifdef ARM_COMPUTE_LOGGING_ENABLED /** Create a logger @@ -102,4 +103,115 @@ 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 +inline typename std::enable_if::type +logParamsImpl(std::vector &data_registry, const std::tuple &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 +inline typename std::enable_if < Index::type +logParamsImpl(std::vector &data_registry, const std::tuple &in_params_tuple) +{ + data_registry.push_back(arm_compute::to_string(std::get(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 + * + * @return vector of the parameters' data in a string format + */ +template +const std::vector logParams(Ts... ins) +{ + std::vector data_registry{}; + std::tuple 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 getParamsNames(const std::string &in_params_str) +{ + std::stringstream ss(in_params_str); + + // Vector containing all the names of the input parameters + std::vector 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 ¶ms_names, + const std::vector &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(...) \ + ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL(constructDataLog(getParamsNames(#__VA_ARGS__), \ + logParams(__VA_ARGS__))); + #endif /* SRC_COMMON_LOG_H */ diff --git a/src/cpu/operators/CpuAdd.cpp b/src/cpu/operators/CpuAdd.cpp index 42a7b99ceb..755b1994ae 100644 --- a/src/cpu/operators/CpuAdd.cpp +++ b/src/cpu/operators/CpuAdd.cpp @@ -25,12 +25,15 @@ #include "src/cpu/kernels/CpuAddKernel.h" +#include "src/common/utils/Log.h" + namespace arm_compute { namespace cpu { void CpuAdd::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst, ConvertPolicy policy, const ActivationLayerInfo &act_info) { + ARM_COMPUTE_LOG_PARAMS(src0, src1, policy, act_info); ARM_COMPUTE_UNUSED(act_info); auto k = std::make_unique(); k->configure(src0, src1, dst, policy); diff --git a/utils/TypePrinter.h b/utils/TypePrinter.h index 3ccba2c7e9..58ddb3f7bf 100644 --- a/utils/TypePrinter.h +++ b/utils/TypePrinter.h @@ -1056,6 +1056,19 @@ inline std::string to_string(const TensorInfo &info) return str.str(); } +/** Formatted output of the ITensorInfo* type. + * + * @param[in] info Type to output. + * + * @return Formatted string. + */ +inline std::string to_string(const ITensorInfo *info) +{ + std::stringstream str; + str << info; + return str.str(); +} + /** Formatted output of the Dimensions type. * * @param[in] dimensions Type to output. -- cgit v1.2.1