diff --git a/include/onnxruntime/core/common/common.h b/include/onnxruntime/core/common/common.h index 9071906337..4b1c350849 100644 --- a/include/onnxruntime/core/common/common.h +++ b/include/onnxruntime/core/common/common.h @@ -118,42 +118,42 @@ void LogRuntimeError(uint32_t session_id, const common::Status& status, const ch // Throw an exception with optional message. // NOTE: The arguments get streamed into a string via ostringstream::operator<< // DO NOT use a printf format string, as that will not work as you expect. -#define ORT_THROW(...) \ - do { \ - std::cerr << ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, \ - ::onnxruntime::MakeString(__VA_ARGS__)) \ - .what() \ - << std::endl; \ - abort(); \ +#define ORT_THROW(...) \ + do { \ + std::cerr << ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, \ + ::onnxruntime::MakeStringLite(__VA_ARGS__)) \ + .what() \ + << std::endl; \ + abort(); \ } while (false) // Just in order to mark things as not implemented. Do not use in final code. -#define ORT_NOT_IMPLEMENTED(...) \ - do { \ - std::cerr << ::onnxruntime::NotImplementedException(::onnxruntime::MakeString(__VA_ARGS__)) \ - .what() \ - << std::endl; \ - abort(); \ +#define ORT_NOT_IMPLEMENTED(...) \ + do { \ + std::cerr << ::onnxruntime::NotImplementedException(::onnxruntime::MakeStringLite(__VA_ARGS__)) \ + .what() \ + << std::endl; \ + abort(); \ } while (false) // Check condition. // NOTE: The arguments get streamed into a string via ostringstream::operator<< // DO NOT use a printf format string, as that will not work as you expect. -#define ORT_ENFORCE(condition, ...) \ - do { \ - if (!(condition)) { \ - std::cerr << ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, #condition, \ - ::onnxruntime::MakeString(__VA_ARGS__)) \ - .what() \ - << std::endl; \ - abort(); \ - } \ +#define ORT_ENFORCE(condition, ...) \ + do { \ + if (!(condition)) { \ + std::cerr << ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, #condition, \ + ::onnxruntime::MakeStringLite(__VA_ARGS__)) \ + .what() \ + << std::endl; \ + abort(); \ + } \ } while (false) -#define ORT_THROW_EX(ex, ...) \ - do { \ - std::cerr << #ex << "(" << ::onnxruntime::MakeString(__VA_ARGS__) << ")" << std::endl; \ - abort(); \ +#define ORT_THROW_EX(ex, ...) \ + do { \ + std::cerr << #ex << "(" << ::onnxruntime::MakeStringLite(__VA_ARGS__) << ")" << std::endl; \ + abort(); \ } while (false) #else @@ -168,11 +168,11 @@ void LogRuntimeError(uint32_t session_id, const common::Status& status, const ch // NOTE: The arguments get streamed into a string via ostringstream::operator<< // DO NOT use a printf format string, as that will not work as you expect. #define ORT_THROW(...) \ - throw ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, ::onnxruntime::MakeString(__VA_ARGS__)) + throw ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, ::onnxruntime::MakeStringLite(__VA_ARGS__)) // Just in order to mark things as not implemented. Do not use in final code. #define ORT_NOT_IMPLEMENTED(...) \ - throw ::onnxruntime::NotImplementedException(::onnxruntime::MakeString(__VA_ARGS__)) + throw ::onnxruntime::NotImplementedException(::onnxruntime::MakeStringLite(__VA_ARGS__)) // Check condition. // NOTE: The arguments get streamed into a string via ostringstream::operator<< @@ -180,7 +180,7 @@ void LogRuntimeError(uint32_t session_id, const common::Status& status, const ch #define ORT_ENFORCE(condition, ...) \ if (!(condition)) \ throw ::onnxruntime::OnnxRuntimeException(ORT_WHERE_WITH_STACK, #condition, \ - ::onnxruntime::MakeString(__VA_ARGS__)) + ::onnxruntime::MakeStringLite(__VA_ARGS__)) #define ORT_THROW_EX(ex, ...) \ throw ex(__VA_ARGS__) @@ -190,21 +190,21 @@ void LogRuntimeError(uint32_t session_id, const common::Status& status, const ch #define ORT_MAKE_STATUS(category, code, ...) \ ::onnxruntime::common::Status(::onnxruntime::common::category, \ ::onnxruntime::common::code, \ - ::onnxruntime::MakeString(__VA_ARGS__)) + ::onnxruntime::MakeStringLite(__VA_ARGS__)) // Check condition. if met, return status. -#define ORT_RETURN_IF(condition, ...) \ - if (condition) { \ - return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, \ - "Satisfied, but should not be: " #condition "\n", \ - ORT_WHERE.ToString(), ::onnxruntime::MakeString(__VA_ARGS__)); \ +#define ORT_RETURN_IF(condition, ...) \ + if (condition) { \ + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, \ + "Satisfied, but should not be: " #condition "\n", \ + ORT_WHERE.ToString(), ::onnxruntime::MakeStringLite(__VA_ARGS__)); \ } // Check condition. if not met, return status. -#define ORT_RETURN_IF_NOT(condition, ...) \ - if (!(condition)) { \ - return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Not satisfied: " #condition "\n", \ - ORT_WHERE.ToString(), ::onnxruntime::MakeString(__VA_ARGS__)); \ +#define ORT_RETURN_IF_NOT(condition, ...) \ + if (!(condition)) { \ + return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Not satisfied: " #condition "\n", \ + ORT_WHERE.ToString(), ::onnxruntime::MakeStringLite(__VA_ARGS__)); \ } // Macros to disable the copy and/or move ctor and assignment methods diff --git a/include/onnxruntime/core/common/string_utils.h b/include/onnxruntime/core/common/string_utils.h index bb3dd62cc9..fae0530485 100644 --- a/include/onnxruntime/core/common/string_utils.h +++ b/include/onnxruntime/core/common/string_utils.h @@ -50,6 +50,20 @@ std::string MakeString(const Args&... args) { return ss.str(); } +/** + * Makes a string by concatenating string representations of the arguments. + * This version uses the current locale. + */ +template +std::string MakeStringLite(const Args&... args) { + std::ostringstream ss; + // the following line causes a binary size increase + //ss.imbue(std::locale::classic()); + // just use the current locale for this lite version + detail::MakeStringImpl(ss, args...); + return ss.str(); +} + // MakeString versions for already-a-string types. inline std::string MakeString(const std::string& str) { diff --git a/onnxruntime/core/framework/execution_frame.cc b/onnxruntime/core/framework/execution_frame.cc index d6f67478bd..24f8492715 100644 --- a/onnxruntime/core/framework/execution_frame.cc +++ b/onnxruntime/core/framework/execution_frame.cc @@ -421,7 +421,7 @@ Status ExecutionFrame::AllocateMLValueTensorPreAllocateBuffer(OrtValue& ort_valu if (buffer_num_elements != required_num_elements) { // could be an allocation planner bug (less likely) or the model incorrectly uses something like 'None' // as a dim_param, or -1 in dim_value in multiple places making the planner think those shapes are equal. - auto message = onnxruntime::MakeString( + auto message = onnxruntime::MakeStringLite( "Shape mismatch attempting to re-use buffer. ", reuse_tensor->Shape(), " != ", shape, ". Validate usage of dim_value (values should be > 0) and " diff --git a/onnxruntime/core/framework/memcpy.cc b/onnxruntime/core/framework/memcpy.cc index 9c45a2044e..e409cc2802 100644 --- a/onnxruntime/core/framework/memcpy.cc +++ b/onnxruntime/core/framework/memcpy.cc @@ -16,11 +16,11 @@ Status Memcpy::Compute(OpKernelContext* ctx) const { Status retval = Info().GetDataTransferManager().CopyTensor(*X, *Y, Info().GetKernelDef().ExecQueueId()); if (!retval.IsOK()) { - LOGS(ctx->Logger(), ERROR) << MakeString(retval.ErrorMessage(), - " Copying ", Node().InputDefs()[0]->Name(), - " to ", Node().OutputDefs()[0]->Name(), - " Input shape:", X->Shape(), " Output shape:", Y->Shape(), - " X data:", X->DataRaw(), " Y data:", Y->DataRaw()); + LOGS(ctx->Logger(), ERROR) << MakeStringLite(retval.ErrorMessage(), + " Copying ", Node().InputDefs()[0]->Name(), + " to ", Node().OutputDefs()[0]->Name(), + " Input shape:", X->Shape(), " Output shape:", Y->Shape(), + " X data:", X->DataRaw(), " Y data:", Y->DataRaw()); } return retval; diff --git a/onnxruntime/core/framework/sequential_executor.cc b/onnxruntime/core/framework/sequential_executor.cc index 1e5e688146..5a5b3f1124 100644 --- a/onnxruntime/core/framework/sequential_executor.cc +++ b/onnxruntime/core/framework/sequential_executor.cc @@ -277,7 +277,7 @@ Status SequentialExecutor::Execute(const SessionState& session_state, const std: const std::string node_name_for_profiling = [&]() -> std::string { if (!is_profiler_enabled) return {}; // Derive something meaningful for profile traces and logs if node name field is blank in execution graph - return node.Name().empty() ? MakeString(node.OpType(), "_", node_index) : node.Name(); + return node.Name().empty() ? MakeStringLite(node.OpType(), "_", node_index) : node.Name(); }(); if (is_profiler_enabled) { @@ -303,7 +303,7 @@ Status SequentialExecutor::Execute(const SessionState& session_state, const std: #endif #ifdef ENABLE_NVTX_PROFILE profile::NvtxRangeCreator node_compute_range( - MakeString(node.OpType(), ".", node.Index(), "(", node.Name(), ")"), profile::Color::Blue); + MakeStringLite(node.OpType(), ".", node.Index(), "(", node.Name(), ")"), profile::Color::Blue); node_compute_range.Begin(); #endif ORT_TRY {