From 02c1a0c3debca7b9720fb954dc4a1d98ebc27918 Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Tue, 7 Jun 2022 15:48:12 +0800 Subject: [PATCH] fix(imperative/format): warn once when parameter without format been attached GitOrigin-RevId: 02174447f61e95c55b551ee0d0b71b56e716bd29 --- imperative/src/impl/transformations/format.cpp | 14 ++++++++------ imperative/src/include/megbrain/imperative/utils/helper.h | 7 +++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/imperative/src/impl/transformations/format.cpp b/imperative/src/impl/transformations/format.cpp index 287cd65c..f7e4f4a4 100644 --- a/imperative/src/impl/transformations/format.cpp +++ b/imperative/src/impl/transformations/format.cpp @@ -5,6 +5,8 @@ #include "megbrain/imperative/ops/autogen.h" #include "megbrain/imperative/ops/utility.h" +#include "megbrain/imperative/utils/helper.h" + namespace mgb { namespace imperative { @@ -664,9 +666,9 @@ ValueRefList FormatTransformation::apply_transformation( if (inp_ref) { return {FormatValue::make(inp_ref->format())}; } else { - mgb_log_warn( + MGE_CALL_ONCE(mgb_log_warn( "Not FormattedTensorValue input for GetFormat op: %s, %s", - op.to_string().c_str(), inputs[0].to_string().c_str()); + op.to_string().c_str(), inputs[0].to_string().c_str())); return {FormatValue::make(FT::DEFAULT)}; } } else if (auto* _op = op.as()) { @@ -700,9 +702,9 @@ ValueRefList FormatTransformation::apply_transformation( // make params(GradValue) as FormattedTensor return wrap_outputs(outputs, format); } else { - mgb_log_warn( + MGE_CALL_ONCE(mgb_log_warn( "Not FormattedTensorValue input for AttachGrad op: %s, %s", - op.to_string().c_str(), inputs[0].to_string().c_str()); + op.to_string().c_str(), inputs[0].to_string().c_str())); return imperative::apply(op, inputs); } } else if (auto* set_grad = op.as()) { @@ -723,9 +725,9 @@ ValueRefList FormatTransformation::apply_transformation( wrapped_outputs[i] = m_value_type.make(outputs[i], output_ref->format()); } else { - mgb_log_warn( + MGE_CALL_ONCE(mgb_log_warn( "Not FormattedTensorValue outputs for SetGrad op: %s, %s", - op.to_string().c_str(), inputs_[i].to_string().c_str()); + op.to_string().c_str(), inputs_[i].to_string().c_str())); wrapped_outputs[i] = m_value_type.make(outputs[i], FT::DEFAULT); } } diff --git a/imperative/src/include/megbrain/imperative/utils/helper.h b/imperative/src/include/megbrain/imperative/utils/helper.h index 8a633624..c646816b 100644 --- a/imperative/src/include/megbrain/imperative/utils/helper.h +++ b/imperative/src/include/megbrain/imperative/utils/helper.h @@ -2,6 +2,7 @@ #include #include +#include #include #include "megbrain/utils/metahelper.h" @@ -26,6 +27,12 @@ inline std::string quoted(std::string str) { return ss.str(); } +#define MGE_CALL_ONCE(...) \ + do { \ + static std::once_flag _once_flag; \ + std::call_once(_once_flag, [&] { __VA_ARGS__; }); \ + } while (false) + } // namespace imperative } // namespace mgb