From 7b8ef971613ac53ce6d312481ad8ec26ad54a2c7 Mon Sep 17 00:00:00 2001 From: liufengwei <2472937968@qq.com> Date: Fri, 13 Aug 2021 08:48:54 +0800 Subject: [PATCH 1/9] fix missing error info in XXError --- oneflow/core/common/error_util.cpp | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/oneflow/core/common/error_util.cpp b/oneflow/core/common/error_util.cpp index ce173dd8132..80f6ec71030 100644 --- a/oneflow/core/common/error_util.cpp +++ b/oneflow/core/common/error_util.cpp @@ -112,12 +112,48 @@ std::string FormatErrorSummaryAndMsg(const std::shared_ptr& err std::stringstream ss; if (error->has_error_summary()) { ss << error->error_summary(); } if (error->has_msg()) { ss << (ss.str().size() != 0 ? ", " + error->msg() : error->msg()); } + if (error->has_input_device_not_match_error()) { + std::stringstream input_device_not_match_ss; + input_device_not_match_ss << *error->mutable_input_device_not_match_error()->mutable_info(0); + ss << (ss.str().size() != 0 ? ", " + input_device_not_match_ss.str() + : input_device_not_match_ss.str()); + } + if (error->has_memory_zone_out_of_memory_error()) { + auto* memory_zone_out_of_memory_error = error->mutable_memory_zone_out_of_memory_error(); + std::stringstream memory_zone_out_of_memory_ss; + memory_zone_out_of_memory_ss << "machine_id: " + << *memory_zone_out_of_memory_error->mutable_machine_id(0); + memory_zone_out_of_memory_ss << ", mem_zone_id: " + << *memory_zone_out_of_memory_error->mutable_mem_zone_id(0); + memory_zone_out_of_memory_ss << ", device_tag: " + << *memory_zone_out_of_memory_error->mutable_device_tag(0); + memory_zone_out_of_memory_ss << ", required: " + << *memory_zone_out_of_memory_error->mutable_required(0); + memory_zone_out_of_memory_ss << ", available: " + << *memory_zone_out_of_memory_error->mutable_available(0) << "."; + ss << (ss.str().size() != 0 ? ", " + memory_zone_out_of_memory_ss.str() + : memory_zone_out_of_memory_ss.str()); + } + if (error->has_multiple_op_kernels_matched_error()) { + auto* multiple_op_kernels_matched_error = error->mutable_multiple_op_kernels_matched_error(); + std::stringstream multiple_op_kernels_matched_ss; + for (auto matched_op_kernels_debug_str = + multiple_op_kernels_matched_error->mutable_matched_op_kernels_debug_str()->begin(); + matched_op_kernels_debug_str + < multiple_op_kernels_matched_error->mutable_matched_op_kernels_debug_str()->end(); + matched_op_kernels_debug_str++) { + multiple_op_kernels_matched_ss << *matched_op_kernels_debug_str; + } + ss << (ss.str().size() != 0 ? ", " + multiple_op_kernels_matched_ss.str() + : multiple_op_kernels_matched_ss.str()); + } return ss.str(); } } // namespace Maybe FormatErrorStr(const std::shared_ptr& error) { + std::cout << "erro stack frame\n" << error->DebugString() << std::endl; std::stringstream ss; for (auto stack_frame = error->mutable_stack_frame()->rbegin(); stack_frame < error->mutable_stack_frame()->rend(); stack_frame++) { From 40c9ed7d78c8daf73fde67ca241e94ab7812ca8b Mon Sep 17 00:00:00 2001 From: liufengwei <2472937968@qq.com> Date: Fri, 13 Aug 2021 14:19:43 +0800 Subject: [PATCH 2/9] refine --- oneflow/core/common/error.cpp | 2 +- oneflow/core/common/error_util.cpp | 99 +++++++++++++++--------------- oneflow/core/common/error_util.h | 2 - 3 files changed, 52 insertions(+), 51 deletions(-) diff --git a/oneflow/core/common/error.cpp b/oneflow/core/common/error.cpp index d9c07160bdb..9ad09366ab8 100644 --- a/oneflow/core/common/error.cpp +++ b/oneflow/core/common/error.cpp @@ -274,7 +274,7 @@ Error Error::InputDeviceNotMatchError() { auto error = std::make_shared(); auto* input_device_not_match_error = error->mutable_input_device_not_match_error(); input_device_not_match_error->add_info( - std::string("The devices of input tensors are inconsistent,please try to use tensor.to or " + std::string("The devices of input tensors are inconsistent, please try to use tensor.to or " "module.to to correct it.")); return error; } diff --git a/oneflow/core/common/error_util.cpp b/oneflow/core/common/error_util.cpp index 80f6ec71030..dd28ddc7df3 100644 --- a/oneflow/core/common/error_util.cpp +++ b/oneflow/core/common/error_util.cpp @@ -15,6 +15,7 @@ limitations under the License. */ #include #include "oneflow/core/common/error_util.h" +#include "oneflow/core/common/util.h" namespace oneflow { @@ -41,7 +42,7 @@ std::string StripBrackets(std::string str) { return str; } -Maybe ShortenErrorMsg(std::string str) { +Maybe ShortenMsg(std::string str) { // 150 characters is the threshold const int num_displayed_char = 150; if (str.size() == 0) { return str; } @@ -80,27 +81,27 @@ Maybe ShortenErrorMsg(std::string str) { return ss.str(); } -std::string FormatFile(const std::string& file) { +std::string FormatFileOfStackFrame(const std::string& file) { std::stringstream ss; ss << "\n File \"" << file << "\", "; return ss.str(); } -std::string FormatLine(const int64_t& line) { +std::string FormatLineOfStackFrame(const int64_t& line) { std::stringstream ss; ss << "line " << line << ","; return ss.str(); } -std::string FormatFunction(const std::string& function) { +std::string FormatFunctionOfStackFrame(const std::string& function) { std::stringstream ss; ss << " in " << function; return ss.str(); } -Maybe FormatErrorMsg(std::string error_msg, bool is_last_stack_frame) { +Maybe FormatMsgOfInStackFrame(std::string error_msg, bool is_last_stack_frame) { error_msg = StripBrackets(error_msg); - if (!is_last_stack_frame) { error_msg = *JUST(ShortenErrorMsg(error_msg)); } + if (!is_last_stack_frame) { error_msg = *JUST(ShortenMsg(error_msg)); } // error_msg of last stack frame come from "<<" if (is_last_stack_frame) { error_msg = StripSpace(error_msg); } std::stringstream ss; @@ -108,44 +109,37 @@ Maybe FormatErrorMsg(std::string error_msg, bool is_last_stack_fram return ss.str(); } -std::string FormatErrorSummaryAndMsg(const std::shared_ptr& error) { +std::string FormatErrorSummaryAndMsgOfErrorProto(const std::shared_ptr& error) { std::stringstream ss; if (error->has_error_summary()) { ss << error->error_summary(); } - if (error->has_msg()) { ss << (ss.str().size() != 0 ? ", " + error->msg() : error->msg()); } - if (error->has_input_device_not_match_error()) { - std::stringstream input_device_not_match_ss; - input_device_not_match_ss << *error->mutable_input_device_not_match_error()->mutable_info(0); - ss << (ss.str().size() != 0 ? ", " + input_device_not_match_ss.str() - : input_device_not_match_ss.str()); - } - if (error->has_memory_zone_out_of_memory_error()) { - auto* memory_zone_out_of_memory_error = error->mutable_memory_zone_out_of_memory_error(); - std::stringstream memory_zone_out_of_memory_ss; - memory_zone_out_of_memory_ss << "machine_id: " - << *memory_zone_out_of_memory_error->mutable_machine_id(0); - memory_zone_out_of_memory_ss << ", mem_zone_id: " - << *memory_zone_out_of_memory_error->mutable_mem_zone_id(0); - memory_zone_out_of_memory_ss << ", device_tag: " - << *memory_zone_out_of_memory_error->mutable_device_tag(0); - memory_zone_out_of_memory_ss << ", required: " - << *memory_zone_out_of_memory_error->mutable_required(0); - memory_zone_out_of_memory_ss << ", available: " - << *memory_zone_out_of_memory_error->mutable_available(0) << "."; - ss << (ss.str().size() != 0 ? ", " + memory_zone_out_of_memory_ss.str() - : memory_zone_out_of_memory_ss.str()); - } - if (error->has_multiple_op_kernels_matched_error()) { - auto* multiple_op_kernels_matched_error = error->mutable_multiple_op_kernels_matched_error(); - std::stringstream multiple_op_kernels_matched_ss; - for (auto matched_op_kernels_debug_str = - multiple_op_kernels_matched_error->mutable_matched_op_kernels_debug_str()->begin(); - matched_op_kernels_debug_str - < multiple_op_kernels_matched_error->mutable_matched_op_kernels_debug_str()->end(); - matched_op_kernels_debug_str++) { - multiple_op_kernels_matched_ss << *matched_op_kernels_debug_str; - } - ss << (ss.str().size() != 0 ? ", " + multiple_op_kernels_matched_ss.str() - : multiple_op_kernels_matched_ss.str()); + if (error->has_msg()) { ss << (ss.str().size() != 0 ? "\n" + error->msg() : error->msg()); } + return ss.str(); +} + +// The msg in error type instance. +Maybe FormatMsgOfErrorType(const std::shared_ptr& error) { + std::stringstream ss; + CHECK_NE(error->error_type_case(), cfg::ErrorProto::ERROR_TYPE_NOT_SET); + switch (error->error_type_case()) { + case cfg::ErrorProto::kInputDeviceNotMatchError: + ss << error->input_device_not_match_error().DebugString(); + break; + case cfg::ErrorProto::kMemoryZoneOutOfMemoryError: + ss << error->memory_zone_out_of_memory_error().DebugString(); + break; + case cfg::ErrorProto::kMultipleOpKernelsMatchedError: + ss << error->multiple_op_kernels_matched_error().DebugString(); + break; + case cfg::ErrorProto::kOpKernelNotFoundError: + ss << error->op_kernel_not_found_error().DebugString(); + break; + case cfg::ErrorProto::kConfigResourceUnavailableError: + ss << error->config_resource_unavailable_error().DebugString(); + break; + case cfg::ErrorProto::kConfigAssertFailedError: + ss << error->config_assert_failed_error().DebugString(); + break; + default: OF_UNIMPLEMENTED(); } return ss.str(); } @@ -153,16 +147,25 @@ std::string FormatErrorSummaryAndMsg(const std::shared_ptr& err } // namespace Maybe FormatErrorStr(const std::shared_ptr& error) { - std::cout << "erro stack frame\n" << error->DebugString() << std::endl; std::stringstream ss; + // Get msg from stack frame of error proto for (auto stack_frame = error->mutable_stack_frame()->rbegin(); stack_frame < error->mutable_stack_frame()->rend(); stack_frame++) { - ss << FormatFile(*stack_frame->mutable_file()) << FormatLine(*stack_frame->mutable_line()) - << FormatFunction(*stack_frame->mutable_function()) - << *JUST(FormatErrorMsg(*stack_frame->mutable_error_msg(), - stack_frame == error->mutable_stack_frame()->rend() - 1)); + ss << FormatFileOfStackFrame(*stack_frame->mutable_file()) + << FormatLineOfStackFrame(*stack_frame->mutable_line()) + << FormatFunctionOfStackFrame(*stack_frame->mutable_function()) + << *JUST(FormatMsgOfInStackFrame(*stack_frame->mutable_error_msg(), + stack_frame == error->mutable_stack_frame()->rend() - 1)); } - ss << "\n" << FormatErrorSummaryAndMsg(error); + // Get msg from error summary and msg of error proto + std::string error_summary_and_msg_of_error_proto = FormatErrorSummaryAndMsgOfErrorProto(error); + if (error_summary_and_msg_of_error_proto.size() != 0) { + ss << "\n" << error_summary_and_msg_of_error_proto; + } + // Get msg from error type of error proto + std::string msg_of_error_type = *JUST(FormatMsgOfErrorType(error)); + if (msg_of_error_type.size() != 0) { ss << "\n" << msg_of_error_type; } + return ss.str(); } diff --git a/oneflow/core/common/error_util.h b/oneflow/core/common/error_util.h index aa25886dd9a..e1fc9f71ef0 100644 --- a/oneflow/core/common/error_util.h +++ b/oneflow/core/common/error_util.h @@ -24,8 +24,6 @@ namespace oneflow { namespace cfg { class ErrorProto; } -std::string* MutErrorStr(); -const std::string& GetErrorStr(); Maybe FormatErrorStr(const std::shared_ptr& error); } // namespace oneflow From d2078c9896e834456c55003af05156d73c2f83ee Mon Sep 17 00:00:00 2001 From: liufengwei <2472937968@qq.com> Date: Fri, 13 Aug 2021 14:28:26 +0800 Subject: [PATCH 3/9] Correct spelling mistake --- oneflow/core/common/error_util.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/oneflow/core/common/error_util.cpp b/oneflow/core/common/error_util.cpp index dd28ddc7df3..197361d879a 100644 --- a/oneflow/core/common/error_util.cpp +++ b/oneflow/core/common/error_util.cpp @@ -99,7 +99,7 @@ std::string FormatFunctionOfStackFrame(const std::string& function) { return ss.str(); } -Maybe FormatMsgOfInStackFrame(std::string error_msg, bool is_last_stack_frame) { +Maybe FormatMsgOfStackFrame(std::string error_msg, bool is_last_stack_frame) { error_msg = StripBrackets(error_msg); if (!is_last_stack_frame) { error_msg = *JUST(ShortenMsg(error_msg)); } // error_msg of last stack frame come from "<<" @@ -154,8 +154,8 @@ Maybe FormatErrorStr(const std::shared_ptr& error) ss << FormatFileOfStackFrame(*stack_frame->mutable_file()) << FormatLineOfStackFrame(*stack_frame->mutable_line()) << FormatFunctionOfStackFrame(*stack_frame->mutable_function()) - << *JUST(FormatMsgOfInStackFrame(*stack_frame->mutable_error_msg(), - stack_frame == error->mutable_stack_frame()->rend() - 1)); + << *JUST(FormatMsgOfStackFrame(*stack_frame->mutable_error_msg(), + stack_frame == error->mutable_stack_frame()->rend() - 1)); } // Get msg from error summary and msg of error proto std::string error_summary_and_msg_of_error_proto = FormatErrorSummaryAndMsgOfErrorProto(error); From 7349757570f23c77cca918f9cdc59f44e05743b0 Mon Sep 17 00:00:00 2001 From: liufengwei <2472937968@qq.com> Date: Fri, 13 Aug 2021 18:08:27 +0800 Subject: [PATCH 4/9] refine --- oneflow/core/common/error_util.cpp | 75 ++++++++++++------- .../user/ops/math_binary_broadcast_ops.cpp | 1 + 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/oneflow/core/common/error_util.cpp b/oneflow/core/common/error_util.cpp index 197361d879a..320c1ab5af0 100644 --- a/oneflow/core/common/error_util.cpp +++ b/oneflow/core/common/error_util.cpp @@ -44,61 +44,77 @@ std::string StripBrackets(std::string str) { Maybe ShortenMsg(std::string str) { // 150 characters is the threshold - const int num_displayed_char = 150; + const int character_num_threshold = 150; + const int num_displayed_char = 50; if (str.size() == 0) { return str; } // strip space when JUST( xx ); str = StripSpace(str); - if (str.size() < num_displayed_char) { return str; } - - // Find first index where the number of characters from the start to the index is less than 50, - // last index is the same - int first_index = -1; - int last_index = -1; - int pre_index = 0; - CHECK_OR_RETURN(str.size() >= 1); - for (int i = 1; i < str.size(); i++) { - if (IsLetterNumberOrUnderline(str.at(i)) && !IsLetterNumberOrUnderline(str.at(i - 1))) { - if (first_index == -1 && i >= num_displayed_char / 3) { first_index = pre_index; } - if (last_index == -1 && str.size() - i <= num_displayed_char / 3) { last_index = i; } - pre_index = i; + if (str.size() < character_num_threshold) { return str; } + + // left part whose number of characters is just over 50 + int left_index = num_displayed_char; + if (IsLetterNumberOrUnderline(str.at(left_index))) { + for (; left_index < str.size(); left_index++) { + if (!IsLetterNumberOrUnderline(str.at(left_index))) { break; } + } + } else { + for (; left_index < str.size(); left_index++) { + if (IsLetterNumberOrUnderline(str.at(left_index))) { break; } } } - // A string of more than 150 characters - if (first_index == -1 && last_index == -1) { return str; } - CHECK_OR_RETURN(first_index <= str.size()); - CHECK_OR_RETURN(last_index <= str.size()); - std::stringstream ss; - // The number of characters before the first word exceeds 50 - if (first_index == -1) { - ss << " ... " << str.substr(last_index); - } - // The number of characters after the last word exceeds 50 - else if (last_index == -1) { - ss << str.substr(0, first_index) << " ... "; + + // right part whose number of characters is just over 50 + int right_index = str.size() - num_displayed_char; + if (IsLetterNumberOrUnderline(str.at(right_index))) { + for (; right_index >= 0; right_index--) { + if (!IsLetterNumberOrUnderline(str.at(right_index))) { + right_index++; + break; + } + } } else { - ss << str.substr(0, first_index) << " ... " << str.substr(last_index); + for (; right_index >= 0; right_index--) { + if (IsLetterNumberOrUnderline(str.at(right_index))) { + right_index++; + break; + } + } } + // a long word of more than 150 + if (right_index - left_index < 50) { return str; } + std::stringstream ss; + CHECK_OR_RETURN(left_index >= 0); + CHECK_OR_RETURN(left_index < str.size()); + ss << str.substr(0, left_index); + ss << " ... "; + CHECK_OR_RETURN(right_index >= 0); + CHECK_OR_RETURN(right_index < str.size()); + ss << str.substr(right_index); return ss.str(); } +// file info in stack frame std::string FormatFileOfStackFrame(const std::string& file) { std::stringstream ss; ss << "\n File \"" << file << "\", "; return ss.str(); } +// line info in stack frame std::string FormatLineOfStackFrame(const int64_t& line) { std::stringstream ss; ss << "line " << line << ","; return ss.str(); } +// function info in stack frame std::string FormatFunctionOfStackFrame(const std::string& function) { std::stringstream ss; ss << " in " << function; return ss.str(); } +// msg in stack frame Maybe FormatMsgOfStackFrame(std::string error_msg, bool is_last_stack_frame) { error_msg = StripBrackets(error_msg); if (!is_last_stack_frame) { error_msg = *JUST(ShortenMsg(error_msg)); } @@ -109,6 +125,7 @@ Maybe FormatMsgOfStackFrame(std::string error_msg, bool is_last_sta return ss.str(); } +// the error_summary and msg in error proto std::string FormatErrorSummaryAndMsgOfErrorProto(const std::shared_ptr& error) { std::stringstream ss; if (error->has_error_summary()) { ss << error->error_summary(); } @@ -116,7 +133,7 @@ std::string FormatErrorSummaryAndMsgOfErrorProto(const std::shared_ptr FormatMsgOfErrorType(const std::shared_ptr& error) { std::stringstream ss; CHECK_NE(error->error_type_case(), cfg::ErrorProto::ERROR_TYPE_NOT_SET); @@ -139,7 +156,7 @@ Maybe FormatMsgOfErrorType(const std::shared_ptr& case cfg::ErrorProto::kConfigAssertFailedError: ss << error->config_assert_failed_error().DebugString(); break; - default: OF_UNIMPLEMENTED(); + default: ss << ""; } return ss.str(); } diff --git a/oneflow/user/ops/math_binary_broadcast_ops.cpp b/oneflow/user/ops/math_binary_broadcast_ops.cpp index 641781191c9..b56f2c0601e 100644 --- a/oneflow/user/ops/math_binary_broadcast_ops.cpp +++ b/oneflow/user/ops/math_binary_broadcast_ops.cpp @@ -53,6 +53,7 @@ Maybe InferTensorDescBinaryBroadcastNormal(user_op::InferContext* ctx) { } *tensor_z->mut_shape() = out_shape; } + CHECK_OR_RETURN(1 == 2) << " this is a test ! "; tensor_z->set_is_dynamic(tensor_x.is_dynamic() || tensor_y.is_dynamic()); return Maybe::Ok(); } From 7069792e63b228d26a23911a8151e0bb186c5a58 Mon Sep 17 00:00:00 2001 From: liufengwei <2472937968@qq.com> Date: Fri, 13 Aug 2021 18:22:19 +0800 Subject: [PATCH 5/9] fix bug --- oneflow/core/common/error_util.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/oneflow/core/common/error_util.cpp b/oneflow/core/common/error_util.cpp index 320c1ab5af0..7b28abe3509 100644 --- a/oneflow/core/common/error_util.cpp +++ b/oneflow/core/common/error_util.cpp @@ -25,8 +25,8 @@ std::string StripSpace(std::string str) { if (str.size() == 0) { return ""; } size_t pos = str.find_first_not_of(" "); if (pos != std::string::npos) { str.erase(0, pos); } - pos = str.find_last_not_of(" ") + 1; - if (pos != std::string::npos) { str.erase(pos); } + pos = str.find_last_not_of(" "); + if (pos != std::string::npos) { str.erase(pos + 1); } return str; } @@ -45,14 +45,14 @@ std::string StripBrackets(std::string str) { Maybe ShortenMsg(std::string str) { // 150 characters is the threshold const int character_num_threshold = 150; - const int num_displayed_char = 50; + const int displayed_char_num = 50; if (str.size() == 0) { return str; } // strip space when JUST( xx ); str = StripSpace(str); if (str.size() < character_num_threshold) { return str; } // left part whose number of characters is just over 50 - int left_index = num_displayed_char; + int left_index = displayed_char_num; if (IsLetterNumberOrUnderline(str.at(left_index))) { for (; left_index < str.size(); left_index++) { if (!IsLetterNumberOrUnderline(str.at(left_index))) { break; } @@ -64,7 +64,7 @@ Maybe ShortenMsg(std::string str) { } // right part whose number of characters is just over 50 - int right_index = str.size() - num_displayed_char; + int right_index = str.size() - displayed_char_num; if (IsLetterNumberOrUnderline(str.at(right_index))) { for (; right_index >= 0; right_index--) { if (!IsLetterNumberOrUnderline(str.at(right_index))) { From d35d7507af8351549efdd6575fbd6fcc9f429967 Mon Sep 17 00:00:00 2001 From: liufengwei <2472937968@qq.com> Date: Fri, 13 Aug 2021 18:24:03 +0800 Subject: [PATCH 6/9] refine --- oneflow/user/ops/math_binary_broadcast_ops.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/oneflow/user/ops/math_binary_broadcast_ops.cpp b/oneflow/user/ops/math_binary_broadcast_ops.cpp index b56f2c0601e..641781191c9 100644 --- a/oneflow/user/ops/math_binary_broadcast_ops.cpp +++ b/oneflow/user/ops/math_binary_broadcast_ops.cpp @@ -53,7 +53,6 @@ Maybe InferTensorDescBinaryBroadcastNormal(user_op::InferContext* ctx) { } *tensor_z->mut_shape() = out_shape; } - CHECK_OR_RETURN(1 == 2) << " this is a test ! "; tensor_z->set_is_dynamic(tensor_x.is_dynamic() || tensor_y.is_dynamic()); return Maybe::Ok(); } From 1d994d92ded725993b9c024fd3c9cbcb0301ae1f Mon Sep 17 00:00:00 2001 From: liufengwei <2472937968@qq.com> Date: Sun, 15 Aug 2021 18:40:18 +0800 Subject: [PATCH 7/9] Use protobuf's reflection to get error msg from error type --- oneflow/core/common/error_util.cpp | 35 ++++++++++-------------------- oneflow/core/common/error_util.h | 1 + 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/oneflow/core/common/error_util.cpp b/oneflow/core/common/error_util.cpp index 7b28abe3509..459f379a7d3 100644 --- a/oneflow/core/common/error_util.cpp +++ b/oneflow/core/common/error_util.cpp @@ -135,29 +135,19 @@ std::string FormatErrorSummaryAndMsgOfErrorProto(const std::shared_ptr FormatMsgOfErrorType(const std::shared_ptr& error) { + CHECK_NE_OR_RETURN(error->error_type_case(), cfg::ErrorProto::ERROR_TYPE_NOT_SET); std::stringstream ss; - CHECK_NE(error->error_type_case(), cfg::ErrorProto::ERROR_TYPE_NOT_SET); - switch (error->error_type_case()) { - case cfg::ErrorProto::kInputDeviceNotMatchError: - ss << error->input_device_not_match_error().DebugString(); - break; - case cfg::ErrorProto::kMemoryZoneOutOfMemoryError: - ss << error->memory_zone_out_of_memory_error().DebugString(); - break; - case cfg::ErrorProto::kMultipleOpKernelsMatchedError: - ss << error->multiple_op_kernels_matched_error().DebugString(); - break; - case cfg::ErrorProto::kOpKernelNotFoundError: - ss << error->op_kernel_not_found_error().DebugString(); - break; - case cfg::ErrorProto::kConfigResourceUnavailableError: - ss << error->config_resource_unavailable_error().DebugString(); - break; - case cfg::ErrorProto::kConfigAssertFailedError: - ss << error->config_assert_failed_error().DebugString(); - break; - default: ss << ""; - } + ErrorProto pb_error; + error->ToProto(&pb_error); + const google::protobuf::Descriptor* pb_error_des = pb_error.GetDescriptor(); + const google::protobuf::OneofDescriptor* oneof_field_des = + pb_error_des->FindOneofByName("error_type"); + const google::protobuf::Reflection* pb_error_ref = pb_error.GetReflection(); + const google::protobuf::FieldDescriptor* field_des = + pb_error_ref->GetOneofFieldDescriptor(pb_error, oneof_field_des); + CHECK_OR_RETURN(field_des != nullptr); + const google::protobuf::Message& error_type = pb_error_ref->GetMessage(pb_error, field_des); + ss << error_type.DebugString(); return ss.str(); } @@ -182,7 +172,6 @@ Maybe FormatErrorStr(const std::shared_ptr& error) // Get msg from error type of error proto std::string msg_of_error_type = *JUST(FormatMsgOfErrorType(error)); if (msg_of_error_type.size() != 0) { ss << "\n" << msg_of_error_type; } - return ss.str(); } diff --git a/oneflow/core/common/error_util.h b/oneflow/core/common/error_util.h index e1fc9f71ef0..d93de56c98d 100644 --- a/oneflow/core/common/error_util.h +++ b/oneflow/core/common/error_util.h @@ -18,6 +18,7 @@ limitations under the License. #include #include "oneflow/core/common/error.cfg.h" +#include "oneflow/core/common/error.pb.h" #include "oneflow/core/common/maybe.h" namespace oneflow { From dca53b8a2a93cabc5a14f1d669beae9c20b56404 Mon Sep 17 00:00:00 2001 From: liufengwei <2472937968@qq.com> Date: Tue, 17 Aug 2021 21:33:17 +0800 Subject: [PATCH 8/9] refine --- oneflow/core/common/error_util.cpp | 41 +++++++++++------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/oneflow/core/common/error_util.cpp b/oneflow/core/common/error_util.cpp index 459f379a7d3..ac42af623f7 100644 --- a/oneflow/core/common/error_util.cpp +++ b/oneflow/core/common/error_util.cpp @@ -44,40 +44,29 @@ std::string StripBrackets(std::string str) { Maybe ShortenMsg(std::string str) { // 150 characters is the threshold - const int character_num_threshold = 150; - const int displayed_char_num = 50; + const int num_character_threshold = 150; + const int num_displayed_character = 50; if (str.size() == 0) { return str; } // strip space when JUST( xx ); str = StripSpace(str); - if (str.size() < character_num_threshold) { return str; } + if (str.size() < num_character_threshold) { return str; } // left part whose number of characters is just over 50 - int left_index = displayed_char_num; - if (IsLetterNumberOrUnderline(str.at(left_index))) { - for (; left_index < str.size(); left_index++) { - if (!IsLetterNumberOrUnderline(str.at(left_index))) { break; } - } - } else { - for (; left_index < str.size(); left_index++) { - if (IsLetterNumberOrUnderline(str.at(left_index))) { break; } - } + int left_index = num_displayed_character; + bool pre_condition = IsLetterNumberOrUnderline(str.at(left_index)); + for (; left_index < str.size(); left_index++) { + bool cur_condition = IsLetterNumberOrUnderline(str.at(left_index)); + if ((pre_condition && !cur_condition) || (!pre_condition && cur_condition)) { break; } } // right part whose number of characters is just over 50 - int right_index = str.size() - displayed_char_num; - if (IsLetterNumberOrUnderline(str.at(right_index))) { - for (; right_index >= 0; right_index--) { - if (!IsLetterNumberOrUnderline(str.at(right_index))) { - right_index++; - break; - } - } - } else { - for (; right_index >= 0; right_index--) { - if (IsLetterNumberOrUnderline(str.at(right_index))) { - right_index++; - break; - } + int right_index = str.size() - num_displayed_character; + pre_condition = IsLetterNumberOrUnderline(str.at(right_index)); + for (; right_index >= 0; right_index--) { + bool cur_condition = IsLetterNumberOrUnderline(str.at(right_index)); + if ((pre_condition && cur_condition) || (!pre_condition && cur_condition)) { + right_index++; + break; } } // a long word of more than 150 From 081b85210a06c725cd261079bf455333b683e2e4 Mon Sep 17 00:00:00 2001 From: liufengwei <2472937968@qq.com> Date: Tue, 17 Aug 2021 21:36:23 +0800 Subject: [PATCH 9/9] fix bug --- oneflow/core/common/error_util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oneflow/core/common/error_util.cpp b/oneflow/core/common/error_util.cpp index ac42af623f7..180f423b212 100644 --- a/oneflow/core/common/error_util.cpp +++ b/oneflow/core/common/error_util.cpp @@ -64,7 +64,7 @@ Maybe ShortenMsg(std::string str) { pre_condition = IsLetterNumberOrUnderline(str.at(right_index)); for (; right_index >= 0; right_index--) { bool cur_condition = IsLetterNumberOrUnderline(str.at(right_index)); - if ((pre_condition && cur_condition) || (!pre_condition && cur_condition)) { + if ((pre_condition && !cur_condition) || (!pre_condition && cur_condition)) { right_index++; break; }