Add MakeStringLite which uses current locale, update some MakeString call sites to use it instead. (#6252)

* Add MakeStringLite which uses current locale, update macros to use that to generate messages.

* Convert calls to MakeStringLite().
This commit is contained in:
Edward Chen 2021-01-04 19:27:24 -08:00 committed by GitHub
parent 493bf931c5
commit ce6161cf67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 47 deletions

View file

@ -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

View file

@ -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 <typename... Args>
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) {

View file

@ -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 "

View file

@ -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;

View file

@ -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 {